Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
204 lines (170 loc) · 5.25 KB

testdyngl.c

File metadata and controls

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