initial commit
[dos_low3d] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <conio.h>
5 #include <dos.h>
6 #include "video.h"
7 #include "3dgfx.h"
8
9 void update(void);
10 void wait_vsync(void);
11 void handle_key(int key);
12 void interrupt timer_intr();
13
14 static int quit;
15 static unsigned char *fb;
16
17 static volatile unsigned long nticks;
18
19 static void interrupt (*prev_timer_intr)();
20
21 int main(void)
22 {
23         long rate, nframes = 0;
24         long tstart, tdur;
25
26         if(!(fb = malloc(64000))) {
27                 fprintf(stderr, "failed to allocate framebuffer\n");
28                 return 1;
29         }
30
31         init_video();
32
33         prev_timer_intr = _dos_getvect(0x1c);
34         _dos_setvect(0x1c, timer_intr);
35         _disable();
36         tstart = nticks;
37         _enable();
38
39         for(;;) {
40                 while(kbhit()) {
41                         int c = getch();
42                         handle_key(c);
43                         if(quit) goto end;
44                 }
45
46                 update();
47                 nframes++;
48         }
49
50 end:
51         _disable();
52         tdur = nticks - tstart;
53         _enable();
54         _dos_setvect(0x1c, prev_timer_intr);
55
56         close_video();
57         free(fb);
58
59         rate = nframes * 100 * 18 / tdur;
60         printf("%ld frames in %ld sec, rate: %ld.%ld\n", nframes, tdur / 18,
61                         rate / 100, rate % 100);
62         return 0;
63 }
64
65 void update(void)
66 {
67         memset(fb, 0, 64000);
68
69         wait_vsync();
70         memcpy((void*)0xa0000, fb, 64000);
71 }
72
73 void handle_key(int key)
74 {
75         switch(key) {
76         case 27:
77                 quit = 1;
78                 break;
79         }
80 }
81
82 void interrupt timer_intr()
83 {
84         nticks++;
85         _chain_intr(prev_timer_intr);
86 }