text: Add va_list variant for GP_Print()
[gfxprim/pasky.git] / demos / bogoman / bogoman.c
blob0f745847c12d40548e6b08854e544b7d5a8fa1c5
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-2013 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
23 #include <GP.h>
25 #include "bogoman_debug.h"
26 #include "bogoman_map.h"
27 #include "bogoman_loader.h"
28 #include "bogoman_render.h"
30 #define ELEM_SIZE 33
32 static void save_png(struct bogoman_map *map, unsigned int elem_size,
33 const char *filename)
35 GP_Context *ctx;
36 unsigned int rx, ry;
38 rx = elem_size * map->w;
39 ry = elem_size * map->h;
41 ctx = GP_ContextAlloc(rx, ry, GP_PIXEL_RGB888);
43 if (ctx == NULL)
44 return;
46 struct bogoman_render render = {
47 .map = map,
48 .map_x_offset = 0,
49 .map_y_offset = 0,
50 .ctx = ctx,
51 .map_elem_size = elem_size,
54 bogoman_render(&render, BOGOMAN_RENDER_ALL);
56 GP_SavePNG(ctx, filename, NULL);
57 GP_ContextFree(ctx);
60 static struct GP_Backend *backend;
62 static void event_loop(struct bogoman_map *map)
64 while (GP_BackendEventsQueued(backend)) {
65 GP_Event ev;
67 GP_BackendGetEvent(backend, &ev);
69 switch (ev.type) {
70 case GP_EV_KEY:
71 if (ev.code != GP_EV_KEY_DOWN)
72 break;
74 switch (ev.val.val) {
75 case GP_KEY_ESC:
76 GP_BackendExit(backend);
77 exit(0);
78 break;
79 case GP_KEY_RIGHT:
80 bogoman_map_player_move(map, 1, 0);
81 break;
82 case GP_KEY_LEFT:
83 bogoman_map_player_move(map, -1, 0);
84 break;
85 case GP_KEY_UP:
86 bogoman_map_player_move(map, 0, -1);
87 break;
88 case GP_KEY_DOWN:
89 bogoman_map_player_move(map, 0, 1);
90 break;
92 break;
99 int main(int argc, char *argv[])
101 struct bogoman_map *map;
103 bogoman_set_dbg_level(10);
105 if (argc > 1)
106 map = bogoman_load(argv[1]);
107 else
108 map = bogoman_load("map.txt");
110 if (map == NULL) {
111 fprintf(stderr, "Failed to load map");
112 return 1;
115 bogoman_map_dump(map);
117 unsigned int cw = map->w * ELEM_SIZE;
118 unsigned int ch = map->h * ELEM_SIZE;
120 backend = GP_BackendX11Init(NULL, 0, 0, cw, ch, "Bogoman", 0);
122 if (backend == NULL) {
123 fprintf(stderr, "Failed to initialize backend");
124 return 1;
127 struct bogoman_render render = {
128 .map = map,
129 .map_x_offset = 0,
130 .map_y_offset = 0,
131 .ctx = backend->context,
132 .map_elem_size = ELEM_SIZE,
135 bogoman_render(&render, BOGOMAN_RENDER_ALL);
136 GP_BackendFlip(backend);
138 for (;;) {
139 GP_BackendPoll(backend);
140 event_loop(map);
142 bogoman_render(&render, BOGOMAN_RENDER_DIRTY);
143 GP_BackendFlip(backend);
145 usleep(50000);
148 return 0;