Debian Codegraph @

test/testgles.c

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