SirCumferance

Draw Shockwave

May 4th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// @function           shockwave_draw();
  2. /// @description        Draw the shockwave effect
  3.  
  4. // Check for the texture surface and create as required
  5. if !surface_exists(wave_surf)
  6. {
  7. // No texture surface so create it
  8. var _num = 0;
  9. while (!surface_exists(wave_surf))
  10.     {
  11.     if wave_view != -1
  12.         {
  13.         // A view has been apecified so create the surface the size of it
  14.         var ww = camera_get_view_width(view_camera[wave_view]);
  15.         var hh = camera_get_view_height(view_camera[wave_view]);
  16.         wave_surf = surface_create(ww, hh);
  17.         }
  18.     else
  19.         {
  20.         // No view has been specified so create the surface the size of the room
  21.         wave_surf = surface_create(room_width, room_height);
  22.         }
  23.     // Check to make sure the surface was created and show an error if it repeatedly fails
  24.     if _num++ > 25
  25.         {
  26.         show_debug_message("Shockwave: Couldn't Create Effect Surface! Destroying Instance...");
  27.         instance_destroy();
  28.         exit; // Exit the draw event to prevent errors
  29.         }
  30.     }
  31. // Set wave texture variables
  32. wave_surf_t = surface_get_texture(wave_surf);
  33. wave_surf_w = surface_get_width(wave_surf);
  34. wave_surf_h = surface_get_height(wave_surf);
  35. // show_debug_message("Shockwave: Effect Surface Created!");
  36. // Copy the application surface to the texture surface
  37. surface_set_target(wave_surf);
  38. draw_surface_stretched(application_surface, 0, 0, wave_surf_w, wave_surf_h);
  39. surface_reset_target();
  40. }
  41. else
  42. {
  43. // Texture surface exists, so update the shockwave effect if required
  44. if wave_update
  45.    {
  46.    surface_set_target(wave_surf);
  47.    draw_surface_stretched(application_surface, 0, 0, wave_surf_w, wave_surf_h);
  48.    surface_reset_target();
  49.    }
  50.  
  51. // Check effect radius and continue or destroy as necessary
  52. wave_radius += wave_speed;
  53. if (wave_radius > wave_max_radius)
  54.    {
  55.    instance_destroy();
  56.    exit;
  57.    }
  58.  
  59. // Setup local vars for drawing the primitives that the effect uses
  60. // Note that we pre-calculate as much as possible before running the
  61. // main draw loop so that the loop has to do as little as possible.
  62. var _xx, _yy, _cosx, _siny, _wave_dir;
  63. var _wave_ang = 360 / wave_segment;                             // The angle size of each "segment" of the wave effect
  64. var _wave_offset_inside = max(wave_radius - wave_width, 0);     // Calculate the offset from the edge of the wave inwards
  65. var _wave_offset_outside = wave_radius + wave_width;            // Calculate the offset from the edge of the wave outwards
  66. var _wave_multi = 0;                                            // Prepare the "counter" variable that will increment as we create vertices
  67. var _wave_tex_x = 1 / wave_surf_w;                              // Calculate the pixel ratio for the texture (value between 0 - 1) on the x axis
  68. var _wave_tex_y = 1 / wave_surf_h;                              // Calculate the pixel ratio for the texture (value between 0 - 1) on the y axis
  69.  
  70. // Check for the view being active and if it is store its position
  71. // We'll use this later when calculating the texture position.
  72. if wave_view > -1
  73.     {
  74.     var _vx = camera_get_view_x(view_camera[wave_view]);
  75.     var _vy = camera_get_view_y(view_camera[wave_view]);
  76.     }
  77. else
  78.     {
  79.     var _vx = 0;
  80.     var _vy = 0;
  81.     }
  82.  
  83. // Start drawing to the vertex buffer
  84. vertex_begin(wave_vb_inside, wave_vf);
  85. vertex_begin(wave_vb_outside, wave_vf);
  86.  
  87. // Run a loop to create each vertice of the primitive that will draw the shockwave
  88. repeat (wave_segment + 1)
  89.     {
  90.     _wave_dir = _wave_multi * _wave_ang;            // Get the current drawing angle
  91.     _cosx = dcos(_wave_dir);                        // Precaculate x component for position (this saves using lengthdir_x everywhere in the code)
  92.     _siny = -(dsin(_wave_dir));                     // Precaculate y component for position (this saves using lengthdir_y everywhere in the code)
  93.     _xx = x + (_cosx * wave_radius);                // Get the x position of the vertice for the radius of the wave
  94.     _yy = y + (_siny * wave_radius);                // Get the y position of the vertice for the radius of the wave
  95.     // Add radius vertices for both outer and inner rings
  96.     vertex_position(wave_vb_outside, _xx, _yy);
  97.     vertex_position(wave_vb_inside, _xx, _yy);
  98.     _xx = _xx - _vx + (_cosx *  -wave_push);        // Get the x texture position
  99.     _yy = _yy - _vy + (_siny * -wave_push);         // Get the y texture position
  100.     // Add texture coordinates to the vertices
  101.     vertex_texcoord(wave_vb_outside, _wave_tex_x * _xx , _wave_tex_y * _yy);
  102.     vertex_texcoord(wave_vb_inside, _wave_tex_x * _xx, _wave_tex_y * _yy);
  103.     // Add colour data to the vertices
  104.     vertex_colour(wave_vb_outside, wave_blend, image_alpha);
  105.     vertex_colour(wave_vb_inside, wave_blend, image_alpha);
  106.    
  107.     // Draw the outer vertices (same as above but using the offset value to offset it outwards from the wave radius)
  108.     _xx = x + (_cosx * _wave_offset_outside);
  109.     _yy = y + (_siny * _wave_offset_outside);
  110.     vertex_position(wave_vb_outside, _xx, _yy);
  111.     vertex_texcoord(wave_vb_outside, _wave_tex_x * (_xx - _vx) , _wave_tex_y * (_yy - _vy));
  112.     vertex_colour(wave_vb_outside, wave_blend, 0);
  113.    
  114.     // Draw the inner vertices (same as above but using the offset value to offset it inwards from the wave radius)
  115.     _xx = x + (_cosx * _wave_offset_inside);
  116.     _yy = y + (_siny * _wave_offset_inside);
  117.     vertex_position(wave_vb_inside, _xx, _yy);
  118.     vertex_texcoord(wave_vb_inside, _wave_tex_x * (_xx - _vx) , _wave_tex_y * (_yy - _vy));
  119.     vertex_colour(wave_vb_inside, wave_blend, 0);
  120.    
  121.     ++_wave_multi;
  122.     }
  123.  
  124. // End the defining of the vertex buffers
  125. vertex_end(wave_vb_inside);
  126. vertex_end(wave_vb_outside);
  127.  
  128. // Check for additive blending (this can be removed if you never use additive blending)
  129. if wave_additive
  130.     {
  131.     gpu_set_blendmode(bm_add);
  132.     }
  133.  
  134. // Draw the primitives in the buffers
  135. vertex_submit(wave_vb_inside,  pr_trianglestrip, wave_surf_t);
  136. vertex_submit(wave_vb_outside, pr_trianglestrip, wave_surf_t);
  137.  
  138. // Reset additive if enabled (this can be removed if you never use additive blending)
  139. if wave_additive
  140.     {
  141.     gpu_set_blendmode(bm_normal);
  142.     }
  143.    
  144. // Set total alpha (the wave will fade as it gets bigger)
  145. image_alpha -= wave_alpha;
  146. }
Add Comment
Please, Sign In to add comment