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
|
#include "main.h"
#include "interface.h"
#include "font.h"
#include "image.h"
static TTF_Font *g_font;
static int fontSize;
static bool_t isFontInit = FALSE;
bool_t isFontInicialized()
{
return isFontInit;
}
void initFont(char *file, int size)
{
char str[STR_PATH_SIZE];
assert(file != NULL);
assert(size > 0);
assert(isInterfaceInicialized() == TRUE);
sprintf(str, "%s", file);
if (TTF_Init() == -1) {
fprintf(stderr, "%s\n", SDL_GetError());
return;
}
accessExistFile(str);
g_font = TTF_OpenFont(str, size);
TTF_SetFontStyle(g_font, TTF_STYLE_NORMAL);
fontSize = size;
#ifdef DEBUG
printf(_("Loading font: \"%s\"\n"), file);
#endif
isFontInit = TRUE;
}
/*
* Zobrazi text *string na suradnicu x y s farbou RGB
*/
void drawFont(char *string, int x, int y, int r, int g, int b)
{
SDL_Surface *text;
image_t *image;
SDL_Color font_color = {r, g, b, SDL_ALPHA_OPAQUE};
assert( string != NULL );
text = TTF_RenderUTF8_Blended(g_font, string, font_color);
// because if string=="" TTF_RenderUTF8_Blended returns NULL
if( text != NULL ){
image = newImage(text);
drawImage(image, x, y, 0, 0, image->w, image->h);
destroyImage(image);
#ifdef SUPPORT_OPENGL
SDL_FreeSurface(text); // we don't need text anymore
#endif
};
}
void drawFontMaxSize(char *s, int x, int y, int w, int h, int r, int g, int b)
{
SDL_Rect src_rect, dst_rect;
SDL_Surface *text;
image_t *i;
SDL_Color font_color = {r, g, b, SDL_ALPHA_OPAQUE};
int my_w, my_h;
assert(s != NULL);
text = TTF_RenderUTF8_Blended(g_font, s, font_color);
my_w = text->w;
if (my_w > w) {
my_w = w;
}
my_h = text->h;
if (my_w > w) {
my_h = h;
}
src_rect.x = 0;
src_rect.y = 0;
src_rect.w = my_w;
src_rect.h = my_h;
dst_rect.x = x;
dst_rect.y = y;
i = newImage(text);
//posibly broken
drawImage(i, x, y, 0, 0, my_w, my_h);
destroyImage(i);
#ifdef SUPPORT_OPENGL
SDL_FreeSurface(text); // we don't need text anymore
#endif
}
/*
* Vrati velkost daneho fontu.
*/
int getFontSize()
{
return fontSize;
}
void getTextSize(char *s, int *w, int *h)
{
assert(s != NULL);
assert(w != NULL);
assert(h != NULL);
TTF_SizeUTF8(g_font, s, w, h);
}
/*
* Uvolni font z pamete.
*/
void quitFont()
{
TTF_CloseFont(g_font);
TTF_Quit();
#ifdef DEBUG
printf(_("Unloading font\n"));
#endif
isFontInit = FALSE;
}
|