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