简体   繁体   中英

C++ and my global variable for mouse class

I'm so stuck, it's so annoying... I have windows message sending mouse coordinates and I have a game loop that sees those coordinates, but when I call upon a class that looks at the mouse coordinates, it can't see the mouse coordinates, it just creates its own version of the mouse regardless of me defining a global.h and referencing extern on the .cpp file that's using it:

Mouse.h

#pragma once

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

Global.h

#include "Mouse.h"

static Mouse mouse;

Game.cpp //snippet of code//

Game::Game( HWND hWnd, Mouse &mouse  )
    :
    gfx( hWnd ),
    mouse( mouse )
{
...
if( scenes[ a ]->interactiveObjects[ b ]->CheckCollision( mouse.x, mouse.y ) )
{
.... // Game is looping if no messages stored, the windows loop updates my mouse coordinates by calling a routine in my Mouse.cpp. My windows loop sends the mouse through as a parameter to my game object which I no longer want to use... I want to use my global mouse as the mouse reference.

"InteractiveObject.cpp" it contains the "Global.h" and references the mouse declared in it... right? So why does my check collision not see the mouse.x and mouse.y (I have to pass the coordinates in from my game object as paramteres mouseX and mouseY :(

#include "Global.h"
extern Mouse mouse;

#include "InteractiveObject.h"


InteractiveObject::InteractiveObject(  int id_, string title_, Image* theImage_, int type_, int x_, int y_, int z_ )
    : 
    id( id_ ),
    title( title_ ),
    theImage( theImage_ ),
    type( type_ ),
    x( x_ ),
    y( y_ ),
    z( z_ )
{
    pos.x = x_;
    pos.y = y_;
    pos.z = z_;
}

bool InteractiveObject::CheckCollision( int mouseX, int mouseY )
{
    if( 
        mouse.x > x &&
        mouse.x < x + theImage->width &&
        mouse.y > y &&
        mouse.y < y + theImage->height
    )   
    /*if( 
        mouseX > x &&
        mouseX < x + theImage->width &&
        mouseY > y &&
        mouseY < y + theImage->height
    )*/
    {
        return TRUE;
    }

    return FALSE;
}

您的global.h将被扩展到包含它的每个文件中。因此,包含它的每个文件都有一个全局的静态实例化(仅表示此文件)。尝试使用单例模式或在变量中将变量声明为extern。标头,然后在mouse.cpp中输入一次

I'm not a fan of singletons, but in the interest of reigning in your bugs for the time being...

Make it a singleton .

class Mouse
{
public:
    int x;
    int y;
    void MouseMove( int x, int y );
    static Mouse & get_mouse()
    {
        static Mouse m;
        return m;
    }
private:
    // Inaccessible outside of Mouse!
    Mouse();
    Mouse( const Mouse & );
};

Now your code will need to call Mouse::get_mouse() to get the one-and-only Mouse .

You will now be assured that additional instances of Mouse are not accidentally created anywhere. Your compiler will stop you if it happens.

In your global.h header you should have

extern Mouse mouse;

In one place in your code (and one place only) at file scope in an implementation file, you should have the line below. InteractiveObject.cpp works ok for this.

Mouse mouse;

This way, global.h promises any file unit that includes it that a mouse variable of type Mouse exists somewhere. The "Mouse mouse" line actually allocates that instance.

There are architectural implications of doing it this way, but it'll work. The way you had it, any implementation file that includes global.h would have created it's own local instance of "mouse" that was a different instance than any other. While the extern Mouse... line promised the linker that somewhere "mouse" existed at global scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM