rayzor
view src/main.cc @ 17:79609d482762
the renderer renders, also fixed an unnoticed matrix conversion problem between scenegraph and min3d
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 14 Apr 2014 07:34:45 +0300 |
parents | a9a948809c6f |
children | 859ccadca671 |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <signal.h>
5 #include <errno.h>
6 #include <float.h>
7 #include <direct.h>
8 #include "inttypes.h"
9 #include "gfx.h"
10 #include "keyb.h"
11 #include "mouse.h"
12 #include "logger.h"
13 #include "scene.h"
14 #include "rayzor.h"
15 #include "screen.h"
16 #include "modeller.h"
17 #include "renderer.h"
18 #include "scrman.h"
19 #include "timer.h"
21 #ifdef __DOS__
22 #undef USE_ASM_SWAPBUF
23 #endif
25 #ifdef USE_ASM_SWAPBUF
26 // defined in swapbuf.asm
27 extern "C" void swap_buffers_asm(void *dest, void *src, int xsz, int ysz, int bpp);
28 #endif
30 static bool init();
31 static void cleanup();
32 static void display();
33 static void swap_buffers();
34 static void draw_cursor(uint32_t *buf, int mx, int my);
35 static void screenshot();
36 static void handle_keyboard();
37 static void handle_mouse();
38 static bool parse_args(int argc, char **argv);
39 static void sig(int s);
41 uint32_t *fb_pixels;
42 int fb_width = 640;
43 int fb_height = 480;
44 int fb_bpp = 32;
45 Scene *scene;
47 static bool novideo;
48 static void *fb;
49 static int rbits, gbits, bbits;
50 static int rshift, gshift, bshift;
51 static unsigned int rmask, gmask, bmask;
53 static bool use_asm_swap = true;
54 static bool use_mouse;
55 static int mouse_x, mouse_y;
56 static bool quit;
57 static bool cap_shot;
59 int main(int argc, char **argv)
60 {
61 unsigned long start_msec, msec;
62 unsigned long nframes = 0;
64 if(!parse_args(argc, argv)) {
65 return 1;
66 }
67 if(!init()) {
68 return 1;
69 }
71 start_msec = get_msec();
73 // main loop
74 for(;;) {
75 handle_keyboard();
76 handle_mouse();
77 if(quit) break;
79 display();
80 ++nframes;
82 if(novideo) break;
83 }
85 msec = get_msec() - start_msec;
87 cleanup();
89 printf("Average framerate: %g\n", (float)nframes / ((float)msec / 1000.0f));
90 printf("Thank you for using Rayzor!\n");
91 return 0;
92 }
94 void quit_app()
95 {
96 quit = true;
97 }
99 static bool init()
100 {
101 signal(SIGINT, sig);
102 signal(SIGSEGV, sig);
103 signal(SIGILL, sig);
104 signal(SIGFPE, sig);
106 // mask all fpe except invalid op
107 _control87(~_EM_INVALID, _MCW_EM);
109 init_timer(128);
111 if(!novideo) {
112 if(kb_init(32) == -1) {
113 fprintf(stderr, "failed to initialize keyboard driver\n");
114 return false;
115 }
117 if(!(fb = set_video_mode(fb_width, fb_height, fb_bpp))) {
118 set_text_mode();
119 fprintf(stderr, "failed to set video mode: %dx%d %dbpp\n", fb_width, fb_height, fb_bpp);
120 return false;
121 }
122 fb_bpp = get_color_depth();
123 get_color_bits(&rbits, &gbits, &bbits);
124 get_color_shift(&rshift, &gshift, &bshift);
125 get_color_mask(&rmask, &gmask, &bmask);
127 printlog("video resolution: %dx%d\n", fb_width, fb_height);
128 printlog("bpp: %d (%d %d %d)\n", fb_bpp, rbits, gbits, bbits);
129 printlog("shift: %d %d %d\n", rshift, gshift, bshift);
130 printlog("mask: %x %x %x\n", rmask, gmask, bmask);
132 if(have_mouse()) {
133 use_mouse = true;
134 set_mouse_limits(0, 0, fb_width - 1, fb_height - 1);
135 }
136 } else {
137 logger_output(stdout);
138 printlog("novideo (debug) mode\n");
139 fb_bpp = 24;
140 rbits = gbits = bbits = 8;
141 }
143 fb_pixels = new uint32_t[fb_width * fb_height * 4];
144 if(!fb_pixels) {
145 return false;
146 }
148 scene = new Scene;
150 Sphere *sph = new Sphere;
151 sph->mtl.diffuse = Vector3(1.0, 0.3, 0.1);
152 sph->mtl.roughness = 0.4;
153 scene->add(sph);
155 Box *box = new Box;
156 box->mtl.diffuse = Vector3(0.1, 0.4, 1.0);
157 box->mtl.roughness = 0.9;
158 box->set_position(Vector3(0, -1.1, 0));
159 box->set_scaling(Vector3(4, 0.1, 4));
160 scene->add(box);
162 Light *lt = new Light;
163 lt->set_intensity(0.8);
164 lt->set_position(Vector3(-10, 10, 10));
165 scene->add(lt);
167 Modeller *modeller = new Modeller;
168 if(!modeller->init()) {
169 return false;
170 }
171 add_screen(modeller);
173 Renderer *renderer = new Renderer;
174 if(!renderer->init()) {
175 return false;
176 }
177 add_screen(renderer);
179 activate_screen(modeller); // start the modeller screen
180 return true;
181 }
183 static void cleanup()
184 {
185 delete scene;
186 delete [] fb_pixels;
188 destroy_screens();
190 if(!novideo) {
191 set_text_mode();
192 kb_shutdown();
193 }
194 }
196 static void display()
197 {
198 Screen *scr = active_screen();
199 if(scr) {
200 scr->update();
201 scr->draw();
202 }
204 // draw the mouse cursor
205 if(use_mouse) {
206 draw_cursor(fb_pixels, mouse_x, mouse_y);
207 }
209 if(cap_shot) {
210 screenshot();
211 cap_shot = false;
212 }
214 if(!novideo) {
215 wait_vsync();
216 #ifdef USE_ASM_SWAPBUF
217 swap_buffers_asm(fb, fb_pixels, fb_width, fb_height, fb_bpp);
218 #else
219 swap_buffers();
220 #endif
221 }
222 }
224 #define PACK_RGB(r, g, b) \
225 ((((r) << rshift) & rmask) | \
226 (((g) << gshift) & gmask) | \
227 (((b) << bshift) & bmask))
229 #define UNPACK_RED(c) (((c) >> 16) & 0xff)
230 #define UNPACK_GREEN(c) (((c) >> 8) & 0xff)
231 #define UNPACK_BLUE(c) ((c) & 0xff)
233 static void swap_buffers()
234 {
235 uint32_t *src = fb_pixels;
236 int num_pixels = fb_width * fb_height;
238 switch(fb_bpp) {
239 case 32:
240 memcpy(fb, fb_pixels, num_pixels * 4);
241 break;
243 case 24:
244 {
245 unsigned char *dest = (unsigned char*)fb;
246 for(int i=0; i<num_pixels-1; i++) {
247 *((uint32_t*)dest) = *src++;
248 dest += 3;
249 }
250 *dest++ = UNPACK_RED(*src);
251 *dest++ = UNPACK_GREEN(*src);
252 *dest++ = UNPACK_BLUE(*src);
253 }
254 break;
256 case 16:
257 {
258 uint16_t *dest = (uint16_t*)fb;
259 for(int i=0; i<num_pixels; i++) {
260 uint32_t c = *src++;
261 unsigned char r = UNPACK_RED(c);
262 unsigned char g = UNPACK_GREEN(c);
263 unsigned char b = UNPACK_BLUE(c);
265 *dest++ = (((r) << 8) & 0xf800) |
266 (((g) << 3) & 0x7e0) |
267 (((b) >> 3) & 0x1f);
268 }
269 }
270 break;
272 default:
273 break;
274 }
275 }
277 static void draw_cursor(uint32_t *buf, int mx, int my)
278 {
279 uint32_t *cptr = buf + my * fb_width + mx;
280 int i, cw[2] = {4, 4}, ch[2] = {4, 4};
282 if(mx < cw[0]) cw[0] = mx;
283 if(my < ch[0]) ch[0] = my;
284 if(fb_width - mx < cw[1]) cw[1] = fb_width - mx - 1;
285 if(fb_height - my < ch[1]) ch[1] = fb_height - my - 1;
287 for(i=1; i<cw[0]; i++) {
288 int idx = -i;
289 cptr[idx] = 0xffffff;
290 }
291 for(i=1; i<cw[1]; i++) {
292 int idx = i;
293 cptr[idx] = 0xffffff;
294 }
295 for(i=1; i<ch[0]; i++) {
296 int idx = -i * fb_width;
297 cptr[idx] = 0xffffff;
298 }
299 for(i=1; i<ch[1]; i++) {
300 int idx = i * fb_width;
301 cptr[idx] = 0xffffff;
302 }
303 }
305 #define PPM_COMMENT "screenshot saved by the rayzor modeller/renderer"
306 static void screenshot()
307 {
308 static int shotidx = -1;
309 FILE *fp;
310 char fname[PATH_MAX];
312 if(shotidx == -1) {
313 DIR *dir;
314 struct dirent *dent;
316 shotidx = 0;
317 if((dir = opendir("."))) {
318 while((dent = readdir(dir))) {
319 int i, num;
320 for(i=0; dent->d_name[i]; i++) {
321 fname[i] = tolower(dent->d_name[i]);
322 }
323 fname[i] = 0;
325 if(sscanf(fname, "shot%d.ppm", &num) == 1 && num > shotidx) {
326 shotidx = num;
327 }
328 }
329 closedir(dir);
330 }
331 }
333 sprintf(fname, "shot%04d.ppm", ++shotidx);
334 if(!(fp = fopen(fname, "wb"))) {
335 printlog("failed to save screenshot %s: %s\n", fname, strerror(errno));
336 return;
337 }
339 fprintf(fp, "P6\n# " PPM_COMMENT "\n%d %d\n255\n", fb_width, fb_height);
340 for(int i=0; i<fb_width * fb_height; i++) {
341 uint32_t c = fb_pixels[i];
342 fputc(UNPACK_RED(c), fp);
343 fputc(UNPACK_GREEN(c), fp);
344 fputc(UNPACK_BLUE(c), fp);
345 }
347 fclose(fp);
348 }
350 static void handle_keyboard()
351 {
352 int key;
353 Screen *scr = active_screen();
355 if(novideo) return;
357 while((key = kb_getkey()) != -1) {
358 switch(key) {
359 case '`':
360 use_asm_swap = !use_asm_swap;
361 break;
363 case 'q':
364 case 'x':
365 if(kb_isdown(KB_ALT)) {
366 quit_app();
367 }
368 break;
370 case KB_F12:
371 cap_shot = true;
372 break;
374 default:
375 break;
376 }
377 scr->handle_keyboard(key, true); // TODO also generate release events...
378 }
379 }
381 static void handle_mouse()
382 {
383 static int prev_mx, prev_my, prev_bnmask, bndiff;
384 int mx, my, bnmask;
385 Screen *scr = active_screen();
387 if(!use_mouse || novideo) return;
389 bnmask = read_mouse(&mx, &my);
390 if(scr && (bndiff = bnmask ^ prev_bnmask)) {
391 for(int i=0; i<8; i++) {
392 int bit = 1 << i;
393 if(bndiff & bit) {
394 scr->handle_mbutton(i, bnmask & bit, mx, my);
395 }
396 }
397 }
398 prev_bnmask = bnmask;
400 if(scr && (mx != prev_mx || my != prev_my)) {
401 scr->handle_mmotion(mx, my);
402 }
403 prev_mx = mx;
404 prev_my = my;
406 mouse_x = mx;
407 mouse_y = my;
408 }
411 static struct {
412 int opt;
413 const char *lopt;
414 const char *desc;
415 } options[] = {
416 {'s', "size", "resolution <xres>x<yres>[:bpp]"},
417 {'n', "novid", "don't switch video mode (for debugging)"},
418 {'h', "help", "print usage information and exit"},
419 {-1, 0, 0}
420 };
422 static void print_usage(const char *argv0)
423 {
424 printf("%s usage\n", argv0);
425 for(int i=0; options[i].opt != -1; i++) {
426 printf(" -%c, -%s: %s\n", options[i].opt, options[i].lopt, options[i].desc);
427 }
428 exit(0);
429 }
431 static bool parse_args(int argc, char **argv)
432 {
433 for(int i=1; i<argc; i++) {
434 if(argv[i][0] == '-') {
435 int opt = -1;
437 for(int j=0; options[j].opt != -1; j++) {
438 if(argv[i][2] == 0) {
439 if(argv[i][1] == options[j].opt) {
440 opt = options[j].opt;
441 break;
442 }
443 } else {
444 if(strcmp(argv[i] + 1, options[j].lopt) == 0) {
445 opt = options[j].opt;
446 break;
447 }
448 }
449 }
451 switch(opt) {
452 case 's':
453 if(sscanf(argv[++i], "%dx%d:%d", &fb_width, &fb_height, &fb_bpp) < 2) {
454 fprintf(stderr, "%s must be followed by a resolution: WxH\n", argv[i - 1]);
455 return false;
456 }
457 break;
459 case 'n':
460 novideo = true;
461 break;
463 case 'h':
464 print_usage(argv[0]); // doesn't return
465 break;
467 default:
468 fprintf(stderr, "unknown option: %s\n", argv[i]);
469 return false;
470 }
471 } else {
472 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
473 return false;
474 }
475 }
476 return true;
477 }
479 static void sig(int s)
480 {
481 cleanup();
482 fprintf(stderr, "signal caught: %d\n", s);
484 if(s == SIGFPE) {
485 unsigned int st = _status87();
486 fprintf(stderr, "fpu status: %x\n", st);
487 }
489 exit(1);
490 }