demos: Fix and cleanup GP_BackendInit() + docs.
[gfxprim/pasky.git] / demos / c_simple / blittest.c
blob02b374b67df0d8970000a51276689df00a487d9a
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
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. *
8 * *
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. *
13 * *
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 *
18 * *
19 * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
21 * *
22 * Copyright (C) 2009-2013 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
26 #include <GP.h>
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;
49 if (bitmap_x < 0) {
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;
59 if (bitmap_y < 0) {
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)
84 bitmap = bitmap_conv;
85 else
86 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));
93 void event_loop(void)
95 GP_Event ev;
97 while (GP_BackendGetEvent(win, &ev)) {
98 GP_EventDump(&ev);
100 switch (ev.type) {
101 case GP_EV_KEY:
102 if (ev.code != GP_EV_KEY_DOWN)
103 continue;
105 switch (ev.val.key.key) {
106 case GP_KEY_X:
107 bitmap->x_swap = !bitmap->x_swap;
108 break;
109 case GP_KEY_Y:
110 bitmap->y_swap = !bitmap->y_swap;
111 break;
112 case GP_KEY_R:
113 bitmap->axes_swap = !bitmap->axes_swap;
114 break;
115 case GP_KEY_P:
116 pause_flag = !pause_flag;
117 break;
118 case GP_KEY_SPACE:
119 change_bitmap();
120 break;
121 case GP_KEY_ESC:
122 GP_BackendExit(win);
123 exit(0);
124 break;
126 break;
127 case GP_EV_SYS:
128 switch(ev.code) {
129 case GP_EV_SYS_QUIT:
130 GP_BackendExit(win);
131 exit(0);
132 break;
134 break;
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");
150 int main(void)
152 const char *sprite = "ball.ppm";
153 const char *backend_opts = "X11";
155 print_instructions();
157 bitmap_raw = GP_LoadImage(sprite, NULL);
159 if (!bitmap_raw) {
160 fprintf(stderr, "Failed to load '%s'\n", sprite);
161 return 1;
164 win = GP_BackendInit(backend_opts, "Blit Test");
166 if (win == NULL) {
167 fprintf(stderr, "Failed to initalize backend '%s'\n",
168 backend_opts);
169 return 1;
172 bitmap_conv = GP_ContextConvertAlloc(bitmap_raw,
173 win->context->pixel_type);
174 change_bitmap();
176 black = GP_ColorToContextPixel(GP_COL_BLACK, win->context);
177 white = GP_ColorToContextPixel(GP_COL_WHITE, win->context);
179 GP_Fill(win->context, black);
180 GP_BackendFlip(win);
182 for (;;) {
183 GP_BackendPoll(win);
184 event_loop();
186 usleep(8000);
188 if (pause_flag)
189 continue;
191 redraw_screen();