// View.cpp: implementation of the CView class.
//
//////////////////////////////////////////////////////////////////////
#include "View.h"
#include "application.h"
// the various scenes
#include "testscene.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CView::CView()
: pScene(NULL),
hDC(0),
hRC(0),
hWnd(0)
{
}
CView::~CView()
{
delete pScene;
OutputDebugString("CView destructor\n");
pApp->pLog->Log("CView destructor called");
}
void CView::Update()
{
pScene->ProcessInput();
pScene->Update();
SwapBuffers(hDC);
}
void CView::Resize()
{
pApp->pLog->Log("CView::Resize");
pScene->Resize();
}
bool CView::Init()
{
// Create Our OpenGL Window
pApp->pData->Window_Bits = 16;
pApp->pData->SetDimension(640, 480);
if (!CreateMainWindow())
{
pApp->pLog->Log("CreateWindow failed!");
return false;
}
// Resize window if in fullscreen mode
// In window mode we will get a WM_SIZE message right
// after WM_CREATE
if (bFullscreen)
Resize();
if (!ShowTestScene())
return false;
return true;
}
bool CView::CreateMainWindow()
{
DWORD dwExStyle; // Window Extended Style
DWORD dwStyle; // Window Style
RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
WindowRect.left=(long)0; // Set Left Value To 0
WindowRect.right=pApp->pData->Window_Width; // Set Right Value To Requested Width
WindowRect.top=(long)0; // Set Top Value To 0
WindowRect.bottom=pApp->pData->Window_Height; // Set Bottom Value To Requested Height
if (!_RegisterMyClass())
return false;
if (bFullscreen) // Attempt Fullscreen Mode?
{
if (!SwitchToFullscreen())
// couldn't set fullscreen mode and user didn't want to use
// windowed mode
return false;
}
// Are We Still In Fullscreen Mode?
if (bFullscreen)
{
pApp->pLog->Log(" Entering Fullscreen mode @ %dx%dx%d bits",
pApp->pData->Window_Width, pApp->pData->Window_Height, pApp->pData->Window_Bits);
dwExStyle=WS_EX_APPWINDOW; // Window Extended Style
dwStyle=WS_POPUP; // Windows Style
ShowCursor(FALSE); // Hide Mouse Pointer
}
else
{
pApp->pLog->Log(" Entering windowed mode %dx%dx%d bits",
pApp->pData->Window_Width, pApp->pData->Window_Height, pApp->pData->Window_Bits);
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style
}
AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size
// Create The Window
pApp->pLog->Log(" Creating window...");
if (!(hWnd=CreateWindowEx( dwExStyle, // Extended Style For The Window
szClassName, // Class Name
szProgName, // Window Title
dwStyle | // Defined Window Style
WS_CLIPSIBLINGS | // Required Window Style
WS_CLIPCHILDREN, // Required Window Style
0, 0, // Window Position
pApp->pData->Window_Width, // Calculate Window Width
pApp->pData->Window_Height, // Calculate Window Height
NULL, // No Parent Window
NULL, // No Menu
pApp->hInst, // Instance
NULL))) // Dont Pass Anything To WM_CREATE
{
KillWindow(); // Reset The Display
pApp->pLog->Log(" Window Creation Error");
MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
if (!_SetPixelFormat())
{
KillWindow();
return false;
}
if (!(hRC=wglCreateContext(hDC))) // Are We Able To Get A Rendering Context?
{
KillWindow(); // Reset The Display
pApp->pLog->Log(" Can't Create A GL Rendering Context");
MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
pApp->pLog->Log(" GL Rendering Context Created");
if(!wglMakeCurrent(hDC,hRC)) // Try To Activate The Rendering Context
{
KillWindow(); // Reset The Display
pApp->pLog->Log(" Can't Activate The GL Rendering Context");
MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
pApp->pLog->Log(" GL Rendering Context Activated");
ShowWindow(hWnd,SW_SHOW); // Show The Window
pApp->pLog->Log(" Window Displayed");
SetForegroundWindow(hWnd); // Slightly Higher Priority
SetFocus(hWnd); // Sets Keyboard Focus To The Window
pApp->pLog->Log(" Initialization of OpenGL Was a Success");
pApp->pLog->Log("Window Initialization Completed");
return TRUE; // Success
}
void CView::KillWindow()
{
pApp->pLog->Log("Killing window...");
if (bFullscreen) // Are We In Fullscreen Mode?
{
ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop
ShowCursor(TRUE); // Show Mouse Pointer
}
if (hRC) // Do We Have A Rendering Context?
{
if (!wglMakeCurrent(NULL,NULL)) // Are We Able To Release The DC And RC Contexts?
{
pApp->pLog->Log(" Release Of DC And RC Failed");
}
if (!wglDeleteContext(hRC)) // Are We Able To Delete The RC?
{
pApp->pLog->Log(" Release Rendering Context Failed");
}
hRC=NULL; // Set RC To NULL
}
if (hDC && !ReleaseDC(hWnd,hDC)) // Are We Able To Release The DC
{
pApp->pLog->Log(" Release Device Context Failed");
hDC=NULL; // Set DC To NULL
}
if (hWnd && !DestroyWindow(hWnd)) // Are We Able To Destroy The Window?
{
pApp->pLog->Log(" Could Not Release hWnd");
hWnd=NULL; // Set hWnd To NULL
}
if (!UnregisterClass(szClassName, pApp->hInst)) // Are We Able To Unregister Class
{
pApp->pLog->Log(" Could Not Unregister Class");
}
pApp->pLog->Log(" Success");
}
void CView::Exit()
{
KillWindow();
}
bool CView::_RegisterMyClass()
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.
wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages
wc.cbClsExtra = 0; // No Extra Window Data
wc.cbWndExtra = 0; // No Extra Window Data
wc.hInstance = pApp->hInst; // Set The Instance
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
wc.hbrBackground = NULL; // No Background Required For GL
wc.lpszMenuName = NULL; // We Don't Want A Menu
wc.lpszClassName = szClassName; // Set The Class Name
if (!RegisterClass(&wc)) // Attempt To Register The Window Class
{
pApp->pLog->Log("Failed To Register The Window Class");
return FALSE; // Return FALSE
}
pApp->pLog->Log(" Window Class Registered");
return true;
}
bool CView::SwitchToFullscreen()
{
DEVMODE dmScreenSettings; // Device Mode
memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared
dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure
dmScreenSettings.dmPelsWidth = pApp->pData->Window_Width; // Selected Screen Width
dmScreenSettings.dmPelsHeight = pApp->pData->Window_Height; // Selected Screen Height
dmScreenSettings.dmBitsPerPel = pApp->pData->Window_Bits; // Selected Bits Per Pixel
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
// 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 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)
{
pApp->pLog->Log(" Fullscreen mode %dx%d %d bits not supported",
pApp->pData->Window_Width, pApp->pData->Window_Height, pApp->pData->Window_Bits);
bFullscreen=FALSE; // Windowed Mode Selected. Fullscreen = FALSE
}
else
{
// Pop Up A Message Box Letting User Know The Program Is Closing.
pApp->pLog->Log(" Fullscreen mode %dx%d %d bits not supported\n\tClosing program",
pApp->pData->Window_Width, pApp->pData->Window_Height, pApp->pData->Window_Bits);
MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
return FALSE; // Return FALSE
}
}
return true;
}
bool CView::_SetPixelFormat()
{
static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
{
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
pApp->pData->Window_Bits, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};
if (!(hDC=GetDC(hWnd))) // Did We Get A Device Context?
{
KillWindow(); // Reset The Display
pApp->pLog->Log(" Can't Create A GL Device Context");
MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
pApp->pLog->Log(" GL Device Context Created");
// Holds The Results After Searching For A Match
GLuint PixelFormat;
if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?
{
KillWindow(); // Reset The Display
pApp->pLog->Log(" Can't Find A Suitable PixelFormat");
MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
pApp->pLog->Log(" Matching Pixel Format Found");
if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?
{
KillWindow(); // Reset The Display
pApp->pLog->Log(" Can't Set The PixelFormat");
MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
pApp->pLog->Log(" Pixel Format Set");
return true;
}
bool CView::ShowTestScene()
{
// delete old scene
if (pScene != NULL)
{
delete pScene;
}
pScene = new CTestScene();
if (!pScene->Init())
{
// fatal exit
KillWindow();
pApp->pLog->Log(" Initialization of TestScene failed");
MessageBox(NULL,"Initialization of TestScene Failed.","Fatal Error",MB_OK|MB_ICONEXCLAMATION);
return false;
}
return true;
}