/[pcsx2_0.9.7]/trunk/3rdparty/SDL-1.3.0-5387/test/testgles.c
ViewVC logotype

Contents of /trunk/3rdparty/SDL-1.3.0-5387/test/testgles.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22