2010-07-10 08:20:50 +00:00
/* ZZ Open GL graphics plugin
* Copyright ( c ) 2009 - 2010 zeydlitz @ gmail . com , arcum42 @ gmail . com
* Based on Zerofrog ' s ZeroGS KOSMOS ( c ) 2005 - 2008
2010-03-19 05:24:36 +00:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
2010-07-04 22:49:00 +00:00
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA
2010-03-19 05:24:36 +00:00
*/
2010-04-25 00:31:27 +00:00
2010-03-19 05:24:36 +00:00
# include "GS.h"
# include "zerogs.h"
2010-09-01 10:28:37 +00:00
# include "GLWin.h"
2010-03-19 05:24:36 +00:00
# ifdef GL_WIN32_WINDOW
2010-07-03 07:57:56 +00:00
HDC hDC = NULL ; // Private GDI Device Context
HGLRC hRC = NULL ; // Permanent Rendering Context
2010-05-01 20:33:53 +00:00
LRESULT WINAPI MsgProc ( HWND hWnd , UINT msg , WPARAM wParam , LPARAM lParam )
2010-03-19 05:24:36 +00:00
{
static int nWindowWidth = 0 , nWindowHeight = 0 ;
2010-05-01 20:33:53 +00:00
switch ( msg )
{
2010-03-19 05:24:36 +00:00
case WM_DESTROY :
2010-05-01 20:33:53 +00:00
PostQuitMessage ( 0 ) ;
2010-03-19 05:24:36 +00:00
return 0 ;
case WM_KEYDOWN :
// switch(wParam) {
// case VK_ESCAPE:
// SendMessage(hWnd, WM_DESTROY, 0L, 0L);
// break;
// }
break ;
case WM_SIZE :
2010-05-01 20:33:53 +00:00
nWindowWidth = lParam & 0xffff ;
nWindowHeight = lParam > > 16 ;
2010-03-19 05:24:36 +00:00
ZeroGS : : ChangeWindowSize ( nWindowWidth , nWindowHeight ) ;
break ;
case WM_SIZING :
// if button is 0, then just released so can resize
2010-05-01 20:33:53 +00:00
if ( GetSystemMetrics ( SM_SWAPBUTTON ) ? ! GetAsyncKeyState ( VK_RBUTTON ) : ! GetAsyncKeyState ( VK_LBUTTON ) )
{
2010-03-19 05:24:36 +00:00
ZeroGS : : SetChangeDeviceSize ( nWindowWidth , nWindowHeight ) ;
}
break ;
case WM_SETCURSOR :
SetCursor ( NULL ) ;
break ;
}
2010-05-01 20:33:53 +00:00
return DefWindowProc ( hWnd , msg , wParam , lParam ) ;
2010-03-19 05:24:36 +00:00
}
2010-03-29 21:15:49 +00:00
bool GLWindow : : CreateWindow ( void * pDisplay )
2010-04-25 00:31:27 +00:00
{
2010-03-19 05:24:36 +00:00
RECT rc , rcdesktop ;
2010-05-01 20:33:53 +00:00
rc . left = 0 ;
rc . top = 0 ;
rc . right = conf . width ;
rc . bottom = conf . height ;
2010-03-19 05:24:36 +00:00
WNDCLASSEX wc ;
HINSTANCE hInstance = GetModuleHandle ( NULL ) ;
DWORD dwExStyle , dwStyle ;
wc . cbSize = sizeof ( WNDCLASSEX ) ;
wc . style = CS_CLASSDC ;
wc . lpfnWndProc = ( WNDPROC ) MsgProc ;
wc . cbClsExtra = 0 ;
wc . cbWndExtra = 0 ;
wc . hInstance = hInstance ;
wc . hIcon = NULL ;
wc . hIconSm = NULL ;
wc . hCursor = NULL ;
wc . hbrBackground = NULL ;
wc . lpszMenuName = NULL ;
wc . lpszClassName = " PS2EMU_ZEROGS " ;
2010-05-01 20:33:53 +00:00
RegisterClassEx ( & wc ) ;
2010-03-19 05:24:36 +00:00
2010-06-19 06:23:40 +00:00
if ( conf . fullscreen ( ) )
2010-03-19 05:24:36 +00:00
{
dwExStyle = WS_EX_APPWINDOW ;
dwStyle = WS_POPUP ;
}
else
{
dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE ;
dwStyle = WS_OVERLAPPEDWINDOW ;
}
2010-05-01 23:34:44 +00:00
AdjustWindowRectEx ( & rc , dwStyle , false , dwExStyle ) ;
2010-05-01 20:33:53 +00:00
2010-03-19 05:24:36 +00:00
GetWindowRect ( GetDesktopWindow ( ) , & rcdesktop ) ;
GShwnd = CreateWindowEx (
2010-05-01 20:33:53 +00:00
dwExStyle ,
" PS2EMU_ZEROGS " ,
" ZeroGS " ,
dwStyle ,
( rcdesktop . right - ( rc . right - rc . left ) ) / 2 ,
( rcdesktop . bottom - ( rc . bottom - rc . top ) ) / 2 ,
rc . right - rc . left ,
rc . bottom - rc . top ,
NULL ,
NULL ,
hInstance ,
NULL ) ;
2010-03-19 05:24:36 +00:00
if ( GShwnd = = NULL ) return false ;
if ( pDisplay ! = NULL ) * ( HWND * ) pDisplay = GShwnd ;
2010-05-01 20:33:53 +00:00
2010-04-30 04:40:31 +00:00
// set just in case
SetWindowLongPtr ( GShwnd , GWLP_WNDPROC , ( LPARAM ) ( WNDPROC ) MsgProc ) ;
2010-05-01 20:33:53 +00:00
ShowWindow ( GShwnd , SW_SHOWDEFAULT ) ;
UpdateWindow ( GShwnd ) ;
2010-04-30 04:40:31 +00:00
SetFocus ( GShwnd ) ;
2010-04-25 00:31:27 +00:00
2010-03-19 05:24:36 +00:00
return ( pDisplay ! = NULL ) ;
}
2010-09-01 10:28:37 +00:00
bool GLWindow : : ReleaseContext ( )
2010-04-25 00:31:27 +00:00
{
2010-03-19 05:24:36 +00:00
if ( hRC ) // Do We Have A Rendering Context?
{
2010-05-01 20:33:53 +00:00
if ( ! wglMakeCurrent ( NULL , NULL ) ) // Are We Able To Release The DC And RC Contexts?
2010-03-19 05:24:36 +00:00
{
2010-05-01 20:33:53 +00:00
MessageBox ( NULL , " Release Of DC And RC Failed. " , " SHUTDOWN ERROR " , MB_OK | MB_ICONINFORMATION ) ;
2010-03-19 05:24:36 +00:00
}
if ( ! wglDeleteContext ( hRC ) ) // Are We Able To Delete The RC?
{
2010-05-01 20:33:53 +00:00
MessageBox ( NULL , " Release Rendering Context Failed. " , " SHUTDOWN ERROR " , MB_OK | MB_ICONINFORMATION ) ;
2010-03-19 05:24:36 +00:00
}
2010-05-01 20:33:53 +00:00
hRC = NULL ; // Set RC To NULL
2010-03-19 05:24:36 +00:00
}
2010-05-01 20:33:53 +00:00
if ( hDC & & ! ReleaseDC ( GShwnd , hDC ) ) // Are We Able To Release The DC
2010-03-19 05:24:36 +00:00
{
2010-05-01 20:33:53 +00:00
MessageBox ( NULL , " Release Device Context Failed. " , " SHUTDOWN ERROR " , MB_OK | MB_ICONINFORMATION ) ;
hDC = NULL ; // Set DC To NULL
2010-03-19 05:24:36 +00:00
}
return true ;
}
void GLWindow : : CloseWindow ( )
2010-04-25 00:31:27 +00:00
{
2010-05-01 20:33:53 +00:00
if ( GShwnd ! = NULL )
2010-03-19 05:24:36 +00:00
{
DestroyWindow ( GShwnd ) ;
GShwnd = NULL ;
}
}
bool GLWindow : : DisplayWindow ( int _width , int _height )
{
GLuint PixelFormat ; // Holds The Results After Searching For A Match
DWORD dwExStyle ; // Window Extended Style
DWORD dwStyle ; // Window Style
RECT rcdesktop ;
GetWindowRect ( GetDesktopWindow ( ) , & rcdesktop ) ;
2010-06-19 06:23:40 +00:00
if ( conf . fullscreen ( ) )
2010-05-01 20:33:53 +00:00
{
2010-03-19 05:24:36 +00:00
nBackbufferWidth = rcdesktop . right - rcdesktop . left ;
nBackbufferHeight = rcdesktop . bottom - rcdesktop . top ;
2010-05-01 20:33:53 +00:00
dwExStyle = WS_EX_APPWINDOW ;
dwStyle = WS_POPUP ;
2010-05-01 23:34:44 +00:00
ShowCursor ( false ) ;
2010-03-19 05:24:36 +00:00
}
2010-05-01 20:33:53 +00:00
else
{
dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE ;
dwStyle = WS_OVERLAPPEDWINDOW ;
2010-03-19 05:24:36 +00:00
}
RECT rc ;
2010-05-01 20:33:53 +00:00
rc . left = 0 ;
rc . top = 0 ;
rc . right = nBackbufferWidth ;
rc . bottom = nBackbufferHeight ;
2010-05-01 23:34:44 +00:00
AdjustWindowRectEx ( & rc , dwStyle , false , dwExStyle ) ;
2010-05-01 20:33:53 +00:00
int X = ( rcdesktop . right - rcdesktop . left ) / 2 - ( rc . right - rc . left ) / 2 ;
int Y = ( rcdesktop . bottom - rcdesktop . top ) / 2 - ( rc . bottom - rc . top ) / 2 ;
2010-03-19 05:24:36 +00:00
2010-05-01 20:33:53 +00:00
SetWindowLong ( GShwnd , GWL_STYLE , dwStyle ) ;
SetWindowLong ( GShwnd , GWL_EXSTYLE , dwExStyle ) ;
2010-03-19 05:24:36 +00:00
2010-05-01 20:33:53 +00:00
SetWindowPos ( GShwnd , HWND_TOP , X , Y , rc . right - rc . left , rc . bottom - rc . top , SWP_SHOWWINDOW ) ;
2010-04-25 00:31:27 +00:00
2010-06-19 06:23:40 +00:00
if ( conf . fullscreen ( ) )
2010-05-01 20:33:53 +00:00
{
2010-03-19 05:24:36 +00:00
DEVMODE dmScreenSettings ;
2010-05-01 20:33:53 +00:00
memset ( & dmScreenSettings , 0 , sizeof ( dmScreenSettings ) ) ;
dmScreenSettings . dmSize = sizeof ( dmScreenSettings ) ;
2010-03-19 05:24:36 +00:00
dmScreenSettings . dmPelsWidth = nBackbufferWidth ;
dmScreenSettings . dmPelsHeight = nBackbufferHeight ;
dmScreenSettings . dmBitsPerPel = 32 ;
2010-05-01 20:33:53 +00:00
dmScreenSettings . dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT ;
2010-03-19 05:24:36 +00:00
// Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
2010-05-01 20:33:53 +00:00
if ( ChangeDisplaySettings ( & dmScreenSettings , CDS_FULLSCREEN ) ! = DISP_CHANGE_SUCCESSFUL )
2010-03-19 05:24:36 +00:00
{
2010-05-01 20:33:53 +00:00
if ( MessageBox ( NULL , " The Requested Fullscreen Mode Is Not Supported By \n Your Video Card. Use Windowed Mode Instead? " , " NeHe GL " , MB_YESNO | MB_ICONEXCLAMATION ) = = IDYES )
2010-06-19 06:23:40 +00:00
conf . setFullscreen ( false ) ;
2010-03-19 05:24:36 +00:00
else
return false ;
}
}
2010-05-01 20:33:53 +00:00
else
{
2010-03-19 05:24:36 +00:00
// change to default resolution
ChangeDisplaySettings ( NULL , 0 ) ;
}
2010-05-01 20:33:53 +00:00
PIXELFORMATDESCRIPTOR pfd = // pfd Tells Windows How We Want Things To Be
2010-03-19 05:24:36 +00:00
{
sizeof ( PIXELFORMATDESCRIPTOR ) , // Size Of This Pixel Format Descriptor
1 , // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER , // Must Support Double Buffering
PFD_TYPE_RGBA , // Request An RGBA Format
32 , // Select Our Color Depth
0 , 0 , 0 , 0 , 0 , 0 , // Color Bits Ignored
0 , // 8bit Alpha Buffer
0 , // Shift Bit Ignored
0 , // No Accumulation Buffer
0 , 0 , 0 , 0 , // Accumulation Bits Ignored
2010-04-25 00:31:27 +00:00
24 , // 24Bit Z-Buffer (Depth Buffer)
2010-03-19 05:24:36 +00:00
8 , // 8bit Stencil Buffer
0 , // No Auxiliary Buffer
PFD_MAIN_PLANE , // Main Drawing Layer
0 , // Reserved
0 , 0 , 0 // Layer Masks Ignored
} ;
2010-04-25 00:31:27 +00:00
2010-05-01 20:33:53 +00:00
if ( ! ( hDC = GetDC ( GShwnd ) ) )
{
MessageBox ( NULL , " (1) Can't Create A GL Device Context. " , " ERROR " , MB_OK | MB_ICONEXCLAMATION ) ;
2010-03-19 05:24:36 +00:00
return false ;
}
2010-04-25 00:31:27 +00:00
2010-05-01 20:33:53 +00:00
if ( ! ( PixelFormat = ChoosePixelFormat ( hDC , & pfd ) ) )
{
MessageBox ( NULL , " (2) Can't Find A Suitable PixelFormat. " , " ERROR " , MB_OK | MB_ICONEXCLAMATION ) ;
2010-03-19 05:24:36 +00:00
return false ;
}
2010-05-01 20:33:53 +00:00
if ( ! SetPixelFormat ( hDC , PixelFormat , & pfd ) )
{
MessageBox ( NULL , " (3) Can't Set The PixelFormat. " , " ERROR " , MB_OK | MB_ICONEXCLAMATION ) ;
2010-03-19 05:24:36 +00:00
return false ;
}
2010-05-01 20:33:53 +00:00
if ( ! ( hRC = wglCreateContext ( hDC ) ) )
{
MessageBox ( NULL , " (4) Can't Create A GL Rendering Context. " , " ERROR " , MB_OK | MB_ICONEXCLAMATION ) ;
2010-03-19 05:24:36 +00:00
return false ;
}
2010-05-01 20:33:53 +00:00
if ( ! wglMakeCurrent ( hDC , hRC ) )
{
MessageBox ( NULL , " (5) Can't Activate The GL Rendering Context. " , " ERROR " , MB_OK | MB_ICONEXCLAMATION ) ;
2010-03-19 05:24:36 +00:00
return false ;
}
2010-04-25 00:31:27 +00:00
2010-03-19 05:24:36 +00:00
UpdateWindow ( GShwnd ) ;
2010-05-01 20:33:53 +00:00
2010-03-19 05:24:36 +00:00
return true ;
}
void GLWindow : : SwapGLBuffers ( )
{
static u32 lastswaptime = 0 ;
SwapBuffers ( hDC ) ;
lastswaptime = timeGetTime ( ) ;
}
2010-04-25 00:31:27 +00:00
2010-03-19 05:24:36 +00:00
void GLWindow : : SetTitle ( char * strtitle )
{
2010-09-01 10:28:37 +00:00
if ( ! conf . fullscreen ( ) ) SetWindowText ( GShwnd , strtitle ) ;
2010-03-19 05:24:36 +00:00
}
void GLWindow : : ResizeCheck ( )
{
}
2010-04-25 00:31:27 +00:00
# endif