📄 main.cpp
字号:
// Enable front face culling, since that's what Quake3 does
glCullFace(GL_FRONT);
glEnable(GL_CULL_FACE);
}
///////////////////////////////// MAIN GAME LOOP \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This function Handles the main game loop
/////
///////////////////////////////// MAIN GAME LOOP \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
WPARAM MainLoop()
{
MSG msg;
while(1) // Do our infinate loop
{ // Check if there was a message
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT) // If the message wasnt to quit
break;
TranslateMessage(&msg); // Find out what the message does
DispatchMessage(&msg); // Execute the message
}
else // if there wasn't a message
{
g_Camera.Update(); // Update the camera info
RenderScene(); // Render the scene every frame
}
}
DeInit(); // Clean up and free all allocated memory
return(msg.wParam); // Return from the program
}
///////////////////////////////// RENDER SCENE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This function renders the entire scene.
/////
///////////////////////////////// RENDER SCENE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void RenderScene()
{
int i = 0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The matrix
// Tell OpenGL where to look from our camera info
g_Camera.Look();
// Calculate our frustum to check the world data again for PVS and Portal Rendering
g_Frustum.CalculateFrustum();
// Render the level to the screen
static CVector3 s_pos;
s_pos = g_Camera.Position();
if (g_StopUpdate)
{
g_Level.RenderLevel(s_pos);
}
else
{
g_Level.RenderLevel(g_Camera.Position());
}
// Swap the backbuffers to the foreground
SwapBuffers(g_hDC);
}
///////////////////////////////// WIN PROC \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This function handles the window messages.
/////
///////////////////////////////// WIN PROC \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
LRESULT CALLBACK WinProc(HWND hWnd,UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LONG lRet = 0;
PAINTSTRUCT ps;
switch (uMsg)
{
case WM_SIZE: // If the window is resized
if(!g_bFullScreen) // Do this only if we are in window mode
{
SizeOpenGLScreen(LOWORD(lParam),HIWORD(lParam));// LoWord=Width, HiWord=Height
GetClientRect(hWnd, &g_rRect); // Get the window rectangle
}
break;
case WM_PAINT: // If we need to repaint the scene
BeginPaint(hWnd, &ps); // Init the paint struct
EndPaint(hWnd, &ps); // EndPaint, Clean up
break;
case WM_LBUTTONDOWN: // If we hit the left mouse button
g_bLightmaps = !g_bLightmaps; // Turn lightmaps on/off
if(!g_bLightmaps) // If we don't want lightmaps
{
glActiveTextureARB(GL_TEXTURE1_ARB); // Turn the second texture off
glDisable(GL_TEXTURE_2D);
}
break;
case WM_RBUTTONDOWN: // If we hit the right mouse button
g_bTextures = !g_bTextures; // Turn texture mapping on/off
if(!g_bTextures) // If we don't want texture mapping
{
glActiveTextureARB(GL_TEXTURE0_ARB);
glDisable(GL_TEXTURE_2D); // Disable texture mapping
}
break;
case WM_KEYDOWN:
switch(wParam) { // Check if we hit a key
case VK_ESCAPE: // If we hit the escape key
PostQuitMessage(0); // Send a QUIT message to the window
break;
case VK_F1: // Check if we hit the space bar
g_RenderMode = !g_RenderMode; // Change the rendering mode
// Change the rendering mode to and from lines or triangles
if(g_RenderMode)
{
// Render the triangles in fill mode
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
else
{
// Render the triangles in wire frame mode
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
break;
case VK_F2:
g_StopUpdate = !g_StopUpdate;
break;
/////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// *
case VK_SPACE: // Check if the user hits the space bar
// This is what will create our jump effect. Before we add our jump
// velocity, we make sure that we are on the ground. This makes it
// so we can't jump infinitely and keep ramming our head into the cieling.
// This type of realism depends on your project of course. If we are on
// the ground, we can then set our velocity.y value to our jump velocity
// which will be added to our camera (or player) every frame, until the
// gravity eventually cancels it out.
if(g_Level.IsOnGround())
g_vVelocity.y = kJumpAcceleration;
break;
/////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// *
}
break;
case WM_CLOSE: // If the window is being closes
PostQuitMessage(0); // Send a QUIT Message to the window
break;
default: // Return by default
lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
break;
}
return lRet; // Return by default
}
/////////////////////////////////////////////////////////////////////////////////
//
// * QUICK NOTES *
//
// In this file we added a define for our velocity vector. We also changed the
// camera position to place the camera in a different place of the world and then
// put a higher Y value than the floor so that it has a cool falling effect in
// the beginning of the tutorial. It shows our sweet gravity effect :)
//
//
// * What's Next? *
//
// When you code it, let us know :) *grin*
//
// As for the collision detection tutorials for BSP files we are finished.
//
//
// Ben Humphrey (DigiBen)
// Game Programmer
// DigiBen@GameTutorials.com
// Co-Web Host of www.GameTutorials.com
// ?2000-2003 GameTutorials
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -