Added scrolling
[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
10 int fb_width = 320;
11 int fb_height = 240;
12 int fb_bpp = 16;
13 void *fb_pixels;
14 unsigned long time_msec;
15 int mouse_x, mouse_y;
16 unsigned int mouse_bmask;
17
18 static unsigned long nframes;
19 static const char *start_scr_name;
20
21 int demo_init(int argc, char **argv)
22 {
23         struct screen *scr;
24
25         if(argv[1]) {
26                 start_scr_name = argv[1];
27         }
28
29         if(scr_init() == -1) {
30                 return -1;
31         }
32         if(start_scr_name) {
33                 scr = scr_lookup(start_scr_name);
34         } else {
35                 scr = scr_screen(0);
36         }
37
38         if(!scr || scr_change(scr, 4000) == -1) {
39                 fprintf(stderr, "screen %s not found\n", start_scr_name ? start_scr_name : "0");
40                 return -1;
41         }
42
43         /* clear the framebuffer at least once */
44         memset(fb_pixels, 0, fb_width * fb_height * fb_bpp / CHAR_BIT);
45         return 0;
46 }
47
48 void demo_cleanup(void)
49 {
50         scr_shutdown();
51
52         if(time_msec) {
53                 float fps = (float)nframes / ((float)time_msec / 1000.0f);
54                 printf("average framerate: %.1f\n", fps);
55         }
56 }
57
58 void demo_draw(void)
59 {
60         scr_update();
61         scr_draw();
62
63         ++nframes;
64 }
65
66 void demo_keyboard(int key, int state)
67 {
68         if(state) {
69                 switch(key) {
70                 case 27:
71                         demo_quit();
72                         break;
73
74                 default:
75                         if(key >= '1' && key <= '1' + scr_num_screens()) {
76                                 int idx = key - '1';
77                                 printf("change screen %d\n", idx);
78                                 scr_change(scr_screen(idx), 4000);
79                         }
80                         break;
81                 }
82         }
83 }