Permalink
Browse files

Adding gfx.c

  • Loading branch information...
1 parent 5f6cd18 commit 372a790ff4a3b6a2dbf52c37883c35f6ee55cde9 @mpolitzer committed May 7, 2012
Showing with 132 additions and 4 deletions.
  1. +6 −3 Makefile
  2. +16 −0 game.h
  3. +86 −0 gfx.c
  4. +24 −1 main.c
View
@@ -3,11 +3,14 @@ CC=gcc
CFLAGS=-Wall -O0 -funroll-loops -c -g -Wno-unused-function -Wno-unused-result
LDFLAGS=-O2
LDLIBS=-lm
-SOURCES=main.c game.c bind.c
+SOURCES=main.c game.c bind.c gfx.c
HEADERS=game.h bind.h
-CFLAGS+=`pkg-config swipl --cflags`
-LDLIBS+=`pkg-config swipl --libs`
+PACKAGES="allegro-5.0 allegro_ttf-5.0 allegro_image-5.0 allegro_dialog-5.0 \
+ allegro_primitives-5.0 gl glu swipl"
+
+CFLAGS+=`pkg-config $(PACKAGES) --cflags`
+LDLIBS+=`pkg-config $(PACKAGES) --libs`
OBJECTS=$(addsuffix .o, $(basename ${SOURCES}))
EXECUTABLE=main
View
@@ -5,6 +5,9 @@
#include <string.h>
#include <ctype.h>
+#include <allegro5/allegro.h>
+#include <allegro5/allegro_primitives.h>
+
#define NUM_HOSPITALS 4
#define NUM_STATIONS 4
#define NUM_ZOMBIES 60
@@ -60,6 +63,13 @@ struct game_info {
int goal_x, goal_y;
int was_hit;
+
+ ALLEGRO_DISPLAY *display;
+
+ ALLEGRO_TIMER *tick;
+ ALLEGRO_EVENT_QUEUE *evQ;
+
+
};
struct player {
@@ -98,4 +108,10 @@ void game_init(void);
void game_dump(void);
int game_check_border(int x, int y);
+#define die(error, ...) _die(error, __FILE__, __LINE__, __VA_ARGS__)
+void _die(int error, char* filename, int line, const char* format, ...);
+
+void gfx_init(int w, int h, int fps);
+void gfx_step(int x, int y);
+
#endif /* GAME_H */
View
@@ -0,0 +1,86 @@
+#include "game.h"
+
+#define MOVE_FRAMES 10
+static int scale;
+
+static inline void gfx_render(int x, int y, int frame)
+{
+ int i;
+ int scale = 20;
+ float render_pos[2];
+
+ al_clear_to_color(al_map_rgb(0,0,0));
+ /* tiles */
+ for (i=0; i<MAPH; i++) {
+ int j;
+ for (j=0; j<MAPW; j++) {
+ ALLEGRO_COLOR color = al_map_rgb( 50, 0, 0); /* R */
+ if (GI.map[j][i] == MAP_WALL)
+ color = al_map_rgb( 0, 0, 0); /* X */
+
+ al_draw_filled_rectangle(scale*i, scale*j,
+ scale*(i + 1), scale*(j + 1),
+ color);
+ }
+ }
+#if 0
+ /* prize */
+ for (i=0; i<G.gi.num_prizes; i++) {
+ float x = G.gi.prizes[i][0] + 0.5;
+ float y = G.gi.prizes[i][1] + 0.5;
+ al_draw_filled_triangle(scale*(x), scale*(y-0.3),
+ scale*(x+0.3), scale*(y+0.3),
+ scale*(x-0.3), scale*(y+0.3),
+ al_map_rgb(255, 255, 0));
+ }
+ /* end */
+ al_draw_filled_circle(scale * (G.gi.end[0]+0.5),
+ scale * (G.gi.end[1]+0.5), scale/2,
+ al_map_rgb(255, 0, 0));
+ render_pos[0]=prev_pos[0]+(float)frame*(pos[0] - prev_pos[0])/MOVE_FRAMES;
+ render_pos[1]=prev_pos[1]+(float)frame*(pos[1] - prev_pos[1])/MOVE_FRAMES;
+#endif
+ render_pos[0] = x;
+ render_pos[1] = y;
+
+ /* player */
+ al_draw_filled_circle(scale * (render_pos[0]+0.5),
+ scale * (render_pos[1]+0.5), scale/2,
+ al_map_rgb( 0, 0,255));
+ al_flip_display();
+}
+
+
+void gfx_init(int w, int h, int fps)
+{
+ if (!al_init())
+ die(1, "can't init allegro");
+ if (!(al_init_primitives_addon()))
+ die(2, "can't init primitives addon");
+ if (!(GI.display = al_create_display(w, h)))
+ die(3, "can't create display");
+ if (!(GI.evQ = al_create_event_queue()))
+ die(4, "can't create event queue");
+ if (!(GI.tick = al_create_timer(1.0 / (fps*MOVE_FRAMES))))
+ die(5, "tick died, invalid fps");
+
+ /* current position is the begining */
+
+ al_register_event_source(GI.evQ, al_get_display_event_source(GI.display));
+ al_register_event_source(GI.evQ, al_get_timer_event_source(GI.tick));
+ al_start_timer(GI.tick);
+}
+
+void gfx_step(int x, int y)
+{
+ int i;
+ ALLEGRO_EVENT ev;
+
+ for (i = 0; i < MOVE_FRAMES; i++) {
+ gfx_render(x, y, i);
+ do {
+ al_wait_for_event(GI.evQ, &ev);
+ } while (ev.type != ALLEGRO_EVENT_TIMER);
+ }
+}
+
View
@@ -13,20 +13,43 @@
*
* */
+#define die(error, ...) _die(error, __FILE__, __LINE__, __VA_ARGS__)
+void _die(int error, char* filename, int line, const char* format, ...)
+{
+ va_list args;
+
+ fprintf(stderr, "[%s:%d] ", filename, line);
+ va_start(args, format);
+ vfprintf(stderr, format, args);
+ va_end(args);
+ fprintf(stderr, "\n");
+ exit(error);
+}
+
+
int main(int argc, char *argv[])
{
FILE *f = fopen("in.txt", "r");
+ ALLEGRO_EVENT ev;
+
+ gfx_init(600, 600, 10);
game_init();
game_read(f);
register_binds();
+ al_wait_for_event(GI.evQ, &ev);
+
+ if (ev.type == ALLEGRO_EVENT_KEY_DOWN
+ && ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
+ return -1;
+
if (!PL_initialise(argc, argv)) {
fprintf(stderr, "failed to initialize prolog\n");
exit(1);
}
- game_dump();
+ gfx_step(player.x, player.y);
PL_halt(PL_toplevel() ? 0 : 1);
return 0;
}

0 comments on commit 372a790

Please sign in to comment.