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