Legacy GM Light system without affecting background

Megax60

Member
i already have a code for the light system, but it also affects the background, pls halp.

here's the code for "obj_lightcontrol"

Create event:
global.light = surface_create(room_width, room_height)

Draw event:
var bbb = 1, i;
with (obj_lightspot){
xs = instance_nearest(x,y,obj_lightrand).xd
}

surface_set_target(global.light)/*With this function you set all further drawing to
the target surface rather than the screen and in this
way you can tell GameMaker:Studio to only draw
specific things to the specified surface.*/

draw_clear(c_black)//That line make the screen black
draw_set_blend_mode(bm_subtract)//We're starting to substract here
with (obj_lightspot){
for (i=0; i <= bbb; i+=1)//This line make the circle glow more
{
draw_sprite_ext(spr_light,0,x,y,xs,xs,0,c_white,1) //This is what we substract! A simple circle.
}
}
draw_set_blend_mode(bm_normal)//Important thing here! Stop to substract.
surface_reset_target()//With this function you reset all further drawing from the target surface back to the screen.

if surface_exists(global.light)//A safe way to avoid errors. If it exist, draw it. If not create it.
{
draw_surface(global.light,0,0)
}else{
global.light=surface_create(room_width,room_height)
}


The "obj_lightspot" is where all the lights are going to be created, this object just creates a "obj_lightrand" and this just randomizes the light size every step.

i want it to not affect the background. pleh
 

Kyon

Member
Well of course it affects the background, you're drawing a big surface over the entire room that's black.
What exactly do you want? Could you explain the lightning effect you're trying to achieve, how it should look?
 
B

BerickCook

Guest
After you clear the surface to black, try drawing your background image on the surface then performing your subtractions. In other words, add this to your code:

draw_clear(c_black)//That line make the screen black
draw_background(YourBackground,0,0);
draw_set_blend_mode(bm_subtract)//We're starting to substract here
 

Kyon

Member
After you clear the surface to black, try drawing your background image on the surface then performing your subtractions. In other words, add this to your code:

draw_clear(c_black)//That line make the screen black
draw_background(YourBackground,0,0);
draw_set_blend_mode(bm_subtract)//We're starting to substract here
This would place the background above the tiles and stuff though right?
 

Surgeon_

Symbian Curator
I had this very problem and I managed to solve it. I don't know if a better way exists because this was NOT pretty to implement. My Draw phases were like this:

  • 1. Clear the application_surface (this is the very beginning of the Draw event).
  • 2. Draw the background on the application_surface.
  • 3. Copy the whole application_surface to a helper surface of the same size.
  • 4. The rest of the draw event happens normally and draws sprites and stuff to the application_surface as usual.
  • 5. A specialized shader is now used to compare the application_surface and the helper surface constructed at the beginning of the Draw event. Its output is saved to yet another surface, which will have opaque white pixels where the two input surfaces compare equal, and opaque black pixels where they are unequal (essentially, it creates a mask by looking in which places the original background can still be seen).
  • 6. At the end of the Draw event the lighting engine renders the lighting on its own surface.
  • 7. The mask (created in step 5) is rendered over the lighting surface created by the engine using additive blending.
  • 8. Finally, the modified lighting surface is rendered to the game world.
  • 9. After that the GUI is drawn like it normally would be etc...
 

Megax60

Member
After you clear the surface to black, try drawing your background image on the surface then performing your subtractions. In other words, add this to your code:

draw_clear(c_black)//That line make the screen black
draw_background(YourBackground,0,0);
draw_set_blend_mode(bm_subtract)//We're starting to substract here
Hey, that worked a bit, it placed the whole background over the titles, but i kinda fixed it:

draw_clear(c_black)//That line make the screen black
draw_background(Background,0,0)
with(obj_solid){
with draw_sprite_ext(spr_solid,0,x,y,image_xscale,image_yscale,0,c_black,1){ //this removes the background that is placed over all obj_solid
depth = -1
}
}
draw_set_blend_mode(bm_subtract)//We're starting to substract here

but the background its still over the tiles, i can't fix that
 
Top