//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include "GameClient.h"
//-----------------------------------------------------------------
// Game Engine Functions
//-----------------------------------------------------------------
BOOL GameInitialize(HINSTANCE hInstance)
{
// Create the game engine
// NOTE: _pGame is a global variable from GameClient.h
_pGame = new GameEngine(hInstance, TEXT("Game Skeleton"),
TEXT("Game Skeleton"));
if (_pGame == NULL)
{
return FALSE;
}
// Set the frame rate
_pGame->SetFrameRate(15);
//Perform OpenGL Initilization
HDC hDC;
HWND hWindow = _pGame->GetWindow();
hDC = GetDC(hWindow);
//Set up the Pixel Format
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.nVersion = 1; //Version
pfd.iPixelType = PFD_TYPE_RGBA; //Color Type
pfd.cColorBits = 32; //Desired Color Depth
pfd.cDepthBits = 24; //Depth Buffer
pfd.iLayerType = PFD_MAIN_PLANE; //Main layer
//Choose the best matching pixel format, return index
int pixelFormat = -1234;
pixelFormat = ChoosePixelFormat(hDC, &pfd);
//Test the pixel format
if (pixelFormat == -1234)
{
MessageBox(NULL, "OpenGL could not select a pixel format.", "An error occurred", MB_ICONERROR | MB_OK);
}
//Set pixel format to device context and test
if(!SetPixelFormat(hDC, pixelFormat, &pfd))
{
MessageBox(NULL, "A pixel format could not be set.", "An error occurred", MB_ICONERROR | MB_OK);
}
//Set up a Rendering Context
//Set the version we want. In this case, it is version 3.0
int attribs[] = {
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
WGL_CONTEXT_MINOR_VERSION_ARB, 0,
0}; //Zero signifies the end of the array
//Create a temporary context so that we can get a pointer to the function we want.
HGLRC tempContext = wglCreateContext(hDC);
//Make the temporary context current
wglMakeCurrent(hDC, tempContext);
//Grabe the function pointer we want
PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB;
wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
//Test versions. If this is null, then 3.0 is not supported.
if(!wglCreateContextAttribsARB)
{
MessageBox(NULL, "OpenGL 3.0 is not supported", "An error occurred", MB_ICONERROR | MB_OK);
//Because OpenGL 3.0 is not supported, wglCreateContextAttribsARB cannot be used to
//create our context. We instead will have to use wglCreateContext. This means that
//our temporary context is now our main context.
//Deselect the temporary context
wglMakeCurrent(hDC, NULL);
//Create the new context
renderContext = wglCreateContext(hDC);
}
else
{
//Create a new OpenGL 3.0 context
renderContext = wglCreateContextAttribsARB(hDC, 0, attribs);
//Deselect the temp context
wglMakeCurrent(hDC, NULL);
//Delete the temporary context.
wglDeleteContext(tempContext);
}
//Check the Rendering Context
if(!renderContext)
{
MessageBox(NULL, "Failed to create a Rendering Context", "An error occurred", MB_ICONERROR | MB_OK);
}
//Make the new context active and test.
if(!(wglMakeCurrent(hDC, renderContext)))
{
MessageBox(NULL, "Rendering Context could not be made active.", "An error occurred", MB_ICONERROR | MB_OK);
}
//OpenGL Drawing initialization
//Enable Depth testing
glEnable(GL_DEPTH_TEST);
//Set background
glClearColor(0.75f, 0.5f, 0.5f, 0.5f);
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity();
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity();
ReleaseDC(hWindow, hDC);
return TRUE;
}
void GameStart(HWND hWindow)
{
// Seed the random number generator
srand(GetTickCount());
}
void GameDeactivate(HWND hWindow)
{
HDC hDC;
RECT rect;
// Draw deactivation text on the game screen
GetClientRect(hWindow, &rect);
hDC = GetDC(hWindow);
DrawText(hDC, TEXT("Deactivated!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
ReleaseDC(hWindow, hDC);
}
void GamePaint(HDC hDC)
{
}
void GameCycle()
{
HDC hDC;
HWND hWindow = _pGame->GetWindow();
hDC = GetDC(hWindow);
//OpenGL drawing
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-0.5f, -0.5f, -2.0f);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(0.5f, -0.5f, -2.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.5f, -2.0f);
glEnd();
//SwapBuffers(hDC);
ReleaseDC(hWindow, hDC);
}
void GameEnd()
{
// Cleanup the game engine
delete _pGame;
}
void GameActivate(HWND hWindow)
{
HDC hDC;
RECT rect;
// Draw activation text on the game screen
GetClientRect(hWindow, &rect);
hDC = GetDC(hWindow);
DrawText(hDC, TEXT("Activated!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
ReleaseDC(hWindow, hDC);
}