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