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