Do you have a state system built into your game? How do you separate the logic from your main menu and the actual gameplay and the options screen and etc...? (Look into state machines, but specifically for games). Lazy Foo has a state machine article, using SDL.
Now, state machines can be more than just things that take over the entire logic. You can have tiny state machines inside other state machines. For example, you could, while in the regular 'playing the game' state have an small state that shows the damage indicator, and when you get hurt, you can set that state to 'show damage', and it can, as time passes and you call Update(amountOfTimePast) on it every frame, automatically change itself from 'show damage' to 'damage fading' to 'damage gone'.
Or, in your main gameplay state, just have an integer value for the damage indicator. When it's 0, don't draw anything. When the player gets hurt, set it to 255. Every 30 millaseconds in the Update(amountOfTimePast) function of the main gameplay state, subtract 1 from that integer. Use that as the alpha for the effect drawn onscreen.
Actually, rather than having integers from 0 to 255, it's better to have floats from 0.0 to 1.0, then you can multiply a 'DurationOfEffectInSeconds' constant float againsts the 'amountOfTimePast' float and subtract that from the 'currentEffectAlpha'.
void GameState::onUpdate(float amountOfTimePastInSeconds)
currentEffectAlpha -= ((1.0 / EffectDurationInSeconds) * amountOfTimePastInSeconds);
Basically, however you implement it, you need these things:
- A way of currently storing the state of the effect.
- A way of checking the amount of time past from frame to frame.
- A way of updating the state of the effect every frame, based on the amount of time past.
- A way to draw the effect every frame, if it's in the right state.
- A way of initially triggering the state.
The trick is, you don't need to "create" any C++ objects for the effect when the player gets damaged - you can have it always created, just in an 'invisible' state. When the player gets damaged, you change it's state to something that makes it visible.
My code has a generic 'game state' engine. Each game state, in my specific code, can have children gamestates.
Each gamestate has a virtual Update(time past), Draw(destination), React(game event), among other things.
Other peoples gamestates are implemented differently.