Advertisement
snake5

fx-lightning source

Apr 16th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. include "sgsxgmath";
  3.  
  4.  
  5. function lerp( a, b, t ){ return a * (1-t) + b * t; }
  6. function randrange( from, to ){ return lerp( from, to, randf() ); }
  7.  
  8.  
  9. function generate_lightning( from, to, iterations )
  10. {
  11.     lines = [ [ from, to ] ];
  12.    
  13.     offset_amount = 0.2; // 0.6;
  14.     offset_power = 0.95; // 1.4;
  15.     branch_possibility = 0.9;
  16.     branch_power = 3.7;
  17.     while( iterations --> 0 )
  18.     {
  19.         out = [];
  20.         foreach( line : lines )
  21.         {
  22.             p1 = line[0];
  23.             p2 = line[1];
  24.            
  25.             splitpoint = lerp( p1, p2, randrange( 0.4, 0.6 ) ) + ( p2 - p1 ).perp * randrange(-1,1) * offset_amount;
  26.             out.push
  27.             (
  28.                 [p1,splitpoint],
  29.                 [splitpoint,p2]
  30.             );
  31.             if( randf() < branch_possibility )
  32.             {
  33.                 branchpoint = splitpoint + ( splitpoint - (p1+p2) * 0.5 ).normalized * ((p1-splitpoint).length + (p2-splitpoint).length) * 0.25;
  34.                 out.push([ splitpoint, branchpoint ]);
  35.             }
  36.         }
  37.         lines = out;
  38.        
  39.         offset_amount = pow( offset_amount, offset_power );
  40.         branch_possibility = pow( branch_possibility, branch_power );
  41.     }
  42.    
  43.     return lines;
  44. }
  45.  
  46. function regen_lightning(){ global line_list = generate_lightning( vec2(0,-1), vec2(0.5,1), 5 ); }
  47.  
  48.  
  49. function configure()
  50. {
  51. }
  52.  
  53. function initialize()
  54. {
  55.     global Window = SS_CreateWindow( "fx-lightning", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1024, 576, SDL_WINDOW_OPENGLMAYBE );
  56.     Window.initRenderer( SS_RENDERER_DONTCARE, SS_RENDERER_DONTCARE, SS_RENDERER_VSYNC );
  57.     SS_InitDrawFunctions();
  58.    
  59.     regen_lightning();
  60. }
  61.  
  62. global timeout = 1;
  63. global lasttime = ftime();
  64. function update()
  65. {
  66.     global timeout, lasttime;
  67.     curtime = ftime();
  68.     delta = curtime - lasttime;
  69.     lasttime = curtime;
  70.     timeout -= delta * 2;
  71.     if( timeout < 0 )
  72.     {
  73.         regen_lightning();
  74.         timeout++;
  75.     }
  76.    
  77.     SS_Clear( color(0.1,1) );
  78.     SS_SetCamera2D( 0, 0, 1.5, Window.width/Window.height, 0 );
  79.    
  80.     foreach( line : line_list )
  81.         SS_DrawColorLine( line[0].x, line[0].y, line[1].x, line[1].y, 0.2, 0.4, 0.8, 1 );
  82.    
  83.     SS_Present();
  84. }
  85.  
  86. function on_event( e )
  87. {
  88.     if( e.type == SDL_QUIT )
  89.         global sys_exit = true;
  90. }
  91.  
  92. function cleanup()
  93. {
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement