1 //opengl platform specific functions for WGL - added by Peter Hawkins
2 //fullscreen example code courtesy of Jeff Slutter
3 //everything merged together and cleaned up by Matt Mueller (with some win32 help from Nirvana)
14 /*#include "event.h"*/
20 extern int Inferno_verbose;
22 static int mouse_hidden=0;
25 //extern unsigned int key_wparam, key_lparam, key_msg;
26 void keyboard_handler();
27 extern int WMKey_Handler_Ready;
31 static int GLPREF_width,GLPREF_height;
32 static int GLSTATE_width,GLSTATE_height;
33 static bool GLPREF_windowed;
35 static HGLRC GL_ResourceContext=NULL;
36 //static WORD Saved_gamma_values[256*3];
37 bool OpenGL_Initialize(void);
38 void OpenGL_Shutdown(void);
41 void PumpMessages(void)
45 while (PeekMessage(&msg,NULL,0,0,PM_REMOVE|PM_NOYIELD))
47 TranslateMessage(&msg);
48 DispatchMessage(&msg);
51 static void finiObjects()
59 // DisableOpenGL( g_hWnd, hDC, hRC );
63 long PASCAL DescentWndProc(HWND hWnd,UINT message,
64 WPARAM wParam,LPARAM lParam )
71 if (WMKey_Handler_Ready) {
72 // key_wparam=wParam; key_lparam=lParam; key_msg=message;
82 case WM_NCLBUTTONDOWN:
84 case WM_NCRBUTTONDOWN:
87 case WM_PALETTECHANGED:
88 case WM_PALETTEISCHANGING:
91 // Win32_Key_Hook(wParam);
92 // DPH: This doesn't work... no idea why not...
99 return DefWindowProc(hWnd,message,wParam,lParam);
104 void ogl_swap_buffers_internal(void){
108 int get_win_x_bs(void){
109 // return GetSystemMetrics(SM_CXBORDER)*2
110 return GetSystemMetrics(SM_CXFIXEDFRAME)*2;
112 int get_win_y_bs(void){
113 // return GetSystemMetrics(SM_CYBORDER)*2+GetSystemMetrics(SM_CYCAPTION);
114 return GetSystemMetrics(SM_CYFIXEDFRAME)*2+GetSystemMetrics(SM_CYCAPTION);
116 void win32_create_window(int x,int y)
120 WNDCLASS wcDescentClass;
123 Error("hInst=NULL\n");
126 wcDescentClass.lpszClassName = "WinD1X";
127 wcDescentClass.hInstance = hInst;
128 wcDescentClass.lpfnWndProc = DescentWndProc;
129 wcDescentClass.hCursor = LoadCursor(NULL, IDC_ARROW);
130 wcDescentClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
131 wcDescentClass.lpszMenuName = NULL;
132 wcDescentClass.hbrBackground = NULL;
133 wcDescentClass.style = CS_OWNDC;
134 wcDescentClass.cbClsExtra = 0;
135 wcDescentClass.cbWndExtra = 0;
137 // Register the class
138 if (!RegisterClass(&wcDescentClass)){
139 // printf("RegisterClass==0?\n");
140 //always seems to return 0 after the first time, yet if you remove the call, it crashes. Heh.
144 flags=WS_POPUP | WS_SYSMENU;
146 flags=WS_OVERLAPPED | WS_BORDER | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
148 if (!ogl_fullscreen){
149 x+=get_win_x_bs();y+=get_win_y_bs();
151 if (FindArg("-gl_test2")){
152 x=GetSystemMetrics(SM_CXSCREEN);
153 y=GetSystemMetrics(SM_CYSCREEN);
156 g_hWnd = CreateWindowEx(0,
168 if (!g_hWnd) Error("no window?\n");
169 ShowWindow(g_hWnd,SW_SHOWNORMAL);
170 UpdateWindow(g_hWnd);
178 if (!FindArg( "-nomouse" ))
180 if (!FindArg( "-nojoystick" ))
181 joy_init(JOYSTICKID1);
182 if (!FindArg( "-nosound" ))
184 // printf("arch_init successfully completed\n");
190 void ogl_destroy_window(void){
192 ogl_smash_texture_list_internal();
200 if (!FindArg( "-nomouse" ))
202 if (!FindArg( "-nojoystick" ))
204 if (!FindArg( "-nosound" ))
206 DestroyWindow(g_hWnd);
208 Error("ogl_destroy_window: no g_hWnd?\n");
214 void ogl_do_fullscreen_internal(void){
215 if (GLPREF_windowed==ogl_fullscreen){
216 ogl_destroy_window();
217 win32_create_window(GLPREF_width,GLPREF_height);
218 ogl_vivify_texture_list_internal();
223 int ogl_init_window(int x, int y){
227 if (GLSTATE_width==GLPREF_width && GLSTATE_height==GLPREF_height && GLPREF_windowed!=ogl_fullscreen)
228 return 0;//we are already in the right mode, don't do anything.
229 if (!ogl_fullscreen && GLPREF_windowed){
230 SetWindowPos(g_hWnd,0,0,0,x+get_win_x_bs(),y+get_win_y_bs(),SWP_NOMOVE);
232 ogl_destroy_window();
233 win32_create_window(x,y);
234 ogl_vivify_texture_list_internal();
237 win32_create_window(x,y);
242 hInst=GetModuleHandle (NULL);
244 void ogl_close(void){
245 ogl_destroy_window();
249 //windows opengl fullscreen changing - courtesy of Jeff Slutter
253 Windows Full Screen Setup
255 ===========================================================================
259 // Entering this function, the following values must be valid
260 // GLPREF_width,GLPREF_height: preferred width and height
261 // GLPREF_windowed: do we want windowed or full screen mode
262 // g_hWnd: handle to the window created for OpenGL
263 // On exit from this function (if returned true) the following values will be set
264 // GLSTATE_width,GLSTATE_height: real width and height of screen
265 // hDC: device context of the window
266 // GL_ResourceContext: OpenGL resource context
267 // Saved_gamma_values: Initial gamma values
268 bool OpenGL_Initialize(void)
273 PIXELFORMATDESCRIPTOR pfd;//, pfd_copy;
275 GLPREF_windowed=!ogl_fullscreen;
277 if (FindArg("-gl_test1")){
278 GLSTATE_width = GLPREF_width;
279 GLSTATE_height = GLPREF_height;
283 // First set our display mode
284 // Create direct draw surface
288 devmode.dmSize=sizeof(devmode);
289 devmode.dmBitsPerPel=16;
290 devmode.dmPelsWidth=GLPREF_width;
291 devmode.dmPelsHeight=GLPREF_height;
292 devmode.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
294 retval=ChangeDisplaySettings(&devmode,0);
296 if (retval!=DISP_CHANGE_SUCCESSFUL)
298 // we couldn't switch to the desired screen mode
299 // fall back to 640x480
301 devmode.dmBitsPerPel=16;
302 devmode.dmPelsWidth=640;
303 devmode.dmPelsHeight=480;
304 devmode.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
306 retval=ChangeDisplaySettings(&devmode,0);
307 if (retval!=DISP_CHANGE_SUCCESSFUL)
309 errstr="ChangeDisplaySettings";
310 // we couldn't even switch to 640x480, we're out of here
311 // restore screen mode to default
312 ChangeDisplaySettings(NULL,0);
316 // successful, change our global settings to reflect what
323 // success at changing the video mode
330 // we want windowed mode, figure out how big the window is
332 GetWindowRect(g_hWnd,&rect);
333 width=abs(rect.right-rect.left);
334 height=abs(rect.bottom-rect.top);
338 // full screen mode, we want the window to be on top of everything
339 SetWindowPos(g_hWnd,HWND_TOPMOST,0,0,GLPREF_width,GLPREF_height,SWP_FRAMECHANGED);
341 height=GLPREF_height;
342 GetWindowRect(g_hWnd,&rect);
345 GLSTATE_width = width;
346 GLSTATE_height = height;
352 // Now we finally setup OpenGL
353 // If OpenGL is to be dynamically loaded, do this now (if the DLL isn't already
355 // remove the following error when you figure out what you want to do
356 // it's put here to make sure you notice this
357 #ifdef OGL_RUNTIME_LOAD
358 ogl_init_load_library();
361 // Setup our pixel format
363 memset(&pfd,0,sizeof(pfd));
364 pfd.nSize = sizeof(pfd);
366 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
367 // pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_GENERIC_ACCELERATED;
368 pfd.iPixelType = PFD_TYPE_RGBA;
373 pfd.cStencilBits = 0;
374 pfd.iLayerType = PFD_MAIN_PLANE;
375 pfd.dwLayerMask = PFD_MAIN_PLANE;
378 // Find the user's "best match" PFD
379 pf = ChoosePixelFormat(hDC,&pfd);
382 errstr="ChoosePixelFormat";
383 // no pixel format closely matches
388 if(SetPixelFormat(hDC,pf,&pfd)==FALSE)
390 errstr="SetPixelFormat";
391 // unable to set the pixel format
395 // Now retrieve the PFD, we need to check some things
396 /* if(DescribePixelFormat(hDC,pf,sizeof(PIXELFORMATDESCRIPTOR),&pfd_copy)==0)
398 errstr="DescribePixelFormat";
399 // unable to get the PFD
403 // Make sure we are hardware accelerated
404 if((pfd_copy.dwFlags&PFD_GENERIC_ACCELERATED)==0&&(pfd_copy.dwFlags&PFD_GENERIC_FORMAT)!=0)
406 // we are not hardware accelerated!
410 // Finally, create our OpenGL context and make it current
411 GL_ResourceContext = wglCreateContext(hDC);
412 if(GL_ResourceContext==NULL)
414 errstr="wglCreateContext";
415 // we couldn't create a context!
419 // Make the context current
420 wglMakeCurrent(hDC,GL_ResourceContext);
422 // Save our gamma values because we'll probably be changing them,
423 // this way we can restore them on exit
425 // GetDeviceGammaRamp(hDC,(LPVOID)Saved_gamma_values);
432 Error("opengl init error: %s\n",errstr);
436 void OpenGL_Shutdown(void)
438 // Do any needed OpenGL shutdown here
441 // Now do Window specific shutdown
442 if(wglMakeCurrent)//check to make sure the function is valid (dyanmic loaded OpenGL)
443 wglMakeCurrent(NULL, NULL);
445 if(wglDeleteContext)//check to make sure the function is valid (dyanmic loaded OpenGL)
446 wglDeleteContext(GL_ResourceContext);
448 // Restore back to user screen settings
450 ChangeDisplaySettings(NULL,0);
452 // Restore gamma values
454 // SetDeviceGammaRamp(hDC,(LPVOID)Saved_gamma_values);
456 ReleaseDC(g_hWnd,hDC);