6d83027d039bdcc22650b88cfab7e999b177db4a
[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 / CHAR_BIT;
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         if(!(fb_pixels = malloc(fbsize))) {
50                 fprintf(stderr, "failed to allocate backbuffer\n");
51                 return 1;
52         }
53
54         if(!(vmem = set_video_mode(fb_width, fb_height, fb_bpp))) {
55                 return 1;
56         }
57
58         if(demo_init(argc, argv) == -1) {
59                 set_text_mode();
60                 return 1;
61         }
62
63         if(opt.sball && sball_init() == 0) {
64                 use_sball = 1;
65         }
66
67         reset_timer();
68
69         while(!quit) {
70 #ifndef NOKEYB
71                 int key;
72                 while((key = kb_getkey()) != -1) {
73                         demo_keyboard(key, 1);
74                 }
75 #else
76                 if(kbhit()) {
77                         demo_keyboard(getch(), 1);
78                 }
79 #endif
80                 if(quit) goto break_evloop;
81
82                 if(use_mouse) {
83                         mouse_bmask = read_mouse(&mouse_x, &mouse_y);
84                 }
85                 if(use_sball && sball_pending()) {
86                         sball_event ev;
87                         while(sball_getevent(&ev)) {
88                                 handle_sball_event(&ev);
89                         }
90                         recalc_sball_matrix(sball_matrix);
91                 }
92
93                 time_msec = get_msec();
94                 demo_draw();
95         }
96
97 break_evloop:
98         set_text_mode();
99         demo_cleanup();
100 #ifndef NOKEYB
101         kb_shutdown();
102 #endif
103         if(use_sball) {
104                 sball_shutdown();
105         }
106         return 0;
107 }
108
109 void demo_quit(void)
110 {
111         quit = 1;
112 }
113
114 void swap_buffers(void *pixels)
115 {
116         if(!pixels) {
117                 pixels = fb_pixels;
118         }
119
120         /* just memcpy to the front buffer */
121         if(opt.vsync) {
122                 wait_vsync();
123         }
124         drawFps(pixels);
125         memcpy(vmem, pixels, fbsize);
126 }
127
128
129 #define TX(ev)  ((ev)->motion.motion[0])
130 #define TY(ev)  ((ev)->motion.motion[1])
131 #define TZ(ev)  ((ev)->motion.motion[2])
132 #define RX(ev)  ((ev)->motion.motion[3])
133 #define RY(ev)  ((ev)->motion.motion[4])
134 #define RZ(ev)  ((ev)->motion.motion[5])
135
136 static int handle_sball_event(sball_event *ev)
137 {
138         switch(ev->type) {
139         case SBALL_EV_MOTION:
140                 if(RX(ev) | RY(ev) | RZ(ev)) {
141                         float rx = (float)RX(ev);
142                         float ry = (float)RY(ev);
143                         float rz = (float)RZ(ev);
144                         float axis_len = sqrt(rx * rx + ry * ry + rz * rz);
145                         if(axis_len > 0.0) {
146                                 rot = quat_rotate(rot, axis_len * 0.001, -rx / axis_len,
147                                                 -ry / axis_len, -rz / axis_len);
148                         }
149                 }
150
151                 pos.x += TX(ev) * 0.001;
152                 pos.y += TY(ev) * 0.001;
153                 pos.z += TZ(ev) * 0.001;
154                 break;
155
156         case SBALL_EV_BUTTON:
157                 if(ev->button.pressed) {
158                         pos = v3_cons(0, 0, 0);
159                         rot = quat_cons(1, 0, 0, 0);
160                 }
161                 break;
162         }
163
164         return 0;
165 }
166
167 void recalc_sball_matrix(float *xform)
168 {
169         quat_to_mat(xform, rot);
170         xform[12] = pos.x;
171         xform[13] = pos.y;
172         xform[14] = pos.z;
173 }