2 Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
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.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
17 #include "SDL_test_common.h"
18 #define WINDOW_WIDTH 720
19 #define WINDOW_HEIGHT 1280
20 #if defined(__IPHONEOS__) || defined(__ANDROID__) || defined(__TIZEN__)
27 #include "SDL_opengles.h"
29 static SDLTest_CommonState *state;
30 static SDL_GLContext *context = NULL;
31 static int depth = 16;
33 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
39 if (context != NULL) {
40 for (i = 0; i < state->num_windows; i++) {
42 SDL_GL_DeleteContext(context[i]);
49 SDLTest_CommonQuit(state);
56 static GLubyte color[8][4] = { {255, 0, 0, 0},
65 static GLfloat cube[8][3] = { {0.5, 0.5, -0.5},
67 {-0.5f, -0.5f, -0.5f},
74 static GLubyte indices[36] = { 0, 3, 4,
89 /* Do our drawing, too. */
90 glClearColor(0.0, 0.0, 0.0, 1.0);
91 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
94 glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);
95 glEnableClientState(GL_COLOR_ARRAY);
96 glVertexPointer(3, GL_FLOAT, 0, cube);
97 glEnableClientState(GL_VERTEX_ARRAY);
98 glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);
100 glMatrixMode(GL_MODELVIEW);
101 glRotatef(5.0, 1.0, 1.0, 1.0);
104 main(int argc, char *argv[])
109 SDL_DisplayMode mode;
111 Uint32 then, now, frames;
114 /* Enable standard application logging */
115 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
117 /* Initialize parameters */
121 /* Initialize test framework */
122 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
123 state->window_w = WINDOW_WIDTH;
124 state->window_h = WINDOW_HEIGHT;
128 for (i = 1; i < argc;) {
131 consumed = SDLTest_CommonArg(state, i);
133 if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
136 } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
139 } else if (SDL_strcasecmp(argv[i], "--zdepth") == 0) {
144 depth = SDL_atoi(argv[i]);
152 SDL_Log("Usage: %s %s [--fsaa] [--accel] [--zdepth %%d]\n", argv[0],
153 SDLTest_CommonUsage(state));
159 /* Set OpenGL parameters */
160 state->window_flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_BORDERLESS;
161 state->gl_red_size = 5;
162 state->gl_green_size = 5;
163 state->gl_blue_size = 5;
164 state->gl_depth_size = depth;
165 state->gl_major_version = 1;
166 state->gl_minor_version = 1;
167 state->gl_profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
169 state->gl_multisamplebuffers=1;
170 state->gl_multisamplesamples=fsaa;
173 state->gl_accelerated=1;
175 if (!SDLTest_CommonInit(state)) {
179 context = (SDL_GLContext *)SDL_calloc(state->num_windows, sizeof(context));
180 if (context == NULL) {
181 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
185 /* Create OpenGL ES contexts */
186 for (i = 0; i < state->num_windows; i++) {
187 context[i] = SDL_GL_CreateContext(state->windows[i]);
189 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
194 if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
195 SDL_GL_SetSwapInterval(1);
197 SDL_GL_SetSwapInterval(0);
200 SDL_GetCurrentDisplayMode(0, &mode);
201 SDL_Log("Screen bpp: %d\n", SDL_BITSPERPIXEL(mode.format));
203 SDL_Log("Vendor : %s\n", glGetString(GL_VENDOR));
204 SDL_Log("Renderer : %s\n", glGetString(GL_RENDERER));
205 SDL_Log("Version : %s\n", glGetString(GL_VERSION));
206 SDL_Log("Extensions : %s\n", glGetString(GL_EXTENSIONS));
209 status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
211 SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
213 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_RED_SIZE: %s\n",
216 status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
218 SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
220 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_GREEN_SIZE: %s\n",
223 status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
225 SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
227 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_BLUE_SIZE: %s\n",
230 status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
232 SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", depth, value);
234 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_DEPTH_SIZE: %s\n",
238 status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
240 SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
242 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
245 status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
247 SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
250 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
255 status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
257 SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
259 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
264 /* Set rendering settings for each context */
265 for (i = 0; i < state->num_windows; ++i) {
268 status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
270 SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
272 /* Continue for next window */
276 aspectAdjust = (4.0f / 3.0f) / ((float)state->window_w / state->window_h);
277 glViewport(0, 0, state->window_w, state->window_h);
278 glMatrixMode(GL_PROJECTION);
280 glOrthof(-2.0, 2.0, -2.0 * aspectAdjust, 2.0 * aspectAdjust, -20.0, 20.0);
281 glMatrixMode(GL_MODELVIEW);
283 glEnable(GL_DEPTH_TEST);
284 glDepthFunc(GL_LESS);
285 glShadeModel(GL_SMOOTH);
288 /* Main render loop */
290 then = SDL_GetTicks();
293 /* Check for events */
295 while (SDL_PollEvent(&event)) {
296 switch (event.type) {
297 /*case SDL_WINDOWEVENT:
298 switch (event.window.event) {
299 case SDL_WINDOWEVENT_RESIZED:
300 for (i = 0; i < state->num_windows; ++i) {
301 if (event.window.windowID == SDL_GetWindowID(state->windows[i])) {
302 status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
304 SDLTest_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
307 // Change view port to the new window dimensions
308 glViewport(0, 0, event.window.data1, event.window.data2);
309 // Update window content
311 SDL_GL_SwapWindow(state->windows[i]);
318 if(event.key.keysym.sym == 0)
330 SDLTest_CommonEvent(state, &event, &done);
332 for (i = 0; i < state->num_windows; ++i) {
333 if (state->windows[i] == NULL)
335 status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
337 SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
339 /* Continue for next window */
343 SDL_GL_SwapWindow(state->windows[i]);
347 /* Print out some timing information */
348 now = SDL_GetTicks();
350 SDL_Log("%2.2f frames per second\n",
351 ((double) frames * 1000) / (now - then));
353 #if !defined(__ANDROID__)
359 #else /* HAVE_OPENGLES */
362 main(int argc, char *argv[])
364 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No OpenGL ES support on this system\n");
368 #endif /* HAVE_OPENGLES */
370 /* vi: set ts=4 sw=4 expandtab: */