optimizations and best color depth attempt
[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         int i, trybpp[] = {32, 24, 16, 0};
24
25         fbgfx_save_video_mode();
26         fbgfx_get_video_mode(&xsz, &ysz, &depth);
27
28         for(i=0; trybpp[i]; i++) {
29                 if(!(vmem = fbgfx_set_video_mode(xsz, ysz, trybpp[i]))) {
30                         continue;
31                 }
32                 fbgfx_get_video_mode(&xsz, &ysz, &depth);
33                 if(depth == trybpp[i]) {
34                         break;
35                 }
36                 fprintf(stderr, "failed to set color depth: %dbpp\n", trybpp[i]);
37         }
38         if(trybpp[i] == 0) {
39                 fprintf(stderr, "no usable color depths found\n");
40                 goto end;
41         }
42
43         if(fbev_init() == -1) {
44                 goto end;
45         }
46         fbev_keyboard(keyboard, 0);
47         fbev_mbutton(mouse, 0);
48         fbev_mmotion(motion, 0);
49
50         if(init_tunnel(xsz, ysz, depth) == -1) {
51                 goto end;
52         }
53
54         start_msec = get_time_msec();
55         for(;;) {
56                 fbev_update();
57                 if(quit) break;
58
59                 time_msec = get_time_msec() - start_msec;
60
61                 draw_tunnel(vmem);
62                 ++num_frames;
63         }
64
65         time_msec = get_time_msec() - start_msec;
66 end:
67         destroy_tunnel();
68         fbev_shutdown();
69         fbgfx_restore_video_mode();
70         if(num_frames && time_msec) {
71                 printf("\ravg framerate: %.1f\n", (float)num_frames / ((float)time_msec / 1000.0));
72         }
73         return 0;
74 }
75
76 static void keyboard(int key, int pressed, void *cls)
77 {
78         if(!pressed) return;
79
80         switch(key) {
81         case 27:
82         case 'q':
83         case 'Q':
84                 quit = 1;
85                 break;
86         }
87 }
88
89 static void mouse(int bn, int pressed, int x, int y, void *cls)
90 {
91 }
92
93 static void motion(int x, int y, void *cls)
94 {
95 }