# Creating a Flashing Effect

## Recommended Posts

c_fried_rice    163

Hey guys,

I'm trying to create a flashing effect for my game - basically when the player is in danger of some sorts, the screen begins to flash red for a brief amount of time. However, I am not sure how to do this in SDL. I want this to be done via function call that can have an SDL_Color as a parameter that determines the color of the flash.

I honestly have no idea how to go about doing this. The only idea I had come up with was to clear the screen, set the renderer's draw color to the provided color, draw a rectangle, then clear the screen again. But I, despite not believing it would work, tried it and surprise! it didn't.

Any tips on how to go about doing this would be much appreciated.

Thanks!

##### Share on other sites

How about drawing SDL_FillRect(screen, rect, color) over the entire screen for a frame or two?

SDL_FillRect takes a Uint32 as a color, which you have to get through SDL_MapRGBA, meaning it looks like this:

void DrawRectangle(SDL_Surface *destination, const SDL_Color &color, SDL_Rect *rect = NULL /* If NULL, fills the entire surface */)
{
Uint32 mappedColor = SDL_MapRGB(screen->format, color.r, color.g, color.b);
SDL_FillRect(screen, rect, mappedColor);
}


(This is the older version of SDL, my SDL knowledge is outdated by several years, so you might need to tweak it)

##### Share on other sites
c_fried_rice    163

My problem really lies with the whole displaying for a brief period of time part of it. I have an image I can blit to the screen, and I've also considered just using the RenderFillRect() function.

Basically, how do I just have the image briefly flash on the screen then disappear? I'm for some reason drawing a total blank on how to do this, but it can't be a difficult task can it? >.<

Help would be appreciated because I just can't think straight right now.

Edited by c_fried_rice

##### Share on other sites

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.

##### Share on other sites
c_fried_rice    163

Yeah, I'm using a gamestate manager by using a basic FSM. Each state gets its own update() function, etc.

But my problem now seems to be adjusting the alpha of the image. I tried using the function listed here, but I never see the image on the screen, despite my debugger hitting the line where the draw should happen. =S

Can anyone explain what kind of value I should be giving the function I linked to? I've been doing the suggestion you had Servant, and then multiplying 255 by that but it doesn't seem to work, even when the value of the alpha variable is .9 for example (255 * .9 therefore should give an alpha value of ~229).

##### Share on other sites

Can anyone explain what kind of value I should be giving the function I linked to? I've been doing the suggestion you had Servant, and then multiplying 255 by that but it doesn't seem to work, even when the value of the alpha variable is .9 for example (255 * .9 therefore should give an alpha value of ~229).

Ah, that's the time to debug.  First, check the result of the multiplication before passing it to the function by using an intermediate variable and a debugger (or std::cout if you've never used a debugger before - but now would be a good time to learn!), to make sure it actually does result in 229.

Second, test the return value of SDL_SetTextureAlphaMod() and see if it returns -1. If it does, call SDL_GetError() and print out the error message, so you can see what it is reporting.

Third, test SDL_SetTextureAlphaMod() with a literal value, like 128, and see if it has any effect.

I've never used that function before - I use a different API, and my previous SDL experience was SDL 1.2 - but debugging is a normal part of programming regardless of API.

##### Share on other sites
c_fried_rice    163

Ah, yes silly me. It turned out the path name for the image was being read in as "" as opposed to a string that wasn't empty. 'Twas a silly mistake in the Lua file where I read in the path for the image.

Thanks a ton though!