1 |
#include <stdlib.h> |
2 |
#include <stdio.h> |
3 |
#include <string.h> |
4 |
#include <math.h> |
5 |
|
6 |
#include "SDL.h" |
7 |
|
8 |
#include "SDL_opengl.h" |
9 |
|
10 |
/* Undefine this if you want a flat cube instead of a rainbow cube */ |
11 |
#define SHADED_CUBE |
12 |
|
13 |
/* Define this to be the name of the logo image to use with -logo */ |
14 |
#define LOGO_FILE "icon.bmp" |
15 |
|
16 |
static SDL_Surface *global_image = NULL; |
17 |
static GLuint global_texture = 0; |
18 |
|
19 |
#include "entity.h" |
20 |
|
21 |
#include "txtprt_import.h" |
22 |
|
23 |
/**********************************************************************/ |
24 |
|
25 |
view camera; |
26 |
|
27 |
void |
28 |
HotKey_ToggleFullScreen (void) |
29 |
{ |
30 |
SDL_Surface *screen; |
31 |
|
32 |
screen = SDL_GetVideoSurface (); |
33 |
if (SDL_WM_ToggleFullScreen (screen)) |
34 |
printf ("Toggled fullscreen mode - now %s\n", |
35 |
(screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed"); |
36 |
else |
37 |
printf ("Unable to toggle fullscreen mode\n"); |
38 |
} |
39 |
|
40 |
void |
41 |
HotKey_ToggleGrab (void) |
42 |
{ |
43 |
SDL_GrabMode mode; |
44 |
|
45 |
printf ("Ctrl-G: toggling input grab!\n"); |
46 |
mode = SDL_WM_GrabInput (SDL_GRAB_QUERY); |
47 |
if (mode == SDL_GRAB_ON) |
48 |
printf ("Grab was on\n"); |
49 |
else |
50 |
printf ("Grab was off\n"); |
51 |
|
52 |
mode = SDL_WM_GrabInput (mode ? SDL_GRAB_OFF : SDL_GRAB_ON); |
53 |
if (mode == SDL_GRAB_ON) |
54 |
printf ("Grab is now on\n"); |
55 |
else |
56 |
printf ("Grab is now off\n"); |
57 |
} |
58 |
|
59 |
void |
60 |
HotKey_Iconify (void) |
61 |
{ |
62 |
printf ("Ctrl-Z: iconifying window!\n"); |
63 |
SDL_WM_IconifyWindow (); |
64 |
} |
65 |
|
66 |
int |
67 |
HandleEvent (SDL_Event * event) |
68 |
{ |
69 |
int done; |
70 |
|
71 |
done = 0; |
72 |
switch (event->type) |
73 |
{ |
74 |
case SDL_ACTIVEEVENT: |
75 |
/* See what happened */ |
76 |
printf ("app %s ", event->active.gain ? "gained" : "lost"); |
77 |
if (event->active.state & SDL_APPACTIVE) |
78 |
{ |
79 |
printf ("active "); |
80 |
} |
81 |
else if (event->active.state & SDL_APPMOUSEFOCUS) |
82 |
{ |
83 |
printf ("mouse "); |
84 |
} |
85 |
else if (event->active.state & SDL_APPINPUTFOCUS) |
86 |
{ |
87 |
printf ("input "); |
88 |
} |
89 |
|
90 |
printf ("focus\n"); |
91 |
break; |
92 |
|
93 |
case SDL_KEYDOWN: |
94 |
|
95 |
if (event->key.keysym.sym == SDLK_UP) |
96 |
camera.p.z -= 1; |
97 |
if (event->key.keysym.sym == SDLK_DOWN) |
98 |
camera.p.z += 1; |
99 |
if (event->key.keysym.sym == SDLK_LEFT) |
100 |
camera.p.x -= 1; |
101 |
if (event->key.keysym.sym == SDLK_RIGHT) |
102 |
camera.p.x += 1; |
103 |
|
104 |
if (event->key.keysym.sym == SDLK_ESCAPE) |
105 |
done = 1; |
106 |
|
107 |
if ((event->key.keysym.sym == SDLK_g) && |
108 |
(event->key.keysym.mod & KMOD_CTRL)) |
109 |
HotKey_ToggleGrab (); |
110 |
|
111 |
if ((event->key.keysym.sym == SDLK_z) && |
112 |
(event->key.keysym.mod & KMOD_CTRL)) |
113 |
HotKey_Iconify (); |
114 |
|
115 |
if ((event->key.keysym.sym == SDLK_RETURN) && |
116 |
(event->key.keysym.mod & KMOD_ALT)) |
117 |
HotKey_ToggleFullScreen (); |
118 |
|
119 |
break; |
120 |
|
121 |
case SDL_QUIT: |
122 |
done = 1; |
123 |
break; |
124 |
} |
125 |
|
126 |
return (done); |
127 |
} |
128 |
|
129 |
/* Quick utility function for texture creation */ |
130 |
static int |
131 |
power_of_two (int input) |
132 |
{ |
133 |
int value = 1; |
134 |
|
135 |
while (value < input) |
136 |
{ |
137 |
value <<= 1; |
138 |
} |
139 |
return value; |
140 |
} |
141 |
|
142 |
GLuint |
143 |
SDL_GL_LoadTexture (SDL_Surface * surface, GLfloat * texcoord) |
144 |
{ |
145 |
GLuint texture; |
146 |
int w, h; |
147 |
SDL_Surface *image; |
148 |
SDL_Rect area; |
149 |
Uint32 saved_flags; |
150 |
Uint8 saved_alpha; |
151 |
|
152 |
/* Use the surface width and height expanded to powers of 2 */ |
153 |
w = power_of_two (surface->w); |
154 |
h = power_of_two (surface->h); |
155 |
texcoord[0] = 0.0f; /* Min X */ |
156 |
texcoord[1] = 0.0f; /* Min Y */ |
157 |
texcoord[2] = (GLfloat) surface->w / w; /* Max X */ |
158 |
texcoord[3] = (GLfloat) surface->h / h; /* Max Y */ |
159 |
|
160 |
image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32, |
161 |
#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
162 |
0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 |
163 |
#else |
164 |
0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF |
165 |
#endif |
166 |
); |
167 |
if (image == NULL) |
168 |
{ |
169 |
return 0; |
170 |
} |
171 |
|
172 |
/* Save the alpha blending attributes */ |
173 |
saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); |
174 |
saved_alpha = surface->format->alpha; |
175 |
if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
176 |
{ |
177 |
SDL_SetAlpha (surface, 0, 0); |
178 |
} |
179 |
|
180 |
/* Copy the surface into the GL texture image */ |
181 |
area.x = 0; |
182 |
area.y = 0; |
183 |
area.w = surface->w; |
184 |
area.h = surface->h; |
185 |
SDL_BlitSurface (surface, &area, image, &area); |
186 |
|
187 |
/* Restore the alpha blending attributes */ |
188 |
if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) |
189 |
{ |
190 |
SDL_SetAlpha (surface, saved_flags, saved_alpha); |
191 |
} |
192 |
|
193 |
/* Create an OpenGL texture for the image */ |
194 |
glGenTextures (1, &texture); |
195 |
glBindTexture (GL_TEXTURE_2D, texture); |
196 |
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
197 |
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
198 |
glTexImage2D (GL_TEXTURE_2D, |
199 |
0, |
200 |
GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); |
201 |
SDL_FreeSurface (image); /* No longer needed */ |
202 |
|
203 |
return texture; |
204 |
} |
205 |
|
206 |
int |
207 |
RunGLTest (int argc, char *argv[], |
208 |
int logo, int slowly, int bpp, float gamma, int noframe, int fsaa) |
209 |
{ |
210 |
int i; |
211 |
int rgb_size[3]; |
212 |
int w = 640; |
213 |
int h = 480; |
214 |
int done = 0; |
215 |
int frames; |
216 |
Uint32 start_time, this_time; |
217 |
Uint32 video_flags; |
218 |
int value; |
219 |
|
220 |
if (SDL_Init (SDL_INIT_VIDEO) < 0) |
221 |
{ |
222 |
fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); |
223 |
exit (1); |
224 |
} |
225 |
|
226 |
/* See if we should detect the display depth */ |
227 |
if (bpp == 0) |
228 |
{ |
229 |
if (SDL_GetVideoInfo ()->vfmt->BitsPerPixel <= 8) |
230 |
bpp = 8; |
231 |
else |
232 |
bpp = 16; /* More doesn't seem to work */ |
233 |
} |
234 |
|
235 |
video_flags = SDL_OPENGL; |
236 |
|
237 |
for (i = 1; argv[i]; ++i) |
238 |
if (strcmp (argv[1], "-fullscreen") == 0) |
239 |
video_flags |= SDL_FULLSCREEN; |
240 |
|
241 |
if (noframe) |
242 |
video_flags |= SDL_NOFRAME; |
243 |
|
244 |
/* Initialize the display */ |
245 |
switch (bpp) |
246 |
{ |
247 |
case 8: |
248 |
rgb_size[0] = 3; |
249 |
rgb_size[1] = 3; |
250 |
rgb_size[2] = 2; |
251 |
break; |
252 |
|
253 |
case 15: |
254 |
case 16: |
255 |
rgb_size[0] = 5; |
256 |
rgb_size[1] = 5; |
257 |
rgb_size[2] = 5; |
258 |
break; |
259 |
|
260 |
default: |
261 |
rgb_size[0] = 8; |
262 |
rgb_size[1] = 8; |
263 |
rgb_size[2] = 8; |
264 |
break; |
265 |
} |
266 |
SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgb_size[0]); |
267 |
SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb_size[1]); |
268 |
SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgb_size[2]); |
269 |
SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); |
270 |
SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); |
271 |
|
272 |
if (fsaa) |
273 |
{ |
274 |
SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1); |
275 |
SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, fsaa); |
276 |
} |
277 |
|
278 |
if (SDL_SetVideoMode (w, h, bpp, video_flags) == NULL) |
279 |
{ |
280 |
fprintf (stderr, "Couldn't set GL mode: %s\n", SDL_GetError ()); |
281 |
SDL_Quit (); |
282 |
exit (1); |
283 |
} |
284 |
|
285 |
printf ("Screen BPP: %d\n", SDL_GetVideoSurface ()->format->BitsPerPixel); |
286 |
printf ("\n"); |
287 |
printf ("Vendor : %s\n", glGetString (GL_VENDOR)); |
288 |
printf ("Renderer : %s\n", glGetString (GL_RENDERER)); |
289 |
printf ("Version : %s\n", glGetString (GL_VERSION)); |
290 |
printf ("Extensions : %s\n", glGetString (GL_EXTENSIONS)); |
291 |
printf ("\n"); |
292 |
|
293 |
SDL_GL_GetAttribute (SDL_GL_RED_SIZE, &value); |
294 |
printf ("SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0], value); |
295 |
SDL_GL_GetAttribute (SDL_GL_GREEN_SIZE, &value); |
296 |
printf ("SDL_GL_GREEN_SIZE: requested %d, got %d\n", rgb_size[1], value); |
297 |
SDL_GL_GetAttribute (SDL_GL_BLUE_SIZE, &value); |
298 |
printf ("SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2], value); |
299 |
SDL_GL_GetAttribute (SDL_GL_DEPTH_SIZE, &value); |
300 |
printf ("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value); |
301 |
SDL_GL_GetAttribute (SDL_GL_DOUBLEBUFFER, &value); |
302 |
printf ("SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value); |
303 |
|
304 |
if (fsaa) |
305 |
{ |
306 |
SDL_GL_GetAttribute (SDL_GL_MULTISAMPLEBUFFERS, &value); |
307 |
printf ("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value); |
308 |
SDL_GL_GetAttribute (SDL_GL_MULTISAMPLESAMPLES, &value); |
309 |
printf ("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa, |
310 |
value); |
311 |
} |
312 |
|
313 |
/* Set the window manager title bar */ |
314 |
SDL_WM_SetCaption ("SDL GL test", "testgl"); |
315 |
|
316 |
/* Set the gamma for the window */ |
317 |
if (gamma != 0.0) |
318 |
SDL_SetGamma (gamma, gamma, gamma); |
319 |
|
320 |
camera.p = point (0, 0, 10); |
321 |
camera.d = vec3 (0, 0, -1); |
322 |
camera.u = vec3 (0, 1, 0); |
323 |
camera.w = w; camera.h = h; |
324 |
camera.fov = 90; |
325 |
|
326 |
glMatrixMode (GL_MODELVIEW); |
327 |
glLoadIdentity (); |
328 |
|
329 |
glEnable (GL_DEPTH_TEST); |
330 |
|
331 |
glDepthFunc (GL_LESS); |
332 |
|
333 |
glShadeModel (GL_SMOOTH); |
334 |
|
335 |
glEnable (GL_LIGHTING); |
336 |
|
337 |
GLfloat lightc[4] = { 1, 0.1, 0.1, 1 }; |
338 |
//glLightf (GL_LIGHT0, GL_QUADRATIC_ATTENUATION); |
339 |
glLightfv (GL_LIGHT0, GL_DIFFUSE, lightc); |
340 |
glEnable (GL_LIGHT0); |
341 |
glEnable (GL_COLOR_MATERIAL); |
342 |
|
343 |
/* Loop until done. */ |
344 |
start_time = SDL_GetTicks (); |
345 |
frames = 0; |
346 |
while (!done) |
347 |
{ |
348 |
GLenum gl_error; |
349 |
char *sdl_error; |
350 |
SDL_Event event; |
351 |
|
352 |
/* Do our drawing, too. */ |
353 |
glClearColor (0.0, 0.0, 0.0, 1.0); |
354 |
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
355 |
|
356 |
GLfloat lightp[4]; |
357 |
lightp[0] = camera.p.x; |
358 |
lightp[1] = camera.p.y; |
359 |
lightp[2] = camera.p.z; |
360 |
lightp[3] = 1; |
361 |
glLightfv (GL_LIGHT0, GL_POSITION, lightp); |
362 |
|
363 |
#if 0 |
364 |
static GLfloat ry; |
365 |
ry += 0.03; |
366 |
v.d.x = cos (ry); |
367 |
v.d.z = sin (ry); |
368 |
#endif |
369 |
|
370 |
draw_context c; |
371 |
camera.draw (c); |
372 |
|
373 |
SDL_GL_SwapBuffers (); |
374 |
|
375 |
/* Check for error conditions. */ |
376 |
gl_error = glGetError (); |
377 |
|
378 |
if (gl_error != GL_NO_ERROR) |
379 |
fprintf (stderr, "testgl: OpenGL error: %d\n", gl_error); |
380 |
|
381 |
sdl_error = SDL_GetError (); |
382 |
|
383 |
if (sdl_error[0] != '\0') |
384 |
{ |
385 |
fprintf (stderr, "testgl: SDL error '%s'\n", sdl_error); |
386 |
SDL_ClearError (); |
387 |
} |
388 |
|
389 |
/* Allow the user to see what's happening */ |
390 |
SDL_Delay (20); |
391 |
|
392 |
/* Check if there's a pending event. */ |
393 |
while (SDL_PollEvent (&event)) |
394 |
{ |
395 |
done = HandleEvent (&event); |
396 |
} |
397 |
++frames; |
398 |
} |
399 |
|
400 |
/* Print out the frames per second */ |
401 |
this_time = SDL_GetTicks (); |
402 |
if (this_time != start_time) |
403 |
{ |
404 |
printf ("%2.2f FPS\n", |
405 |
((float) frames / (this_time - start_time)) * 1000.0); |
406 |
} |
407 |
|
408 |
if (global_image) |
409 |
{ |
410 |
SDL_FreeSurface (global_image); |
411 |
global_image = NULL; |
412 |
} |
413 |
if (global_texture) |
414 |
{ |
415 |
glDeleteTextures (1, &global_texture); |
416 |
global_texture = 0; |
417 |
} |
418 |
|
419 |
/* Destroy our GL context, etc. */ |
420 |
SDL_Quit (); |
421 |
return (0); |
422 |
} |
423 |
|
424 |
int |
425 |
main (int argc, char *argv[]) |
426 |
{ |
427 |
int i, logo; |
428 |
int numtests; |
429 |
int bpp = 0; |
430 |
int slowly; |
431 |
float gamma = 0.0; |
432 |
int noframe = 0; |
433 |
int fsaa = 0; |
434 |
|
435 |
// load a entity |
436 |
txtprt_parser p; |
437 |
entity *e; |
438 |
try { |
439 |
e = p.read ("test.blasc"); |
440 |
} catch (txtprt_i_exception & e) { |
441 |
cout << "ERR: " << e.msg << endl; |
442 |
} |
443 |
e->show (); |
444 |
|
445 |
logo = 0; |
446 |
slowly = 0; |
447 |
numtests = 1; |
448 |
for (i = 1; argv[i]; ++i) |
449 |
{ |
450 |
if (strcmp (argv[i], "-twice") == 0) |
451 |
{ |
452 |
++numtests; |
453 |
} |
454 |
if (strcmp (argv[i], "-slow") == 0) |
455 |
{ |
456 |
slowly = 1; |
457 |
} |
458 |
if (strcmp (argv[i], "-bpp") == 0) |
459 |
{ |
460 |
bpp = atoi (argv[++i]); |
461 |
} |
462 |
if (strcmp (argv[i], "-gamma") == 0) |
463 |
{ |
464 |
gamma = (float) atof (argv[++i]); |
465 |
} |
466 |
if (strcmp (argv[i], "-noframe") == 0) |
467 |
{ |
468 |
noframe = 1; |
469 |
} |
470 |
if (strcmp (argv[i], "-fsaa") == 0) |
471 |
{ |
472 |
++fsaa; |
473 |
} |
474 |
if (strncmp (argv[i], "-h", 2) == 0) |
475 |
{ |
476 |
printf |
477 |
("Usage: %s [-twice] [-logo] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa]\n", |
478 |
argv[0]); |
479 |
exit (0); |
480 |
} |
481 |
} |
482 |
for (i = 0; i < numtests; ++i) |
483 |
{ |
484 |
RunGLTest (argc, argv, logo, slowly, bpp, gamma, noframe, fsaa); |
485 |
} |
486 |
return 0; |
487 |
} |