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-2013 Cyril Hrubis <metan@ucw.cz> *
24 *****************************************************************************/
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;
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
49 static int shape
= SHAPE_FIRST
;
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
)
63 *x
= random() % (3*c
->w
) - c
->w
;
64 *y
= random() % (3*c
->h
) - 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
)
80 random_point(win
->context
, &x
, &y
);
81 int r
= random() % 50;
84 GP_FillCircle(win
->context
, x
, y
, r
, pixel
);
87 GP_Circle(win
->context
, x
, y
, r
, white
);
90 void draw_random_ellipse(GP_Pixel pixel
)
93 random_point(win
->context
, &x
, &y
);
94 int rx
= random() % 50;
95 int ry
= random() % 50;
98 GP_FillEllipse(win
->context
, x
, y
, rx
, ry
, pixel
);
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
);
112 GP_FillTriangle(win
->context
, x0
, y0
, x1
, y1
, x2
, y2
, pixel
);
115 GP_Triangle(win
->context
, x0
, y0
, x1
, y1
, x2
, y2
, white
);
118 void draw_random_rectangle(GP_Pixel pixel
)
121 random_point(win
->context
, &x0
, &y0
);
122 random_point(win
->context
, &x1
, &y1
);
125 GP_FillRect(win
->context
, x0
, y0
, x1
, y1
, pixel
);
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
);
140 GP_FillTetragon(win
->context
, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
, pixel
);
143 GP_Tetragon(win
->context
, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
, pixel
);
146 void draw_random_polygon(GP_Pixel pixel
)
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
)
161 random_point_AA(win
->context
, &x0
, &y0
);
162 random_point_AA(win
->context
, &x1
, &y1
);
165 GP_FillRect_AA(win
->context
, x0
, y0
, x1
, y1
, pixel
);
168 // GP_Rect(win->context, x0, y0, x1, y1, white);
172 void clear_screen(void)
174 GP_Fill(win
->context
, black
);
178 void redraw_screen(void)
183 /* Pick a random color for drawing. */
185 pixel
= GP_RGBToPixel(random() % 256, random() % 256,
186 random() % 256, win
->context
->pixel_type
);
190 draw_random_circle(pixel
);
193 draw_random_ellipse(pixel
);
196 draw_random_triangle(pixel
);
198 case SHAPE_RECTANGLE
:
199 draw_random_rectangle(pixel
);
202 draw_random_tetragon(pixel
);
205 draw_random_polygon(pixel
);
207 case SHAPE_RECTANGLE_AA
:
208 draw_random_rectangle_AA(pixel
);
213 void event_loop(void)
217 while (GP_BackendGetEvent(win
, &ev
)) {
220 if (ev
.code
!= GP_EV_KEY_DOWN
)
223 switch (ev
.val
.key
.key
) {
226 if (shape
> SHAPE_LAST
)
232 pause_flag
= !pause_flag
;
235 fill_flag
= !fill_flag
;
236 if (!fill_flag
&& !outline_flag
)
240 outline_flag
= !outline_flag
;
241 if (!fill_flag
&& !outline_flag
)
245 cliptest_flag
= !cliptest_flag
;
261 const char *backend_opts
= "X11";
263 win
= GP_BackendInit(backend_opts
, "Random Shape Test");
266 fprintf(stderr
, "Failed to initalize backend '%s'\n",
271 white
= GP_RGBToContextPixel(0xff, 0xff, 0xff, win
->context
);
272 black
= GP_RGBToContextPixel(0x00, 0x00, 0x00, win
->context
);