Answer the question
In order to leave comments, you need to log in
How to correctly handle clicks in a self-written game engine?
In general, it was necessary to pervert and write an engine for a 2D toy in C ++. In the engine, you need to handle mouse clicks by determining which widget hitbox hit the click (a widget is anything that can be drawn: a button, a unit on the screen, etc.), and in some way calling the handler. Now the trick is to figure out how and in what form it is more correct to add handlers so that it looks glamorous and does not lead to unnecessary writing or refactoring when I wrote a lot of code and saw that everything was garbage and I needed it all over again.
There are two options running through my head right now:
Answer the question
In order to leave comments, you need to log in
When you click on the game screen, it searches for all elements that can receive clicks. If the click hit something, then all child elements that can receive clicks are searched. And so on, until the click hits a specific button. This is the general scheme. How you do it is a matter of taste. What is important here is not so much OOP, that is, the inheritance of classes, as the implementation of the hierarchy of elements of the interface itself (GUI or HUD), that is, the inheritance of objects. The objects themselves need references to child and parent elements, much like in the browser DOM.
Further nuances. For example, an element can have its own handler and child elements at the same time. You can make it so that if there is an own handler, then the child ones are ignored. Or you can make it so that if the click did not hit the children, then its own handler is called. Everything has pros and cons. Again, do as you please.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question