src/video/wincommon/SDL_wingl.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 19 Aug 2002 17:58:08 +0000
changeset 450 8a43e0cbf02f
parent 373 c638fde8a824
child 453 a6fa62b1be09
permissions -rw-r--r--
Added SDL_GL_STEREO for stereoscopic OpenGL contexts
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@297
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
/* WGL implementation of SDL OpenGL support */
slouken@0
    29
slouken@327
    30
#include "SDL_opengl.h"
slouken@0
    31
#include "SDL_error.h"
slouken@0
    32
#include "SDL_lowvideo.h"
slouken@0
    33
#include "SDL_wingl_c.h"
slouken@0
    34
slouken@0
    35
#ifdef HAVE_OPENGL
slouken@0
    36
#define DEFAULT_GL_DRIVER_PATH "OPENGL32.DLL"
slouken@0
    37
#endif
slouken@0
    38
slouken@373
    39
/* If setting the HDC fails, we may need to recreate the window (MSDN) */
slouken@373
    40
static int WIN_GL_ResetWindow(_THIS)
slouken@373
    41
{
slouken@373
    42
	int status = 0;
slouken@373
    43
	int can_reset = 1;
slouken@373
    44
slouken@373
    45
	/* If we were passed a window, then we can't create a new one */
slouken@373
    46
	if ( SDL_windowid ) {
slouken@373
    47
		can_reset = 0;
slouken@373
    48
	}
slouken@373
    49
#ifndef _WIN32_WCE /* FIXME WinCE needs the UNICODE version of CreateWindow() */
slouken@373
    50
	if ( can_reset ) {
slouken@373
    51
		/* Save the existing window attributes */
slouken@373
    52
		LONG style;
slouken@373
    53
		RECT rect = { 0, 0, 0, 0 };
slouken@373
    54
		style = GetWindowLong(SDL_Window, GWL_STYLE);
slouken@373
    55
		GetWindowRect(SDL_Window, &rect);
slouken@373
    56
		DestroyWindow(SDL_Window);
slouken@373
    57
		SDL_Window = CreateWindow(SDL_Appname, SDL_Appname,
slouken@373
    58
		                          style,
slouken@373
    59
					  rect.left, rect.top,
slouken@373
    60
                                          (rect.right-rect.left)+1,
slouken@373
    61
                                          (rect.top-rect.bottom)+1,
slouken@373
    62
		                          NULL, NULL, SDL_Instance, NULL);
slouken@373
    63
		if ( SDL_Window ) {
slouken@373
    64
			this->SetCaption(this, this->wm_title, this->wm_icon);
slouken@373
    65
		} else {
slouken@373
    66
			SDL_SetError("Couldn't create window");
slouken@373
    67
			status = -1;
slouken@373
    68
		}
slouken@373
    69
	} else
slouken@373
    70
#endif /* !_WIN32_WCE */
slouken@373
    71
	{
slouken@373
    72
		SDL_SetError("Unable to reset window for OpenGL context");
slouken@373
    73
		status = -1;
slouken@373
    74
	}
slouken@373
    75
	return(status);
slouken@373
    76
}
slouken@0
    77
slouken@0
    78
int WIN_GL_SetupWindow(_THIS)
slouken@0
    79
{
slouken@0
    80
	int retval;
slouken@0
    81
#ifdef HAVE_OPENGL
slouken@373
    82
	int i;
slouken@0
    83
	int pixel_format;
slouken@0
    84
slouken@0
    85
	/* load the gl driver from a default path */
slouken@0
    86
	if ( ! this->gl_config.driver_loaded ) {
slouken@0
    87
		/* no driver has been loaded, use default (ourselves) */
slouken@0
    88
		if ( WIN_GL_LoadLibrary(this, NULL) < 0 ) {
slouken@0
    89
			return(-1);
slouken@0
    90
		}
slouken@0
    91
	}
slouken@0
    92
slouken@373
    93
	for ( i=0; ; ++i ) {
slouken@373
    94
		/* Get the window device context for our OpenGL drawing */
slouken@373
    95
		GL_hdc = GetDC(SDL_Window);
slouken@373
    96
		if ( GL_hdc == NULL ) {
slouken@373
    97
			SDL_SetError("Unable to get DC for SDL_Window");
slouken@373
    98
			return(-1);
slouken@373
    99
		}
slouken@0
   100
slouken@373
   101
		/* Set up the pixel format descriptor with our needed format */
slouken@373
   102
		memset(&GL_pfd, 0, sizeof(GL_pfd));
slouken@373
   103
		GL_pfd.nSize = sizeof(GL_pfd);
slouken@373
   104
		GL_pfd.nVersion = 1;
slouken@373
   105
		GL_pfd.dwFlags = (PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL);
slouken@373
   106
		if ( this->gl_config.double_buffer ) {
slouken@373
   107
			GL_pfd.dwFlags |= PFD_DOUBLEBUFFER;
slouken@373
   108
		}
slouken@450
   109
		if ( this->gl_config.stereo ) {
slouken@450
   110
			GL_pfd.dwFlags |= PFD_STEREO;
slouken@450
   111
		}
slouken@373
   112
		GL_pfd.iPixelType = PFD_TYPE_RGBA;
slouken@373
   113
		GL_pfd.cColorBits = this->gl_config.buffer_size;
slouken@373
   114
		GL_pfd.cRedBits = this->gl_config.red_size;
slouken@373
   115
		GL_pfd.cGreenBits = this->gl_config.green_size;
slouken@373
   116
		GL_pfd.cBlueBits = this->gl_config.blue_size;
slouken@373
   117
		GL_pfd.cAlphaBits = this->gl_config.alpha_size;
slouken@373
   118
		GL_pfd.cAccumRedBits = this->gl_config.accum_red_size;
slouken@373
   119
		GL_pfd.cAccumGreenBits = this->gl_config.accum_green_size;
slouken@373
   120
		GL_pfd.cAccumBlueBits = this->gl_config.accum_blue_size;
slouken@373
   121
		GL_pfd.cAccumAlphaBits = this->gl_config.accum_alpha_size;
slouken@373
   122
		GL_pfd.cAccumBits =
slouken@373
   123
			(GL_pfd.cAccumRedBits + GL_pfd.cAccumGreenBits +
slouken@373
   124
			 GL_pfd.cAccumBlueBits + GL_pfd.cAccumAlphaBits);
slouken@373
   125
		GL_pfd.cDepthBits = this->gl_config.depth_size;
slouken@373
   126
		GL_pfd.cStencilBits = this->gl_config.stencil_size;
slouken@0
   127
slouken@373
   128
		/* Choose and set the closest available pixel format */
slouken@373
   129
		pixel_format = ChoosePixelFormat(GL_hdc, &GL_pfd);
slouken@373
   130
		if ( !pixel_format ) {
slouken@373
   131
			SDL_SetError("No matching GL pixel format available");
slouken@373
   132
			return(-1);
slouken@373
   133
		}
slouken@373
   134
		if( !SetPixelFormat(GL_hdc, pixel_format, &GL_pfd) ) {
slouken@373
   135
			if ( i == 0 ) {
slouken@373
   136
				/* First time through, try resetting the window */
slouken@373
   137
				if ( WIN_GL_ResetWindow(this) < 0 ) {
slouken@373
   138
					return(-1);
slouken@373
   139
				}
slouken@373
   140
				continue;
slouken@373
   141
			}
slouken@373
   142
			SDL_SetError("Unable to set HDC pixel format");
slouken@373
   143
			return(-1);
slouken@373
   144
		}
slouken@373
   145
		/* We either succeeded or failed by this point */
slouken@373
   146
		break;
slouken@0
   147
	}
slouken@0
   148
	DescribePixelFormat(GL_hdc, pixel_format, sizeof(GL_pfd), &GL_pfd);
slouken@0
   149
slouken@0
   150
	GL_hrc = this->gl_data->wglCreateContext(GL_hdc);
slouken@0
   151
	if( GL_hrc == NULL ) {
slouken@0
   152
		SDL_SetError("Unable to create GL context");
slouken@0
   153
		return(-1);
slouken@0
   154
	}
slouken@0
   155
	gl_active = 1;
slouken@0
   156
#else
slouken@0
   157
	SDL_SetError("WIN driver not configured with OpenGL");
slouken@0
   158
#endif
slouken@0
   159
	if ( gl_active ) {
slouken@0
   160
		retval = 0;
slouken@0
   161
	} else {
slouken@0
   162
		retval = -1;
slouken@0
   163
	}
slouken@0
   164
	return(retval);
slouken@0
   165
}
slouken@0
   166
slouken@0
   167
void WIN_GL_ShutDown(_THIS)
slouken@0
   168
{
slouken@0
   169
#ifdef HAVE_OPENGL
slouken@0
   170
	/* Clean up OpenGL */
slouken@0
   171
	if ( GL_hrc ) {
slouken@0
   172
		this->gl_data->wglMakeCurrent(NULL, NULL);
slouken@0
   173
		this->gl_data->wglDeleteContext(GL_hrc);
slouken@0
   174
		GL_hrc = NULL;
slouken@0
   175
	}
slouken@0
   176
	if ( GL_hdc ) {
slouken@0
   177
		ReleaseDC(SDL_Window, GL_hdc);
slouken@0
   178
		GL_hdc = NULL;
slouken@0
   179
	}
slouken@0
   180
	gl_active = 0;
slouken@0
   181
slouken@0
   182
	WIN_GL_UnloadLibrary(this);
slouken@0
   183
#endif /* HAVE_OPENGL */
slouken@0
   184
}
slouken@0
   185
slouken@0
   186
#ifdef HAVE_OPENGL
slouken@0
   187
slouken@0
   188
/* Make the current context active */
slouken@0
   189
int WIN_GL_MakeCurrent(_THIS)
slouken@0
   190
{
slouken@0
   191
	int retval;
slouken@0
   192
slouken@0
   193
	retval = 0;
slouken@0
   194
	if ( ! this->gl_data->wglMakeCurrent(GL_hdc, GL_hrc) ) {
slouken@0
   195
		SDL_SetError("Unable to make GL context current");
slouken@0
   196
		retval = -1;
slouken@0
   197
	}
slouken@0
   198
	return(retval);
slouken@0
   199
}
slouken@0
   200
slouken@0
   201
/* Get attribute data from glX. */
slouken@0
   202
int WIN_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
slouken@0
   203
{
slouken@0
   204
	int retval;
slouken@0
   205
slouken@0
   206
	retval = 0;
slouken@0
   207
	switch( attrib ) {
slouken@0
   208
	    case SDL_GL_RED_SIZE:
slouken@0
   209
		*value = GL_pfd.cRedBits;
slouken@0
   210
		break;
slouken@0
   211
	    case SDL_GL_GREEN_SIZE:
slouken@0
   212
		*value = GL_pfd.cGreenBits;
slouken@0
   213
		break;
slouken@0
   214
	    case SDL_GL_BLUE_SIZE:
slouken@0
   215
		*value = GL_pfd.cBlueBits;
slouken@0
   216
		break;
slouken@0
   217
	    case SDL_GL_ALPHA_SIZE:
slouken@0
   218
		*value = GL_pfd.cAlphaBits;
slouken@0
   219
		break;
slouken@0
   220
	    case SDL_GL_DOUBLEBUFFER:
slouken@0
   221
		if ( GL_pfd.dwFlags & PFD_DOUBLEBUFFER ) {
slouken@0
   222
			*value = 1;
slouken@0
   223
		} else {
slouken@0
   224
			*value = 0;
slouken@0
   225
		}
slouken@0
   226
		break;
slouken@0
   227
	    case SDL_GL_BUFFER_SIZE:
slouken@0
   228
		*value = GL_pfd.cColorBits;
slouken@0
   229
		break;
slouken@0
   230
	    case SDL_GL_DEPTH_SIZE:
slouken@0
   231
		*value = GL_pfd.cDepthBits;
slouken@0
   232
		break;
slouken@0
   233
	    case SDL_GL_STENCIL_SIZE:
slouken@0
   234
		*value = GL_pfd.cStencilBits;
slouken@0
   235
		break;
slouken@0
   236
	    case SDL_GL_ACCUM_RED_SIZE:
slouken@0
   237
		*value = GL_pfd.cAccumRedBits;
slouken@0
   238
		break;
slouken@0
   239
	    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@0
   240
		*value = GL_pfd.cAccumGreenBits;
slouken@0
   241
		break;
slouken@0
   242
	    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@0
   243
		*value = GL_pfd.cAccumBlueBits;
slouken@0
   244
		break;
slouken@0
   245
	    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@0
   246
		*value = GL_pfd.cAccumAlphaBits;
slouken@0
   247
		break;
slouken@450
   248
	    case SDL_GL_STEREO:
slouken@450
   249
		if ( GL_pfd.dwFlags & PFD_STEREO ) {
slouken@450
   250
			*value = 1;
slouken@450
   251
		} else {
slouken@450
   252
			*value = 0;
slouken@450
   253
		}
slouken@450
   254
		break;
slouken@0
   255
	    default:
slouken@0
   256
		retval = -1;
slouken@0
   257
		break;
slouken@0
   258
	}
slouken@0
   259
	return retval;
slouken@0
   260
}
slouken@0
   261
slouken@0
   262
void WIN_GL_SwapBuffers(_THIS)
slouken@0
   263
{
slouken@0
   264
	SwapBuffers(GL_hdc);
slouken@0
   265
}
slouken@0
   266
slouken@0
   267
#endif /* HAVE_OPENGL */
slouken@0
   268
slouken@0
   269
#ifdef HAVE_OPENGL
slouken@0
   270
slouken@0
   271
void WIN_GL_UnloadLibrary(_THIS)
slouken@0
   272
{
slouken@0
   273
	if ( this->gl_config.driver_loaded ) {
slouken@0
   274
		FreeLibrary((HMODULE)this->gl_config.dll_handle);
slouken@0
   275
slouken@0
   276
		this->gl_data->wglGetProcAddress = NULL;
slouken@0
   277
		this->gl_data->wglCreateContext = NULL;
slouken@0
   278
		this->gl_data->wglDeleteContext = NULL;
slouken@0
   279
		this->gl_data->wglMakeCurrent = NULL;
slouken@0
   280
slouken@0
   281
		this->gl_config.dll_handle = NULL;
slouken@0
   282
		this->gl_config.driver_loaded = 0;
slouken@0
   283
	}
slouken@0
   284
}
slouken@0
   285
slouken@0
   286
/* Passing a NULL path means load pointers from the application */
slouken@0
   287
int WIN_GL_LoadLibrary(_THIS, const char* path) 
slouken@0
   288
{
slouken@0
   289
	HMODULE handle;
slouken@0
   290
slouken@0
   291
 	if ( gl_active ) {
slouken@0
   292
 		SDL_SetError("OpenGL context already created");
slouken@0
   293
 		return -1;
slouken@0
   294
 	}
slouken@0
   295
slouken@0
   296
	if ( path == NULL ) {
slouken@0
   297
		path = DEFAULT_GL_DRIVER_PATH;
slouken@0
   298
	}
slouken@0
   299
	handle = LoadLibrary(path);
slouken@0
   300
	if ( handle == NULL ) {
slouken@0
   301
		SDL_SetError("Could not load OpenGL library");
slouken@0
   302
		return -1;
slouken@0
   303
	}
slouken@0
   304
slouken@0
   305
	/* Unload the old driver and reset the pointers */
slouken@0
   306
	WIN_GL_UnloadLibrary(this);
slouken@0
   307
slouken@0
   308
	/* Load new function pointers */
slouken@0
   309
	this->gl_data->wglGetProcAddress = (void * (WINAPI *)(const char *))
slouken@0
   310
		GetProcAddress(handle, "wglGetProcAddress");
slouken@0
   311
	this->gl_data->wglCreateContext = (HGLRC (WINAPI *)(HDC))
slouken@0
   312
		GetProcAddress(handle, "wglCreateContext");
slouken@0
   313
	this->gl_data->wglDeleteContext = (BOOL (WINAPI *)(HGLRC))
slouken@0
   314
		GetProcAddress(handle, "wglDeleteContext");
slouken@0
   315
	this->gl_data->wglMakeCurrent = (BOOL (WINAPI *)(HDC, HGLRC))
slouken@0
   316
		GetProcAddress(handle, "wglMakeCurrent");
slouken@0
   317
slouken@0
   318
	if ( (this->gl_data->wglGetProcAddress == NULL) ||
slouken@0
   319
	     (this->gl_data->wglCreateContext == NULL) ||
slouken@0
   320
	     (this->gl_data->wglDeleteContext == NULL) ||
slouken@0
   321
	     (this->gl_data->wglMakeCurrent == NULL) ) {
slouken@0
   322
		SDL_SetError("Could not retrieve OpenGL functions");
slouken@0
   323
		FreeLibrary(handle);
slouken@0
   324
		return -1;
slouken@0
   325
	}
slouken@0
   326
slouken@0
   327
	this->gl_config.dll_handle = handle;
slouken@0
   328
	strcpy(this->gl_config.driver_path, path);
slouken@0
   329
	this->gl_config.driver_loaded = 1;
slouken@0
   330
	return 0;
slouken@0
   331
}
slouken@0
   332
slouken@0
   333
void *WIN_GL_GetProcAddress(_THIS, const char* proc)
slouken@0
   334
{
slouken@0
   335
	void *func;
slouken@0
   336
slouken@0
   337
	/* This is to pick up extensions */
slouken@0
   338
	func = this->gl_data->wglGetProcAddress(proc);
slouken@0
   339
	if ( ! func ) {
slouken@0
   340
		/* This is probably a normal GL function */
slouken@0
   341
		func = GetProcAddress(this->gl_config.dll_handle, proc);
slouken@0
   342
	}
slouken@0
   343
	return func;
slouken@0
   344
}
slouken@0
   345
slouken@0
   346
#endif /* HAVE_OPENGL */