2 Copyright (C) 1997-2016 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"
25 #include "SDL_opengl.h"
27 typedef struct GL_Context
29 #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
30 #include "../src/render/opengl/SDL_glfuncs.h"
35 /* Undefine this if you want a flat cube instead of a rainbow cube */
38 static SDLTest_CommonState *state;
39 static SDL_GLContext context;
40 static GL_Context ctx;
42 static int LoadContext(GL_Context * data)
44 #if SDL_VIDEO_DRIVER_UIKIT
45 #define __SDL_NOGETPROCADDR__
46 #elif SDL_VIDEO_DRIVER_ANDROID
47 #define __SDL_NOGETPROCADDR__
48 #elif SDL_VIDEO_DRIVER_PANDORA
49 #define __SDL_NOGETPROCADDR__
52 #if defined __SDL_NOGETPROCADDR__
53 #define SDL_PROC(ret,func,params) data->func=func;
55 #define SDL_PROC(ret,func,params) \
57 data->func = SDL_GL_GetProcAddress(#func); \
58 if ( ! data->func ) { \
59 return SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
62 #endif /* __SDL_NOGETPROCADDR__ */
64 #include "../src/render/opengl/SDL_glfuncs.h"
70 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
75 /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
76 SDL_GL_DeleteContext(context);
78 SDLTest_CommonQuit(state);
85 static float color[8][3] = {
95 static float cube[8][3] = {
106 /* Do our drawing, too. */
107 ctx.glClearColor(0.0, 0.0, 0.0, 1.0);
108 ctx.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
110 ctx.glBegin(GL_QUADS);
113 ctx.glColor3fv(color[0]);
114 ctx.glVertex3fv(cube[0]);
115 ctx.glColor3fv(color[1]);
116 ctx.glVertex3fv(cube[1]);
117 ctx.glColor3fv(color[2]);
118 ctx.glVertex3fv(cube[2]);
119 ctx.glColor3fv(color[3]);
120 ctx.glVertex3fv(cube[3]);
122 ctx.glColor3fv(color[3]);
123 ctx.glVertex3fv(cube[3]);
124 ctx.glColor3fv(color[4]);
125 ctx.glVertex3fv(cube[4]);
126 ctx.glColor3fv(color[7]);
127 ctx.glVertex3fv(cube[7]);
128 ctx.glColor3fv(color[2]);
129 ctx.glVertex3fv(cube[2]);
131 ctx.glColor3fv(color[0]);
132 ctx.glVertex3fv(cube[0]);
133 ctx.glColor3fv(color[5]);
134 ctx.glVertex3fv(cube[5]);
135 ctx.glColor3fv(color[6]);
136 ctx.glVertex3fv(cube[6]);
137 ctx.glColor3fv(color[1]);
138 ctx.glVertex3fv(cube[1]);
140 ctx.glColor3fv(color[5]);
141 ctx.glVertex3fv(cube[5]);
142 ctx.glColor3fv(color[4]);
143 ctx.glVertex3fv(cube[4]);
144 ctx.glColor3fv(color[7]);
145 ctx.glVertex3fv(cube[7]);
146 ctx.glColor3fv(color[6]);
147 ctx.glVertex3fv(cube[6]);
149 ctx.glColor3fv(color[5]);
150 ctx.glVertex3fv(cube[5]);
151 ctx.glColor3fv(color[0]);
152 ctx.glVertex3fv(cube[0]);
153 ctx.glColor3fv(color[3]);
154 ctx.glVertex3fv(cube[3]);
155 ctx.glColor3fv(color[4]);
156 ctx.glVertex3fv(cube[4]);
158 ctx.glColor3fv(color[6]);
159 ctx.glVertex3fv(cube[6]);
160 ctx.glColor3fv(color[1]);
161 ctx.glVertex3fv(cube[1]);
162 ctx.glColor3fv(color[2]);
163 ctx.glVertex3fv(cube[2]);
164 ctx.glColor3fv(color[7]);
165 ctx.glVertex3fv(cube[7]);
166 #else /* flat cube */
167 ctx.glColor3f(1.0, 0.0, 0.0);
168 ctx.glVertex3fv(cube[0]);
169 ctx.glVertex3fv(cube[1]);
170 ctx.glVertex3fv(cube[2]);
171 ctx.glVertex3fv(cube[3]);
173 ctx.glColor3f(0.0, 1.0, 0.0);
174 ctx.glVertex3fv(cube[3]);
175 ctx.glVertex3fv(cube[4]);
176 ctx.glVertex3fv(cube[7]);
177 ctx.glVertex3fv(cube[2]);
179 ctx.glColor3f(0.0, 0.0, 1.0);
180 ctx.glVertex3fv(cube[0]);
181 ctx.glVertex3fv(cube[5]);
182 ctx.glVertex3fv(cube[6]);
183 ctx.glVertex3fv(cube[1]);
185 ctx.glColor3f(0.0, 1.0, 1.0);
186 ctx.glVertex3fv(cube[5]);
187 ctx.glVertex3fv(cube[4]);
188 ctx.glVertex3fv(cube[7]);
189 ctx.glVertex3fv(cube[6]);
191 ctx.glColor3f(1.0, 1.0, 0.0);
192 ctx.glVertex3fv(cube[5]);
193 ctx.glVertex3fv(cube[0]);
194 ctx.glVertex3fv(cube[3]);
195 ctx.glVertex3fv(cube[4]);
197 ctx.glColor3f(1.0, 0.0, 1.0);
198 ctx.glVertex3fv(cube[6]);
199 ctx.glVertex3fv(cube[1]);
200 ctx.glVertex3fv(cube[2]);
201 ctx.glVertex3fv(cube[7]);
202 #endif /* SHADED_CUBE */
206 ctx.glMatrixMode(GL_MODELVIEW);
207 ctx.glRotatef(5.0, 1.0, 1.0, 1.0);
211 main(int argc, char *argv[])
216 SDL_DisplayMode mode;
218 Uint32 then, now, frames;
222 /* Enable standard application logging */
223 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
225 /* Initialize parameters */
229 /* Initialize test framework */
230 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
234 for (i = 1; i < argc;) {
237 consumed = SDLTest_CommonArg(state, i);
239 if (SDL_strcasecmp(argv[i], "--fsaa") == 0 && i+1 < argc) {
240 fsaa = atoi(argv[i+1]);
242 } else if (SDL_strcasecmp(argv[i], "--accel") == 0 && i+1 < argc) {
243 accel = atoi(argv[i+1]);
250 SDL_Log("Usage: %s %s [--fsaa n] [--accel n]\n", argv[0],
251 SDLTest_CommonUsage(state));
257 /* Set OpenGL parameters */
258 state->window_flags |= SDL_WINDOW_OPENGL;
259 state->gl_red_size = 5;
260 state->gl_green_size = 5;
261 state->gl_blue_size = 5;
262 state->gl_depth_size = 16;
263 state->gl_double_buffer = 1;
265 state->gl_multisamplebuffers = 1;
266 state->gl_multisamplesamples = fsaa;
269 state->gl_accelerated = accel;
272 if (!SDLTest_CommonInit(state)) {
276 /* Create OpenGL context */
277 context = SDL_GL_CreateContext(state->windows[0]);
279 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
283 /* Important: call this *after* creating the context */
284 if (LoadContext(&ctx) < 0) {
285 SDL_Log("Could not load GL functions\n");
290 if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
291 /* try late-swap-tearing first. If not supported, try normal vsync. */
292 if (SDL_GL_SetSwapInterval(-1) == -1) {
293 SDL_GL_SetSwapInterval(1);
296 SDL_GL_SetSwapInterval(0); /* disable vsync. */
299 SDL_GetCurrentDisplayMode(0, &mode);
300 SDL_Log("Screen BPP : %d\n", SDL_BITSPERPIXEL(mode.format));
301 SDL_Log("Swap Interval : %d\n", SDL_GL_GetSwapInterval());
302 SDL_GetWindowSize(state->windows[0], &dw, &dh);
303 SDL_Log("Window Size : %d,%d\n", dw, dh);
304 SDL_GL_GetDrawableSize(state->windows[0], &dw, &dh);
305 SDL_Log("Draw Size : %d,%d\n", dw, dh);
307 SDL_Log("Vendor : %s\n", ctx.glGetString(GL_VENDOR));
308 SDL_Log("Renderer : %s\n", ctx.glGetString(GL_RENDERER));
309 SDL_Log("Version : %s\n", ctx.glGetString(GL_VERSION));
310 SDL_Log("Extensions : %s\n", ctx.glGetString(GL_EXTENSIONS));
313 status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
315 SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
317 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_RED_SIZE: %s\n", SDL_GetError());
319 status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
321 SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
323 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_GREEN_SIZE: %s\n", SDL_GetError());
325 status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
327 SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
329 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_BLUE_SIZE: %s\n", SDL_GetError());
331 status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
333 SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", 16, value);
335 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_DEPTH_SIZE: %s\n", SDL_GetError());
338 status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
340 SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
342 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
345 status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
347 SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
350 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
355 status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
357 SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested %d, got %d\n", accel,
360 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
365 /* Set rendering settings */
366 ctx.glMatrixMode(GL_PROJECTION);
367 ctx.glLoadIdentity();
368 ctx.glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
369 ctx.glMatrixMode(GL_MODELVIEW);
370 ctx.glLoadIdentity();
371 ctx.glEnable(GL_DEPTH_TEST);
372 ctx.glDepthFunc(GL_LESS);
373 ctx.glShadeModel(GL_SMOOTH);
375 /* Main render loop */
377 then = SDL_GetTicks();
380 /* Check for events */
382 while (SDL_PollEvent(&event)) {
383 SDLTest_CommonEvent(state, &event, &done);
385 for (i = 0; i < state->num_windows; ++i) {
387 if (state->windows[i] == NULL)
389 SDL_GL_MakeCurrent(state->windows[i], context);
390 SDL_GL_GetDrawableSize(state->windows[i], &w, &h);
391 ctx.glViewport(0, 0, w, h);
393 SDL_GL_SwapWindow(state->windows[i]);
397 /* Print out some timing information */
398 now = SDL_GetTicks();
400 SDL_Log("%2.2f frames per second\n",
401 ((double) frames * 1000) / (now - then));
407 #else /* HAVE_OPENGL */
410 main(int argc, char *argv[])
412 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No OpenGL support on this system\n");
416 #endif /* HAVE_OPENGL */