aed12e98feaec4fe8b245b839ad12697828c8abb
[dosdemo] / src / dos / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <string.h>
5 #include <limits.h>
6 #include <assert.h>
7 #include <conio.h>
8 #include "demo.h"
9 #include "keyb.h"
10 #include "mouse.h"
11 #include "timer.h"
12 #include "gfx.h"
13 #include "vmath.h"
14 #include "sball.h"
15 #include "cfgopt.h"
16 #include "logger.h"
17 #include "tinyfps.h"
18
19 #undef NOKEYB
20
21 static int handle_sball_event(sball_event *ev);
22 static void recalc_sball_matrix(float *xform);
23
24 static int quit;
25 static int use_mouse;
26 static long fbsize;
27
28 static int use_sball;
29 static vec3_t pos = {0, 0, 0};
30 static quat_t rot = {0, 0, 0, 1};
31
32 int main(int argc, char **argv)
33 {
34         fbsize = fb_width * fb_height * fb_bpp / 8;
35
36         init_logger("demo.log");
37
38         init_timer(100);
39 #ifndef NOKEYB
40         kb_init(32);
41 #endif
42
43         if((use_mouse = have_mouse())) {
44                 printf("initializing mouse input\n");
45                 set_mouse_limits(0, 0, fb_width, fb_height);
46                 set_mouse(fb_width / 2, fb_height / 2);
47         }
48
49         /* allocate a couple extra rows as a guard band, until we fucking fix the rasterizer */
50         if(!(fb_pixels = malloc(fbsize + (fb_width * fb_bpp / 8) * 2))) {
51                 fprintf(stderr, "failed to allocate backbuffer\n");
52                 return 1;
53         }
54         fb_pixels += fb_width;
55
56         if(!(vmem = set_video_mode(fb_width, fb_height, fb_bpp, 1))) {
57                 return 1;
58         }
59
60         if(demo_init(argc, argv) == -1) {
61                 set_text_mode();
62                 return 1;
63         }
64
65         if(opt.sball && sball_init() == 0) {
66                 use_sball = 1;
67         }
68
69         reset_timer();
70
71         while(!quit) {
72 #ifndef NOKEYB
73                 int key;
74                 while((key = kb_getkey()) != -1) {
75                         demo_keyboard(key, 1);
76                 }
77 #else
78                 if(kbhit()) {
79                         demo_keyboard(getch(), 1);
80                 }
81 #endif
82                 if(quit) goto break_evloop;
83
84                 if(use_mouse) {
85                         mouse_bmask = read_mouse(&mouse_x, &mouse_y);
86                 }
87                 if(use_sball && sball_pending()) {
88                         sball_event ev;
89                         while(sball_getevent(&ev)) {
90                                 handle_sball_event(&ev);
91                         }
92                         recalc_sball_matrix(sball_matrix);
93                 }
94
95                 time_msec = get_msec();
96                 demo_draw();
97         }
98
99 break_evloop:
100         set_text_mode();
101         demo_cleanup();
102 #ifndef NOKEYB
103         kb_shutdown();
104 #endif
105         if(use_sball) {
106                 sball_shutdown();
107         }
108         return 0;
109 }
110
111 void demo_quit(void)
112 {
113         quit = 1;
114 }
115
116 void swap_buffers(void *pixels)
117 {
118         if(!pixels) {
119                 pixels = fb_pixels;
120         }
121
122         /* just memcpy to the front buffer */
123         if(opt.vsync) {
124                 wait_vsync();
125         }
126         drawFps(pixels);
127         memcpy(vmem, pixels, fbsize);
128 }
129
130
131 #define TX(ev)  ((ev)->motion.motion[0])
132 #define TY(ev)  ((ev)->motion.motion[1])
133 #define TZ(ev)  ((ev)->motion.motion[2])
134 #define RX(ev)  ((ev)->motion.motion[3])
135 #define RY(ev)  ((ev)->motion.motion[4])
136 #define RZ(ev)  ((ev)->motion.motion[5])
137
138 static int handle_sball_event(sball_event *ev)
139 {
140         switch(ev->type) {
141         case SBALL_EV_MOTION:
142                 if(RX(ev) | RY(ev) | RZ(ev)) {
143                         float rx = (float)RX(ev);
144                         float ry = (float)RY(ev);
145                         float rz = (float)RZ(ev);
146                         float axis_len = sqrt(rx * rx + ry * ry + rz * rz);
147                         if(axis_len > 0.0) {
148                                 rot = quat_rotate(rot, axis_len * 0.001, -rx / axis_len,
149                                                 -ry / axis_len, -rz / axis_len);
150                         }
151                 }
152
153                 pos.x += TX(ev) * 0.001;
154                 pos.y += TY(ev) * 0.001;
155                 pos.z += TZ(ev) * 0.001;
156                 break;
157
158         case SBALL_EV_BUTTON:
159                 if(ev->button.pressed) {
160                         pos = v3_cons(0, 0, 0);
161                         rot = quat_cons(1, 0, 0, 0);
162                 }
163                 break;
164         }
165
166         return 0;
167 }
168
169 void recalc_sball_matrix(float *xform)
170 {
171         quat_to_mat(xform, rot);
172         xform[12] = pos.x;
173         xform[13] = pos.y;
174         xform[14] = pos.z;
175 }