eobish
view src/main.c @ 4:ce0548d24918
mostly works
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 18 Jan 2015 13:30:30 +0200 |
parents | e32bdd5fb622 |
children |
line source
1 #include <stdio.h>
2 #include "fblib.h"
3 #include "rend.h"
4 #include "level.h"
5 #include "player.h"
7 static int init(void);
8 static void cleanup(void);
9 static void display(void);
10 static int keyb(int key, int state, void *cls);
12 static struct player player;
13 static struct level level;
15 int main(int argc, char **argv)
16 {
17 if(init() == -1) {
18 return 1;
19 }
21 for(;;) {
22 if(fb_process_events() == -1) {
23 break;
24 }
25 display();
26 }
28 cleanup();
29 return 0;
30 }
32 static int init(void)
33 {
34 int i;
36 if(fb_init(320, 240, 8) == -1) {
37 return -1;
38 }
39 fb_keyboard_callback(keyb, 0);
41 level_init(&level);
42 if(level_load(&level, "data/0.lvl") == -1) {
43 return -1;
44 }
46 if(init_renderer() == -1) {
47 return -1;
48 }
50 player_init(&player, &level);
52 /* setup the level tileset palette
53 * TODO: do it properly
54 */
55 for(i=0; i<level.tileset.pal_size; i++) {
56 struct color *col = level.tileset.pal + i;
57 fb_set_palette_entry(i, col->r, col->g, col->b);
58 }
60 return 0;
61 }
63 static void cleanup(void)
64 {
65 shutdown_renderer();
66 level_destroy(&level);
67 fb_shutdown();
68 }
70 static void display(void)
71 {
72 int width = fb_get_width();
73 int height = fb_get_height();
74 unsigned char *pixels = (unsigned char*)fb_begin_frame();
76 setup_renderer(pixels, width, height);
77 render_level(&level, player.x, player.y, player.dir);
79 fb_end_frame();
80 }
82 static int keyb(int key, int state, void *cls)
83 {
84 if(!state) return 0;
86 switch(key) {
87 case 27:
88 return -1;
90 case 'w':
91 player_move(&player, 1, 0);
92 break;
94 case 's':
95 player_move(&player, -1, 0);
96 break;
98 case 'q':
99 player_move(&player, 0, -1);
100 break;
102 case 'e':
103 player_move(&player, 0, 1);
104 break;
106 case 'a':
107 player_turn(&player, -1);
108 break;
110 case 'd':
111 player_turn(&player, 1);
112 break;
114 default:
115 break;
116 }
118 return 0;
119 }