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