Mouse Input Logic

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
Quantum1982
Posts: 45
Joined: Mon Apr 23, 2012 9:31 am

Mouse Input Logic

Post by Quantum1982 »

Greetings

I am making a maze game using irrlicht. Later on 3d objects will be used. There seems to be a problem after I click the left mouse button. I don't if it's a logic problem or whether I am not using the events object properly. I am trying to make it so that when I click the left mouse button, a left wall is added to the cell that the mouse cursor is pointing to. I will add the logic later to add the other walls. When you click the cell, add its all the left walls for that entire row, but I just want it for that single cell. Don't know why it's doing this ?

Any help would be appreciated. Thanks

Here is the code

Code: Select all

 
#include <irrlicht.h>
#include "driverChoice.h"
#include "irrlist.h"
 
using namespace irr;
 
#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#endif
 
#define XCells 8;
 
 
 
class CMazeItem
{
public:
        bool bLeft, bRight, bUp, bDown;
};
 
class CMaze
{
public:
        CMazeItem TheMaze[8][8];
        void ClearMaze(void);
        void GenerateMaze(void);
        void UpdateAlien(void);
 
        int iHumanX,iHumanY;
        int iAlienX,iAlienY;
};
 
////////////////////////////////////////////// GLOBALS ///////////////////////////////////////////
// create the maze
CMaze Maze;
 
 
////////////////////////////////////////////// GLOBALS ///////////////////////////////////////////
 
// | , |, -. _. |_, |-, -|, _|, ||,-_, 
 
void CMaze::ClearMaze(void)
{
        for (int i=0;i<8;i++)
                for (int j=0;j<8;j++)
                {
                        TheMaze[i,j]->bLeft=false;
                        TheMaze[i,j]->bRight=false;
                        TheMaze[i,j]->bUp=false;
                        TheMaze[i,j]->bDown=false;
                } // for j
                iHumanX = 5; iHumanY=5;
                iAlienX = 0; iAlienY = 0;
}
void CMaze::GenerateMaze(void)
{
        int iNum;
        srand(8);
        for (int i=0;i<8;i++)
                for (int j=0;j<8;j++)
                {
                        iNum = rand() % 10;
                        if (iNum==0) 
                        {
                                TheMaze[i,j]->bLeft=true;
                                if (i>0) Maze.TheMaze[i-1,j]->bRight=true; 
                        }
                        else if (iNum==1) 
                        {
                                TheMaze[i,j]->bRight=true;
                                if (i<7) Maze.TheMaze[i+1,Maze.iHumanY]->bLeft=true; 
                        }
                        else if (iNum==2) 
                        {
                                TheMaze[i,j]->bUp=true;
                                if (j>0) Maze.TheMaze[i,j-1]->bDown=true; 
                        } // else if 
                        else if (iNum==3) 
                        {
                                TheMaze[i,j]->bDown=true;
                                if (j<7) Maze.TheMaze[i,j+1]->bUp=true;
                        }
                        else if (iNum==4) 
                        { 
                                TheMaze[i,j]->bLeft=true; 
                                if (i>0) Maze.TheMaze[i-1,j]->bRight=true; 
                                TheMaze[i,j]->bDown=true; 
                                if (j<7) Maze.TheMaze[i,j+1]->bUp=true;
                        }
                        else if (iNum==5) 
                        { 
                                TheMaze[i,j]->bLeft=true; 
                                if (i>0) Maze.TheMaze[i-1,j]->bRight=true; 
                                TheMaze[i,j]->bUp=true; 
                                if (j>0) Maze.TheMaze[i,j-1]->bDown=true; 
                        }
                        else if (iNum==6) 
                        { 
                                TheMaze[i,j]->bUp=true; 
                                if (j>0) Maze.TheMaze[i,j-1]->bDown=true; 
                                TheMaze[i,j]->bRight=true; 
                                if (i<7) Maze.TheMaze[i+1,Maze.iHumanY]->bLeft=true; 
                        }
                        else if (iNum==7) 
                        { 
                                TheMaze[i,j]->bDown=true; 
                                if (j<7) Maze.TheMaze[i,j+1]->bUp=true;
                                TheMaze[i,j]->bRight=true; 
                                if (i<7) Maze.TheMaze[i+1,Maze.iHumanY]->bLeft=true; 
                        }
                        else if (iNum==8) 
                        { 
                                TheMaze[i,j]->bLeft=true; 
                                if (i>0) Maze.TheMaze[i-1,j]->bRight=true; 
                                TheMaze[i,j]->bRight=true; 
                                if (i<7) Maze.TheMaze[i+1,Maze.iHumanY]->bLeft=true; 
 
                        }
                        else if (iNum==9) 
                        { 
                                TheMaze[i,j]->bUp=true; 
                                if (j>0) Maze.TheMaze[i,j-1]->bDown=true; 
                                TheMaze[i,j]->bDown=true; 
                                if (j<7) Maze.TheMaze[i,j+1]->bUp=true;
                        }
 
                } // for j
}
void CMaze::UpdateAlien(void)
{
        if ((iHumanX<iAlienX)&&(Maze.TheMaze[iAlienX,iAlienY]->bLeft==false))
                iAlienX--;
        else if ((iHumanX>iAlienX)&&(Maze.TheMaze[iAlienX,iAlienY]->bRight==false))
                iAlienX++;
 
        if ((iHumanY<iAlienY)&&(Maze.TheMaze[iAlienX,iAlienY]->bUp==false))
                iAlienY--;
        else if ((iHumanY>iAlienY)&&(Maze.TheMaze[iAlienX,iAlienY]->bDown==false))
                iAlienY++;
}
 
class MyEventReceiver : public IEventReceiver
{
public:
 
        MyEventReceiver(video::IVideoDriver* driver)
        {
        
        }
 
        bool OnEvent(const SEvent& event)
        {
                
                if (event.EventType == irr::EET_KEY_INPUT_EVENT) 
                        
                {
                        if ((event.KeyInput.Key == irr::KEY_LEFT)&&(Maze.iHumanX>0)&&(Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bLeft==false))
                        {
                                Maze.iHumanX--;
                                Maze.UpdateAlien();
                        }
                        else if ((event.KeyInput.Key == irr::KEY_RIGHT)&&(Maze.iHumanX<7)&&(Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bRight==false))
                        {
                                Maze.iHumanX++;
                                Maze.UpdateAlien();
                        }
 
                        else if ((event.KeyInput.Key == irr::KEY_UP)&&(Maze.iHumanY>0)&&(Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bUp==false))
                        {
                                Maze.iHumanY--;
                                Maze.UpdateAlien();
                        }
                        else if ((event.KeyInput.Key == irr::KEY_DOWN)&&(Maze.iHumanY<7)&&(Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bDown==false))
                        {
                                Maze.iHumanY++;
                                Maze.UpdateAlien();
                        }
                        else if (event.KeyInput.Key == irr::KEY_KEY_0) 
                        { 
                                Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bLeft=true; 
                                if (Maze.iHumanX>0) Maze.TheMaze[Maze.iHumanX-1,Maze.iHumanY]->bRight=true; 
                        }
                        else if (event.KeyInput.Key == irr::KEY_KEY_1) 
                        {
                                Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bRight=true;
                                if (Maze.iHumanX<7) Maze.TheMaze[Maze.iHumanX+1,Maze.iHumanY]->bLeft=true; 
                        } // else if 
                        else if (event.KeyInput.Key == irr::KEY_KEY_2) 
                        {
                                Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bUp=true;
                                if (Maze.iHumanY>0) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY-1]->bDown=true; 
                        } // else if 
                        else if (event.KeyInput.Key == irr::KEY_KEY_3) 
                        {
                                Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bDown=true;
                                if (Maze.iHumanY<7) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY+1]->bUp=true;
                        } // else if 
                        else if (event.KeyInput.Key == irr::KEY_KEY_4) 
                        { 
                                Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bLeft=true; 
                                if (Maze.iHumanX>0) Maze.TheMaze[Maze.iHumanX-1,Maze.iHumanY]->bRight=true; 
                                Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bDown=true; 
                                if (Maze.iHumanY<7) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY+1]->bUp=true;
                        }
                        else if (event.KeyInput.Key == irr::KEY_KEY_5) 
                        { 
                                Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bLeft=true; 
                                if (Maze.iHumanX>0) Maze.TheMaze[Maze.iHumanX-1,Maze.iHumanY]->bRight=true; 
                                Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bUp=true; 
                                if (Maze.iHumanY>0) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY-1]->bDown=true; 
                        }
                        else if (event.KeyInput.Key == irr::KEY_KEY_6) 
                        { 
                                Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bUp=true; 
                                if (Maze.iHumanY>0) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY-1]->bDown=true; 
                                Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bRight=true; 
                                if (Maze.iHumanX<7) Maze.TheMaze[Maze.iHumanX+1,Maze.iHumanY]->bLeft=true; 
                        }
                        else if (event.KeyInput.Key == irr::KEY_KEY_7) 
                        { 
                                Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bDown=true; 
                                if (Maze.iHumanY<7) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY+1]->bUp=true;
                                Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bRight=true; 
                                if (Maze.iHumanX<7) Maze.TheMaze[Maze.iHumanX+1,Maze.iHumanY]->bLeft=true; 
                        }
                        else if (event.KeyInput.Key == irr::KEY_KEY_8) 
                        { 
                                Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bLeft=true; 
                                if (Maze.iHumanX>0) Maze.TheMaze[Maze.iHumanX-1,Maze.iHumanY]->bRight=true;
                                Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bRight=true; 
                                if (Maze.iHumanX<7) Maze.TheMaze[Maze.iHumanX+1,Maze.iHumanY]->bLeft=true;
                        }
                        else if (event.KeyInput.Key == irr::KEY_KEY_9) 
                        { 
                                Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bUp=true; 
                                if (Maze.iHumanY>0) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY-1]->bDown=true; 
                                Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bDown=true; 
                                if (Maze.iHumanY<7) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY+1]->bUp=true;
                        }
                        else if (event.KeyInput.Key == irr::KEY_KEY_C) 
                                Maze.ClearMaze();
                        else if (event.KeyInput.Key == irr::KEY_KEY_G) 
                                Maze.GenerateMaze();
                        else
                                return false;
                }
                else if (event.EventType == irr::EET_MOUSE_INPUT_EVENT)
                {
                        if (event.MouseInput.isLeftPressed())
                        {
                                Maze.TheMaze[event.MouseInput.X/50,event.MouseInput.Y/50]->bLeft=true;
                                if (Maze.iHumanX>0) Maze.TheMaze[event.MouseInput.X/50,event.MouseInput.Y/50]->bRight=true;
                        } // if 
                } // else if 
 
                return false;
        }
};
/*
At first, we let the user select the driver type, then start up the engine, set
a caption, and get a pointer to the video driver.
*/
 
int main()
{
        //Maze.ClearMaze();
        //Maze.GenerateMaze();
        // ask user for driver
        video::E_DRIVER_TYPE driverType=driverChoiceConsole();
        if (driverType==video::EDT_COUNT)
                return 1;
 
        // create device
 
        IrrlichtDevice *device = createDevice(driverType,
                core::dimension2d<u32>(640, 480),16U,true);
 
        if (device == 0)
                return 1; // could not create selected driver.
 
        device->setWindowCaption(L"MazeGame");
 
        video::IVideoDriver* driver = device->getVideoDriver();
 
        gui::IGUIFont* font = device->getGUIEnvironment()->getBuiltInFont();
        gui::IGUIFont* font2 =
                device->getGUIEnvironment()->getFont("../../media/fonthaettenschweiler.bmp");
 
                //srand(device->getTimer()->getRealTime());
        int dir; // 0 =left 1 = right, 2 = up, 3 = down
        int iNewPosX, iNewPosY;
 
        //Maze.GenerateMaze();
        
        //irr::io::IFileSystem file;
        //file.createAndWriteFile("C:\Users\York\Desktop\Debug",true);
        //file += "5";
 
        Maze.iHumanX=5;
        Maze.iHumanY=5;
        Maze.iAlienX=5;
        Maze.iAlienY=0;
 
        irr::core::vector2d<irr::s32> start;
        irr::core::vector2d<irr::s32> end;
        
        MyEventReceiver receiver(driver);
        device->setEventReceiver(&receiver);
 
        while(device->run() && driver)
        {
                if (device->isWindowActive())
                {
                        u32 time = device->getTimer()->getTime();
                        
 
                        driver->beginScene(true, true, video::SColor(255,255,255,255));
 
                        // EET_KEY_INPUT_EVENT , KEY_LEFT , irr::IEventReceiver, bool irr::IEventReceiver::OnEvent      (       const SEvent &          event    )
 
 
                        for (int i=0;i<8;i++)
                                for (int j=0;j<8;j++)
                                {
                                        irr::core::stringw str;
                                        //str += Maze.iAlienX;
                                        //str += ",";
                                        //str += Maze.iAlienY;
 
                                        str = "HumanbLeft = ";
                                        str += (irr::core::stringw) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bLeft;
                                        str += ", HumanbRight = ";
                                        str += (irr::core::stringw) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bRight;
                                        str += "HumanbUp = ";
                                        str += (irr::core::stringw) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bUp;
                                        str += ", HumanbDown = ";
                                        str += (irr::core::stringw) Maze.TheMaze[Maze.iHumanX,Maze.iHumanY]->bDown;
 
                                        /*
                                        str += ",";
                                        str += (irr::core::stringw) Maze.TheMaze[i,j]->bUp;
                                        str += ",";
                                        str+= (irr::core::stringw) Maze.TheMaze[i,j]->bDown;
                                        */
                                        if (font)
                                                font->draw(str,irr::core::rect<s32>(400,20,600,40),irr::video::SColor(255,255,0,255));
 
                                        str = "AlienbLeft = ";
                                        str += (irr::core::stringw) Maze.TheMaze[Maze.iAlienX,Maze.iAlienY]->bLeft;
                                        str += ", AlienbRight = ";
                                        str += (irr::core::stringw) Maze.TheMaze[Maze.iAlienX,Maze.iAlienY]->bRight;
                                        str += "AlienbUp = ";
                                        str += (irr::core::stringw) Maze.TheMaze[Maze.iAlienX,Maze.iAlienY]->bUp;
                                        str += ", AlienbDown = ";
                                        //str += (irr::core::stringw) Maze.TheMaze[Maze.iAlienX,Maze.iAlienY]->bDown;
                                        str += (irr::core::stringw) (device->getTimer()->getRealTime()%10);
                                        //str += ", rand() % 10 = ";
                                        
                                        if (font)
                                                font->draw(str,irr::core::rect<s32>(300,50,600,70),irr::video::SColor(255,255,0,255));
                                        str = "";
                                        str += (irr::core::stringw) (device->getTimer()->getRealTime()%10);
                                        if (font)
                                                font->draw(str,irr::core::rect<s32>(300,50+(j*10),600,70+(j*10)),irr::video::SColor(255,255,0,255));
                                        if (Maze.TheMaze[i,j]->bLeft==true)
                                        {
                                                start.X=i*50;
                                                start.Y=j*50;
                                                end.X=i*50;
                                                end.Y=j*50+50;
                                                
                                                driver->draw2DLine(start,end,irr::video::SColor(255,255,0,255));
                                                /*
                                                str+= "  ,";
                                                str += start.X;
                                                str += ",";
                                                str += start.Y;
                                                */
                                                //if (font)
                                                ///font->draw(str,irr::core::rect<s32>(i*50,j*50,i*50+50,j*50+50),irr::video::SColor(255,255,0,255));
                                        } // if 
                                        if (Maze.TheMaze[i,j]->bRight==true)
                                        {
                                                start.X=i*50+50;
                                                start.Y=j*50;
                                                end.X=i*50+50;
                                                end.Y=j*50+50;
                                                
                                                driver->draw2DLine(start,end,irr::video::SColor(255,255,0,255));
                                        } // if 
                                        if (Maze.TheMaze[i,j]->bUp==true)
                                        {
                                                start.X=i*50;
                                                start.Y=j*50;
                                                end.X=i*50+50;
                                                end.Y=j*50;
                                                
                                                driver->draw2DLine(start,end,irr::video::SColor(255,255,0,255));
                                        } // if 
                                        if (Maze.TheMaze[i,j]->bDown==true)
                                        {
                                                start.X=i*50;
                                                start.Y=j*50+50;
                                                end.X=i*50+50;
                                                end.Y=j*50+50;
                                                
                                                driver->draw2DLine(start,end,irr::video::SColor(255,255,0,255));
                                        } // if 
                                } // for j
 
                                // draw human
                                driver->draw2DRectangle(irr::video::SColor(255,255,0,255),
                                        irr::core::rect<s32>(Maze.iHumanX*50+5,Maze.iHumanY*50+5,Maze.iHumanX*50+50-5,Maze.iHumanY*50+50-5));
 
                                // draw alien
                                driver->draw2DRectangle(irr::video::SColor(255,0,0,255),
                                        irr::core::rect<s32>(Maze.iAlienX*50+5,Maze.iAlienY*50+5,Maze.iAlienX*50+50-5,Maze.iAlienY*50+50-5));
 
                        driver->endScene();
                }
        }
 
        device->drop();
 
        return 0;
}
 
 
 
CuteAlien
Admin
Posts: 9929
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Mouse Input Logic

Post by CuteAlien »

Several problems. The first one is that you access array just wrong:

Code: Select all

 
// WRONG:
TheMaze[i,j]->bLeft=false;
// RIGHT:
TheMaze[i][j].bLeft=false;
 
Explaining why this still compiles is a little tricky - basically TheMaze[i,j]->bLeft is identical to TheMaze[j]->bLeft which is identical to &( TheMaze[j][0];
You're doing this bug throughout, so fix it everywhere.


Next thing which is maybe what you want or maybe not is this line:

Code: Select all

 
if (event.MouseInput.isLeftPressed())
 
This would check on _all_ mouse-events if the left button is pressed (so for example when you move the mouse as well). If you only want to catch pressing the mouse-button it's:

Code: Select all

 
if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN )
 
Last a hint - you already have #define XCells 8; ... so use the define (and one for y as well) instead of hardcoding 8 all over the place. Numbers directly in code are called magic numbers because no-one will know what they mean without understanding the code. Always use defines or constants instead. Also makes it easier adding changes.
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
Quantum1982
Posts: 45
Joined: Mon Apr 23, 2012 9:31 am

Re: Mouse Input Logic

Post by Quantum1982 »

Greetings

Thank you for taking the time to help me. Much appreciated. Ya the reason I used TheMaze[i,j]->bLeft=false; is because TheMaze[i,j].bLeft=false wasn't working, but I will try again.

Also, I actually did plan on using those defines, but for some reason I get compile errors every time I substitute them inside an array.

I will make the necessary changes and post again. Thanks again for the help.

Cheers
CuteAlien
Admin
Posts: 9929
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Mouse Input Logic

Post by CuteAlien »

Quantum1982 wrote:Greetings
Thank you for taking the time to help me. Much appreciated. Ya the reason I used TheMaze[i,j]->bLeft=false; is because TheMaze[i,j].bLeft=false wasn't working, but I will try again.
That is because TheMaze[i,j].bLeft=false would also be wrong. Please look carefully at the correct version of accesssing a multi-dimensional array - it's not using any comma! (the comma operator does a completely different thing - it evalates i then throws it away and returns j - nothing you'd use in this case).
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
Post Reply