test/testdyngl.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 3323 8d0889509afb
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 /*
    13  * Small SDL example to demonstrate dynamically loading 
    14  * OpenGL lib and functions
    15  *
    16  * (FYI it was supposed to look like snow in the wind or something...)
    17  *
    18  * Compile with :
    19  * gcc testdyngl.c `sdl-config --libs --cflags` -o testdyngl -DHAVE_OPENGL
    20  *
    21  * You can specify a different OpenGL lib on the command line, i.e. :
    22  * ./testdyngl  /usr/X11R6/lib/libGL.so.1.2
    23  * or
    24  * ./testdyngl  /usr/lib/libGL.so.1.0.4496
    25  *
    26  */
    27 
    28 #include <stdio.h>
    29 #include <stdlib.h>
    30 
    31 #include "SDL.h"
    32 
    33 #ifdef __MACOS__
    34 #define HAVE_OPENGL
    35 #endif
    36 
    37 #ifdef HAVE_OPENGL
    38 
    39 #include "SDL_opengl.h"
    40 
    41 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    42 static void
    43 quit(int rc)
    44 {
    45     SDL_Quit();
    46     exit(rc);
    47 }
    48 
    49 void *
    50 get_funcaddr(const char *p)
    51 {
    52     void *f = SDL_GL_GetProcAddress(p);
    53     if (f) {
    54         return f;
    55     } else {
    56         printf("Unable to get function pointer for %s\n", p);
    57         quit(1);
    58     }
    59     return NULL;
    60 }
    61 
    62 typedef struct
    63 {
    64     void (APIENTRY * glBegin) (GLenum);
    65     void (APIENTRY * glEnd) ();
    66     void (APIENTRY * glVertex3f) (GLfloat, GLfloat, GLfloat);
    67     void (APIENTRY * glClearColor) (GLfloat, GLfloat, GLfloat, GLfloat);
    68     void (APIENTRY * glClear) (GLbitfield);
    69     void (APIENTRY * glDisable) (GLenum);
    70     void (APIENTRY * glEnable) (GLenum);
    71     void (APIENTRY * glColor4ub) (GLubyte, GLubyte, GLubyte, GLubyte);
    72     void (APIENTRY * glPointSize) (GLfloat);
    73     void (APIENTRY * glHint) (GLenum, GLenum);
    74     void (APIENTRY * glBlendFunc) (GLenum, GLenum);
    75     void (APIENTRY * glMatrixMode) (GLenum);
    76     void (APIENTRY * glLoadIdentity) ();
    77     void (APIENTRY * glOrtho) (GLdouble, GLdouble, GLdouble, GLdouble,
    78                                GLdouble, GLdouble);
    79     void (APIENTRY * glRotatef) (GLfloat, GLfloat, GLfloat, GLfloat);
    80     void (APIENTRY * glViewport) (GLint, GLint, GLsizei, GLsizei);
    81     void (APIENTRY * glFogf) (GLenum, GLfloat);
    82     const GLubyte* (APIENTRY * glGetString) (GLenum);
    83 }
    84 glfuncs;
    85 
    86 void
    87 init_glfuncs(glfuncs * f)
    88 {
    89     f->glBegin = get_funcaddr("glBegin");
    90     f->glEnd = get_funcaddr("glEnd");
    91     f->glVertex3f = get_funcaddr("glVertex3f");
    92     f->glClearColor = get_funcaddr("glClearColor");
    93     f->glClear = get_funcaddr("glClear");
    94     f->glDisable = get_funcaddr("glDisable");
    95     f->glEnable = get_funcaddr("glEnable");
    96     f->glColor4ub = get_funcaddr("glColor4ub");
    97     f->glPointSize = get_funcaddr("glPointSize");
    98     f->glHint = get_funcaddr("glHint");
    99     f->glBlendFunc = get_funcaddr("glBlendFunc");
   100     f->glMatrixMode = get_funcaddr("glMatrixMode");
   101     f->glLoadIdentity = get_funcaddr("glLoadIdentity");
   102     f->glOrtho = get_funcaddr("glOrtho");
   103     f->glRotatef = get_funcaddr("glRotatef");
   104     f->glViewport = get_funcaddr("glViewport");
   105     f->glFogf = get_funcaddr("glFogf");
   106     f->glGetString = get_funcaddr("glGetString");
   107 
   108     printf("Vendor     : %s\n", f->glGetString(GL_VENDOR));
   109     printf("Renderer   : %s\n", f->glGetString(GL_RENDERER));
   110     printf("Version    : %s\n", f->glGetString(GL_VERSION));
   111     printf("Extensions : %s\n", f->glGetString(GL_EXTENSIONS));
   112 }
   113 
   114 #define NB_PIXELS 1000
   115 
   116 int
   117 main(int argc, char *argv[])
   118 {
   119     glfuncs f;
   120     int i;
   121     SDL_Event event;
   122     int done = 0;
   123     GLfloat pixels[NB_PIXELS * 3];
   124     const char *gl_library = NULL;      /* Use the default GL library */
   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_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) < 0) {
   136         printf("Unable to set GL attribute : %s\n", SDL_GetError());
   137         quit(1);
   138     }
   139 
   140     if (SDL_GL_LoadLibrary(gl_library) < 0) {
   141         printf("Unable to dynamically open GL lib : %s\n", SDL_GetError());
   142         quit(1);
   143     }
   144 
   145     if (SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL) {
   146         printf("Unable to open video mode : %s\n", SDL_GetError());
   147         quit(1);
   148     }
   149 
   150     /* Set the window manager title bar */
   151     SDL_WM_SetCaption("SDL Dynamic OpenGL Loading Test", "testdyngl");
   152 
   153     init_glfuncs(&f);
   154 
   155     for (i = 0; i < NB_PIXELS; i++) {
   156         pixels[3 * i] = rand() % 250 - 125;
   157         pixels[3 * i + 1] = rand() % 250 - 125;
   158         pixels[3 * i + 2] = rand() % 250 - 125;
   159     }
   160 
   161     f.glViewport(0, 0, 640, 480);
   162 
   163     f.glMatrixMode(GL_PROJECTION);
   164     f.glLoadIdentity();
   165     f.glOrtho(-100, 100, -100, 100, -500, 500);
   166 
   167     f.glMatrixMode(GL_MODELVIEW);
   168     f.glLoadIdentity();
   169 
   170     f.glEnable(GL_DEPTH_TEST);
   171     f.glDisable(GL_TEXTURE_2D);
   172     f.glEnable(GL_BLEND);
   173     f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   174 
   175     f.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   176     f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   177 
   178     f.glEnable(GL_POINT_SMOOTH);
   179     f.glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
   180     f.glPointSize(5.0f);
   181     f.glEnable(GL_FOG);
   182     f.glFogf(GL_FOG_START, -500);
   183     f.glFogf(GL_FOG_END, 500);
   184     f.glFogf(GL_FOG_DENSITY, 0.005);
   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.glColor4ub(255, 255, 255, 255);
   193         f.glBegin(GL_POINTS);
   194         for (i = 0; i < NB_PIXELS; i++) {
   195             f.glVertex3f(pixels[3 * i], pixels[3 * i + 1], pixels[3 * i + 2]);
   196         }
   197         f.glEnd();
   198         SDL_GL_SwapBuffers();
   199 
   200         while (SDL_PollEvent(&event)) {
   201             if (event.type == SDL_KEYDOWN)
   202                 done = 1;
   203         }
   204 
   205         SDL_Delay(20);
   206     } while (!done);
   207 
   208     SDL_Quit();
   209     return 0;
   210 }
   211 
   212 #else /* HAVE_OPENGL */
   213 
   214 int
   215 main(int argc, char *argv[])
   216 {
   217     printf("No OpenGL support on this system\n");
   218     return 1;
   219 }
   220 
   221 #endif /* HAVE_OPENGL */