test/testdyngl.c
author Holmes Futrell <hfutrell@umail.ucsb.edu>
Fri, 18 Jul 2008 19:23:13 +0000
branchgsoc2008_iphone
changeset 2377 e9a0bad0ea07
parent 2327 7b53a8401195
child 2669 e27bdcc80744
permissions -rw-r--r--
Just renamed it...
     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 __MACOS__
    23 #define HAVE_OPENGL
    24 #endif
    25 
    26 #ifdef HAVE_OPENGL
    27 
    28 #include "SDL_opengl.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     void (APIENTRY * glClearColor) (GLfloat, GLfloat, GLfloat, GLfloat);
    57     void (APIENTRY * glClear) (GLbitfield);
    58     void (APIENTRY * glDisable) (GLenum);
    59     void (APIENTRY * glEnable) (GLenum);
    60     void (APIENTRY * glColor4ub) (GLubyte, GLubyte, GLubyte, GLubyte);
    61     void (APIENTRY * glPointSize) (GLfloat);
    62     void (APIENTRY * glHint) (GLenum, GLenum);
    63     void (APIENTRY * glBlendFunc) (GLenum, GLenum);
    64     void (APIENTRY * glMatrixMode) (GLenum);
    65     void (APIENTRY * glLoadIdentity) ();
    66     void (APIENTRY * glOrtho) (GLdouble, GLdouble, GLdouble, GLdouble,
    67                                GLdouble, GLdouble);
    68     void (APIENTRY * glRotatef) (GLfloat, GLfloat, GLfloat, GLfloat);
    69     void (APIENTRY * glViewport) (GLint, GLint, GLsizei, GLsizei);
    70     void (APIENTRY * glFogf) (GLenum, GLfloat);
    71 }
    72 glfuncs;
    73 
    74 void
    75 init_glfuncs(glfuncs * f)
    76 {
    77     f->glBegin = get_funcaddr("glBegin");
    78     f->glEnd = get_funcaddr("glEnd");
    79     f->glVertex3f = get_funcaddr("glVertex3f");
    80     f->glClearColor = get_funcaddr("glClearColor");
    81     f->glClear = get_funcaddr("glClear");
    82     f->glDisable = get_funcaddr("glDisable");
    83     f->glEnable = get_funcaddr("glEnable");
    84     f->glColor4ub = get_funcaddr("glColor4ub");
    85     f->glPointSize = get_funcaddr("glPointSize");
    86     f->glHint = get_funcaddr("glHint");
    87     f->glBlendFunc = get_funcaddr("glBlendFunc");
    88     f->glMatrixMode = get_funcaddr("glMatrixMode");
    89     f->glLoadIdentity = get_funcaddr("glLoadIdentity");
    90     f->glOrtho = get_funcaddr("glOrtho");
    91     f->glRotatef = get_funcaddr("glRotatef");
    92     f->glViewport = get_funcaddr("glViewport");
    93     f->glFogf = get_funcaddr("glFogf");
    94 }
    95 
    96 #define NB_PIXELS 1000
    97 
    98 int
    99 main(int argc, char *argv[])
   100 {
   101     glfuncs f;
   102     int i;
   103     SDL_Event event;
   104     int done = 0;
   105     GLfloat pixels[NB_PIXELS * 3];
   106     const char *gl_library = NULL;      /* Use the default GL library */
   107 
   108     if (argv[1]) {
   109         gl_library = argv[1];
   110     }
   111 
   112     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   113         printf("Unable to init SDL : %s\n", SDL_GetError());
   114         return (1);
   115     }
   116 
   117     if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) < 0) {
   118         printf("Unable to set GL attribute : %s\n", SDL_GetError());
   119         quit(1);
   120     }
   121 
   122     if (SDL_GL_LoadLibrary(gl_library) < 0) {
   123         printf("Unable to dynamically open GL lib : %s\n", SDL_GetError());
   124         quit(1);
   125     }
   126 
   127     if (SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL) {
   128         printf("Unable to open video mode : %s\n", SDL_GetError());
   129         quit(1);
   130     }
   131 
   132     /* Set the window manager title bar */
   133     SDL_WM_SetCaption("SDL Dynamic OpenGL Loading Test", "testdyngl");
   134 
   135     init_glfuncs(&f);
   136 
   137     for (i = 0; i < NB_PIXELS; i++) {
   138         pixels[3 * i] = rand() % 250 - 125;
   139         pixels[3 * i + 1] = rand() % 250 - 125;
   140         pixels[3 * i + 2] = rand() % 250 - 125;
   141     }
   142 
   143     f.glViewport(0, 0, 640, 480);
   144 
   145     f.glMatrixMode(GL_PROJECTION);
   146     f.glLoadIdentity();
   147     f.glOrtho(-100, 100, -100, 100, -500, 500);
   148 
   149     f.glMatrixMode(GL_MODELVIEW);
   150     f.glLoadIdentity();
   151 
   152     f.glEnable(GL_DEPTH_TEST);
   153     f.glDisable(GL_TEXTURE_2D);
   154     f.glEnable(GL_BLEND);
   155     f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   156 
   157     f.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   158     f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   159 
   160     f.glEnable(GL_POINT_SMOOTH);
   161     f.glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
   162     f.glPointSize(5.0f);
   163     f.glEnable(GL_FOG);
   164     f.glFogf(GL_FOG_START, -500);
   165     f.glFogf(GL_FOG_END, 500);
   166     f.glFogf(GL_FOG_DENSITY, 0.005);
   167 
   168     do {
   169         f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   170 
   171         f.glRotatef(2.0, 1.0, 1.0, 1.0);
   172         f.glRotatef(1.0, 0.0, 1.0, 1.0);
   173 
   174         f.glColor4ub(255, 255, 255, 255);
   175         f.glBegin(GL_POINTS);
   176         for (i = 0; i < NB_PIXELS; i++) {
   177             f.glVertex3f(pixels[3 * i], pixels[3 * i + 1], pixels[3 * i + 2]);
   178         }
   179         f.glEnd();
   180         SDL_GL_SwapBuffers();
   181 
   182         while (SDL_PollEvent(&event)) {
   183             if (event.type == SDL_KEYDOWN)
   184                 done = 1;
   185         }
   186 
   187         SDL_Delay(20);
   188     }
   189     while (!done);
   190 
   191     SDL_Quit();
   192     return 0;
   193 }
   194 
   195 #else /* HAVE_OPENGL */
   196 
   197 int
   198 main(int argc, char *argv[])
   199 {
   200     printf("No OpenGL support on this system\n");
   201     return 1;
   202 }
   203 
   204 #endif /* HAVE_OPENGL */