more vbe
[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_back = set_video_mode(fb_width, fb_height, fb_bpp))) {
55                 return 1;
56         }
57         if(!(vmem_front = page_flip(FLIP_NOW))) {
58                 fprintf(stderr, "page flipping not supported. falling back to double buffering\n");
59                 vmem_front = vmem_back;
60         } else {
61                 assert(vmem_back != vmem_front);
62         }
63
64         if(demo_init(argc, argv) == -1) {
65                 set_text_mode();
66                 return 1;
67         }
68
69         if(opt.sball && sball_init() == 0) {
70                 use_sball = 1;
71         }
72
73         reset_timer();
74
75         while(!quit) {
76 #ifndef NOKEYB
77                 int key;
78                 while((key = kb_getkey()) != -1) {
79                         demo_keyboard(key, 1);
80                 }
81 #else
82                 if(kbhit()) {
83                         demo_keyboard(getch(), 1);
84                 }
85 #endif
86                 if(quit) goto break_evloop;
87
88                 if(use_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                 demo_draw();
101         }
102
103 break_evloop:
104         set_text_mode();
105         demo_cleanup();
106 #ifndef NOKEYB
107         kb_shutdown();
108 #endif
109         if(use_sball) {
110                 sball_shutdown();
111         }
112         return 0;
113 }
114
115 void demo_quit(void)
116 {
117         quit = 1;
118 }
119
120 void swap_buffers(void *pixels)
121 {
122         if(pixels) {
123                 /* just memcpy to the front buffer */
124                 if(opt.vsync) {
125                         wait_vsync();
126                 }
127                 drawFps(pixels);
128                 memcpy(vmem_front, pixels, fbsize);
129
130         } else {
131                 /* attempt page flipping */
132                 void *next;
133
134                 drawFps(vmem_back);
135                 if((next = page_flip(opt.vsync ? FLIP_VBLANK_WAIT : FLIP_NOW))) {
136                         assert(next == vmem_back);
137                         vmem_back = vmem_front;
138                         vmem_front = next;
139                 } else {
140                         /* failed to page flip, assume we drew in the front buffer then
141                          * and just wait for vsync if necessary
142                          */
143                         if(opt.vsync) {
144                                 wait_vsync();
145                         }
146                 }
147         }
148 }
149
150
151 #define TX(ev)  ((ev)->motion.motion[0])
152 #define TY(ev)  ((ev)->motion.motion[1])
153 #define TZ(ev)  ((ev)->motion.motion[2])
154 #define RX(ev)  ((ev)->motion.motion[3])
155 #define RY(ev)  ((ev)->motion.motion[4])
156 #define RZ(ev)  ((ev)->motion.motion[5])
157
158 static int handle_sball_event(sball_event *ev)
159 {
160         switch(ev->type) {
161         case SBALL_EV_MOTION:
162                 if(RX(ev) | RY(ev) | RZ(ev)) {
163                         float rx = (float)RX(ev);
164                         float ry = (float)RY(ev);
165                         float rz = (float)RZ(ev);
166                         float axis_len = sqrt(rx * rx + ry * ry + rz * rz);
167                         if(axis_len > 0.0) {
168                                 rot = quat_rotate(rot, axis_len * 0.001, -rx / axis_len,
169                                                 -ry / axis_len, -rz / axis_len);
170                         }
171                 }
172
173                 pos.x += TX(ev) * 0.001;
174                 pos.y += TY(ev) * 0.001;
175                 pos.z += TZ(ev) * 0.001;
176                 break;
177
178         case SBALL_EV_BUTTON:
179                 if(ev->button.pressed) {
180                         pos = v3_cons(0, 0, 0);
181                         rot = quat_cons(1, 0, 0, 0);
182                 }
183                 break;
184         }
185
186         return 0;
187 }
188
189 void recalc_sball_matrix(float *xform)
190 {
191         quat_to_mat(xform, rot);
192         xform[12] = pos.x;
193         xform[13] = pos.y;
194         xform[14] = pos.z;
195 }