diff -r 7079dcc3bd74 -r 929aad02c5f2 ccdemos/common_sdl.h --- a/ccdemos/common_sdl.h Fri Dec 14 16:51:22 2007 +0100 +++ b/ccdemos/common_sdl.h Mon Dec 17 22:03:50 2007 +0100 @@ -17,8 +17,9 @@ ++fp10s_acc_samples; } t = tnow; - printf("\b\b\b\b\b\b\b\b\bfps:%3d.%1d", fp10s/10, fp10s%10); - fflush(stdout); + char s[40]; + sprintf(s, "fps:%3d.%1d", fp10s/10, fp10s%10); + SDL_WM_SetCaption(s, NULL); rt.render(w, h, render_buffer); @@ -50,14 +51,8 @@ SDL_UpdateRect(screen, 0, 0, w, h); } -void quit() -{ - Uint32 fp100s_aver = fp10s_acc*10/fp10s_acc_samples; - printf("\naverlage fps: %3d.%2d\n", fp100s_aver/100, fp100s_aver%100); - SDL_Quit(); -} - -void loop_sdl(Raytracer &rt, Camera &cam) +void loop_sdl(Raytracer &rt, Camera &cam, + void (*update_callback)() = NULL, void (*key_callback)(int, int) = NULL) { SDL_Surface *screen; Float *render_buffer; @@ -69,9 +64,7 @@ fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); exit(1); } - - atexit(quit); - + screen = SDL_SetVideoMode(w, h, 32, SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE); if ( screen == NULL ) { fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError()); @@ -87,8 +80,8 @@ { switch (event.type) { case SDL_VIDEORESIZE: - w = event.resize.w; - h = event.resize.h; + w = (event.resize.w-1) / 8 * 8 + 8; + h = (event.resize.h-1) / 8 * 8 + 8; render_buffer = (Float *) realloc(render_buffer, w*h*3*sizeof(Float)); screen = SDL_SetVideoMode(w, h, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE); break; @@ -121,6 +114,17 @@ move = -0.5; break; } + if (event.key.keysym.sym == SDLK_c) { + // print camera coordinates + cout << "Camera: eye=" << cam.eye + << ", p=" << cam.p + << ", u=" << cam.u + << ", v=" << cam.v + << endl; + break; + } + if (key_callback != NULL) + key_callback(event.key.keysym.sym, 1); break; case SDL_KEYUP: if (event.key.keysym.sym == SDLK_LEFT || event.key.keysym.sym == SDLK_RIGHT) { @@ -132,9 +136,11 @@ break; } if (event.key.keysym.sym == SDLK_w || event.key.keysym.sym == SDLK_s) { - move = 0.0; - break; - } + move = 0.0; + break; + } + if (key_callback != NULL) + key_callback(event.key.keysym.sym, 0); break; case SDL_QUIT: quit = true; @@ -144,8 +150,15 @@ cam.rotate(Quaternion(cos(rotx),cam.u[0]*sin(rotx),0,cam.u[2]*sin(rotx)).normalize()); cam.u.y = 0; cam.u.normalize(); - cam.move(move,0,0); + if (move != 0.0) + cam.move(move,0,0); + if (update_callback != NULL) + update_callback(); update(rt, screen, render_buffer); } free(render_buffer); + + Uint32 fp100s_aver = fp10s_acc*10/fp10s_acc_samples; + printf("averlage fps: %3d.%2d\n\n", fp100s_aver/100, fp100s_aver%100); + SDL_Quit(); }