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