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

LRESULT CALLBACK WndProc() // Handle For This Window

In WinMain() there are a few changes. First thing to note is the new caption on the title bar :)

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("Piotr Cieslak & NeHe's Morphing Points 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 && keys[VK_ESCAPE]) // Active? Was There A Quit Received?

   {

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

   } else // Not Time To Quit, Update Screen

   {

    DrawGLScene(); // Draw The Scene (Don't Draw When Inactive 1% CPU Use)

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

The code below watches for key presses. By now you should understand the code fairly easily. If page up is pressed we increase zspeed. This causes the object to spin faster on the z axis in a positive direction.

If page down is pressed we decrease zspeed. This causes the object to spin faster on the z axis in a negative direction.

If the down arrow is pressed we increase xspeed. This causes the object to spin faster on the x axis in a positive direction.

If the up arrow is pressed we decrease xspeed. This causes the object to spin faster on the x axis in a negative direction.

If the right arrow is pressed we increase yspeed. This causes the object to spin faster on the y axis in a positive direction.

If the left arrow is pressed we decrease yspeed. This causes the object to spin faster on the y axis in a negative direction.

    if(keys[VK_PRIOR]) // Is Page Up Being Pressed?

     zspeed+=0.01f; // Increase zspeed

    if(keys[VK_NEXT]) // Is Page Down Being Pressed?

     zspeed-=0.01f; // Decrease zspeed

    if(keys[VK_DOWN]) // Is Page Up Being Pressed?

     xspeed+=0.01f; // Increase xspeed

    if(keys[VK_UP]) // Is Page Up Being Pressed?

     xspeed-=0.01f; // Decrease xspeed

    if(keys[VK_RIGHT]) // Is Page Up Being Pressed?

     yspeed+=0.01f; // Increase yspeed

    if(keys[VK_LEFT]) // Is Page Up Being Pressed?

     yspeed-=0.01f; // Decrease yspeed

The following keys physically move the object. 'Q' moves it into the screen, 'Z' moves it towards the viewer, 'W' moves the object up, 'S' moves it down, 'D' moves it right, and 'A' moves it left.

    if (keys['Q']) // Is Q Key Being Pressed?

     cz-=0.01f; // Move Object Away From Viewer

    if (keys['Z']) // Is Z Key Being Pressed?

     cz+=0.01f; // Move Object Towards Viewer

    if (keys['W']) // Is W Key Being Pressed?

     cy+=0.01f; // Move Object Up

    if (keys['S']) // Is S Key Being Pressed?

     cy-=0.01f; // Move Object Down

    if (keys['D']) // Is D Key Being Pressed?

     cx+=0.01f; // Move Object Right

    if (keys['A']) // Is A Key Being Pressed?

     cx-=0.01f; // Move Object Left

Now we watch to see if keys 1 through 4 are pressed. If 1 is pressed and key is not equal to 1 (not the current object already) and morph is false (not already in the process of morphing), we set key to 1, so that our program knows we just selected object 1. We then set morph to TRUE, letting our program know it's time to start morphing, and last we set the destination object (dest) to equal object 1 (morph1).

Pressing keys 2, 3, and 4 does the same thing. If 2 is pressed we set dest to morph2, and we set key to equal 2. Pressing 3, sets dest to morph3 and key to 3.

By setting key to the value of the key we just pressed on the keyboard, we prevent the user from trying to morph from a sphere to a sphere or a cone to a cone!

    if (keys['1'] && (key!=1) && !morph) // Is 1 Pressed, key Not Equal To 1 And Morph False?

    {

     key=1; // Sets key To 1 (To Prevent Pressing 1 2x In A Row)

     morph=TRUE; // Set morph To True (Starts Morphing Process)

     dest=&morph1; // Destination Object To Morph To Becomes morph1

    }

    if (keys['2'] && (key!=2) && !morph) // Is 2 Pressed, key Not Equal To 2 And Morph False?

    {

     key=2; // Sets key To 2 (To Prevent Pressing 2 2x In A Row)

     morph=TRUE; // Set morph To True (Starts Morphing Process)

     dest=&morph2; // Destination Object To Morph To Becomes morph2

    }

    if (keys['3'] && (key!=3) && !morph) // Is 3 Pressed, key Not Equal To 3 And Morph False?

    {

     key=3; // Sets key To 3 (To Prevent Pressing 3 2x In A Row)

     morph=TRUE; // Set morph To True (Starts Morphing Process)

     dest=&morph3; // Destination Object To Morph To Becomes morph3

    }

    if (keys['4'] && (key!=4) && !morph) // Is 4 Pressed, key Not Equal To 4 And Morph False?

    {

     key=4; // Sets key To 4 (To Prevent Pressing 4 2x In A Row)

     morph=TRUE; // Set morph To True (Starts Morphing Process)

     dest=&morph4; // Destination Object To Morph To Becomes morph4

    }

Finally we watch to see if F1 is pressed if it is we toggle from Fullscreen to Windowed mode or Windowed mode to Fullscreen mode!

    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