|
1 #include <SDL.h> |
|
2 |
|
3 Uint32 fp10s_acc = 0; |
|
4 Uint32 fp10s_acc_samples = 0; |
|
5 |
|
6 int w = 512; |
|
7 int h = 384; |
|
8 |
|
9 void update(Raytracer &rt, SDL_Surface *screen, Float *render_buffer) |
|
10 { |
|
11 static Uint32 t = 0; |
|
12 Uint32 tnow = SDL_GetTicks(); |
|
13 int fp10s = 10000/(int)(tnow - t); |
|
14 if (t != 0) |
|
15 { |
|
16 fp10s_acc += fp10s; |
|
17 ++fp10s_acc_samples; |
|
18 } |
|
19 t = tnow; |
|
20 printf("\b\b\b\b\b\b\b\b\bfps:%3d.%1d", fp10s/10, fp10s%10); |
|
21 fflush(stdout); |
|
22 |
|
23 rt.render(w, h, render_buffer); |
|
24 |
|
25 if (SDL_MUSTLOCK(screen)) |
|
26 if (SDL_LockSurface(screen) < 0) |
|
27 return; |
|
28 |
|
29 Uint32 *bufp = (Uint32 *)screen->pixels; |
|
30 unsigned char c[3]; |
|
31 for (Float *fd = render_buffer; fd != render_buffer + w*h*3; fd += 3) |
|
32 { |
|
33 for (int i = 0; i < 3; i++) |
|
34 { |
|
35 if (fd[i] > 1.0) |
|
36 c[i] = 255; |
|
37 else |
|
38 c[i] = (unsigned char)(fd[i] * 255.0); |
|
39 } |
|
40 *bufp = SDL_MapRGB(screen->format, c[0], c[1], c[2]); |
|
41 bufp++; |
|
42 } |
|
43 |
|
44 if (SDL_MUSTLOCK(screen)) |
|
45 SDL_UnlockSurface(screen); |
|
46 |
|
47 if (screen->flags & SDL_DOUBLEBUF) |
|
48 SDL_Flip(screen); |
|
49 else |
|
50 SDL_UpdateRect(screen, 0, 0, w, h); |
|
51 } |
|
52 |
|
53 void quit() |
|
54 { |
|
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 { |
|
62 SDL_Surface *screen; |
|
63 Float *render_buffer; |
|
64 |
|
65 pyrit_verbosity = 0; |
|
66 render_buffer = (Float *) malloc(w*h*3*sizeof(Float)); |
|
67 |
|
68 if( SDL_Init(SDL_INIT_VIDEO) < 0 ) { |
|
69 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
|
70 exit(1); |
|
71 } |
|
72 |
|
73 atexit(quit); |
|
74 |
|
75 screen = SDL_SetVideoMode(w, h, 32, SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE); |
|
76 if ( screen == NULL ) { |
|
77 fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError()); |
|
78 exit(1); |
|
79 } |
|
80 |
|
81 SDL_Event event; |
|
82 bool quit = false; |
|
83 Float roty = 0.0, rotx = 0.0, move = 0.0; |
|
84 while (!quit) |
|
85 { |
|
86 while (SDL_PollEvent(&event)) |
|
87 { |
|
88 switch (event.type) { |
|
89 case SDL_VIDEORESIZE: |
|
90 w = event.resize.w; |
|
91 h = event.resize.h; |
|
92 render_buffer = (Float *) realloc(render_buffer, w*h*3*sizeof(Float)); |
|
93 screen = SDL_SetVideoMode(w, h, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE); |
|
94 break; |
|
95 case SDL_KEYDOWN: |
|
96 if (event.key.keysym.sym == SDLK_ESCAPE) { |
|
97 quit = true; |
|
98 break; |
|
99 } |
|
100 if (event.key.keysym.sym == SDLK_LEFT) { |
|
101 roty = -0.01; |
|
102 break; |
|
103 } |
|
104 if (event.key.keysym.sym == SDLK_RIGHT) { |
|
105 roty = +0.01; |
|
106 break; |
|
107 } |
|
108 if (event.key.keysym.sym == SDLK_DOWN) { |
|
109 rotx = +0.01; |
|
110 break; |
|
111 } |
|
112 if (event.key.keysym.sym == SDLK_UP) { |
|
113 rotx = -0.01; |
|
114 break; |
|
115 } |
|
116 if (event.key.keysym.sym == SDLK_w) { |
|
117 move = +0.5; |
|
118 break; |
|
119 } |
|
120 if (event.key.keysym.sym == SDLK_s) { |
|
121 move = -0.5; |
|
122 break; |
|
123 } |
|
124 break; |
|
125 case SDL_KEYUP: |
|
126 if (event.key.keysym.sym == SDLK_LEFT || event.key.keysym.sym == SDLK_RIGHT) { |
|
127 roty = 0.0; |
|
128 break; |
|
129 } |
|
130 if (event.key.keysym.sym == SDLK_UP || event.key.keysym.sym == SDLK_DOWN) { |
|
131 rotx = 0.0; |
|
132 break; |
|
133 } |
|
134 if (event.key.keysym.sym == SDLK_w || event.key.keysym.sym == SDLK_s) { |
|
135 move = 0.0; |
|
136 break; |
|
137 } |
|
138 break; |
|
139 case SDL_QUIT: |
|
140 quit = true; |
|
141 } |
|
142 } |
|
143 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()); |
|
145 cam.u.y = 0; |
|
146 cam.u.normalize(); |
|
147 cam.move(move,0,0); |
|
148 update(rt, screen, render_buffer); |
|
149 } |
|
150 free(render_buffer); |
|
151 } |