Rename GP_Context -> GP_Pixmap
[gfxprim.git] / demos / c_simple / randomshapetest.c
blob766b48e5ee928f60f29183fbdfd28c7a2e35b570
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-2013 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
26 #include <stdio.h>
27 #include <stdlib.h>
29 #include <GP.h>
31 static GP_Backend *win;
33 /* Globally used colors. */
34 static GP_Pixel white, black;
36 /* Holding flag (pauses drawing). */
37 static int pause_flag = 0;
39 /* Shape to draw */
40 #define SHAPE_FIRST 1
41 #define SHAPE_CIRCLE 1
42 #define SHAPE_ELLIPSE 2
43 #define SHAPE_TRIANGLE 3
44 #define SHAPE_RECTANGLE 4
45 #define SHAPE_TETRAGON 5
46 #define SHAPE_POLYGON 6
47 #define SHAPE_LAST 6
48 static int shape = SHAPE_FIRST;
50 /* Draw outlines? */
51 static int outline_flag = 0;
53 /* Draw filled shapes? */
54 static int fill_flag = 1;
56 /* Do a clipping test? */
57 static int cliptest_flag = 0;
59 void random_point(const GP_Pixmap *c, int *x, int *y)
61 if (cliptest_flag) {
62 *x = random() % (3*c->w) - c->w;
63 *y = random() % (3*c->h) - c->h;
64 } else {
65 *x = random() % c->w;
66 *y = random() % c->h;
70 void random_point_AA(const GP_Pixmap *c, int *x, int *y)
72 *x = random() % (c->w<<8);
73 *y = random() % (c->h<<8);
76 void draw_random_circle(GP_Pixel pixel)
78 int x, y;
79 random_point(win->pixmap, &x, &y);
80 int r = random() % 50;
82 if (fill_flag)
83 GP_FillCircle(win->pixmap, x, y, r, pixel);
85 if (outline_flag)
86 GP_Circle(win->pixmap, x, y, r, white);
89 void draw_random_ellipse(GP_Pixel pixel)
91 int x, y;
92 random_point(win->pixmap, &x, &y);
93 int rx = random() % 50;
94 int ry = random() % 50;
96 if (fill_flag)
97 GP_FillEllipse(win->pixmap, x, y, rx, ry, pixel);
99 if (outline_flag)
100 GP_Ellipse(win->pixmap, x, y, rx, ry, white);
103 void draw_random_triangle(GP_Pixel pixel)
105 int x0, y0, x1, y1, x2, y2;
106 random_point(win->pixmap, &x0, &y0);
107 random_point(win->pixmap, &x1, &y1);
108 random_point(win->pixmap, &x2, &y2);
110 if (fill_flag)
111 GP_FillTriangle(win->pixmap, x0, y0, x1, y1, x2, y2, pixel);
113 if (outline_flag)
114 GP_Triangle(win->pixmap, x0, y0, x1, y1, x2, y2, white);
117 void draw_random_rectangle(GP_Pixel pixel)
119 int x0, y0, x1, y1;
120 random_point(win->pixmap, &x0, &y0);
121 random_point(win->pixmap, &x1, &y1);
123 if (fill_flag)
124 GP_FillRect(win->pixmap, x0, y0, x1, y1, pixel);
126 if (outline_flag)
127 GP_Rect(win->pixmap, x0, y0, x1, y1, white);
130 void draw_random_tetragon(GP_Pixel pixel)
132 int x0, y0, x1, y1, x2, y2, x3, y3;
133 random_point(win->pixmap, &x0, &y0);
134 random_point(win->pixmap, &x1, &y1);
135 random_point(win->pixmap, &x2, &y2);
136 random_point(win->pixmap, &x3, &y3);
138 if (fill_flag)
139 GP_FillTetragon(win->pixmap, x0, y0, x1, y1, x2, y2, x3, y3, pixel);
141 if (outline_flag)
142 GP_Tetragon(win->pixmap, x0, y0, x1, y1, x2, y2, x3, y3, pixel);
145 void draw_random_polygon(GP_Pixel pixel)
147 GP_Coord xy[10];
148 int i;
150 for (i = 0; i < 5; i++) {
151 random_point(win->pixmap, xy + 2*i, xy + 2*i + 1);
154 GP_FillPolygon_Raw(win->pixmap, 5, xy, pixel);
157 void clear_screen(void)
159 GP_Fill(win->pixmap, black);
160 GP_BackendFlip(win);
163 void redraw_screen(void)
165 if (pause_flag)
166 return;
168 /* Pick a random color for drawing. */
169 GP_Pixel pixel;
170 pixel = GP_RGBToPixel(random() % 256, random() % 256,
171 random() % 256, win->pixmap->pixel_type);
173 switch (shape) {
174 case SHAPE_CIRCLE:
175 draw_random_circle(pixel);
176 break;
177 case SHAPE_ELLIPSE:
178 draw_random_ellipse(pixel);
179 break;
180 case SHAPE_TRIANGLE:
181 draw_random_triangle(pixel);
182 break;
183 case SHAPE_RECTANGLE:
184 draw_random_rectangle(pixel);
185 break;
186 case SHAPE_TETRAGON:
187 draw_random_tetragon(pixel);
188 break;
189 case SHAPE_POLYGON:
190 draw_random_polygon(pixel);
191 break;
195 void event_loop(void)
197 GP_Event ev;
199 while (GP_BackendGetEvent(win, &ev)) {
200 switch (ev.type) {
201 case GP_EV_KEY:
202 if (ev.code != GP_EV_KEY_DOWN)
203 continue;
205 switch (ev.val.key.key) {
206 case GP_KEY_SPACE:
207 shape++;
208 if (shape > SHAPE_LAST)
209 shape = SHAPE_FIRST;
210 clear_screen();
211 pause_flag = 0;
212 break;
213 case GP_KEY_P:
214 pause_flag = !pause_flag;
215 break;
216 case GP_KEY_F:
217 fill_flag = !fill_flag;
218 if (!fill_flag && !outline_flag)
219 outline_flag = 1;
220 break;
221 case GP_KEY_O:
222 outline_flag = !outline_flag;
223 if (!fill_flag && !outline_flag)
224 fill_flag = 1;
225 break;
226 case GP_KEY_C:
227 cliptest_flag = !cliptest_flag;
228 break;
229 case GP_KEY_X:
230 clear_screen();
231 break;
232 case GP_KEY_ESC:
233 GP_BackendExit(win);
234 exit(0);
235 break;
237 break;
238 case GP_EV_SYS:
239 if (ev.code == GP_EV_SYS_RESIZE) {
240 GP_BackendResizeAck(win);
241 clear_screen();
242 GP_BackendFlip(win);
244 break;
249 int main(void)
251 const char *backend_opts = "X11";
253 win = GP_BackendInit(backend_opts, "Random Shape Test");
255 if (win == NULL) {
256 fprintf(stderr, "Failed to initalize backend '%s'\n",
257 backend_opts);
258 return 1;
261 white = GP_RGBToPixmapPixel(0xff, 0xff, 0xff, win->pixmap);
262 black = GP_RGBToPixmapPixel(0x00, 0x00, 0x00, win->pixmap);
264 for (;;) {
265 GP_BackendPoll(win);
266 event_loop();
268 usleep(20000);
270 if (pause_flag)
271 continue;
273 redraw_screen();
274 GP_BackendFlip(win);