Advertisement
MRDANEEYUL

lights_surface_create

Oct 15th, 2022 (edited)
2,342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Game Maker 3.11 KB | Source Code | 0 0
  1. /// @function       lights_surface_create()
  2. /// @description    loops through light objects and copies to a surface
  3. function lights_surface_create() {
  4.    
  5.     // We only want this to run on the layer's MAIN Draw event, so if it's
  6.     // anything except the main Draw event, return from the function early.
  7.     // Weirdly, T=there does not appear to be a constant for the main Draw
  8.     // event, hence the 0.
  9.     if (event_type != ev_draw || event_number != 0)
  10.         return;
  11.    
  12.     #region create surfaces if needed
  13.    
  14.     if (!surface_exists(global.maskLightingSurface))
  15.         global.maskLightingSurface = surface_create(global.viewW, global.viewH);
  16.        
  17.     if (!surface_exists(global.lightingSurface))
  18.         global.lightingSurface = surface_create(global.viewW, global.viewH);
  19.    
  20.     #endregion create surfaces if needed
  21.    
  22.     #region create mask surface
  23.    
  24.     // the mask surface will act like a "stencil" for our lighting surface
  25.     surface_set_target(global.maskLightingSurface);
  26.     {
  27.         draw_clear(c_black); // paint it black
  28.         gpu_set_blendmode(bm_subtract); // bm_subtract will allow us to cut the holes in the mask where the lights will be
  29.         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)
  30.         with (obj_light) // loop through our light objects
  31.         {
  32.             // viewX, viewY is the camera pos, but we want to draw to the surface, which has
  33.             // its own coordinates, so subtract the camera pos from the light object's pos.
  34.             // The +1 and +2 are just tweaks for looks, and the + z is specific to my game,
  35.             // so you probably don't need it.
  36.             var _x = x + 1 - global.viewX;
  37.             var _y = y + 2 + z - global.viewY;
  38.    
  39.             draw_circle(_x, _y, radius, false); // since our blend mode is bm_subtract, this actually cuts a hole in the black surface
  40.         }
  41.         gpu_set_blendmode(bm_normal); // always reset the blend mode
  42.     }
  43.     surface_reset_target(); // always reset the surface target too :)
  44.    
  45.     #endregion create mask surface
  46.  
  47.     #region draw lighting surface
  48.    
  49.     surface_set_target(global.lightingSurface)
  50.     {      
  51.         // Okay, here's the good stuff. Everything under this layer has already been
  52.         // drawn to the application_surface, which is the game's main surface. Draw
  53.         // the application_surface to the lighting surface.
  54.         draw_surface_stretched(application_surface, 0, 0, global.viewW, global.viewH);
  55.                
  56.         // Use the mask surface to cut out everything that isn't the lighting. The mask
  57.         // will just be black with holes where the "lights" will be, meaning when we use
  58.         // bm_subtract to draw the mask surface onto the lighting surface, all the mask's
  59.         // black will be removed from the lighting surface and leave all the "holes" alone.
  60.         // It sounds confusing, but essentially lightingSurface will be left with circles,
  61.         // and later we'll just copy those circles on top of the night filter for our lights!
  62.         gpu_set_blendmode(bm_subtract);
  63.         draw_surface(global.maskLightingSurface, 0, 0); // "stamp" our mask/stencil
  64.         gpu_set_blendmode(bm_normal); // always reset blend mode!
  65.  
  66.     }
  67.     surface_reset_target(); // always reset surface target
  68.    
  69.     #endregion draw lighting surface
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement