confusing me with pointers and references, c ++ (Get WinAPI mouse coords)

advertisements

I have a class:

class Mouse
{
public:
    int x;
    int y;
    void MouseMove( int x, int y );
    Mouse();
};

I include it as a header file to my windows.cpp that contains WndProc and WinMain functions. Just underneath that include I declare a static instance of my mouse:

#include "Mouse.h"
#include "Game.h"

static Mouse mouse;

My mouse methods look like this:

#include "Mouse.h"

void Mouse::MouseMove( int x, int y )
{
    this->x = x;
    this->y = y;
}

On my WndProc I am processing WM_MOUSEMOUSE and passing the x and y values (which have correct values in through to my MouseMove function:

case WM_MOUSEMOVE:
            {

                int x = ( short )LOWORD( lParam );
                int y = ( short )HIWORD( lParam );

                bool leftButtonDown = wParam & MK_LBUTTON;
                bool rightButtonDown = wParam & MK_RBUTTON;

                mouse.MouseMove( x, y );

            }
            break;

My MouseMove function runs through and sets this->x to x successfully and the same with the y value. That is all... done.

Now, in my windows loop I am running my game (theGame.Go):

Game theGame = Game( hWnd );

    MSG msg;

    ZeroMemory( &msg, sizeof(msg) );

    while( msg.message!=WM_QUIT )
    {
       if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
       {
          TranslateMessage( &msg );
          DispatchMessage( &msg );
       }
       else
       {
            theGame.Go();
       }
    }

My game header looks like this:

#include "Mouse.h"

    class Game
    {
    public:
        Game( HWND hWnd );
        ~Game();
        void Go();
        void ComposeFrame();

        LPD3DXSPRITE sprite;
        LPDIRECT3DTEXTURE9 gTexture;
        D3DXVECTOR3 pos;
    private:
        D3DGraphics gfx;
    };

My Game construct looks like this:

Game::Game( HWND hWnd )
    :
    gfx( hWnd )
{
    HRESULT result;

    sprite = NULL;
    result = D3DXCreateSprite( gfx.d3dDevice, &sprite );
    assert( !FAILED( result ) );

    gTexture = NULL;
    result = D3DXCreateTextureFromFile( gfx.d3dDevice, "Images/character001.png", &gTexture );
    assert( !FAILED( result ) );

    gfx.d3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE);
};

The game object has no idea that the mouse object declared in my windows.cpp exists regardless of the fact I have declared it globally there. So I think to myself, I need to pass the mouse object by reference into my Game object. I start by modifying the windows loop like so:

Game theGame = Game( hWnd, &mouse );

    MSG msg;

    ZeroMemory( &msg, sizeof(msg) );

    while( msg.message!=WM_QUIT )
    {
       if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
       {
          TranslateMessage( &msg );
          DispatchMessage( &msg );
       }
       else
       {
            theGame.Go();
       }
    }

Now I add a few parameters to my Game.h class so that I have a reference to the memory and can get the *mouse.x from it later...:

class Game
{
public:
    Game( HWND hWnd, Mouse &mouse );
...

I go back to look at my windows loop and there's a squiggly under my call:

Game theGame = Game( hWnd, &mouse );

which states that:

6   IntelliSense: no instance of constructor "Game::Game" matches the argument list
        argument types are: (HWND, Mouse *) c:\Users\James\Documents\Visual Studio 2012\Projects\Game\Game\Windows.cpp  75  17  Game

I don't get it? How on earth do I just change one global instance of my mouse and call it from within my god damn Game object :(


You've declared your Game::Game constructor as taking a reference to a Mouse but you are passing it a pointer.

Either change to:

Game::Game(HWND hWnd, Mouse* mouse);

or:

Game(hWnd, mouse);

Also note that when you call your Game constructor, you are actually making an unnecessary copy:

Game theGame = Game( hWnd, &mouse );

Instead, change this to:

Game theGame( hWnd, &mouse );