Incorrect Mouse Coordenates from SEvent? (fixed)

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
Mastiff
Posts: 17
Joined: Thu Apr 19, 2007 9:55 pm

Incorrect Mouse Coordenates from SEvent? (fixed)

Post by Mastiff »

I was working on my keyboard and mouse input class and was going to implement the Mouse coordinates, my problem is that the first four pixels of the X axis of the window are not handled properly with the coordinates I get from the Event structure.

The problem is the first pixel is 0, the next is 1, the next is 1, the next is 0, then the next is 4, after that everything is correct, though I can go 1 pixel farther than the width of the window.

I can get the correct X coordinates from device->getCursorControl()->getPosition().X

I was wondering if this might be a bug or I'm missing something?

Here is the test application I was using that compares 3 of irrlicht's methods of gaining the mouse coordinates (it's a modified movement example, for simplicity):

Code: Select all

#include <iostream>
#include <wchar.h>
#include "./irrlicht/irrlicht.h"
#include "../projects/MastEventReceiver.cpp"

using namespace std;
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

#pragma comment(lib, "Irrlicht.lib")

ISceneNode* node = 0;
IrrlichtDevice* device = 0;
ICameraSceneNode *camera = 0;

///////////////
//Start Main
///////////////
int main()
{
	MastEventReceiver receiver;
	receiver.init();

	device = createDevice(EDT_OPENGL, dimension2d<s32>(640,480), 32, false, false, true, &receiver);

	IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();
	IGUIEnvironment* gui = device->getGUIEnvironment();

	//Stationary Box Model
	node = smgr->addCubeSceneNode();
	node->setPosition(vector3df(0,0,30));
	node->setMaterialTexture(0, driver->getTexture("C:/Irrlicht/media/wall.bmp"));

	//Create Camera
	ICameraSceneNode *camera = smgr->addCameraSceneNodeMaya();

	int lastFPS = -1;

	IGUIStaticText *text = gui->addStaticText(L"", rect<int>(10,10,350,40), true);
	IGUIStaticText *texttwo = gui->addStaticText(L"", rect<int>(10,50,350,80), true);
	IGUIStaticText *textthree = gui->addStaticText(L"", rect<int>(10,90,350,120), true);

	while (device->run())
	{
		receiver.endEventProcess();


		wchar_t tmp[1024];
		swprintf(tmp, 1024, L"SEvent \nMouseX:(%d)\n(MouseY:%d)", receiver.mouseX(), receiver.mouseY());
		text->setText(tmp);

		swprintf(tmp, 1024, L"getCursorControl()->getPosition() \nMouseX:(%d)\n(MouseY:%d)", device->getCursorControl()->getPosition().X, device->getCursorControl()->getPosition().Y);
		texttwo->setText(tmp);

		swprintf(tmp, 1024, L"getCursorControl()->getRelativePosition() \nMouseX:(%f)\n(MouseY:%f)", device->getCursorControl()->getRelativePosition().X, device->getCursorControl()->getRelativePosition().Y);
		textthree->setText(tmp);

		// Draw everything.
		driver->beginScene(true, true, SColor(255,90,90,156));
		smgr->drawAll();
		gui->drawAll();
		driver->endScene();


		receiver.startEventProcess();
	}
	//END OF  while (device->run())


	device->drop();

 	cout << "Program has Ended, Press Enter to Continue.\n";
	cin.get();

	return 0;
}

And the version of MastEventReceiver.cpp

Code: Select all


// Change this to the path where your Irrlicht Header Files are.
#include "./irrlicht/irrlicht.h"

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

/// ==============================
/// MastEventReceiver
/// ==============================
class MastEventReceiver : public IEventReceiver
{

	protected:
	// Enumeration for UP, DOWN, PRESSED and RELEASED key states. Also used for mouse button states.
	enum keyStatesENUM {UP, DOWN, PRESSED, RELEASED};

	// Enumeration for Event Handling State.
	enum processStateENUM {STARTED, ENDED};

	// Mouse button states.
	keyStatesENUM mouseButtonState[2]; //Left(0), Middle(1) and Right(2) Buttons.

	// Keyboard key states.
	keyStatesENUM keyState[KEY_KEY_CODES_COUNT];

	// Mouse X/Y coordinates.
	struct mouse
	{
	int X;
	int Y;
	};
	struct mouse mousePos;

	processStateENUM processState; // STARTED = handling events, ENDED = not handling events

	virtual bool OnEvent(SEvent event)
	{
		bool eventprocessed = false;

			//////////////////////////////
			// Keyboard Input Event
			//////////////////////////////
			if (event.EventType == EET_KEY_INPUT_EVENT)
			{
				if (processState == STARTED)
				{
					// if key is Pressed Down
					if (event.KeyInput.PressedDown == true)
					{
						// If key was not down before
						if (keyState[event.KeyInput.Key] == UP || keyState[event.KeyInput.Key] == RELEASED)
						{
							keyState[event.KeyInput.Key] = PRESSED; // Set to Pressed
						}
						else
						{
							// if key was down before
							keyState[event.KeyInput.Key] = DOWN; // Set to Down
						}
					}
					else
					{

							// if the key is down
							if (keyState[event.KeyInput.Key] != UP)
							{
								keyState[event.KeyInput.Key] = RELEASED; // Set to Released
							}
					}
				}


			eventprocessed = true;
		}

		//////////////////////////////
		// Mouse Input Event
		//////////////////////////////

		if (event.EventType == EET_MOUSE_INPUT_EVENT)
		{
			//Mouse changed position
			if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
			{
				mousePos.Y = event.MouseInput.Y;
				mousePos.X = event.MouseInput.X;
			}


			//Left Mouse Button Pressed
			if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
			{
				//
				if (mouseButtonState[0] == UP || mouseButtonState[0] == RELEASED)
				{
				mouseButtonState[0] = PRESSED;
				}
				else
				{
				mouseButtonState[0] = DOWN;
				}
			}

			//Left Mouse Button Rleased
			if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
			{
				//
				if (mouseButtonState[0] != UP)
				{
				mouseButtonState[0] = RELEASED;
				}
			}

			//Middle Mouse Button Pressed
			if (event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
			{
				//
				if (mouseButtonState[1] == UP || mouseButtonState[1] == RELEASED)
				{
				mouseButtonState[1] = PRESSED;
				}
				else
				{
				mouseButtonState[1] = DOWN;
				}
			}

			//Middle Mouse Button Rleased
			if (event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
			{
				//
				if (mouseButtonState[1] != UP)
				{
				mouseButtonState[1] = RELEASED;
				}
			}

			//Right Mouse Button Pressed
			if (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
			{
				//
				if (mouseButtonState[2] == UP || mouseButtonState[2] == RELEASED)
				{
				mouseButtonState[2] = PRESSED;
				}
				else
				{
				mouseButtonState[2] = DOWN;
				}
			}

			//Right Mouse Button Rleased
			if (event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
			{
				//
				if (mouseButtonState[2] != UP)
				{
				mouseButtonState[2] = RELEASED;
				}
			}



			eventprocessed = true;
		}




		return eventprocessed;
	}


	//////////////////////
	// Public functions
	//////////////////////
	public:


	int mouseX()
	{
		return mousePos.X;
	}

	int mouseY()
	{
		return mousePos.Y;
	}

	bool leftMouseReleased()
	{
		if (mouseButtonState[0] == RELEASED)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	bool leftMouseUp()
	{
		if (mouseButtonState[0] == RELEASED || mouseButtonState[0] == UP)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool leftMousePressed()
	{
		if (mouseButtonState[0] == PRESSED)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool leftMouseDown()
	{
		if (mouseButtonState[0] == PRESSED || mouseButtonState[0] == DOWN)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool middleMouseReleased()
	{
		if (mouseButtonState[1] == RELEASED)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	bool middleMouseUp()
	{
		if (mouseButtonState[1] == RELEASED || mouseButtonState[1] == UP)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool middleMousePressed()
	{
		if (mouseButtonState[1] == PRESSED)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool middleMouseDown()
	{
		if (mouseButtonState[1] == PRESSED || mouseButtonState[1] == DOWN)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool rightMouseReleased()
	{
		if (mouseButtonState[2] == RELEASED)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	bool rightMouseUp()
	{
		if (mouseButtonState[2] == RELEASED || mouseButtonState[2] == UP)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool rightMousePressed()
	{
		if (mouseButtonState[2] == PRESSED)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool rightMouseDown()
	{
		if (mouseButtonState[2] == PRESSED || mouseButtonState[2] == DOWN)
		{
			return true;
		}
		else
		{
			return false;
		}
	}//

	bool keyPressed(char keycode)
	{
		if (keyState[keycode] == PRESSED)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool keyDown(char keycode)
	{
		if (keyState[keycode] == DOWN || keyState[keycode] == PRESSED)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool keyUp(char keycode)
	{
		if (keyState[keycode] == UP || keyState[keycode] == RELEASED)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool keyReleased(char keycode)
	{
		if (keyState[keycode] == RELEASED)
		{
			return true;
		}
		else
		{
			return false;
		}
	}


	// This is used so that the Key States will not be changed during execution of your Main game loop.
	// Place this at the very START of your Main Loop
	void endEventProcess()
	{
		processState = ENDED;
	}

	// This is used so that the Key States will not be changed during execution of your Main game loop.
	// Place this function at the END of your Main Loop.
	void startEventProcess()
	{

		processState = STARTED;
		//Keyboard Key States
		for (int i = 0; i <= KEY_KEY_CODES_COUNT; i++)
		{
			if (keyState[i] == RELEASED)
			{
				keyState[i] = UP;
			}

			if (keyState[i] == PRESSED)
			{
				keyState[i] = DOWN;
			}
		}
		//Mouse Button States
		for (int i = 0; i <= 2; i++)
		{
			if (mouseButtonState[i] == RELEASED)
			{
				mouseButtonState[i] = UP;
			}

			if (mouseButtonState[i] == PRESSED)
			{
				mouseButtonState[i] = DOWN;
			}
		}

	}

	void init()
	{
		//KeyBoard States.
		for (int i = 0; i <= KEY_KEY_CODES_COUNT; i++)
		{
			keyState[i] = UP;
		}
		//Mouse states
		for (int i = 0; i <= 2; i++)
		{
			mouseButtonState[i] = UP;
		}
		//Mouse X/Y coordenates.
		mousePos.X = 0;
		mousePos.Y = 0;
	}


};
/// ==========================================
/// END OF MastEventReceiver
/// ==========================================
Last edited by Mastiff on Sat Jun 02, 2007 9:13 pm, edited 1 time in total.
#include <"signature.h">

OS = Windows XP
IDE = Code::Blocks
Compiler = GCC MingW
Mastiff
Posts: 17
Joined: Thu Apr 19, 2007 9:55 pm

Post by Mastiff »

I narrowed it down to my startEventProcess() function and furthermore I found it was exactly this line "for (int i = 0; i <= KEY_KEY_CODES_COUNT; i++)" the problem was the "<=", I now changed it to "<" and everything works :?
#include <"signature.h">

OS = Windows XP
IDE = Code::Blocks
Compiler = GCC MingW
Post Reply