test/testdyngles.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 19 Jan 2011 22:20:44 -0800
changeset 5046 4cb778067834
parent 3410 f4bc25567731
child 5150 1435f8a6425c
permissions -rw-r--r--
Clearing the API changes for the 1.3.0 release
     1 /*
     2  * Small SDL example to demonstrate dynamically loading 
     3  * OpenGL lib and functions
     4  *
     5  * (FYI it was supposed to look like snow in the wind or something...)
     6  *
     7  * Compile with :
     8  * gcc testdyngl.c `sdl-config --libs --cflags` -o testdyngl -DHAVE_OPENGL
     9  *
    10  * You can specify a different OpenGL lib on the command line, i.e. :
    11  * ./testdyngl  /usr/X11R6/lib/libGL.so.1.2
    12  * or
    13  * ./testdyngl  /usr/lib/libGL.so.1.0.4496
    14  *
    15  */
    16 
    17 #include <stdio.h>
    18 #include <stdlib.h>
    19 
    20 #include "SDL.h"
    21 
    22 #ifdef __IPHONEOS__
    23 #define HAVE_OPENGLES
    24 #endif
    25 
    26 #ifdef HAVE_OPENGLES
    27 
    28 #include "SDL_opengles.h"
    29 
    30 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    31 static void
    32 quit(int rc)
    33 {
    34     SDL_Quit();
    35     exit(rc);
    36 }
    37 
    38 void *
    39 get_funcaddr(const char *p)
    40 {
    41     void *f = SDL_GL_GetProcAddress(p);
    42     if (f) {
    43         return f;
    44     } else {
    45         printf("Unable to get function pointer for %s\n", p);
    46         quit(1);
    47     }
    48     return NULL;
    49 }
    50 
    51 typedef struct
    52 {
    53     void (APIENTRY * glEnableClientState) (GLenum array);
    54     void (APIENTRY * glDisableClientState) (GLenum array);
    55     void (APIENTRY * glVertexPointer) (GLint size, GLenum type,
    56                                        GLsizei stride,
    57                                        const GLvoid * pointer);
    58     void (APIENTRY * glDrawArrays) (GLenum mode, GLint first, GLsizei count);
    59 
    60 
    61     void (APIENTRY * glClearColor) (GLfloat, GLfloat, GLfloat, GLfloat);
    62     void (APIENTRY * glClear) (GLbitfield);
    63     void (APIENTRY * glDisable) (GLenum);
    64     void (APIENTRY * glEnable) (GLenum);
    65     void (APIENTRY * glColor4f) (GLfloat, GLfloat, GLfloat, GLfloat);
    66     void (APIENTRY * glPointSize) (GLfloat);
    67     void (APIENTRY * glHint) (GLenum, GLenum);
    68     void (APIENTRY * glBlendFunc) (GLenum, GLenum);
    69     void (APIENTRY * glMatrixMode) (GLenum);
    70     void (APIENTRY * glLoadIdentity) ();
    71     void (APIENTRY * glOrthof) (GLfloat, GLfloat, GLfloat, GLfloat,
    72                                 GLfloat, GLfloat);
    73     void (APIENTRY * glRotatef) (GLfloat, GLfloat, GLfloat, GLfloat);
    74     void (APIENTRY * glViewport) (GLint, GLint, GLsizei, GLsizei);
    75     void (APIENTRY * glFogf) (GLenum, GLfloat);
    76 }
    77 glfuncs;
    78 
    79 void
    80 init_glfuncs(glfuncs * f)
    81 {
    82     f->glEnableClientState = get_funcaddr("glEnableClientState");
    83     f->glDisableClientState = get_funcaddr("glDisableClientState");
    84     f->glVertexPointer = get_funcaddr("glVertexPointer");
    85     f->glDrawArrays = get_funcaddr("glDrawArrays");
    86     f->glClearColor = get_funcaddr("glClearColor");
    87     f->glClear = get_funcaddr("glClear");
    88     f->glDisable = get_funcaddr("glDisable");
    89     f->glEnable = get_funcaddr("glEnable");
    90     f->glColor4f = get_funcaddr("glColor4f");
    91     f->glPointSize = get_funcaddr("glPointSize");
    92     f->glHint = get_funcaddr("glHint");
    93     f->glBlendFunc = get_funcaddr("glBlendFunc");
    94     f->glMatrixMode = get_funcaddr("glMatrixMode");
    95     f->glLoadIdentity = get_funcaddr("glLoadIdentity");
    96     f->glOrthof = get_funcaddr("glOrthof");
    97     f->glRotatef = get_funcaddr("glRotatef");
    98     f->glViewport = get_funcaddr("glViewport");
    99     f->glFogf = get_funcaddr("glFogf");
   100 }
   101 
   102 #define NB_PIXELS 1000
   103 
   104 int
   105 main(int argc, char *argv[])
   106 {
   107     glfuncs f;
   108     int i;
   109     SDL_Event event;
   110     int done = 0;
   111     GLfloat pixels[NB_PIXELS * 3];
   112     const char *gl_library = NULL;      /* Use the default GL library */
   113 
   114     int video_w, video_h;
   115 
   116     /* you may want to change these according to the platform */
   117     video_w = 320;
   118     video_h = 480;
   119 #ifdef __QNXNTO__
   120     video_w = 480;
   121     video_h = 640;
   122 #endif /* __QNXNTO__ */
   123 
   124     if (argv[1]) {
   125         gl_library = argv[1];
   126     }
   127 
   128     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   129         printf("Unable to init SDL : %s\n", SDL_GetError());
   130         return (1);
   131     }
   132 
   133     if (SDL_GL_LoadLibrary(gl_library) < 0) {
   134         printf("Unable to dynamically open GL ES lib : %s\n", SDL_GetError());
   135         quit(1);
   136     }
   137 
   138     if (SDL_SetVideoMode(video_h, video_w, 0, SDL_OPENGL) == NULL) {
   139         printf("Unable to open video mode : %s\n", SDL_GetError());
   140         quit(1);
   141     }
   142 
   143     /* Set the window manager title bar */
   144     SDL_WM_SetCaption("SDL Dynamic OpenGL ES Loading Test", "testdyngles");
   145 
   146     init_glfuncs(&f);
   147 
   148     for (i = 0; i < NB_PIXELS; i++) {
   149         pixels[3 * i] = rand() % 250 - 125;
   150         pixels[3 * i + 1] = rand() % 250 - 125;
   151         pixels[3 * i + 2] = rand() % 250 - 125;
   152     }
   153 
   154 #ifdef __QNXNTO__
   155     f.glViewport(0, 0, video_h, video_w);
   156 #else
   157     f.glViewport(0, 0, video_w, video_h);
   158 #endif /* __QNXNTO__ */
   159 
   160     f.glMatrixMode(GL_PROJECTION);
   161     f.glLoadIdentity();
   162     f.glOrthof(-100, 100, -100, 100, -500, 500);
   163 
   164     f.glMatrixMode(GL_MODELVIEW);
   165     f.glLoadIdentity();
   166 
   167     f.glEnable(GL_DEPTH_TEST);
   168     f.glDisable(GL_TEXTURE_2D);
   169     f.glEnable(GL_BLEND);
   170     f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   171 
   172     f.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   173     f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   174 
   175     f.glEnable(GL_POINT_SMOOTH);
   176     f.glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
   177     f.glPointSize(1.0f);
   178     f.glEnable(GL_FOG);
   179     f.glFogf(GL_FOG_START, -500);
   180     f.glFogf(GL_FOG_END, 500);
   181     f.glFogf(GL_FOG_DENSITY, 0.005);
   182 
   183     f.glVertexPointer(3, GL_FLOAT, 0, pixels);
   184     f.glEnableClientState(GL_VERTEX_ARRAY);
   185 
   186     do {
   187         f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   188 
   189         f.glRotatef(2.0, 1.0, 1.0, 1.0);
   190         f.glRotatef(1.0, 0.0, 1.0, 1.0);
   191 
   192         f.glColor4f(1.0, 1.0, 1.0, 1.0);
   193 
   194         f.glDrawArrays(GL_POINTS, 0, NB_PIXELS);
   195 
   196         SDL_GL_SwapBuffers();
   197 
   198         while (SDL_PollEvent(&event)) {
   199             if (event.type == SDL_QUIT)
   200                 done = 1;
   201             if (event.type == SDL_KEYDOWN)
   202                 done = 1;
   203         }
   204 
   205         SDL_Delay(20);
   206     }
   207     while (!done);
   208 
   209     f.glDisableClientState(GL_VERTEX_ARRAY);
   210 
   211     SDL_Quit();
   212     return 0;
   213 }
   214 
   215 #else /* HAVE_OPENGLES */
   216 
   217 int
   218 main(int argc, char *argv[])
   219 {
   220     printf("No OpenGL ES support on this system\n");
   221     return 1;
   222 }
   223 
   224 #endif /* HAVE_OPENGLES */