rayzor
view src/main.cc @ 12:d94a69933a71
lots of stuff, can't remember
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 12 Apr 2014 23:28:24 +0300 |
parents | fe94d9e986ae |
children | a9a948809c6f |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <signal.h>
5 #include "inttypes.h"
6 #include "gfx.h"
7 #include "keyb.h"
8 #include "mouse.h"
9 #include "logger.h"
10 #include "scene.h"
11 #include "rayzor.h"
12 #include "screen.h"
13 #include "modeller.h"
14 #include "renderer.h"
15 #include "scrman.h"
16 #include "timer.h"
18 #ifdef __DOS__
19 #undef USE_ASM_SWAPBUF
20 #endif
22 #ifdef USE_ASM_SWAPBUF
23 // defined in swapbuf.asm
24 extern "C" void swap_buffers_asm(void *dest, void *src, int xsz, int ysz, int bpp);
25 #endif
27 static bool init();
28 static void cleanup();
29 static void display();
30 static void swap_buffers();
31 static void draw_cursor(uint32_t *buf, int mx, int my);
32 static void handle_keyboard();
33 static void handle_mouse();
34 static bool parse_args(int argc, char **argv);
35 static void sig(int s);
37 uint32_t *fb_pixels;
38 int fb_width = 640;
39 int fb_height = 480;
40 int fb_bpp = 32;
41 Scene *scene;
43 static bool novideo;
44 static void *fb;
45 static int rbits, gbits, bbits;
46 static int rshift, gshift, bshift;
47 static unsigned int rmask, gmask, bmask;
49 static bool use_asm_swap = true;
50 static bool use_mouse;
51 static int mouse_x, mouse_y;
52 static bool quit;
54 int main(int argc, char **argv)
55 {
56 unsigned long start_msec, msec;
57 unsigned long nframes = 0;
59 if(!parse_args(argc, argv)) {
60 return 1;
61 }
62 if(!init()) {
63 return 1;
64 }
66 start_msec = get_msec();
68 // main loop
69 for(;;) {
70 handle_keyboard();
71 handle_mouse();
72 if(quit) break;
74 display();
75 ++nframes;
77 if(novideo) break;
78 }
80 msec = get_msec() - start_msec;
82 cleanup();
84 printf("Average framerate: %g\n", (float)nframes / ((float)msec / 1000.0f));
85 printf("Thank you for using Rayzor!\n");
86 return 0;
87 }
89 void quit_app()
90 {
91 quit = true;
92 }
94 static bool init()
95 {
96 signal(SIGINT, sig);
97 signal(SIGSEGV, sig);
98 signal(SIGILL, sig);
99 signal(SIGFPE, sig);
101 init_timer(128);
103 if(!novideo) {
104 if(kb_init(32) == -1) {
105 fprintf(stderr, "failed to initialize keyboard driver\n");
106 return false;
107 }
109 if(!(fb = set_video_mode(fb_width, fb_height, fb_bpp))) {
110 set_text_mode();
111 fprintf(stderr, "failed to set video mode: %dx%d %dbpp\n", fb_width, fb_height, fb_bpp);
112 return false;
113 }
114 fb_bpp = get_color_depth();
115 get_color_bits(&rbits, &gbits, &bbits);
116 get_color_shift(&rshift, &gshift, &bshift);
117 get_color_mask(&rmask, &gmask, &bmask);
119 printlog("video resolution: %dx%d\n", fb_width, fb_height);
120 printlog("bpp: %d (%d %d %d)\n", fb_bpp, rbits, gbits, bbits);
121 printlog("shift: %d %d %d\n", rshift, gshift, bshift);
122 printlog("mask: %x %x %x\n", rmask, gmask, bmask);
124 if(have_mouse()) {
125 use_mouse = true;
126 set_mouse_limits(0, 0, fb_width - 1, fb_height - 1);
127 }
128 } else {
129 logger_output(stdout);
130 printlog("novideo (debug) mode\n");
131 fb_bpp = 24;
132 rbits = gbits = bbits = 8;
133 }
135 fb_pixels = new uint32_t[fb_width * fb_height * 4];
136 if(!fb_pixels) {
137 return false;
138 }
140 scene = new Scene;
142 Sphere *sph = new Sphere;
143 scene->add(sph);
145 Box *box = new Box;
146 scene->add(box);
147 box->set_scaling(Vector3(2, 0.25, 2));
149 Modeller *modeller = new Modeller;
150 if(!modeller->init()) {
151 return false;
152 }
153 add_screen(modeller);
155 Renderer *renderer = new Renderer;
156 if(!renderer->init()) {
157 return false;
158 }
159 add_screen(renderer);
161 activate_screen(modeller); // start the modeller screen
162 return true;
163 }
165 static void cleanup()
166 {
167 delete scene;
168 delete [] fb_pixels;
170 destroy_screens();
172 if(!novideo) {
173 set_text_mode();
174 kb_shutdown();
175 }
176 }
178 static void display()
179 {
180 Screen *scr = active_screen();
181 if(scr) {
182 scr->update();
183 scr->draw();
184 }
186 // draw the mouse cursor
187 if(use_mouse) {
188 draw_cursor(fb_pixels, mouse_x, mouse_y);
189 }
191 if(!novideo) {
192 wait_vsync();
193 #ifdef USE_ASM_SWAPBUF
194 swap_buffers_asm(fb, fb_pixels, fb_width, fb_height, fb_bpp);
195 #else
196 swap_buffers();
197 #endif
198 }
199 }
201 #define PACK_RGB(r, g, b) \
202 ((((r) << rshift) & rmask) | \
203 (((g) << gshift) & gmask) | \
204 (((b) << bshift) & bmask))
206 #define UNPACK_RED(c) (((c) >> 16) & 0xff)
207 #define UNPACK_GREEN(c) (((c) >> 8) & 0xff)
208 #define UNPACK_BLUE(c) ((c) & 0xff)
210 static void swap_buffers()
211 {
212 uint32_t *src = fb_pixels;
213 int num_pixels = fb_width * fb_height;
215 switch(fb_bpp) {
216 case 32:
217 memcpy(fb, fb_pixels, num_pixels * 4);
218 break;
220 case 24:
221 {
222 unsigned char *dest = (unsigned char*)fb;
223 for(int i=0; i<num_pixels-1; i++) {
224 *((uint32_t*)dest) = *src++;
225 dest += 3;
226 }
227 *dest++ = UNPACK_RED(*src);
228 *dest++ = UNPACK_GREEN(*src);
229 *dest++ = UNPACK_BLUE(*src);
230 }
231 break;
233 case 16:
234 {
235 uint16_t *dest = (uint16_t*)fb;
236 for(int i=0; i<num_pixels; i++) {
237 uint32_t c = *src++;
238 unsigned char r = UNPACK_RED(c);
239 unsigned char g = UNPACK_GREEN(c);
240 unsigned char b = UNPACK_BLUE(c);
242 *dest++ = (((r) << 8) & 0xf800) |
243 (((g) << 3) & 0x7e0) |
244 (((b) >> 3) & 0x1f);
245 }
246 }
247 break;
249 default:
250 break;
251 }
252 }
254 static void draw_cursor(uint32_t *buf, int mx, int my)
255 {
256 uint32_t *cptr = buf + my * fb_width + mx;
257 int i, cw[2] = {4, 4}, ch[2] = {4, 4};
259 if(mx < cw[0]) cw[0] = mx;
260 if(my < ch[0]) ch[0] = my;
261 if(fb_width - mx < cw[1]) cw[1] = fb_width - mx - 1;
262 if(fb_height - my < ch[1]) ch[1] = fb_height - my - 1;
264 for(i=1; i<cw[0]; i++) {
265 int idx = -i;
266 cptr[idx] = 0xffffff;
267 }
268 for(i=1; i<cw[1]; i++) {
269 int idx = i;
270 cptr[idx] = 0xffffff;
271 }
272 for(i=1; i<ch[0]; i++) {
273 int idx = -i * fb_width;
274 cptr[idx] = 0xffffff;
275 }
276 for(i=1; i<ch[1]; i++) {
277 int idx = i * fb_width;
278 cptr[idx] = 0xffffff;
279 }
280 }
282 static void handle_keyboard()
283 {
284 int key;
285 Screen *scr = active_screen();
287 if(novideo) return;
289 while((key = kb_getkey()) != -1) {
290 switch(key) {
291 case '`':
292 use_asm_swap = !use_asm_swap;
293 break;
295 case 'q':
296 case 'x':
297 if(kb_isdown(KB_ALT)) {
298 quit_app();
299 }
300 break;
302 default:
303 break;
304 }
305 scr->handle_keyboard(key, true); // TODO also generate release events...
306 }
307 }
309 static void handle_mouse()
310 {
311 static int prev_mx, prev_my, prev_bnmask, bndiff;
312 int mx, my, bnmask;
313 Screen *scr = active_screen();
315 if(!use_mouse || novideo) return;
317 bnmask = read_mouse(&mx, &my);
318 if(scr && (bndiff = bnmask ^ prev_bnmask)) {
319 for(int i=0; i<8; i++) {
320 int bit = 1 << i;
321 if(bndiff & bit) {
322 scr->handle_mbutton(i, bnmask & bit, mx, my);
323 }
324 }
325 }
326 prev_bnmask = bnmask;
328 if(scr && (mx != prev_mx || my != prev_my)) {
329 scr->handle_mmotion(mx, my);
330 }
331 prev_mx = mx;
332 prev_my = my;
334 mouse_x = mx;
335 mouse_y = my;
336 }
339 static struct {
340 int opt;
341 const char *lopt;
342 const char *desc;
343 } options[] = {
344 {'s', "size", "resolution <xres>x<yres>[:bpp]"},
345 {'n', "novid", "don't switch video mode (for debugging)"},
346 {'h', "help", "print usage information and exit"},
347 {-1, 0, 0}
348 };
350 static void print_usage(const char *argv0)
351 {
352 printf("%s usage\n", argv0);
353 for(int i=0; options[i].opt != -1; i++) {
354 printf(" -%c, -%s: %s\n", options[i].opt, options[i].lopt, options[i].desc);
355 }
356 exit(0);
357 }
359 static bool parse_args(int argc, char **argv)
360 {
361 for(int i=1; i<argc; i++) {
362 if(argv[i][0] == '-') {
363 int opt = -1;
365 for(int j=0; options[j].opt != -1; j++) {
366 if(argv[i][2] == 0) {
367 if(argv[i][1] == options[j].opt) {
368 opt = options[j].opt;
369 break;
370 }
371 } else {
372 if(strcmp(argv[i] + 1, options[j].lopt) == 0) {
373 opt = options[j].opt;
374 break;
375 }
376 }
377 }
379 switch(opt) {
380 case 's':
381 if(sscanf(argv[++i], "%dx%d:%d", &fb_width, &fb_height, &fb_bpp) < 2) {
382 fprintf(stderr, "%s must be followed by a resolution: WxH\n", argv[i - 1]);
383 return false;
384 }
385 break;
387 case 'n':
388 novideo = true;
389 break;
391 case 'h':
392 print_usage(argv[0]); // doesn't return
393 break;
395 default:
396 fprintf(stderr, "unknown option: %s\n", argv[i]);
397 return false;
398 }
399 } else {
400 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
401 return false;
402 }
403 }
404 return true;
405 }
407 static void sig(int s)
408 {
409 cleanup();
410 fprintf(stderr, "signal caught: %d\n", s);
411 exit(1);
412 }