- VBE banked mode fixes backported from rbench
[dosdemo] / src / dos / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "demo.h"
5 #include "keyb.h"
6 #include "timer.h"
7 #include "gfx.h"
8 #include "logger.h"
9 #include "cdpmi.h"
10 #include "audio.h"
11 #include "mouse.h"
12 #include "sball.h"
13 #include "vmath.h"
14 #include "cpuid.h"
15
16 static int handle_sball_event(sball_event *ev);
17 static void recalc_sball_matrix(float *xform);
18
19 static int quit;
20
21 int force_snd_config;
22
23 static int use_sball;
24 static vec3_t pos = {0, 0, 0};
25 static quat_t rot = {0, 0, 0, 1};
26
27
28 int main(int argc, char **argv)
29 {
30         int i;
31         int vmidx, status = 0;
32
33         for(i=1; i<argc; i++) {
34                 if(strcmp(argv[i], "-setup") == 0) {
35                         force_snd_config = 1;
36                 }
37         }
38
39 #ifdef __DJGPP__
40         __djgpp_nearptr_enable();
41 #endif
42
43         init_logger("demo.log");
44
45 #ifdef __WATCOMC__
46         printf("watcom build\n");
47 #elif defined(__DJGPP__)
48         printf("djgpp build\n");
49 #endif
50
51         if(read_cpuid(&cpuid) == 0) {
52                 print_cpuid(&cpuid);
53         }
54
55         /* au_init needs to be called early, before init_timer, and also before
56          * we enter graphics mode, to use the midas configuration tool if necessary
57          */
58         if(au_init() == -1) {
59                 return 1;
60         }
61
62         init_timer(100);
63         kb_init(32);
64
65         if(init_video() == -1) {
66                 return 1;
67         }
68
69         if((vmidx = match_video_mode(FB_WIDTH, FB_HEIGHT, FB_BPP)) == -1) {
70                 return 1;
71         }
72         if(!(vmem = set_video_mode(vmidx, 1))) {
73                 return 1;
74         }
75
76         if(opt.mouse) {
77                 if((opt.mouse = have_mouse())) {
78                         printf("initializing mouse input\n");
79                         set_mouse_limits(0, 0, FB_WIDTH - 1, FB_HEIGHT - 1);
80                         set_mouse(FB_WIDTH / 2, FB_HEIGHT / 2);
81                 }
82         }
83
84         if(demo_init(argc, argv) == -1) {
85                 status = -1;
86                 goto break_evloop;
87         }
88
89         if(opt.sball && sball_init() == 0) {
90                 use_sball = 1;
91         }
92
93         reset_timer();
94
95         for(;;) {
96                 int key;
97                 while((key = kb_getkey()) != -1) {
98                         demo_keyboard(key, 1);
99                         if(quit) goto break_evloop;
100                 }
101
102                 if(opt.mouse) {
103                         mouse_bmask = read_mouse(&mouse_x, &mouse_y);
104                 }
105                 if(use_sball && sball_pending()) {
106                         sball_event ev;
107                         while(sball_getevent(&ev)) {
108                                 handle_sball_event(&ev);
109                         }
110                         recalc_sball_matrix(sball_matrix);
111                 }
112
113                 time_msec = get_msec();
114                 demo_draw();
115         }
116
117 break_evloop:
118         demo_cleanup();
119         set_text_mode();
120         cleanup_video();
121         kb_shutdown();
122         au_shutdown();
123         if(use_sball) {
124                 sball_shutdown();
125         }
126         return status;
127 }
128
129 void demo_quit(void)
130 {
131         quit = 1;
132 }
133
134 #define TX(ev)  ((ev)->motion.motion[0])
135 #define TY(ev)  ((ev)->motion.motion[1])
136 #define TZ(ev)  ((ev)->motion.motion[2])
137 #define RX(ev)  ((ev)->motion.motion[3])
138 #define RY(ev)  ((ev)->motion.motion[4])
139 #define RZ(ev)  ((ev)->motion.motion[5])
140
141 static int handle_sball_event(sball_event *ev)
142 {
143         switch(ev->type) {
144         case SBALL_EV_MOTION:
145                 if(RX(ev) | RY(ev) | RZ(ev)) {
146                         float rx = (float)RX(ev);
147                         float ry = (float)RY(ev);
148                         float rz = (float)RZ(ev);
149                         float axis_len = sqrt(rx * rx + ry * ry + rz * rz);
150                         if(axis_len > 0.0) {
151                                 rot = quat_rotate(rot, axis_len * 0.001, -rx / axis_len,
152                                                 -ry / axis_len, -rz / axis_len);
153                         }
154                 }
155
156                 pos.x += TX(ev) * 0.001;
157                 pos.y += TY(ev) * 0.001;
158                 pos.z += TZ(ev) * 0.001;
159                 break;
160
161         case SBALL_EV_BUTTON:
162                 if(ev->button.pressed) {
163                         pos = v3_cons(0, 0, 0);
164                         rot = quat_cons(1, 0, 0, 0);
165                 }
166                 break;
167         }
168
169         return 0;
170 }
171
172 void recalc_sball_matrix(float *xform)
173 {
174         quat_to_mat(xform, rot);
175         xform[12] = pos.x;
176         xform[13] = pos.y;
177         xform[14] = pos.z;
178 }