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