test/testdyngl.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 26 Feb 2006 05:02:14 +0000
changeset 1439 4d3bb026cd16
parent 1436 b5d4f280c960
child 1440 ae78374ac894
permissions -rw-r--r--
Fixed warnings in -pedantic mode
     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 	return NULL;
    46 }
    47 
    48 typedef struct
    49 {
    50 	void(APIENTRY*glBegin)(GLenum);
    51 	void(APIENTRY*glEnd)();
    52 	void(APIENTRY*glVertex3f)(GLfloat, GLfloat, GLfloat);
    53 	void(APIENTRY*glClearColor)(GLfloat, GLfloat, GLfloat, GLfloat);
    54 	void(APIENTRY*glClear)(GLbitfield);
    55 	void(APIENTRY*glDisable)(GLenum);
    56 	void(APIENTRY*glEnable)(GLenum);
    57 	void(APIENTRY*glColor4ub)(GLubyte,GLubyte,GLubyte,GLubyte);
    58 	void(APIENTRY*glPointSize)(GLfloat);
    59 	void(APIENTRY*glHint)(GLenum,GLenum);
    60 	void(APIENTRY*glBlendFunc)(GLenum,GLenum);
    61 	void(APIENTRY*glMatrixMode)(GLenum);
    62 	void(APIENTRY*glLoadIdentity)();
    63 	void(APIENTRY*glOrtho)(GLdouble,GLdouble,GLdouble,GLdouble,GLdouble,GLdouble);
    64 	void(APIENTRY*glRotatef)(GLfloat,GLfloat,GLfloat,GLfloat);
    65 	void(APIENTRY*glViewport)(GLint,GLint,GLsizei,GLsizei);
    66 	void(APIENTRY*glFogf)(GLenum,GLfloat);
    67 }
    68 glfuncs;
    69 
    70 void init_glfuncs(glfuncs* f)
    71 {
    72 	f->glBegin=get_funcaddr("glBegin");
    73 	f->glEnd=get_funcaddr("glEnd");
    74 	f->glVertex3f=get_funcaddr("glVertex3f");
    75 	f->glClearColor=get_funcaddr("glClearColor");
    76 	f->glClear=get_funcaddr("glClear");
    77 	f->glDisable=get_funcaddr("glDisable");
    78 	f->glEnable=get_funcaddr("glEnable");
    79 	f->glColor4ub=get_funcaddr("glColor4ub");
    80 	f->glPointSize=get_funcaddr("glPointSize");
    81 	f->glHint=get_funcaddr("glHint");
    82 	f->glBlendFunc=get_funcaddr("glBlendFunc");
    83 	f->glMatrixMode=get_funcaddr("glMatrixMode");
    84 	f->glLoadIdentity=get_funcaddr("glLoadIdentity");
    85 	f->glOrtho=get_funcaddr("glOrtho");
    86 	f->glRotatef=get_funcaddr("glRotatef");
    87 	f->glViewport=get_funcaddr("glViewport");
    88 	f->glFogf=get_funcaddr("glFogf");
    89 }
    90 
    91 #define NB_PIXELS 1000
    92 
    93 int main(int argc,char *argv[])
    94 {
    95 	glfuncs f;
    96 	int i;
    97 	SDL_Event event;
    98 	int done=0;
    99 	GLfloat pixels[NB_PIXELS*3];
   100 #if defined(__WIN32__)
   101 	const char *gl_library = "OpenGL32.DLL";
   102 #elif defined(__MACOS__)
   103 	const char *gl_library = "OpenGLLibrary";
   104 #elif defined(__MACOSX__)
   105 	const char *gl_library = "/usr/X11R6/lib/libGL.1.dylib";
   106 #elif defined(__IRIX__)
   107 	const char *gl_library = "libGL.so";
   108 #elif defined(__QNXNTO__)
   109 	const char *gl_library = "libGL.so.3";
   110 #else
   111 	const char *gl_library = "libGL.so.1";
   112 #endif
   113 
   114 	if (argv[1]) {
   115 		gl_library = argv[1];
   116 	}
   117 	
   118 	if (SDL_Init(SDL_INIT_VIDEO)<0)
   119 	{
   120 		printf("Unable to init SDL : %s\n",SDL_GetError());
   121 		return(1);
   122 	}
   123 
   124 	if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1)<0)
   125 	{
   126 		printf("Unable to set GL attribute : %s\n",SDL_GetError());
   127 		quit(1);
   128 	}
   129 	
   130 	if (SDL_GL_LoadLibrary(gl_library)<0)
   131 	{
   132 		printf("Unable to dynamically open GL lib : %s\n",SDL_GetError());
   133 		quit(1);
   134 	}
   135 
   136 	if (SDL_SetVideoMode(640,480,0,SDL_OPENGL)==NULL)
   137 	{
   138 		printf("Unable to open video mode : %s\n",SDL_GetError());
   139 		quit(1);
   140 	}
   141 
   142 	/* Set the window manager title bar */
   143 	SDL_WM_SetCaption( "SDL Dynamic OpenGL Loading Test", "testdyngl" );
   144 
   145 	init_glfuncs(&f);
   146 
   147 	for(i=0;i<NB_PIXELS;i++)
   148 	{
   149 		pixels[3*i]=rand()%250-125;
   150 		pixels[3*i+1]=rand()%250-125;
   151 		pixels[3*i+2]=rand()%250-125;
   152 	}
   153 	
   154 	f.glViewport(0,0,640,480);
   155 	
   156 	f.glMatrixMode(GL_PROJECTION);
   157 	f.glLoadIdentity();
   158 	f.glOrtho(-100,100,-100,100,-500,500);
   159 	
   160 	f.glMatrixMode(GL_MODELVIEW);
   161 	f.glLoadIdentity();
   162 	
   163 	f.glEnable(GL_DEPTH_TEST);
   164 	f.glDisable(GL_TEXTURE_2D);
   165 	f.glEnable(GL_BLEND);
   166 	f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   167 	
   168 	f.glClearColor(0.0f,0.0f,0.0f,0.0f);
   169 	f.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
   170 	
   171 	f.glEnable(GL_POINT_SMOOTH);
   172 	f.glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);
   173 	f.glPointSize(5.0f);
   174 	f.glEnable(GL_FOG);
   175 	f.glFogf(GL_FOG_START,-500);
   176 	f.glFogf(GL_FOG_END,500);
   177 	f.glFogf(GL_FOG_DENSITY,0.005);
   178 	
   179 	do
   180 	{
   181 		f.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
   182 		
   183 		f.glRotatef(2.0,1.0,1.0,1.0);
   184 		f.glRotatef(1.0,0.0,1.0,1.0);
   185 		
   186 		f.glColor4ub(255,255,255,255);
   187 		f.glBegin(GL_POINTS);
   188 		for(i=0;i<NB_PIXELS;i++)
   189 		{
   190 			f.glVertex3f(pixels[3*i],pixels[3*i+1],pixels[3*i+2]);
   191 		}
   192 		f.glEnd();
   193 		SDL_GL_SwapBuffers();
   194 
   195 		while(SDL_PollEvent(&event))
   196 		{
   197 			if(event.type & SDL_KEYDOWN)
   198 				done=1;
   199 		}
   200 
   201 		SDL_Delay(20);
   202 	}
   203 	while(!done);
   204 	
   205 	SDL_Quit();
   206 	return 0;
   207 }
   208 
   209 #else /* HAVE_OPENGL */
   210 
   211 int main(int argc, char *argv[])
   212 {
   213 	printf("No OpenGL support on this system\n");
   214 	return 1;
   215 }
   216 
   217 #endif /* HAVE_OPENGL */