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