I made a few simple classes I'm proud of even they don't do anything remotely useful at the time. For now I have created 3 classes:
- Clickable: detects if mouse is hovering over the object or is clicked on with a certain mouse button.
- Interactable: detects if object is hovering over the object or a certain button is pressed while hovering.
- TileMap: Creates a map of tiles given by the user (more explanation below).
The classes Clickable and Interactable are meant to be parents of other objects to make them clickable or interactable, whereas TileMap should be fully functional on its own.
Here are all files (.h and .cpp in a .zip). If you just want to see what the functions do, click
here (executable in .zip).
The program will draw a map on the screen, and will create an Interactable and a Clickable on the same place as the map. Moving your mouse or the red box over the map will trigger an output on the output screen. Move the red box with the arrow keys. More outputs are triggered when hovering and pressing the right button: for the mouse it's the left click, for the box it's space or enter.
ClickableKey functions:
bool IsHovering(); // return true when mouse on box
bool StartHovering(); // return true when mouse enters box
bool StopHovering(); // return true when mouse leaves box
bool IsClicked(sf::Mouse::Button); // return true when clicked on box
Not much to say about it, it's pretty obvious what it does and how it does it. IsHovering() is called for every other function mentioned above. None of these functions handle events, this is why the function IsClicked() is best called after testing if event == sf::Event::MouseButtonPressed.
InteractableKey functions:
bool IsHovering();
bool StartHovering();
bool StopHovering();
bool InteractButton(sf::Keyboard::Key);
So basically the same as Clickable, but only accepts keyboard input. The biggest difference is that Interactable works with an interaction box or point. This means for example that you can create an invisible box ahead of your character. When that box hovers over an Interactable, IsHovering() will return true. The argument of InteractButton() should always be sf::Event.key.code. InteractButton() does not handle events.
TileMapKey functions:
void Draw(); // draws map on app
This class uses a texture where all tiles are located in. It needs to know the size of the tiles and the width and height of the map in tiles. When it is given an int m[width*height], where m[i+j*height] (with i and j from a for-loop) has the value for which tile it should display at that position, it will draw the map on the given sf::RenderWindow.
If you wish to give me some advice on the functions, I know it's a little bit too much at once to handle. Just pick one random and give all you've got on that one. You should only need to look at the key functions (under // <class> functions in every file) and the variables under protected: or private:.