1 /* $Id: wgl.c,v 1.6 2004-05-20 02:04:29 btb Exp $ */
4 * opengl platform specific functions for WGL - added by Peter Hawkins
5 * fullscreen example code courtesy of Jeff Slutter
6 * everything merged together and cleaned up by Matt Mueller
7 * (with some win32 help from Nirvana)
28 /*#include "event.h"*/
31 //not defined in cygwin, needed to keep windows from resizing everything to fit the fullscreen res.
32 #ifndef CDS_FULLSCREEN
33 #define CDS_FULLSCREEN 0x4
36 DEVMODE devmode;//global so the activate proc can reset the screen mode.
40 extern int Inferno_verbose;
42 static int mouse_hidden=0;
45 //extern unsigned int key_wparam, key_lparam, key_msg;
46 void keyboard_handler();
47 extern int WMKey_Handler_Ready;
51 static int GLPREF_width,GLPREF_height;
52 static int GLSTATE_width,GLSTATE_height;
53 static bool GLPREF_windowed;
55 static HGLRC GL_ResourceContext=NULL;
56 //static WORD Saved_gamma_values[256*3];
57 bool OpenGL_Initialize(void);
58 void OpenGL_Shutdown(void);
61 void PumpMessages(void)
65 while (PeekMessage(&msg,NULL,0,0,PM_REMOVE|PM_NOYIELD))
67 TranslateMessage(&msg);
68 DispatchMessage(&msg);
71 static void finiObjects()
79 // DisableOpenGL( g_hWnd, hDC, hRC );
84 int wgl_game_activate(int active,int min){
85 int i=0;//do_game_activate(active,min);
86 if (!gl_initialized) return -1;
87 // printf("****** wgl_game_activate: %i %i,%i %i *******\n",GLPREF_windowed,active,min,vid_susp);
92 ChangeDisplaySettings(NULL,0);
99 ChangeDisplaySettings(&devmode,CDS_FULLSCREEN);
100 ShowWindow(g_hWnd,SW_SHOWNORMAL);
101 SetWindowPos(g_hWnd,HWND_TOPMOST,0,0,GLPREF_width,GLPREF_height,SWP_FRAMECHANGED);//if you don't set the pos, it comes up half off the screen, or just shows the desktop
102 UpdateWindow(g_hWnd);
103 wglMakeCurrent(NULL, NULL);
104 wglMakeCurrent(hDC,GL_ResourceContext);//this seems to fix some glitches that appear when you alt-tab back in.
108 // printf("****** -----------------: %i %i,%i %i *******\n",GLPREF_windowed,active,min,vid_susp);
111 long PASCAL DescentWndProc(HWND hWnd,UINT message,
112 WPARAM wParam,LPARAM lParam )
119 if (WMKey_Handler_Ready) {
120 // key_wparam=wParam; key_lparam=lParam; key_msg=message;
130 case WM_NCLBUTTONDOWN:
132 case WM_NCRBUTTONDOWN:
135 case WM_PALETTECHANGED:
136 case WM_PALETTEISCHANGING:
139 if (!GLPREF_windowed)
140 ShowWindow(g_hWnd, SW_SHOWMINNOACTIVE);//this minimizes the game after you alt-tab out.
143 wgl_game_activate((!(LOWORD(wParam) == WA_INACTIVE)),(BOOL) HIWORD(wParam));
146 // Win32_Key_Hook(wParam);
147 // DPH: This doesn't work... no idea why not...
154 return DefWindowProc(hWnd,message,wParam,lParam);
160 int ogl_setbrightness_internal(void)
162 return -1; // TODO: not yet implemented
165 void ogl_swap_buffers_internal(void){
169 int get_win_x_bs(void){
170 // return GetSystemMetrics(SM_CXBORDER)*2
171 return GetSystemMetrics(SM_CXFIXEDFRAME)*2;
173 int get_win_y_bs(void){
174 // return GetSystemMetrics(SM_CYBORDER)*2+GetSystemMetrics(SM_CYCAPTION);
175 return GetSystemMetrics(SM_CYFIXEDFRAME)*2+GetSystemMetrics(SM_CYCAPTION);
177 void win32_create_window(int x,int y)
181 WNDCLASS wcDescentClass;
184 Error("hInst=NULL\n");
187 wcDescentClass.lpszClassName = "WinD1X";
188 wcDescentClass.hInstance = hInst;
189 wcDescentClass.lpfnWndProc = DescentWndProc;
190 wcDescentClass.hCursor = LoadCursor(NULL, IDC_ARROW);
191 //wcDescentClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
192 wcDescentClass.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_MAIN_ICON));
193 wcDescentClass.lpszMenuName = NULL;
194 wcDescentClass.hbrBackground = NULL;
195 wcDescentClass.style = CS_OWNDC;
196 wcDescentClass.cbClsExtra = 0;
197 wcDescentClass.cbWndExtra = 0;
199 // Register the class
200 if (!RegisterClass(&wcDescentClass)){
201 // printf("RegisterClass==0?\n");
202 //always seems to return 0 after the first time, yet if you remove the call, it crashes. Heh.
206 flags=WS_POPUP;//uh.. don't remember if removing sysmenu did anything or not.. but it works.
207 //flags=WS_POPUP | WS_SYSMENU;
209 flags=WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;//let us minimize it.
210 //flags=WS_OVERLAPPED | WS_BORDER | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
212 if (!ogl_fullscreen){
213 x+=get_win_x_bs();y+=get_win_y_bs();
215 if (FindArg("-gl_test2")){
216 x=GetSystemMetrics(SM_CXSCREEN);
217 y=GetSystemMetrics(SM_CYSCREEN);
220 g_hWnd = CreateWindowEx(0,
232 if (!g_hWnd) Error("no window?\n");
233 ShowWindow(g_hWnd,SW_SHOWNORMAL);
234 UpdateWindow(g_hWnd);
245 joy_init(JOYSTICKID1);
246 if (!FindArg( "-nosound" ))
248 // printf("arch_init successfully completed\n");
252 void ogl_destroy_window(void){
254 ogl_smash_texture_list_internal();
263 if (!FindArg( "-nosound" ))
266 DestroyWindow(g_hWnd);
268 Error("ogl_destroy_window: no g_hWnd?\n");
274 void ogl_do_fullscreen_internal(void){
275 if (GLPREF_windowed==ogl_fullscreen){
276 ogl_destroy_window();
277 win32_create_window(GLPREF_width,GLPREF_height);
278 ogl_vivify_texture_list_internal();
283 int ogl_init_window(int x, int y){
287 if (GLSTATE_width==GLPREF_width && GLSTATE_height==GLPREF_height && GLPREF_windowed!=ogl_fullscreen)
288 return 0;//we are already in the right mode, don't do anything.
289 if (!ogl_fullscreen && GLPREF_windowed){
290 SetWindowPos(g_hWnd,0,0,0,x+get_win_x_bs(),y+get_win_y_bs(),SWP_NOMOVE);
292 ogl_destroy_window();
293 win32_create_window(x,y);
294 ogl_vivify_texture_list_internal();
297 win32_create_window(x,y);
302 hInst=GetModuleHandle (NULL);
304 void ogl_close(void){
305 ogl_destroy_window();
309 //windows opengl fullscreen changing - courtesy of Jeff Slutter
313 Windows Full Screen Setup
315 ===========================================================================
319 // Entering this function, the following values must be valid
320 // GLPREF_width,GLPREF_height: preferred width and height
321 // GLPREF_windowed: do we want windowed or full screen mode
322 // g_hWnd: handle to the window created for OpenGL
323 // On exit from this function (if returned true) the following values will be set
324 // GLSTATE_width,GLSTATE_height: real width and height of screen
325 // hDC: device context of the window
326 // GL_ResourceContext: OpenGL resource context
327 // Saved_gamma_values: Initial gamma values
328 bool OpenGL_Initialize(void)
333 PIXELFORMATDESCRIPTOR pfd;//, pfd_copy;
335 GLPREF_windowed=!ogl_fullscreen;
337 if (FindArg("-gl_test1")){
338 GLSTATE_width = GLPREF_width;
339 GLSTATE_height = GLPREF_height;
343 // First set our display mode
344 // Create direct draw surface
347 devmode.dmSize=sizeof(devmode);
348 devmode.dmBitsPerPel=16;
349 devmode.dmPelsWidth=GLPREF_width;
350 devmode.dmPelsHeight=GLPREF_height;
351 devmode.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
352 if ((retval=FindArg("-gl_refresh"))){
353 devmode.dmDisplayFrequency=atoi(Args[retval+1]);
354 if (devmode.dmDisplayFrequency>=60)//uhh, I hope no one actually wants a refresh lower than 60.. gag.
355 devmode.dmFields|=DM_DISPLAYFREQUENCY;
356 printf("trying refresh %li hz\n", devmode.dmDisplayFrequency);
359 retval=ChangeDisplaySettings(&devmode,CDS_FULLSCREEN);
361 if (retval!=DISP_CHANGE_SUCCESSFUL)
363 // we couldn't switch to the desired screen mode
364 // fall back to 640x480
366 devmode.dmBitsPerPel=16;
367 devmode.dmPelsWidth=640;
368 devmode.dmPelsHeight=480;
369 devmode.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
371 retval=ChangeDisplaySettings(&devmode,CDS_FULLSCREEN);
372 if (retval!=DISP_CHANGE_SUCCESSFUL)
374 errstr="ChangeDisplaySettings";
375 // we couldn't even switch to 640x480, we're out of here
376 // restore screen mode to default
377 ChangeDisplaySettings(NULL,0);
381 // successful, change our global settings to reflect what
388 // success at changing the video mode
395 // we want windowed mode, figure out how big the window is
397 GetWindowRect(g_hWnd,&rect);
398 width=abs(rect.right-rect.left);
399 height=abs(rect.bottom-rect.top);
403 // full screen mode, we want the window to be on top of everything
404 SetWindowPos(g_hWnd,HWND_TOPMOST,0,0,GLPREF_width,GLPREF_height,SWP_FRAMECHANGED);
406 height=GLPREF_height;
407 GetWindowRect(g_hWnd,&rect);
410 GLSTATE_width = width;
411 GLSTATE_height = height;
417 // Now we finally setup OpenGL
418 // If OpenGL is to be dynamically loaded, do this now (if the DLL isn't already
420 // remove the following error when you figure out what you want to do
421 // it's put here to make sure you notice this
422 #ifdef OGL_RUNTIME_LOAD
423 ogl_init_load_library();
426 // Setup our pixel format
428 memset(&pfd,0,sizeof(pfd));
429 pfd.nSize = sizeof(pfd);
431 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
432 // pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_GENERIC_ACCELERATED;
433 pfd.iPixelType = PFD_TYPE_RGBA;
434 //let the ogl driver decide. (fixes no hw accel in 16bit mode in w2k with tnt2)
435 // pfd.cColorBits = 16;
436 // pfd.cAlphaBits = 8;
437 // pfd.cDepthBits = 0;
438 // pfd.cAccumBits = 0;
439 // pfd.cStencilBits = 0;
440 pfd.iLayerType = PFD_MAIN_PLANE;
441 pfd.dwLayerMask = PFD_MAIN_PLANE;
444 // Find the user's "best match" PFD
445 pf = ChoosePixelFormat(hDC,&pfd);
448 errstr="ChoosePixelFormat";
449 // no pixel format closely matches
454 if(SetPixelFormat(hDC,pf,&pfd)==FALSE)
456 errstr="SetPixelFormat";
457 // unable to set the pixel format
461 // Now retrieve the PFD, we need to check some things
462 /* if(DescribePixelFormat(hDC,pf,sizeof(PIXELFORMATDESCRIPTOR),&pfd_copy)==0)
464 errstr="DescribePixelFormat";
465 // unable to get the PFD
469 // Make sure we are hardware accelerated
470 if((pfd_copy.dwFlags&PFD_GENERIC_ACCELERATED)==0&&(pfd_copy.dwFlags&PFD_GENERIC_FORMAT)!=0)
472 // we are not hardware accelerated!
476 // Finally, create our OpenGL context and make it current
477 GL_ResourceContext = wglCreateContext(hDC);
478 if(GL_ResourceContext==NULL)
480 errstr="wglCreateContext";
481 // we couldn't create a context!
485 // Make the context current
486 wglMakeCurrent(hDC,GL_ResourceContext);
488 // Save our gamma values because we'll probably be changing them,
489 // this way we can restore them on exit
491 // GetDeviceGammaRamp(hDC,(LPVOID)Saved_gamma_values);
498 Error("opengl init error: %s\n",errstr);
502 void OpenGL_Shutdown(void)
504 // Do any needed OpenGL shutdown here
507 // Now do Window specific shutdown
508 if(wglMakeCurrent)//check to make sure the function is valid (dyanmic loaded OpenGL)
509 wglMakeCurrent(NULL, NULL);
511 if(wglDeleteContext)//check to make sure the function is valid (dyanmic loaded OpenGL)
512 wglDeleteContext(GL_ResourceContext);
514 // Restore back to user screen settings
516 ChangeDisplaySettings(NULL,0);
518 // Restore gamma values
520 // SetDeviceGammaRamp(hDC,(LPVOID)Saved_gamma_values);
522 ReleaseDC(g_hWnd,hDC);