dos: stop logger and write fps to the console before exiting
[retrobench] / src / dos / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <conio.h>
4 #include "rbench.h"
5 #include "timer.h"
6 #include "gfx.h"
7 #include "logger.h"
8 #include "cdpmi.h"
9
10 static int parse_args(int argc, char **argv);
11
12 static struct video_mode *vidmode;
13
14 int main(int argc, char **argv)
15 {
16         int vmidx;
17         int num_frames = 0;
18         void *vmem;
19
20         read_config("rbench.cfg");
21
22         if(parse_args(argc, argv) == -1) {
23                 return 1;
24         }
25
26 #ifdef __DJGPP__
27         __djgpp_nearptr_enable();
28 #endif
29
30         init_logger("rbench.log");
31
32         if(init_video() == -1) {
33                 return 1;
34         }
35
36         if((vmidx = match_video_mode(opt.width, opt.height, opt.bpp)) == -1) {
37                 return 1;
38         }
39         if(!(vmem = set_video_mode(vmidx, 1))) {
40                 return 1;
41         }
42         vidmode = get_video_mode(vmidx);
43
44         fb_rmask = vidmode->rmask;
45         fb_gmask = vidmode->gmask;
46         fb_bmask = vidmode->bmask;
47         fb_rshift = vidmode->rshift;
48         fb_gshift = vidmode->gshift;
49         fb_bshift = vidmode->bshift;
50
51         init_timer(100);
52
53         for(;;) {
54                 while(kbhit()) {
55                         int c = getch();
56                         if(c == 27) goto end;
57                         key_event(c, 1);
58                 }
59
60                 time_msec = get_msec();
61                 num_frames++;
62                 redraw();
63
64                 blit_frame(framebuf, 0);
65         }
66
67 end:
68         set_text_mode();
69         cleanup_video();
70         stop_logger();
71
72         if(num_frames) {
73                 printf("%d frames in %d msec\n", num_frames, time_msec);
74                 printf("avg framerate: %.1f fps\n", (10000 * num_frames / time_msec) / 10.0f);
75         }
76         return 0;
77 }
78
79 int resizefb(int x, int y, int bpp, int pitch)
80 {
81         printf("resizefb %dx%d %dbpp (pitch: %d)\n", x, y, bpp, pitch);
82
83         free(framebuf);
84
85         fb_width = x;
86         fb_height = y;
87         fb_bpp = bpp;
88         fb_pitch = pitch;
89
90         if(!(framebuf = malloc(fb_pitch * fb_height))) {
91                 fprintf(stderr, "failed to allocate %dx%d (%dbpp) framebuffer\n",
92                                 fb_width, fb_height, fb_bpp);
93                 return -1;
94         }
95         return 0;
96 }
97
98 static const char *usage_str =
99         "Usage: %s [options]\n"
100         "Options:\n"
101         "  -s <WxH>: resolution\n"
102         "  -b <bpp>: color depth\n"
103         "  -h: print usage and exit\n";
104
105 static int parse_args(int argc, char **argv)
106 {
107         int i;
108
109         for(i=1; i<argc; i++) {
110                 if(argv[i][0] == '-') {
111                         if(argv[i][2]) {
112                                 goto inval_arg;
113                         }
114                         switch(argv[i][1]) {
115                         case 's':
116                                 if(!argv[++i] || sscanf(argv[i], "%dx%d", &opt.width, &opt.height) != 2) {
117                                         fprintf(stderr, "-s must be followed by WxH\n");
118                                         return -1;
119                                 }
120                                 break;
121
122                         case 'b':
123                                 if(!argv[++i] || !(opt.bpp = atoi(argv[i]))) {
124                                         fprintf(stderr, "-b must be followed by the color depth\n");
125                                         return -1;
126                                 }
127                                 break;
128
129                         case 'h':
130                                 printf(usage_str, argv[0]);
131                                 exit(0);
132
133                         default:
134                                 goto inval_arg;
135                         }
136                 } else {
137 inval_arg:      fprintf(stderr, "invalid argument: %s\n", argv[i]);
138                         return -1;
139                 }
140         }
141         return 0;
142 }
143