For the windows version, if running with fullscreen disabled, center the main window instead of creating it top-left all the time.
Also renamed WindowRect to windowRect & PixelFormat to pixelFormat for consistency with other locally declared variables. (Uppercase should be reserved for class names, ie WindowRect is the class/struct, windowRect is an local object in your code.)
This commit is contained in:
+136
-127
@@ -357,161 +357,170 @@ BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscree
|
|||||||
actualHeight = height;
|
actualHeight = height;
|
||||||
|
|
||||||
|
|
||||||
GLuint PixelFormat; // Holds The Results After Searching For A Match
|
GLuint pixelFormat; // Holds The Results After Searching For A Match
|
||||||
WNDCLASS wc; // Windows Class Structure
|
WNDCLASS wc; // Windows Class Structure
|
||||||
DWORD dwExStyle; // Window Extended Style
|
DWORD dwExStyle; // Window Extended Style
|
||||||
DWORD dwStyle; // Window Style
|
DWORD dwStyle; // Window Style
|
||||||
RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
|
RECT windowRect; // Grabs Rectangle Upper Left / Lower Right Values
|
||||||
WindowRect.left=(long)0; // Set Left Value To 0
|
|
||||||
WindowRect.right=(long)width; // Set Right Value To Requested Width
|
windowRect.left = (long) 0;
|
||||||
WindowRect.top=(long)0; // Set Top Value To 0
|
windowRect.top = (long) 0;
|
||||||
WindowRect.bottom=(long)height; // Set Bottom Value To Requested Height
|
|
||||||
|
if (fullscreenflag == false)
|
||||||
|
{
|
||||||
|
windowRect.left = (::GetSystemMetrics(SM_CXSCREEN) - width) / 2;
|
||||||
|
windowRect.top = (::GetSystemMetrics(SM_CYSCREEN) - height) / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
windowRect.right = windowRect.left + (long)width; // Set Right Value To Requested Width
|
||||||
|
windowRect.bottom = windowRect.top + (long)height; // Set Bottom Value To Requested Height
|
||||||
|
|
||||||
|
|
||||||
fullscreen=fullscreenflag; // Set The Global Fullscreen Flag
|
fullscreen=fullscreenflag; // Set The Global Fullscreen Flag
|
||||||
|
|
||||||
hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window
|
hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window
|
||||||
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.
|
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.
|
||||||
wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages
|
wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages
|
||||||
wc.cbClsExtra = 0; // No Extra Window Data
|
wc.cbClsExtra = 0; // No Extra Window Data
|
||||||
wc.cbWndExtra = 0; // No Extra Window Data
|
wc.cbWndExtra = 0; // No Extra Window Data
|
||||||
wc.hInstance = hInstance; // Set The Instance
|
wc.hInstance = hInstance; // Set The Instance
|
||||||
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon
|
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon
|
||||||
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
|
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
|
||||||
wc.hbrBackground = NULL; // No Background Required For GL
|
wc.hbrBackground = NULL; // No Background Required For GL
|
||||||
wc.lpszMenuName = NULL; // We Don't Want A Menu
|
wc.lpszMenuName = NULL; // We Don't Want A Menu
|
||||||
wc.lpszClassName = "OpenGL"; // Set The Class Name
|
wc.lpszClassName = "OpenGL"; // Set The Class Name
|
||||||
|
|
||||||
if (!RegisterClass(&wc)) // Attempt To Register The Window Class
|
if (!RegisterClass(&wc)) // Attempt To Register The Window Class
|
||||||
{
|
{
|
||||||
MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
|
MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
|
||||||
return FALSE; // Return FALSE
|
return FALSE; // Return FALSE
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fullscreen) // Attempt Fullscreen Mode?
|
if (fullscreen) // Attempt Fullscreen Mode?
|
||||||
{
|
{
|
||||||
DEVMODE dmScreenSettings; // Device Mode
|
DEVMODE dmScreenSettings; // Device Mode
|
||||||
memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared
|
memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared
|
||||||
dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure
|
dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure
|
||||||
dmScreenSettings.dmPelsWidth = width; // Selected Screen Width
|
dmScreenSettings.dmPelsWidth = width; // Selected Screen Width
|
||||||
dmScreenSettings.dmPelsHeight = height; // Selected Screen Height
|
dmScreenSettings.dmPelsHeight = height; // Selected Screen Height
|
||||||
dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel
|
dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel
|
||||||
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
|
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
|
||||||
|
|
||||||
// Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
|
// Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
|
||||||
if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
|
if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
|
||||||
{
|
{
|
||||||
// If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode.
|
// If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode.
|
||||||
if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
|
if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
|
||||||
{
|
{
|
||||||
fullscreen=FALSE; // Windowed Mode Selected. Fullscreen = FALSE
|
fullscreen=FALSE; // Windowed Mode Selected. Fullscreen = FALSE
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Pop Up A Message Box Letting User Know The Program Is Closing.
|
// Pop Up A Message Box Letting User Know The Program Is Closing.
|
||||||
MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
|
MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
|
||||||
return FALSE; // Return FALSE
|
return FALSE; // Return FALSE
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (fullscreen) // Are We Still In Fullscreen Mode?
|
if (fullscreen) // Are We Still In Fullscreen Mode?
|
||||||
{
|
{
|
||||||
dwExStyle=WS_EX_APPWINDOW; // Window Extended Style
|
dwExStyle=WS_EX_APPWINDOW; // Window Extended Style
|
||||||
dwStyle=WS_POPUP; // Windows Style
|
dwStyle=WS_POPUP; // Windows Style
|
||||||
ShowCursor(FALSE); // Hide Mouse Pointer
|
ShowCursor(FALSE); // Hide Mouse Pointer
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
|
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
|
||||||
//dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style
|
//dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style
|
||||||
dwStyle= WS_OVERLAPPED | \
|
dwStyle= WS_OVERLAPPED | \
|
||||||
WS_CAPTION | \
|
WS_CAPTION | \
|
||||||
WS_MINIMIZEBOX |
|
WS_MINIMIZEBOX |
|
||||||
WS_SIZEBOX |
|
WS_SIZEBOX |
|
||||||
WS_MAXIMIZEBOX |
|
WS_MAXIMIZEBOX |
|
||||||
//WS_MINIMIZE |
|
//WS_MINIMIZE |
|
||||||
WS_SYSMENU;// |
|
WS_SYSMENU;// |
|
||||||
//WS_THICKFRAME ;
|
//WS_THICKFRAME ;
|
||||||
}
|
}
|
||||||
|
|
||||||
AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size
|
AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size
|
||||||
|
|
||||||
// Create The Window
|
// Create The Window
|
||||||
if (!(hWnd=CreateWindowEx( dwExStyle, // Extended Style For The Window
|
if (!(hWnd=CreateWindowEx( dwExStyle, // Extended Style For The Window
|
||||||
"OpenGL", // Class Name
|
"OpenGL", // Class Name
|
||||||
title, // Window Title
|
title, // Window Title
|
||||||
dwStyle | // Defined Window Style
|
dwStyle | // Defined Window Style
|
||||||
WS_CLIPSIBLINGS | // Required Window Style
|
WS_CLIPSIBLINGS | // Required Window Style
|
||||||
WS_CLIPCHILDREN, // Required Window Style
|
WS_CLIPCHILDREN, // Required Window Style
|
||||||
0, 0, // Window Position
|
windowRect.left, windowRect.top, // Window Position
|
||||||
WindowRect.right-WindowRect.left, // Calculate Window Width
|
windowRect.right-windowRect.left, // Calculate Window Width
|
||||||
WindowRect.bottom-WindowRect.top, // Calculate Window Height
|
windowRect.bottom-windowRect.top, // Calculate Window Height
|
||||||
NULL, // No Parent Window
|
NULL, // No Parent Window
|
||||||
NULL, // No Menu
|
NULL, // No Menu
|
||||||
hInstance, // Instance
|
hInstance, // Instance
|
||||||
NULL))) // Dont Pass Anything To WM_CREATE
|
NULL))) // Dont Pass Anything To WM_CREATE
|
||||||
{
|
{
|
||||||
KillGLWindow(); // Reset The Display
|
KillGLWindow(); // Reset The Display
|
||||||
MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
|
MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
|
||||||
return FALSE; // Return FALSE
|
return FALSE; // Return FALSE
|
||||||
}
|
}
|
||||||
|
|
||||||
static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
|
static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
|
||||||
{
|
{
|
||||||
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
|
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
|
||||||
1, // Version Number
|
1, // Version Number
|
||||||
PFD_DRAW_TO_WINDOW | // Format Must Support Window
|
PFD_DRAW_TO_WINDOW | // Format Must Support Window
|
||||||
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
|
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
|
||||||
PFD_DOUBLEBUFFER, // Must Support Double Buffering
|
PFD_DOUBLEBUFFER, // Must Support Double Buffering
|
||||||
PFD_TYPE_RGBA, // Request An RGBA Format
|
PFD_TYPE_RGBA, // Request An RGBA Format
|
||||||
bits, // Select Our Color Depth
|
bits, // Select Our Color Depth
|
||||||
0, 0, 0, 0, 0, 0, // Color Bits Ignored
|
0, 0, 0, 0, 0, 0, // Color Bits Ignored
|
||||||
0, // No Alpha Buffer
|
0, // No Alpha Buffer
|
||||||
0, // Shift Bit Ignored
|
0, // Shift Bit Ignored
|
||||||
0, // No Accumulation Buffer
|
0, // No Accumulation Buffer
|
||||||
0, 0, 0, 0, // Accumulation Bits Ignored
|
0, 0, 0, 0, // Accumulation Bits Ignored
|
||||||
16, // 16Bit Z-Buffer (Depth Buffer)
|
16, // 16Bit Z-Buffer (Depth Buffer)
|
||||||
0, // No Stencil Buffer
|
0, // No Stencil Buffer
|
||||||
0, // No Auxiliary Buffer
|
0, // No Auxiliary Buffer
|
||||||
PFD_MAIN_PLANE, // Main Drawing Layer
|
PFD_MAIN_PLANE, // Main Drawing Layer
|
||||||
0, // Reserved
|
0, // Reserved
|
||||||
0, 0, 0 // Layer Masks Ignored
|
0, 0, 0 // Layer Masks Ignored
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!(hDC=GetDC(hWnd))) // Did We Get A Device Context?
|
if (!(hDC=GetDC(hWnd))) // Did We Get A Device Context?
|
||||||
{
|
{
|
||||||
KillGLWindow(); // Reset The Display
|
KillGLWindow(); // Reset The Display
|
||||||
MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
|
MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
|
||||||
return FALSE; // Return FALSE
|
return FALSE; // Return FALSE
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?
|
if (!(pixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?
|
||||||
{
|
{
|
||||||
KillGLWindow(); // Reset The Display
|
KillGLWindow(); // Reset The Display
|
||||||
MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
|
MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
|
||||||
return FALSE; // Return FALSE
|
return FALSE; // Return FALSE
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?
|
if(!SetPixelFormat(hDC,pixelFormat,&pfd)) // Are We Able To Set The Pixel Format?
|
||||||
{
|
{
|
||||||
KillGLWindow(); // Reset The Display
|
KillGLWindow(); // Reset The Display
|
||||||
MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
|
MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
|
||||||
return FALSE; // Return FALSE
|
return FALSE; // Return FALSE
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(hRC=wglCreateContext(hDC))) // Are We Able To Get A Rendering Context?
|
if (!(hRC=wglCreateContext(hDC))) // Are We Able To Get A Rendering Context?
|
||||||
{
|
{
|
||||||
KillGLWindow(); // Reset The Display
|
KillGLWindow(); // Reset The Display
|
||||||
MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
|
MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
|
||||||
return FALSE; // Return FALSE
|
return FALSE; // Return FALSE
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!wglMakeCurrent(hDC,hRC)) // Try To Activate The Rendering Context
|
if(!wglMakeCurrent(hDC,hRC)) // Try To Activate The Rendering Context
|
||||||
{
|
{
|
||||||
KillGLWindow(); // Reset The Display
|
KillGLWindow(); // Reset The Display
|
||||||
MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
|
MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
|
||||||
return FALSE; // Return FALSE
|
return FALSE; // Return FALSE
|
||||||
}
|
}
|
||||||
|
|
||||||
ShowWindow(hWnd,SW_SHOW); // Show The Window
|
ShowWindow(hWnd,SW_SHOW); // Show The Window
|
||||||
SetForegroundWindow(hWnd); // Slightly Higher Priority
|
SetForegroundWindow(hWnd); // Slightly Higher Priority
|
||||||
@@ -519,18 +528,18 @@ BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscree
|
|||||||
ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen
|
ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen
|
||||||
|
|
||||||
if (!InitGL()) // Initialize Our Newly Created GL Window
|
if (!InitGL()) // Initialize Our Newly Created GL Window
|
||||||
{
|
{
|
||||||
KillGLWindow(); // Reset The Display
|
KillGLWindow(); // Reset The Display
|
||||||
MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
|
MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
|
||||||
return FALSE; // Return FALSE
|
return FALSE; // Return FALSE
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!InitGame()) // Initialize Our Game
|
if (!InitGame()) // Initialize Our Game
|
||||||
{
|
{
|
||||||
KillGLWindow(); // Reset The Display
|
KillGLWindow(); // Reset The Display
|
||||||
MessageBox(NULL,"Game Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
|
MessageBox(NULL,"Game Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
|
||||||
return FALSE; // Return FALSE
|
return FALSE; // Return FALSE
|
||||||
}
|
}
|
||||||
|
|
||||||
return TRUE; // Success
|
return TRUE; // Success
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user