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-2012 Cyril Hrubis <metan@ucw.cz> *
24 *****************************************************************************/
34 static GP_Pixel black
;
35 static GP_Pixel white
;
37 SDL_Surface
*display
= NULL
;
39 GP_Context
*bitmap
, *bitmap_raw
, *bitmap_conv
;
43 SDL_UserEvent timer_event
;
45 static int pause_flag
= 0;
47 static int bitmap_x
, bitmap_y
, bitmap_vx
= -3, bitmap_vy
= -3;
49 Uint32
timer_callback(__attribute__((unused
)) Uint32 interval
,
50 __attribute__((unused
)) void *param
)
52 timer_event
.type
= SDL_USEREVENT
;
53 SDL_PushEvent((SDL_Event
*) &timer_event
);
57 static char text_buf
[255];
59 void redraw_screen(void)
64 bitmap_x
+= bitmap_vx
;
65 bitmap_y
+= bitmap_vy
;
67 if (bitmap_x
+ GP_ContextW(bitmap
) > context
.w
) {
68 bitmap_vx
= -bitmap_vx
;
69 bitmap_x
+= bitmap_vx
;
73 bitmap_vx
= -bitmap_vx
;
74 bitmap_x
+= bitmap_vx
;
77 if (bitmap_y
+ GP_ContextH(bitmap
) > context
.h
) {
78 bitmap_vy
= -bitmap_vy
;
79 bitmap_y
+= bitmap_vy
;
83 bitmap_vy
= -bitmap_vy
;
84 bitmap_y
+= bitmap_vy
;
87 SDL_LockSurface(display
);
89 GP_FillRectXYWH(&context
, 20, 20, 300, 50, black
);
91 GP_Text(&context
, NULL
, 20, 20, GP_ALIGN_RIGHT
|GP_VALIGN_BOTTOM
,
92 white
, black
, text_buf
);
94 GP_Print(&context
, NULL
, 250, 20, GP_ALIGN_RIGHT
|GP_VALIGN_BOTTOM
,
95 white
, black
, "%c|%c|%c", bitmap
->x_swap
? 'x' : ' ',
96 bitmap
->y_swap
? 'y' : ' ', bitmap
->axes_swap
? 'a' : ' ');
98 GP_Blit(bitmap
, 0, 0, GP_ContextW(bitmap
), GP_ContextH(bitmap
),
99 &context
, bitmap_x
, bitmap_y
);
101 SDL_UpdateRect(display
, bitmap_x
, bitmap_y
,
102 GP_ContextW(bitmap
), GP_ContextH(bitmap
));
103 SDL_UpdateRect(display
, 20, 20, 400, 50);
105 SDL_UnlockSurface(display
);
108 static void change_bitmap(void)
110 if (bitmap
== bitmap_raw
)
111 bitmap
= bitmap_conv
;
115 snprintf(text_buf
, sizeof(text_buf
), "'%s' -> '%s'",
116 GP_PixelTypes
[bitmap
->pixel_type
].name
,
117 GP_PixelTypes
[context
.pixel_type
].name
);
120 void event_loop(void)
124 while (SDL_WaitEvent(&event
) > 0) {
126 switch (event
.type
) {
132 switch (event
.key
.keysym
.sym
) {
134 pause_flag
= !pause_flag
;
137 bitmap
->x_swap
= !bitmap
->x_swap
;
140 bitmap
->y_swap
= !bitmap
->y_swap
;
143 bitmap
->axes_swap
= !bitmap
->axes_swap
;
162 void print_instructions(void)
164 printf("Use the following keys to control the test:\n");
165 printf(" Esc ................. exit\n");
166 printf(" Space ............... converts bitmap to screen pixel format\n");
167 printf(" A ................... swap sprite axes\n");
168 printf(" X ................... mirror sprite X\n");
169 printf(" Y ................... mirror sprite Y\n");
170 printf(" P ................... pause\n");
173 int main(int argc
, char *argv
[])
176 const char *sprite
= "ball.ppm";
178 print_instructions();
181 for (i
= 1; i
< argc
; i
++) {
182 if (strcmp(argv
[i
], "-16") == 0) {
184 } else if (strcmp(argv
[i
], "-24") == 0) {
186 } else if (strcmp(argv
[i
], "-32") == 0) {
193 GP_SetDebugLevel(10);
195 if ((bitmap_raw
= GP_LoadImage(sprite
, NULL
)) == NULL
) {
196 fprintf(stderr
, "Failed to load bitmap: %s\n", strerror(errno
));
201 if (SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_TIMER
) != 0) {
202 fprintf(stderr
, "Could not initialize SDL: %s\n", SDL_GetError());
206 /* Create a window with a software back surface */
207 display
= SDL_SetVideoMode(640, 480, display_bpp
, SDL_SWSURFACE
);
208 if (display
== NULL
) {
209 fprintf(stderr
, "Could not open display: %s\n", SDL_GetError());
213 GP_SDL_ContextFromSurface(&context
, display
);
215 bitmap_conv
= GP_ContextConvertAlloc(bitmap_raw
, context
.pixel_type
);
218 black
= GP_ColorToContextPixel(GP_COL_BLACK
, &context
);
219 white
= GP_ColorToContextPixel(GP_COL_WHITE
, &context
);
221 /* Set up the refresh timer */
222 timer
= SDL_AddTimer(60, timer_callback
, NULL
);
224 fprintf(stderr
, "Could not set up timer: %s\n", SDL_GetError());
228 /* Enter the event loop */