| version 1.7 | | version 1.8 |
|---|
| | |
| //opengl platform specific functions for WGL - added by Peter Hawkins | | //opengl platform specific functions for WGL - added by Peter Hawkins |
| //fullscreen example code donated by Jeff Slutter | | //fullscreen example code courtesy of Jeff Slutter |
| //everything merged together and cleaned up by Matt Mueller (with some win32 help from Nirvana) | | //everything merged together and cleaned up by Matt Mueller (with some win32 help from Nirvana) |
| #include <windows.h> | | #include <windows.h> |
| #include <mmsystem.h> | | #include <mmsystem.h> |
| | |
| /*#include "event.h"*/ | | /*#include "event.h"*/ |
| | | |
| | | |
| HINSTANCE hInst; | | HINSTANCE hInst=NULL; |
| HWND g_hWnd=NULL; | | HWND g_hWnd=NULL; |
| | | |
| extern int Inferno_verbose; | | extern int Inferno_verbose; |
| | |
| extern int WMKey_Handler_Ready; | | extern int WMKey_Handler_Ready; |
| | | |
| HDC hDC; | | HDC hDC; |
| HGLRC hRC; | | |
| | | |
| static int GLPREF_width,GLPREF_height; | | static int GLPREF_width,GLPREF_height; |
| static int GLSTATE_width,GLSTATE_height; | | static int GLSTATE_width,GLSTATE_height; |
| static bool GLPREF_windowed; | | static bool GLPREF_windowed; |
| //static HWND GLPREF_gamewindow; | | |
| #define GLPREF_gamewindow g_hWnd | | |
| //static HDC GLPREF_windowdc; | | |
| #define GLPREF_windowdc hDC | | |
| | | |
| static HGLRC GL_ResourceContext=NULL; | | static HGLRC GL_ResourceContext=NULL; |
| //static WORD Saved_gamma_values[256*3]; | | //static WORD Saved_gamma_values[256*3]; |
| | |
| } | | } |
| int get_win_y_bs(void){ | | int get_win_y_bs(void){ |
| // return GetSystemMetrics(SM_CYBORDER)*2+GetSystemMetrics(SM_CYCAPTION); | | // return GetSystemMetrics(SM_CYBORDER)*2+GetSystemMetrics(SM_CYCAPTION); |
| return GetSystemMetrics(SM_CXFIXEDFRAME)*2+GetSystemMetrics(SM_CYCAPTION); | | return GetSystemMetrics(SM_CYFIXEDFRAME)*2+GetSystemMetrics(SM_CYCAPTION); |
| } | | } |
| | | |
| void win32_create_window(int x,int y) | | void win32_create_window(int x,int y) |
| { | | { |
| WNDCLASS wcDescentClass; | | |
| int flags; | | int flags; |
| | | |
| | | WNDCLASS wcDescentClass; |
| | | |
| | | if (!hInst) |
| | | Error("hInst=NULL\n"); |
| | | |
| | | |
| wcDescentClass.lpszClassName = "WinD1X"; | | wcDescentClass.lpszClassName = "WinD1X"; |
| wcDescentClass.hInstance = hInst; | | wcDescentClass.hInstance = hInst; |
| wcDescentClass.lpfnWndProc = DescentWndProc; | | wcDescentClass.lpfnWndProc = DescentWndProc; |
| | |
| wcDescentClass.cbWndExtra = 0; | | wcDescentClass.cbWndExtra = 0; |
| | | |
| // Register the class | | // Register the class |
| RegisterClass(&wcDescentClass); | | if (!RegisterClass(&wcDescentClass)){ |
| | | // printf("RegisterClass==0?\n"); |
| | | //always seems to return 0 after the first time, yet if you remove the call, it crashes. Heh. |
| | | } |
| | | |
| if (ogl_fullscreen) | | if (ogl_fullscreen) |
| flags=WS_POPUP | WS_SYSMENU; | | flags=WS_POPUP | WS_SYSMENU; |
| else | | else |
| flags=WS_OVERLAPPED | WS_BORDER; | | flags=WS_OVERLAPPED | WS_BORDER | WS_CLIPCHILDREN | WS_CLIPSIBLINGS; |
| | | |
| if (!ogl_fullscreen){ | | if (!ogl_fullscreen){ |
| x+=get_win_x_bs();y+=get_win_y_bs(); | | x+=get_win_x_bs();y+=get_win_y_bs(); |
| }else{ | | }else{ |
| | | if (FindArg("-gl_test2")){ |
| x=GetSystemMetrics(SM_CXSCREEN); | | x=GetSystemMetrics(SM_CXSCREEN); |
| y=GetSystemMetrics(SM_CYSCREEN); | | y=GetSystemMetrics(SM_CYSCREEN); |
| } | | } |
| | | } |
| g_hWnd = CreateWindowEx(0, | | g_hWnd = CreateWindowEx(0, |
| "WinD1X", | | "WinD1X", |
| "Descent", | | "Descent", |
| | |
| NULL | | NULL |
| ); | | ); |
| | | |
| // if (!g_hWnd) return; // CRAP! | | |
| if (!g_hWnd) Error("no window?\n"); | | if (!g_hWnd) Error("no window?\n"); |
| ShowWindow(g_hWnd,SW_SHOWNORMAL); | | ShowWindow(g_hWnd,SW_SHOWNORMAL); |
| UpdateWindow(g_hWnd); | | UpdateWindow(g_hWnd); |
| | |
| } | | } |
| | | |
| key_init(); | | key_init(); |
| | | if (!FindArg( "-nomouse" )) |
| mouse_init(0); | | mouse_init(0); |
| | | if (!FindArg( "-nojoystick" )) |
| joy_init(JOYSTICKID1); | | joy_init(JOYSTICKID1); |
| if (!FindArg( "-nosound" )) | | if (!FindArg( "-nosound" )) |
| digi_init(); | | digi_init(); |
| | |
| | | |
| gl_initialized=1; | | gl_initialized=1; |
| } | | } |
| void win32_destroy_window(void){ | | void ogl_destroy_window(void){ |
| | | if (gl_initialized){ |
| | | ogl_smash_texture_list_internal(); |
| | | OpenGL_Shutdown(); |
| if (mouse_hidden){ | | if (mouse_hidden){ |
| ShowCursor(TRUE); | | ShowCursor(TRUE); |
| mouse_hidden = 0; | | mouse_hidden = 0; |
| } | | } |
| if (g_hWnd){ | | if (g_hWnd){ |
| key_close(); | | key_close(); |
| | | if (!FindArg( "-nomouse" )) |
| mouse_close(); | | mouse_close(); |
| | | if (!FindArg( "-nojoystick" )) |
| joy_close(); | | joy_close(); |
| if (!FindArg( "-nosound" )) | | if (!FindArg( "-nosound" )) |
| digi_close(); | | digi_close(); |
| DestroyWindow(g_hWnd); | | DestroyWindow(g_hWnd); |
| } | | }else |
| } | | Error("ogl_destroy_window: no g_hWnd?\n"); |
| void ogl_destroy_window(void){ | | |
| if (gl_initialized){ | | |
| ogl_smash_texture_list_internal(); | | |
| OpenGL_Shutdown(); | | |
| win32_destroy_window(); | | |
| gl_initialized=0; | | gl_initialized=0; |
| } | | } |
| return; | | return; |
| | |
| if (GLPREF_windowed==ogl_fullscreen){ | | if (GLPREF_windowed==ogl_fullscreen){ |
| ogl_destroy_window(); | | ogl_destroy_window(); |
| win32_create_window(GLPREF_width,GLPREF_height); | | win32_create_window(GLPREF_width,GLPREF_height); |
| | | ogl_vivify_texture_list_internal(); |
| } | | } |
| } | | } |
| | | |
| | | |
| int ogl_init_window(int x, int y){ | | int ogl_init_window(int x, int y){ |
| /* if (x<=320){ | | if (FindArg("-wgl_nolowres")){//hack to avoid "icon squish problem." Heh |
| x*=2; | | x=640;y=480; |
| y*=2; | | } |
| }*/ | | |
| GLPREF_width=x; | | GLPREF_width=x; |
| GLPREF_height=y; | | GLPREF_height=y; |
| if (gl_initialized){ | | if (gl_initialized){ |
| if (GLSTATE_width==GLPREF_width && GLSTATE_height==GLPREF_height && GLPREF_windowed!=ogl_fullscreen) | | if (GLSTATE_width==GLPREF_width && GLSTATE_height==GLPREF_height && GLPREF_windowed!=ogl_fullscreen) |
| return 0;//we are already in the right mode, don't do anything. | | return 0;//we are already in the right mode, don't do anything. |
| if (!ogl_fullscreen && GLPREF_windowed){ | | if (!ogl_fullscreen && GLPREF_windowed){ |
| SetWindowPos(g_hWnd,0,0,0,x+get_win_x_bs(),y+get_win_y_bs(),SWP_NOMOVE);//the width/height seem to include borders/title/etc.. this isn't an exact value, but I didn't feel like testing enough to find the true values. -MPM | | SetWindowPos(g_hWnd,0,0,0,x+get_win_x_bs(),y+get_win_y_bs(),SWP_NOMOVE); |
| // SetWindowPos(g_hWnd,0,0,0,x,y,0);//the width/height seem to include borders/title/etc.. this isn't an exact value, but I didn't feel like testing enough to find the true values. -MPM | | |
| }else{ | | }else{ |
| ogl_destroy_window(); | | ogl_destroy_window(); |
| win32_create_window(x,y); | | win32_create_window(x,y); |
| | | ogl_vivify_texture_list_internal(); |
| } | | } |
| }else { | | }else { |
| win32_create_window(x,y); | | win32_create_window(x,y); |
| /* if (GLPREF_windowed){ | | |
| SetWindowPos(g_hWnd,0,0,0,x,y,0);//the width/height seem to include borders/title/etc.. this isn't an exact value, but I didn't feel like testing enough to find the true values. -MPM | | |
| }*/ | | |
| } | | } |
| return 0; | | return 0; |
| } | | } |
| void ogl_init(void){ | | void ogl_init(void){ |
| | | hInst=GetModuleHandle (NULL); |
| } | | } |
| void ogl_close(void){ | | void ogl_close(void){ |
| ogl_destroy_window(); | | ogl_destroy_window(); |
| | |
| // Entering this function, the following values must be valid | | // Entering this function, the following values must be valid |
| // GLPREF_width,GLPREF_height: preferred width and height | | // GLPREF_width,GLPREF_height: preferred width and height |
| // GLPREF_windowed: do we want windowed or full screen mode | | // GLPREF_windowed: do we want windowed or full screen mode |
| // GLPREF_gamewindow: handle to the window created for OpenGL | | // g_hWnd: handle to the window created for OpenGL |
| // On exit from this function (if returned true) the following values will be set | | // On exit from this function (if returned true) the following values will be set |
| // GLSTATE_width,GLSTATE_height: real width and height of screen | | // GLSTATE_width,GLSTATE_height: real width and height of screen |
| // GLPREF_windowdc: device context of the window | | // hDC: device context of the window |
| // GL_ResourceContext: OpenGL resource context | | // GL_ResourceContext: OpenGL resource context |
| // Saved_gamma_values: Initial gamma values | | // Saved_gamma_values: Initial gamma values |
| bool OpenGL_Initialize(void) | | bool OpenGL_Initialize(void) |
| | |
| char *errstr=""; | | char *errstr=""; |
| int width,height; | | int width,height; |
| int pf; | | int pf; |
| PIXELFORMATDESCRIPTOR pfd, pfd_copy; | | PIXELFORMATDESCRIPTOR pfd;//, pfd_copy; |
| | | |
| GLPREF_windowed=!ogl_fullscreen; | | GLPREF_windowed=!ogl_fullscreen; |
| | | |
| | | if (FindArg("-gl_test1")){ |
| | | GLSTATE_width = GLPREF_width; |
| | | GLSTATE_height = GLPREF_height; |
| | | }else{ |
| if(!GLPREF_windowed) | | if(!GLPREF_windowed) |
| { | | { |
| // First set our display mode | | // First set our display mode |
| | |
| } | | } |
| } | | } |
| | | |
| GLPREF_windowdc = GetDC(GLPREF_gamewindow); | | |
| | | |
| if(GLPREF_windowed) | | if(GLPREF_windowed) |
| { | | { |
| // we want windowed mode, figure out how big the window is | | // we want windowed mode, figure out how big the window is |
| RECT rect; | | RECT rect; |
| GetWindowRect(GLPREF_gamewindow,&rect); | | GetWindowRect(g_hWnd,&rect); |
| width=abs(rect.right-rect.left); | | width=abs(rect.right-rect.left); |
| height=abs(rect.bottom-rect.top); | | height=abs(rect.bottom-rect.top); |
| }else | | }else |
| { | | { |
| RECT rect; | | RECT rect; |
| // full screen mode, we want the window to be on top of everything | | // full screen mode, we want the window to be on top of everything |
| SetWindowPos(GLPREF_gamewindow,HWND_TOPMOST,0,0,GLPREF_width,GLPREF_height,SWP_FRAMECHANGED); | | SetWindowPos(g_hWnd,HWND_TOPMOST,0,0,GLPREF_width,GLPREF_height,SWP_FRAMECHANGED); |
| width=GLPREF_width; | | width=GLPREF_width; |
| height=GLPREF_height; | | height=GLPREF_height; |
| GetWindowRect(GLPREF_gamewindow,&rect); | | GetWindowRect(g_hWnd,&rect); |
| } | | } |
| | | |
| GLSTATE_width = width; | | GLSTATE_width = width; |
| GLSTATE_height = height; | | GLSTATE_height = height; |
| | | |
| | | } |
| | | |
| | | hDC = GetDC(g_hWnd); |
| | | |
| // Now we finally setup OpenGL | | // Now we finally setup OpenGL |
| // If OpenGL is to be dynamically loaded, do this now (if the DLL isn't already | | // If OpenGL is to be dynamically loaded, do this now (if the DLL isn't already |
| // loaded) | | // loaded) |
| | |
| pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; | | pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; |
| // pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_GENERIC_ACCELERATED; | | // pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_GENERIC_ACCELERATED; |
| pfd.iPixelType = PFD_TYPE_RGBA; | | pfd.iPixelType = PFD_TYPE_RGBA; |
| | | pfd.cColorBits = 16; |
| | | pfd.cAlphaBits = 8; |
| | | pfd.cDepthBits = 0; |
| | | pfd.cAccumBits = 0; |
| | | pfd.cStencilBits = 0; |
| | | pfd.iLayerType = PFD_MAIN_PLANE; |
| | | pfd.dwLayerMask = PFD_MAIN_PLANE; |
| | | |
| | | |
| // Find the user's "best match" PFD | | // Find the user's "best match" PFD |
| pf = ChoosePixelFormat(GLPREF_windowdc,&pfd); | | pf = ChoosePixelFormat(hDC,&pfd); |
| if(pf == 0) | | if(pf == 0) |
| { | | { |
| errstr="ChoosePixelFormat"; | | errstr="ChoosePixelFormat"; |
| | |
| } | | } |
| | | |
| // Set the new PFD | | // Set the new PFD |
| if(SetPixelFormat(GLPREF_windowdc,pf,&pfd)==FALSE) | | if(SetPixelFormat(hDC,pf,&pfd)==FALSE) |
| { | | { |
| errstr="SetPixelFormat"; | | errstr="SetPixelFormat"; |
| // unable to set the pixel format | | // unable to set the pixel format |
| | |
| } | | } |
| | | |
| // Now retrieve the PFD, we need to check some things | | // Now retrieve the PFD, we need to check some things |
| if(DescribePixelFormat(GLPREF_windowdc,pf,sizeof(PIXELFORMATDESCRIPTOR),&pfd_copy)==0) | | /* if(DescribePixelFormat(hDC,pf,sizeof(PIXELFORMATDESCRIPTOR),&pfd_copy)==0) |
| { | | { |
| errstr="DescribePixelFormat"; | | errstr="DescribePixelFormat"; |
| // unable to get the PFD | | // unable to get the PFD |
| | |
| } | | } |
| | | |
| // Make sure we are hardware accelerated | | // Make sure we are hardware accelerated |
| /* if((pfd_copy.dwFlags&PFD_GENERIC_ACCELERATED)==0&&(pfd_copy.dwFlags&PFD_GENERIC_FORMAT)!=0) | | if((pfd_copy.dwFlags&PFD_GENERIC_ACCELERATED)==0&&(pfd_copy.dwFlags&PFD_GENERIC_FORMAT)!=0) |
| { | | { |
| // we are not hardware accelerated! | | // we are not hardware accelerated! |
| goto OpenGLError; | | goto OpenGLError; |
| }*/ | | }*/ |
| | | |
| // Finally, create our OpenGL context and make it current | | // Finally, create our OpenGL context and make it current |
| GL_ResourceContext = wglCreateContext(GLPREF_windowdc); | | GL_ResourceContext = wglCreateContext(hDC); |
| if(GL_ResourceContext==NULL) | | if(GL_ResourceContext==NULL) |
| { | | { |
| errstr="wglCreateContext"; | | errstr="wglCreateContext"; |
| | |
| } | | } |
| | | |
| // Make the context current | | // Make the context current |
| wglMakeCurrent(GLPREF_windowdc,GL_ResourceContext); | | wglMakeCurrent(hDC,GL_ResourceContext); |
| | | |
| // Save our gamma values because we'll probably be changing them, | | // Save our gamma values because we'll probably be changing them, |
| // this way we can restore them on exit | | // this way we can restore them on exit |
| | | |
| // GetDeviceGammaRamp(GLPREF_windowdc,(LPVOID)Saved_gamma_values); | | // GetDeviceGammaRamp(hDC,(LPVOID)Saved_gamma_values); |
| | | |
| return true; | | return true; |
| | | |
| | |
| | | |
| // Restore gamma values | | // Restore gamma values |
| | | |
| // SetDeviceGammaRamp(GLPREF_windowdc,(LPVOID)Saved_gamma_values); | | // SetDeviceGammaRamp(hDC,(LPVOID)Saved_gamma_values); |
| | | |
| ReleaseDC(GLPREF_gamewindow,GLPREF_windowdc); | | ReleaseDC(g_hWnd,hDC); |
| } | | } |