input, backends: Add support for SYS_RESIZE and SYS_QUIT.
[gfxprim.git] / tests / SDL / subcontext.c
blob5147ddf0acd0e8979da03f629e8b0b1b85d24b3c
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 <math.h>
29 #include <SDL/SDL.h>
31 #include "GP.h"
32 #include "GP_SDL.h"
34 static GP_Pixel black, white, gray;
36 SDL_Surface *display = NULL;
37 GP_Context context, *sub_context;
39 SDL_TimerID timer;
41 SDL_UserEvent timer_event;
43 static int pause_flag = 0;
44 static int draw_flag = 0;
46 Uint32 timer_callback(__attribute__((unused)) Uint32 interval,
47 __attribute__((unused)) void *param)
49 timer_event.type = SDL_USEREVENT;
50 SDL_PushEvent((SDL_Event *) &timer_event);
51 return 30;
54 static void draw_line(GP_Context *dest, GP_Coord x, GP_Coord y,
55 GP_Size w, GP_Size h, GP_Color col)
57 GP_Coord x1, x2, y1, y2;
59 x1 = random() % w + x;
60 y1 = random() % h + y;
61 x2 = random() % w + x;
62 y2 = random() % h + y;
64 GP_Line(dest, x1, y1, x2, y2, col);
67 static void draw_triangle(GP_Context *dest, GP_Coord x, GP_Coord y,
68 GP_Size w, GP_Size h, GP_Color col, int flag)
70 GP_Coord x1, x2, x3, y1, y2, y3;
72 x1 = random() % w + x;
73 y1 = random() % h + y;
74 x2 = random() % w + x;
75 y2 = random() % h + y;
76 x3 = random() % w + x;
77 y3 = random() % h + y;
79 if (flag)
80 GP_FillTriangle(dest, x1, y1, x2, y2, x3, y3, col);
81 else
82 GP_Triangle(dest, x1, y1, x2, y2, x3, y3, col);
85 static void draw_rect(GP_Context *dest, GP_Coord x, GP_Coord y,
86 GP_Size w, GP_Size h, GP_Color col, int flag)
88 GP_Coord x1, x2, y1, y2;
90 x1 = random() % w + x;
91 y1 = random() % h + y;
92 x2 = random() % w + x;
93 y2 = random() % h + y;
95 if (flag)
96 GP_FillRect(dest, x1, y1, x2, y2, col);
97 else
98 GP_Rect(dest, x1, y1, x2, y2, col);
101 static void draw_circle(GP_Context *dest, GP_Coord x, GP_Coord y,
102 GP_Size w, GP_Size h, GP_Color col, int flag)
104 GP_Coord x1, y1, r;
106 r = random() % 150;
108 x1 = random() % (w - 2*r) + x + r;
109 y1 = random() % (h - 2*r) + y + r;
111 if (flag)
112 GP_FillCircle(dest, x1, y1, r, col);
113 else
114 GP_Circle(dest, x1, y1, r, col);
117 #define TEXT "Lorem Ipsum Dolor Sit Amet"
119 static void draw_text(GP_Context *dest, GP_Coord x, GP_Coord y,
120 GP_Size w, GP_Size h, GP_Color col)
122 GP_Coord x1, y1;
123 GP_Size tw, th;
125 tw = GP_TextWidth(NULL, TEXT);
126 th = GP_TextHeight(NULL);
128 x1 = random() % (w - tw) + x;
129 y1 = random() % (h - th) + y;
131 GP_Text(dest, NULL, x1, y1, GP_ALIGN_RIGHT|GP_VALIGN_BOTTOM, col, black, TEXT);
134 void redraw_screen(void)
136 if (pause_flag)
137 return;
139 SDL_LockSurface(display);
141 uint8_t v = random() % 128 + 50;
142 GP_Color col;
143 if (sub_context->pixel_type == GP_PIXEL_P8)
144 col = random() % 256;
145 else
146 col = GP_RGBToContextPixel(v, v, 255, sub_context);
148 /* frame around subcontext */
149 GP_Rect(&context, 99, 99, context.w - 100, context.h - 100, white);
151 switch (draw_flag) {
152 case 0:
153 draw_line(sub_context, -100, -100, sub_context->w + 200, sub_context->h + 200, col);
154 break;
155 case 1:
156 draw_triangle(sub_context, -100, -100, sub_context->w + 200, sub_context->h + 200, col, 0);
157 break;
158 case 2:
159 draw_triangle(sub_context, -100, -100, sub_context->w + 200, sub_context->h + 200, col, 1);
160 break;
161 case 3:
162 draw_rect(sub_context, -100, -100, sub_context->w + 200, sub_context->h + 200, col, 0);
163 break;
164 case 4:
165 draw_rect(sub_context, -100, -100, sub_context->w + 200, sub_context->h + 200, col, 1);
166 break;
167 case 5:
168 draw_circle(sub_context, -100, -100, sub_context->w + 200, sub_context->h + 200, col, 0);
169 break;
170 case 6:
171 draw_circle(sub_context, -100, -100, sub_context->w + 200, sub_context->h + 200, col, 1);
172 break;
173 case 7:
174 draw_text(sub_context, -100, -100, sub_context->w + 200, sub_context->h + 200, col);
175 break;
178 SDL_Flip(display);
180 SDL_UnlockSurface(display);
183 void clear_screen(void)
185 GP_Fill(&context, black);
186 GP_Fill(sub_context, gray);
189 void event_loop(void)
191 SDL_Event event;
193 while (SDL_WaitEvent(&event) > 0) {
195 switch (event.type) {
197 case SDL_USEREVENT:
198 redraw_screen();
199 break;
200 case SDL_KEYDOWN:
201 switch (event.key.keysym.sym) {
202 case SDLK_p:
203 pause_flag = !pause_flag;
204 break;
205 case SDLK_SPACE:
206 draw_flag = (draw_flag + 1) % 8;
207 clear_screen();
208 break;
209 case SDLK_ESCAPE:
210 return;
212 default:
213 break;
215 break;
216 case SDL_QUIT:
217 return;
218 default:
219 break;
224 int main(int argc, char *argv[])
226 /* Bits per pixel to be set for the display surface. */
227 int display_bpp = 0;
229 int i;
230 for (i = 1; i < argc; i++) {
231 if (strcmp(argv[i], "-8") == 0) {
232 display_bpp = 8;
233 } else if (strcmp(argv[i], "-16") == 0) {
234 display_bpp = 16;
236 else if (strcmp(argv[i], "-24") == 0) {
237 display_bpp = 24;
239 else if (strcmp(argv[i], "-32") == 0) {
240 display_bpp = 32;
244 GP_SetDebugLevel(10);
246 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
247 fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
248 return 1;
251 display = SDL_SetVideoMode(640, 480, display_bpp, SDL_SWSURFACE);
252 if (display == NULL) {
253 fprintf(stderr, "Could not open display: %s\n", SDL_GetError());
254 goto fail;
257 GP_SDL_ContextFromSurface(&context, display);
259 black = GP_ColorToContextPixel(GP_COL_BLACK, &context);
260 white = GP_ColorToContextPixel(GP_COL_WHITE, &context);
261 gray = GP_ColorToContextPixel(GP_COL_GRAY_DARK, &context);
263 sub_context = GP_ContextSubContext(&context, NULL, 100, 100, 440, 280);
264 GP_Fill(sub_context, gray);
266 timer = SDL_AddTimer(60, timer_callback, NULL);
267 if (timer == 0) {
268 fprintf(stderr, "Could not set up timer: %s\n", SDL_GetError());
269 goto fail;
272 event_loop();
274 SDL_Quit();
275 return 0;
277 fail:
278 SDL_Quit();
279 return 1;