-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
282 lines (220 loc) · 7.79 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include <iostream>
#include <SDL2/SDL.h>
#include <vector>
#include <string>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "external/imgui/imgui.h"
#include "external/imgui/backends/imgui_impl_sdl.h"
#include "external/imgui/backends/imgui_impl_sdlrenderer.h"
#include "external/imgui/misc/cpp/imgui_stdlib.h"
SDL_Renderer *renderer;
SDL_Window *window;
SDL_Event event;
struct CharSpriteMap
{
char c;
unsigned int sprite_sheet_row; // this assumes each row only contains 1 letter animation.
};
struct SpriteSheet
{
SDL_Texture *texture;
size_t n_rows;
size_t n_cols;
size_t cell_size;
};
struct AnimatedFontSprite
{
std::vector<CharSpriteMap> char_sprite_maps;
unsigned int char_pixel_width;
float seconds_per_frame;
float accumulator;
unsigned int curr_frame;
SpriteSheet sprite_sheet;
std::string text;
int color_mod[3];
unsigned int n_frames()
{
return sprite_sheet.n_cols;
}
void increment(float delta_time_seconds)
{
accumulator += delta_time_seconds;
if (accumulator >= seconds_per_frame)
{
accumulator = 0.0f;
curr_frame++;
}
if (curr_frame >= n_frames())
{
curr_frame = 0;
}
}
};
void AnimatedFontSprite_increment(AnimatedFontSprite &animated_font_sprite, float delta_time_seconds)
{
animated_font_sprite.accumulator += delta_time_seconds;
if (animated_font_sprite.accumulator >= animated_font_sprite.seconds_per_frame)
{
animated_font_sprite.accumulator = 0.0f;
animated_font_sprite.curr_frame++;
}
if (animated_font_sprite.curr_frame >= animated_font_sprite.n_frames())
{
animated_font_sprite.curr_frame = 0;
}
}
SDL_Rect AnimatedFontSprite_generate_src_rect_for_char(char c, AnimatedFontSprite font_sprite)
{
bool found = false;
unsigned int sprite_sheet_row;
SDL_Rect src_rect;
for (CharSpriteMap char_sprite_map: font_sprite.char_sprite_maps)
{
if (toupper(c) == toupper(char_sprite_map.c))
{
found = true;
sprite_sheet_row = char_sprite_map.sprite_sheet_row;
break;
}
}
const unsigned int not_found_char_row = 3;
if (found)
{
src_rect.x = font_sprite.sprite_sheet.cell_size * font_sprite.curr_frame;
src_rect.y = font_sprite.sprite_sheet.cell_size * sprite_sheet_row;
src_rect.w = src_rect.h = font_sprite.sprite_sheet.cell_size;
}
else
{
src_rect.x = font_sprite.sprite_sheet.cell_size * font_sprite.curr_frame;
src_rect.y = font_sprite.sprite_sheet.cell_size * not_found_char_row;
src_rect.w = src_rect.h = font_sprite.sprite_sheet.cell_size;
}
return src_rect;
}
void AnimatedFontSprite_render(int posx, int posy, AnimatedFontSprite animated_font_sprite)
{
SDL_SetTextureColorMod(animated_font_sprite.sprite_sheet.texture, animated_font_sprite.color_mod[0],
animated_font_sprite.color_mod[1], animated_font_sprite.color_mod[2]);
int row = 0;
int col = 0;
for (char c : animated_font_sprite.text)
{
if(c == '\n')
{
col = 0;
row++;
continue;
}
int renderx = posx + (col * animated_font_sprite.char_pixel_width);
int rendery = posy + (row * animated_font_sprite.char_pixel_width);
SDL_Rect src_rect = AnimatedFontSprite_generate_src_rect_for_char(c, animated_font_sprite);
SDL_Rect dest_rect = {
renderx,
rendery,
(int) animated_font_sprite.char_pixel_width,
(int) animated_font_sprite.char_pixel_width
};
SDL_RenderCopy(renderer, animated_font_sprite.sprite_sheet.texture, &src_rect, &dest_rect);
col++;
}
}
SpriteSheet
SpriteSheet_init(const std::string &filename, unsigned int n_rows, unsigned int n_cols, unsigned int cell_size)
{
int req_format = STBI_rgb_alpha;
int image_w, image_h, image_channels;
unsigned char *image_data = stbi_load(filename.c_str(), &image_w, &image_h, &image_channels, req_format);
int depth, pitch;
SDL_Surface *image_surface;
Uint32 pixel_format;
depth = 32;
pitch = 4 * image_w;
pixel_format = SDL_PIXELFORMAT_RGBA32;
image_surface = SDL_CreateRGBSurfaceWithFormatFrom(image_data, image_w, image_h, depth, pitch, pixel_format);
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, image_surface);
// SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_MOD);
if (texture == NULL)
{
fprintf(stderr, "%s\n", SDL_GetError());
}
SDL_FreeSurface(image_surface);
stbi_image_free(image_data);
SpriteSheet sprite_sheet;
sprite_sheet.texture = texture;
sprite_sheet.n_rows = n_rows;
sprite_sheet.n_cols = n_cols;
sprite_sheet.cell_size = cell_size;
return sprite_sheet;
}
int main()
{
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1440, 1440, 0);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
// IMGUI Setup
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO();
(void) io;
ImGui::StyleColorsDark();
ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
ImGui_ImplSDLRenderer_Init(renderer);
AnimatedFontSprite animated_font_sprite;
animated_font_sprite.sprite_sheet = SpriteSheet_init("fontSpriteSheet.png", 4, 4, 200);
animated_font_sprite.curr_frame = 0;
animated_font_sprite.accumulator = 0.0f;
animated_font_sprite.seconds_per_frame = 0.1;
animated_font_sprite.text = "LORD MAZE\nRULER OF MAZES";
animated_font_sprite.color_mod[0] = 255;
animated_font_sprite.color_mod[1] = 255;animated_font_sprite.color_mod[2] = 255;
animated_font_sprite.char_pixel_width = 100;
for (char i = 0; i <= 26; i++)
{
char c = 'A' + i;
unsigned int row = i;
animated_font_sprite.char_sprite_maps.push_back((CharSpriteMap) {c, row});
}
animated_font_sprite.char_sprite_maps.push_back((CharSpriteMap) {' ', 27});
double delta_time_in_sec = 0.0f;
double frame_start_seconds = 0.0f;
double frame_end_seconds = 0.0f;
bool quit = false;
while (!quit)
{
delta_time_in_sec = frame_end_seconds - frame_start_seconds;
frame_start_seconds = (double) SDL_GetTicks() / 1000.0f;
while (SDL_PollEvent(&event))
{
ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT)
{
quit = true;
}
}
ImGui_ImplSDLRenderer_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
if (ImGui::Begin("Animated Font Sprite"))
{
ImGui::SliderInt("R", (int *) &animated_font_sprite.color_mod[0], 0, 255);
ImGui::SliderInt("G", (int *) &animated_font_sprite.color_mod[1], 0, 255);
ImGui::SliderInt("B", (int *) &animated_font_sprite.color_mod[2], 0, 255 );
// ImGui::InputInt("G", (int *) &animated_font_sprite.color_mod.g);
// ImGui::InputInt("B", (int *) &animated_font_sprite.color_mod.b);
// // add line
ImGui::InputTextMultiline("text", &animated_font_sprite.text);
}
ImGui::End();
animated_font_sprite.increment(delta_time_in_sec);
SDL_SetRenderDrawColor(renderer, 50, 50, 50, 255);
SDL_RenderClear(renderer);
AnimatedFontSprite_render(10, 10, animated_font_sprite);
ImGui::Render();
ImGui_ImplSDLRenderer_RenderDrawData(ImGui::GetDrawData());
SDL_RenderPresent(renderer);
frame_end_seconds = (double) SDL_GetTicks() / 1000.0f;
}
std::cout << "Hello, World!" << std::endl;
return 0;
}