test/testdyngl.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1659 14717b52abc0
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
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@1659
    22
#ifdef __MACOS__
slouken@1659
    23
#define HAVE_OPENGL
slouken@1659
    24
#endif
slouken@1659
    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@1662
    31
static void
slouken@1662
    32
quit (int rc)
icculus@1151
    33
{
slouken@1662
    34
    SDL_Quit ();
slouken@1662
    35
    exit (rc);
icculus@1151
    36
}
icculus@1151
    37
slouken@1662
    38
void *
slouken@1662
    39
get_funcaddr (const char *p)
slouken@839
    40
{
slouken@1662
    41
    void *f = SDL_GL_GetProcAddress (p);
slouken@1662
    42
    if (f) {
slouken@1662
    43
        return f;
slouken@1662
    44
    } else {
slouken@1662
    45
        printf ("Unable to get function pointer for %s\n", p);
slouken@1662
    46
        quit (1);
slouken@1662
    47
    }
slouken@1662
    48
    return NULL;
slouken@839
    49
}
slouken@839
    50
slouken@839
    51
typedef struct
slouken@839
    52
{
slouken@1662
    53
    void (APIENTRY * glBegin) (GLenum);
slouken@1662
    54
    void (APIENTRY * glEnd) ();
slouken@1662
    55
    void (APIENTRY * glVertex3f) (GLfloat, GLfloat, GLfloat);
slouken@1662
    56
    void (APIENTRY * glClearColor) (GLfloat, GLfloat, GLfloat, GLfloat);
slouken@1662
    57
    void (APIENTRY * glClear) (GLbitfield);
slouken@1662
    58
    void (APIENTRY * glDisable) (GLenum);
slouken@1662
    59
    void (APIENTRY * glEnable) (GLenum);
slouken@1662
    60
    void (APIENTRY * glColor4ub) (GLubyte, GLubyte, GLubyte, GLubyte);
slouken@1662
    61
    void (APIENTRY * glPointSize) (GLfloat);
slouken@1662
    62
    void (APIENTRY * glHint) (GLenum, GLenum);
slouken@1662
    63
    void (APIENTRY * glBlendFunc) (GLenum, GLenum);
slouken@1662
    64
    void (APIENTRY * glMatrixMode) (GLenum);
slouken@1662
    65
    void (APIENTRY * glLoadIdentity) ();
slouken@1662
    66
    void (APIENTRY * glOrtho) (GLdouble, GLdouble, GLdouble, GLdouble,
slouken@1662
    67
                               GLdouble, GLdouble);
slouken@1662
    68
    void (APIENTRY * glRotatef) (GLfloat, GLfloat, GLfloat, GLfloat);
slouken@1662
    69
    void (APIENTRY * glViewport) (GLint, GLint, GLsizei, GLsizei);
slouken@1662
    70
    void (APIENTRY * glFogf) (GLenum, GLfloat);
slouken@839
    71
}
slouken@839
    72
glfuncs;
slouken@839
    73
slouken@1662
    74
void
slouken@1662
    75
init_glfuncs (glfuncs * f)
slouken@839
    76
{
slouken@1662
    77
    f->glBegin = get_funcaddr ("glBegin");
slouken@1662
    78
    f->glEnd = get_funcaddr ("glEnd");
slouken@1662
    79
    f->glVertex3f = get_funcaddr ("glVertex3f");
slouken@1662
    80
    f->glClearColor = get_funcaddr ("glClearColor");
slouken@1662
    81
    f->glClear = get_funcaddr ("glClear");
slouken@1662
    82
    f->glDisable = get_funcaddr ("glDisable");
slouken@1662
    83
    f->glEnable = get_funcaddr ("glEnable");
slouken@1662
    84
    f->glColor4ub = get_funcaddr ("glColor4ub");
slouken@1662
    85
    f->glPointSize = get_funcaddr ("glPointSize");
slouken@1662
    86
    f->glHint = get_funcaddr ("glHint");
slouken@1662
    87
    f->glBlendFunc = get_funcaddr ("glBlendFunc");
slouken@1662
    88
    f->glMatrixMode = get_funcaddr ("glMatrixMode");
slouken@1662
    89
    f->glLoadIdentity = get_funcaddr ("glLoadIdentity");
slouken@1662
    90
    f->glOrtho = get_funcaddr ("glOrtho");
slouken@1662
    91
    f->glRotatef = get_funcaddr ("glRotatef");
slouken@1662
    92
    f->glViewport = get_funcaddr ("glViewport");
slouken@1662
    93
    f->glFogf = get_funcaddr ("glFogf");
slouken@839
    94
}
slouken@839
    95
slouken@839
    96
#define NB_PIXELS 1000
slouken@839
    97
slouken@1662
    98
int
slouken@1662
    99
main (int argc, char *argv[])
slouken@839
   100
{
slouken@1662
   101
    glfuncs f;
slouken@1662
   102
    int i;
slouken@1662
   103
    SDL_Event event;
slouken@1662
   104
    int done = 0;
slouken@1662
   105
    GLfloat pixels[NB_PIXELS * 3];
slouken@1662
   106
    const char *gl_library = NULL;      /* Use the default GL library */
slouken@1402
   107
slouken@1662
   108
    if (argv[1]) {
slouken@1662
   109
        gl_library = argv[1];
slouken@1662
   110
    }
patmandin@990
   111
slouken@1662
   112
    if (SDL_Init (SDL_INIT_VIDEO) < 0) {
slouken@1662
   113
        printf ("Unable to init SDL : %s\n", SDL_GetError ());
slouken@1662
   114
        return (1);
slouken@1662
   115
    }
slouken@839
   116
slouken@1662
   117
    if (SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1) < 0) {
slouken@1662
   118
        printf ("Unable to set GL attribute : %s\n", SDL_GetError ());
slouken@1662
   119
        quit (1);
slouken@1662
   120
    }
slouken@839
   121
slouken@1662
   122
    if (SDL_GL_LoadLibrary (gl_library) < 0) {
slouken@1662
   123
        printf ("Unable to dynamically open GL lib : %s\n", SDL_GetError ());
slouken@1662
   124
        quit (1);
slouken@1662
   125
    }
slouken@910
   126
slouken@1662
   127
    if (SDL_SetVideoMode (640, 480, 0, SDL_OPENGL) == NULL) {
slouken@1662
   128
        printf ("Unable to open video mode : %s\n", SDL_GetError ());
slouken@1662
   129
        quit (1);
slouken@1662
   130
    }
slouken@839
   131
slouken@1662
   132
    /* Set the window manager title bar */
slouken@1662
   133
    SDL_WM_SetCaption ("SDL Dynamic OpenGL Loading Test", "testdyngl");
slouken@839
   134
slouken@1662
   135
    init_glfuncs (&f);
slouken@839
   136
slouken@1662
   137
    for (i = 0; i < NB_PIXELS; i++) {
slouken@1662
   138
        pixels[3 * i] = rand () % 250 - 125;
slouken@1662
   139
        pixels[3 * i + 1] = rand () % 250 - 125;
slouken@1662
   140
        pixels[3 * i + 2] = rand () % 250 - 125;
slouken@1662
   141
    }
slouken@1662
   142
slouken@1662
   143
    f.glViewport (0, 0, 640, 480);
slouken@1662
   144
slouken@1662
   145
    f.glMatrixMode (GL_PROJECTION);
slouken@1662
   146
    f.glLoadIdentity ();
slouken@1662
   147
    f.glOrtho (-100, 100, -100, 100, -500, 500);
slouken@1662
   148
slouken@1662
   149
    f.glMatrixMode (GL_MODELVIEW);
slouken@1662
   150
    f.glLoadIdentity ();
slouken@1662
   151
slouken@1662
   152
    f.glEnable (GL_DEPTH_TEST);
slouken@1662
   153
    f.glDisable (GL_TEXTURE_2D);
slouken@1662
   154
    f.glEnable (GL_BLEND);
slouken@1662
   155
    f.glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@1662
   156
slouken@1662
   157
    f.glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
slouken@1662
   158
    f.glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
slouken@1662
   159
slouken@1662
   160
    f.glEnable (GL_POINT_SMOOTH);
slouken@1662
   161
    f.glHint (GL_POINT_SMOOTH_HINT, GL_NICEST);
slouken@1662
   162
    f.glPointSize (5.0f);
slouken@1662
   163
    f.glEnable (GL_FOG);
slouken@1662
   164
    f.glFogf (GL_FOG_START, -500);
slouken@1662
   165
    f.glFogf (GL_FOG_END, 500);
slouken@1662
   166
    f.glFogf (GL_FOG_DENSITY, 0.005);
slouken@1662
   167
slouken@1662
   168
    do {
slouken@1662
   169
        f.glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
slouken@1662
   170
slouken@1662
   171
        f.glRotatef (2.0, 1.0, 1.0, 1.0);
slouken@1662
   172
        f.glRotatef (1.0, 0.0, 1.0, 1.0);
slouken@1662
   173
slouken@1662
   174
        f.glColor4ub (255, 255, 255, 255);
slouken@1662
   175
        f.glBegin (GL_POINTS);
slouken@1662
   176
        for (i = 0; i < NB_PIXELS; i++) {
slouken@1662
   177
            f.glVertex3f (pixels[3 * i], pixels[3 * i + 1],
slouken@1662
   178
                          pixels[3 * i + 2]);
slouken@1662
   179
        }
slouken@1662
   180
        f.glEnd ();
slouken@1662
   181
        SDL_GL_SwapBuffers ();
slouken@1662
   182
slouken@1662
   183
        while (SDL_PollEvent (&event)) {
slouken@1662
   184
            if (event.type & SDL_KEYDOWN)
slouken@1662
   185
                done = 1;
slouken@1662
   186
        }
slouken@1662
   187
slouken@1662
   188
        SDL_Delay (20);
slouken@1662
   189
    }
slouken@1662
   190
    while (!done);
slouken@1662
   191
slouken@1662
   192
    SDL_Quit ();
slouken@1662
   193
    return 0;
slouken@839
   194
}
slouken@839
   195
slouken@839
   196
#else /* HAVE_OPENGL */
slouken@839
   197
slouken@1662
   198
int
slouken@1662
   199
main (int argc, char *argv[])
slouken@839
   200
{
slouken@1662
   201
    printf ("No OpenGL support on this system\n");
slouken@1662
   202
    return 1;
slouken@839
   203
}
slouken@839
   204
slouken@839
   205
#endif /* HAVE_OPENGL */