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
slouken@839
     1
/*
slouken@839
     2
 * Small SDL example to demonstrate dynamically loading 
slouken@839
     3
 * OpenGL lib and functions
slouken@839
     4
 *
slouken@839
     5
 * (FYI it was supposed to look like snow in the wind or something...)
slouken@839
     6
 *
slouken@839
     7
 * Compile with :
slouken@847
     8
 * gcc testdyngl.c `sdl-config --libs --cflags` -o testdyngl -DHAVE_OPENGL
slouken@839
     9
 *
slouken@839
    10
 * You can specify a different OpenGL lib on the command line, i.e. :
slouken@839
    11
 * ./testdyngl  /usr/X11R6/lib/libGL.so.1.2
slouken@839
    12
 * or
slouken@839
    13
 * ./testdyngl  /usr/lib/libGL.so.1.0.4496
slouken@839
    14
 *
slouken@839
    15
 */
slouken@839
    16
slouken@839
    17
#include <stdio.h>
slouken@839
    18
#include <stdlib.h>
slouken@839
    19
slouken@839
    20
#include "SDL.h"
slouken@839
    21
slouken@1815
    22
#ifdef __MACOS__
slouken@1815
    23
#define HAVE_OPENGL
slouken@1815
    24
#endif
slouken@1815
    25
slouken@839
    26
#ifdef HAVE_OPENGL
slouken@839
    27
slouken@839
    28
#include "SDL_opengl.h"
slouken@839
    29
icculus@1151
    30
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1895
    31
static void
slouken@1895
    32
quit(int rc)
icculus@1151
    33
{
slouken@1895
    34
    SDL_Quit();
slouken@1895
    35
    exit(rc);
icculus@1151
    36
}
icculus@1151
    37
slouken@1895
    38
void *
slouken@1895
    39
get_funcaddr(const char *p)
slouken@839
    40
{
slouken@1895
    41
    void *f = SDL_GL_GetProcAddress(p);
slouken@1895
    42
    if (f) {
slouken@1895
    43
        return f;
slouken@1895
    44
    } else {
slouken@1895
    45
        printf("Unable to get function pointer for %s\n", p);
slouken@1895
    46
        quit(1);
slouken@1895
    47
    }
slouken@1895
    48
    return NULL;
slouken@839
    49
}
slouken@839
    50
slouken@839
    51
typedef struct
slouken@839
    52
{
slouken@1895
    53
    void (APIENTRY * glBegin) (GLenum);
slouken@1895
    54
    void (APIENTRY * glEnd) ();
slouken@1895
    55
    void (APIENTRY * glVertex3f) (GLfloat, GLfloat, GLfloat);
slouken@1895
    56
    void (APIENTRY * glClearColor) (GLfloat, GLfloat, GLfloat, GLfloat);
slouken@1895
    57
    void (APIENTRY * glClear) (GLbitfield);
slouken@1895
    58
    void (APIENTRY * glDisable) (GLenum);
slouken@1895
    59
    void (APIENTRY * glEnable) (GLenum);
slouken@1895
    60
    void (APIENTRY * glColor4ub) (GLubyte, GLubyte, GLubyte, GLubyte);
slouken@1895
    61
    void (APIENTRY * glPointSize) (GLfloat);
slouken@1895
    62
    void (APIENTRY * glHint) (GLenum, GLenum);
slouken@1895
    63
    void (APIENTRY * glBlendFunc) (GLenum, GLenum);
slouken@1895
    64
    void (APIENTRY * glMatrixMode) (GLenum);
slouken@1895
    65
    void (APIENTRY * glLoadIdentity) ();
slouken@1895
    66
    void (APIENTRY * glOrtho) (GLdouble, GLdouble, GLdouble, GLdouble,
slouken@1895
    67
                               GLdouble, GLdouble);
slouken@1895
    68
    void (APIENTRY * glRotatef) (GLfloat, GLfloat, GLfloat, GLfloat);
slouken@1895
    69
    void (APIENTRY * glViewport) (GLint, GLint, GLsizei, GLsizei);
slouken@1895
    70
    void (APIENTRY * glFogf) (GLenum, GLfloat);
slouken@3323
    71
    const GLubyte* (APIENTRY * glGetString) (GLenum);
slouken@839
    72
}
slouken@839
    73
glfuncs;
slouken@839
    74
slouken@1895
    75
void
slouken@1895
    76
init_glfuncs(glfuncs * f)
slouken@839
    77
{
slouken@1895
    78
    f->glBegin = get_funcaddr("glBegin");
slouken@1895
    79
    f->glEnd = get_funcaddr("glEnd");
slouken@1895
    80
    f->glVertex3f = get_funcaddr("glVertex3f");
slouken@1895
    81
    f->glClearColor = get_funcaddr("glClearColor");
slouken@1895
    82
    f->glClear = get_funcaddr("glClear");
slouken@1895
    83
    f->glDisable = get_funcaddr("glDisable");
slouken@1895
    84
    f->glEnable = get_funcaddr("glEnable");
slouken@1895
    85
    f->glColor4ub = get_funcaddr("glColor4ub");
slouken@1895
    86
    f->glPointSize = get_funcaddr("glPointSize");
slouken@1895
    87
    f->glHint = get_funcaddr("glHint");
slouken@1895
    88
    f->glBlendFunc = get_funcaddr("glBlendFunc");
slouken@1895
    89
    f->glMatrixMode = get_funcaddr("glMatrixMode");
slouken@1895
    90
    f->glLoadIdentity = get_funcaddr("glLoadIdentity");
slouken@1895
    91
    f->glOrtho = get_funcaddr("glOrtho");
slouken@1895
    92
    f->glRotatef = get_funcaddr("glRotatef");
slouken@1895
    93
    f->glViewport = get_funcaddr("glViewport");
slouken@1895
    94
    f->glFogf = get_funcaddr("glFogf");
slouken@3323
    95
    f->glGetString = get_funcaddr("glGetString");
slouken@3323
    96
slouken@3323
    97
    printf("Vendor     : %s\n", f->glGetString(GL_VENDOR));
slouken@3323
    98
    printf("Renderer   : %s\n", f->glGetString(GL_RENDERER));
slouken@3323
    99
    printf("Version    : %s\n", f->glGetString(GL_VERSION));
slouken@3323
   100
    printf("Extensions : %s\n", f->glGetString(GL_EXTENSIONS));
slouken@839
   101
}
slouken@839
   102
slouken@839
   103
#define NB_PIXELS 1000
slouken@839
   104
slouken@1895
   105
int
slouken@1895
   106
main(int argc, char *argv[])
slouken@839
   107
{
slouken@1895
   108
    glfuncs f;
slouken@1895
   109
    int i;
slouken@1895
   110
    SDL_Event event;
slouken@1895
   111
    int done = 0;
slouken@1895
   112
    GLfloat pixels[NB_PIXELS * 3];
slouken@1895
   113
    const char *gl_library = NULL;      /* Use the default GL library */
slouken@1402
   114
slouken@1895
   115
    if (argv[1]) {
slouken@1895
   116
        gl_library = argv[1];
slouken@1895
   117
    }
patmandin@990
   118
slouken@1895
   119
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@1895
   120
        printf("Unable to init SDL : %s\n", SDL_GetError());
slouken@1895
   121
        return (1);
slouken@1895
   122
    }
slouken@839
   123
slouken@1895
   124
    if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) < 0) {
slouken@1895
   125
        printf("Unable to set GL attribute : %s\n", SDL_GetError());
slouken@1895
   126
        quit(1);
slouken@1895
   127
    }
slouken@839
   128
slouken@1895
   129
    if (SDL_GL_LoadLibrary(gl_library) < 0) {
slouken@1895
   130
        printf("Unable to dynamically open GL lib : %s\n", SDL_GetError());
slouken@1895
   131
        quit(1);
slouken@1895
   132
    }
slouken@910
   133
slouken@1895
   134
    if (SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL) {
slouken@1895
   135
        printf("Unable to open video mode : %s\n", SDL_GetError());
slouken@1895
   136
        quit(1);
slouken@1895
   137
    }
slouken@839
   138
slouken@1895
   139
    /* Set the window manager title bar */
slouken@1895
   140
    SDL_WM_SetCaption("SDL Dynamic OpenGL Loading Test", "testdyngl");
slouken@839
   141
slouken@1895
   142
    init_glfuncs(&f);
slouken@839
   143
slouken@1895
   144
    for (i = 0; i < NB_PIXELS; i++) {
slouken@1895
   145
        pixels[3 * i] = rand() % 250 - 125;
slouken@1895
   146
        pixels[3 * i + 1] = rand() % 250 - 125;
slouken@1895
   147
        pixels[3 * i + 2] = rand() % 250 - 125;
slouken@1895
   148
    }
slouken@1895
   149
slouken@1895
   150
    f.glViewport(0, 0, 640, 480);
slouken@1895
   151
slouken@1895
   152
    f.glMatrixMode(GL_PROJECTION);
slouken@1895
   153
    f.glLoadIdentity();
slouken@1895
   154
    f.glOrtho(-100, 100, -100, 100, -500, 500);
slouken@1895
   155
slouken@1895
   156
    f.glMatrixMode(GL_MODELVIEW);
slouken@1895
   157
    f.glLoadIdentity();
slouken@1895
   158
slouken@1895
   159
    f.glEnable(GL_DEPTH_TEST);
slouken@1895
   160
    f.glDisable(GL_TEXTURE_2D);
slouken@1895
   161
    f.glEnable(GL_BLEND);
slouken@1895
   162
    f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@1895
   163
slouken@1895
   164
    f.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
slouken@1895
   165
    f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
slouken@1895
   166
slouken@1895
   167
    f.glEnable(GL_POINT_SMOOTH);
slouken@1895
   168
    f.glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
slouken@1895
   169
    f.glPointSize(5.0f);
slouken@1895
   170
    f.glEnable(GL_FOG);
slouken@1895
   171
    f.glFogf(GL_FOG_START, -500);
slouken@1895
   172
    f.glFogf(GL_FOG_END, 500);
slouken@1895
   173
    f.glFogf(GL_FOG_DENSITY, 0.005);
slouken@1895
   174
slouken@1895
   175
    do {
slouken@1895
   176
        f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
slouken@1895
   177
slouken@1895
   178
        f.glRotatef(2.0, 1.0, 1.0, 1.0);
slouken@1895
   179
        f.glRotatef(1.0, 0.0, 1.0, 1.0);
slouken@1895
   180
slouken@1895
   181
        f.glColor4ub(255, 255, 255, 255);
slouken@1895
   182
        f.glBegin(GL_POINTS);
slouken@1895
   183
        for (i = 0; i < NB_PIXELS; i++) {
slouken@1895
   184
            f.glVertex3f(pixels[3 * i], pixels[3 * i + 1], pixels[3 * i + 2]);
slouken@1895
   185
        }
slouken@1895
   186
        f.glEnd();
slouken@1895
   187
        SDL_GL_SwapBuffers();
slouken@1895
   188
slouken@1895
   189
        while (SDL_PollEvent(&event)) {
bob@2327
   190
            if (event.type == SDL_KEYDOWN)
slouken@1895
   191
                done = 1;
slouken@1895
   192
        }
slouken@1895
   193
slouken@1895
   194
        SDL_Delay(20);
slouken@2735
   195
    } while (!done);
slouken@1895
   196
slouken@1895
   197
    SDL_Quit();
slouken@1895
   198
    return 0;
slouken@839
   199
}
slouken@839
   200
slouken@839
   201
#else /* HAVE_OPENGL */
slouken@839
   202
slouken@1895
   203
int
slouken@1895
   204
main(int argc, char *argv[])
slouken@839
   205
{
slouken@1895
   206
    printf("No OpenGL support on this system\n");
slouken@1895
   207
    return 1;
slouken@839
   208
}
slouken@839
   209
slouken@839
   210
#endif /* HAVE_OPENGL */