removed clang-format and clang_complete files from the repo
[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_pci() != -1) {
66                 /* TODO detect and initialize S3 virge */
67         }
68
69         if(init_video() == -1) {
70                 return 1;
71         }
72
73         if((vmidx = match_video_mode(FB_WIDTH, FB_HEIGHT, FB_BPP)) == -1) {
74                 return 1;
75         }
76         if(!(vmem = set_video_mode(vmidx, 1))) {
77                 return 1;
78         }
79
80         if(opt.mouse) {
81                 if((opt.mouse = have_mouse())) {
82                         printf("initializing mouse input\n");
83                         set_mouse_limits(0, 0, FB_WIDTH - 1, FB_HEIGHT - 1);
84                         set_mouse(FB_WIDTH / 2, FB_HEIGHT / 2);
85                 }
86         }
87
88         if(demo_init(argc, argv) == -1) {
89                 status = -1;
90                 goto break_evloop;
91         }
92
93         if(opt.sball && sball_init() == 0) {
94                 use_sball = 1;
95         }
96
97         reset_timer();
98
99         for(;;) {
100                 int key;
101                 while((key = kb_getkey()) != -1) {
102                         demo_keyboard(key, 1);
103                         if(quit) goto break_evloop;
104                 }
105
106                 if(opt.mouse) {
107                         mouse_bmask = read_mouse(&mouse_x, &mouse_y);
108                 }
109                 if(use_sball && sball_pending()) {
110                         sball_event ev;
111                         while(sball_getevent(&ev)) {
112                                 handle_sball_event(&ev);
113                         }
114                         recalc_sball_matrix(sball_matrix);
115                 }
116
117                 time_msec = get_msec();
118                 demo_draw();
119         }
120
121 break_evloop:
122         demo_cleanup();
123         set_text_mode();
124         cleanup_video();
125         kb_shutdown();
126         au_shutdown();
127         if(use_sball) {
128                 sball_shutdown();
129         }
130         return status;
131 }
132
133 void demo_quit(void)
134 {
135         quit = 1;
136 }
137
138 void demo_abort(void)
139 {
140         set_text_mode();
141         stop_logger();
142         printf("demo_abort called. see demo.log for details. Last lines:\n\n");
143         print_tail("demo.log");
144         abort();
145 }
146
147 #define TX(ev)  ((ev)->motion.motion[0])
148 #define TY(ev)  ((ev)->motion.motion[1])
149 #define TZ(ev)  ((ev)->motion.motion[2])
150 #define RX(ev)  ((ev)->motion.motion[3])
151 #define RY(ev)  ((ev)->motion.motion[4])
152 #define RZ(ev)  ((ev)->motion.motion[5])
153
154 static int handle_sball_event(sball_event *ev)
155 {
156         switch(ev->type) {
157         case SBALL_EV_MOTION:
158                 if(RX(ev) | RY(ev) | RZ(ev)) {
159                         float rx = (float)RX(ev);
160                         float ry = (float)RY(ev);
161                         float rz = (float)RZ(ev);
162                         float axis_len = sqrt(rx * rx + ry * ry + rz * rz);
163                         if(axis_len > 0.0) {
164                                 rot = quat_rotate(rot, axis_len * 0.001, -rx / axis_len,
165                                                 -ry / axis_len, -rz / axis_len);
166                         }
167                 }
168
169                 pos.x += TX(ev) * 0.001;
170                 pos.y += TY(ev) * 0.001;
171                 pos.z += TZ(ev) * 0.001;
172                 break;
173
174         case SBALL_EV_BUTTON:
175                 if(ev->button.pressed) {
176                         pos = v3_cons(0, 0, 0);
177                         rot = quat_cons(1, 0, 0, 0);
178                 }
179                 break;
180         }
181
182         return 0;
183 }
184
185 void recalc_sball_matrix(float *xform)
186 {
187         quat_to_mat(xform, rot);
188         xform[12] = pos.x;
189         xform[13] = pos.y;
190         xform[14] = pos.z;
191 }