-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.c
54 lines (41 loc) · 1.06 KB
/
text.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
#include "text.h"
static SDL_Texture* fontTexture;
static char drawTextBuffer[MAX_LINE_LENGTH];
void initFonts(void)
{
fontTexture = loadTexture("gfx/font.png");
}
void drawText(int x, int y, int r, int g, int b, double scale, int align, char* textToFormat, ...)
{
int i, len, c;
SDL_Rect rect; /* to specify what region of the texture to use */
va_list args;
memset(&drawTextBuffer, '\0', sizeof(drawTextBuffer));
va_start(args, textToFormat);
vsprintf(drawTextBuffer, textToFormat, args);
va_end(args);
len = strlen(drawTextBuffer);
switch (align)
{
case TEXT_RIGHT:
x -= (len * GLYPH_WIDTH);
break;
case TEXT_CENTER:
x -= (len * GLYPH_WIDTH) / 2;
break;
}
rect.w = GLYPH_WIDTH;
rect.h = GLYPH_HEIGHT;
rect.y = 0;
SDL_SetTextureColorMod(fontTexture, r, g, b);
for (i = 0; i < len; i++)
{
c = drawTextBuffer[i];
if (c >= ' ' && c <= 'Z')
{
rect.x = (c - ' ') * GLYPH_WIDTH; /* L'espace a la première position dans la texture des glyphes, valeur 0. */
blitRectScale(fontTexture, &rect, x, y, scale);
x += GLYPH_WIDTH;
}
}
}