8881963284eea003037c2ae5769b9ab6d28f5edc
[fbgfx] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "fbgfx.h"
5 #include "fbevents.h"
6 #include "tunnel.h"
7 #include "timer.h"
8
9 unsigned long start_msec, time_msec, num_frames;
10
11 static void keyboard(int key, int pressed, void *cls);
12 static void mouse(int bn, int pressed, int x, int y, void *cls);
13 static void motion(int x, int y, void *cls);
14
15 static void *vmem;
16 static int xsz, ysz, depth;
17
18 static int quit;
19
20
21 int main(void)
22 {
23         fbgfx_save_video_mode();
24         fbgfx_get_video_mode(&xsz, &ysz, &depth);
25
26         if(!(vmem = fbgfx_set_video_mode(xsz, ysz, 16))) {
27                 return 1;
28         }
29         fbgfx_get_video_mode(&xsz, &ysz, &depth);
30         if(depth != 16) {
31                 fprintf(stderr, "failed to set color depth: 16bpp\n");
32                 goto end;
33         }
34         if(fbev_init() == -1) {
35                 goto end;
36         }
37         fbev_keyboard(keyboard, 0);
38         fbev_mbutton(mouse, 0);
39         fbev_mmotion(motion, 0);
40
41         if(init_tunnel(xsz, ysz) == -1) {
42                 goto end;
43         }
44
45         start_msec = get_time_msec();
46         for(;;) {
47                 fbev_update();
48                 if(quit) break;
49
50                 time_msec = get_time_msec() - start_msec;
51
52                 draw_tunnel(vmem);
53                 ++num_frames;
54         }
55
56         time_msec = get_time_msec() - start_msec;
57 end:
58         destroy_tunnel();
59         fbev_shutdown();
60         fbgfx_restore_video_mode();
61         if(num_frames && time_msec) {
62                 printf("\ravg framerate: %.1f\n", (float)num_frames / ((float)time_msec / 1000.0));
63         }
64         return 0;
65 }
66
67 static void keyboard(int key, int pressed, void *cls)
68 {
69         if(!pressed) return;
70
71         switch(key) {
72         case 27:
73         case 'q':
74         case 'Q':
75                 quit = 1;
76                 break;
77         }
78 }
79
80 static void mouse(int bn, int pressed, int x, int y, void *cls)
81 {
82 }
83
84 static void motion(int x, int y, void *cls)
85 {
86 }