1 /*
2
3 TestFonts.c: test dynamic font loading code
4
5 (C) A. Schiffler, August 2001, zlib License
6
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <math.h>
13 #include <time.h>
14
15 #include "SDL.h"
16
17 #ifdef WIN32
18 #include <windows.h>
19 #include "SDL_gfxPrimitives.h"
20 #else
21 #include "SDL/SDL_gfxPrimitives.h"
22 #endif
23
24 #define WIDTH 1024
25 #define HEIGHT 768
26
WaitForEvent()27 void WaitForEvent()
28 {
29 int done;
30 SDL_Event event;
31
32 /* Check for events */
33 done = 0;
34 while (!done) {
35 SDL_PollEvent(&event);
36 switch (event.type) {
37 case SDL_KEYDOWN:
38 case SDL_QUIT:
39 done = 1;
40 break;
41 }
42 SDL_Delay(100);
43 }
44 }
45
ClearScreen(SDL_Surface * screen)46 void ClearScreen(SDL_Surface *screen)
47 {
48 int i;
49 /* Set the screen to black */
50 if ( SDL_LockSurface(screen) == 0 ) {
51 Uint32 black;
52 Uint8 *pixels;
53 black = SDL_MapRGB(screen->format, 0, 0, 0);
54 pixels = (Uint8 *)screen->pixels;
55 for ( i=0; i<screen->h; ++i ) {
56 memset(pixels, black,
57 screen->w*screen->format->BytesPerPixel);
58 pixels += screen->pitch;
59 }
60 SDL_UnlockSurface(screen);
61 }
62 }
63
FileExists(const char * filename)64 int FileExists(const char * filename)
65 {
66 FILE *file;
67 if (file = fopen(filename, "r"))
68 {
69 fclose(file);
70 return 1;
71 }
72 return 0;
73 }
74
75 #define NUM_SDLGFX_FONTS 7
76
77 /* Filenames of font files */
78 static char *fontfile[NUM_SDLGFX_FONTS] = {
79 "",
80 "5x7.fnt",
81 "7x13.fnt",
82 "7x13B.fnt",
83 "7x13O.fnt",
84 "9x18.fnt",
85 "9x18B.fnt",
86 };
87
88 /* Width of font characters */
89 static int fontw[NUM_SDLGFX_FONTS] = {
90 8,
91 5,
92 7,
93 7,
94 7,
95 9,
96 9,
97 };
98
99 /* Height of fonts characters */
100 static int fonth[NUM_SDLGFX_FONTS] = {
101 8,
102 7,
103 13,
104 13,
105 13,
106 18,
107 18,
108 };
109
110 /* Bytes of fontfiles */
111 static int fontsize[NUM_SDLGFX_FONTS] = {
112 0,
113 1792,
114 3328,
115 3328,
116 3328,
117 9216,
118 9216,
119 };
120
121 /* Helper that searches and loads a fontfile */
LoadFontFile(int i)122 char *LoadFontFile(int i)
123 {
124 char *myfont;
125 char filename[128];
126 FILE *file;
127 int bytesRead;
128
129 /* Check index */
130 if (i==0)
131 {
132 return NULL;
133 }
134
135 /* Allocate memory for font data */
136 myfont=(char *)malloc(fontsize[i]);
137 if (myfont) {
138 if (strcmp(fontfile[i],"default")) {
139 /* Load a font data */
140 sprintf(filename,"../Fonts/%s",fontfile[i]);
141 if (!FileExists(filename))
142 {
143 sprintf(filename,"..\\Fonts\\%s",fontfile[i]);
144 if (!FileExists(filename))
145 {
146 sprintf(filename,"..\\..\\Fonts\\%s",fontfile[i]);
147 if (!FileExists(filename))
148 {
149 sprintf(filename,"..\\..\\..\\Fonts\\%s",fontfile[i]);
150 if (!FileExists(filename))
151 {
152 fprintf(stderr,"Cannot find fontfile: %s\n", fontfile[i]);
153 exit(-1);
154 }
155 }
156 }
157 }
158 file = fopen(filename,"r");
159 bytesRead = fread(myfont,fontsize[i],1,file);
160 fclose(file);
161 }
162 }
163
164 return myfont;
165 }
166
Draw(SDL_Surface * screen)167 void Draw(SDL_Surface *screen)
168 {
169 int i, rotation;
170 char *myfont;
171 char mytext[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
172 int x,y,yold;
173
174 /* Black screen */
175 ClearScreen(screen);
176 y=0;
177
178 /* Try all horizontal rotations */
179 rotation = 0;
180 gfxPrimitivesSetFontRotation(rotation);
181
182 /* Render all fonts */
183 for (i=0; i<NUM_SDLGFX_FONTS; i++) {
184 fprintf(stderr,"Drawing Font: size %ix%i, file '%s', %i bytes, rotation %i\n",fontw[i], fonth[i], fontfile[i],fontsize[i], rotation);
185 /* Reset line pos */
186 x=4;
187 myfont = NULL;
188 if (i>0) {
189 myfont=LoadFontFile(i);
190 }
191 /* Set font data and use it */
192 gfxPrimitivesSetFont(myfont,fontw[i],fonth[i]);
193 y += fonth[i];
194 stringRGBA(screen,x,y,fontfile[i],255,255,255,255);
195 x += 100;
196 stringRGBA(screen,x,y,mytext,255,255,255,255);
197 y += 10;
198 if (i>0)
199 {
200 /* Clean up font-data */
201 free(myfont);
202 }
203 }
204
205 y += 20;
206
207 yold = y;
208
209 rotation = 2;
210 gfxPrimitivesSetFontRotation(rotation);
211
212 /* Render all fonts */
213 for (i=0; i<NUM_SDLGFX_FONTS; i++) {
214 fprintf(stderr,"Drawing Font: size %ix%i, file '%s', %i bytes, rotation %i\n",fontw[i], fonth[i], fontfile[i],fontsize[i], rotation);
215 /* Reset line pos */
216 x=WIDTH - 14;
217 myfont = NULL;
218 if (i>0) {
219 myfont=LoadFontFile(i);
220 }
221 /* Set font data and use it */
222 gfxPrimitivesSetFont(myfont,fontw[i],fonth[i]);
223 y += fonth[i];
224 stringRGBA(screen,x,y,fontfile[i],255,255,255,255);
225 x -= 100;
226 stringRGBA(screen,x,y,mytext,255,255,255,255);
227 y += 10;
228 if (i>0)
229 {
230 /* Clean up font-data */
231 free(myfont);
232 }
233 }
234
235 y += 20;
236
237 /* Try all vertical rotations */
238 rotation = 1;
239 gfxPrimitivesSetFontRotation(rotation);
240
241 x = 14;
242
243 /* Render all fonts */
244 for (i=0; i<NUM_SDLGFX_FONTS; i++) {
245 fprintf(stderr,"Drawing Font: size %ix%i, file '%s', %i bytes, rotation %i\n",fontw[i], fonth[i], fontfile[i],fontsize[i], rotation);
246 /* Reset line pos */
247 y=yold;
248 myfont = NULL;
249 if (i>0) {
250 myfont=LoadFontFile(i);
251 }
252 /* Set font data and use it */
253 gfxPrimitivesSetFont(myfont,fontw[i],fonth[i]);
254 x += fonth[i];
255 stringRGBA(screen,x,y,fontfile[i],255,255,255,255);
256 y += 100;
257 stringRGBA(screen,x,y,mytext,255,255,255,255);
258 x += 10;
259 if (i>0)
260 {
261 /* Clean up font-data */
262 free(myfont);
263 }
264 }
265
266 x += 20;
267
268 rotation = 3;
269 gfxPrimitivesSetFontRotation(rotation);
270
271 /* Render all fonts */
272 for (i=0; i<NUM_SDLGFX_FONTS; i++) {
273 fprintf(stderr,"Drawing Font: size %ix%i, file '%s', %i bytes, rotation %i\n",fontw[i], fonth[i], fontfile[i],fontsize[i], rotation);
274 /* Reset line pos */
275 y=HEIGHT - 14;
276 myfont = NULL;
277 if (i>0) {
278 if (i>0) {
279 myfont=LoadFontFile(i);
280 }
281 /* Set font data and use it */
282 gfxPrimitivesSetFont(myfont,fontw[i],fonth[i]);
283 x += fonth[i];
284 stringRGBA(screen,x,y,fontfile[i],255,255,255,255);
285 y -= 100;
286 stringRGBA(screen,x,y,mytext,255,255,255,255);
287 x += 10;
288 if (i>0)
289 {
290 /* Clean up font-data */
291 free(myfont);
292 }
293 }
294
295 /* Display by flipping screens */
296 SDL_Flip(screen);
297 }
298 }
299
300 /* ======== */
301
main(int argc,char * argv[])302 int main(int argc, char *argv[])
303 {
304 SDL_Surface *screen;
305 Uint8 video_bpp;
306 Uint32 videoflags;
307 char title[64];
308
309 /* Generate title strings */
310 sprintf (title, "TestFonts - v%i.%i.%i", SDL_GFXPRIMITIVES_MAJOR, SDL_GFXPRIMITIVES_MINOR, SDL_GFXPRIMITIVES_MICRO);
311
312 /* Initialize SDL */
313 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
314 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
315 exit(1);
316 }
317 atexit(SDL_Quit);
318
319 video_bpp = 32;
320 videoflags = SDL_SWSURFACE | SDL_SRCALPHA | SDL_RESIZABLE;
321 while ( argc > 1 ) {
322 --argc;
323 if ( strcmp(argv[argc-1], "-bpp") == 0 ) {
324 video_bpp = atoi(argv[argc]);
325 --argc;
326 } else
327 if ( strcmp(argv[argc], "-hw") == 0 ) {
328 videoflags |= SDL_HWSURFACE;
329 } else
330 if ( strcmp(argv[argc], "-warp") == 0 ) {
331 videoflags |= SDL_HWPALETTE;
332 } else
333 if ( strcmp(argv[argc], "-fullscreen") == 0 ) {
334 videoflags |= SDL_FULLSCREEN;
335 } else {
336 fprintf(stderr,
337 "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
338 argv[0]);
339 exit(1);
340 }
341 }
342
343 /* Force double buffering */
344 videoflags |= SDL_DOUBLEBUF;
345
346 /* Set video mode */
347 if ( (screen=SDL_SetVideoMode(WIDTH, HEIGHT, video_bpp, videoflags)) == NULL ) {
348 fprintf(stderr, "Couldn't set %ix%i %i bpp video mode: %s\n",WIDTH,HEIGHT,video_bpp,SDL_GetError());
349 exit(2);
350 }
351
352 /* Use alpha blending */
353 SDL_SetAlpha(screen, SDL_SRCALPHA, 0);
354
355 /* Set title for window */
356 SDL_WM_SetCaption(title,title);
357
358 /* Do all the drawing work */
359 Draw(screen);
360
361 WaitForEvent();
362
363 return(0);
364 }