equal
deleted
inserted
replaced
15 { |
15 { |
16 fp10s_acc += fp10s; |
16 fp10s_acc += fp10s; |
17 ++fp10s_acc_samples; |
17 ++fp10s_acc_samples; |
18 } |
18 } |
19 t = tnow; |
19 t = tnow; |
20 printf("\b\b\b\b\b\b\b\b\bfps:%3d.%1d", fp10s/10, fp10s%10); |
20 char s[40]; |
21 fflush(stdout); |
21 sprintf(s, "fps:%3d.%1d", fp10s/10, fp10s%10); |
|
22 SDL_WM_SetCaption(s, NULL); |
22 |
23 |
23 rt.render(w, h, render_buffer); |
24 rt.render(w, h, render_buffer); |
24 |
25 |
25 if (SDL_MUSTLOCK(screen)) |
26 if (SDL_MUSTLOCK(screen)) |
26 if (SDL_LockSurface(screen) < 0) |
27 if (SDL_LockSurface(screen) < 0) |
48 SDL_Flip(screen); |
49 SDL_Flip(screen); |
49 else |
50 else |
50 SDL_UpdateRect(screen, 0, 0, w, h); |
51 SDL_UpdateRect(screen, 0, 0, w, h); |
51 } |
52 } |
52 |
53 |
53 void quit() |
54 void loop_sdl(Raytracer &rt, Camera &cam, |
54 { |
55 void (*update_callback)() = NULL, void (*key_callback)(int, int) = NULL) |
55 Uint32 fp100s_aver = fp10s_acc*10/fp10s_acc_samples; |
|
56 printf("\naverlage fps: %3d.%2d\n", fp100s_aver/100, fp100s_aver%100); |
|
57 SDL_Quit(); |
|
58 } |
|
59 |
|
60 void loop_sdl(Raytracer &rt, Camera &cam) |
|
61 { |
56 { |
62 SDL_Surface *screen; |
57 SDL_Surface *screen; |
63 Float *render_buffer; |
58 Float *render_buffer; |
64 |
59 |
65 pyrit_verbosity = 0; |
60 pyrit_verbosity = 0; |
67 |
62 |
68 if( SDL_Init(SDL_INIT_VIDEO) < 0 ) { |
63 if( SDL_Init(SDL_INIT_VIDEO) < 0 ) { |
69 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
64 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
70 exit(1); |
65 exit(1); |
71 } |
66 } |
72 |
67 |
73 atexit(quit); |
|
74 |
|
75 screen = SDL_SetVideoMode(w, h, 32, SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE); |
68 screen = SDL_SetVideoMode(w, h, 32, SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE); |
76 if ( screen == NULL ) { |
69 if ( screen == NULL ) { |
77 fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError()); |
70 fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError()); |
78 exit(1); |
71 exit(1); |
79 } |
72 } |
85 { |
78 { |
86 while (SDL_PollEvent(&event)) |
79 while (SDL_PollEvent(&event)) |
87 { |
80 { |
88 switch (event.type) { |
81 switch (event.type) { |
89 case SDL_VIDEORESIZE: |
82 case SDL_VIDEORESIZE: |
90 w = event.resize.w; |
83 w = (event.resize.w-1) / 8 * 8 + 8; |
91 h = event.resize.h; |
84 h = (event.resize.h-1) / 8 * 8 + 8; |
92 render_buffer = (Float *) realloc(render_buffer, w*h*3*sizeof(Float)); |
85 render_buffer = (Float *) realloc(render_buffer, w*h*3*sizeof(Float)); |
93 screen = SDL_SetVideoMode(w, h, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE); |
86 screen = SDL_SetVideoMode(w, h, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE); |
94 break; |
87 break; |
95 case SDL_KEYDOWN: |
88 case SDL_KEYDOWN: |
96 if (event.key.keysym.sym == SDLK_ESCAPE) { |
89 if (event.key.keysym.sym == SDLK_ESCAPE) { |
119 } |
112 } |
120 if (event.key.keysym.sym == SDLK_s) { |
113 if (event.key.keysym.sym == SDLK_s) { |
121 move = -0.5; |
114 move = -0.5; |
122 break; |
115 break; |
123 } |
116 } |
|
117 if (event.key.keysym.sym == SDLK_c) { |
|
118 // print camera coordinates |
|
119 cout << "Camera: eye=" << cam.eye |
|
120 << ", p=" << cam.p |
|
121 << ", u=" << cam.u |
|
122 << ", v=" << cam.v |
|
123 << endl; |
|
124 break; |
|
125 } |
|
126 if (key_callback != NULL) |
|
127 key_callback(event.key.keysym.sym, 1); |
124 break; |
128 break; |
125 case SDL_KEYUP: |
129 case SDL_KEYUP: |
126 if (event.key.keysym.sym == SDLK_LEFT || event.key.keysym.sym == SDLK_RIGHT) { |
130 if (event.key.keysym.sym == SDLK_LEFT || event.key.keysym.sym == SDLK_RIGHT) { |
127 roty = 0.0; |
131 roty = 0.0; |
128 break; |
132 break; |
130 if (event.key.keysym.sym == SDLK_UP || event.key.keysym.sym == SDLK_DOWN) { |
134 if (event.key.keysym.sym == SDLK_UP || event.key.keysym.sym == SDLK_DOWN) { |
131 rotx = 0.0; |
135 rotx = 0.0; |
132 break; |
136 break; |
133 } |
137 } |
134 if (event.key.keysym.sym == SDLK_w || event.key.keysym.sym == SDLK_s) { |
138 if (event.key.keysym.sym == SDLK_w || event.key.keysym.sym == SDLK_s) { |
135 move = 0.0; |
139 move = 0.0; |
136 break; |
140 break; |
137 } |
141 } |
|
142 if (key_callback != NULL) |
|
143 key_callback(event.key.keysym.sym, 0); |
138 break; |
144 break; |
139 case SDL_QUIT: |
145 case SDL_QUIT: |
140 quit = true; |
146 quit = true; |
141 } |
147 } |
142 } |
148 } |
143 cam.rotate(Quaternion(cos(roty),0,sin(roty),0).normalize()); |
149 cam.rotate(Quaternion(cos(roty),0,sin(roty),0).normalize()); |
144 cam.rotate(Quaternion(cos(rotx),cam.u[0]*sin(rotx),0,cam.u[2]*sin(rotx)).normalize()); |
150 cam.rotate(Quaternion(cos(rotx),cam.u[0]*sin(rotx),0,cam.u[2]*sin(rotx)).normalize()); |
145 cam.u.y = 0; |
151 cam.u.y = 0; |
146 cam.u.normalize(); |
152 cam.u.normalize(); |
147 cam.move(move,0,0); |
153 if (move != 0.0) |
|
154 cam.move(move,0,0); |
|
155 if (update_callback != NULL) |
|
156 update_callback(); |
148 update(rt, screen, render_buffer); |
157 update(rt, screen, render_buffer); |
149 } |
158 } |
150 free(render_buffer); |
159 free(render_buffer); |
|
160 |
|
161 Uint32 fp100s_aver = fp10s_acc*10/fp10s_acc_samples; |
|
162 printf("averlage fps: %3d.%2d\n\n", fp100s_aver/100, fp100s_aver%100); |
|
163 SDL_Quit(); |
151 } |
164 } |