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
48 static int shape
= SHAPE_FIRST
;
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
)
62 *x
= random() % (3*c
->w
) - c
->w
;
63 *y
= random() % (3*c
->h
) - 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
)
79 random_point(win
->pixmap
, &x
, &y
);
80 int r
= random() % 50;
83 GP_FillCircle(win
->pixmap
, x
, y
, r
, pixel
);
86 GP_Circle(win
->pixmap
, x
, y
, r
, white
);
89 void draw_random_ellipse(GP_Pixel pixel
)
92 random_point(win
->pixmap
, &x
, &y
);
93 int rx
= random() % 50;
94 int ry
= random() % 50;
97 GP_FillEllipse(win
->pixmap
, x
, y
, rx
, ry
, pixel
);
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
);
111 GP_FillTriangle(win
->pixmap
, x0
, y0
, x1
, y1
, x2
, y2
, pixel
);
114 GP_Triangle(win
->pixmap
, x0
, y0
, x1
, y1
, x2
, y2
, white
);
117 void draw_random_rectangle(GP_Pixel pixel
)
120 random_point(win
->pixmap
, &x0
, &y0
);
121 random_point(win
->pixmap
, &x1
, &y1
);
124 GP_FillRect(win
->pixmap
, x0
, y0
, x1
, y1
, pixel
);
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
);
139 GP_FillTetragon(win
->pixmap
, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
, pixel
);
142 GP_Tetragon(win
->pixmap
, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
, pixel
);
145 void draw_random_polygon(GP_Pixel pixel
)
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
);
163 void redraw_screen(void)
168 /* Pick a random color for drawing. */
170 pixel
= GP_RGBToPixel(random() % 256, random() % 256,
171 random() % 256, win
->pixmap
->pixel_type
);
175 draw_random_circle(pixel
);
178 draw_random_ellipse(pixel
);
181 draw_random_triangle(pixel
);
183 case SHAPE_RECTANGLE
:
184 draw_random_rectangle(pixel
);
187 draw_random_tetragon(pixel
);
190 draw_random_polygon(pixel
);
195 void event_loop(void)
199 while (GP_BackendGetEvent(win
, &ev
)) {
202 if (ev
.code
!= GP_EV_KEY_DOWN
)
205 switch (ev
.val
.key
.key
) {
208 if (shape
> SHAPE_LAST
)
214 pause_flag
= !pause_flag
;
217 fill_flag
= !fill_flag
;
218 if (!fill_flag
&& !outline_flag
)
222 outline_flag
= !outline_flag
;
223 if (!fill_flag
&& !outline_flag
)
227 cliptest_flag
= !cliptest_flag
;
239 if (ev
.code
== GP_EV_SYS_RESIZE
) {
240 GP_BackendResizeAck(win
);
251 const char *backend_opts
= "X11";
253 win
= GP_BackendInit(backend_opts
, "Random Shape Test");
256 fprintf(stderr
, "Failed to initalize backend '%s'\n",
261 white
= GP_RGBToPixmapPixel(0xff, 0xff, 0xff, win
->pixmap
);
262 black
= GP_RGBToPixmapPixel(0x00, 0x00, 0x00, win
->pixmap
);