[ XF9 ]

Basic Events and Handler in C++

Posted on in C++.

Using events and handler in C++ is pretty simple and straight forward. This is a brief walkthrough on how to use them.

The Event

Lets assume you are building a basic button class with SFML.
Please Note: we are not going into detail on how to build your button, just enough to make the clicking work. This is the C++ version – if you are looking for the C# one, you can find it here.

Okay, let’s go. Your button header file could look something like this:

class Button
{
	public:
		Button(std::string);
		~Button();

		void update(sf::Window* window);

		__event void Click();

	private:
		sf::Text* text;
};

We have our basic constructor and destructor, an update function which will determine when to raise the Click event, the said Click event and a SFML text so we have something to click on. The update function will get a reference to the current window so we can get the current mouse position in relation to the window. Now let’s have a look at this function:

void Button::update(sf::Window* window)
{
	sf::Vector2i mouseposition = sf::Mouse::getPosition(*window);
	sf::FloatRect boundingbox = text->getGlobalBounds();

	if (boundingbox.contains(mouseposition))
	{
		text->setColor(sf::Color::Red);

		if (sf::Mouse::Button::Left)
			__raise this->Click();
	}
	else
		text->setColor(sf::Color::White);
}

First thing is to get the current mouse position as mouseposition and the text proportions as boundingbox. Now we will do a collision test, checking if the mouse position is inside the bounding box and, if so, we will change the text color to red to indicate the hover state – otherwise the text will be white. If the the mouse is pointing at the text and the left mouse button is down, we will raise the event and therefore notify all hooked handler.

Please Note: this will raise the event every time you call the update function and the left mouse button is down. If you call the update function with every draw cycle clicking on the text will actually raise the Click event many times – every frame till the mouse button is released! If you don’t want this behavior you need to wait till the mouse button is released and raise the event at that point. For example you could define a boolean value indicating if the mouse button was down last frame and check with something like that: if(downLastFrame && !downCurrentFrame) { __raise this->Click(); }

The Handler

Now we have an event that can be raised – what to do now? Next step is to react to the event. Lets assume you have a game class, containing the button:
The header file contains the usual constructor/destructor and a function to handle the click event.

class Game{
public:
	Game();
	void onButtonClick();
};

onBUttonClick is the function to be called once the event has been raised. You’ll need to define your response to the event there. But how do we link the event and the function? Let’s have a look at the constructor:

	Game::Game()
	{
		Button* button = new Button("Start");
		__hook(&Button::Click, button, &Game::onButtonClick);
	}
};

First off we create a new Button instance and then we hook the Click event from the button to the onButtonClick from this class so it will be called once the event raises. Syntax for hook is as follows:

  1. the event to hook to
  2. the object which will raise the event
  3. the function to call once the event raises
  4. [the class to respond to the raised event]

For more in depth explanation on the __event, __raise and __hook have a look at the documentation which can be found here: MSDN.

The Summary

To make it short: define an __event, __raise it at an appropriate point of time and __hook it to a function in which you define what to do.


Write a reply or comment

Your email address will not be published.