loaders: Add meta-data clear method and double type.
[gfxprim.git] / tests / SDL / symbolstest.c
blobbe964228acfd07343071b3a9e1b7f59940ed8822
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-2011 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <SDL/SDL.h>
30 #include "GP_SDL.h"
32 static GP_Pixel black;
34 /* The surface used as a display (in fact it is a software surface). */
35 SDL_Surface *display = NULL;
36 GP_Context context;
38 /* Timer used for refreshing the display */
39 SDL_TimerID timer;
41 /* An event used for signaling that the timer was triggered. */
42 SDL_UserEvent timer_event;
44 static int pause_flag = 0;
46 static int fill_flag = 0;
48 Uint32 timer_callback(__attribute__((unused)) Uint32 interval,
49 __attribute__((unused)) void *param)
51 timer_event.type = SDL_USEREVENT;
52 SDL_PushEvent((SDL_Event *) &timer_event);
53 return 60;
56 void random_point(SDL_Surface *surf, int *x, int *y)
58 *x = random() % surf->w;
59 *y = random() % surf->h;
62 void draw_random_symbol(GP_Pixel pixel)
64 int x, y, w, h;
65 random_point(display, &x, &y);
67 w = (random() % 10) + 5;
68 h = (random() % 10) + 5;
70 if (fill_flag)
71 GP_FillSymbol(&context, random() % GP_SYM_MAX, x, y, w, h,
72 pixel);
73 else
74 GP_Symbol(&context, random() % GP_SYM_MAX, x, y, w, h,
75 pixel);
78 void clear_screen(void)
80 SDL_LockSurface(display);
81 GP_Fill(&context, black);
82 SDL_UnlockSurface(display);
85 void redraw_screen(void)
87 if (pause_flag)
88 return;
90 SDL_LockSurface(display);
92 GP_Pixel pixel;
93 pixel = GP_RGBToPixel(random() % 256, random() % 256,
94 random() % 256, context.pixel_type);
96 draw_random_symbol(pixel);
98 SDL_UnlockSurface(display);
101 void event_loop(void)
103 SDL_Event event;
105 while (SDL_WaitEvent(&event) > 0) {
107 switch (event.type) {
109 case SDL_USEREVENT:
110 redraw_screen();
111 SDL_Flip(display);
112 break;
114 case SDL_KEYDOWN:
115 switch (event.key.keysym.sym) {
116 case SDLK_SPACE:
117 fill_flag = !fill_flag;
118 clear_screen();
119 break;
120 case SDLK_p:
121 pause_flag = !pause_flag;
122 break;
123 case SDLK_ESCAPE:
124 return;
126 default:
127 break;
129 break;
130 case SDL_QUIT:
131 return;
132 default:
133 break;
138 int main(int argc, char *argv[])
140 /* Bits per pixel to be set for the display surface. */
141 int display_bpp = 0;
143 int i;
144 for (i = 1; i < argc; i++) {
145 if (strcmp(argv[i], "-16") == 0) {
146 display_bpp = 16;
148 else if (strcmp(argv[i], "-24") == 0) {
149 display_bpp = 24;
151 else if (strcmp(argv[i], "-32") == 0) {
152 display_bpp = 32;
156 /* Initialize SDL */
157 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
158 fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
159 return 1;
162 /* Create a window with a software back surface */
163 display = SDL_SetVideoMode(640, 480, display_bpp, SDL_SWSURFACE);
164 if (display == NULL) {
165 fprintf(stderr, "Could not open display: %s\n", SDL_GetError());
166 goto fail;
169 /* Set up a clipping rectangle to test proper clipping of pixels */
170 SDL_Rect clip_rect = {10, 10, 620, 460};
171 SDL_SetClipRect(display, &clip_rect);
173 GP_SDL_ContextFromSurface(&context, display);
175 black = GP_ColorToContextPixel(GP_COL_BLACK, &context);
177 /* Set up the refresh timer */
178 timer = SDL_AddTimer(60, timer_callback, NULL);
179 if (timer == 0) {
180 fprintf(stderr, "Could not set up timer: %s\n", SDL_GetError());
181 goto fail;
184 /* Enter the event loop */
185 event_loop();
187 /* We're done */
188 SDL_Quit();
189 return 0;
191 fail:
192 SDL_Quit();
193 return 1;