added two new global pointers in demo.h: vmem_back and vmem_front, and
[dosdemo] / src / demo.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <errno.h>
6 #include <limits.h>
7 #include "demo.h"
8 #include "screen.h"
9 #include "3dgfx.h"
10
11 int fb_width = 320;
12 int fb_height = 240;
13 int fb_bpp = 16;
14 uint16_t *fb_pixels, *vmem_back, *vmem_front;
15 unsigned long time_msec;
16 int mouse_x, mouse_y;
17 unsigned int mouse_bmask;
18
19 static unsigned long nframes;
20 static const char *start_scr_name;
21
22 int demo_init(int argc, char **argv)
23 {
24         struct screen *scr;
25
26         start_scr_name = getenv("START_SCR");
27         if(argv[1]) {
28                 start_scr_name = argv[1];
29         }
30
31         if(g3d_init() == -1) {
32                 return -1;
33         }
34         g3d_framebuffer(fb_width, fb_height, fb_pixels);
35
36         if(scr_init() == -1) {
37                 return -1;
38         }
39         if(start_scr_name) {
40                 scr = scr_lookup(start_scr_name);
41         } else {
42                 scr = scr_screen(0);
43         }
44
45         if(!scr || scr_change(scr, 4000) == -1) {
46                 fprintf(stderr, "screen %s not found\n", start_scr_name ? start_scr_name : "0");
47                 return -1;
48         }
49
50         /* clear the framebuffer at least once */
51         memset(fb_pixels, 0, fb_width * fb_height * fb_bpp / CHAR_BIT);
52         return 0;
53 }
54
55 void demo_cleanup(void)
56 {
57         scr_shutdown();
58         g3d_destroy();
59
60         if(time_msec) {
61                 float fps = (float)nframes / ((float)time_msec / 1000.0f);
62                 printf("average framerate: %.1f\n", fps);
63         }
64 }
65
66 void demo_draw(void)
67 {
68         scr_update();
69         scr_draw();
70
71         ++nframes;
72 }
73
74 void demo_keyboard(int key, int state)
75 {
76         if(state) {
77                 switch(key) {
78                 case 27:
79                         demo_quit();
80                         break;
81
82                 default:
83                         if(key >= '1' && key <= '1' + scr_num_screens()) {
84                                 int idx = key - '1';
85                                 printf("change screen %d\n", idx);
86                                 scr_change(scr_screen(idx), 4000);
87                         }
88                         break;
89                 }
90         }
91 }