Thread: cam tut query

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    55
    Rep Power
    12

    cam tut query


    I've been trying to understand a camera tutorial, where the cam gets moved around by pressing the arrow keys. I've included the section of source below, basically the test for the key is WinProc gets called by WPARAM mainloop by the following:

    TranslateMessage(&msg); // Find out what the message does
    DispatchMessage(&msg); // Execute the message

    Can anyone tell me what type of c++ procedure this is? I can't find it in any of the c tuts. Cheers

    ///////////////////////////////// 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
    {
    // Render the scene every frame
    RenderScene();
    }
    }

    DeInit(); // Free everything after the program finishes

    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

    //////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** ////////////////////

    // Now instead of passing in hard coded values, we give gluLookAt()
    // the values that our global camera has. Now we can check if we
    // hit the LEFT RIGHT UP and DOWN arrow keys, then update the camera.
    // It will automatically update right here. Cool huh?

    // Give openGL our camera position, then camera view, then camera up vector
    gluLookAt(g_Camera.m_vPosition.x, g_Camera.m_vPosition.y, g_Camera.m_vPosition.z,
    g_Camera.m_vView.x, g_Camera.m_vView.y, g_Camera.m_vView.z,
    g_Camera.m_vUpVector.x, g_Camera.m_vUpVector.y, g_Camera.m_vUpVector.z);

    //////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** ////////////////////

    // Below we say that we want to draw triangles
    glBegin (GL_TRIANGLES); // This is our BEGIN to draw

    glColor3ub(255, 0, 0); // Make the top vertex RED
    glVertex3f(0, 1, 0); // Here is the top point of the triangle

    glColor3ub(0, 255, 0); // Make the left vertex GREEN
    glVertex3f(-1, 0, 0); // Here is the left point of the triangle

    glColor3ub(0, 0, 255); // Make the right vertex BLUE
    glVertex3f(1, 0, 0); // Here is the right point of the triangle
    glEnd(); // This is the END of drawing

    SwapBuffers(g_hDC); // Swap the backbuffers to the foreground
    }

    ///////////////////////////////// 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_KEYDOWN:

    //////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** ////////////////////

    switch(wParam) { // Check if we hit a key
    case VK_ESCAPE:
    PostQuitMessage(0); // Send a QUIT message
    break;

    case VK_UP: // If we hit the UP arrow key
    g_Camera.MoveCamera(kSpeed); // Move our camera forward by a positive speed
    RenderScene(); // Redraw the screen to reflect changes
    break;

    case VK_DOWN: // If we hit the DOWN arrow key
    g_Camera.MoveCamera(-kSpeed); // Move our camera backward by a negative speed
    RenderScene(); // Redraw the screen to reflect changes
    break;
    }
    break;

    //////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** ////////////////////

    case WM_CLOSE: // If the window is being closed
    PostQuitMessage(0); // Post a QUIT message to the window; // Release memory and restore settings
    break;

    default: // Return by default
    lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
    break;
    }

    return lRet; // Return by default
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    YouŽll find TranslateMessage() and DispatchMessage() in the WinAPI documentation somewhere on http://msdn.microsoft.com.

    - and they are C functions.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    55
    Rep Power
    12
    Ok, I can mostly understand that now. The only question now is, if this bit:

    { // Check if there was a message
    if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {

    is checking windows to see if a key was pressed, how is the winproc section called? It must be called from mainloop, but I can't see how it's being called. Cheers
    Last edited by leftfield; July 24th, 2003 at 04:11 AM.

IMN logo majestic logo threadwatch logo seochat tools logo