Added a test program for dynamically loading OpenGL
authorSam Lantinga
Tue, 17 Feb 2004 18:16:49 +0000
changeset 8397b255f0290da
parent 838 f9a5fbcdf868
child 840 6013fddf4c91
Added a test program for dynamically loading OpenGL
test/Makefile.am
test/testdyngl.c
     1.1 --- a/test/Makefile.am	Tue Feb 17 18:01:39 2004 +0000
     1.2 +++ b/test/Makefile.am	Tue Feb 17 18:16:49 2004 +0000
     1.3 @@ -4,7 +4,7 @@
     1.4  	testver testtypes testcpuinfo testhread testlock testerror testsem testtimer \
     1.5  	loopwave testcdrom testkeys testvidinfo checkkeys testwin graywin \
     1.6  	testsprite testbitmap testalpha testgamma testpalette testwm \
     1.7 -	threadwin testoverlay testoverlay2 testgl testjoystick
     1.8 +	threadwin testoverlay testoverlay2 testgl testdyngl testjoystick
     1.9  
    1.10  testalpha_SOURCES = testalpha.c
    1.11  testalpha_LDADD = @MATHLIB@
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/testdyngl.c	Tue Feb 17 18:16:49 2004 +0000
     2.3 @@ -0,0 +1,196 @@
     2.4 +/*
     2.5 + * Small SDL example to demonstrate dynamically loading 
     2.6 + * OpenGL lib and functions
     2.7 + *
     2.8 + * (FYI it was supposed to look like snow in the wind or something...)
     2.9 + *
    2.10 + * Compile with :
    2.11 + * gcc testdyngl.c `sdl-config --libs --cflags` -o testdyngl
    2.12 + *
    2.13 + * You can specify a different OpenGL lib on the command line, i.e. :
    2.14 + * ./testdyngl  /usr/X11R6/lib/libGL.so.1.2
    2.15 + * or
    2.16 + * ./testdyngl  /usr/lib/libGL.so.1.0.4496
    2.17 + *
    2.18 + */
    2.19 +
    2.20 +#include <stdio.h>
    2.21 +#include <stdlib.h>
    2.22 +
    2.23 +#include "SDL.h"
    2.24 +
    2.25 +#ifdef HAVE_OPENGL
    2.26 +
    2.27 +#include "SDL_opengl.h"
    2.28 +
    2.29 +void* get_funcaddr(const char* p)
    2.30 +{
    2.31 +	void* f=SDL_GL_GetProcAddress(p);
    2.32 +	if (f)
    2.33 +	{
    2.34 +		return f;
    2.35 +	}
    2.36 +	else
    2.37 +	{
    2.38 +		printf("Unable to get function pointer for %s\n",p);
    2.39 +		exit(1);
    2.40 +	}
    2.41 +}
    2.42 +
    2.43 +typedef struct
    2.44 +{
    2.45 +	void(*glBegin)(GLenum);
    2.46 +	void(*glEnd)();
    2.47 +	void(*glVertex3f)(GLfloat, GLfloat, GLfloat);
    2.48 +	void(*glClearColor)(GLfloat, GLfloat, GLfloat, GLfloat);
    2.49 +	void(*glClear)(GLbitfield);
    2.50 +	void(*glDisable)(GLenum);
    2.51 +	void(*glEnable)(GLenum);
    2.52 +	void(*glColor4ub)(GLubyte,GLubyte,GLubyte,GLubyte);
    2.53 +	void(*glPointSize)(GLfloat);
    2.54 +	void(*glHint)(GLenum,GLenum);
    2.55 +	void(*glBlendFunc)(GLenum,GLenum);
    2.56 +	void(*glMatrixMode)(GLenum);
    2.57 +	void(*glLoadIdentity)();
    2.58 +	void(*glOrtho)(GLdouble,GLdouble,GLdouble,GLdouble,GLdouble,GLdouble);
    2.59 +	void(*glRotatef)(GLfloat,GLfloat,GLfloat,GLfloat);
    2.60 +	void(*glViewport)(GLint,GLint,GLsizei,GLsizei);
    2.61 +	void(*glFogf)(GLenum,GLfloat);
    2.62 +}
    2.63 +glfuncs;
    2.64 +
    2.65 +void init_glfuncs(glfuncs* f)
    2.66 +{
    2.67 +	f->glBegin=get_funcaddr("glBegin");
    2.68 +	f->glEnd=get_funcaddr("glEnd");
    2.69 +	f->glVertex3f=get_funcaddr("glVertex3f");
    2.70 +	f->glClearColor=get_funcaddr("glClearColor");
    2.71 +	f->glClear=get_funcaddr("glClear");
    2.72 +	f->glDisable=get_funcaddr("glDisable");
    2.73 +	f->glEnable=get_funcaddr("glEnable");
    2.74 +	f->glColor4ub=get_funcaddr("glColor4ub");
    2.75 +	f->glPointSize=get_funcaddr("glPointSize");
    2.76 +	f->glHint=get_funcaddr("glHint");
    2.77 +	f->glBlendFunc=get_funcaddr("glBlendFunc");
    2.78 +	f->glMatrixMode=get_funcaddr("glMatrixMode");
    2.79 +	f->glLoadIdentity=get_funcaddr("glLoadIdentity");
    2.80 +	f->glOrtho=get_funcaddr("glOrtho");
    2.81 +	f->glRotatef=get_funcaddr("glRotatef");
    2.82 +	f->glViewport=get_funcaddr("glViewport");
    2.83 +	f->glFogf=get_funcaddr("glFogf");
    2.84 +}
    2.85 +
    2.86 +#define NB_PIXELS 1000
    2.87 +
    2.88 +int main(int argc,char *argv[])
    2.89 +{
    2.90 +	glfuncs f;
    2.91 +	int i;
    2.92 +	SDL_Event event;
    2.93 +	int done=0;
    2.94 +	GLfloat pixels[NB_PIXELS*3];
    2.95 +	char default_gl_lib[]="/usr/lib/libGL.so";
    2.96 +	char* current_gl_lib=default_gl_lib;
    2.97 +	
    2.98 +	if (argc==2)
    2.99 +	{
   2.100 +		current_gl_lib=argv[1];
   2.101 +	}
   2.102 +	
   2.103 +	if (SDL_Init(SDL_INIT_EVERYTHING)<0)
   2.104 +	{
   2.105 +		printf("Unable to init SDL : %s\n",SDL_GetError());
   2.106 +		exit(1);
   2.107 +	}
   2.108 +	
   2.109 +	if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1)<0)
   2.110 +	{
   2.111 +		printf("Unable to set GL attribute : %s\n",SDL_GetError());
   2.112 +		exit(1);
   2.113 +	}
   2.114 +	
   2.115 +	if (SDL_GL_LoadLibrary(current_gl_lib)<0)
   2.116 +	{
   2.117 +		printf("Unable to dynamically open GL lib : %s\n",SDL_GetError());
   2.118 +		exit(1);
   2.119 +	}
   2.120 +
   2.121 +	if (SDL_SetVideoMode(640,480,0,SDL_OPENGL)==NULL)
   2.122 +	{
   2.123 +		printf("Unable to open video mode : %s\n",SDL_GetError());
   2.124 +		exit(1);
   2.125 +	}
   2.126 +
   2.127 +	init_glfuncs(&f);
   2.128 +
   2.129 +	for(i=0;i<NB_PIXELS;i++)
   2.130 +	{
   2.131 +		pixels[3*i]=rand()%250-125;
   2.132 +		pixels[3*i+1]=rand()%250-125;
   2.133 +		pixels[3*i+2]=rand()%250-125;
   2.134 +	}
   2.135 +	
   2.136 +	f.glViewport(0,0,640,480);
   2.137 +	
   2.138 +	f.glMatrixMode(GL_PROJECTION);
   2.139 +	f.glLoadIdentity();
   2.140 +	f.glOrtho(-100,100,-100,100,-500,500);
   2.141 +	
   2.142 +	f.glMatrixMode(GL_MODELVIEW);
   2.143 +	f.glLoadIdentity();
   2.144 +	
   2.145 +	f.glEnable(GL_DEPTH_TEST);
   2.146 +	f.glDisable(GL_TEXTURE_2D);
   2.147 +	f.glEnable(GL_BLEND);
   2.148 +	f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   2.149 +	
   2.150 +	f.glClearColor(0.0f,0.0f,0.0f,0.0f);
   2.151 +	f.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
   2.152 +	
   2.153 +	f.glEnable(GL_POINT_SMOOTH);
   2.154 +	f.glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);
   2.155 +	f.glPointSize(5.0f);
   2.156 +	f.glEnable(GL_FOG);
   2.157 +	f.glFogf(GL_FOG_START,-500);
   2.158 +	f.glFogf(GL_FOG_END,500);
   2.159 +	f.glFogf(GL_FOG_DENSITY,0.005);
   2.160 +	
   2.161 +	do
   2.162 +	{
   2.163 +		f.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
   2.164 +		
   2.165 +		f.glRotatef(2.0,1.0,1.0,1.0);
   2.166 +		f.glRotatef(1.0,0.0,1.0,1.0);
   2.167 +		
   2.168 +		f.glColor4ub(255,255,255,255);
   2.169 +		f.glBegin(GL_POINTS);
   2.170 +		for(i=0;i<NB_PIXELS;i++)
   2.171 +		{
   2.172 +			f.glVertex3f(pixels[3*i],pixels[3*i+1],pixels[3*i+2]);
   2.173 +		}
   2.174 +		f.glEnd();
   2.175 +		SDL_GL_SwapBuffers();
   2.176 +
   2.177 +		while(SDL_PollEvent(&event))
   2.178 +		{
   2.179 +			if(event.type & SDL_KEYDOWN)
   2.180 +				done=1;
   2.181 +		}
   2.182 +
   2.183 +		SDL_Delay(20);
   2.184 +	}
   2.185 +	while(!done);
   2.186 +	
   2.187 +	SDL_Quit();
   2.188 +	return 0;
   2.189 +}
   2.190 +
   2.191 +#else /* HAVE_OPENGL */
   2.192 +
   2.193 +int main(int argc, char *argv[])
   2.194 +{
   2.195 +	printf("No OpenGL support on this system\n");
   2.196 +	return 1;
   2.197 +}
   2.198 +
   2.199 +#endif /* HAVE_OPENGL */