This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project gfxprim.git.
The branch, master has been updated via 5109c64bfb2ac61e8fe89114c3843c0782e22543 (commit) from 1a93c45d484a05a1e5e8ca1cf1a0046e10d3398d (commit)
Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below.
- Log ----------------------------------------------------------------- http://repo.or.cz/w/gfxprim.git/commit/5109c64bfb2ac61e8fe89114c3843c0782e22...
commit 5109c64bfb2ac61e8fe89114c3843c0782e22543 Author: Cyril Hrubis metan@ucw.cz Date: Tue Jan 29 22:43:35 2013 +0100
demos: Start bogoman.
diff --git a/demos/Makefile b/demos/Makefile index ec4885b..1943f91 100644 --- a/demos/Makefile +++ b/demos/Makefile @@ -1,3 +1,3 @@ TOPDIR=.. -SUBDIRS=grinder spiv particle ttf2img c_simple +SUBDIRS=grinder spiv particle ttf2img c_simple bogoman include $(TOPDIR)/post.mk diff --git a/demos/bogoman/Makefile b/demos/bogoman/Makefile new file mode 100644 index 0000000..e90de8e --- /dev/null +++ b/demos/bogoman/Makefile @@ -0,0 +1,15 @@ +TOPDIR=../.. + +CSOURCES=$(shell echo *.c) + +INCLUDE= +LDFLAGS+=-L$(TOPDIR)/build/ +LDLIBS+=`$(TOPDIR)/gfxprim-config --libs --libs-backends` + +APPS=bogoman + +bogoman: bogoman_map.o bogoman_debug.o bogoman_loader.o bogoman_render.o + +include $(TOPDIR)/pre.mk +include $(TOPDIR)/app.mk +include $(TOPDIR)/post.mk diff --git a/demos/bogoman/bogoman.c b/demos/bogoman/bogoman.c new file mode 100644 index 0000000..f6f969b --- /dev/null +++ b/demos/bogoman/bogoman.c @@ -0,0 +1,149 @@ +/***************************************************************************** + * This file is part of gfxprim library. * + * * + * Gfxprim is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * Gfxprim is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with gfxprim; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, * + * Boston, MA 02110-1301 USA * + * * + * Copyright (C) 2009-2013 Cyril Hrubis metan@ucw.cz * + * * + *****************************************************************************/ + +#include <GP.h> + +#include "bogoman_debug.h" +#include "bogoman_map.h" +#include "bogoman_loader.h" +#include "bogoman_render.h" + +#define ELEM_SIZE 33 + +static void save_png(struct bogoman_map *map, unsigned int elem_size, + const char *filename) +{ + GP_Context *ctx; + unsigned int rx, ry; + + rx = elem_size * map->w; + ry = elem_size * map->h; + + ctx = GP_ContextAlloc(rx, ry, GP_PIXEL_RGB888); + + if (ctx == NULL) + return; + + struct bogoman_render render = { + .map = map, + .map_x_offset = 0, + .map_y_offset = 0, + .ctx = ctx, + .map_elem_size = elem_size, + }; + + bogoman_render(&render, BOGOMAN_RENDER_ALL); + + GP_SavePNG(ctx, filename, NULL); + GP_ContextFree(ctx); +} + +static struct GP_Backend *backend; + +static void event_loop(struct bogoman_map *map) +{ + while (GP_EventsQueued()) { + GP_Event ev; + + GP_EventGet(&ev); + + switch (ev.type) { + case GP_EV_KEY: + if (ev.code != GP_EV_KEY_DOWN) + break; + + switch (ev.val.val) { + case GP_KEY_ESC: + GP_BackendExit(backend); + exit(0); + break; + case GP_KEY_RIGHT: + bogoman_map_player_move(map, 1, 0); + break; + case GP_KEY_LEFT: + bogoman_map_player_move(map, -1, 0); + break; + case GP_KEY_UP: + bogoman_map_player_move(map, 0, -1); + break; + case GP_KEY_DOWN: + bogoman_map_player_move(map, 0, 1); + break; + } + break; + } + } + + +} + +int main(int argc, char *argv[]) +{ + struct bogoman_map *map; + + bogoman_set_dbg_level(10); + + if (argc > 1) + map = bogoman_load(argv[1]); + else + map = bogoman_load("map.txt"); + + if (map == NULL) { + fprintf(stderr, "Failed to load map"); + return 1; + } + + bogoman_map_dump(map); + + unsigned int cw = map->w * ELEM_SIZE; + unsigned int ch = map->h * ELEM_SIZE; + + backend = GP_BackendX11Init(NULL, 0, 0, cw, ch, "Bogoman", 0); + + if (backend == NULL) { + fprintf(stderr, "Failed to initialize backend"); + return 1; + } + + struct bogoman_render render = { + .map = map, + .map_x_offset = 0, + .map_y_offset = 0, + .ctx = backend->context, + .map_elem_size = ELEM_SIZE, + }; + + bogoman_render(&render, BOGOMAN_RENDER_ALL); + GP_BackendFlip(backend); + + for (;;) { + GP_BackendPoll(backend); + event_loop(map); + + bogoman_render(&render, BOGOMAN_RENDER_DIRTY); + GP_BackendFlip(backend); + + usleep(100000); + } + + return 0; +} diff --git a/demos/bogoman/bogoman_common.h b/demos/bogoman/bogoman_common.h new file mode 100644 index 0000000..39367a5 --- /dev/null +++ b/demos/bogoman/bogoman_common.h @@ -0,0 +1,37 @@ +/***************************************************************************** + * This file is part of gfxprim library. * + * * + * Gfxprim is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * Gfxprim is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with gfxprim; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, * + * Boston, MA 02110-1301 USA * + * * + * Copyright (C) 2009-2013 Cyril Hrubis metan@ucw.cz * + * * + *****************************************************************************/ + +#ifndef __BOGOMAN_COMMON_H__ +#define __BOGOMAN_COMMON_H__ + +#define SWAP(a, b) do { + typeof(a) tmp = b; + b = a; + a = tmp; +} while (0) + +#define SIGN(a) ({ + typeof(a) tmp = a; + (tmp < 0) ? -1 : (tmp > 0) ? 1 : 0; +}) + +#endif /* __BOGOMAN_COMMON_H__ */ diff --git a/demos/bogoman/bogoman_debug.c b/demos/bogoman/bogoman_debug.c new file mode 100644 index 0000000..d5ee213 --- /dev/null +++ b/demos/bogoman/bogoman_debug.c @@ -0,0 +1,51 @@ +/***************************************************************************** + * This file is part of gfxprim library. * + * * + * Gfxprim is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * Gfxprim is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with gfxprim; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, * + * Boston, MA 02110-1301 USA * + * * + * Copyright (C) 2009-2013 Cyril Hrubis metan@ucw.cz * + * * + *****************************************************************************/ + +#include <stdarg.h> +#include <stdio.h> + +#include "bogoman_debug.h" + +static unsigned int dbg_level = 0; + +void bogoman_dbg_print(unsigned int level, const char *file, const char *fn, + unsigned int line, const char *fmt, ...) +{ + if (level > dbg_level) + return; + + if (level == 0) + fprintf(stderr, "WARNING: %s:%s:%int", file, fn, line); + else + fprintf(stderr, "DEBUG %i:%s:%s:%i:nt", level, file, fn, line); + + va_list va; + + va_start(va, fmt); + vfprintf(stderr, fmt, va); + va_end(va); +} + +void bogoman_set_dbg_level(unsigned int level) +{ + dbg_level = level; +} diff --git a/demos/bogoman/bogoman_debug.h b/demos/bogoman/bogoman_debug.h new file mode 100644 index 0000000..b84729a --- /dev/null +++ b/demos/bogoman/bogoman_debug.h @@ -0,0 +1,39 @@ +/***************************************************************************** + * This file is part of gfxprim library. * + * * + * Gfxprim is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * Gfxprim is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with gfxprim; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, * + * Boston, MA 02110-1301 USA * + * * + * Copyright (C) 2009-2013 Cyril Hrubis metan@ucw.cz * + * * + *****************************************************************************/ + +#ifndef __BOGOMAN_DEBUG_H__ +#define __BOGOMAN_DEBUG_H__ + +#define DEBUG(level, ...) + bogoman_dbg_print(level, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__) + +#define WARN(...) + bogoman_dbg_print(0, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__) + + +void bogoman_dbg_print(unsigned int level, const char *file, const char *fn, + unsigned int line, const char *fmt, ...) + __attribute__ ((format (printf, 5, 6))); + +void bogoman_set_dbg_level(unsigned int level); + +#endif /* __BOGOMAN_DEBUG_H__ */ diff --git a/demos/bogoman/bogoman_loader.c b/demos/bogoman/bogoman_loader.c new file mode 100644 index 0000000..e1f3eed --- /dev/null +++ b/demos/bogoman/bogoman_loader.c @@ -0,0 +1,220 @@ +/***************************************************************************** + * This file is part of gfxprim library. * + * * + * Gfxprim is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * Gfxprim is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with gfxprim; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, * + * Boston, MA 02110-1301 USA * + * * + * Copyright (C) 2009-2013 Cyril Hrubis metan@ucw.cz * + * * + *****************************************************************************/ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "bogoman_debug.h" +#include "bogoman_common.h" +#include "bogoman_loader.h" + +/* + * Counts map w and h by reading number of lines and maximal size of line till + * first empty line or EOF. + */ +static void get_map_info(FILE *f, unsigned int *w, unsigned int *h) +{ + unsigned int curw = 0; + int ch; + + *w = 0; + *h = 0; + + while ((ch = getc(f)) != EOF) { + switch (ch) { + case 'n': + if (*w < curw) + *w = curw; + + if (curw == 0) { + rewind(f); + return; + } + + curw = 0; + (*h)++; + break; + default: + curw++; + } + } + + rewind(f); +} + +static enum bogoman_map_elem_id id_from_char(const char ch) +{ + switch (ch) { + case ' ': + return BOGOMAN_NONE; + case '@': + return BOGOMAN_PLAYER; + case '-': + case '|': + case '+': + case '/': + case '': + return BOGOMAN_WALL; + case '$': + return BOGOMAN_DIAMOND; + case 'M': + return BOGOMAN_MOVEABLE; + case 'E': + return BOGOMAN_EDIBLE; + } + + WARN("Unknown map character '%c'", ch); + + return BOGOMAN_NONE; +} + +#define LINE_MAX 256 + +struct line { + unsigned int len; + unsigned char line[LINE_MAX]; +}; + +static void get_line(FILE *f, struct line *l) +{ + int ch; + + l->len = 0; + + while ((ch = getc(f)) != EOF) { + switch (ch) { + case 'n': + return; + default: + l->line[l->len++] = id_from_char(ch); + break; + } + } +} + +static void load_map(FILE *f, struct bogoman_map *map) +{ + struct line line_a, line_b; + struct line *line_cur, *line_next; + unsigned int y = 0; + unsigned int player_found = 0; + + line_cur = &line_a; + line_next = &line_b; + + get_line(f, line_cur); + get_line(f, line_next); + + while (line_cur->len != 0) { + unsigned int x; + + for (x = 0; x < line_cur->len; x++) { + struct bogoman_map_elem *elem; + + elem = bogoman_get_map_elem(map, x, y); + + elem->id = line_cur->line[x]; + + switch (elem->id) { + /* Compute wall continuations */ + case BOGOMAN_WALL: + if (x > 0 && + line_cur->line[x - 1] == BOGOMAN_WALL) + elem->flags |= BOGOMAN_WALL_LEFT; + + if (x + 1 < line_cur->len && + line_cur->line[x + 1] == BOGOMAN_WALL) + elem->flags |= BOGOMAN_WALL_RIGHT; + + if (y > 0 && + bogoman_map_is_id(map, x, y-1, BOGOMAN_WALL)) + elem->flags |= BOGOMAN_WALL_UP; + + if (x < line_next->len && + line_next->line[x] == BOGOMAN_WALL) + elem->flags |= BOGOMAN_WALL_DOWN; + + break; + case BOGOMAN_PLAYER: + if (player_found) + WARN("Duplicated player at %ux%u previously at %ux%un", + x, y, map->player_x, map->player_y); + + map->player_x = x; + map->player_y = y; + + player_found = 1; + break; + case BOGOMAN_DIAMOND: + map->diamonds_total++; + break; + default: + break; + } + } + + SWAP(line_cur, line_next); + get_line(f, line_next); + y++; + } + + if (!player_found) + WARN("No player found in mapn"); +} + +struct bogoman_map *bogoman_load(const char *path) +{ + FILE *f = fopen(path, "r"); + + if (f == NULL) + return NULL; + + unsigned int w, h; + + get_map_info(f, &w, &h); + + DEBUG(1, "Have map %ux%un", w, h); + + struct bogoman_map *map; + size_t map_size; + + map_size = sizeof(struct bogoman_map) + + w * h * sizeof(struct bogoman_map_elem); + + map = malloc(map_size); + + if (map == NULL) + goto err0; + + memset(map, 0, map_size); + + map->w = w; + map->h = h; + + load_map(f, map); + + return map; +err0: + fclose(f); + return NULL; +} diff --git a/demos/bogoman/bogoman_loader.h b/demos/bogoman/bogoman_loader.h new file mode 100644 index 0000000..027efd4 --- /dev/null +++ b/demos/bogoman/bogoman_loader.h @@ -0,0 +1,30 @@ +/***************************************************************************** + * This file is part of gfxprim library. * + * * + * Gfxprim is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * Gfxprim is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with gfxprim; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, * + * Boston, MA 02110-1301 USA * + * * + * Copyright (C) 2009-2013 Cyril Hrubis metan@ucw.cz * + * * + *****************************************************************************/ + +#ifndef __BOGOMAN_LOADER_H__ +#define __BOGOMAN_LOADER_H__ + +#include "bogoman_map.h" + +struct bogoman_map *bogoman_load(const char *path); + +#endif /* __BOGOMAN_LOADER_H__ */ diff --git a/demos/bogoman/bogoman_map.c b/demos/bogoman/bogoman_map.c new file mode 100644 index 0000000..2f62c64 --- /dev/null +++ b/demos/bogoman/bogoman_map.c @@ -0,0 +1,245 @@ +/***************************************************************************** + * This file is part of gfxprim library. * + * * + * Gfxprim is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * Gfxprim is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with gfxprim; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, * + * Boston, MA 02110-1301 USA * + * * + * Copyright (C) 2009-2013 Cyril Hrubis metan@ucw.cz * + * * + *****************************************************************************/ + +#include <stdio.h> + +#include "bogoman_common.h" +#include "bogoman_debug.h" + +#include "bogoman_map.h" + +static void print_wall(struct bogoman_map_elem *elem) +{ + switch (elem->flags) { + case BOGOMAN_WALL_LEFT: + case BOGOMAN_WALL_RIGHT: + case BOGOMAN_WALL_LEFT | BOGOMAN_WALL_RIGHT: + printf("-"); + break; + case BOGOMAN_WALL_UP: + case BOGOMAN_WALL_DOWN: + case BOGOMAN_WALL_UP | BOGOMAN_WALL_DOWN: + printf("|"); + break; + case BOGOMAN_WALL_UP | BOGOMAN_WALL_RIGHT: + case BOGOMAN_WALL_DOWN | BOGOMAN_WALL_LEFT: + printf(""); + break; + case BOGOMAN_WALL_UP | BOGOMAN_WALL_LEFT: + case BOGOMAN_WALL_DOWN | BOGOMAN_WALL_RIGHT: + printf("/"); + break; + default: + printf("+"); + break; + } +} + +void bogoman_map_dump(struct bogoman_map *map) +{ + unsigned int x, y; + + for (y = 0; y < map->h; y++) { + for (x = 0; x < map->w; x++) { + struct bogoman_map_elem *elem; + + elem = bogoman_get_map_elem(map, x, y); + + switch (elem->id) { + case BOGOMAN_NONE: + printf(" "); + break; + case BOGOMAN_PLAYER: + printf("@"); + break; + case BOGOMAN_DIAMOND: + printf("$"); + break; + case BOGOMAN_MOVEABLE: + printf("M"); + break; + case BOGOMAN_EDIBLE: + printf("E"); + break; + case BOGOMAN_WALL: + print_wall(elem); + break; + } + } + + printf("n"); + } + + printf("Player at %ux%u diamonds total %un", + map->player_x, map->player_y, map->diamonds_total); +} + +static void copy_block(struct bogoman_map *map, + int dst_x, int dst_y, + int src_x, int src_y) +{ + struct bogoman_map_elem *src, *dst; + + src = bogoman_get_map_elem(map, src_x, src_y); + dst = bogoman_get_map_elem(map, dst_x, dst_y); + + *dst = *src; + + dst->dirty = 1; +} + +static void clear_block(struct bogoman_map *map, + unsigned int x, unsigned int y) +{ + struct bogoman_map_elem *block; + + block = bogoman_get_map_elem(map, x, y); + + block->id = BOGOMAN_NONE; + block->dirty = 1; +} + +/* + * Removes diamond when player has stepped on it. + */ +static void move_get_diamond(struct bogoman_map *map, + unsigned int x, unsigned int y) +{ + clear_block(map, x, y); + + map->player_diamonds++; + + DEBUG(1, "Diamond taken, yet to collect %un", + map->diamonds_total - map->player_diamonds); +} + +/* + * Moves block to empty space, only one of dx or dy can be set + * to nonzero value and the value could only be 1 or -1. + */ +static int try_move_block(struct bogoman_map *map, + int x, int y, int dx, int dy) +{ + struct bogoman_map_elem *elem = bogoman_get_map_elem(map, x, y); + + int new_x = (int)x + dx; + int new_y = (int)y + dy; + + if (bogoman_coord_in_map(map, new_x, new_y)) { + + if (!bogoman_is_empty(map, new_x, new_y)) + return 0; + + copy_block(map, new_x, new_y, x, y); + } else { + DEBUG(1, "Block moved out of screen %ux%u -> %ix%i", + x, y, new_x, new_y); + } + + clear_block(map, x, y); + return 1; +} + +void bogoman_map_player_move(struct bogoman_map *map, int x, int y) +{ + if (x != 0 && y != 0) { + WARN("Player can move only in one directionn"); + return; + } + + int new_x = map->player_x + x; + int new_y = map->player_y + y; + + if (new_x < 0 || new_x >= (int)map->w || + new_y < 0 || new_y >= (int)map->h) { + WARN("Player can move only in screen got %ix%in", + new_x, new_y); + return; + } + + int player_x = map->player_x; + int player_y = map->player_y; + + DEBUG(1, "Player %ix%i -> %ix%in", player_x, player_y, new_x, new_y); + + while (player_x != new_x || player_y != new_y) { + int dx = SIGN(new_x - player_x); + int dy = SIGN(new_y - player_y); + + int px = map->player_x + dx; + int py = map->player_y + dy; + + switch (bogoman_get_map_elem_id(map, px, py)) { + case BOGOMAN_NONE: + break; + case BOGOMAN_DIAMOND: + move_get_diamond(map, px, py); + break; + case BOGOMAN_MOVEABLE: + if (!try_move_block(map, px, py, dx, dy)) + goto finish_move; + break; + case BOGOMAN_EDIBLE: + clear_block(map, px, py); + break; + default: + goto finish_move; + } + + player_x = px; + player_y = py; + } + + /* Update the map */ + struct bogoman_map_elem *player, *space; + +finish_move: + + player = bogoman_get_map_elem(map, map->player_x, map->player_y); + space = bogoman_get_map_elem(map, player_x, player_y); + + *player = map->under_player; + map->under_player = *space; + space->id = BOGOMAN_PLAYER; + + map->player_x = player_x; + map->player_y = player_y; + + /* turn on dirty flags */ + player->dirty = 1; + space->dirty = 1; +} + +void bogoman_map_timer_tick(struct bogoman_map *map) +{ + unsigned int x, y; + + for (y = 0; y < map->h; y++) { + for (x = 0; x < map->w; x++) { + struct bogoman_map_elem *elem; + + elem = bogoman_get_map_elem(map, x, y); + + //TODO + } + } +} diff --git a/demos/bogoman/bogoman_map.h b/demos/bogoman/bogoman_map.h new file mode 100644 index 0000000..b178dcf --- /dev/null +++ b/demos/bogoman/bogoman_map.h @@ -0,0 +1,124 @@ +/***************************************************************************** + * This file is part of gfxprim library. * + * * + * Gfxprim is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * Gfxprim is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with gfxprim; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, * + * Boston, MA 02110-1301 USA * + * * + * Copyright (C) 2009-2013 Cyril Hrubis metan@ucw.cz * + * * + *****************************************************************************/ + +#ifndef __BOGOMAN_MAP_H__ +#define __BOGOMAN_MAP_H__ + +enum bogoman_map_elem_id { + /* empty walkable space */ + BOGOMAN_NONE = 0x00, + /* player */ + BOGOMAN_PLAYER = 0x01, + /* wall */ + BOGOMAN_WALL = 0x02, + /* diamons to collect */ + BOGOMAN_DIAMOND = 0x03, + /* like a wall but player moveable */ + BOGOMAN_MOVEABLE = 0x04, + /* like a diamond but doesn't counts to points */ + BOGOMAN_EDIBLE = 0x05, + + BOGOMAN_MAX = BOGOMAN_EDIBLE, +}; + +/* + * Wall cal be applied as bitflags. Each bitflag determines wall continuation + * in particular direction. + */ +enum bogoman_wall_flags { + BOGOMAN_WALL_LEFT = 0x01, + BOGOMAN_WALL_RIGHT = 0x02, + BOGOMAN_WALL_UP = 0x04, + BOGOMAN_WALL_DOWN = 0x08, +}; + +struct bogoman_map_elem { + unsigned char id; + unsigned char flags; + + /* the element changed, needs to be redrawn */ + unsigned char dirty:1; +}; + +struct bogoman_map { + unsigned int w; + unsigned int h; + + unsigned int diamonds_total; + + /* player data */ + unsigned int player_x; + unsigned int player_y; + unsigned int player_diamonds; + /* used to save element player steps on */ + struct bogoman_map_elem under_player; + + struct bogoman_map_elem map[]; +}; + +static inline struct bogoman_map_elem * + bogoman_get_map_elem(struct bogoman_map *map, + unsigned int x, unsigned int y) +{ + return &(map->map[x + y * map->w]); +} + +static inline enum bogoman_map_elem_id + bogoman_get_map_elem_id(struct bogoman_map *map, + unsigned int x, unsigned int y) +{ + struct bogoman_map_elem *elem = bogoman_get_map_elem(map, x, y); + + return elem->id; +} + +static inline int bogoman_map_is_id(struct bogoman_map *map, + unsigned int x, unsigned int y, + enum bogoman_map_elem_id id) +{ + struct bogoman_map_elem *elem = bogoman_get_map_elem(map, x, y); + + return elem->id == id; +} + +static inline int bogoman_coord_in_map(struct bogoman_map *map, int x, int y) +{ + return (x >= 0) && ((unsigned)x < map->w) && + (y >= 0) && ((unsigned)y < map->w); +} + +static inline int bogoman_is_empty(struct bogoman_map *map, + unsigned int x, unsigned int y) +{ + return bogoman_get_map_elem_id(map, x, y) == BOGOMAN_NONE; +} + +void bogoman_map_player_move(struct bogoman_map *map, int x, int y); + +void bogoman_map_timer_tick(struct bogoman_map *map); + +/* + * Dumps map into the stdout. + */ +void bogoman_map_dump(struct bogoman_map *map); + +#endif /* __BOGOMAN_MAP_H__ */ diff --git a/demos/bogoman/bogoman_render.c b/demos/bogoman/bogoman_render.c new file mode 100644 index 0000000..43a09bd --- /dev/null +++ b/demos/bogoman/bogoman_render.c @@ -0,0 +1,198 @@ +/***************************************************************************** + * This file is part of gfxprim library. * + * * + * Gfxprim is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * Gfxprim is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with gfxprim; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, * + * Boston, MA 02110-1301 USA * + * * + * Copyright (C) 2009-2013 Cyril Hrubis metan@ucw.cz * + * * + *****************************************************************************/ + +#include <GP.h> + +#include "bogoman_map.h" +#include "bogoman_debug.h" + +#include "bogoman_render.h" + +struct render_colors { + /* global background */ + GP_Pixel bg; + + /* player color */ + GP_Pixel player; + + /* frames around things */ + GP_Pixel frames; + + /* diamod color */ + GP_Pixel diamond; + + /* wall color */ + GP_Pixel wall; + + /* moveable color */ + GP_Pixel moveable; + + /* edible color */ + GP_Pixel edible; +}; + +static struct render_colors colors; + +static void init_colors(GP_Context *ctx, struct render_colors *colors) +{ + colors->bg = GP_RGBToContextPixel(0xee, 0xee, 0xee, ctx); + colors->player = GP_RGBToContextPixel(0x00, 0xee, 0x00, ctx); + colors->frames = GP_RGBToContextPixel(0x00, 0x00, 0x00, ctx); + colors->diamond = GP_RGBToContextPixel(0x00, 0x00, 0xee, ctx); + colors->wall = GP_RGBToContextPixel(0x66, 0x66, 0x66, ctx); + colors->moveable = GP_RGBToContextPixel(0xff, 0xff, 0x60, ctx); + colors->edible = GP_RGBToContextPixel(0xff, 0x7f, 0x50, ctx); +} + +static void render_none(struct bogoman_render *render, + unsigned int x, unsigned int y, + struct bogoman_map_elem *elem) +{ + unsigned int w = render->map_elem_size; + + (void) elem; + + GP_FillRectXYWH(render->ctx, x, y, w, w, colors.bg); +} + +static void render_player(struct bogoman_render *render, + unsigned int x, unsigned int y, + struct bogoman_map_elem *elem) +{ + unsigned int w = render->map_elem_size; + + (void) elem; + + GP_FillRectXYWH(render->ctx, x, y, w, w, colors.bg); + GP_FillCircle(render->ctx, x + w/2, y + w/2, w/2 - 1, colors.player); + GP_Circle(render->ctx, x + w/2, y + w/2, w/2 - 1, colors.frames); +} + +static void render_wall(struct bogoman_render *render, + unsigned int x, unsigned int y, + struct bogoman_map_elem *elem) +{ + unsigned int w = render->map_elem_size; + + GP_FillRectXYWH(render->ctx, x, y, w, w, colors.wall); + + if (!(elem->flags & BOGOMAN_WALL_LEFT)) + GP_VLineXYH(render->ctx, x, y, w, colors.frames); + + if (!(elem->flags & BOGOMAN_WALL_RIGHT)) + GP_VLineXYH(render->ctx, x + w - 1, y, w, colors.frames); + + if (!(elem->flags & BOGOMAN_WALL_UP)) + GP_HLineXYW(render->ctx, x, y, w, colors.frames); + + if (!(elem->flags & BOGOMAN_WALL_DOWN)) + GP_HLineXYW(render->ctx, x, y + w - 1, w, colors.frames); +} + +static void render_diamond(struct bogoman_render *render, + unsigned int x, unsigned int y, + struct bogoman_map_elem *elem) +{ + unsigned int w = render->map_elem_size; + + GP_FillRectXYWH(render->ctx, x, y, w, w, colors.bg); + + GP_FillTetragon(render->ctx, x + w/2, y, x + w - 1, y + w/2, + x + w/2, y + w - 1, x, y + w/2, colors.diamond); + + GP_Tetragon(render->ctx, x + w/2, y, x + w - 1, y + w/2, + x + w/2, y + w - 1, x, y + w/2, colors.frames); +} + +static void render_moveable(struct bogoman_render *render, + unsigned int x, unsigned int y, + struct bogoman_map_elem *elem) +{ + unsigned int w = render->map_elem_size; + + GP_FillRectXYWH(render->ctx, x, y, w, w, colors.bg); + + GP_FillRectXYWH(render->ctx, x + 1, y + 1, w - 2, w - 2, colors.moveable); + GP_RectXYWH(render->ctx, x + 1, y + 1, w - 2, w - 2, colors.frames); +} + +static void render_edible(struct bogoman_render *render, + unsigned int x, unsigned int y, + struct bogoman_map_elem *elem) +{ + unsigned int w = render->map_elem_size; + + GP_FillRectXYWH(render->ctx, x, y, w, w, colors.bg); + + GP_FillRectXYWH(render->ctx, x + 1, y + 1, w - 2, w - 2, colors.edible); +} + +static void (*renders[])(struct bogoman_render *render, + unsigned int x, unsigned int y, + struct bogoman_map_elem *elem) = +{ + render_none, + render_player, + render_wall, + render_diamond, + render_moveable, + render_edible, +}; + +static void render_elem(struct bogoman_render *render, + unsigned int x, unsigned int y, + int flags) +{ + struct bogoman_map_elem *elem; + unsigned int cx, cy; + + elem = bogoman_get_map_elem(render->map, x, y); + + if (elem->dirty) { + elem->dirty = 0; + } else { + if (flags & BOGOMAN_RENDER_DIRTY) + return; + } + + cx = (x - render->map_x_offset) * render->map_elem_size; + cy = (y - render->map_y_offset) * render->map_elem_size; + + if (elem->id > BOGOMAN_MAX) + WARN("Invalid elem ID %u at %ux%un", elem->id, x, y); + else + renders[elem->id](render, cx, cy, elem); +} + +void bogoman_render(struct bogoman_render *render, int flags) +{ + unsigned int x, y; + + //TODO: Hack + init_colors(render->ctx, &colors); + + for (y = render->map_x_offset; y < render->map->h; y++) { + for (x = render->map_x_offset; x < render->map->w; x++) { + render_elem(render, x, y, flags); + } + } +} diff --git a/demos/bogoman/bogoman_render.h b/demos/bogoman/bogoman_render.h new file mode 100644 index 0000000..72c6a48 --- /dev/null +++ b/demos/bogoman/bogoman_render.h @@ -0,0 +1,52 @@ +/***************************************************************************** + * This file is part of gfxprim library. * + * * + * Gfxprim is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * Gfxprim is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with gfxprim; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, * + * Boston, MA 02110-1301 USA * + * * + * Copyright (C) 2009-2013 Cyril Hrubis metan@ucw.cz * + * * + *****************************************************************************/ + +#ifndef __BOGOMAN_RENDER_H__ +#define __BOGOMAN_RENDER_H__ + +struct bogoman_map; +struct GP_Context; + +struct bogoman_render { + /* both in map elements */ + unsigned int map_x_offset; + unsigned int map_y_offset; + + /* current map */ + struct bogoman_map *map; + + /* context to be used for rendering */ + struct GP_Context *ctx; + + /* elem size in pixels */ + unsigned int map_elem_size; +}; + +enum bogonam_render_flags { + /* renders all map elements, not only dirty ones */ + BOGOMAN_RENDER_ALL = 0x00, + BOGOMAN_RENDER_DIRTY = 0x01, +}; + +void bogoman_render(struct bogoman_render *render, int flags); + +#endif /* __BOGOMAN_RENDER_H__ */ diff --git a/demos/bogoman/levels/01-warmup.txt b/demos/bogoman/levels/01-warmup.txt new file mode 100644 index 0000000..f0cc034 --- /dev/null +++ b/demos/bogoman/levels/01-warmup.txt @@ -0,0 +1,7 @@ +/-------+| M$$$M | +|M$M$M$M| +|$M$@$M$| +|M$M$M$M| +| M$$$M | +-------/ diff --git a/demos/bogoman/map.txt b/demos/bogoman/map.txt new file mode 100644 index 0000000..337d07c --- /dev/null +++ b/demos/bogoman/map.txt @@ -0,0 +1,8 @@ ++---------+ +----+ +| | | | | $ | +| +----+ | +| E @ | ++--M----------------+ +| M | +| |$|$|$| | ++----------+-+-+-+--+ diff --git a/demos/bogoman/runtest.sh b/demos/bogoman/runtest.sh new file mode 100755 index 0000000..0794707 --- /dev/null +++ b/demos/bogoman/runtest.sh @@ -0,0 +1,9 @@ +#!/bin/bash +# +# Run dynamically linked test. +# + +PROG="$1" +shift + +LD_LIBRARY_PATH=../../build/ ./$PROG "$@"
-----------------------------------------------------------------------
Summary of changes: demos/Makefile | 2 +- demos/{spiv => bogoman}/Makefile | 6 +- demos/bogoman/bogoman.c | 149 ++++++++++++ .../GP_Filter.h => demos/bogoman/bogoman_common.h | 26 +- .../bogoman/bogoman_debug.c | 44 ++-- .../bogoman/bogoman_debug.h | 32 +-- demos/bogoman/bogoman_loader.c | 220 ++++++++++++++++++ .../histogram.h => bogoman/bogoman_loader.h} | 12 +- demos/bogoman/bogoman_map.c | 245 ++++++++++++++++++++ demos/bogoman/bogoman_map.h | 124 ++++++++++ demos/bogoman/bogoman_render.c | 198 ++++++++++++++++ .../bogoman/bogoman_render.h | 53 +++-- demos/bogoman/levels/01-warmup.txt | 7 + demos/bogoman/map.txt | 8 + demos/{c_simple => bogoman}/runtest.sh | 0 15 files changed, 1036 insertions(+), 90 deletions(-) copy demos/{spiv => bogoman}/Makefile (52%) create mode 100644 demos/bogoman/bogoman.c copy include/filters/GP_Filter.h => demos/bogoman/bogoman_common.h (80%) copy libs/filters/GP_Cubic.gen.c.t => demos/bogoman/bogoman_debug.c (74%) copy libs/filters/GP_Cubic.gen.c.t => demos/bogoman/bogoman_debug.h (74%) create mode 100644 demos/bogoman/bogoman_loader.c copy demos/{grinder/histogram.h => bogoman/bogoman_loader.h} (87%) create mode 100644 demos/bogoman/bogoman_map.c create mode 100644 demos/bogoman/bogoman_map.h create mode 100644 demos/bogoman/bogoman_render.c copy libs/filters/GP_Cubic.gen.c.t => demos/bogoman/bogoman_render.h (73%) create mode 100644 demos/bogoman/levels/01-warmup.txt create mode 100644 demos/bogoman/map.txt copy demos/{c_simple => bogoman}/runtest.sh (100%)
repo.or.cz automatic notification. Contact project admin jiri.bluebear.dluhos@gmail.com if you want to unsubscribe, or site admin admin@repo.or.cz if you receive no reply.