ee28dc9970fa3b749762c5b3956e6082d6e3b5b6
[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 void demo_abort(void)
135 {
136         set_text_mode();
137         stop_logger();
138         printf("demo_abort called. see demo.log for details. Last lines:\n\n");
139         print_tail("demo.log");
140         abort();
141 }
142
143 #define TX(ev)  ((ev)->motion.motion[0])
144 #define TY(ev)  ((ev)->motion.motion[1])
145 #define TZ(ev)  ((ev)->motion.motion[2])
146 #define RX(ev)  ((ev)->motion.motion[3])
147 #define RY(ev)  ((ev)->motion.motion[4])
148 #define RZ(ev)  ((ev)->motion.motion[5])
149
150 static int handle_sball_event(sball_event *ev)
151 {
152         switch(ev->type) {
153         case SBALL_EV_MOTION:
154                 if(RX(ev) | RY(ev) | RZ(ev)) {
155                         float rx = (float)RX(ev);
156                         float ry = (float)RY(ev);
157                         float rz = (float)RZ(ev);
158                         float axis_len = sqrt(rx * rx + ry * ry + rz * rz);
159                         if(axis_len > 0.0) {
160                                 rot = quat_rotate(rot, axis_len * 0.001, -rx / axis_len,
161                                                 -ry / axis_len, -rz / axis_len);
162                         }
163                 }
164
165                 pos.x += TX(ev) * 0.001;
166                 pos.y += TY(ev) * 0.001;
167                 pos.z += TZ(ev) * 0.001;
168                 break;
169
170         case SBALL_EV_BUTTON:
171                 if(ev->button.pressed) {
172                         pos = v3_cons(0, 0, 0);
173                         rot = quat_cons(1, 0, 0, 0);
174                 }
175                 break;
176         }
177
178         return 0;
179 }
180
181 void recalc_sball_matrix(float *xform)
182 {
183         quat_to_mat(xform, rot);
184         xform[12] = pos.x;
185         xform[13] = pos.y;
186         xform[14] = pos.z;
187 }