test/testdyngles.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Aug 2009 18:39:57 +0000
changeset 3227 458e53d8662c
parent 3139 7f684f249ec9
child 3410 f4bc25567731
permissions -rw-r--r--
Clarified API documentation
     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 * glBegin) (GLenum);
    54     //void (APIENTRY * glEnd) ();
    55     //void (APIENTRY * glVertex3f) (GLfloat, GLfloat, GLfloat);
    56 
    57     void (APIENTRY * glEnableClientState) (GLenum array);
    58     void (APIENTRY * glVertexPointer) (GLint size, GLenum type,
    59                                        GLsizei stride,
    60                                        const GLvoid * pointer);
    61     void (APIENTRY * glDrawArrays) (GLenum mode, GLint first, GLsizei count);
    62 
    63 
    64     void (APIENTRY * glClearColor) (GLfloat, GLfloat, GLfloat, GLfloat);
    65     void (APIENTRY * glClear) (GLbitfield);
    66     void (APIENTRY * glDisable) (GLenum);
    67     void (APIENTRY * glEnable) (GLenum);
    68     void (APIENTRY * glColor4f) (GLfloat, GLfloat, GLfloat, GLfloat);
    69     void (APIENTRY * glPointSize) (GLfloat);
    70     void (APIENTRY * glHint) (GLenum, GLenum);
    71     void (APIENTRY * glBlendFunc) (GLenum, GLenum);
    72     void (APIENTRY * glMatrixMode) (GLenum);
    73     void (APIENTRY * glLoadIdentity) ();
    74     void (APIENTRY * glOrthof) (GLfloat, GLfloat, GLfloat, GLfloat,
    75                                 GLfloat, GLfloat);
    76     void (APIENTRY * glRotatef) (GLfloat, GLfloat, GLfloat, GLfloat);
    77     void (APIENTRY * glViewport) (GLint, GLint, GLsizei, GLsizei);
    78     void (APIENTRY * glFogf) (GLenum, GLfloat);
    79 }
    80 glfuncs;
    81 
    82 void
    83 init_glfuncs(glfuncs * f)
    84 {
    85     f->glEnableClientState = get_funcaddr("glEnableClientState");
    86     f->glVertexPointer = get_funcaddr("glVertexPointer");
    87     f->glDrawArrays = get_funcaddr("glDrawArrays");
    88     f->glClearColor = get_funcaddr("glClearColor");
    89     f->glClear = get_funcaddr("glClear");
    90     f->glDisable = get_funcaddr("glDisable");
    91     f->glEnable = get_funcaddr("glEnable");
    92     f->glColor4f = get_funcaddr("glColor4f");
    93     f->glPointSize = get_funcaddr("glPointSize");
    94     f->glHint = get_funcaddr("glHint");
    95     f->glBlendFunc = get_funcaddr("glBlendFunc");
    96     f->glMatrixMode = get_funcaddr("glMatrixMode");
    97     f->glLoadIdentity = get_funcaddr("glLoadIdentity");
    98     f->glOrthof = get_funcaddr("glOrthof");
    99     f->glRotatef = get_funcaddr("glRotatef");
   100     f->glViewport = get_funcaddr("glViewport");
   101     f->glFogf = get_funcaddr("glFogf");
   102 }
   103 
   104 #define NB_PIXELS 1000
   105 
   106 int
   107 main(int argc, char *argv[])
   108 {
   109     glfuncs f;
   110     int i;
   111     SDL_Event event;
   112     int done = 0;
   113     GLfloat pixels[NB_PIXELS * 3];
   114     const char *gl_library = NULL;      /* Use the default GL library */
   115 
   116     int video_w, video_h;
   117 
   118     /* you may want to change these according to the platform */
   119     video_w = 320;
   120     video_h = 480;
   121 #ifdef __QNXNTO__
   122     video_h = 640;
   123     video_w = 480;
   124 #endif /* __QNXNTO__ */
   125 
   126     if (argv[1]) {
   127         gl_library = argv[1];
   128     }
   129 
   130     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   131         printf("Unable to init SDL : %s\n", SDL_GetError());
   132         return (1);
   133     }
   134 
   135     if (SDL_GL_LoadLibrary(gl_library) < 0) {
   136         printf("Unable to dynamically open GL ES lib : %s\n", SDL_GetError());
   137         quit(1);
   138     }
   139 
   140     if (SDL_SetVideoMode(video_h, video_w, 0, SDL_OPENGL) == NULL) {
   141         printf("Unable to open video mode : %s\n", SDL_GetError());
   142         quit(1);
   143     }
   144 
   145     /* Set the window manager title bar */
   146     SDL_WM_SetCaption("SDL Dynamic OpenGL ES Loading Test", "testdyngles");
   147 
   148     init_glfuncs(&f);
   149 
   150     for (i = 0; i < NB_PIXELS; i++) {
   151         pixels[3 * i] = rand() % 250 - 125;
   152         pixels[3 * i + 1] = rand() % 250 - 125;
   153         pixels[3 * i + 2] = rand() % 250 - 125;
   154     }
   155 
   156 #ifdef __QNXNTO__
   157     f.glViewport(0, 0, video_h, video_w);
   158 #else
   159     f.glViewport(0, 0, video_w, video_h);
   160 #endif /* __QNXNTO__ */
   161 
   162     f.glMatrixMode(GL_PROJECTION);
   163     f.glLoadIdentity();
   164     f.glOrthof(-100, 100, -100, 100, -500, 500);
   165 
   166     f.glMatrixMode(GL_MODELVIEW);
   167     f.glLoadIdentity();
   168 
   169     f.glEnable(GL_DEPTH_TEST);
   170     f.glDisable(GL_TEXTURE_2D);
   171     f.glEnable(GL_BLEND);
   172     f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   173 
   174     f.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   175     f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   176 
   177     f.glEnable(GL_POINT_SMOOTH);
   178     f.glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
   179     f.glPointSize(5.0f);
   180     f.glEnable(GL_FOG);
   181     f.glFogf(GL_FOG_START, -500);
   182     f.glFogf(GL_FOG_END, 500);
   183     f.glFogf(GL_FOG_DENSITY, 0.005);
   184 
   185     do {
   186         f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   187 
   188         f.glRotatef(2.0, 1.0, 1.0, 1.0);
   189         f.glRotatef(1.0, 0.0, 1.0, 1.0);
   190 
   191         f.glColor4f(1.0, 1.0, 1.0, 1.0);
   192 
   193         f.glEnableClientState(GL_VERTEX_ARRAY);
   194         f.glVertexPointer(3, GL_FLOAT, 0, pixels);
   195         f.glDrawArrays(GL_POINTS, 0, NB_PIXELS);
   196 
   197         SDL_GL_SwapBuffers();
   198 
   199         while (SDL_PollEvent(&event)) {
   200             if (event.type == SDL_QUIT)
   201                 done = 1;
   202             if (event.type == SDL_KEYDOWN)
   203                 done = 1;
   204         }
   205 
   206         SDL_Delay(20);
   207     }
   208     while (!done);
   209 
   210     SDL_Quit();
   211     return 0;
   212 }
   213 
   214 #else /* HAVE_OPENGLES */
   215 
   216 int
   217 main(int argc, char *argv[])
   218 {
   219     printf("No OpenGL ES support on this system\n");
   220     return 1;
   221 }
   222 
   223 #endif /* HAVE_OPENGLES */