test/testdyngles.c
author Mike Gorchak <lestat@i.com.ua>
Sat, 21 Nov 2009 08:42:42 +0000
changeset 3478 f7541260e89c
parent 3410 f4bc25567731
child 5150 1435f8a6425c
permissions -rw-r--r--
Added support for QNX default font. Backspace and Return keys now handled.
slouken@2765
     1
/*
slouken@2765
     2
 * Small SDL example to demonstrate dynamically loading 
slouken@2765
     3
 * OpenGL lib and functions
slouken@2765
     4
 *
slouken@2765
     5
 * (FYI it was supposed to look like snow in the wind or something...)
slouken@2765
     6
 *
slouken@2765
     7
 * Compile with :
slouken@2765
     8
 * gcc testdyngl.c `sdl-config --libs --cflags` -o testdyngl -DHAVE_OPENGL
slouken@2765
     9
 *
slouken@2765
    10
 * You can specify a different OpenGL lib on the command line, i.e. :
slouken@2765
    11
 * ./testdyngl  /usr/X11R6/lib/libGL.so.1.2
slouken@2765
    12
 * or
slouken@2765
    13
 * ./testdyngl  /usr/lib/libGL.so.1.0.4496
slouken@2765
    14
 *
slouken@2765
    15
 */
slouken@2765
    16
slouken@2765
    17
#include <stdio.h>
slouken@2765
    18
#include <stdlib.h>
slouken@2765
    19
slouken@2765
    20
#include "SDL.h"
slouken@2765
    21
slouken@2765
    22
#ifdef __IPHONEOS__
slouken@2765
    23
#define HAVE_OPENGLES
slouken@2765
    24
#endif
slouken@2765
    25
slouken@2765
    26
#ifdef HAVE_OPENGLES
slouken@2765
    27
slouken@2765
    28
#include "SDL_opengles.h"
slouken@2765
    29
slouken@2765
    30
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@2765
    31
static void
slouken@2765
    32
quit(int rc)
slouken@2765
    33
{
slouken@2765
    34
    SDL_Quit();
slouken@2765
    35
    exit(rc);
slouken@2765
    36
}
slouken@2765
    37
slouken@2765
    38
void *
slouken@2765
    39
get_funcaddr(const char *p)
slouken@2765
    40
{
slouken@2765
    41
    void *f = SDL_GL_GetProcAddress(p);
slouken@2765
    42
    if (f) {
slouken@2765
    43
        return f;
slouken@2765
    44
    } else {
slouken@2765
    45
        printf("Unable to get function pointer for %s\n", p);
slouken@2765
    46
        quit(1);
slouken@2765
    47
    }
slouken@2765
    48
    return NULL;
slouken@2765
    49
}
slouken@2765
    50
slouken@2765
    51
typedef struct
slouken@2765
    52
{
slouken@2765
    53
    void (APIENTRY * glEnableClientState) (GLenum array);
lestat@3410
    54
    void (APIENTRY * glDisableClientState) (GLenum array);
slouken@2765
    55
    void (APIENTRY * glVertexPointer) (GLint size, GLenum type,
slouken@2765
    56
                                       GLsizei stride,
slouken@2765
    57
                                       const GLvoid * pointer);
slouken@2765
    58
    void (APIENTRY * glDrawArrays) (GLenum mode, GLint first, GLsizei count);
slouken@2765
    59
slouken@2765
    60
slouken@2765
    61
    void (APIENTRY * glClearColor) (GLfloat, GLfloat, GLfloat, GLfloat);
slouken@2765
    62
    void (APIENTRY * glClear) (GLbitfield);
slouken@2765
    63
    void (APIENTRY * glDisable) (GLenum);
slouken@2765
    64
    void (APIENTRY * glEnable) (GLenum);
slouken@3099
    65
    void (APIENTRY * glColor4f) (GLfloat, GLfloat, GLfloat, GLfloat);
slouken@2765
    66
    void (APIENTRY * glPointSize) (GLfloat);
slouken@2765
    67
    void (APIENTRY * glHint) (GLenum, GLenum);
slouken@2765
    68
    void (APIENTRY * glBlendFunc) (GLenum, GLenum);
slouken@2765
    69
    void (APIENTRY * glMatrixMode) (GLenum);
slouken@2765
    70
    void (APIENTRY * glLoadIdentity) ();
slouken@2765
    71
    void (APIENTRY * glOrthof) (GLfloat, GLfloat, GLfloat, GLfloat,
slouken@2765
    72
                                GLfloat, GLfloat);
slouken@2765
    73
    void (APIENTRY * glRotatef) (GLfloat, GLfloat, GLfloat, GLfloat);
slouken@2765
    74
    void (APIENTRY * glViewport) (GLint, GLint, GLsizei, GLsizei);
slouken@2765
    75
    void (APIENTRY * glFogf) (GLenum, GLfloat);
slouken@2765
    76
}
slouken@2765
    77
glfuncs;
slouken@2765
    78
slouken@2765
    79
void
slouken@2765
    80
init_glfuncs(glfuncs * f)
slouken@2765
    81
{
slouken@2765
    82
    f->glEnableClientState = get_funcaddr("glEnableClientState");
lestat@3410
    83
    f->glDisableClientState = get_funcaddr("glDisableClientState");
slouken@2765
    84
    f->glVertexPointer = get_funcaddr("glVertexPointer");
slouken@2765
    85
    f->glDrawArrays = get_funcaddr("glDrawArrays");
slouken@2765
    86
    f->glClearColor = get_funcaddr("glClearColor");
slouken@2765
    87
    f->glClear = get_funcaddr("glClear");
slouken@2765
    88
    f->glDisable = get_funcaddr("glDisable");
slouken@2765
    89
    f->glEnable = get_funcaddr("glEnable");
slouken@3099
    90
    f->glColor4f = get_funcaddr("glColor4f");
slouken@2765
    91
    f->glPointSize = get_funcaddr("glPointSize");
slouken@2765
    92
    f->glHint = get_funcaddr("glHint");
slouken@2765
    93
    f->glBlendFunc = get_funcaddr("glBlendFunc");
slouken@2765
    94
    f->glMatrixMode = get_funcaddr("glMatrixMode");
slouken@2765
    95
    f->glLoadIdentity = get_funcaddr("glLoadIdentity");
slouken@2765
    96
    f->glOrthof = get_funcaddr("glOrthof");
slouken@2765
    97
    f->glRotatef = get_funcaddr("glRotatef");
slouken@2765
    98
    f->glViewport = get_funcaddr("glViewport");
slouken@2765
    99
    f->glFogf = get_funcaddr("glFogf");
slouken@2765
   100
}
slouken@2765
   101
slouken@2765
   102
#define NB_PIXELS 1000
slouken@2765
   103
slouken@2765
   104
int
slouken@2765
   105
main(int argc, char *argv[])
slouken@2765
   106
{
slouken@2765
   107
    glfuncs f;
slouken@2765
   108
    int i;
slouken@2765
   109
    SDL_Event event;
slouken@2765
   110
    int done = 0;
slouken@2765
   111
    GLfloat pixels[NB_PIXELS * 3];
slouken@2765
   112
    const char *gl_library = NULL;      /* Use the default GL library */
slouken@2765
   113
slouken@2765
   114
    int video_w, video_h;
slouken@2765
   115
slouken@2765
   116
    /* you may want to change these according to the platform */
slouken@2765
   117
    video_w = 320;
slouken@2765
   118
    video_h = 480;
slouken@3139
   119
#ifdef __QNXNTO__
lestat@3410
   120
    video_w = 480;
slouken@3139
   121
    video_h = 640;
slouken@3139
   122
#endif /* __QNXNTO__ */
slouken@2765
   123
slouken@2765
   124
    if (argv[1]) {
slouken@2765
   125
        gl_library = argv[1];
slouken@2765
   126
    }
slouken@2765
   127
slouken@2765
   128
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@2765
   129
        printf("Unable to init SDL : %s\n", SDL_GetError());
slouken@2765
   130
        return (1);
slouken@2765
   131
    }
slouken@2765
   132
slouken@2765
   133
    if (SDL_GL_LoadLibrary(gl_library) < 0) {
slouken@3087
   134
        printf("Unable to dynamically open GL ES lib : %s\n", SDL_GetError());
slouken@2765
   135
        quit(1);
slouken@2765
   136
    }
slouken@2765
   137
slouken@2765
   138
    if (SDL_SetVideoMode(video_h, video_w, 0, SDL_OPENGL) == NULL) {
slouken@2765
   139
        printf("Unable to open video mode : %s\n", SDL_GetError());
slouken@2765
   140
        quit(1);
slouken@2765
   141
    }
slouken@2765
   142
slouken@2765
   143
    /* Set the window manager title bar */
slouken@3087
   144
    SDL_WM_SetCaption("SDL Dynamic OpenGL ES Loading Test", "testdyngles");
slouken@2765
   145
slouken@2765
   146
    init_glfuncs(&f);
slouken@2765
   147
slouken@2765
   148
    for (i = 0; i < NB_PIXELS; i++) {
slouken@2765
   149
        pixels[3 * i] = rand() % 250 - 125;
slouken@2765
   150
        pixels[3 * i + 1] = rand() % 250 - 125;
slouken@2765
   151
        pixels[3 * i + 2] = rand() % 250 - 125;
slouken@2765
   152
    }
slouken@2765
   153
slouken@3139
   154
#ifdef __QNXNTO__
slouken@3139
   155
    f.glViewport(0, 0, video_h, video_w);
slouken@3139
   156
#else
slouken@3139
   157
    f.glViewport(0, 0, video_w, video_h);
slouken@3139
   158
#endif /* __QNXNTO__ */
slouken@2765
   159
slouken@2765
   160
    f.glMatrixMode(GL_PROJECTION);
slouken@2765
   161
    f.glLoadIdentity();
slouken@2765
   162
    f.glOrthof(-100, 100, -100, 100, -500, 500);
slouken@2765
   163
slouken@2765
   164
    f.glMatrixMode(GL_MODELVIEW);
slouken@2765
   165
    f.glLoadIdentity();
slouken@2765
   166
slouken@2765
   167
    f.glEnable(GL_DEPTH_TEST);
slouken@2765
   168
    f.glDisable(GL_TEXTURE_2D);
slouken@2765
   169
    f.glEnable(GL_BLEND);
slouken@2765
   170
    f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@2765
   171
slouken@2765
   172
    f.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
slouken@2765
   173
    f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
slouken@2765
   174
slouken@2765
   175
    f.glEnable(GL_POINT_SMOOTH);
slouken@2765
   176
    f.glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
lestat@3410
   177
    f.glPointSize(1.0f);
slouken@2765
   178
    f.glEnable(GL_FOG);
slouken@2765
   179
    f.glFogf(GL_FOG_START, -500);
slouken@2765
   180
    f.glFogf(GL_FOG_END, 500);
slouken@2765
   181
    f.glFogf(GL_FOG_DENSITY, 0.005);
slouken@2765
   182
lestat@3410
   183
    f.glVertexPointer(3, GL_FLOAT, 0, pixels);
lestat@3410
   184
    f.glEnableClientState(GL_VERTEX_ARRAY);
lestat@3410
   185
slouken@2765
   186
    do {
slouken@2765
   187
        f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
slouken@2765
   188
slouken@2765
   189
        f.glRotatef(2.0, 1.0, 1.0, 1.0);
slouken@2765
   190
        f.glRotatef(1.0, 0.0, 1.0, 1.0);
slouken@2765
   191
slouken@3099
   192
        f.glColor4f(1.0, 1.0, 1.0, 1.0);
slouken@2765
   193
slouken@2765
   194
        f.glDrawArrays(GL_POINTS, 0, NB_PIXELS);
slouken@2765
   195
slouken@2765
   196
        SDL_GL_SwapBuffers();
slouken@2765
   197
slouken@2765
   198
        while (SDL_PollEvent(&event)) {
slouken@3099
   199
            if (event.type == SDL_QUIT)
slouken@3099
   200
                done = 1;
slouken@2765
   201
            if (event.type == SDL_KEYDOWN)
slouken@2765
   202
                done = 1;
slouken@2765
   203
        }
slouken@2765
   204
slouken@2765
   205
        SDL_Delay(20);
slouken@2765
   206
    }
slouken@2765
   207
    while (!done);
slouken@2765
   208
lestat@3410
   209
    f.glDisableClientState(GL_VERTEX_ARRAY);
lestat@3410
   210
slouken@2765
   211
    SDL_Quit();
slouken@2765
   212
    return 0;
slouken@2765
   213
}
slouken@2765
   214
slouken@2765
   215
#else /* HAVE_OPENGLES */
slouken@2765
   216
slouken@2765
   217
int
slouken@2765
   218
main(int argc, char *argv[])
slouken@2765
   219
{
slouken@3087
   220
    printf("No OpenGL ES support on this system\n");
slouken@2765
   221
    return 1;
slouken@2765
   222
}
slouken@2765
   223
slouken@2765
   224
#endif /* HAVE_OPENGLES */