logo
[eradicate] / src / dos / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "game.h"
5 #include "keyb.h"
6 #include "timer.h"
7 #include "gfx.h"
8 #include "logger.h"
9 #include "cdpmi.h"
10
11 static struct video_mode *vmode;
12 static int quit;
13
14 int main(int argc, char **argv)
15 {
16         void *fb_buf;
17         struct video_mode *vmodes;
18         int vmidx, status = 0;
19
20         init_logger("game.log");
21
22         init_timer(100);
23         kb_init(32);
24
25         if(init_video() == -1) {
26                 return 1;
27         }
28         vmodes = video_modes();
29
30         if((vmidx = match_video_mode(640, 480, 16)) == -1) {
31                 return 1;
32         }
33         if(!(vmem = set_video_mode(vmidx, 1))) {
34                 return 1;
35         }
36         vmode = vmodes + vmidx;
37
38         fb_width = vmode->xsz;
39         fb_height = vmode->ysz;
40         fb_size = vmode->pitch * vmode->ysz;
41
42         if(!(fb_buf = malloc(fb_size + vmode->pitch * 2))) {
43                 fprintf(stderr, "failed to allocate framebuffer\n");
44                 status = -1;
45                 goto break_evloop;
46         }
47         fb_pixels = (char*)fb_buf + vmode->pitch;
48
49         if(init(argc, argv) == -1) {
50                 status = -1;
51                 goto break_evloop;
52         }
53
54         reset_timer();
55
56         for(;;) {
57                 int key;
58                 if(key_event) {
59                         while((key = kb_getkey()) != -1) {
60                                 key_event(key, 1);
61                         }
62                 } else {
63                         while((key = kb_getkey()) != -1) {
64                                 if(key == 27) goto break_evloop;
65                         }
66                 }
67                 if(quit) goto break_evloop;
68
69                 time_msec = get_msec();
70                 draw();
71         }
72
73 break_evloop:
74         free(fb_buf);
75         cleanup();
76         set_text_mode();
77         cleanup_video();
78         kb_shutdown();
79         return status;
80 }
81
82 void game_quit(void)
83 {
84         quit = 1;
85 }