Jimmyt1988
Jimmyt1988

Reputation: 21156

confusing myself with pointers and references, c++ (Getting mouse coords from WinAPI)

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 :(

Upvotes: 0

Views: 129

Answers (2)

Joseph Mansfield
Joseph Mansfield

Reputation: 110698

The problem that's causing your error is that your Game constructor takes a reference to Mouse:

Game( HWND hWnd, Mouse &mouse );

You, however, are passing a pointer to Mouse by taking the address of mouse:

Game theGame = Game( hWnd, &mouse );

You can change one or the other: either change the parameter type to a pointer (Mouse* mouse) or pass mouse as the argument.

However, this is an atypical approach to having a global object. By declaring mouse as static, you are giving it internal linkage and it cannot be accessed in any other translation unit. What you would generally do is have the following declaration in a single header file which you will include whenever you need access to the global mouse object:

extern Mouse mouse;

And then in a single implementation file provide the definition:

Mouse mouse;

Upvotes: 1

Jonathan Potter
Jonathan Potter

Reputation: 37192

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 );

Upvotes: 1

Related Questions