rayzor

annotate src/main.cc @ 10:235c8b764c0b

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