Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// @function lights_surface_create()
- /// @description loops through light objects and copies to a surface
- function lights_surface_create() {
- // We only want this to run on the layer's MAIN Draw event, so if it's
- // anything except the main Draw event, return from the function early.
- // Weirdly, T=there does not appear to be a constant for the main Draw
- // event, hence the 0.
- if (event_type != ev_draw || event_number != 0)
- return;
- #region create surfaces if needed
- if (!surface_exists(global.maskLightingSurface))
- global.maskLightingSurface = surface_create(global.viewW, global.viewH);
- if (!surface_exists(global.lightingSurface))
- global.lightingSurface = surface_create(global.viewW, global.viewH);
- #endregion create surfaces if needed
- #region create mask surface
- // the mask surface will act like a "stencil" for our lighting surface
- surface_set_target(global.maskLightingSurface);
- {
- draw_clear(c_black); // paint it black
- gpu_set_blendmode(bm_subtract); // bm_subtract will allow us to cut the holes in the mask where the lights will be
- draw_set_circle_precision(12); // circles will be drawn as 12-sided shapes, dodecagons (you don't have to do this, I just think it has a cool effect)
- with (obj_light) // loop through our light objects
- {
- // viewX, viewY is the camera pos, but we want to draw to the surface, which has
- // its own coordinates, so subtract the camera pos from the light object's pos.
- // The +1 and +2 are just tweaks for looks, and the + z is specific to my game,
- // so you probably don't need it.
- var _x = x + 1 - global.viewX;
- var _y = y + 2 + z - global.viewY;
- draw_circle(_x, _y, radius, false); // since our blend mode is bm_subtract, this actually cuts a hole in the black surface
- }
- gpu_set_blendmode(bm_normal); // always reset the blend mode
- }
- surface_reset_target(); // always reset the surface target too :)
- #endregion create mask surface
- #region draw lighting surface
- surface_set_target(global.lightingSurface)
- {
- // Okay, here's the good stuff. Everything under this layer has already been
- // drawn to the application_surface, which is the game's main surface. Draw
- // the application_surface to the lighting surface.
- draw_surface_stretched(application_surface, 0, 0, global.viewW, global.viewH);
- // Use the mask surface to cut out everything that isn't the lighting. The mask
- // will just be black with holes where the "lights" will be, meaning when we use
- // bm_subtract to draw the mask surface onto the lighting surface, all the mask's
- // black will be removed from the lighting surface and leave all the "holes" alone.
- // It sounds confusing, but essentially lightingSurface will be left with circles,
- // and later we'll just copy those circles on top of the night filter for our lights!
- gpu_set_blendmode(bm_subtract);
- draw_surface(global.maskLightingSurface, 0, 0); // "stamp" our mask/stencil
- gpu_set_blendmode(bm_normal); // always reset blend mode!
- }
- surface_reset_target(); // always reset surface target
- #endregion draw lighting surface
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement