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 *****************************************************************************/
28 static GP_Pixel black
;
29 static GP_Pixel white
;
31 static GP_Backend
*win
;
33 static GP_Context
*bitmap
, *bitmap_raw
, *bitmap_conv
;
34 static int bitmap_x
, bitmap_y
, bitmap_vx
= -3, bitmap_vy
= -3;
35 static int pause_flag
= 0;
37 static char text_buf
[255];
39 void redraw_screen(void)
41 bitmap_x
+= bitmap_vx
;
42 bitmap_y
+= bitmap_vy
;
44 if (bitmap_x
+ GP_ContextW(bitmap
) > win
->context
->w
) {
45 bitmap_vx
= -bitmap_vx
;
46 bitmap_x
+= bitmap_vx
;
50 bitmap_vx
= -bitmap_vx
;
51 bitmap_x
+= bitmap_vx
;
54 if (bitmap_y
+ GP_ContextH(bitmap
) > win
->context
->h
) {
55 bitmap_vy
= -bitmap_vy
;
56 bitmap_y
+= bitmap_vy
;
60 bitmap_vy
= -bitmap_vy
;
61 bitmap_y
+= bitmap_vy
;
64 GP_FillRectXYWH(win
->context
, 20, 20, 300, 50, black
);
66 GP_Text(win
->context
, NULL
, 20, 20, GP_ALIGN_RIGHT
|GP_VALIGN_BOTTOM
,
67 white
, black
, text_buf
);
69 GP_Print(win
->context
, NULL
, 250, 20, GP_ALIGN_RIGHT
|GP_VALIGN_BOTTOM
,
70 white
, black
, "%c|%c|%c", bitmap
->x_swap
? 'x' : ' ',
71 bitmap
->y_swap
? 'y' : ' ', bitmap
->axes_swap
? 'a' : ' ');
73 GP_Blit(bitmap
, 0, 0, GP_ContextW(bitmap
), GP_ContextH(bitmap
),
74 win
->context
, bitmap_x
, bitmap_y
);
76 GP_BackendUpdateRectXYWH(win
, bitmap_x
, bitmap_y
,
77 GP_ContextW(bitmap
), GP_ContextH(bitmap
));
78 GP_BackendUpdateRectXYWH(win
, 20, 20, 400, 50);
81 static void change_bitmap(void)
83 if (bitmap
== bitmap_raw
)
88 snprintf(text_buf
, sizeof(text_buf
), "'%s' -> '%s'",
89 GP_PixelTypeName(bitmap
->pixel_type
),
90 GP_PixelTypeName(win
->context
->pixel_type
));
97 while (GP_BackendGetEvent(win
, &ev
)) {
102 if (ev
.code
!= GP_EV_KEY_DOWN
)
105 switch (ev
.val
.key
.key
) {
107 bitmap
->x_swap
= !bitmap
->x_swap
;
110 bitmap
->y_swap
= !bitmap
->y_swap
;
113 bitmap
->axes_swap
= !bitmap
->axes_swap
;
116 pause_flag
= !pause_flag
;
139 void print_instructions(void)
141 printf("Use the following keys to control the test:\n");
142 printf(" Esc ............. exit\n");
143 printf(" Space ........... converts bitmap to screen pixel format\n");
144 printf(" R ............... swap sprite axes\n");
145 printf(" X ............... mirror sprite X\n");
146 printf(" Y ............... mirror sprite Y\n");
147 printf(" P ............... pause\n");
152 const char *sprite
= "ball.ppm";
153 const char *backend_opts
= "X11";
155 print_instructions();
157 bitmap_raw
= GP_LoadImage(sprite
, NULL
);
160 fprintf(stderr
, "Failed to load '%s'\n", sprite
);
164 win
= GP_BackendInit(backend_opts
, "Blit Test");
167 fprintf(stderr
, "Failed to initalize backend '%s'\n",
172 bitmap_conv
= GP_ContextConvertAlloc(bitmap_raw
,
173 win
->context
->pixel_type
);
176 black
= GP_ColorToContextPixel(GP_COL_BLACK
, win
->context
);
177 white
= GP_ColorToContextPixel(GP_COL_WHITE
, win
->context
);
179 GP_Fill(win
->context
, black
);