core: Get rid of GP_Color.
[gfxprim.git] / demos / c_simple / randomshapetest.c
blobe5f4e5d868351a2d93ef5312ee3c85f0607822f1
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_RECTANGLE_AA 7
48 #define SHAPE_LAST 7
49 static int shape = SHAPE_FIRST;
51 /* Draw outlines? */
52 static int outline_flag = 0;
54 /* Draw filled shapes? */
55 static int fill_flag = 1;
57 /* Do a clipping test? */
58 static int cliptest_flag = 0;
60 void random_point(const GP_Context *c, int *x, int *y)
62 if (cliptest_flag) {
63 *x = random() % (3*c->w) - c->w;
64 *y = random() % (3*c->h) - c->h;
65 } else {
66 *x = random() % c->w;
67 *y = random() % c->h;
71 void random_point_AA(const GP_Context *c, int *x, int *y)
73 *x = random() % (c->w<<8);
74 *y = random() % (c->h<<8);
77 void draw_random_circle(GP_Pixel pixel)
79 int x, y;
80 random_point(win->context, &x, &y);
81 int r = random() % 50;
83 if (fill_flag)
84 GP_FillCircle(win->context, x, y, r, pixel);
86 if (outline_flag)
87 GP_Circle(win->context, x, y, r, white);
90 void draw_random_ellipse(GP_Pixel pixel)
92 int x, y;
93 random_point(win->context, &x, &y);
94 int rx = random() % 50;
95 int ry = random() % 50;
97 if (fill_flag)
98 GP_FillEllipse(win->context, x, y, rx, ry, pixel);
100 if (outline_flag)
101 GP_Ellipse(win->context, x, y, rx, ry, white);
104 void draw_random_triangle(GP_Pixel pixel)
106 int x0, y0, x1, y1, x2, y2;
107 random_point(win->context, &x0, &y0);
108 random_point(win->context, &x1, &y1);
109 random_point(win->context, &x2, &y2);
111 if (fill_flag)
112 GP_FillTriangle(win->context, x0, y0, x1, y1, x2, y2, pixel);
114 if (outline_flag)
115 GP_Triangle(win->context, x0, y0, x1, y1, x2, y2, white);
118 void draw_random_rectangle(GP_Pixel pixel)
120 int x0, y0, x1, y1;
121 random_point(win->context, &x0, &y0);
122 random_point(win->context, &x1, &y1);
124 if (fill_flag)
125 GP_FillRect(win->context, x0, y0, x1, y1, pixel);
127 if (outline_flag)
128 GP_Rect(win->context, x0, y0, x1, y1, white);
131 void draw_random_tetragon(GP_Pixel pixel)
133 int x0, y0, x1, y1, x2, y2, x3, y3;
134 random_point(win->context, &x0, &y0);
135 random_point(win->context, &x1, &y1);
136 random_point(win->context, &x2, &y2);
137 random_point(win->context, &x3, &y3);
139 if (fill_flag)
140 GP_FillTetragon(win->context, x0, y0, x1, y1, x2, y2, x3, y3, pixel);
142 if (outline_flag)
143 GP_Tetragon(win->context, x0, y0, x1, y1, x2, y2, x3, y3, pixel);
146 void draw_random_polygon(GP_Pixel pixel)
148 GP_Coord xy[10];
149 int i;
151 for (i = 0; i < 5; i++) {
152 random_point(win->context, xy + 2*i, xy + 2*i + 1);
155 GP_FillPolygon_Raw(win->context, 5, xy, pixel);
158 void draw_random_rectangle_AA(GP_Pixel pixel)
160 int x0, y0, x1, y1;
161 random_point_AA(win->context, &x0, &y0);
162 random_point_AA(win->context, &x1, &y1);
164 // if (fill_flag)
165 GP_FillRect_AA(win->context, x0, y0, x1, y1, pixel);
167 // if (outline_flag)
168 // GP_Rect(win->context, x0, y0, x1, y1, white);
172 void clear_screen(void)
174 GP_Fill(win->context, black);
175 GP_BackendFlip(win);
178 void redraw_screen(void)
180 if (pause_flag)
181 return;
183 /* Pick a random color for drawing. */
184 GP_Pixel pixel;
185 pixel = GP_RGBToPixel(random() % 256, random() % 256,
186 random() % 256, win->context->pixel_type);
188 switch (shape) {
189 case SHAPE_CIRCLE:
190 draw_random_circle(pixel);
191 break;
192 case SHAPE_ELLIPSE:
193 draw_random_ellipse(pixel);
194 break;
195 case SHAPE_TRIANGLE:
196 draw_random_triangle(pixel);
197 break;
198 case SHAPE_RECTANGLE:
199 draw_random_rectangle(pixel);
200 break;
201 case SHAPE_TETRAGON:
202 draw_random_tetragon(pixel);
203 break;
204 case SHAPE_POLYGON:
205 draw_random_polygon(pixel);
206 break;
207 case SHAPE_RECTANGLE_AA:
208 draw_random_rectangle_AA(pixel);
209 break;
213 void event_loop(void)
215 GP_Event ev;
217 while (GP_BackendGetEvent(win, &ev)) {
218 switch (ev.type) {
219 case GP_EV_KEY:
220 if (ev.code != GP_EV_KEY_DOWN)
221 continue;
223 switch (ev.val.key.key) {
224 case GP_KEY_SPACE:
225 shape++;
226 if (shape > SHAPE_LAST)
227 shape = SHAPE_FIRST;
228 clear_screen();
229 pause_flag = 0;
230 break;
231 case GP_KEY_P:
232 pause_flag = !pause_flag;
233 break;
234 case GP_KEY_F:
235 fill_flag = !fill_flag;
236 if (!fill_flag && !outline_flag)
237 outline_flag = 1;
238 break;
239 case GP_KEY_O:
240 outline_flag = !outline_flag;
241 if (!fill_flag && !outline_flag)
242 fill_flag = 1;
243 break;
244 case GP_KEY_C:
245 cliptest_flag = !cliptest_flag;
246 break;
247 case GP_KEY_X:
248 clear_screen();
249 break;
250 case GP_KEY_ESC:
251 GP_BackendExit(win);
252 exit(0);
253 break;
259 int main(void)
261 const char *backend_opts = "X11";
263 win = GP_BackendInit(backend_opts, "Random Shape Test");
265 if (win == NULL) {
266 fprintf(stderr, "Failed to initalize backend '%s'\n",
267 backend_opts);
268 return 1;
271 white = GP_RGBToContextPixel(0xff, 0xff, 0xff, win->context);
272 black = GP_RGBToContextPixel(0x00, 0x00, 0x00, win->context);
274 for (;;) {
275 GP_BackendPoll(win);
276 event_loop();
278 usleep(20000);
280 if (pause_flag)
281 continue;
283 redraw_screen();
284 GP_BackendFlip(win);