1 /*****************************************************************************
2 * This file is part of gfxprim library. *
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. *
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. *
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 *
19 * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
22 * Copyright (C) 2009-2010 Cyril Hrubis <metan@ucw.cz> *
24 *****************************************************************************/
34 SDL_Surface
*display
= NULL
;
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;
53 void redraw_screen(void)
55 SDL_LockSurface(display
);
57 GP_Fill(&context
, black_pixel
);
59 GP_TextStyle style
= GP_DEFAULT_TEXT_STYLE
;
63 style
.font
= &GP_default_proportional_font
;
66 style
.font
= &GP_default_console_font
;
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 *);
80 for (i
= 0; i
< TEST_STRING_COUNT
; i
++) {
81 const char *test_string
= test_strings
[i
];
85 style
.pixel_xspace
= 0;
86 style
.pixel_yspace
= 0;
87 style
.char_xspace
= tracking
;
89 GP_FillRectXYWH(&context
,
91 GP_TextWidth(&style
, test_string
),
97 GP_TextMaxWidth(&style
, strlen(test_string
)),
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)
124 while (SDL_WaitEvent(&event
) > 0) {
125 switch (event
.type
) {
127 case SDL_VIDEOEXPOSE
:
133 switch (event
.key
.keysym
.sym
) {
136 font_flag
= (font_flag
+ 1) % 3;
138 font_flag
= (font_flag
+ 1) % 2;
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();
180 fprintf(stderr
, "\nLoading font '%s'\n", argv
[1]);
181 err
= GP_FontLoad(&font
, argv
[1]);
184 fprintf(stderr
, "Error: %s\n", GP_RetCodeName(err
));
190 if (SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_TIMER
) != 0) {
191 fprintf(stderr
, "Could not initialize SDL: %s\n", SDL_GetError());
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());
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
);