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-2011 Cyril Hrubis <metan@ucw.cz> *
24 *****************************************************************************/
32 static GP_Pixel black
;
34 /* The surface used as a display (in fact it is a software surface). */
35 SDL_Surface
*display
= NULL
;
38 /* Timer used for refreshing the display */
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
);
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
)
65 random_point(display
, &x
, &y
);
67 w
= (random() % 10) + 5;
68 h
= (random() % 10) + 5;
71 GP_FillSymbol(&context
, random() % GP_SYM_MAX
, x
, y
, w
, h
,
74 GP_Symbol(&context
, random() % GP_SYM_MAX
, x
, y
, w
, h
,
78 void clear_screen(void)
80 SDL_LockSurface(display
);
81 GP_Fill(&context
, black
);
82 SDL_UnlockSurface(display
);
85 void redraw_screen(void)
90 SDL_LockSurface(display
);
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)
105 while (SDL_WaitEvent(&event
) > 0) {
107 switch (event
.type
) {
115 switch (event
.key
.keysym
.sym
) {
117 fill_flag
= !fill_flag
;
121 pause_flag
= !pause_flag
;
138 int main(int argc
, char *argv
[])
140 /* Bits per pixel to be set for the display surface. */
144 for (i
= 1; i
< argc
; i
++) {
145 if (strcmp(argv
[i
], "-16") == 0) {
148 else if (strcmp(argv
[i
], "-24") == 0) {
151 else if (strcmp(argv
[i
], "-32") == 0) {
157 if (SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_TIMER
) != 0) {
158 fprintf(stderr
, "Could not initialize SDL: %s\n", SDL_GetError());
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());
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
);
180 fprintf(stderr
, "Could not set up timer: %s\n", SDL_GetError());
184 /* Enter the event loop */