Pyramid: Why does Switch Case WM_MOUSEMOVE not work for me?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: SHC (SHC_at_discussions.microsoft.com)
Date: 08/01/04


Date: Sat, 31 Jul 2004 21:25:02 -0700

Hi all,

I just inserted the case WM_MOUSEMOVE:
                {
                        ptCurrentMousePosit.x = LOWORD (lParam);
                        ptCurrentMousePosit.y = HIWORD (lParam);

                        if( bMousing )
                        {
                                g_fSpinX -= (ptCurrentMousePosit.x - ptLastMousePosit.x);
                                g_fSpinY -= (ptCurrentMousePosit.y - ptLastMousePosit.y);
                        }

                        ptLastMousePosit.x = ptCurrentMousePosit.x;
                        ptLastMousePosit.y = ptCurrentMousePosit.y;
                }
                break;

into my 4t1q_Pyramid.cpp (see the attached code). I did 'Build' and 'Start without Debugging'. The Pyramind appeared on my PC screen. But it did not turn, if I applied the Mouse curser on the figure and dragged it. Please tell me why it does not work and how I should correct the problem.

Thanks in advanve,
SHC
//////4t1q_Pyramid.cpp//////////
#include <windows.h>
#include <d3d9.h>
#include <d3dx9tex.h>
// #include <dxguid.h> //added 12 June 2004
#include <dxerr9.h> //added 12 June 2004

HINSTANCE hInst; // holds the instance for this app
HWND wndHandle; // global window handle

LPDIRECT3D9 pD3D;
LPDIRECT3DDEVICE9 pd3dDevice;
LPDIRECT3DVERTEXBUFFER9 vertexBuffer;

float g_fSpinX = 0.0f;
float g_fSpinY = 0.0f;

// camera variables
D3DXMATRIX matView; // the view matrix
D3DXMATRIX matProj; // the projection matrix
D3DXVECTOR3 cameraPosition; // the position of the camera
D3DXVECTOR3 cameraLook; // where the camera is pointing

// A structure for our custom vertex type
struct CUSTOMVERTEX
{
        FLOAT x, y, z; // The untransformed, 3D position for the vertex
        DWORD color;
};

// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)

////////////////////////////////////////////// forward declarations
bool initWindow(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

bool initDirect3D(HWND hwnd);
void shutdownDirect3D(void);
bool createPyramid(void);

void createCamera(float nearClip, float farClip);
void moveCamera(D3DXVECTOR3 vec);
void pointCamera(D3DXVECTOR3 vec);

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
        // call our function to init and create our window
        if (!initWindow(hInstance))
        {
                MessageBox(NULL, "Unable to create window", "ERROR", MB_OK);
                return false;
        }

        if (!initDirect3D(wndHandle))
        {
                MessageBox(NULL, "Unable to init Direct3D", "ERROR", MB_OK);
                return false;
        }

        if (!createPyramid())
        {
                MessageBox(NULL, "Pyramid could not be created", "ERROR", MB_OK);
                return false;
        }

        createCamera(1.0f, 500.0f); // near clip plane, far clip plane
        moveCamera(D3DXVECTOR3(0.0f, 0.0f, -450.0f));
        pointCamera(D3DXVECTOR3(0.0f, 0.0f, 0.0f));

        D3DXMATRIX meshMat, meshScale, meshRotate;
        // set the rotation
        D3DXMatrixRotationY(&meshRotate, D3DXToRadian(75));
        // set the scaling
        D3DXMatrixScaling(&meshScale, 1.0f, 1.0f, 1.0f);
        // multiple the scaling and rotation matrices to create the meshMat matrix
        D3DXMatrixMultiply(&meshMat, &meshScale, &meshRotate);

        // transform the object in world space
        pd3dDevice->SetTransform(D3DTS_WORLD, &meshMat);

        // Main message loop:
        // Enter the message loop
    MSG msg;
    ZeroMemory( &msg, sizeof(msg) );
    while( msg.message!=WM_QUIT )
    {
                // check for messages
                if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
        {
                        TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
                // this is called when no messages are pending
                else
                {
                        // Clear the backbuffer to a black color
                        pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,255), 1.0f, 0 );

                        pd3dDevice->BeginScene();

                        pd3dDevice->SetStreamSource( 0, vertexBuffer, 0, sizeof(CUSTOMVERTEX) );
                    pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
                        
                        pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 1 );
                        pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 3, 1 );
                        pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 6, 1 );
                        pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 9, 1 );
 // pd3dDevice->DrawPrimitive( D3DPT_LINESTRIP, 12, 4 );
            pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 12, 2 );
 // pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 15, 1 );

                        pd3dDevice->EndScene();

                        // Present the backbuffer contents to the display
                        pd3dDevice->Present( NULL, NULL, NULL, NULL );
                }
    }

        // release and shutdown Direct3D
        shutdownDirect3D();

        return (int) msg.wParam;
}

bool initWindow(HINSTANCE hInstance)
{
        WNDCLASSEX wcex;

        wcex.cbSize = sizeof(WNDCLASSEX);
        wcex.style = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc = (WNDPROC)WndProc;
        wcex.cbClsExtra = 0;
        wcex.cbWndExtra = 0;
        wcex.hInstance = hInstance;
        wcex.hIcon = 0;
        wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wcex.lpszMenuName = NULL;
        wcex.lpszClassName = "DirectXExample";
        wcex.hIconSm = 0;
        RegisterClassEx(&wcex);

        wndHandle = CreateWindow("DirectXExample",
                                                         "DirectXExample",
                                                         WS_OVERLAPPEDWINDOW,
                                                         CW_USEDEFAULT,
                                                         CW_USEDEFAULT,
                                                         SCREEN_WIDTH,
                                                         SCREEN_HEIGHT,
                                                         NULL,
                                                         NULL,
                                                         hInstance,
                                                         NULL);
   if (!wndHandle)
      return false;
   
   ShowWindow(wndHandle, SW_SHOW);
   UpdateWindow(wndHandle);

   return true;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
   static POINT ptLastMousePosit;
        static POINT ptCurrentMousePosit;
        static bool bMousing;

        switch (message)
        {
                case WM_DESTROY:
                        PostQuitMessage(0);
                        break;

        case WM_MOUSEMOVE:
                {
                        ptCurrentMousePosit.x = LOWORD (lParam);
                        ptCurrentMousePosit.y = HIWORD (lParam);

                        if( bMousing )
                        {
                                g_fSpinX -= (ptCurrentMousePosit.x - ptLastMousePosit.x);
                                g_fSpinY -= (ptCurrentMousePosit.y - ptLastMousePosit.y);
                        }

                        ptLastMousePosit.x = ptCurrentMousePosit.x;
                        ptLastMousePosit.y = ptCurrentMousePosit.y;
                }
                break;

         
        }
        return DefWindowProc(hWnd, message, wParam, lParam);
}

bool createPyramid(void)
{
        // Initialize five vertices for rendering a pyramid
        CUSTOMVERTEX g_Vertices[] =
        {
       // Triangle #1
                //{ 0.0f, 0.0f, -64.0f, D3DCOLOR_ARGB(255,255,0,0)},
                { 64.0f, 64.0f, 0.0f, D3DCOLOR_ARGB(255,0,0,0)},
                { 64.0f, -64.0f, 0.0f, D3DCOLOR_ARGB(255,0,0,0)},
        { 0.0f, 0.0f, -64.0f, D3DCOLOR_ARGB(255,0,0,0)},

                // Triangle #2
                //{ 0.0f, 0.0f, -64.0f, D3DCOLOR_ARGB(0,255,0,0)},
                { 64.0f, -64.0f, 0.0f, D3DCOLOR_ARGB(0,255,0,0)},
                {-64.0f, -64.0f, 0.0f, D3DCOLOR_ARGB(0,255,0,0)},
        { 0.0f, 0.0f, -64.0f, D3DCOLOR_ARGB(0,255,0,0)},

                // Triangle #3
                { 0.0f, 0.0f, -64.0f, D3DCOLOR_ARGB(0,0,255,0)},
                {-64.0f, -64.0f, 0.0f, D3DCOLOR_ARGB(0,0,255,0)},
                {-64.0f, 64.0f, 0.0f, D3DCOLOR_ARGB(0,0,255,0)},

                // Triangle #4
                { 0.0f, 0.0f, -64.0f, D3DCOLOR_ARGB(0,0,0,255)},
                {-64.0f, 64.0f, 0.0f, D3DCOLOR_ARGB(0,0,0,255)},
                { 64.0f, 64.0f, 0.0f, D3DCOLOR_ARGB(0,0,0,255)},

                // Quad #1
                { 64.0f, 64.0f, 0.0f, D3DCOLOR_ARGB(0,0,0,0)},
                { 64.0f, -64.0f, 0.0f, D3DCOLOR_ARGB(0,255,0,0)},
                { -64.0f, -64.0f, 0.0f, D3DCOLOR_ARGB(0,0,255,0)},
                { -64.0f, 64.0f, 0.0f, D3DCOLOR_ARGB(0,0,0,255)},
            { 64.0f, 64.0f, 0.0f, D3DCOLOR_ARGB(0,0,255,0)}, //Repeat
        { -64.0f, -64.0f, 0.0f, D3DCOLOR_ARGB(0,0,0,0)}, //Repeat
        };

        // Create the vertex buffer.
    HRESULT hr;
        
        hr = pd3dDevice->CreateVertexBuffer(sizeof(g_Vertices) * sizeof(CUSTOMVERTEX),
                                                                              0,
                                                                                D3DFVF_CUSTOMVERTEX,
                                                                                D3DPOOL_DEFAULT,
                                                                                &vertexBuffer,
                                                                                NULL );
        if FAILED (hr)
                return false;

        // prepare to copy the vertices into the vertex buffer
        VOID* pVertices;
        // lock the vertex buffer
        hr = vertexBuffer->Lock(0, sizeof(g_Vertices), (void**)&pVertices, 0);

        // check to make sure the vertex buffer can be locked
        if FAILED (hr)
                return false;

        // copy the vertices into the buffer
    memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );

        // unlock the vertex buffer
        vertexBuffer->Unlock();

        return true;
}
bool initDirect3D(HWND hwnd)
{
        if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
                return false;
        
        D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
        d3dpp.BackBufferCount = 1;
        d3dpp.BackBufferHeight = SCREEN_HEIGHT;
        d3dpp.BackBufferWidth = SCREEN_WIDTH;
        d3dpp.hDeviceWindow = hwnd;
        
    if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &pd3dDevice ) ) )
    return false;
    
// pd3dDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(255, 255, 255));
// pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
    pd3dDevice->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_FLAT); // Flat shading for one color for each triangle
        pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
//Turn off Direct3D's culling
        pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
        return true;
}

void shutdownDirect3D(void)
{
    if( pd3dDevice != NULL)
        {
        pd3dDevice->Release();
                pd3dDevice = NULL;
        }
    if( pD3D != NULL)
        {
        pD3D->Release();
                pD3D = NULL;
        }
}

/*************************************************************************
* createCamera
* creates a virtual camera
*************************************************************************/
void createCamera(float nearClip, float farClip)
{
        //Here we specify the field of view, aspect ration and near and far clipping planes.
    D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 640/480, nearClip, farClip);
    pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);

}

/*************************************************************************
* moveCamera
* moves the camera to a position specified by the vector passed as a
* parameter
*************************************************************************/
void moveCamera(D3DXVECTOR3 vec)
{
        cameraPosition = vec;
}

/*************************************************************************
* pointCamera
* points the camera a location specified by the passed vector
*************************************************************************/
void pointCamera(D3DXVECTOR3 vec)
{
        cameraLook = vec;

        D3DXMatrixLookAtLH(&matView, &cameraPosition, //Camera Position
                                 &cameraLook, //Look At Position
                                 &D3DXVECTOR3(0.0f, 1.0f, 0.0f)); //Up Direction

        pd3dDevice->SetTransform(D3DTS_VIEW, &matView);
}



Relevant Pages