test/testdyngl.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 28 Sep 2009 15:32:19 +0000
changeset 3323 8d0889509afb
parent 2735 204be4fc2726
child 5535 96594ac5fd1a
permissions -rw-r--r--
Added renderer information for dynamic OpenGL test
     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     const GLubyte* (APIENTRY * glGetString) (GLenum);
    72 }
    73 glfuncs;
    74 
    75 void
    76 init_glfuncs(glfuncs * f)
    77 {
    78     f->glBegin = get_funcaddr("glBegin");
    79     f->glEnd = get_funcaddr("glEnd");
    80     f->glVertex3f = get_funcaddr("glVertex3f");
    81     f->glClearColor = get_funcaddr("glClearColor");
    82     f->glClear = get_funcaddr("glClear");
    83     f->glDisable = get_funcaddr("glDisable");
    84     f->glEnable = get_funcaddr("glEnable");
    85     f->glColor4ub = get_funcaddr("glColor4ub");
    86     f->glPointSize = get_funcaddr("glPointSize");
    87     f->glHint = get_funcaddr("glHint");
    88     f->glBlendFunc = get_funcaddr("glBlendFunc");
    89     f->glMatrixMode = get_funcaddr("glMatrixMode");
    90     f->glLoadIdentity = get_funcaddr("glLoadIdentity");
    91     f->glOrtho = get_funcaddr("glOrtho");
    92     f->glRotatef = get_funcaddr("glRotatef");
    93     f->glViewport = get_funcaddr("glViewport");
    94     f->glFogf = get_funcaddr("glFogf");
    95     f->glGetString = get_funcaddr("glGetString");
    96 
    97     printf("Vendor     : %s\n", f->glGetString(GL_VENDOR));
    98     printf("Renderer   : %s\n", f->glGetString(GL_RENDERER));
    99     printf("Version    : %s\n", f->glGetString(GL_VERSION));
   100     printf("Extensions : %s\n", f->glGetString(GL_EXTENSIONS));
   101 }
   102 
   103 #define NB_PIXELS 1000
   104 
   105 int
   106 main(int argc, char *argv[])
   107 {
   108     glfuncs f;
   109     int i;
   110     SDL_Event event;
   111     int done = 0;
   112     GLfloat pixels[NB_PIXELS * 3];
   113     const char *gl_library = NULL;      /* Use the default GL library */
   114 
   115     if (argv[1]) {
   116         gl_library = argv[1];
   117     }
   118 
   119     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   120         printf("Unable to init SDL : %s\n", SDL_GetError());
   121         return (1);
   122     }
   123 
   124     if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) < 0) {
   125         printf("Unable to set GL attribute : %s\n", SDL_GetError());
   126         quit(1);
   127     }
   128 
   129     if (SDL_GL_LoadLibrary(gl_library) < 0) {
   130         printf("Unable to dynamically open GL lib : %s\n", SDL_GetError());
   131         quit(1);
   132     }
   133 
   134     if (SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL) {
   135         printf("Unable to open video mode : %s\n", SDL_GetError());
   136         quit(1);
   137     }
   138 
   139     /* Set the window manager title bar */
   140     SDL_WM_SetCaption("SDL Dynamic OpenGL Loading Test", "testdyngl");
   141 
   142     init_glfuncs(&f);
   143 
   144     for (i = 0; i < NB_PIXELS; i++) {
   145         pixels[3 * i] = rand() % 250 - 125;
   146         pixels[3 * i + 1] = rand() % 250 - 125;
   147         pixels[3 * i + 2] = rand() % 250 - 125;
   148     }
   149 
   150     f.glViewport(0, 0, 640, 480);
   151 
   152     f.glMatrixMode(GL_PROJECTION);
   153     f.glLoadIdentity();
   154     f.glOrtho(-100, 100, -100, 100, -500, 500);
   155 
   156     f.glMatrixMode(GL_MODELVIEW);
   157     f.glLoadIdentity();
   158 
   159     f.glEnable(GL_DEPTH_TEST);
   160     f.glDisable(GL_TEXTURE_2D);
   161     f.glEnable(GL_BLEND);
   162     f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   163 
   164     f.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   165     f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   166 
   167     f.glEnable(GL_POINT_SMOOTH);
   168     f.glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
   169     f.glPointSize(5.0f);
   170     f.glEnable(GL_FOG);
   171     f.glFogf(GL_FOG_START, -500);
   172     f.glFogf(GL_FOG_END, 500);
   173     f.glFogf(GL_FOG_DENSITY, 0.005);
   174 
   175     do {
   176         f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   177 
   178         f.glRotatef(2.0, 1.0, 1.0, 1.0);
   179         f.glRotatef(1.0, 0.0, 1.0, 1.0);
   180 
   181         f.glColor4ub(255, 255, 255, 255);
   182         f.glBegin(GL_POINTS);
   183         for (i = 0; i < NB_PIXELS; i++) {
   184             f.glVertex3f(pixels[3 * i], pixels[3 * i + 1], pixels[3 * i + 2]);
   185         }
   186         f.glEnd();
   187         SDL_GL_SwapBuffers();
   188 
   189         while (SDL_PollEvent(&event)) {
   190             if (event.type == SDL_KEYDOWN)
   191                 done = 1;
   192         }
   193 
   194         SDL_Delay(20);
   195     } while (!done);
   196 
   197     SDL_Quit();
   198     return 0;
   199 }
   200 
   201 #else /* HAVE_OPENGL */
   202 
   203 int
   204 main(int argc, char *argv[])
   205 {
   206     printf("No OpenGL support on this system\n");
   207     return 1;
   208 }
   209 
   210 #endif /* HAVE_OPENGL */