Atlas - testgles.c

Home / ext / SDL / test Lines: 1 | Size: 11142 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Copyright (C) 1997-2025 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 14#include <SDL3/SDL_test_common.h> 15#include <SDL3/SDL_main.h> 16 17#if defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_ANDROID) 18#define HAVE_OPENGLES 19#endif 20 21#ifdef HAVE_OPENGLES 22 23#include <SDL3/SDL_opengles.h> 24 25static SDLTest_CommonState *state; 26static SDL_GLContext *context = NULL; 27static int depth = 16; 28 29/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ 30static void 31quit(int rc) 32{ 33 int i; 34 35 if (context) { 36 for (i = 0; i < state->num_windows; i++) { 37 if (context[i]) { 38 SDL_GL_DestroyContext(context[i]); 39 } 40 } 41 42 SDL_free(context); 43 } 44 45 SDLTest_CommonQuit(state); 46 /* Let 'main()' return normally */ 47 if (rc != 0) { 48 exit(rc); 49 } 50} 51 52static void 53Render(void) 54{ 55 static GLubyte color[8][4] = { { 255, 0, 0, 0 }, 56 { 255, 0, 0, 255 }, 57 { 0, 255, 0, 255 }, 58 { 0, 255, 0, 255 }, 59 { 0, 255, 0, 255 }, 60 { 255, 255, 255, 255 }, 61 { 255, 0, 255, 255 }, 62 { 0, 0, 255, 255 } }; 63 static GLfloat cube[8][3] = { { 0.5, 0.5, -0.5 }, 64 { 0.5f, -0.5f, -0.5f }, 65 { -0.5f, -0.5f, -0.5f }, 66 { -0.5f, 0.5f, -0.5f }, 67 { -0.5f, 0.5f, 0.5f }, 68 { 0.5f, 0.5f, 0.5f }, 69 { 0.5f, -0.5f, 0.5f }, 70 { -0.5f, -0.5f, 0.5f } }; 71 static GLubyte indices[36] = { 0, 3, 4, 72 4, 5, 0, 73 0, 5, 6, 74 6, 1, 0, 75 6, 7, 2, 76 2, 1, 6, 77 7, 4, 3, 78 3, 2, 7, 79 5, 4, 7, 80 7, 6, 5, 81 2, 3, 1, 82 3, 0, 1 }; 83 84 /* Do our drawing, too. */ 85 glClearColor(0.0, 0.0, 0.0, 1.0); 86 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 87 88 /* Draw the cube */ 89 glColorPointer(4, GL_UNSIGNED_BYTE, 0, color); 90 glEnableClientState(GL_COLOR_ARRAY); 91 glVertexPointer(3, GL_FLOAT, 0, cube); 92 glEnableClientState(GL_VERTEX_ARRAY); 93 glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices); 94 95 glMatrixMode(GL_MODELVIEW); 96 glRotatef(5.0, 1.0, 1.0, 1.0); 97} 98 99int main(int argc, char *argv[]) 100{ 101 int fsaa, accel; 102 int value; 103 int i, done; 104 const SDL_DisplayMode *mode; 105 SDL_Event event; 106 Uint32 then, now, frames; 107 108 /* Initialize parameters */ 109 fsaa = 0; 110 accel = 0; 111 112 /* Initialize test framework */ 113 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); 114 if (!state) { 115 return 1; 116 } 117 118 for (i = 1; i < argc;) { 119 int consumed; 120 121 consumed = SDLTest_CommonArg(state, i); 122 if (consumed == 0) { 123 if (SDL_strcasecmp(argv[i], "--fsaa") == 0) { 124 ++fsaa; 125 consumed = 1; 126 } else if (SDL_strcasecmp(argv[i], "--accel") == 0) { 127 ++accel; 128 consumed = 1; 129 } else if (SDL_strcasecmp(argv[i], "--zdepth") == 0) { 130 i++; 131 if (!argv[i]) { 132 consumed = -1; 133 } else { 134 char *endptr = NULL; 135 depth = (int)SDL_strtol(argv[i], &endptr, 0); 136 if (endptr != argv[i] && *endptr == '\0') { 137 consumed = 1; 138 } else { 139 consumed = -1; 140 } 141 } 142 } else { 143 consumed = -1; 144 } 145 } 146 if (consumed < 0) { 147 static const char *options[] = { "[--fsaa]", "[--accel]", "[--zdepth %d]", NULL }; 148 SDLTest_CommonLogUsage(state, argv[0], options); 149 quit(1); 150 } 151 i += consumed; 152 } 153 154 /* Set OpenGL parameters */ 155 state->window_flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_BORDERLESS; 156 state->gl_red_size = 5; 157 state->gl_green_size = 5; 158 state->gl_blue_size = 5; 159 state->gl_depth_size = depth; 160 state->gl_major_version = 1; 161 state->gl_minor_version = 1; 162 state->gl_profile_mask = SDL_GL_CONTEXT_PROFILE_ES; 163 if (fsaa) { 164 state->gl_multisamplebuffers = 1; 165 state->gl_multisamplesamples = fsaa; 166 } 167 if (accel) { 168 state->gl_accelerated = 1; 169 } 170 if (!SDLTest_CommonInit(state)) { 171 quit(2); 172 } 173 174 context = (SDL_GLContext *)SDL_calloc(state->num_windows, sizeof(*context)); 175 if (!context) { 176 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!"); 177 quit(2); 178 } 179 180 /* Create OpenGL ES contexts */ 181 for (i = 0; i < state->num_windows; i++) { 182 context[i] = SDL_GL_CreateContext(state->windows[i]); 183 if (!context[i]) { 184 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GL_CreateContext(): %s", SDL_GetError()); 185 quit(2); 186 } 187 } 188 189 SDL_GL_SetSwapInterval(state->render_vsync); 190 191 mode = SDL_GetCurrentDisplayMode(SDL_GetPrimaryDisplay()); 192 if (mode) { 193 SDL_Log("Screen bpp: %d", SDL_BITSPERPIXEL(mode->format)); 194 SDL_Log("%s", ""); 195 } 196 SDL_Log("Vendor : %s", glGetString(GL_VENDOR)); 197 SDL_Log("Renderer : %s", glGetString(GL_RENDERER)); 198 SDL_Log("Version : %s", glGetString(GL_VERSION)); 199 SDL_Log("Extensions : %s", glGetString(GL_EXTENSIONS)); 200 SDL_Log("%s", ""); 201 202 if (SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value)) { 203 SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d", 5, value); 204 } else { 205 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_RED_SIZE: %s", 206 SDL_GetError()); 207 } 208 if (SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value)) { 209 SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d", 5, value); 210 } else { 211 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_GREEN_SIZE: %s", 212 SDL_GetError()); 213 } 214 if (SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value)) { 215 SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d", 5, value); 216 } else { 217 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_BLUE_SIZE: %s", 218 SDL_GetError()); 219 } 220 if (SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value)) { 221 SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d", depth, value); 222 } else { 223 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_DEPTH_SIZE: %s", 224 SDL_GetError()); 225 } 226 if (fsaa) { 227 if (SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value)) { 228 SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d", value); 229 } else { 230 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s", 231 SDL_GetError()); 232 } 233 if (SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value)) { 234 SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d", fsaa, 235 value); 236 } else { 237 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s", 238 SDL_GetError()); 239 } 240 } 241 if (accel) { 242 if (SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value)) { 243 SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d", value); 244 } else { 245 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_ACCELERATED_VISUAL: %s", 246 SDL_GetError()); 247 } 248 } 249 250 /* Set rendering settings for each context */ 251 for (i = 0; i < state->num_windows; ++i) { 252 float aspectAdjust; 253 254 if (!SDL_GL_MakeCurrent(state->windows[i], context[i])) { 255 SDL_Log("SDL_GL_MakeCurrent(): %s", SDL_GetError()); 256 257 /* Continue for next window */ 258 continue; 259 } 260 261 aspectAdjust = (4.0f / 3.0f) / ((float)state->window_w / state->window_h); 262 glViewport(0, 0, state->window_w, state->window_h); 263 glMatrixMode(GL_PROJECTION); 264 glLoadIdentity(); 265 glOrthof(-2.0, 2.0, -2.0 * aspectAdjust, 2.0 * aspectAdjust, -20.0, 20.0); 266 glMatrixMode(GL_MODELVIEW); 267 glLoadIdentity(); 268 glEnable(GL_DEPTH_TEST); 269 glDepthFunc(GL_LESS); 270 glShadeModel(GL_SMOOTH); 271 } 272 273 /* Main render loop */ 274 frames = 0; 275 then = SDL_GetTicks(); 276 done = 0; 277 while (!done) { 278 /* Check for events */ 279 ++frames; 280 while (SDL_PollEvent(&event)) { 281 if (event.type == SDL_EVENT_WINDOW_RESIZED) { 282 for (i = 0; i < state->num_windows; ++i) { 283 if (event.window.windowID == SDL_GetWindowID(state->windows[i])) { 284 if (!SDL_GL_MakeCurrent(state->windows[i], context[i])) { 285 SDL_Log("SDL_GL_MakeCurrent(): %s", SDL_GetError()); 286 break; 287 } 288 /* Change view port to the new window dimensions */ 289 glViewport(0, 0, event.window.data1, event.window.data2); 290 /* Update window content */ 291 Render(); 292 SDL_GL_SwapWindow(state->windows[i]); 293 break; 294 } 295 } 296 } 297 SDLTest_CommonEvent(state, &event, &done); 298 } 299 for (i = 0; i < state->num_windows; ++i) { 300 if (state->windows[i] == NULL) { 301 continue; 302 } 303 if (!SDL_GL_MakeCurrent(state->windows[i], context[i])) { 304 SDL_Log("SDL_GL_MakeCurrent(): %s", SDL_GetError()); 305 306 /* Continue for next window */ 307 continue; 308 } 309 Render(); 310 SDL_GL_SwapWindow(state->windows[i]); 311 } 312 } 313 314 /* Print out some timing information */ 315 now = SDL_GetTicks(); 316 if (now > then) { 317 SDL_Log("%2.2f frames per second", 318 ((double)frames * 1000) / (now - then)); 319 } 320#ifndef SDL_PLATFORM_ANDROID 321 quit(0); 322#endif 323 return 0; 324} 325 326#else /* HAVE_OPENGLES */ 327 328int main(int argc, char *argv[]) 329{ 330 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No OpenGL ES support on this system"); 331 return 1; 332} 333 334#endif /* HAVE_OPENGLES */ 335
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.