test/testgl2.c
changeset 8062 4fc5f66d63cc
parent 7787 e6f3e8fc96ea
child 8149 681eb46b8ac4
     1.1 --- a/test/testgl2.c	Fri Dec 13 09:48:12 2013 -0300
     1.2 +++ b/test/testgl2.c	Sat Dec 14 20:18:43 2013 -0300
     1.3 @@ -24,11 +24,48 @@
     1.4  
     1.5  #include "SDL_opengl.h"
     1.6  
     1.7 +typedef struct GL_Context
     1.8 +{
     1.9 +#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
    1.10 +#include "../src/render/opengl/SDL_glfuncs.h"
    1.11 +#undef SDL_PROC
    1.12 +} GL_Context;
    1.13 +
    1.14 +
    1.15  /* Undefine this if you want a flat cube instead of a rainbow cube */
    1.16  #define SHADED_CUBE
    1.17  
    1.18  static SDLTest_CommonState *state;
    1.19  static SDL_GLContext context;
    1.20 +static GL_Context ctx;
    1.21 +
    1.22 +static int LoadContext(GL_Context * data)
    1.23 +{
    1.24 +#if SDL_VIDEO_DRIVER_UIKIT
    1.25 +#define __SDL_NOGETPROCADDR__
    1.26 +#elif SDL_VIDEO_DRIVER_ANDROID
    1.27 +#define __SDL_NOGETPROCADDR__
    1.28 +#elif SDL_VIDEO_DRIVER_PANDORA
    1.29 +#define __SDL_NOGETPROCADDR__
    1.30 +#endif
    1.31 +
    1.32 +#if defined __SDL_NOGETPROCADDR__
    1.33 +#define SDL_PROC(ret,func,params) data->func=func;
    1.34 +#else
    1.35 +#define SDL_PROC(ret,func,params) \
    1.36 +    do { \
    1.37 +        data->func = SDL_GL_GetProcAddress(#func); \
    1.38 +        if ( ! data->func ) { \
    1.39 +            return SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
    1.40 +        } \
    1.41 +    } while ( 0 );
    1.42 +#endif /* _SDL_NOGETPROCADDR_ */
    1.43 +
    1.44 +#include "../src/render/opengl/SDL_glfuncs.h"
    1.45 +#undef SDL_PROC
    1.46 +    return 0;
    1.47 +}
    1.48 +
    1.49  
    1.50  /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    1.51  static void
    1.52 @@ -67,107 +104,107 @@
    1.53      };
    1.54  
    1.55      /* Do our drawing, too. */
    1.56 -    glClearColor(0.0, 0.0, 0.0, 1.0);
    1.57 -    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.58 +    ctx.glClearColor(0.0, 0.0, 0.0, 1.0);
    1.59 +    ctx.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.60  
    1.61 -    glBegin(GL_QUADS);
    1.62 +    ctx.glBegin(GL_QUADS);
    1.63  
    1.64  #ifdef SHADED_CUBE
    1.65 -    glColor3fv(color[0]);
    1.66 -    glVertex3fv(cube[0]);
    1.67 -    glColor3fv(color[1]);
    1.68 -    glVertex3fv(cube[1]);
    1.69 -    glColor3fv(color[2]);
    1.70 -    glVertex3fv(cube[2]);
    1.71 -    glColor3fv(color[3]);
    1.72 -    glVertex3fv(cube[3]);
    1.73 +    ctx.glColor3fv(color[0]);
    1.74 +    ctx.glVertex3fv(cube[0]);
    1.75 +    ctx.glColor3fv(color[1]);
    1.76 +    ctx.glVertex3fv(cube[1]);
    1.77 +    ctx.glColor3fv(color[2]);
    1.78 +    ctx.glVertex3fv(cube[2]);
    1.79 +    ctx.glColor3fv(color[3]);
    1.80 +    ctx.glVertex3fv(cube[3]);
    1.81  
    1.82 -    glColor3fv(color[3]);
    1.83 -    glVertex3fv(cube[3]);
    1.84 -    glColor3fv(color[4]);
    1.85 -    glVertex3fv(cube[4]);
    1.86 -    glColor3fv(color[7]);
    1.87 -    glVertex3fv(cube[7]);
    1.88 -    glColor3fv(color[2]);
    1.89 -    glVertex3fv(cube[2]);
    1.90 +    ctx.glColor3fv(color[3]);
    1.91 +    ctx.glVertex3fv(cube[3]);
    1.92 +    ctx.glColor3fv(color[4]);
    1.93 +    ctx.glVertex3fv(cube[4]);
    1.94 +    ctx.glColor3fv(color[7]);
    1.95 +    ctx.glVertex3fv(cube[7]);
    1.96 +    ctx.glColor3fv(color[2]);
    1.97 +    ctx.glVertex3fv(cube[2]);
    1.98  
    1.99 -    glColor3fv(color[0]);
   1.100 -    glVertex3fv(cube[0]);
   1.101 -    glColor3fv(color[5]);
   1.102 -    glVertex3fv(cube[5]);
   1.103 -    glColor3fv(color[6]);
   1.104 -    glVertex3fv(cube[6]);
   1.105 -    glColor3fv(color[1]);
   1.106 -    glVertex3fv(cube[1]);
   1.107 +    ctx.glColor3fv(color[0]);
   1.108 +    ctx.glVertex3fv(cube[0]);
   1.109 +    ctx.glColor3fv(color[5]);
   1.110 +    ctx.glVertex3fv(cube[5]);
   1.111 +    ctx.glColor3fv(color[6]);
   1.112 +    ctx.glVertex3fv(cube[6]);
   1.113 +    ctx.glColor3fv(color[1]);
   1.114 +    ctx.glVertex3fv(cube[1]);
   1.115  
   1.116 -    glColor3fv(color[5]);
   1.117 -    glVertex3fv(cube[5]);
   1.118 -    glColor3fv(color[4]);
   1.119 -    glVertex3fv(cube[4]);
   1.120 -    glColor3fv(color[7]);
   1.121 -    glVertex3fv(cube[7]);
   1.122 -    glColor3fv(color[6]);
   1.123 -    glVertex3fv(cube[6]);
   1.124 +    ctx.glColor3fv(color[5]);
   1.125 +    ctx.glVertex3fv(cube[5]);
   1.126 +    ctx.glColor3fv(color[4]);
   1.127 +    ctx.glVertex3fv(cube[4]);
   1.128 +    ctx.glColor3fv(color[7]);
   1.129 +    ctx.glVertex3fv(cube[7]);
   1.130 +    ctx.glColor3fv(color[6]);
   1.131 +    ctx.glVertex3fv(cube[6]);
   1.132  
   1.133 -    glColor3fv(color[5]);
   1.134 -    glVertex3fv(cube[5]);
   1.135 -    glColor3fv(color[0]);
   1.136 -    glVertex3fv(cube[0]);
   1.137 -    glColor3fv(color[3]);
   1.138 -    glVertex3fv(cube[3]);
   1.139 -    glColor3fv(color[4]);
   1.140 -    glVertex3fv(cube[4]);
   1.141 +    ctx.glColor3fv(color[5]);
   1.142 +    ctx.glVertex3fv(cube[5]);
   1.143 +    ctx.glColor3fv(color[0]);
   1.144 +    ctx.glVertex3fv(cube[0]);
   1.145 +    ctx.glColor3fv(color[3]);
   1.146 +    ctx.glVertex3fv(cube[3]);
   1.147 +    ctx.glColor3fv(color[4]);
   1.148 +    ctx.glVertex3fv(cube[4]);
   1.149  
   1.150 -    glColor3fv(color[6]);
   1.151 -    glVertex3fv(cube[6]);
   1.152 -    glColor3fv(color[1]);
   1.153 -    glVertex3fv(cube[1]);
   1.154 -    glColor3fv(color[2]);
   1.155 -    glVertex3fv(cube[2]);
   1.156 -    glColor3fv(color[7]);
   1.157 -    glVertex3fv(cube[7]);
   1.158 +    ctx.glColor3fv(color[6]);
   1.159 +    ctx.glVertex3fv(cube[6]);
   1.160 +    ctx.glColor3fv(color[1]);
   1.161 +    ctx.glVertex3fv(cube[1]);
   1.162 +    ctx.glColor3fv(color[2]);
   1.163 +    ctx.glVertex3fv(cube[2]);
   1.164 +    ctx.glColor3fv(color[7]);
   1.165 +    ctx.glVertex3fv(cube[7]);
   1.166  #else /* flat cube */
   1.167 -    glColor3f(1.0, 0.0, 0.0);
   1.168 -    glVertex3fv(cube[0]);
   1.169 -    glVertex3fv(cube[1]);
   1.170 -    glVertex3fv(cube[2]);
   1.171 -    glVertex3fv(cube[3]);
   1.172 +    ctx.glColor3f(1.0, 0.0, 0.0);
   1.173 +    ctx.glVertex3fv(cube[0]);
   1.174 +    ctx.glVertex3fv(cube[1]);
   1.175 +    ctx.glVertex3fv(cube[2]);
   1.176 +    ctx.glVertex3fv(cube[3]);
   1.177  
   1.178 -    glColor3f(0.0, 1.0, 0.0);
   1.179 -    glVertex3fv(cube[3]);
   1.180 -    glVertex3fv(cube[4]);
   1.181 -    glVertex3fv(cube[7]);
   1.182 -    glVertex3fv(cube[2]);
   1.183 +    ctx.glColor3f(0.0, 1.0, 0.0);
   1.184 +    ctx.glVertex3fv(cube[3]);
   1.185 +    ctx.glVertex3fv(cube[4]);
   1.186 +    ctx.glVertex3fv(cube[7]);
   1.187 +    ctx.glVertex3fv(cube[2]);
   1.188  
   1.189 -    glColor3f(0.0, 0.0, 1.0);
   1.190 -    glVertex3fv(cube[0]);
   1.191 -    glVertex3fv(cube[5]);
   1.192 -    glVertex3fv(cube[6]);
   1.193 -    glVertex3fv(cube[1]);
   1.194 +    ctx.glColor3f(0.0, 0.0, 1.0);
   1.195 +    ctx.glVertex3fv(cube[0]);
   1.196 +    ctx.glVertex3fv(cube[5]);
   1.197 +    ctx.glVertex3fv(cube[6]);
   1.198 +    ctx.glVertex3fv(cube[1]);
   1.199  
   1.200 -    glColor3f(0.0, 1.0, 1.0);
   1.201 -    glVertex3fv(cube[5]);
   1.202 -    glVertex3fv(cube[4]);
   1.203 -    glVertex3fv(cube[7]);
   1.204 -    glVertex3fv(cube[6]);
   1.205 +    ctx.glColor3f(0.0, 1.0, 1.0);
   1.206 +    ctx.glVertex3fv(cube[5]);
   1.207 +    ctx.glVertex3fv(cube[4]);
   1.208 +    ctx.glVertex3fv(cube[7]);
   1.209 +    ctx.glVertex3fv(cube[6]);
   1.210  
   1.211 -    glColor3f(1.0, 1.0, 0.0);
   1.212 -    glVertex3fv(cube[5]);
   1.213 -    glVertex3fv(cube[0]);
   1.214 -    glVertex3fv(cube[3]);
   1.215 -    glVertex3fv(cube[4]);
   1.216 +    ctx.glColor3f(1.0, 1.0, 0.0);
   1.217 +    ctx.glVertex3fv(cube[5]);
   1.218 +    ctx.glVertex3fv(cube[0]);
   1.219 +    ctx.glVertex3fv(cube[3]);
   1.220 +    ctx.glVertex3fv(cube[4]);
   1.221  
   1.222 -    glColor3f(1.0, 0.0, 1.0);
   1.223 -    glVertex3fv(cube[6]);
   1.224 -    glVertex3fv(cube[1]);
   1.225 -    glVertex3fv(cube[2]);
   1.226 -    glVertex3fv(cube[7]);
   1.227 +    ctx.glColor3f(1.0, 0.0, 1.0);
   1.228 +    ctx.glVertex3fv(cube[6]);
   1.229 +    ctx.glVertex3fv(cube[1]);
   1.230 +    ctx.glVertex3fv(cube[2]);
   1.231 +    ctx.glVertex3fv(cube[7]);
   1.232  #endif /* SHADED_CUBE */
   1.233  
   1.234 -    glEnd();
   1.235 +    ctx.glEnd();
   1.236  
   1.237 -    glMatrixMode(GL_MODELVIEW);
   1.238 -    glRotatef(5.0, 1.0, 1.0, 1.0);
   1.239 +    ctx.glMatrixMode(GL_MODELVIEW);
   1.240 +    ctx.glRotatef(5.0, 1.0, 1.0, 1.0);
   1.241  }
   1.242  
   1.243  int
   1.244 @@ -242,6 +279,13 @@
   1.245          SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
   1.246          quit(2);
   1.247      }
   1.248 +    
   1.249 +    /* Important: call this *after* creating the context */
   1.250 +    if (LoadContext(&ctx) < 0) {
   1.251 +        SDL_Log("Could not load GL functions\n");
   1.252 +        quit(2);
   1.253 +        return 0;
   1.254 +    }
   1.255  
   1.256      if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
   1.257          /* try late-swap-tearing first. If not supported, try normal vsync. */
   1.258 @@ -260,10 +304,10 @@
   1.259      SDL_GL_GetDrawableSize(state->windows[0], &dw, &dh);
   1.260      SDL_Log("Draw Size     : %d,%d\n", dw, dh);
   1.261      SDL_Log("\n");
   1.262 -    SDL_Log("Vendor        : %s\n", glGetString(GL_VENDOR));
   1.263 -    SDL_Log("Renderer      : %s\n", glGetString(GL_RENDERER));
   1.264 -    SDL_Log("Version       : %s\n", glGetString(GL_VERSION));
   1.265 -    SDL_Log("Extensions    : %s\n", glGetString(GL_EXTENSIONS));
   1.266 +    SDL_Log("Vendor        : %s\n", ctx.glGetString(GL_VENDOR));
   1.267 +    SDL_Log("Renderer      : %s\n", ctx.glGetString(GL_RENDERER));
   1.268 +    SDL_Log("Version       : %s\n", ctx.glGetString(GL_VERSION));
   1.269 +    SDL_Log("Extensions    : %s\n", ctx.glGetString(GL_EXTENSIONS));
   1.270      SDL_Log("\n");
   1.271  
   1.272      status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
   1.273 @@ -319,14 +363,14 @@
   1.274      }
   1.275  
   1.276      /* Set rendering settings */
   1.277 -    glMatrixMode(GL_PROJECTION);
   1.278 -    glLoadIdentity();
   1.279 -    glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
   1.280 -    glMatrixMode(GL_MODELVIEW);
   1.281 -    glLoadIdentity();
   1.282 -    glEnable(GL_DEPTH_TEST);
   1.283 -    glDepthFunc(GL_LESS);
   1.284 -    glShadeModel(GL_SMOOTH);
   1.285 +    ctx.glMatrixMode(GL_PROJECTION);
   1.286 +    ctx.glLoadIdentity();
   1.287 +    ctx.glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
   1.288 +    ctx.glMatrixMode(GL_MODELVIEW);
   1.289 +    ctx.glLoadIdentity();
   1.290 +    ctx.glEnable(GL_DEPTH_TEST);
   1.291 +    ctx.glDepthFunc(GL_LESS);
   1.292 +    ctx.glShadeModel(GL_SMOOTH);
   1.293      
   1.294      /* Main render loop */
   1.295      frames = 0;
   1.296 @@ -344,7 +388,7 @@
   1.297                  continue;
   1.298              SDL_GL_MakeCurrent(state->windows[i], context);
   1.299              SDL_GL_GetDrawableSize(state->windows[i], &w, &h);
   1.300 -            glViewport(0, 0, w, h);
   1.301 +            ctx.glViewport(0, 0, w, h);
   1.302              Render();
   1.303              SDL_GL_SwapWindow(state->windows[i]);
   1.304          }