Mouse Events???

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
lucifer1101
Posts: 42
Joined: Sun Nov 16, 2008 11:23 pm
Location: Melbourne, Australia
Contact:

Mouse Events???

Post by lucifer1101 »

I'm having a bit of trouble with mouse events, i did a search and got this http://irrlicht.sourceforge.net/phpBB2/ ... lick+event but im still confused with what i have to do to get the reciever working.

all i want to be able to do is draw a sprite at x, y when left is pressed..

All help is appreciated.[/code]
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

You need to write a class that is derived from IEventReceiver and implements OnEvent(). In the OnEvent() function, you look for left mouse button presses. When the left mouse button is pressed down you create a sprite and place it as needed.

Sometime after the IrrlichtDevice has been created, you create an instance of your event receiver, and pass it to the device with a call to setEventReceiver().

Travis
lucifer1101
Posts: 42
Joined: Sun Nov 16, 2008 11:23 pm
Location: Melbourne, Australia
Contact:

Post by lucifer1101 »

i had tried that by using this code as the event reciever

Code: Select all

class MyEventReceiver1 : public IEventReceiver
{
public:
virtual bool OnEvent(const SEvent& event)
{
if (event.EventType == irr::EET_KEY_INPUT_EVENT)
KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;

else if (event.EventType == EET_MOUSE_INPUT_EVENT)
KeyIsDown[event.MouseInput.Event] = event.MouseInput.Event;

return false;
}

virtual bool IsKeyDown(EKEY_CODE keyCode) const
{
return KeyIsDown[keyCode];
}

MyEventReceiver1()
{
for (int i=0; i<KEY_KEY_CODES_COUNT; ++i)
KeyIsDown[i] = false;
}

private:
bool KeyIsDown[KEY_KEY_CODES_COUNT];
}; 
but it kept getting errors, and i did try to fix it for mouse events with what was provided in the topic but still didnt work...
twilight17
Posts: 362
Joined: Sun Dec 16, 2007 9:25 pm

Post by twilight17 »

Post this userbar I made on other websites to show your support for Irrlicht!
Image
http://img147.imageshack.us/img147/1261 ... wernq4.png
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

First, if you had attempted to fix the code according to what I posted in the other thread, then I'd expect you'd have something differrent than the original poster. Second, why would you copy code that you know doesn't work?

Regardless, if you're just trying to find out when the left mouse button is pressed, why do you need the key state array? Why not just simplify your code down to something that you can manage, and then make it more complicated as you learn?

How about you start with this...

Code: Select all

#include <stdio.h>

class MyEventReceiver1 : public IEventReceiver 
{ 
public:
  MyEventReceiver1() 
  { 
  }

  virtual bool OnEvent(const SEvent& event) 
  { 
    printf ("hello world\n");
    return false; 
  } 
};
Try that on for size. See if you can get it to compile and if it will receive an event. Once that is working, show us the code you think you'd have to write to detect a left mouse button press. Then, when you've successfully detected a left button press, you'll need to write some code to generate your 'sprite', or maybe just move it.

Hint: Everything you need to know is in IEventReceiver.h and on these forums.
Hint: Search the forums for IEventReceiver and EMIE_LMOUSE_PRESSED_DOWN.

Travis
lucifer1101
Posts: 42
Joined: Sun Nov 16, 2008 11:23 pm
Location: Melbourne, Australia
Contact:

Post by lucifer1101 »

@twilight17

thanks for that, i may try to use that later on.

@vitek

i will do that and try to learn as much as possible. but i was after an event reciever that had events for all key and mouse functions.
lucifer1101
Posts: 42
Joined: Sun Nov 16, 2008 11:23 pm
Location: Melbourne, Australia
Contact:

Post by lucifer1101 »

vitek i looked at that little snippet and i have tried to compile it, but dont know what to do with it and i get errors.

Code: Select all

1>c:\users\jason\desktop\irrdemos\eventtest\eventtest\main.cpp(4) : error C2504: 'IEventReceiver' : base class undefined
1>c:\users\jason\desktop\irrdemos\eventtest\eventtest\main.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jason\desktop\irrdemos\eventtest\eventtest\main.cpp(10) : error C2143: syntax error : missing ',' before '&'
1>Build log was saved at "file://c:\Users\Jason\Desktop\IrrDemos\eventtest\eventtest\Debug\BuildLog.htm"
1>eventtest - 3 error(s), 0 warning(s)
i dont know what im supposed to do with it
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Everything in Irrlicht, including the class IEventReceiver, are in the namespace irr. You either need to explicitly qualify it (i.e., you write irr::IEventReceiver), or you need to bring the namespace in with using namespace irr;.

I'm making the assumption that you are using one of the example programs as a starting point. If you were, the code would have compiled without error.

Travis
lucifer1101
Posts: 42
Joined: Sun Nov 16, 2008 11:23 pm
Location: Melbourne, Australia
Contact:

Post by lucifer1101 »

i got that code you gave me to compile without any errors, but i dont know what direction this is taking me..

Code: Select all

#include <Irrlicht.h>
#include <stdio.h>

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

using namespace irr;

class MyEventReceiver1 : public IEventReceiver
{
public:
  MyEventReceiver1()
  {
  }

  virtual bool OnEvent(const SEvent& event)
  {
    printf ("hello world\n");
    return false;
  }
}; 

int main()
{
	printf ("hi\n");

	system("pause");
	return 0;
}
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

lucifer1101 wrote:i got that code you gave me to compile without any errors, but i dont know what direction this is taking me..
all Irrlicht events are send to the onEvent() function first...
there you can check the event and act on it...
if the event is finished, then return true,
and if Irrlicht shall also handle the event return false

Tutorials #04-Movement and #05-UserInterface are good references... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

lucifer1101 wrote:i got that code you gave me to compile without any errors, but i dont know what direction this is taking me..
It is taking you toward a solution. One that you implement on your own instead of having handed to you. You're doing good so far. Just be patient. This may take some time, but by the time you're done, you'll hopefully understand how to learn to use things on your own. This will allow you to work more independently.

So the code that you've posted compiles. Awesome. So lets add more code to make it actually do something. The first thing you need to do in an Irrlicht app is to create an IrrlichtDevice. You need to do this. Then you need to create an instance of your event receiver, and register it with the device.

All of the above is very easy. It is covered in several of the example programs. Again, if you search for IEventReceiver and EMIE_LMOUSE_PRESSED_DOWN, you might find some relevant topics.

Travis
Post Reply