Rename GP_Context -> GP_Pixmap
[gfxprim.git] / demos / c_simple / blittest.c
blobc12c8fe70b35b053b1b01a902df5f7812848a34b
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 <stdio.h>
27 #include <GP.h>
29 static GP_Pixel black;
30 static GP_Pixel white;
32 static GP_Backend *win;
34 static GP_Pixmap *bitmap, *bitmap_raw, *bitmap_conv;
35 static int bitmap_x, bitmap_y, bitmap_vx = -3, bitmap_vy = -3;
36 static int pause_flag = 0;
38 static char text_buf[255];
40 void redraw_screen(void)
42 bitmap_x += bitmap_vx;
43 bitmap_y += bitmap_vy;
45 if (bitmap_x + GP_PixmapW(bitmap) > win->pixmap->w) {
46 bitmap_vx = -bitmap_vx;
47 bitmap_x += bitmap_vx;
50 if (bitmap_x < 0) {
51 bitmap_vx = -bitmap_vx;
52 bitmap_x += bitmap_vx;
55 if (bitmap_y + GP_PixmapH(bitmap) > win->pixmap->h) {
56 bitmap_vy = -bitmap_vy;
57 bitmap_y += bitmap_vy;
60 if (bitmap_y < 0) {
61 bitmap_vy = -bitmap_vy;
62 bitmap_y += bitmap_vy;
65 GP_FillRectXYWH(win->pixmap, 20, 20, 300, 50, black);
67 GP_Text(win->pixmap, NULL, 20, 20, GP_ALIGN_RIGHT|GP_VALIGN_BOTTOM,
68 white, black, text_buf);
70 GP_Print(win->pixmap, NULL, 250, 20, GP_ALIGN_RIGHT|GP_VALIGN_BOTTOM,
71 white, black, "%c|%c|%c", bitmap->x_swap ? 'x' : ' ',
72 bitmap->y_swap ? 'y' : ' ', bitmap->axes_swap ? 'a' : ' ');
74 GP_Blit(bitmap, 0, 0, GP_PixmapW(bitmap), GP_PixmapH(bitmap),
75 win->pixmap, bitmap_x, bitmap_y);
77 GP_BackendUpdateRectXYWH(win, bitmap_x, bitmap_y,
78 GP_PixmapW(bitmap), GP_PixmapH(bitmap));
79 GP_BackendUpdateRectXYWH(win, 20, 20, 400, 50);
82 static void change_bitmap(void)
84 if (bitmap == bitmap_raw)
85 bitmap = bitmap_conv;
86 else
87 bitmap = bitmap_raw;
89 snprintf(text_buf, sizeof(text_buf), "'%s' -> '%s'",
90 GP_PixelTypeName(bitmap->pixel_type),
91 GP_PixelTypeName(win->pixmap->pixel_type));
94 void event_loop(void)
96 GP_Event ev;
98 while (GP_BackendGetEvent(win, &ev)) {
99 GP_EventDump(&ev);
101 switch (ev.type) {
102 case GP_EV_KEY:
103 if (ev.code != GP_EV_KEY_DOWN)
104 continue;
106 switch (ev.val.key.key) {
107 case GP_KEY_X:
108 bitmap->x_swap = !bitmap->x_swap;
109 break;
110 case GP_KEY_Y:
111 bitmap->y_swap = !bitmap->y_swap;
112 break;
113 case GP_KEY_R:
114 bitmap->axes_swap = !bitmap->axes_swap;
115 break;
116 case GP_KEY_P:
117 pause_flag = !pause_flag;
118 break;
119 case GP_KEY_SPACE:
120 change_bitmap();
121 break;
122 case GP_KEY_ESC:
123 GP_BackendExit(win);
124 exit(0);
125 break;
127 break;
128 case GP_EV_SYS:
129 switch(ev.code) {
130 case GP_EV_SYS_QUIT:
131 GP_BackendExit(win);
132 exit(0);
133 break;
134 case GP_EV_SYS_RESIZE:
135 GP_BackendResizeAck(win);
136 GP_Fill(win->pixmap, black);
137 GP_BackendFlip(win);
138 break;
140 break;
145 void print_instructions(void)
147 printf("Use the following keys to control the test:\n");
148 printf(" Esc ............. exit\n");
149 printf(" Space ........... converts bitmap to screen pixel format\n");
150 printf(" R ............... swap sprite axes\n");
151 printf(" X ............... mirror sprite X\n");
152 printf(" Y ............... mirror sprite Y\n");
153 printf(" P ............... pause\n");
156 int main(void)
158 const char *sprite = "ball.ppm";
159 const char *backend_opts = "X11";
161 print_instructions();
163 bitmap_raw = GP_LoadImage(sprite, NULL);
165 if (!bitmap_raw) {
166 fprintf(stderr, "Failed to load '%s'\n", sprite);
167 return 1;
170 win = GP_BackendInit(backend_opts, "Blit Test");
172 if (win == NULL) {
173 fprintf(stderr, "Failed to initalize backend '%s'\n",
174 backend_opts);
175 return 1;
178 bitmap_conv = GP_PixmapConvertAlloc(bitmap_raw,
179 win->pixmap->pixel_type);
180 change_bitmap();
182 black = GP_RGBToPixmapPixel(0x00, 0x00, 0x00, win->pixmap);
183 white = GP_RGBToPixmapPixel(0xff, 0xff, 0xff, win->pixmap);
185 GP_Fill(win->pixmap, black);
186 GP_BackendFlip(win);
188 for (;;) {
189 GP_BackendPoll(win);
190 event_loop();
192 usleep(8000);
194 if (pause_flag)
195 continue;
197 redraw_screen();