Added font loading into fonttest.
[gfxprim.git] / targets / sdl / tests / fonttest.c
blob5e8e12527e126267e61be1ef9c2ecbb6a17d8fbf
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
21 * *
22 * Copyright (C) 2009-2010 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <SDL/SDL.h>
30 #include "GP.h"
31 #include "GP_SDL.h"
33 /* the display */
34 SDL_Surface *display = NULL;
35 GP_Context context;
37 /* precomputed color pixels in display format */
38 static GP_Pixel white_pixel, gray_pixel, dark_gray_pixel, black_pixel,
39 red_pixel, blue_pixel;
41 static const char *test_strings[] = {
42 " !\"#$%&\047()*+,-./0123456789:;<=>?@",
43 "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`",
44 "abcdefghijklmnopqrstuvwxyz{|}~",
45 "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor..."
48 /* draw using proportional font? */
49 static int font_flag = 0;
50 static int tracking = 0;
51 GP_Font *font;
53 void redraw_screen(void)
55 SDL_LockSurface(display);
57 GP_Fill(&context, black_pixel);
59 GP_TextStyle style = GP_DEFAULT_TEXT_STYLE;
61 switch (font_flag) {
62 case 0:
63 style.font = &GP_default_proportional_font;
64 break;
65 case 1:
66 style.font = &GP_default_console_font;
67 break;
68 case 2:
69 style.font = font;
70 break;
73 /* Text alignment (we are always drawing to the right
74 * and below the starting point).
76 int align = GP_ALIGN_RIGHT|GP_VALIGN_BELOW;
78 const size_t TEST_STRING_COUNT = sizeof(test_strings)/sizeof(const char *);
79 size_t i;
80 for (i = 0; i < TEST_STRING_COUNT; i++) {
81 const char *test_string = test_strings[i];
83 style.pixel_xmul = 1;
84 style.pixel_ymul = 1;
85 style.pixel_xspace = 0;
86 style.pixel_yspace = 0;
87 style.char_xspace = tracking;
89 GP_FillRectXYWH(&context,
90 16, 100*i + 16,
91 GP_TextWidth(&style, test_string),
92 style.font->height,
93 red_pixel);
95 GP_RectXYWH(&context,
96 16, 100*i + 16,
97 GP_TextMaxWidth(&style, strlen(test_string)),
98 style.font->height,
99 blue_pixel);
101 GP_Text(&context, &style, 16, 100*i + 16, align, test_string, white_pixel);
103 style.pixel_xmul = 2;
104 style.pixel_ymul = 2;
105 style.pixel_yspace = 1;
107 GP_Text(&context, &style, 34, 100*i + 38, align, test_string, gray_pixel);
109 style.pixel_xmul = 4;
110 style.pixel_ymul = 2;
111 style.pixel_xspace = 1;
112 style.pixel_yspace = 1;
114 GP_Text(&context, &style, 64, 100*i + 72, align, test_string, dark_gray_pixel);
117 SDL_UnlockSurface(display);
120 void event_loop(void)
122 SDL_Event event;
124 while (SDL_WaitEvent(&event) > 0) {
125 switch (event.type) {
127 case SDL_VIDEOEXPOSE:
128 redraw_screen();
129 SDL_Flip(display);
130 break;
132 case SDL_KEYDOWN:
133 switch (event.key.keysym.sym) {
134 case SDLK_SPACE:
135 if (font)
136 font_flag = (font_flag + 1) % 3;
137 else
138 font_flag = (font_flag + 1) % 2;
140 redraw_screen();
141 SDL_Flip(display);
142 break;
143 case SDLK_UP:
144 tracking++;
145 redraw_screen();
146 SDL_Flip(display);
147 break;
148 case SDLK_DOWN:
149 tracking--;
150 redraw_screen();
151 SDL_Flip(display);
152 break;
153 case SDLK_ESCAPE:
154 return;
155 default:
156 break;
158 break;
160 case SDL_QUIT:
161 return;
166 void print_instructions(void)
168 printf("Use the following keys to control the test:\n");
169 printf(" Esc ................. exit\n");
170 printf(" Space ............... change font\n");
171 printf(" up/down ............. increase/decrease tracking\n");
174 int main(int argc, char *argv[])
176 print_instructions();
178 if (argc > 1) {
179 GP_RetCode err;
180 fprintf(stderr, "\nLoading font '%s'\n", argv[1]);
181 err = GP_FontLoad(&font, argv[1]);
183 if (err) {
184 fprintf(stderr, "Error: %s\n", GP_RetCodeName(err));
185 return 1;
189 /* Initialize SDL */
190 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
191 fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
192 return 1;
195 /* Create a window with a software back surface */
196 display = SDL_SetVideoMode(640, 480, 0, SDL_SWSURFACE);
197 if (display == NULL) {
198 fprintf(stderr, "Could not open display: %s\n", SDL_GetError());
199 goto fail;
202 /* Set up a clipping rectangle to test proper clipping of pixels */
203 SDL_Rect clip_rect = {10, 10, 620, 460};
204 SDL_SetClipRect(display, &clip_rect);
206 /* Initialize a GP context from the SDL display */
207 GP_SDL_ContextFromSurface(&context, display);
209 /* Load colors suitable for the display */
210 GP_ColorNameToPixel(&context, GP_COL_WHITE, &white_pixel);
211 GP_ColorNameToPixel(&context, GP_COL_GRAY_LIGHT, &gray_pixel);
212 GP_ColorNameToPixel(&context, GP_COL_GRAY_DARK, &dark_gray_pixel);
213 GP_ColorNameToPixel(&context, GP_COL_BLACK, &black_pixel);
214 GP_ColorNameToPixel(&context, GP_COL_RED, &red_pixel);
215 GP_ColorNameToPixel(&context, GP_COL_BLUE, &blue_pixel);
217 redraw_screen();
218 SDL_Flip(display);
220 event_loop();
222 SDL_Quit();
223 return 0;
225 fail:
226 SDL_Quit();
227 return 1;