-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
462 lines (352 loc) · 10.9 KB
/
main.c
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
// Types
//---------------------------------------------------------------------------------------------------
typedef struct InitSDLValues_s
{
SDL_Window* Window;
SDL_Renderer* Renderer;
} InitSDLValues_t;
typedef struct IntVec2
{
int X; // X coordinate or column number
int Y; // Y coordinate or row number
} IntVec2_t;
// Constants
//---------------------------------------------------------------------------------------------------
# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
// change this to change the size of the window
const IntVec2_t cScreenResolution = {640, 480};
// this is more FYI than anything
const int cFPS = 60;
// 1/60 is 0.016666666666666666
const int cFrameDuration_ms = 16;
// the only reason I stopped at the letter 'L' is because I had a very hard time drawing a letter M that was recognizable,
// to be honest I was mostly just interested in drawing '0' through '9'
#define FONT_IMAGE_COUNT 23
// change this to change the amount of spacing between characters in the string
const int cBetweenCharSpacing_px = 2;
// Unfortunately it's not as simple as "Look for letter X in X.png", because symbols like ? are not valid file names in some OSes
const char* cExpectedLetterFileNames[] =
{
"0.png",
"1.png",
"2.png",
"3.png",
"4.png",
"5.png",
"6.png",
"7.png",
"8.png",
"9.png",
"A.png",
"B.png",
"C.png",
"D.png",
"E.png",
"F.png",
"G.png",
"H.png",
"I.png",
"J.png",
"K.png",
"L.png",
// ... you can add all the other characters here if you want.
"QMark.png"
};
const int cQuestionMarkIndex = 22;
// Globals
//---------------------------------------------------------------------------------------------------
// various SDL resources required for rendering.
InitSDLValues_t SDLGlobals;
// The texture to render to, this is copied to the screen
SDL_Texture* RenderTexture = NULL;
// The size of each letter texture
IntVec2_t LetterSizes[FONT_IMAGE_COUNT];
// The texture for each letter
SDL_Texture* LetterTextures[FONT_IMAGE_COUNT];
static_assert(ARRAY_SIZE(cExpectedLetterFileNames) == ARRAY_SIZE(LetterTextures));
int TypeOn_CharacterIndex = 1;
// note: since this demo runs at 60 FPS, this will be 4 characters typed on per second
int TypeOn_AdvanceTics = 15;
unsigned int FrameIndex = 0;
// Functions
//---------------------------------------------------------------------------------------------------
int min(int a, int b)
{
if(a < b)
{
return a;
}
return b;
}
IntVec2_t InquireTextureSize(SDL_Texture* texture)
{
int width, height;
SDL_QueryTexture(texture, NULL, NULL, &width, &height);
IntVec2_t size = {width, height};
return size;
}
SDL_Texture* LoadImage(SDL_Renderer *renderer, const char* path)
{
SDL_Texture *texture = NULL;
SDL_Surface* image = IMG_Load(path);
if (image != NULL)
{
texture = SDL_CreateTextureFromSurface(renderer, image);
SDL_FreeSurface(image);
image = NULL;
}
else
{
printf("Image '%s' could not be loaded. SDL Error: %s\n", path, SDL_GetError());
}
return texture;
}
// this function expects the images to be in a folder in the same directory as this file
void LoadLetterTextures(const char* folderPath)
{
printf("Looking for images in folder %s\n", folderPath);
chdir(folderPath);
for(int fileIndex = 0; fileIndex < ARRAY_SIZE(LetterTextures); fileIndex++)
{
const char* filePath = cExpectedLetterFileNames[fileIndex];
SDL_Texture* texture = LoadImage(SDLGlobals.Renderer, filePath);
LetterTextures[fileIndex] = texture;
LetterSizes[fileIndex] = InquireTextureSize(texture);
}
chdir("..");
printf("Done loading images.\n");
}
void FreeAllLetterTextures(void)
{
for(int fileIndex = 0; fileIndex < ARRAY_SIZE(LetterTextures); fileIndex++)
{
if(LetterTextures[fileIndex] != NULL)
{
SDL_DestroyTexture(LetterTextures[fileIndex]);
LetterTextures[fileIndex] = NULL;
IntVec2_t zeroSize = {0, 0};
LetterSizes[fileIndex] = zeroSize;
}
}
}
void DrawTexture(SDL_Texture *texture, IntVec2_t textureSize, IntVec2_t screenCoord)
{
SDL_Rect textureRectangle;
textureRectangle.x = 0;
textureRectangle.y = 0;
textureRectangle.w = textureSize.X;
textureRectangle.h = textureSize.Y;
SDL_Rect screenRectangle;
screenRectangle.x = screenCoord.X;
screenRectangle.y = screenCoord.Y;
screenRectangle.w = textureSize.X;
screenRectangle.h = textureSize.Y;
SDL_RenderCopy(SDLGlobals.Renderer, texture, &textureRectangle, &screenRectangle);
}
// uses ? for unknown characters
int TextureIndexForChar(char ch)
{
// http://www.asciitable.com/
if(ch < '0')
{
return cQuestionMarkIndex;
}
if(ch <='9')
{
return (ch - '0');
}
if(ch < 'A')
{
return cQuestionMarkIndex;
}
if(ch < 'M')
{
return ((ch - 'A') + 10);
}
return cQuestionMarkIndex;
}
void DrawChar(IntVec2_t topLeftCorner, char ch)
{
int charIndex = TextureIndexForChar(ch);
assert(charIndex >= 0);
assert(charIndex < ARRAY_SIZE(LetterTextures));
SDL_Texture* charTexture = LetterTextures[charIndex];
IntVec2_t size = LetterSizes[charIndex];
DrawTexture(charTexture, size, topLeftCorner);
}
void RenderString_TypeOn(IntVec2_t startPosition, const char* text, int textLength)
{
if(textLength < 1)
{
return;
}
int stopIndex = min(textLength, TypeOn_CharacterIndex);
int stringCharIndex = 0;
int xPosition = startPosition.X;
for(stringCharIndex = 0; stringCharIndex < stopIndex; stringCharIndex++)
{
IntVec2_t position = {xPosition, startPosition.Y};
const char ch = text[stringCharIndex];
DrawChar(position, ch);
int charID = TextureIndexForChar(ch);
IntVec2_t size = LetterSizes[charID];
xPosition += size.X;
xPosition += cBetweenCharSpacing_px;
int breakpoint = 1;
}
int shouldAdvanceChar = (FrameIndex % TypeOn_AdvanceTics) == 0;
if(shouldAdvanceChar)
{
if(TypeOn_CharacterIndex < textLength)
{
TypeOn_CharacterIndex++;
}
else
{
TypeOn_CharacterIndex = 1;
printf("Type on reset to first character\n");
}
}
}
// note that only letters you have textures for will actually work,
// don't try and render the string "%$^@#&~@abcd||__ unless you want to provide characters for that
// note that space (' ') also needs its own character
void RenderString(IntVec2_t startPosition, const char* text, int textLength)
{
if(textLength < 1)
{
return;
}
int stringCharIndex = 0;
int xPosition = startPosition.X;;
for(stringCharIndex = 0; stringCharIndex < textLength; stringCharIndex++)
{
IntVec2_t position = {xPosition, startPosition.Y};
const char ch = text[stringCharIndex];
DrawChar(position, ch);
int charID = TextureIndexForChar(ch);
IntVec2_t size = LetterSizes[charID];
xPosition += size.X;
xPosition += cBetweenCharSpacing_px;
int breakpoint = 1;
}
}
const InitSDLValues_t InitSDL(IntVec2_t windowSize_px)
{
InitSDLValues_t sdlInitResult = {NULL, NULL};
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
return sdlInitResult;
}
// Init the window
SDL_Window* window = SDL_CreateWindow("Font Demo!", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowSize_px.X, windowSize_px.Y, SDL_WINDOW_SHOWN);
if (!window)
{
printf("An error occured while trying to create window : %s\n", SDL_GetError());
return sdlInitResult;
}
// Init the renderer
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer)
{
printf("An error occured while trying to create renderer : %s\n", SDL_GetError());
return sdlInitResult;
}
sdlInitResult.Window = window;
sdlInitResult.Renderer = renderer;
return sdlInitResult;
}
// just checks for a quit signal. You can put more keypresses and mouse button handlers here if you want.
// returns 1 on quit, returns 0 otherwise
int HandleInput()
{
SDL_Event event = {0};
while (SDL_PollEvent(&event))
{
// E.g., from hitting the close window button
if (event.type == SDL_QUIT)
{
return 1;
}
}
return 0;
}
void Render(void)
{
// Render the string to the texture
SDL_SetRenderTarget(SDLGlobals.Renderer, RenderTexture);
// clear to something that isn't black, because our text is black
SDL_SetRenderDrawColor(SDLGlobals.Renderer, 255, 200, 200, 255);
SDL_RenderClear(SDLGlobals.Renderer);
{
IntVec2_t stringPosition = {32, 64};
const char* testString = "A0A0A0BE334C3D0C";
const int length = strlen(testString);
RenderString(stringPosition, testString, length);
}
{
IntVec2_t stringPosition = {32, 128};
const char* testString2 = "ABCDEFGHIJKL";
const int length2 = strlen(testString2);
RenderString_TypeOn(stringPosition, testString2, length2);
}
SDL_RenderPresent(SDLGlobals.Renderer);
// now render the render texture to the screen
SDL_SetRenderTarget(SDLGlobals.Renderer, NULL);
SDL_Rect fullScreenRectangle = {0};
fullScreenRectangle.w = cScreenResolution.X;
fullScreenRectangle.h = cScreenResolution.Y;
fullScreenRectangle.x = 0;
fullScreenRectangle.y = 0;
SDL_RenderCopy(SDLGlobals.Renderer, RenderTexture, &fullScreenRectangle, &fullScreenRectangle);
}
void FrameDelay(unsigned int targetTicks)
{
// Block at 60 fps
// ticks is in ms
unsigned int ticks = SDL_GetTicks();
if (targetTicks < ticks)
{
return;
}
if (targetTicks > ticks + cFrameDuration_ms)
{
SDL_Delay(cFrameDuration_ms);
}
else
{
SDL_Delay(targetTicks - ticks);
}
}
int main()
{
// initialization
SDLGlobals = InitSDL(cScreenResolution);
RenderTexture = SDL_CreateTexture(SDLGlobals.Renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, cScreenResolution.X, cScreenResolution.Y);
LoadLetterTextures("LetterImages");
// main loop
unsigned int targetTicks = SDL_GetTicks() + cFrameDuration_ms;
while(1)
{
int quitSignal = HandleInput();
if(quitSignal)
{
break;
}
Render();
FrameDelay(targetTicks);
targetTicks = SDL_GetTicks() + cFrameDuration_ms;
FrameIndex++;
}
// after quit: clean up resources
FreeAllLetterTextures();
SDL_DestroyTexture(RenderTexture);
RenderTexture = NULL;
return 0;
}