Add device name of Ecore_Event_Key
[platform/upstream/SDL.git] / test / testgles.c
1 /*
2   Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
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 #define WINDOW_WIDTH 720
19 #define WINDOW_HEIGHT 1280
20 #if defined(__IPHONEOS__) || defined(__ANDROID__) || defined(__TIZEN__)
21 #define HAVE_OPENGLES
22 #endif
23
24
25 #ifdef HAVE_OPENGLES
26
27 #include "SDL_opengles.h"
28
29 static SDLTest_CommonState *state;
30 static SDL_GLContext *context = NULL;
31 static int depth = 16;
32 int once = SDL_TRUE;
33 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
34 static void
35 quit(int rc)
36 {
37     int i;
38
39     if (context != NULL) {
40         for (i = 0; i < state->num_windows; i++) {
41             if (context[i]) {
42                 SDL_GL_DeleteContext(context[i]);
43             }
44         }
45
46         SDL_free(context);
47     }
48
49     SDLTest_CommonQuit(state);
50     exit(rc);
51 }
52
53 static void
54 Render()
55 {
56     static GLubyte color[8][4] = { {255, 0, 0, 0},
57     {255, 0, 0, 255},
58     {0, 255, 0, 255},
59     {0, 255, 0, 255},
60     {0, 255, 0, 255},
61     {255, 255, 255, 255},
62     {255, 0, 255, 255},
63     {0, 0, 255, 255}
64     };
65     static GLfloat cube[8][3] = { {0.5, 0.5, -0.5},
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     {0.5f, -0.5f, 0.5f},
72     {-0.5f, -0.5f, 0.5f}
73     };
74     static GLubyte indices[36] = { 0, 3, 4,
75         4, 5, 0,
76         0, 5, 6,
77         6, 1, 0,
78         6, 7, 2,
79         2, 1, 6,
80         7, 4, 3,
81         3, 2, 7,
82         5, 4, 7,
83         7, 6, 5,
84         2, 3, 1,
85         3, 0, 1
86     };
87
88
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);
92
93     /* Draw the cube */
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);
99
100     glMatrixMode(GL_MODELVIEW);
101     glRotatef(5.0, 1.0, 1.0, 1.0);
102 }
103 int
104 main(int argc, char *argv[])
105 {
106     int fsaa, accel;
107     int value;
108     int i, done;
109     SDL_DisplayMode mode;
110     SDL_Event event;
111     Uint32 then, now, frames;
112     int status;
113
114     /* Enable standard application logging */
115     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
116
117     /* Initialize parameters */
118     fsaa = 0;
119     accel = 0;
120
121     /* Initialize test framework */
122     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
123         state->window_w = WINDOW_WIDTH;
124     state->window_h = WINDOW_HEIGHT;
125     if (!state) {
126         return 1;
127     }
128     for (i = 1; i < argc;) {
129         int consumed;
130
131         consumed = SDLTest_CommonArg(state, i);
132         if (consumed == 0) {
133             if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
134                 ++fsaa;
135                 consumed = 1;
136             } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
137                 ++accel;
138                 consumed = 1;
139             } else if (SDL_strcasecmp(argv[i], "--zdepth") == 0) {
140                 i++;
141                 if (!argv[i]) {
142                     consumed = -1;
143                 } else {
144                     depth = SDL_atoi(argv[i]);
145                     consumed = 1;
146                 }
147             } else {
148                 consumed = -1;
149             }
150         }
151         if (consumed < 0) {
152             SDL_Log("Usage: %s %s [--fsaa] [--accel] [--zdepth %%d]\n", argv[0],
153                     SDLTest_CommonUsage(state));
154             quit(1);
155         }
156         i += consumed;
157     }
158
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;
168     if (fsaa) {
169         state->gl_multisamplebuffers=1;
170         state->gl_multisamplesamples=fsaa;
171     }
172     if (accel) {
173         state->gl_accelerated=1;
174     }
175     if (!SDLTest_CommonInit(state)) {
176         quit(2);
177     }
178
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");
182         quit(2);
183     }
184
185     /* Create OpenGL ES contexts */
186     for (i = 0; i < state->num_windows; i++) {
187         context[i] = SDL_GL_CreateContext(state->windows[i]);
188         if (!context[i]) {
189             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
190             quit(2);
191         }
192     }
193
194     if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
195         SDL_GL_SetSwapInterval(1);
196     } else {
197         SDL_GL_SetSwapInterval(0);
198     }
199
200     SDL_GetCurrentDisplayMode(0, &mode);
201     SDL_Log("Screen bpp: %d\n", SDL_BITSPERPIXEL(mode.format));
202     SDL_Log("\n");
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));
207     SDL_Log("\n");
208
209     status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
210     if (!status) {
211         SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
212     } else {
213         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_RED_SIZE: %s\n",
214                 SDL_GetError());
215     }
216     status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
217     if (!status) {
218         SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
219     } else {
220         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_GREEN_SIZE: %s\n",
221                 SDL_GetError());
222     }
223     status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
224     if (!status) {
225         SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
226     } else {
227         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_BLUE_SIZE: %s\n",
228                 SDL_GetError());
229     }
230     status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
231     if (!status) {
232         SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", depth, value);
233     } else {
234         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_DEPTH_SIZE: %s\n",
235                 SDL_GetError());
236     }
237     if (fsaa) {
238         status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
239         if (!status) {
240             SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
241         } else {
242             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
243                     SDL_GetError());
244         }
245         status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
246         if (!status) {
247             SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
248                    value);
249         } else {
250             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
251                     SDL_GetError());
252         }
253     }
254     if (accel) {
255         status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
256         if (!status) {
257             SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
258         } else {
259             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
260                     SDL_GetError());
261         }
262     }
263
264     /* Set rendering settings for each context */
265     for (i = 0; i < state->num_windows; ++i) {
266         float aspectAdjust;
267
268         status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
269         if (status) {
270             SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
271
272             /* Continue for next window */
273             continue;
274         }
275
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);
279         glLoadIdentity();
280         glOrthof(-2.0, 2.0, -2.0 * aspectAdjust, 2.0 * aspectAdjust, -20.0, 20.0);
281         glMatrixMode(GL_MODELVIEW);
282         glLoadIdentity();
283         glEnable(GL_DEPTH_TEST);
284         glDepthFunc(GL_LESS);
285         glShadeModel(GL_SMOOTH);
286     }
287
288     /* Main render loop */
289     frames = 0;
290     then = SDL_GetTicks();
291     done = 0;
292     while (!done) {
293         /* Check for events */
294         ++frames;
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]);
303                                 if (status) {
304                                     SDLTest_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
305                                     break;
306                                 }
307                                 // Change view port to the new window dimensions 
308                                 glViewport(0, 0, event.window.data1, event.window.data2);
309                                 // Update window content 
310                                 Render();
311                                 SDL_GL_SwapWindow(state->windows[i]);
312                                 break;
313                             }
314                         }
315                         break;
316                 }*/
317                         case SDL_KEYDOWN:
318                                 if(event.key.keysym.sym == 0)
319                                 {
320                                         if(once)
321                                         {
322                                                 once = SDL_FALSE;
323                                         }else
324                                         {
325                                                 done = 1;
326                                         }
327                                 }
328                                 break;
329             }
330             SDLTest_CommonEvent(state, &event, &done);
331         }
332         for (i = 0; i < state->num_windows; ++i) {
333             if (state->windows[i] == NULL)
334                 continue;
335             status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
336             if (status) {
337                 SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
338
339                 /* Continue for next window */
340                 continue;
341             }
342             Render();
343             SDL_GL_SwapWindow(state->windows[i]);
344         }
345     }
346
347     /* Print out some timing information */
348     now = SDL_GetTicks();
349     if (now > then) {
350         SDL_Log("%2.2f frames per second\n",
351                ((double) frames * 1000) / (now - then));
352     }
353 #if !defined(__ANDROID__)
354     quit(0);
355 #endif        
356     return 0;
357 }
358
359 #else /* HAVE_OPENGLES */
360
361 int
362 main(int argc, char *argv[])
363 {
364     SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No OpenGL ES support on this system\n");
365     return 1;
366 }
367
368 #endif /* HAVE_OPENGLES */
369
370 /* vi: set ts=4 sw=4 expandtab: */