added two new global pointers in demo.h: vmem_back and vmem_front, and
[dosdemo] / src / dos / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <limits.h>
5 #include "demo.h"
6 #include "keyb.h"
7 #include "mouse.h"
8 #include "timer.h"
9 #include "gfx.h"
10
11 static int quit;
12 static int use_mouse;
13 static long fbsize;
14
15 int main(int argc, char **argv)
16 {
17         fbsize = fb_width * fb_height * fb_bpp / CHAR_BIT;
18
19         init_timer(100);
20         kb_init(32);
21
22         if((use_mouse = have_mouse())) {
23                 set_mouse_limits(0, 0, fb_width, fb_height);
24                 set_mouse(fb_width / 2, fb_height / 2);
25         }
26
27         if(!(fb_pixels = malloc(fbsize))) {
28                 fprintf(stderr, "failed to allocate backbuffer\n");
29                 return 1;
30         }
31
32         if(!(vmem_front = set_video_mode(fb_width, fb_height, fb_bpp))) {
33                 return 1;
34         }
35         /* TODO implement multiple video memory pages for flipping */
36         vmem_back = vmem_front;
37
38         if(demo_init(argc, argv) == -1) {
39                 set_text_mode();
40                 return 1;
41         }
42         reset_timer();
43
44         while(!quit) {
45                 int key;
46                 while((key = kb_getkey()) != -1) {
47                         demo_keyboard(key, 1);
48                 }
49                 if(quit) goto break_evloop;
50
51                 if(use_mouse) {
52                         mouse_bmask = read_mouse(&mouse_x, &mouse_y);
53                 }
54
55                 time_msec = get_msec();
56                 demo_draw();
57         }
58
59 break_evloop:
60         set_text_mode();
61         demo_cleanup();
62         kb_shutdown();
63         return 0;
64 }
65
66 void demo_quit(void)
67 {
68         quit = 1;
69 }
70
71 void swap_buffers(void *pixels)
72 {
73         /* TODO implement page flipping */
74         if(pixels) {
75                 /*wait_vsync();*/
76                 memcpy(vmem_front, pixels, fbsize);
77         }
78 }