src/video/wincommon/SDL_wingl.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 31 Mar 2002 03:34:16 +0000
changeset 327 13fc64213765
parent 297 f6ffac90895c
child 373 c638fde8a824
permissions -rw-r--r--
*** empty log message ***
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@0
    39
slouken@0
    40
int WIN_GL_SetupWindow(_THIS)
slouken@0
    41
{
slouken@0
    42
	int retval;
slouken@0
    43
#ifdef HAVE_OPENGL
slouken@0
    44
	int pixel_format;
slouken@0
    45
slouken@0
    46
	/* load the gl driver from a default path */
slouken@0
    47
	if ( ! this->gl_config.driver_loaded ) {
slouken@0
    48
		/* no driver has been loaded, use default (ourselves) */
slouken@0
    49
		if ( WIN_GL_LoadLibrary(this, NULL) < 0 ) {
slouken@0
    50
			return(-1);
slouken@0
    51
		}
slouken@0
    52
	}
slouken@0
    53
slouken@0
    54
	/* Get the window device context for our OpenGL drawing */
slouken@0
    55
	GL_hdc = GetDC(SDL_Window);
slouken@0
    56
	if ( GL_hdc == NULL ) {
slouken@0
    57
		SDL_SetError("Unable to get DC for SDL_Window");
slouken@0
    58
		return(-1);
slouken@0
    59
	}
slouken@0
    60
slouken@0
    61
	/* Set up the pixel format descriptor with our needed format */
slouken@0
    62
	memset(&GL_pfd, 0, sizeof(GL_pfd));
slouken@0
    63
	GL_pfd.nSize = sizeof(GL_pfd);
slouken@0
    64
	GL_pfd.nVersion = 1;
slouken@0
    65
	GL_pfd.dwFlags = (PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL);
slouken@0
    66
	if ( this->gl_config.double_buffer ) {
slouken@0
    67
		GL_pfd.dwFlags |= PFD_DOUBLEBUFFER;
slouken@0
    68
	}
slouken@0
    69
	GL_pfd.iPixelType = PFD_TYPE_RGBA;
slouken@0
    70
	GL_pfd.cColorBits = this->gl_config.buffer_size;
slouken@0
    71
	GL_pfd.cRedBits = this->gl_config.red_size;
slouken@0
    72
	GL_pfd.cGreenBits = this->gl_config.green_size;
slouken@0
    73
	GL_pfd.cBlueBits = this->gl_config.blue_size;
slouken@0
    74
	GL_pfd.cAlphaBits = this->gl_config.alpha_size;
slouken@0
    75
	GL_pfd.cAccumRedBits = this->gl_config.accum_red_size;
slouken@0
    76
	GL_pfd.cAccumGreenBits = this->gl_config.accum_green_size;
slouken@0
    77
	GL_pfd.cAccumBlueBits = this->gl_config.accum_blue_size;
slouken@0
    78
	GL_pfd.cAccumAlphaBits = this->gl_config.accum_alpha_size;
slouken@0
    79
	GL_pfd.cAccumBits =
slouken@0
    80
		(GL_pfd.cAccumRedBits + GL_pfd.cAccumGreenBits +
slouken@0
    81
		 GL_pfd.cAccumBlueBits + GL_pfd.cAccumAlphaBits);
slouken@0
    82
	GL_pfd.cDepthBits = this->gl_config.depth_size;
slouken@0
    83
	GL_pfd.cStencilBits = this->gl_config.stencil_size;
slouken@0
    84
slouken@0
    85
	/* Choose and set the closest available pixel format */
slouken@0
    86
	pixel_format = ChoosePixelFormat(GL_hdc, &GL_pfd);
slouken@0
    87
	if ( !pixel_format ) {
slouken@0
    88
		SDL_SetError("No matching GL pixel format available");
slouken@0
    89
		return(-1);
slouken@0
    90
	}
slouken@0
    91
	if( !SetPixelFormat(GL_hdc, pixel_format, &GL_pfd) ) {
slouken@0
    92
		SDL_SetError("Unable to set HDC pixel format");
slouken@0
    93
		return(-1);
slouken@0
    94
	}
slouken@0
    95
	DescribePixelFormat(GL_hdc, pixel_format, sizeof(GL_pfd), &GL_pfd);
slouken@0
    96
slouken@0
    97
	GL_hrc = this->gl_data->wglCreateContext(GL_hdc);
slouken@0
    98
	if( GL_hrc == NULL ) {
slouken@0
    99
		SDL_SetError("Unable to create GL context");
slouken@0
   100
		return(-1);
slouken@0
   101
	}
slouken@0
   102
	gl_active = 1;
slouken@0
   103
#else
slouken@0
   104
	SDL_SetError("WIN driver not configured with OpenGL");
slouken@0
   105
#endif
slouken@0
   106
	if ( gl_active ) {
slouken@0
   107
		retval = 0;
slouken@0
   108
	} else {
slouken@0
   109
		retval = -1;
slouken@0
   110
	}
slouken@0
   111
	return(retval);
slouken@0
   112
}
slouken@0
   113
slouken@0
   114
void WIN_GL_ShutDown(_THIS)
slouken@0
   115
{
slouken@0
   116
#ifdef HAVE_OPENGL
slouken@0
   117
	/* Clean up OpenGL */
slouken@0
   118
	if ( GL_hrc ) {
slouken@0
   119
		this->gl_data->wglMakeCurrent(NULL, NULL);
slouken@0
   120
		this->gl_data->wglDeleteContext(GL_hrc);
slouken@0
   121
		GL_hrc = NULL;
slouken@0
   122
	}
slouken@0
   123
	if ( GL_hdc ) {
slouken@0
   124
		ReleaseDC(SDL_Window, GL_hdc);
slouken@0
   125
		GL_hdc = NULL;
slouken@0
   126
	}
slouken@0
   127
	gl_active = 0;
slouken@0
   128
slouken@0
   129
	WIN_GL_UnloadLibrary(this);
slouken@0
   130
#endif /* HAVE_OPENGL */
slouken@0
   131
}
slouken@0
   132
slouken@0
   133
#ifdef HAVE_OPENGL
slouken@0
   134
slouken@0
   135
/* Make the current context active */
slouken@0
   136
int WIN_GL_MakeCurrent(_THIS)
slouken@0
   137
{
slouken@0
   138
	int retval;
slouken@0
   139
slouken@0
   140
	retval = 0;
slouken@0
   141
	if ( ! this->gl_data->wglMakeCurrent(GL_hdc, GL_hrc) ) {
slouken@0
   142
		SDL_SetError("Unable to make GL context current");
slouken@0
   143
		retval = -1;
slouken@0
   144
	}
slouken@0
   145
	return(retval);
slouken@0
   146
}
slouken@0
   147
slouken@0
   148
/* Get attribute data from glX. */
slouken@0
   149
int WIN_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
slouken@0
   150
{
slouken@0
   151
	int retval;
slouken@0
   152
slouken@0
   153
	retval = 0;
slouken@0
   154
	switch( attrib ) {
slouken@0
   155
	    case SDL_GL_RED_SIZE:
slouken@0
   156
		*value = GL_pfd.cRedBits;
slouken@0
   157
		break;
slouken@0
   158
	    case SDL_GL_GREEN_SIZE:
slouken@0
   159
		*value = GL_pfd.cGreenBits;
slouken@0
   160
		break;
slouken@0
   161
	    case SDL_GL_BLUE_SIZE:
slouken@0
   162
		*value = GL_pfd.cBlueBits;
slouken@0
   163
		break;
slouken@0
   164
	    case SDL_GL_ALPHA_SIZE:
slouken@0
   165
		*value = GL_pfd.cAlphaBits;
slouken@0
   166
		break;
slouken@0
   167
	    case SDL_GL_DOUBLEBUFFER:
slouken@0
   168
		if ( GL_pfd.dwFlags & PFD_DOUBLEBUFFER ) {
slouken@0
   169
			*value = 1;
slouken@0
   170
		} else {
slouken@0
   171
			*value = 0;
slouken@0
   172
		}
slouken@0
   173
		break;
slouken@0
   174
	    case SDL_GL_BUFFER_SIZE:
slouken@0
   175
		*value = GL_pfd.cColorBits;
slouken@0
   176
		break;
slouken@0
   177
	    case SDL_GL_DEPTH_SIZE:
slouken@0
   178
		*value = GL_pfd.cDepthBits;
slouken@0
   179
		break;
slouken@0
   180
	    case SDL_GL_STENCIL_SIZE:
slouken@0
   181
		*value = GL_pfd.cStencilBits;
slouken@0
   182
		break;
slouken@0
   183
	    case SDL_GL_ACCUM_RED_SIZE:
slouken@0
   184
		*value = GL_pfd.cAccumRedBits;
slouken@0
   185
		break;
slouken@0
   186
	    case SDL_GL_ACCUM_GREEN_SIZE:
slouken@0
   187
		*value = GL_pfd.cAccumGreenBits;
slouken@0
   188
		break;
slouken@0
   189
	    case SDL_GL_ACCUM_BLUE_SIZE:
slouken@0
   190
		*value = GL_pfd.cAccumBlueBits;
slouken@0
   191
		break;
slouken@0
   192
	    case SDL_GL_ACCUM_ALPHA_SIZE:
slouken@0
   193
		*value = GL_pfd.cAccumAlphaBits;
slouken@0
   194
		break;
slouken@0
   195
	    default:
slouken@0
   196
		retval = -1;
slouken@0
   197
		break;
slouken@0
   198
	}
slouken@0
   199
	return retval;
slouken@0
   200
}
slouken@0
   201
slouken@0
   202
void WIN_GL_SwapBuffers(_THIS)
slouken@0
   203
{
slouken@0
   204
	SwapBuffers(GL_hdc);
slouken@0
   205
}
slouken@0
   206
slouken@0
   207
#endif /* HAVE_OPENGL */
slouken@0
   208
slouken@0
   209
#ifdef HAVE_OPENGL
slouken@0
   210
slouken@0
   211
void WIN_GL_UnloadLibrary(_THIS)
slouken@0
   212
{
slouken@0
   213
	if ( this->gl_config.driver_loaded ) {
slouken@0
   214
		FreeLibrary((HMODULE)this->gl_config.dll_handle);
slouken@0
   215
slouken@0
   216
		this->gl_data->wglGetProcAddress = NULL;
slouken@0
   217
		this->gl_data->wglCreateContext = NULL;
slouken@0
   218
		this->gl_data->wglDeleteContext = NULL;
slouken@0
   219
		this->gl_data->wglMakeCurrent = NULL;
slouken@0
   220
slouken@0
   221
		this->gl_config.dll_handle = NULL;
slouken@0
   222
		this->gl_config.driver_loaded = 0;
slouken@0
   223
	}
slouken@0
   224
}
slouken@0
   225
slouken@0
   226
/* Passing a NULL path means load pointers from the application */
slouken@0
   227
int WIN_GL_LoadLibrary(_THIS, const char* path) 
slouken@0
   228
{
slouken@0
   229
	HMODULE handle;
slouken@0
   230
slouken@0
   231
 	if ( gl_active ) {
slouken@0
   232
 		SDL_SetError("OpenGL context already created");
slouken@0
   233
 		return -1;
slouken@0
   234
 	}
slouken@0
   235
slouken@0
   236
	if ( path == NULL ) {
slouken@0
   237
		path = DEFAULT_GL_DRIVER_PATH;
slouken@0
   238
	}
slouken@0
   239
	handle = LoadLibrary(path);
slouken@0
   240
	if ( handle == NULL ) {
slouken@0
   241
		SDL_SetError("Could not load OpenGL library");
slouken@0
   242
		return -1;
slouken@0
   243
	}
slouken@0
   244
slouken@0
   245
	/* Unload the old driver and reset the pointers */
slouken@0
   246
	WIN_GL_UnloadLibrary(this);
slouken@0
   247
slouken@0
   248
	/* Load new function pointers */
slouken@0
   249
	this->gl_data->wglGetProcAddress = (void * (WINAPI *)(const char *))
slouken@0
   250
		GetProcAddress(handle, "wglGetProcAddress");
slouken@0
   251
	this->gl_data->wglCreateContext = (HGLRC (WINAPI *)(HDC))
slouken@0
   252
		GetProcAddress(handle, "wglCreateContext");
slouken@0
   253
	this->gl_data->wglDeleteContext = (BOOL (WINAPI *)(HGLRC))
slouken@0
   254
		GetProcAddress(handle, "wglDeleteContext");
slouken@0
   255
	this->gl_data->wglMakeCurrent = (BOOL (WINAPI *)(HDC, HGLRC))
slouken@0
   256
		GetProcAddress(handle, "wglMakeCurrent");
slouken@0
   257
slouken@0
   258
	if ( (this->gl_data->wglGetProcAddress == NULL) ||
slouken@0
   259
	     (this->gl_data->wglCreateContext == NULL) ||
slouken@0
   260
	     (this->gl_data->wglDeleteContext == NULL) ||
slouken@0
   261
	     (this->gl_data->wglMakeCurrent == NULL) ) {
slouken@0
   262
		SDL_SetError("Could not retrieve OpenGL functions");
slouken@0
   263
		FreeLibrary(handle);
slouken@0
   264
		return -1;
slouken@0
   265
	}
slouken@0
   266
slouken@0
   267
	this->gl_config.dll_handle = handle;
slouken@0
   268
	strcpy(this->gl_config.driver_path, path);
slouken@0
   269
	this->gl_config.driver_loaded = 1;
slouken@0
   270
	return 0;
slouken@0
   271
}
slouken@0
   272
slouken@0
   273
void *WIN_GL_GetProcAddress(_THIS, const char* proc)
slouken@0
   274
{
slouken@0
   275
	void *func;
slouken@0
   276
slouken@0
   277
	/* This is to pick up extensions */
slouken@0
   278
	func = this->gl_data->wglGetProcAddress(proc);
slouken@0
   279
	if ( ! func ) {
slouken@0
   280
		/* This is probably a normal GL function */
slouken@0
   281
		func = GetProcAddress(this->gl_config.dll_handle, proc);
slouken@0
   282
	}
slouken@0
   283
	return func;
slouken@0
   284
}
slouken@0
   285
slouken@0
   286
#endif /* HAVE_OPENGL */