test/testdyngl.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 18 Jul 2004 19:46:38 +0000
changeset 910 4ab6d1fd028f
parent 885 9f6ad2286011
child 990 8e20c48a9c13
permissions -rw-r--r--
Date: Sat, 26 Jun 2004 14:58:42 +0300
From: "Mike Gorchak"
Subject: QNX 6.3 fixes for SDL

Sam, I've added new OpenGL framework for SDL, which appeared in the new QNX version - 6.3. I've leave compatibility with previous QNX versions. And I've moved all GL specific functions to the separate module, like it done for the other platforms.

SDL is now ready for the QNX 6.3 :)
     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 void* get_funcaddr(const char* p)
    27 {
    28 	void* f=SDL_GL_GetProcAddress(p);
    29 	if (f)
    30 	{
    31 		return f;
    32 	}
    33 	else
    34 	{
    35 		printf("Unable to get function pointer for %s\n",p);
    36 		exit(1);
    37 	}
    38 }
    39 
    40 typedef struct
    41 {
    42 	void(APIENTRY*glBegin)(GLenum);
    43 	void(APIENTRY*glEnd)();
    44 	void(APIENTRY*glVertex3f)(GLfloat, GLfloat, GLfloat);
    45 	void(APIENTRY*glClearColor)(GLfloat, GLfloat, GLfloat, GLfloat);
    46 	void(APIENTRY*glClear)(GLbitfield);
    47 	void(APIENTRY*glDisable)(GLenum);
    48 	void(APIENTRY*glEnable)(GLenum);
    49 	void(APIENTRY*glColor4ub)(GLubyte,GLubyte,GLubyte,GLubyte);
    50 	void(APIENTRY*glPointSize)(GLfloat);
    51 	void(APIENTRY*glHint)(GLenum,GLenum);
    52 	void(APIENTRY*glBlendFunc)(GLenum,GLenum);
    53 	void(APIENTRY*glMatrixMode)(GLenum);
    54 	void(APIENTRY*glLoadIdentity)();
    55 	void(APIENTRY*glOrtho)(GLdouble,GLdouble,GLdouble,GLdouble,GLdouble,GLdouble);
    56 	void(APIENTRY*glRotatef)(GLfloat,GLfloat,GLfloat,GLfloat);
    57 	void(APIENTRY*glViewport)(GLint,GLint,GLsizei,GLsizei);
    58 	void(APIENTRY*glFogf)(GLenum,GLfloat);
    59 }
    60 glfuncs;
    61 
    62 void init_glfuncs(glfuncs* f)
    63 {
    64 	f->glBegin=get_funcaddr("glBegin");
    65 	f->glEnd=get_funcaddr("glEnd");
    66 	f->glVertex3f=get_funcaddr("glVertex3f");
    67 	f->glClearColor=get_funcaddr("glClearColor");
    68 	f->glClear=get_funcaddr("glClear");
    69 	f->glDisable=get_funcaddr("glDisable");
    70 	f->glEnable=get_funcaddr("glEnable");
    71 	f->glColor4ub=get_funcaddr("glColor4ub");
    72 	f->glPointSize=get_funcaddr("glPointSize");
    73 	f->glHint=get_funcaddr("glHint");
    74 	f->glBlendFunc=get_funcaddr("glBlendFunc");
    75 	f->glMatrixMode=get_funcaddr("glMatrixMode");
    76 	f->glLoadIdentity=get_funcaddr("glLoadIdentity");
    77 	f->glOrtho=get_funcaddr("glOrtho");
    78 	f->glRotatef=get_funcaddr("glRotatef");
    79 	f->glViewport=get_funcaddr("glViewport");
    80 	f->glFogf=get_funcaddr("glFogf");
    81 }
    82 
    83 #define NB_PIXELS 1000
    84 
    85 int main(int argc,char *argv[])
    86 {
    87 	glfuncs f;
    88 	int i;
    89 	SDL_Event event;
    90 	int done=0;
    91 	GLfloat pixels[NB_PIXELS*3];
    92 #if defined(_WIN32)
    93 	char *gl_library = "OpenGL32.DLL";
    94 #elif defined(__QNXNTO__)
    95 	char *gl_library = "libGL.so.3";
    96 #else
    97 	char *gl_library = "libGL.so.1";
    98 #endif
    99 	
   100 	if (argv[1]) {
   101 		gl_library = argv[1];
   102 	}
   103 	
   104 	if (SDL_Init(SDL_INIT_VIDEO)<0)
   105 	{
   106 		printf("Unable to init SDL : %s\n",SDL_GetError());
   107 		exit(1);
   108 	}
   109 	
   110 	if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1)<0)
   111 	{
   112 		printf("Unable to set GL attribute : %s\n",SDL_GetError());
   113 		exit(1);
   114 	}
   115 	
   116 	if (SDL_GL_LoadLibrary(gl_library)<0)
   117 	{
   118 		printf("Unable to dynamically open GL lib : %s\n",SDL_GetError());
   119 		exit(1);
   120 	}
   121 
   122 	if (SDL_SetVideoMode(640,480,0,SDL_OPENGL)==NULL)
   123 	{
   124 		printf("Unable to open video mode : %s\n",SDL_GetError());
   125 		exit(1);
   126 	}
   127 
   128 	/* Set the window manager title bar */
   129 	SDL_WM_SetCaption( "SDL Dynamic OpenGL Loading Test", "testdyngl" );
   130 
   131 	init_glfuncs(&f);
   132 
   133 	for(i=0;i<NB_PIXELS;i++)
   134 	{
   135 		pixels[3*i]=rand()%250-125;
   136 		pixels[3*i+1]=rand()%250-125;
   137 		pixels[3*i+2]=rand()%250-125;
   138 	}
   139 	
   140 	f.glViewport(0,0,640,480);
   141 	
   142 	f.glMatrixMode(GL_PROJECTION);
   143 	f.glLoadIdentity();
   144 	f.glOrtho(-100,100,-100,100,-500,500);
   145 	
   146 	f.glMatrixMode(GL_MODELVIEW);
   147 	f.glLoadIdentity();
   148 	
   149 	f.glEnable(GL_DEPTH_TEST);
   150 	f.glDisable(GL_TEXTURE_2D);
   151 	f.glEnable(GL_BLEND);
   152 	f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   153 	
   154 	f.glClearColor(0.0f,0.0f,0.0f,0.0f);
   155 	f.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
   156 	
   157 	f.glEnable(GL_POINT_SMOOTH);
   158 	f.glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);
   159 	f.glPointSize(5.0f);
   160 	f.glEnable(GL_FOG);
   161 	f.glFogf(GL_FOG_START,-500);
   162 	f.glFogf(GL_FOG_END,500);
   163 	f.glFogf(GL_FOG_DENSITY,0.005);
   164 	
   165 	do
   166 	{
   167 		f.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
   168 		
   169 		f.glRotatef(2.0,1.0,1.0,1.0);
   170 		f.glRotatef(1.0,0.0,1.0,1.0);
   171 		
   172 		f.glColor4ub(255,255,255,255);
   173 		f.glBegin(GL_POINTS);
   174 		for(i=0;i<NB_PIXELS;i++)
   175 		{
   176 			f.glVertex3f(pixels[3*i],pixels[3*i+1],pixels[3*i+2]);
   177 		}
   178 		f.glEnd();
   179 		SDL_GL_SwapBuffers();
   180 
   181 		while(SDL_PollEvent(&event))
   182 		{
   183 			if(event.type & SDL_KEYDOWN)
   184 				done=1;
   185 		}
   186 
   187 		SDL_Delay(20);
   188 	}
   189 	while(!done);
   190 	
   191 	SDL_Quit();
   192 	return 0;
   193 }
   194 
   195 #else /* HAVE_OPENGL */
   196 
   197 int main(int argc, char *argv[])
   198 {
   199 	printf("No OpenGL support on this system\n");
   200 	return 1;
   201 }
   202 
   203 #endif /* HAVE_OPENGL */