| 1 |
/* |
| 2 |
Copyright (C) 1997-2026 Sam Lantinga <[email protected]> |
| 3 |
|
| 4 |
This software is provided 'as-is', without any express or implied |
| 5 |
warranty. In no event will the authors be held liable for any damages |
| 6 |
arising from the use of this software. |
| 7 |
|
| 8 |
Permission is granted to anyone to use this software for any purpose, |
| 9 |
including commercial applications, and to alter it and redistribute it |
| 10 |
freely. |
| 11 |
*/ |
| 12 |
#include <stdlib.h> |
| 13 |
#include <stdio.h> |
| 14 |
#include <string.h> |
| 15 |
#include <math.h> |
| 16 |
|
| 17 |
#include "SDL_test_common.h" |
| 18 |
|
| 19 |
#ifdef __MACOS__ |
| 20 |
#define HAVE_OPENGL |
| 21 |
#endif |
| 22 |
|
| 23 |
#ifdef HAVE_OPENGL |
| 24 |
|
| 25 |
#include "SDL_opengl.h" |
| 26 |
|
| 27 |
typedef struct GL_Context |
| 28 |
{ |
| 29 |
#define SDL_PROC(ret, func, params) ret (APIENTRY *func) params; |
| 30 |
#if 0 |
| 31 |
#include "../src/render/opengl/SDL_glfuncs.h" |
| 32 |
#else |
| 33 |
#include "glfuncs.h" |
| 34 |
#endif |
| 35 |
#undef SDL_PROC |
| 36 |
} GL_Context; |
| 37 |
|
| 38 |
|
| 39 |
/* Undefine this if you want a flat cube instead of a rainbow cube */ |
| 40 |
#define SHADED_CUBE |
| 41 |
|
| 42 |
static SDLTest_CommonState *state; |
| 43 |
static SDL_GLContext context; |
| 44 |
static GL_Context ctx; |
| 45 |
|
| 46 |
static int LoadContext(GL_Context *data) |
| 47 |
{ |
| 48 |
#ifdef SDL_VIDEO_DRIVER_UIKIT |
| 49 |
#define __SDL_NOGETPROCADDR__ |
| 50 |
#elif defined(SDL_VIDEO_DRIVER_ANDROID) |
| 51 |
#define __SDL_NOGETPROCADDR__ |
| 52 |
#elif defined(SDL_VIDEO_DRIVER_PANDORA) |
| 53 |
#define __SDL_NOGETPROCADDR__ |
| 54 |
#endif |
| 55 |
|
| 56 |
#if defined __SDL_NOGETPROCADDR__ |
| 57 |
#define SDL_PROC(ret, func, params) data->func = func; |
| 58 |
#else |
| 59 |
#define SDL_PROC(ret, func, params) \ |
| 60 |
do { \ |
| 61 |
data->func = SDL_GL_GetProcAddress(#func); \ |
| 62 |
if (!data->func) { \ |
| 63 |
return SDL_SetError("Couldn't load GL function %s: %s", #func, SDL_GetError()); \ |
| 64 |
} \ |
| 65 |
} while (0); |
| 66 |
#endif /* __SDL_NOGETPROCADDR__ */ |
| 67 |
|
| 68 |
#if 0 |
| 69 |
#include "../src/render/opengl/SDL_glfuncs.h" |
| 70 |
#else |
| 71 |
#include "glfuncs.h" |
| 72 |
#endif |
| 73 |
#undef SDL_PROC |
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ |
| 78 |
static void |
| 79 |
quit(int rc) |
| 80 |
{ |
| 81 |
if (context) { |
| 82 |
/* SDL_GL_MakeCurrent(0, NULL); */ /* doesn't do anything */ |
| 83 |
SDL_GL_DeleteContext(context); |
| 84 |
} |
| 85 |
SDLTest_CommonQuit(state); |
| 86 |
exit(rc); |
| 87 |
} |
| 88 |
|
| 89 |
static void |
| 90 |
Render(void) |
| 91 |
{ |
| 92 |
static float color[8][3] = { |
| 93 |
{ 1.0, 1.0, 0.0 }, |
| 94 |
{ 1.0, 0.0, 0.0 }, |
| 95 |
{ 0.0, 0.0, 0.0 }, |
| 96 |
{ 0.0, 1.0, 0.0 }, |
| 97 |
{ 0.0, 1.0, 1.0 }, |
| 98 |
{ 1.0, 1.0, 1.0 }, |
| 99 |
{ 1.0, 0.0, 1.0 }, |
| 100 |
{ 0.0, 0.0, 1.0 } |
| 101 |
}; |
| 102 |
static float cube[8][3] = { |
| 103 |
{ 0.5, 0.5, -0.5 }, |
| 104 |
{ 0.5, -0.5, -0.5 }, |
| 105 |
{ -0.5, -0.5, -0.5 }, |
| 106 |
{ -0.5, 0.5, -0.5 }, |
| 107 |
{ -0.5, 0.5, 0.5 }, |
| 108 |
{ 0.5, 0.5, 0.5 }, |
| 109 |
{ 0.5, -0.5, 0.5 }, |
| 110 |
{ -0.5, -0.5, 0.5 } |
| 111 |
}; |
| 112 |
|
| 113 |
/* Do our drawing, too. */ |
| 114 |
ctx.glClearColor(0.0, 0.0, 0.0, 1.0); |
| 115 |
ctx.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 116 |
|
| 117 |
ctx.glBegin(GL_QUADS); |
| 118 |
|
| 119 |
#ifdef SHADED_CUBE |
| 120 |
ctx.glColor3fv(color[0]); |
| 121 |
ctx.glVertex3fv(cube[0]); |
| 122 |
ctx.glColor3fv(color[1]); |
| 123 |
ctx.glVertex3fv(cube[1]); |
| 124 |
ctx.glColor3fv(color[2]); |
| 125 |
ctx.glVertex3fv(cube[2]); |
| 126 |
ctx.glColor3fv(color[3]); |
| 127 |
ctx.glVertex3fv(cube[3]); |
| 128 |
|
| 129 |
ctx.glColor3fv(color[3]); |
| 130 |
ctx.glVertex3fv(cube[3]); |
| 131 |
ctx.glColor3fv(color[4]); |
| 132 |
ctx.glVertex3fv(cube[4]); |
| 133 |
ctx.glColor3fv(color[7]); |
| 134 |
ctx.glVertex3fv(cube[7]); |
| 135 |
ctx.glColor3fv(color[2]); |
| 136 |
ctx.glVertex3fv(cube[2]); |
| 137 |
|
| 138 |
ctx.glColor3fv(color[0]); |
| 139 |
ctx.glVertex3fv(cube[0]); |
| 140 |
ctx.glColor3fv(color[5]); |
| 141 |
ctx.glVertex3fv(cube[5]); |
| 142 |
ctx.glColor3fv(color[6]); |
| 143 |
ctx.glVertex3fv(cube[6]); |
| 144 |
ctx.glColor3fv(color[1]); |
| 145 |
ctx.glVertex3fv(cube[1]); |
| 146 |
|
| 147 |
ctx.glColor3fv(color[5]); |
| 148 |
ctx.glVertex3fv(cube[5]); |
| 149 |
ctx.glColor3fv(color[4]); |
| 150 |
ctx.glVertex3fv(cube[4]); |
| 151 |
ctx.glColor3fv(color[7]); |
| 152 |
ctx.glVertex3fv(cube[7]); |
| 153 |
ctx.glColor3fv(color[6]); |
| 154 |
ctx.glVertex3fv(cube[6]); |
| 155 |
|
| 156 |
ctx.glColor3fv(color[5]); |
| 157 |
ctx.glVertex3fv(cube[5]); |
| 158 |
ctx.glColor3fv(color[0]); |
| 159 |
ctx.glVertex3fv(cube[0]); |
| 160 |
ctx.glColor3fv(color[3]); |
| 161 |
ctx.glVertex3fv(cube[3]); |
| 162 |
ctx.glColor3fv(color[4]); |
| 163 |
ctx.glVertex3fv(cube[4]); |
| 164 |
|
| 165 |
ctx.glColor3fv(color[6]); |
| 166 |
ctx.glVertex3fv(cube[6]); |
| 167 |
ctx.glColor3fv(color[1]); |
| 168 |
ctx.glVertex3fv(cube[1]); |
| 169 |
ctx.glColor3fv(color[2]); |
| 170 |
ctx.glVertex3fv(cube[2]); |
| 171 |
ctx.glColor3fv(color[7]); |
| 172 |
ctx.glVertex3fv(cube[7]); |
| 173 |
#else /* flat cube */ |
| 174 |
ctx.glColor3f(1.0, 0.0, 0.0); |
| 175 |
ctx.glVertex3fv(cube[0]); |
| 176 |
ctx.glVertex3fv(cube[1]); |
| 177 |
ctx.glVertex3fv(cube[2]); |
| 178 |
ctx.glVertex3fv(cube[3]); |
| 179 |
|
| 180 |
ctx.glColor3f(0.0, 1.0, 0.0); |
| 181 |
ctx.glVertex3fv(cube[3]); |
| 182 |
ctx.glVertex3fv(cube[4]); |
| 183 |
ctx.glVertex3fv(cube[7]); |
| 184 |
ctx.glVertex3fv(cube[2]); |
| 185 |
|
| 186 |
ctx.glColor3f(0.0, 0.0, 1.0); |
| 187 |
ctx.glVertex3fv(cube[0]); |
| 188 |
ctx.glVertex3fv(cube[5]); |
| 189 |
ctx.glVertex3fv(cube[6]); |
| 190 |
ctx.glVertex3fv(cube[1]); |
| 191 |
|
| 192 |
ctx.glColor3f(0.0, 1.0, 1.0); |
| 193 |
ctx.glVertex3fv(cube[5]); |
| 194 |
ctx.glVertex3fv(cube[4]); |
| 195 |
ctx.glVertex3fv(cube[7]); |
| 196 |
ctx.glVertex3fv(cube[6]); |
| 197 |
|
| 198 |
ctx.glColor3f(1.0, 1.0, 0.0); |
| 199 |
ctx.glVertex3fv(cube[5]); |
| 200 |
ctx.glVertex3fv(cube[0]); |
| 201 |
ctx.glVertex3fv(cube[3]); |
| 202 |
ctx.glVertex3fv(cube[4]); |
| 203 |
|
| 204 |
ctx.glColor3f(1.0, 0.0, 1.0); |
| 205 |
ctx.glVertex3fv(cube[6]); |
| 206 |
ctx.glVertex3fv(cube[1]); |
| 207 |
ctx.glVertex3fv(cube[2]); |
| 208 |
ctx.glVertex3fv(cube[7]); |
| 209 |
#endif /* SHADED_CUBE */ |
| 210 |
|
| 211 |
ctx.glEnd(); |
| 212 |
|
| 213 |
ctx.glMatrixMode(GL_MODELVIEW); |
| 214 |
ctx.glRotatef(5.0, 1.0, 1.0, 1.0); |
| 215 |
} |
| 216 |
|
| 217 |
static void LogSwapInterval(void) |
| 218 |
{ |
| 219 |
SDL_Log("Swap Interval : %d\n", SDL_GL_GetSwapInterval()); |
| 220 |
} |
| 221 |
|
| 222 |
int main(int argc, char *argv[]) |
| 223 |
{ |
| 224 |
int fsaa, accel; |
| 225 |
int value; |
| 226 |
int i, done; |
| 227 |
SDL_DisplayMode mode; |
| 228 |
SDL_Event event; |
| 229 |
Uint32 then, now, frames; |
| 230 |
int status; |
| 231 |
int dw, dh; |
| 232 |
int swap_interval = 0; |
| 233 |
|
| 234 |
/* Enable standard application logging */ |
| 235 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 236 |
|
| 237 |
/* Initialize parameters */ |
| 238 |
fsaa = 0; |
| 239 |
accel = -1; |
| 240 |
|
| 241 |
/* Initialize test framework */ |
| 242 |
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); |
| 243 |
if (!state) { |
| 244 |
return 1; |
| 245 |
} |
| 246 |
for (i = 1; i < argc;) { |
| 247 |
int consumed; |
| 248 |
|
| 249 |
consumed = SDLTest_CommonArg(state, i); |
| 250 |
if (consumed == 0) { |
| 251 |
if (SDL_strcasecmp(argv[i], "--fsaa") == 0 && i + 1 < argc) { |
| 252 |
fsaa = SDL_atoi(argv[i + 1]); |
| 253 |
consumed = 2; |
| 254 |
} else if (SDL_strcasecmp(argv[i], "--accel") == 0 && i + 1 < argc) { |
| 255 |
accel = SDL_atoi(argv[i + 1]); |
| 256 |
consumed = 2; |
| 257 |
} else { |
| 258 |
consumed = -1; |
| 259 |
} |
| 260 |
} |
| 261 |
if (consumed < 0) { |
| 262 |
static const char *options[] = { "[--fsaa n]", "[--accel n]", NULL }; |
| 263 |
SDLTest_CommonLogUsage(state, argv[0], options); |
| 264 |
quit(1); |
| 265 |
} |
| 266 |
i += consumed; |
| 267 |
} |
| 268 |
|
| 269 |
/* Set OpenGL parameters */ |
| 270 |
state->window_flags |= SDL_WINDOW_OPENGL; |
| 271 |
state->gl_red_size = 5; |
| 272 |
state->gl_green_size = 5; |
| 273 |
state->gl_blue_size = 5; |
| 274 |
state->gl_depth_size = 16; |
| 275 |
state->gl_double_buffer = 1; |
| 276 |
if (fsaa) { |
| 277 |
state->gl_multisamplebuffers = 1; |
| 278 |
state->gl_multisamplesamples = fsaa; |
| 279 |
} |
| 280 |
if (accel >= 0) { |
| 281 |
state->gl_accelerated = accel; |
| 282 |
} |
| 283 |
|
| 284 |
if (!SDLTest_CommonInit(state)) { |
| 285 |
quit(2); |
| 286 |
} |
| 287 |
|
| 288 |
/* Create OpenGL context */ |
| 289 |
context = SDL_GL_CreateContext(state->windows[0]); |
| 290 |
if (!context) { |
| 291 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GL_CreateContext(): %s\n", SDL_GetError()); |
| 292 |
quit(2); |
| 293 |
} |
| 294 |
|
| 295 |
/* Important: call this *after* creating the context */ |
| 296 |
if (LoadContext(&ctx) < 0) { |
| 297 |
SDL_Log("Could not load GL functions\n"); |
| 298 |
quit(2); |
| 299 |
return 0; |
| 300 |
} |
| 301 |
|
| 302 |
if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) { |
| 303 |
/* try late-swap-tearing first. If not supported, try normal vsync. */ |
| 304 |
if (SDL_GL_SetSwapInterval(-1) == 0) { |
| 305 |
swap_interval = -1; |
| 306 |
} else { |
| 307 |
SDL_GL_SetSwapInterval(1); |
| 308 |
swap_interval = 1; |
| 309 |
} |
| 310 |
} else { |
| 311 |
SDL_GL_SetSwapInterval(0); /* disable vsync. */ |
| 312 |
swap_interval = 0; |
| 313 |
} |
| 314 |
|
| 315 |
SDL_GetCurrentDisplayMode(0, &mode); |
| 316 |
SDL_Log("Screen BPP : %" SDL_PRIu32 "\n", SDL_BITSPERPIXEL(mode.format)); |
| 317 |
|
| 318 |
LogSwapInterval(); |
| 319 |
|
| 320 |
SDL_GetWindowSize(state->windows[0], &dw, &dh); |
| 321 |
SDL_Log("Window Size : %d,%d\n", dw, dh); |
| 322 |
SDL_GL_GetDrawableSize(state->windows[0], &dw, &dh); |
| 323 |
SDL_Log("Draw Size : %d,%d\n", dw, dh); |
| 324 |
SDL_Log("\n"); |
| 325 |
SDL_Log("Vendor : %s\n", ctx.glGetString(GL_VENDOR)); |
| 326 |
SDL_Log("Renderer : %s\n", ctx.glGetString(GL_RENDERER)); |
| 327 |
SDL_Log("Version : %s\n", ctx.glGetString(GL_VERSION)); |
| 328 |
SDL_Log("Extensions : %s\n", ctx.glGetString(GL_EXTENSIONS)); |
| 329 |
SDL_Log("\n"); |
| 330 |
|
| 331 |
status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value); |
| 332 |
if (!status) { |
| 333 |
SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value); |
| 334 |
} else { |
| 335 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_RED_SIZE: %s\n", SDL_GetError()); |
| 336 |
} |
| 337 |
status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value); |
| 338 |
if (!status) { |
| 339 |
SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value); |
| 340 |
} else { |
| 341 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_GREEN_SIZE: %s\n", SDL_GetError()); |
| 342 |
} |
| 343 |
status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value); |
| 344 |
if (!status) { |
| 345 |
SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value); |
| 346 |
} else { |
| 347 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_BLUE_SIZE: %s\n", SDL_GetError()); |
| 348 |
} |
| 349 |
status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value); |
| 350 |
if (!status) { |
| 351 |
SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", 16, value); |
| 352 |
} else { |
| 353 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_DEPTH_SIZE: %s\n", SDL_GetError()); |
| 354 |
} |
| 355 |
if (fsaa) { |
| 356 |
status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value); |
| 357 |
if (!status) { |
| 358 |
SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value); |
| 359 |
} else { |
| 360 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n", |
| 361 |
SDL_GetError()); |
| 362 |
} |
| 363 |
status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value); |
| 364 |
if (!status) { |
| 365 |
SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa, |
| 366 |
value); |
| 367 |
} else { |
| 368 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n", |
| 369 |
SDL_GetError()); |
| 370 |
} |
| 371 |
} |
| 372 |
if (accel >= 0) { |
| 373 |
status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value); |
| 374 |
if (!status) { |
| 375 |
SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested %d, got %d\n", accel, |
| 376 |
value); |
| 377 |
} else { |
| 378 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n", |
| 379 |
SDL_GetError()); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
/* Set rendering settings */ |
| 384 |
ctx.glMatrixMode(GL_PROJECTION); |
| 385 |
ctx.glLoadIdentity(); |
| 386 |
ctx.glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0); |
| 387 |
ctx.glMatrixMode(GL_MODELVIEW); |
| 388 |
ctx.glLoadIdentity(); |
| 389 |
ctx.glEnable(GL_DEPTH_TEST); |
| 390 |
ctx.glDepthFunc(GL_LESS); |
| 391 |
ctx.glShadeModel(GL_SMOOTH); |
| 392 |
|
| 393 |
/* Main render loop */ |
| 394 |
frames = 0; |
| 395 |
then = SDL_GetTicks(); |
| 396 |
done = 0; |
| 397 |
while (!done) { |
| 398 |
SDL_bool update_swap_interval = SDL_FALSE; |
| 399 |
|
| 400 |
/* Check for events */ |
| 401 |
++frames; |
| 402 |
while (SDL_PollEvent(&event)) { |
| 403 |
SDLTest_CommonEvent(state, &event, &done); |
| 404 |
if (event.type == SDL_KEYDOWN) { |
| 405 |
if (event.key.keysym.sym == SDLK_o) { |
| 406 |
swap_interval--; |
| 407 |
update_swap_interval = SDL_TRUE; |
| 408 |
} else if (event.key.keysym.sym == SDLK_p) { |
| 409 |
swap_interval++; |
| 410 |
update_swap_interval = SDL_TRUE; |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
if (update_swap_interval) { |
| 416 |
SDL_Log("Swap interval to be set to %d\n", swap_interval); |
| 417 |
} |
| 418 |
|
| 419 |
for (i = 0; i < state->num_windows; ++i) { |
| 420 |
int w, h; |
| 421 |
if (state->windows[i] == NULL) { |
| 422 |
continue; |
| 423 |
} |
| 424 |
SDL_GL_MakeCurrent(state->windows[i], context); |
| 425 |
if (update_swap_interval) { |
| 426 |
SDL_GL_SetSwapInterval(swap_interval); |
| 427 |
LogSwapInterval(); |
| 428 |
} |
| 429 |
SDL_GL_GetDrawableSize(state->windows[i], &w, &h); |
| 430 |
ctx.glViewport(0, 0, w, h); |
| 431 |
Render(); |
| 432 |
SDL_GL_SwapWindow(state->windows[i]); |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
/* Print out some timing information */ |
| 437 |
now = SDL_GetTicks(); |
| 438 |
if (now > then) { |
| 439 |
SDL_Log("%2.2f frames per second\n", |
| 440 |
((double)frames * 1000) / (now - then)); |
| 441 |
} |
| 442 |
quit(0); |
| 443 |
return 0; |
| 444 |
} |
| 445 |
|
| 446 |
#else /* HAVE_OPENGL */ |
| 447 |
|
| 448 |
int main(int argc, char *argv[]) |
| 449 |
{ |
| 450 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No OpenGL support on this system\n"); |
| 451 |
return 1; |
| 452 |
} |
| 453 |
|
| 454 |
#endif /* HAVE_OPENGL */ |