test/testdyngl.c
author Darren Alton <dalton@stevens.edu>
Tue, 10 Jun 2008 06:45:39 +0000
branchgsoc2008_nds
changeset 2669 e27bdcc80744
parent 2327 7b53a8401195
child 3323 8d0889509afb
permissions -rw-r--r--
First commit. Added header configs for DS as well as a Makefile. Initial work on framebuffer video driver, currently not functioning as desired.
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@839
    71
}
slouken@839
    72
glfuncs;
slouken@839
    73
slouken@1895
    74
void
slouken@1895
    75
init_glfuncs(glfuncs * f)
slouken@839
    76
{
slouken@1895
    77
    f->glBegin = get_funcaddr("glBegin");
slouken@1895
    78
    f->glEnd = get_funcaddr("glEnd");
slouken@1895
    79
    f->glVertex3f = get_funcaddr("glVertex3f");
slouken@1895
    80
    f->glClearColor = get_funcaddr("glClearColor");
slouken@1895
    81
    f->glClear = get_funcaddr("glClear");
slouken@1895
    82
    f->glDisable = get_funcaddr("glDisable");
slouken@1895
    83
    f->glEnable = get_funcaddr("glEnable");
slouken@1895
    84
    f->glColor4ub = get_funcaddr("glColor4ub");
slouken@1895
    85
    f->glPointSize = get_funcaddr("glPointSize");
slouken@1895
    86
    f->glHint = get_funcaddr("glHint");
slouken@1895
    87
    f->glBlendFunc = get_funcaddr("glBlendFunc");
slouken@1895
    88
    f->glMatrixMode = get_funcaddr("glMatrixMode");
slouken@1895
    89
    f->glLoadIdentity = get_funcaddr("glLoadIdentity");
slouken@1895
    90
    f->glOrtho = get_funcaddr("glOrtho");
slouken@1895
    91
    f->glRotatef = get_funcaddr("glRotatef");
slouken@1895
    92
    f->glViewport = get_funcaddr("glViewport");
slouken@1895
    93
    f->glFogf = get_funcaddr("glFogf");
slouken@839
    94
}
slouken@839
    95
slouken@839
    96
#define NB_PIXELS 1000
slouken@839
    97
slouken@1895
    98
int
slouken@1895
    99
main(int argc, char *argv[])
slouken@839
   100
{
slouken@1895
   101
    glfuncs f;
slouken@1895
   102
    int i;
slouken@1895
   103
    SDL_Event event;
slouken@1895
   104
    int done = 0;
slouken@1895
   105
    GLfloat pixels[NB_PIXELS * 3];
slouken@1895
   106
    const char *gl_library = NULL;      /* Use the default GL library */
slouken@1402
   107
slouken@1895
   108
    if (argv[1]) {
slouken@1895
   109
        gl_library = argv[1];
slouken@1895
   110
    }
patmandin@990
   111
slouken@1895
   112
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@1895
   113
        printf("Unable to init SDL : %s\n", SDL_GetError());
slouken@1895
   114
        return (1);
slouken@1895
   115
    }
slouken@839
   116
slouken@1895
   117
    if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) < 0) {
slouken@1895
   118
        printf("Unable to set GL attribute : %s\n", SDL_GetError());
slouken@1895
   119
        quit(1);
slouken@1895
   120
    }
slouken@839
   121
slouken@1895
   122
    if (SDL_GL_LoadLibrary(gl_library) < 0) {
slouken@1895
   123
        printf("Unable to dynamically open GL lib : %s\n", SDL_GetError());
slouken@1895
   124
        quit(1);
slouken@1895
   125
    }
slouken@910
   126
slouken@1895
   127
    if (SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL) {
slouken@1895
   128
        printf("Unable to open video mode : %s\n", SDL_GetError());
slouken@1895
   129
        quit(1);
slouken@1895
   130
    }
slouken@839
   131
slouken@1895
   132
    /* Set the window manager title bar */
slouken@1895
   133
    SDL_WM_SetCaption("SDL Dynamic OpenGL Loading Test", "testdyngl");
slouken@839
   134
slouken@1895
   135
    init_glfuncs(&f);
slouken@839
   136
slouken@1895
   137
    for (i = 0; i < NB_PIXELS; i++) {
slouken@1895
   138
        pixels[3 * i] = rand() % 250 - 125;
slouken@1895
   139
        pixels[3 * i + 1] = rand() % 250 - 125;
slouken@1895
   140
        pixels[3 * i + 2] = rand() % 250 - 125;
slouken@1895
   141
    }
slouken@1895
   142
slouken@1895
   143
    f.glViewport(0, 0, 640, 480);
slouken@1895
   144
slouken@1895
   145
    f.glMatrixMode(GL_PROJECTION);
slouken@1895
   146
    f.glLoadIdentity();
slouken@1895
   147
    f.glOrtho(-100, 100, -100, 100, -500, 500);
slouken@1895
   148
slouken@1895
   149
    f.glMatrixMode(GL_MODELVIEW);
slouken@1895
   150
    f.glLoadIdentity();
slouken@1895
   151
slouken@1895
   152
    f.glEnable(GL_DEPTH_TEST);
slouken@1895
   153
    f.glDisable(GL_TEXTURE_2D);
slouken@1895
   154
    f.glEnable(GL_BLEND);
slouken@1895
   155
    f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@1895
   156
slouken@1895
   157
    f.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
slouken@1895
   158
    f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
slouken@1895
   159
slouken@1895
   160
    f.glEnable(GL_POINT_SMOOTH);
slouken@1895
   161
    f.glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
slouken@1895
   162
    f.glPointSize(5.0f);
slouken@1895
   163
    f.glEnable(GL_FOG);
slouken@1895
   164
    f.glFogf(GL_FOG_START, -500);
slouken@1895
   165
    f.glFogf(GL_FOG_END, 500);
slouken@1895
   166
    f.glFogf(GL_FOG_DENSITY, 0.005);
slouken@1895
   167
slouken@1895
   168
    do {
slouken@1895
   169
        f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
slouken@1895
   170
slouken@1895
   171
        f.glRotatef(2.0, 1.0, 1.0, 1.0);
slouken@1895
   172
        f.glRotatef(1.0, 0.0, 1.0, 1.0);
slouken@1895
   173
slouken@1895
   174
        f.glColor4ub(255, 255, 255, 255);
slouken@1895
   175
        f.glBegin(GL_POINTS);
slouken@1895
   176
        for (i = 0; i < NB_PIXELS; i++) {
slouken@1895
   177
            f.glVertex3f(pixels[3 * i], pixels[3 * i + 1], pixels[3 * i + 2]);
slouken@1895
   178
        }
slouken@1895
   179
        f.glEnd();
slouken@1895
   180
        SDL_GL_SwapBuffers();
slouken@1895
   181
slouken@1895
   182
        while (SDL_PollEvent(&event)) {
bob@2327
   183
            if (event.type == SDL_KEYDOWN)
slouken@1895
   184
                done = 1;
slouken@1895
   185
        }
slouken@1895
   186
slouken@1895
   187
        SDL_Delay(20);
dalton@2669
   188
    } while (!done);
slouken@1895
   189
slouken@1895
   190
    SDL_Quit();
slouken@1895
   191
    return 0;
slouken@839
   192
}
slouken@839
   193
slouken@839
   194
#else /* HAVE_OPENGL */
slouken@839
   195
slouken@1895
   196
int
slouken@1895
   197
main(int argc, char *argv[])
slouken@839
   198
{
slouken@1895
   199
    printf("No OpenGL support on this system\n");
slouken@1895
   200
    return 1;
slouken@839
   201
}
slouken@839
   202
slouken@839
   203
#endif /* HAVE_OPENGL */