0
\$\begingroup\$

I'm working on a game right now using my own engine. In the engine, I have a State Machine which handles input for, updates, and draws the current State - the one at the back of the vector. The game engine itself stores Sprites in two maps and a vector, and the States also store pointers to those Sprites. With this many references, it is becoming difficult for me to manage all my Sprites and their scope. Furthermore, I have a Button class which derives from Sprite and adds some mouse input behavior.

I'm trying to figure out a storage solution for my Sprites that would allow me to update all Sprite animations, handle Button input, identify specific Sprites (likely by a string name) primarily for cutscenes, access Sprites directly to begin animations or instantly modify their position, color, or texture coordinates, and batch Sprites of the same batch type (Menu, Background, Player, etc.) together for drawing.

My current solution stores a map of BatchType (an enumerator) to a vector of shared_ptr's pointing to Sprites, solving the batching problem; then a map of strings to Sprites for naming; next, a vector of smart pointers to Buttons for their specific handleInput function; and lastly, the engine's createSprite method returns a shared_ptr to the new Sprite so it can be stored in the State that created it for direct access. In order for any Sprite to be deleted, I have four potential references to delete, and it feels convoluted.

Are there any better storage methods which will still achieve all this? I think the main problem here is that I need to separate Sprites from Buttons in order to call the Button handleInput function.

\$\endgroup\$
0
\$\begingroup\$

I've gone with handling batching in the game engine and leaving the rest to States which get a pointer to the batched Sprites. With the new structure for my state machine this will probably simplify down to handling Sprites entirely through the State.

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.