2D Flashlight Effect?

BlitzMax Forums/BlitzMax Beginners Area/2D Flashlight Effect?

Chroma(Posted 2011) [#1]
I'm looking to have the screen black but have a round flashlight at the mouse cursor. So I would draw my regular graphics, then draw a black sprite the size of the screen, and then draw a round sprite that would need to be able to cut through the black. Anyone have any ideas on how to do this?


Arabia(Posted 2011) [#2]
Just a guess, not sure if this is the right/best way of doing it. Have a massive black sprite with a transparent circle in it - then just position this sprite with an offset to make the circle centre where the mouse is.

I'm sure there is a better way of doing this, but I'm not the right person to ask :D


Chroma(Posted 2011) [#3]
That would definitely work. But I was thinking of some covering the whole screen with one large black sprite and then having a sprite with a white or transparent circle on it and doing some sort of bit mask or something. is there a way to do it like that?


Chroma(Posted 2011) [#4]
MaskPixMap? Sounds about what I'm looking to do. I'll report back with a demo if I get it working.


ima747(Posted 2011) [#5]
Not quite what you want but you could look into using light blend, might be a way to get it out of there, I've gotten some neat lighting effects in a sewer environment before with it...


Zeke(Posted 2011) [#6]
http://www.blitzmax.com/Community/posts.php?topic=49403


Chroma(Posted 2011) [#7]
Sweet, thanks for the replies!


AdamRedwoods(Posted 2011) [#8]
Another idea, similar to the above:

create a black box, cut out your circle (use feathering if desired).
create a normal black box.
load these up as TImages.
draw your scene.
draw these boxes, the cutout box is where the mouse pointer is.
the black box, you move the handle to it's topleft, and stretch it so it's covering the screen up to the cutout box. (or you could use DrawRect)
Do the same for the left, right, and bottom sides of the cutout box.

Another idea:
draw your scene
use GrabImage the size of your cutout box
clear the screen to black, draw the image, then the cutout box over the top of it


Kryzon(Posted 2011) [#9]
No need to use big sprites. This can be achieved with clever blending.

- Draw the flashlight sprite to set up the "visibility" of the next images to be drawn.
- Set blending to be ShadeBlend. Whatever you draw next will only appear on the already visible parts of the screen. This visible part was defined previously by the flashlight sprite, so the user will only see what happens to be drawn on top of the flashlight.

Download this image as the "flashlight.png" to use with the code.
SuperStrict 
SetGraphicsDriver GLMax2DDriver()
Graphics 800,600 

For Local i:Int = 1 To 100
	SetColor Rand(0,255),Rand(0,255),Rand(0,255)
	DrawRect Rand(-20,800),Rand(-20,600),Rand(100,400),Rand(100,400)
Next
SetColor 255,255,255 'Restore default color.

Local background:TImage = CreateImage(800,600)
GrabImage background,0,0
Local flashlight:TImage = LoadImage("flashlight.png")
MidHandleImage flashlight 

While Not (KeyHit(KEY_ESCAPE) Or AppTerminate())
	Cls 
	
	SetBlend ALPHABLEND 'Allow the flashlight sprite to have a feathered image.
	
	DrawImage flashlight,MouseX(),MouseY() 'Draw first the flashlight sprite.
	
	SetBlend SHADEBLEND 'Setup the background blend.
	
	DrawImage background,0,0			    'Draw the background and anything else.
    'DrawImage [...]
	
	Flip
Wend