test/testgles2.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 19 Feb 2015 23:52:10 -0500
changeset 9370 795d4866e4db
parent 9278 8900afb78a19
child 9606 ec24a5add229
permissions -rw-r--r--
Mac OS X: Better way to stop duplicate joystick reports (thanks, Kyungdahm!).

Fixes Bugzilla #2822.
     1 /*
     2   Copyright (r) 1997-2014 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 #ifdef __EMSCRIPTEN__
    18 #include <emscripten/emscripten.h>
    19 #endif
    20 
    21 #include "SDL_test_common.h"
    22 
    23 #if defined(__IPHONEOS__) || defined(__ANDROID__) || defined(__NACL__)
    24 #define HAVE_OPENGLES2
    25 #endif
    26 
    27 #ifdef HAVE_OPENGLES2
    28 
    29 #include "SDL_opengles2.h"
    30 
    31 typedef struct GLES2_Context
    32 {
    33 #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
    34 #include "../src/render/opengles2/SDL_gles2funcs.h"
    35 #undef SDL_PROC
    36 } GLES2_Context;
    37 
    38 
    39 static SDLTest_CommonState *state;
    40 static SDL_GLContext *context = NULL;
    41 static int depth = 16;
    42 static GLES2_Context ctx;
    43 
    44 static int LoadContext(GLES2_Context * data)
    45 {
    46 #if SDL_VIDEO_DRIVER_UIKIT
    47 #define __SDL_NOGETPROCADDR__
    48 #elif SDL_VIDEO_DRIVER_ANDROID
    49 #define __SDL_NOGETPROCADDR__
    50 #elif SDL_VIDEO_DRIVER_PANDORA
    51 #define __SDL_NOGETPROCADDR__
    52 #endif
    53 
    54 #if defined __SDL_NOGETPROCADDR__
    55 #define SDL_PROC(ret,func,params) data->func=func;
    56 #else
    57 #define SDL_PROC(ret,func,params) \
    58     do { \
    59         data->func = SDL_GL_GetProcAddress(#func); \
    60         if ( ! data->func ) { \
    61             return SDL_SetError("Couldn't load GLES2 function %s: %s\n", #func, SDL_GetError()); \
    62         } \
    63     } while ( 0 );
    64 #endif /* _SDL_NOGETPROCADDR_ */
    65 
    66 #include "../src/render/opengles2/SDL_gles2funcs.h"
    67 #undef SDL_PROC
    68     return 0;
    69 }
    70 
    71 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    72 static void
    73 quit(int rc)
    74 {
    75     int i;
    76 
    77     if (context != NULL) {
    78         for (i = 0; i < state->num_windows; i++) {
    79             if (context[i]) {
    80                 SDL_GL_DeleteContext(context[i]);
    81             }
    82         }
    83 
    84         SDL_free(context);
    85     }
    86 
    87     SDLTest_CommonQuit(state);
    88     exit(rc);
    89 }
    90 
    91 #define GL_CHECK(x) \
    92         x; \
    93         { \
    94           GLenum glError = ctx.glGetError(); \
    95           if(glError != GL_NO_ERROR) { \
    96             SDL_Log("glGetError() = %i (0x%.8x) at line %i\n", glError, glError, __LINE__); \
    97             quit(1); \
    98           } \
    99         }
   100 
   101 /* 
   102  * Simulates desktop's glRotatef. The matrix is returned in column-major 
   103  * order. 
   104  */
   105 static void
   106 rotate_matrix(float angle, float x, float y, float z, float *r)
   107 {
   108     float radians, c, s, c1, u[3], length;
   109     int i, j;
   110 
   111     radians = (float)(angle * M_PI) / 180.0f;
   112 
   113     c = SDL_cosf(radians);
   114     s = SDL_sinf(radians);
   115 
   116     c1 = 1.0f - SDL_cosf(radians);
   117 
   118     length = (float)SDL_sqrt(x * x + y * y + z * z);
   119 
   120     u[0] = x / length;
   121     u[1] = y / length;
   122     u[2] = z / length;
   123 
   124     for (i = 0; i < 16; i++) {
   125         r[i] = 0.0;
   126     }
   127 
   128     r[15] = 1.0;
   129 
   130     for (i = 0; i < 3; i++) {
   131         r[i * 4 + (i + 1) % 3] = u[(i + 2) % 3] * s;
   132         r[i * 4 + (i + 2) % 3] = -u[(i + 1) % 3] * s;
   133     }
   134 
   135     for (i = 0; i < 3; i++) {
   136         for (j = 0; j < 3; j++) {
   137             r[i * 4 + j] += c1 * u[i] * u[j] + (i == j ? c : 0.0f);
   138         }
   139     }
   140 }
   141 
   142 /* 
   143  * Simulates gluPerspectiveMatrix 
   144  */
   145 static void 
   146 perspective_matrix(float fovy, float aspect, float znear, float zfar, float *r)
   147 {
   148     int i;
   149     float f;
   150 
   151     f = 1.0f/SDL_tanf(fovy * 0.5f);
   152 
   153     for (i = 0; i < 16; i++) {
   154         r[i] = 0.0;
   155     }
   156 
   157     r[0] = f / aspect;
   158     r[5] = f;
   159     r[10] = (znear + zfar) / (znear - zfar);
   160     r[11] = -1.0f;
   161     r[14] = (2.0f * znear * zfar) / (znear - zfar);
   162     r[15] = 0.0f;
   163 }
   164 
   165 /* 
   166  * Multiplies lhs by rhs and writes out to r. All matrices are 4x4 and column
   167  * major. In-place multiplication is supported.
   168  */
   169 static void
   170 multiply_matrix(float *lhs, float *rhs, float *r)
   171 {
   172 	int i, j, k;
   173     float tmp[16];
   174 
   175     for (i = 0; i < 4; i++) {
   176         for (j = 0; j < 4; j++) {
   177             tmp[j * 4 + i] = 0.0;
   178 
   179             for (k = 0; k < 4; k++) {
   180                 tmp[j * 4 + i] += lhs[k * 4 + i] * rhs[j * 4 + k];
   181             }
   182         }
   183     }
   184 
   185     for (i = 0; i < 16; i++) {
   186         r[i] = tmp[i];
   187     }
   188 }
   189 
   190 /* 
   191  * Create shader, load in source, compile, dump debug as necessary.
   192  *
   193  * shader: Pointer to return created shader ID.
   194  * source: Passed-in shader source code.
   195  * shader_type: Passed to GL, e.g. GL_VERTEX_SHADER.
   196  */
   197 void 
   198 process_shader(GLuint *shader, const char * source, GLint shader_type)
   199 {
   200     GLint status = GL_FALSE;
   201     const char *shaders[1] = { NULL };
   202     char buffer[1024];
   203     GLsizei length;
   204 
   205     /* Create shader and load into GL. */
   206     *shader = GL_CHECK(ctx.glCreateShader(shader_type));
   207 
   208     shaders[0] = source;
   209 
   210     GL_CHECK(ctx.glShaderSource(*shader, 1, shaders, NULL));
   211 
   212     /* Clean up shader source. */
   213     shaders[0] = NULL;
   214 
   215     /* Try compiling the shader. */
   216     GL_CHECK(ctx.glCompileShader(*shader));
   217     GL_CHECK(ctx.glGetShaderiv(*shader, GL_COMPILE_STATUS, &status));
   218 
   219     /* Dump debug info (source and log) if compilation failed. */
   220     if(status != GL_TRUE) {
   221         ctx.glGetProgramInfoLog(*shader, sizeof(buffer), &length, &buffer[0]);
   222         buffer[length] = '\0';
   223         SDL_Log("Shader compilation failed: %s", buffer);fflush(stderr);
   224         quit(-1);
   225     }
   226 }
   227 
   228 /* 3D data. Vertex range -0.5..0.5 in all axes.
   229 * Z -0.5 is near, 0.5 is far. */
   230 const float _vertices[] =
   231 {
   232     /* Front face. */
   233     /* Bottom left */
   234     -0.5,  0.5, -0.5,
   235     0.5, -0.5, -0.5,
   236     -0.5, -0.5, -0.5,
   237     /* Top right */
   238     -0.5,  0.5, -0.5,
   239     0.5,  0.5, -0.5,
   240     0.5, -0.5, -0.5,
   241     /* Left face */
   242     /* Bottom left */
   243     -0.5,  0.5,  0.5,
   244     -0.5, -0.5, -0.5,
   245     -0.5, -0.5,  0.5,
   246     /* Top right */
   247     -0.5,  0.5,  0.5,
   248     -0.5,  0.5, -0.5,
   249     -0.5, -0.5, -0.5,
   250     /* Top face */
   251     /* Bottom left */
   252     -0.5,  0.5,  0.5,
   253     0.5,  0.5, -0.5,
   254     -0.5,  0.5, -0.5,
   255     /* Top right */
   256     -0.5,  0.5,  0.5,
   257     0.5,  0.5,  0.5,
   258     0.5,  0.5, -0.5,
   259     /* Right face */
   260     /* Bottom left */
   261     0.5,  0.5, -0.5,
   262     0.5, -0.5,  0.5,
   263     0.5, -0.5, -0.5,
   264     /* Top right */
   265     0.5,  0.5, -0.5,
   266     0.5,  0.5,  0.5,
   267     0.5, -0.5,  0.5,
   268     /* Back face */
   269     /* Bottom left */
   270     0.5,  0.5,  0.5,
   271     -0.5, -0.5,  0.5,
   272     0.5, -0.5,  0.5,
   273     /* Top right */
   274     0.5,  0.5,  0.5,
   275     -0.5,  0.5,  0.5,
   276     -0.5, -0.5,  0.5,
   277     /* Bottom face */
   278     /* Bottom left */
   279     -0.5, -0.5, -0.5,
   280     0.5, -0.5,  0.5,
   281     -0.5, -0.5,  0.5,
   282     /* Top right */
   283     -0.5, -0.5, -0.5,
   284     0.5, -0.5, -0.5,
   285     0.5, -0.5,  0.5,
   286 };
   287 
   288 const float _colors[] =
   289 {
   290     /* Front face */
   291     /* Bottom left */
   292     1.0, 0.0, 0.0, /* red */
   293     0.0, 0.0, 1.0, /* blue */
   294     0.0, 1.0, 0.0, /* green */
   295     /* Top right */
   296     1.0, 0.0, 0.0, /* red */
   297     1.0, 1.0, 0.0, /* yellow */
   298     0.0, 0.0, 1.0, /* blue */
   299     /* Left face */
   300     /* Bottom left */
   301     1.0, 1.0, 1.0, /* white */
   302     0.0, 1.0, 0.0, /* green */
   303     0.0, 1.0, 1.0, /* cyan */
   304     /* Top right */
   305     1.0, 1.0, 1.0, /* white */
   306     1.0, 0.0, 0.0, /* red */
   307     0.0, 1.0, 0.0, /* green */
   308     /* Top face */
   309     /* Bottom left */
   310     1.0, 1.0, 1.0, /* white */
   311     1.0, 1.0, 0.0, /* yellow */
   312     1.0, 0.0, 0.0, /* red */
   313     /* Top right */
   314     1.0, 1.0, 1.0, /* white */
   315     0.0, 0.0, 0.0, /* black */
   316     1.0, 1.0, 0.0, /* yellow */
   317     /* Right face */
   318     /* Bottom left */
   319     1.0, 1.0, 0.0, /* yellow */
   320     1.0, 0.0, 1.0, /* magenta */
   321     0.0, 0.0, 1.0, /* blue */
   322     /* Top right */
   323     1.0, 1.0, 0.0, /* yellow */
   324     0.0, 0.0, 0.0, /* black */
   325     1.0, 0.0, 1.0, /* magenta */
   326     /* Back face */
   327     /* Bottom left */
   328     0.0, 0.0, 0.0, /* black */
   329     0.0, 1.0, 1.0, /* cyan */
   330     1.0, 0.0, 1.0, /* magenta */
   331     /* Top right */
   332     0.0, 0.0, 0.0, /* black */
   333     1.0, 1.0, 1.0, /* white */
   334     0.0, 1.0, 1.0, /* cyan */
   335     /* Bottom face */
   336     /* Bottom left */
   337     0.0, 1.0, 0.0, /* green */
   338     1.0, 0.0, 1.0, /* magenta */
   339     0.0, 1.0, 1.0, /* cyan */
   340     /* Top right */
   341     0.0, 1.0, 0.0, /* green */
   342     0.0, 0.0, 1.0, /* blue */
   343     1.0, 0.0, 1.0, /* magenta */
   344 };
   345 
   346 const char* _shader_vert_src = 
   347 " attribute vec4 av4position; "
   348 " attribute vec3 av3color; "
   349 " uniform mat4 mvp; "
   350 " varying vec3 vv3color; "
   351 " void main() { "
   352 "    vv3color = av3color; "
   353 "    gl_Position = mvp * av4position; "
   354 " } ";
   355 
   356 const char* _shader_frag_src = 
   357 " precision lowp float; "
   358 " varying vec3 vv3color; "
   359 " void main() { "
   360 "    gl_FragColor = vec4(vv3color, 1.0); "
   361 " } ";
   362 
   363 typedef struct shader_data
   364 {
   365     GLuint shader_program, shader_frag, shader_vert;
   366 
   367     GLint attr_position;
   368     GLint attr_color, attr_mvp;
   369 
   370     int angle_x, angle_y, angle_z;
   371 
   372 } shader_data;
   373 
   374 static void
   375 Render(unsigned int width, unsigned int height, shader_data* data)
   376 {
   377     float matrix_rotate[16], matrix_modelview[16], matrix_perspective[16], matrix_mvp[16];
   378 
   379     /* 
   380     * Do some rotation with Euler angles. It is not a fixed axis as
   381     * quaterions would be, but the effect is cool. 
   382     */
   383     rotate_matrix((float)data->angle_x, 1.0f, 0.0f, 0.0f, matrix_modelview);
   384     rotate_matrix((float)data->angle_y, 0.0f, 1.0f, 0.0f, matrix_rotate);
   385 
   386     multiply_matrix(matrix_rotate, matrix_modelview, matrix_modelview);
   387 
   388     rotate_matrix((float)data->angle_z, 0.0f, 1.0f, 0.0f, matrix_rotate);
   389 
   390     multiply_matrix(matrix_rotate, matrix_modelview, matrix_modelview);
   391 
   392     /* Pull the camera back from the cube */
   393     matrix_modelview[14] -= 2.5;
   394 
   395     perspective_matrix(45.0f, (float)width/height, 0.01f, 100.0f, matrix_perspective);
   396     multiply_matrix(matrix_perspective, matrix_modelview, matrix_mvp);
   397 
   398     GL_CHECK(ctx.glUniformMatrix4fv(data->attr_mvp, 1, GL_FALSE, matrix_mvp));
   399 
   400     data->angle_x += 3;
   401     data->angle_y += 2;
   402     data->angle_z += 1;
   403 
   404     if(data->angle_x >= 360) data->angle_x -= 360;
   405     if(data->angle_x < 0) data->angle_x += 360;
   406     if(data->angle_y >= 360) data->angle_y -= 360;
   407     if(data->angle_y < 0) data->angle_y += 360;
   408     if(data->angle_z >= 360) data->angle_z -= 360;
   409     if(data->angle_z < 0) data->angle_z += 360;
   410 
   411     GL_CHECK(ctx.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT));
   412     GL_CHECK(ctx.glDrawArrays(GL_TRIANGLES, 0, 36));
   413 }
   414 
   415 int done;
   416 Uint32 frames;
   417 shader_data *datas;
   418 
   419 void loop()
   420 {
   421     SDL_Event event;
   422     int i;
   423     int status;
   424 
   425     /* Check for events */
   426     ++frames;
   427     while (SDL_PollEvent(&event) && !done) {
   428         switch (event.type) {
   429         case SDL_WINDOWEVENT:
   430             switch (event.window.event) {
   431                 case SDL_WINDOWEVENT_RESIZED:
   432                     for (i = 0; i < state->num_windows; ++i) {
   433                         if (event.window.windowID == SDL_GetWindowID(state->windows[i])) {
   434                             int w, h;
   435                             status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
   436                             if (status) {
   437                                 SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
   438                                 break;
   439                             }
   440                             /* Change view port to the new window dimensions */
   441                             SDL_GL_GetDrawableSize(state->windows[i], &w, &h);
   442                             ctx.glViewport(0, 0, w, h);
   443                             state->window_w = event.window.data1;
   444                             state->window_h = event.window.data2;
   445                             /* Update window content */
   446                             Render(event.window.data1, event.window.data2, &datas[i]);
   447                             SDL_GL_SwapWindow(state->windows[i]);
   448                             break;
   449                         }
   450                     }
   451                     break;
   452             }
   453         }
   454         SDLTest_CommonEvent(state, &event, &done);
   455     }
   456     if (!done) {
   457       for (i = 0; i < state->num_windows; ++i) {
   458           status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
   459           if (status) {
   460               SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
   461 
   462               /* Continue for next window */
   463               continue;
   464           }
   465           Render(state->window_w, state->window_h, &datas[i]);
   466           SDL_GL_SwapWindow(state->windows[i]);
   467       }
   468     }
   469 }
   470 
   471 int
   472 main(int argc, char *argv[])
   473 {
   474     int fsaa, accel;
   475     int value;
   476     int i;
   477     SDL_DisplayMode mode;
   478     Uint32 then, now;
   479     int status;
   480     shader_data *data;
   481 
   482     /* Initialize parameters */
   483     fsaa = 0;
   484     accel = 0;
   485 
   486     /* Initialize test framework */
   487     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   488     if (!state) {
   489         return 1;
   490     }
   491     for (i = 1; i < argc;) {
   492         int consumed;
   493 
   494         consumed = SDLTest_CommonArg(state, i);
   495         if (consumed == 0) {
   496             if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
   497                 ++fsaa;
   498                 consumed = 1;
   499             } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
   500                 ++accel;
   501                 consumed = 1;
   502             } else if (SDL_strcasecmp(argv[i], "--zdepth") == 0) {
   503                 i++;
   504                 if (!argv[i]) {
   505                     consumed = -1;
   506                 } else {
   507                     depth = SDL_atoi(argv[i]);
   508                     consumed = 1;
   509                 }
   510             } else {
   511                 consumed = -1;
   512             }
   513         }
   514         if (consumed < 0) {
   515             SDL_Log ("Usage: %s %s [--fsaa] [--accel] [--zdepth %%d]\n", argv[0],
   516                     SDLTest_CommonUsage(state));
   517             quit(1);
   518         }
   519         i += consumed;
   520     }
   521 
   522     /* Set OpenGL parameters */
   523     state->window_flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_BORDERLESS;
   524     state->gl_red_size = 5;
   525     state->gl_green_size = 5;
   526     state->gl_blue_size = 5;
   527     state->gl_depth_size = depth;
   528     state->gl_major_version = 2;
   529     state->gl_minor_version = 0;
   530     state->gl_profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
   531 
   532     if (fsaa) {
   533         state->gl_multisamplebuffers=1;
   534         state->gl_multisamplesamples=fsaa;
   535     }
   536     if (accel) {
   537         state->gl_accelerated=1;
   538     }
   539     if (!SDLTest_CommonInit(state)) {
   540         quit(2);
   541         return 0;
   542     }
   543 
   544     context = SDL_calloc(state->num_windows, sizeof(context));
   545     if (context == NULL) {
   546         SDL_Log("Out of memory!\n");
   547         quit(2);
   548     }
   549     
   550     /* Create OpenGL ES contexts */
   551     for (i = 0; i < state->num_windows; i++) {
   552         context[i] = SDL_GL_CreateContext(state->windows[i]);
   553         if (!context[i]) {
   554             SDL_Log("SDL_GL_CreateContext(): %s\n", SDL_GetError());
   555             quit(2);
   556         }
   557     }
   558 
   559     /* Important: call this *after* creating the context */
   560     if (LoadContext(&ctx) < 0) {
   561         SDL_Log("Could not load GLES2 functions\n");
   562         quit(2);
   563         return 0;
   564     }
   565 
   566 
   567 
   568     if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
   569         SDL_GL_SetSwapInterval(1);
   570     } else {
   571         SDL_GL_SetSwapInterval(0);
   572     }
   573 
   574     SDL_GetCurrentDisplayMode(0, &mode);
   575     SDL_Log("Screen bpp: %d\n", SDL_BITSPERPIXEL(mode.format));
   576     SDL_Log("\n");
   577     SDL_Log("Vendor     : %s\n", ctx.glGetString(GL_VENDOR));
   578     SDL_Log("Renderer   : %s\n", ctx.glGetString(GL_RENDERER));
   579     SDL_Log("Version    : %s\n", ctx.glGetString(GL_VERSION));
   580     SDL_Log("Extensions : %s\n", ctx.glGetString(GL_EXTENSIONS));
   581     SDL_Log("\n");
   582 
   583     status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
   584     if (!status) {
   585         SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
   586     } else {
   587         SDL_Log( "Failed to get SDL_GL_RED_SIZE: %s\n",
   588                 SDL_GetError());
   589     }
   590     status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
   591     if (!status) {
   592         SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
   593     } else {
   594         SDL_Log( "Failed to get SDL_GL_GREEN_SIZE: %s\n",
   595                 SDL_GetError());
   596     }
   597     status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
   598     if (!status) {
   599         SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
   600     } else {
   601         SDL_Log( "Failed to get SDL_GL_BLUE_SIZE: %s\n",
   602                 SDL_GetError());
   603     }
   604     status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
   605     if (!status) {
   606         SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", depth, value);
   607     } else {
   608         SDL_Log( "Failed to get SDL_GL_DEPTH_SIZE: %s\n",
   609                 SDL_GetError());
   610     }
   611     if (fsaa) {
   612         status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
   613         if (!status) {
   614             SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
   615         } else {
   616             SDL_Log( "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
   617                     SDL_GetError());
   618         }
   619         status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
   620         if (!status) {
   621             SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
   622                    value);
   623         } else {
   624             SDL_Log( "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
   625                     SDL_GetError());
   626         }
   627     }
   628     if (accel) {
   629         status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
   630         if (!status) {
   631             SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
   632         } else {
   633             SDL_Log( "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
   634                     SDL_GetError());
   635         }
   636     }
   637 
   638     datas = SDL_calloc(state->num_windows, sizeof(shader_data));
   639 
   640     /* Set rendering settings for each context */
   641     for (i = 0; i < state->num_windows; ++i) {
   642 
   643         int w, h;
   644         status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
   645         if (status) {
   646             SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
   647 
   648             /* Continue for next window */
   649             continue;
   650         }
   651         SDL_GL_GetDrawableSize(state->windows[i], &w, &h);
   652         ctx.glViewport(0, 0, w, h);
   653 
   654         data = &datas[i];
   655         data->angle_x = 0; data->angle_y = 0; data->angle_z = 0;
   656 
   657         /* Shader Initialization */
   658         process_shader(&data->shader_vert, _shader_vert_src, GL_VERTEX_SHADER);
   659         process_shader(&data->shader_frag, _shader_frag_src, GL_FRAGMENT_SHADER);
   660 
   661         /* Create shader_program (ready to attach shaders) */
   662         data->shader_program = GL_CHECK(ctx.glCreateProgram());
   663 
   664         /* Attach shaders and link shader_program */
   665         GL_CHECK(ctx.glAttachShader(data->shader_program, data->shader_vert));
   666         GL_CHECK(ctx.glAttachShader(data->shader_program, data->shader_frag));
   667         GL_CHECK(ctx.glLinkProgram(data->shader_program));
   668 
   669         /* Get attribute locations of non-fixed attributes like color and texture coordinates. */
   670         data->attr_position = GL_CHECK(ctx.glGetAttribLocation(data->shader_program, "av4position"));
   671         data->attr_color = GL_CHECK(ctx.glGetAttribLocation(data->shader_program, "av3color"));
   672 
   673         /* Get uniform locations */
   674         data->attr_mvp = GL_CHECK(ctx.glGetUniformLocation(data->shader_program, "mvp"));
   675 
   676         GL_CHECK(ctx.glUseProgram(data->shader_program));
   677 
   678         /* Enable attributes for position, color and texture coordinates etc. */
   679         GL_CHECK(ctx.glEnableVertexAttribArray(data->attr_position));
   680         GL_CHECK(ctx.glEnableVertexAttribArray(data->attr_color));
   681 
   682         /* Populate attributes for position, color and texture coordinates etc. */
   683         GL_CHECK(ctx.glVertexAttribPointer(data->attr_position, 3, GL_FLOAT, GL_FALSE, 0, _vertices));
   684         GL_CHECK(ctx.glVertexAttribPointer(data->attr_color, 3, GL_FLOAT, GL_FALSE, 0, _colors));
   685 
   686         GL_CHECK(ctx.glEnable(GL_CULL_FACE));
   687         GL_CHECK(ctx.glEnable(GL_DEPTH_TEST));
   688     }
   689 
   690     /* Main render loop */
   691     frames = 0;
   692     then = SDL_GetTicks();
   693     done = 0;
   694 
   695 #ifdef __EMSCRIPTEN__
   696     emscripten_set_main_loop(loop, 0, 1);
   697 #else
   698     while (!done) {
   699         loop();
   700     }
   701 #endif
   702 
   703     /* Print out some timing information */
   704     now = SDL_GetTicks();
   705     if (now > then) {
   706         SDL_Log("%2.2f frames per second\n",
   707                ((double) frames * 1000) / (now - then));
   708     }
   709 #if !defined(__ANDROID__) && !defined(__NACL__)  
   710     quit(0);
   711 #endif    
   712     return 0;
   713 }
   714 
   715 #else /* HAVE_OPENGLES2 */
   716 
   717 int
   718 main(int argc, char *argv[])
   719 {
   720     SDL_Log("No OpenGL ES support on this system\n");
   721     return 1;
   722 }
   723 
   724 #endif /* HAVE_OPENGLES2 */
   725 
   726 /* vi: set ts=4 sw=4 expandtab: */