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-2011 Cyril Hrubis <metan@ucw.cz> *
24 *****************************************************************************/
34 static GP_Pixel black
, white
, gray
;
36 SDL_Surface
*display
= NULL
;
37 GP_Context context
, *sub_context
;
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
);
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
;
80 GP_FillTriangle(dest
, x1
, y1
, x2
, y2
, x3
, y3
, col
);
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
;
96 GP_FillRect(dest
, x1
, y1
, x2
, y2
, col
);
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
)
108 x1
= random() % (w
- 2*r
) + x
+ r
;
109 y1
= random() % (h
- 2*r
) + y
+ r
;
112 GP_FillCircle(dest
, x1
, y1
, r
, col
);
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
)
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)
139 SDL_LockSurface(display
);
141 uint8_t v
= random() % 128 + 50;
143 if (sub_context
->pixel_type
== GP_PIXEL_P8
)
144 col
= random() % 256;
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
);
153 draw_line(sub_context
, -100, -100, sub_context
->w
+ 200, sub_context
->h
+ 200, col
);
156 draw_triangle(sub_context
, -100, -100, sub_context
->w
+ 200, sub_context
->h
+ 200, col
, 0);
159 draw_triangle(sub_context
, -100, -100, sub_context
->w
+ 200, sub_context
->h
+ 200, col
, 1);
162 draw_rect(sub_context
, -100, -100, sub_context
->w
+ 200, sub_context
->h
+ 200, col
, 0);
165 draw_rect(sub_context
, -100, -100, sub_context
->w
+ 200, sub_context
->h
+ 200, col
, 1);
168 draw_circle(sub_context
, -100, -100, sub_context
->w
+ 200, sub_context
->h
+ 200, col
, 0);
171 draw_circle(sub_context
, -100, -100, sub_context
->w
+ 200, sub_context
->h
+ 200, col
, 1);
174 draw_text(sub_context
, -100, -100, sub_context
->w
+ 200, sub_context
->h
+ 200, col
);
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)
193 while (SDL_WaitEvent(&event
) > 0) {
195 switch (event
.type
) {
201 switch (event
.key
.keysym
.sym
) {
203 pause_flag
= !pause_flag
;
206 draw_flag
= (draw_flag
+ 1) % 8;
224 int main(int argc
, char *argv
[])
226 /* Bits per pixel to be set for the display surface. */
230 for (i
= 1; i
< argc
; i
++) {
231 if (strcmp(argv
[i
], "-8") == 0) {
233 } else if (strcmp(argv
[i
], "-16") == 0) {
236 else if (strcmp(argv
[i
], "-24") == 0) {
239 else if (strcmp(argv
[i
], "-32") == 0) {
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());
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());
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
);
268 fprintf(stderr
, "Could not set up timer: %s\n", SDL_GetError());