Skip to main content
gamedev.net
🔒 Locked

2D lighting

Started by Hollandera Jun 16, 2010 at 1:14 PM 2 replies 1.1k views
Original Post
Hollandera
Hollandera
I am currently writing a platform game for android, and have asked several questions on this forum. I was wondering what techniques there are for 2D bitmap lighting and what advantages each one has over the other(if there are multiple ways).
rubicondev
rubicondev
Your only realistic option is lightmapping, which works exactly the same as for 3D.

With no vertices to light, or no surfaces made from them, then you're going to be struggling to get useable input for any lighting code.

If you model your sprites in 3D and save them out as renders, I guess there might be a way to save a normal map along with them. I doubt that the Android will have the power to do this per-pixel lighting on the fly though, certainly if you want to do your entire level with it.
------------------------------Great Little War Game
airways
airways
I do simple 2D lighting in my tile based game as follows. The global guy object has properties that tell me if he is carrying a lamp and how bright it is. Each tile also has it's own native lighting level baked into it by the level editor. This is really simple as my needs aren't complex. I'm not sure if you need much more than this.

All lighting in my game is 0-9 which roughly maps to the available colors (0-252).

This code is in BlitzMax. Note that the SetColor() function in BM basically sets a mask used to draw everything - including images - so SetColor(0,0,0) would draw all black pixels while SetColor(255,255,255) would draw pixels as they are in the loaded image file.

Also guy.near(x, y, radius) figures out if a point is within that many squares of the guy. All of this runs on a 25x18 grid of tiles so it is very fast - if you try to use this type of technique with anything denser it very well might not be fast enough.

This could easily be expanded to handle multiple light sources and less easily to do simple shadows.

Function GetLighting:Int(tileX:Int, tileY:Int)	Local l:Int, newL:Int, x:Int, y:Int, d:Int	l = screen.tiles[tileX,tileY].l * 28	If guy.hasLight And guy.near(tileX, tileY, guy.lightSize) Then		If guy.near(tileX, tileY, guy.lightSize-1) Then			newL = guy.hasLight * 28		Else			newL = (guy.hasLight - 2) * 28 ' make the map light "fade" when farther away)		EndIf		If newL > l Then l = newL	EndIf			Return lEndFunctionFunction DrawMap()	Local x:Int, y:Int, i:Int, l:Int, newL:Int, pix:TPixmap,pixel:Int	If screen Then		For x = 0 To 24			For y = 0 To 17				If screen.tiles[x,y] Then				If screen.tiles[x,y].t <> 0 Or screen.tiles[x,y].ext <> "" Then					l = GetLighting(x,y)					SetColor(l,l,l)					DrawImage(images[screen.tiles[x,y].t], x*32, y*32)				EndIf				EndIf			Next		Next	EndIfEndFunction
Zahlman
Zahlman
What kind of effects are you looking for? Draw pictures if necessary.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.