test/testdyngl.c
author Patrice Mandin <patmandin@gmail.com>
Thu, 19 Jan 2006 21:28:52 +0000
changeset 1257 448a9a64537b
parent 1151 be9c9c8f6d53
child 1386 2b7e0180a72c
permissions -rw-r--r--
[PATCH] SDL_GetVideoMode() does not find best mode, part 2

Following commit 1.51, I come accross a problem when SDL must choose between
several video modes that could suit the one asked.

If I ask 320x240 with this list:
768x480 768x240 640x400 640x200 384x480 384x240 320x400 320x200

The smallest selectables modes are 384x240 and 320x400. And SDL choose the later
in this list, but 384x240 is more suitable. So I added a check to compare
the pixel count (surface) of modes, and select the one which has the smallest
pixel count.

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