Jump to content
  • Advertisement
Sign in to follow this  
ochi12

How to implemnt lighting in 2d sdl game?

This topic is 1324 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm workin on a simple 2d game with sdl. I'm currently trying to add some lighting but I have no idea on how to do it. I don't want to use opengl because i think it's very complex and hard to learn(althought i know that the most convenient way to add lighting is with shaders) so I'm doing the lights with alpha bending.

I'm trying to archieve something like this:

4vBeR.png

I drawed a black square on top of the screen with alpha blending so the game looked like it was dark, but now I have no idea on how to make things visivle and add those "lights".

 

Any ideas?

thanks in advance

Share this post


Link to post
Share on other sites
Advertisement

Do you need real lights?

One of the 'easiest' tricks is to bind a transparent png or image file to the camera that has this shape. Unless you plan on having other light sources, it works great and requires no pre-computations.

Share this post


Link to post
Share on other sites

Do you need real lights?

One of the 'easiest' tricks is to bind a transparent png or image file to the camera that has this shape. Unless you plan on having other light sources, it works great and requires no pre-computations.

I do not think this answers his question since there is no concept of a camera in SDL.

I don't have an answer to your question Twk Amz, but I am too interested in how to achive transparency with some kind of a mask in SDL.

Share this post


Link to post
Share on other sites


I do not think this answers his question since there is no concept of a camera in SDL.

Assuming that you are using SDL_BlitSurface, then have a look at the SDL_SetAlpha function especially with its flag parameter set to SDL_SRCALPHA. This allows for alpha blending. Then use an image with an alpha channel, totally black but with full alpha in a central circle, falling off to 30% or so towards the borders. Set the surface to alpha blending, and blit it so that the destination rectangle matches the current view area. That is already what Orymus3 suggested, but expressed using SDL terms.

 


I drawed a black square on top of the screen with alpha blending so the game looked like it was dark, but now I have no idea on how to make things visivle and add those "lights".

Notice that the above trick does not lighting but darkening! The state of the destination surface before is a fully lit scene, and then only those parts of the scene that should not be lit are darkened.

Share this post


Link to post
Share on other sites

 


I do not think this answers his question since there is no concept of a camera in SDL.

Assuming that you are using SDL_BlitSurface, then have a look at the SDL_SetAlpha function especially with its flag parameter set to SDL_SRCALPHA. This allows for alpha blending. Then use an image with an alpha channel, totally black but with full alpha in a central circle, falling off to 30% or so towards the borders. Set the surface to alpha blending, and blit it so that the destination rectangle matches the current view area. That is already what Orymus3 suggested, but expressed using SDL terms.

 

 


I drawed a black square on top of the screen with alpha blending so the game looked like it was dark, but now I have no idea on how to make things visivle and add those "lights".

Notice that the above trick does not lighting but darkening! The state of the destination surface before is a fully lit scene, and then only those parts of the scene that should not be lit are darkened.

 

 

Personally, I am using SDL2 with SDL_Texture's and SDL_RenderCopy. However I think I would be able to use SDL_SetTextureAlphaMod to achive the same result.

 

I think my question was a bit missguided, because my use-case is different.

I want to blend two textures (or surfaces) given a transparency mask where white is 100% image a and black is 100% image b and all shades of gray are a mix of a and b.

My idea was to render image a, then render image b over it with per-pixel alpha applied to it by the mask.

I guess I could use your suggested technique for this.

 

I don't want to hijack this thread any more though, so I'll just thank you for your feedback, and if I need any additional help, I'll start a new thread =)

Share this post


Link to post
Share on other sites

Notice that the above trick does not lighting but darkening! The state of the destination surface before is a fully lit scene, and then only those parts of the scene that should not be lit are darkened.

I know, but thought that I had to darken the whole screen and then add a png with alpha channel to light only a part of the screen, however it didn't work tongue.png . The file wih the alpha channel didn't look like lighting. it was even darker, I tried changing it to alpha blending, alpha mod, but there was no case

If i have to only darken the spots where there's no light, how can i do it? I already got the part of lighting the spot with the alpha channel png, but how do i darken the rest? The only think i can think of is adding multiple semi-transparent black pngs where there shouldn't be lighting but i don't thinks that's convinient.

Edited by Twk Amz

Share this post


Link to post
Share on other sites

I'm not an SDL person, but I believe this method will work.

 

Render the "normal" scene to a texture. Clear the backbuffer to black and render your light map using white to indicate full-light and levels of grey for partial light. Finally, render the texture to the backbuffer with SDL_BLENDMODE_MOD. Modulating by black produces black and modulating by white produces the full-value source color, so it should give a similar effect to what you want.

 

Note: I haven't actually tried this, so if it messes with the perceived color balance then say so. There are some folks around here who are clever with color and they may know a solution.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!