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