• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
c_fried_rice

Creating a Flashing Effect

7 posts in this topic

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!

 

 

0

Share this post


Link to post
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)

1

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
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.

2

Share this post


Link to post
Share on other sites

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).

0

Share this post


Link to post
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. dry.png 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. smile.png

2

Share this post


Link to post
Share on other sites

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! 

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0