Выбрать главу

GLvoid KillGLWindow(GLvoid) // Properly Kill The Window

{ }

The CreateGLWindow() code is also the same as lesson 1.

BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag) { }

The only change in WndProc() is the addition of WM_LBUTTONDOWN. What it does is checks to see if the left mouse button was pressed. If it was, the rendering state is toggled from polygon mode to line mode, or from line mode to polygon mode.

LRESULT CALLBACK WndProc(HWND hWnd, // Handle For This Window

 UINT uMsg, // Message For This Window

 WPARAM wParam, // Additional Message Information

 LPARAM lParam) // Additional Message Information

{

 switch (uMsg) // Check For Windows Messages

 {

 case WM_ACTIVATE: // Watch For Window Activate Message

 {

  if (!HIWORD(wParam)) // Check Minimization State

  {

   active=TRUE; // Program Is Active

  } else {

   active=FALSE; // Program Is No Longer Active

  }

  return 0; // Return To The Message Loop

 }

 case WM_SYSCOMMAND: // Intercept System Commands

 {

  switch (wParam) // Check System Calls

  {

  case SC_SCREENSAVE: // Screensaver Trying To Start?

  case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?

   return 0; // Prevent From Happening

  }

  break; // Exit

 }

 case WM_CLOSE: // Did We Receive A Close Message?

 {

  PostQuitMessage(0); // Send A Quit Message

  return 0; // Jump Back

 }

 case WM_LBUTTONDOWN: // Did We Receive A Left Mouse Click?

 {

  bRender = !bRender; // Change Rendering State Between Fill/Wire Frame

  return 0; // Jump Back

 }

 case WM_KEYDOWN: // Is A Key Being Held Down?

 {

  keys[wParam] = TRUE; // If So, Mark It As TRUE

  return 0; // Jump Back

 }

 case WM_KEYUP: // Has A Key Been Released?

 {

  keys[wParam] = FALSE; // If So, Mark It As FALSE

  return 0; // Jump Back

 }

 case WM_SIZE: // Resize The OpenGL Window

 {

  ReSizeGLScene(LOWORD(lParam), HIWORD(lParam)); // LoWord=Width, HiWord=Height

  return 0; // Jump Back

 }

 }

 // Pass All Unhandled Messages To DefWindowProc

 return DefWindowProc(hWnd, uMsg, wParam, lParam);

}

No major changes in this section of code. The only notable change is the title of the window. Everything else is the same up until we check for key presses.

int WINAPI WinMain(HINSTANCE hInstance, // Instance

 HINSTANCE hPrevInstance, // Previous Instance

 LPSTR lpCmdLine, // Command Line Parameters

 int nCmdShow) // Window Show State

{

 MSG msg; // Windows Message Structure

 BOOL done=FALSE; // Bool Variable To Exit Loop

 // Ask The User Which Screen Mode They Prefer

 if (MessageBox(NULL, "Would You Like To Run In Fullscreen Mode?", "Start FullScreen?", MB_YESNO|MB_ICONQUESTION) == IDNO) {

  fullscreen=FALSE; // Windowed Mode

 }

 // Create Our OpenGL Window

 if (!CreateGLWindow("NeHe & Ben Humphrey's Height Map Tutorial", 640, 480, 16, fullscreen)) {

  return 0; // Quit If Window Was Not Created

 }

 while(!done) // Loop That Runs While done=FALSE

 {

  if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) // Is There A Message Waiting?

  {

   if (msg.message==WM_QUIT) // Have We Received A Quit Message?

   {

    done=TRUE; // If So done=TRUE

   } else // If Not, Deal With Window Messages

   {

    TranslateMessage(&msg); // Translate The Message

    DispatchMessage(&msg); // Dispatch The Message

   }

  } else // If There Are No Messages

  {

   // Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()

   if ((active && !DrawGLScene()) || keys[VK_ESCAPE]) // Active? Was There A Quit Received?

   {

    done=TRUE; // ESC or DrawGLScene Signalled A Quit

   } else if (active) // Not Time To Quit, Update Screen

   {

    SwapBuffers(hDC); // Swap Buffers (Double Buffering)

   }

   if (keys[VK_F1]) // Is F1 Being Pressed?

   {

    keys[VK_F1]=FALSE; // If So Make Key FALSE

    KillGLWindow(); // Kill Our Current Window

    fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode

    // Recreate Our OpenGL Window

    if (!CreateGLWindow("NeHe & Ben Humphrey's Height Map Tutorial", 640, 480, 16, fullscreen)) {

     return 0; // Quit If Window Was Not Created

    }

   }

The code below lets you increase and decrease the scaleValue. By pressing the up key, the scaleValue is increased, making the landscape larger. By pressing the down key, the scaleValue is decreased making the landscape smaller.

   if (keys[VK_UP]) // Is The UP ARROW Being Pressed?

    scaleValue += 0.001f; // Increase The Scale Value To Zoom In

   if (keys[VK_DOWN]) // Is The DOWN ARROW Being Pressed?

    scaleValue –= 0.001f; // Decrease The Scale Value To Zoom Out

  }

 }

 // Shutdown

 KillGLWindow(); // Kill The Window

 return (msg.wParam); // Exit The Program

}

That's all there is to creating a beautiful height mapped landscape. I hope you appreciate Ben's work! As always, if you find mistakes in the tutorial or the code, please email me, and I will attempt to correct the problem / revise the tutorial.

Once you understand how the code works, play around a little. One thing you could try doing is adding a little ball that rolls across the surface. You already know the height of each section of the landscape, so adding the ball should be no problem. Other things to try: Create the heightmap manually, make it a scrolling landscape, add colors to the landscape to represent snowy peaks / water / etc, add textures, use a plasma effect to create a constantly changing landscape. The possibilities are endless :)

Hope you enjoyed the tut! You can visit Ben's site at: http://www.GameTutorials.com.

Ben Humphrey (DigiBen)