3 * opengl platform specific functions for WGL - added by Peter Hawkins
4 * fullscreen example code courtesy of Jeff Slutter
5 * everything merged together and cleaned up by Matt Mueller
6 * (with some win32 help from Nirvana)
27 /*#include "event.h"*/
30 //not defined in cygwin, needed to keep windows from resizing everything to fit the fullscreen res.
31 #ifndef CDS_FULLSCREEN
32 #define CDS_FULLSCREEN 0x4
35 DEVMODE devmode;//global so the activate proc can reset the screen mode.
39 extern int Inferno_verbose;
41 static int mouse_hidden=0;
44 //extern unsigned int key_wparam, key_lparam, key_msg;
45 void keyboard_handler();
46 extern int WMKey_Handler_Ready;
50 static int GLPREF_width,GLPREF_height;
51 static int GLSTATE_width,GLSTATE_height;
52 static bool GLPREF_windowed;
54 static HGLRC GL_ResourceContext=NULL;
55 static WORD Saved_gamma_values[256 * 3];
56 static WORD gammaramp[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)
164 for (i = 0; i < 256; ++i)
166 gammaramp[i] = min((i + ogl_brightness_r * 4) * 256, 0xffff);
167 gammaramp[i + 256] = min((i + ogl_brightness_g * 4) * 256, 0xffff);
168 gammaramp[i + 512] = min((i + ogl_brightness_b * 4) * 256, 0xffff);
171 return SetDeviceGammaRamp(hDC, (LPVOID)gammaramp) ? 0 : -1;
174 void ogl_swap_buffers_internal(void){
178 int get_win_x_bs(void){
179 // return GetSystemMetrics(SM_CXBORDER)*2
180 return GetSystemMetrics(SM_CXFIXEDFRAME)*2;
182 int get_win_y_bs(void){
183 // return GetSystemMetrics(SM_CYBORDER)*2+GetSystemMetrics(SM_CYCAPTION);
184 return GetSystemMetrics(SM_CYFIXEDFRAME)*2+GetSystemMetrics(SM_CYCAPTION);
186 void win32_create_window(int x,int y)
190 WNDCLASS wcDescentClass;
193 Error("hInst=NULL\n");
196 wcDescentClass.lpszClassName = "WinD1X";
197 wcDescentClass.hInstance = hInst;
198 wcDescentClass.lpfnWndProc = DescentWndProc;
199 wcDescentClass.hCursor = LoadCursor(NULL, IDC_ARROW);
200 //wcDescentClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
201 wcDescentClass.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_MAIN_ICON));
202 wcDescentClass.lpszMenuName = NULL;
203 wcDescentClass.hbrBackground = NULL;
204 wcDescentClass.style = CS_OWNDC;
205 wcDescentClass.cbClsExtra = 0;
206 wcDescentClass.cbWndExtra = 0;
208 // Register the class
209 if (!RegisterClass(&wcDescentClass)){
210 // printf("RegisterClass==0?\n");
211 //always seems to return 0 after the first time, yet if you remove the call, it crashes. Heh.
215 flags=WS_POPUP;//uh.. don't remember if removing sysmenu did anything or not.. but it works.
216 //flags=WS_POPUP | WS_SYSMENU;
218 flags=WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;//let us minimize it.
219 //flags=WS_OVERLAPPED | WS_BORDER | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
221 if (!ogl_fullscreen){
222 x+=get_win_x_bs();y+=get_win_y_bs();
224 if (FindArg("-gl_test2")){
225 x=GetSystemMetrics(SM_CXSCREEN);
226 y=GetSystemMetrics(SM_CYSCREEN);
229 g_hWnd = CreateWindowEx(0,
241 if (!g_hWnd) Error("no window?\n");
242 ShowWindow(g_hWnd,SW_SHOWNORMAL);
243 UpdateWindow(g_hWnd);
254 joy_init(JOYSTICKID1);
255 if (!FindArg( "-nosound" ))
257 // printf("arch_init successfully completed\n");
261 void ogl_destroy_window(void){
263 ogl_smash_texture_list_internal();
272 if (!FindArg( "-nosound" ))
275 DestroyWindow(g_hWnd);
277 Error("ogl_destroy_window: no g_hWnd?\n");
283 void ogl_do_fullscreen_internal(void){
284 if (GLPREF_windowed==ogl_fullscreen){
285 ogl_destroy_window();
286 win32_create_window(GLPREF_width,GLPREF_height);
287 ogl_vivify_texture_list_internal();
292 int ogl_init_window(int x, int y){
296 if (GLSTATE_width==GLPREF_width && GLSTATE_height==GLPREF_height && GLPREF_windowed!=ogl_fullscreen)
297 return 0;//we are already in the right mode, don't do anything.
298 if (!ogl_fullscreen && GLPREF_windowed){
299 SetWindowPos(g_hWnd,0,0,0,x+get_win_x_bs(),y+get_win_y_bs(),SWP_NOMOVE);
301 ogl_destroy_window();
302 win32_create_window(x,y);
303 ogl_vivify_texture_list_internal();
306 win32_create_window(x,y);
311 hInst=GetModuleHandle (NULL);
313 void ogl_close(void){
314 ogl_destroy_window();
318 //windows opengl fullscreen changing - courtesy of Jeff Slutter
322 Windows Full Screen Setup
324 ===========================================================================
328 // Entering this function, the following values must be valid
329 // GLPREF_width,GLPREF_height: preferred width and height
330 // GLPREF_windowed: do we want windowed or full screen mode
331 // g_hWnd: handle to the window created for OpenGL
332 // On exit from this function (if returned true) the following values will be set
333 // GLSTATE_width,GLSTATE_height: real width and height of screen
334 // hDC: device context of the window
335 // GL_ResourceContext: OpenGL resource context
336 // Saved_gamma_values: Initial gamma values
337 bool OpenGL_Initialize(void)
342 PIXELFORMATDESCRIPTOR pfd;//, pfd_copy;
344 GLPREF_windowed=!ogl_fullscreen;
346 if (FindArg("-gl_test1")){
347 GLSTATE_width = GLPREF_width;
348 GLSTATE_height = GLPREF_height;
352 // First set our display mode
353 // Create direct draw surface
356 devmode.dmSize=sizeof(devmode);
357 devmode.dmBitsPerPel = FindArg("-gl_16bpp") ? 16 : 32;
358 devmode.dmPelsWidth=GLPREF_width;
359 devmode.dmPelsHeight=GLPREF_height;
360 devmode.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
361 if ((retval=FindArg("-gl_refresh"))){
362 devmode.dmDisplayFrequency=atoi(Args[retval+1]);
363 if (devmode.dmDisplayFrequency>=60)//uhh, I hope no one actually wants a refresh lower than 60.. gag.
364 devmode.dmFields|=DM_DISPLAYFREQUENCY;
365 printf("trying refresh %li hz\n", devmode.dmDisplayFrequency);
368 retval=ChangeDisplaySettings(&devmode,CDS_FULLSCREEN);
370 if (retval!=DISP_CHANGE_SUCCESSFUL)
372 // we couldn't switch to the desired screen mode
373 // fall back to 640x480
375 devmode.dmBitsPerPel=16;
376 devmode.dmPelsWidth=640;
377 devmode.dmPelsHeight=480;
378 devmode.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
380 retval=ChangeDisplaySettings(&devmode,CDS_FULLSCREEN);
381 if (retval!=DISP_CHANGE_SUCCESSFUL)
383 errstr="ChangeDisplaySettings";
384 // we couldn't even switch to 640x480, we're out of here
385 // restore screen mode to default
386 ChangeDisplaySettings(NULL,0);
390 // successful, change our global settings to reflect what
397 // success at changing the video mode
404 // we want windowed mode, figure out how big the window is
406 GetWindowRect(g_hWnd,&rect);
407 width=abs(rect.right-rect.left);
408 height=abs(rect.bottom-rect.top);
412 // full screen mode, we want the window to be on top of everything
413 SetWindowPos(g_hWnd,HWND_TOPMOST,0,0,GLPREF_width,GLPREF_height,SWP_FRAMECHANGED);
415 height=GLPREF_height;
416 GetWindowRect(g_hWnd,&rect);
419 GLSTATE_width = width;
420 GLSTATE_height = height;
426 // Now we finally setup OpenGL
427 // If OpenGL is to be dynamically loaded, do this now (if the DLL isn't already
429 // remove the following error when you figure out what you want to do
430 // it's put here to make sure you notice this
431 #ifdef OGL_RUNTIME_LOAD
432 ogl_init_load_library();
435 // Setup our pixel format
437 memset(&pfd,0,sizeof(pfd));
438 pfd.nSize = sizeof(pfd);
440 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
441 // pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_GENERIC_ACCELERATED;
442 pfd.iPixelType = PFD_TYPE_RGBA;
443 //let the ogl driver decide. (fixes no hw accel in 16bit mode in w2k with tnt2)
444 // pfd.cColorBits = 16;
445 // pfd.cAlphaBits = 8;
446 // pfd.cDepthBits = 0;
447 // pfd.cAccumBits = 0;
448 // pfd.cStencilBits = 0;
449 pfd.iLayerType = PFD_MAIN_PLANE;
450 pfd.dwLayerMask = PFD_MAIN_PLANE;
453 // Find the user's "best match" PFD
454 pf = ChoosePixelFormat(hDC,&pfd);
457 errstr="ChoosePixelFormat";
458 // no pixel format closely matches
463 if(SetPixelFormat(hDC,pf,&pfd)==FALSE)
465 errstr="SetPixelFormat";
466 // unable to set the pixel format
470 // Now retrieve the PFD, we need to check some things
471 /* if(DescribePixelFormat(hDC,pf,sizeof(PIXELFORMATDESCRIPTOR),&pfd_copy)==0)
473 errstr="DescribePixelFormat";
474 // unable to get the PFD
478 // Make sure we are hardware accelerated
479 if((pfd_copy.dwFlags&PFD_GENERIC_ACCELERATED)==0&&(pfd_copy.dwFlags&PFD_GENERIC_FORMAT)!=0)
481 // we are not hardware accelerated!
485 // Finally, create our OpenGL context and make it current
486 GL_ResourceContext = wglCreateContext(hDC);
487 if(GL_ResourceContext==NULL)
489 errstr="wglCreateContext";
490 // we couldn't create a context!
494 // Make the context current
495 wglMakeCurrent(hDC,GL_ResourceContext);
497 // Save our gamma values because we'll probably be changing them,
498 // this way we can restore them on exit
500 GetDeviceGammaRamp(hDC, (LPVOID)Saved_gamma_values);
507 Error("opengl init error: %s\n",errstr);
511 void OpenGL_Shutdown(void)
513 // Do any needed OpenGL shutdown here
516 // Now do Window specific shutdown
517 if(wglMakeCurrent)//check to make sure the function is valid (dyanmic loaded OpenGL)
518 wglMakeCurrent(NULL, NULL);
520 if(wglDeleteContext)//check to make sure the function is valid (dyanmic loaded OpenGL)
521 wglDeleteContext(GL_ResourceContext);
523 // Restore back to user screen settings
525 ChangeDisplaySettings(NULL,0);
527 // Restore gamma values
529 SetDeviceGammaRamp(hDC, (LPVOID)Saved_gamma_values);
531 ReleaseDC(g_hWnd,hDC);