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