Mouse Buttons respons as pressed down?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Alx101
Posts: 15
Joined: Sat Feb 11, 2012 7:04 pm

Mouse Buttons respons as pressed down?

Post by Alx101 »

Hi, im having a problem with my code. Irrlicht seems to register that my mouse buttons it pressed down even though i don't press them. It is either that or my error checking is wrong.

Source

Code: Select all

#include <irrlicht.h>
#include "driverChoice.h"
#include <iostream>
#include "lua_inc.h"
 
 
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#pragma comment(lib, "Irrlicht.lib")
#endif
 
struct Mouse
{
    bool leftButton;
    bool rightButton;
    core::vector3df Position;
};
 
class EventH : public IEventReceiver
{
    public:
        Mouse mouse;
        virtual bool OnEvent(const SEvent& event)
            {
                if (event.EventType == irr::EET_KEY_INPUT_EVENT)
                        KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
 
                if (event.EventType == irr::EET_MOUSE_INPUT_EVENT)
                {
                        switch(event.MouseInput.Event)
                        {
                            case EMIE_LMOUSE_PRESSED_DOWN:
                                //MouseState.LeftButtonDown = true;
                                mouse.leftButton = true;
                                break;
 
                            case EMIE_LMOUSE_LEFT_UP:
                                //MouseState.LeftButtonDown = false;
                                mouse.leftButton = false;
                                break;
 
                            case EMIE_MOUSE_MOVED:
                                /*MouseState.Position.X = event.MouseInput.X;
                                MouseState.Position.Y = event.MouseInput.Y;
                                MouseState.Position.Z = 0;*/
                                mouse.Position.X = event.MouseInput.X;
                                mouse.Position.Y = event.MouseInput.Y;
                                mouse.Position.Z = 0;
                                break;
                            case EMIE_RMOUSE_PRESSED_DOWN:
                                //MouseState.RightButtonDown = true;
                                mouse.rightButton = true;
                            case EMIE_RMOUSE_LEFT_UP:
                                //MouseState.RightButtonDown = false;
                                mouse.rightButton = false;
 
                        default:
 
                                break;
                        }
                }
 
                return false;
        }
 
        virtual bool IsKeyDown(EKEY_CODE keyCode) const
        {
                return KeyIsDown[keyCode];
        }
 
        EventH()
        {
                for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
                        KeyIsDown[i] = false;
        }
    private:
        bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
 
 
int main()
{
    int iErr = 0;
    lua_State *lua = lua_open ();
 
    video::E_DRIVER_TYPE driverType=driverChoiceConsole();
    if (driverType==video::EDT_COUNT)
        return 1;
 
    EventH receiver;
    Mouse mS;
    IrrlichtDevice* device = createDevice(driverType,core::dimension2d<u32>(640, 480), 16, false, false, false, &receiver);
 
 
    device->setWindowCaption(L"Flare Ultimate Creation Kit");
 
    if (device == 0)
        return 1;
 
    video::IVideoDriver* driver = device->getVideoDriver();
    scene::ISceneManager* smgr = device->getSceneManager();
    IGUIEnvironment* guienv = device->getGUIEnvironment();
    /*video::ITexture* mS = driver->getTexture("C:/Users/Alx101/Dropbox/Echoes/GUIGameMaker/Pointer.png");*/
 
    guienv->addStaticText(L"Flare Ultimate Creation Kit",
        rect<int>(10,10,200,22), true);
 
    scene::ISceneNode * node = smgr->addSphereSceneNode();
        if (node)
        {
                node->setPosition(core::vector3df(0,0,30));
                node->setMaterialTexture(0, driver->getTexture("../../media/wall.bmp"));
                node->setMaterialFlag(video::EMF_LIGHTING, false);
        }
 
        scene::ISceneNode* n = smgr->addCubeSceneNode();
 
        if (n)
        {
                n->setMaterialTexture(0, driver->getTexture("../../media/t351sml.jpg"));
                n->setMaterialFlag(video::EMF_LIGHTING, false);
                scene::ISceneNodeAnimator* anim =
                        smgr->createFlyCircleAnimator(core::vector3df(0,0,30), 20.0f);
                if (anim)
                {
                        n->addAnimator(anim);
                        anim->drop();
                }
        }
 
    scene::IAnimatedMeshSceneNode* anms =
    smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/ninja.b3d"));
 
        if (anms)
        {
                scene::ISceneNodeAnimator* anim =
                        smgr->createFlyStraightAnimator(core::vector3df(100,0,60),
                        core::vector3df(-100,0,60), 3500, true);
                if (anim)
                {
                        anms->addAnimator(anim);
                        anim->drop();
                }
 
                anms->setMaterialFlag(video::EMF_LIGHTING, false);
 
                anms->setFrameLoop(0, 13);
                anms->setAnimationSpeed(15);
//              anms->setMD2Animation(scene::EMAT_RUN);
 
                anms->setScale(core::vector3df(2.f,2.f,2.f));
                anms->setRotation(core::vector3df(0,-90,0));
//              anms->setMaterialTexture(0, driver->getTexture("../../media/sydney.bmp"));
 
        }
    /*core::position2d<s32> m = device->getCursorControl()->getPosition();*/
    scene::ICameraSceneNode* camera = smgr->addCameraSceneNode();
    device->getCursorControl()->setVisible(true);
    /*guienv->addImage(mS, position2d<s32>(m.X, m.Y), false, 0, 1, 0);*/
    int lastFPS = -1;
 
    u32 then = device->getTimer()->getTime();
 
    const f32 MOVEMENT_SPEED = 10.f;
        while(device->run())
        {
                const u32 now = device->getTimer()->getTime();
                const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
                then = now;
                core::vector3df nodePosition = node->getPosition();
                core::vector3df cameraPos = camera->getPosition();
                   // camera->setPosition(core::vector3df());
                /*if(receiver.IsKeyDown(irr::KEY_KEY_W))
                        nodePosition.Z += MOVEMENT_SPEED * frameDeltaTime;
                else if(receiver.IsKeyDown(irr::KEY_KEY_S))
                        nodePosition.Z -= MOVEMENT_SPEED * frameDeltaTime;
 
                if(receiver.IsKeyDown(irr::KEY_KEY_E))
                    nodePosition.Y += MOVEMENT_SPEED * frameDeltaTime;
                if(receiver.IsKeyDown(irr::KEY_KEY_Q))
                    nodePosition.Y -= MOVEMENT_SPEED * frameDeltaTime;
 
                if(receiver.IsKeyDown(irr::KEY_KEY_A))
                        nodePosition.X -= MOVEMENT_SPEED * frameDeltaTime;
                else if(receiver.IsKeyDown(irr::KEY_KEY_D))
                        nodePosition.X += MOVEMENT_SPEED * frameDeltaTime;*/
                if(receiver.IsKeyDown(irr::KEY_ESCAPE))
                   device->closeDevice();
                if(mS.leftButton == true)
                {
                    cout << "HI";
                }
 
                //core::position2d<s32> m = device->getCursorControl()->getPosition();
                node->setPosition(nodePosition);
                driver->beginScene(true, true, video::SColor(255,113,113,133));
 
                smgr->drawAll(); // draw the 3d scene
                device->getGUIEnvironment()->drawAll(); // draw the gui environment (the logo)
 
                driver->endScene();
 
                int fps = driver->getFPS();
 
                if (lastFPS != fps)
                {
                        core::stringw tmp(L"[");
                        tmp += driver->getName();
                        tmp += L"] fps: ";
                        tmp += fps;
 
                        device->setWindowCaption(tmp.c_str());
                        lastFPS = fps;
                }
 
        }
 
    device->drop();
    return 0;
}
 
The ouput i get is that it prints out "HI" even though i don't press the left mouse button. There is nothing wrong with my mouse. Any help?
CuteAlien
Admin
Posts: 9942
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Mouse Buttons respons as pressed down?

Post by CuteAlien »

You have two complete independent instances of Mouse which have nothing to do with each other (except having the same structure). One in EventH and one in main(). You can't expect that the one in main - which you are checking - is affected by changing a complete different place in memory (receiver.mouse). Think of classes/structs as stamps. Think of your memory as paper. Now each time you create a variable you push your struct-stamp somewhere on that paper. And if you now write some values in that part of paper obviously all other parts are not affected.

Also you should initialize all variables. Especially until you really, really know what you are doing initialize _every single variable_!. For structs you can do that in the constructor. Right now leftButton has a complete random value depending on whatever was in memory before you put your struct "ms" there.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Alx101
Posts: 15
Joined: Sat Feb 11, 2012 7:04 pm

Re: Mouse Buttons respons as pressed down?

Post by Alx101 »

Oh ok. Didn't realize that. However i cant init the struct inside of the class since it gives me an error that should not be there :C.
Alx101
Posts: 15
Joined: Sat Feb 11, 2012 7:04 pm

Re: Mouse Buttons respons as pressed down?

Post by Alx101 »

It works now. Thank you so much for your patience in the unskilled ones
CuteAlien
Admin
Posts: 9942
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Mouse Buttons respons as pressed down?

Post by CuteAlien »

a) You can. For example in Your EvenH constructor you could write: "mouse.leftButton = false;"
b) Mouse can have it's own constructor - which is even better. So you can initialize all it's members there (as you usually should).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Alx101
Posts: 15
Joined: Sat Feb 11, 2012 7:04 pm

Re: Mouse Buttons respons as pressed down?

Post by Alx101 »

But don't i do that in the struct Mouse?
CuteAlien
Admin
Posts: 9942
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Mouse Buttons respons as pressed down?

Post by CuteAlien »

Unless I'm not seeing it you don't seem to initalize the values anywhere. There is no automatic initialization in c++ (neither in c).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Alx101
Posts: 15
Joined: Sat Feb 11, 2012 7:04 pm

Re: Mouse Buttons respons as pressed down?

Post by Alx101 »

I think i do it in here

Code: Select all

struct Mouse
{
     bool leftMouse;
     bool rightMouse;
     core::vector3df Position;
}
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Mouse Buttons respons as pressed down?

Post by serengeor »

Alx101 wrote:I think i do it in here

Code: Select all

struct Mouse
{
     bool leftMouse;
     bool rightMouse;
     core::vector3df Position;
}
Where?
CuteAlien wrote:There is no automatic initialization in c++ (neither in c).
Is the "leftMouse=false; rightMouse=false;" invisible or what?
Working on game: Marrbles (Currently stopped).
Alx101
Posts: 15
Joined: Sat Feb 11, 2012 7:04 pm

Re: Mouse Buttons respons as pressed down?

Post by Alx101 »

Oh got the initialization thing wrong. That is what i thought what you meant by initialization. The thing you meant i call to set the variable to a value.
Post Reply