I'm trying to implement some shadow/lighting effects in my 2D-project, and I've concluded that if there is an easy way to perform a masked erase on an SDL_Texture, it would make the drawing operations quite cheap.

  • Let's say I have a texture of the part of the level where light is not meant to be rendered.
  • I also have a texture with my "light map"; I want to use this to just draw omni lights from my light sources.
  • Then I want to use the first image to 'subtract' the portions of the light map that are not to be rendered on the final scene.
  • Then I draw my "light map" texture on top of my scene, with additive blending enabled.

This sounds like a good theory in my head, but I can't see any functions in the SDL2 API that let me do masked erase from a texture.

Am I overlooking something? Does anything like this exist?


I know its kind of old question but I see you got no answers and maybe its still relevant...

why do you want to use additive blending instead of modulate blending (SDL_BLENDMODE_MOD)? with modulate the output is dstRGB = srcRGB * dstRGB, so you can make your lit areas brighter (while white is maximum lit) and dark areas are the shade. if I understood you correctly the result would be exactly what you are trying to achieve.

if you are using a static lightmap its very easy, its just rendering a black & white texture over your scene with MOD blending. you can even use other colors to create colored lights.

if you want to be able to render lights dynamically on your light map you need to create an empty texture and fill it with black pixels, and then render "lights" on it with additive blending (meaning every light will only make the output brighter), where "lights" are simply textures that look like the light you want to add. something like this one:

enter image description here

then after you render all the lights render your texture over your entire scene with modulate blending. whenever you want to update the lighting map clear the texture to black again and redraw all the lights again.

to render on textures you should look into SDL_SetRenderTarget. note that you will also need to create the texture with SDL_TEXTUREACCESS_TARGET flag and when you create the renderer add the SDL_RENDERER_TARGETTEXTURE flag.

if you are intrested I've written a graphic engine using SDL which has the lighting effect I described and the result looks like this: http://www.ness-engine.com/#!Tanks-game-demo-with-ness-engine/zoom/c9oo/image3e6

you can explore the source here, but its a lot of code..: https://github.com/RonenNess/ness-engine/tree/master/source/NessEngine

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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