/*
    AGGRESSIVE PONG.
    Copyright (C) 2010  Eugene Popov (popov_e_n@mail.ru)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

#include "../lib/wm.inc.prg"

import "mod_timers"


process x_spark( int graph, int x, int y, int vx, int vy, int ax, int ay, int roll )
begin
  z = wm_z_inact + 1 ;
/*  flags |= B_HMIRROR * rand(0,1);
  flags |= B_VMIRROR * rand(0,1);*/
  while( ! out_region( id, 0 ) )
    frame;
    angle += roll;
    x  += vx/10;
    y  += vy/10;
    vx += ax;
    vy += ay;
  end
end

process blanker( int col )
begin
  x = 0;
  y = 0;
  z = 0;
  graph = map_new( scr_width(), scr_height(), 16 );
  set_center( file, graph, 0, 0 );
  map_clear( file, graph, col );
  loop frame; end
onexit
  map_unload( file, graph );
end

process intro()
local
  int bb, s1, s2, f, aa;
end
begin
  
  x = 0;
  y = 0;
  z = 0;
  
  aa = 10;
  
  bb = blanker( rgb(250,250,250) );
  alpha = 0;
  frame;
  s1 = png_load('../data/splash1.png');
  set_center( file, s1, 0, 0 );
  graph = s1;
  while(alpha<255) alpha+=aa; frame; end
  frame;
  timer[0] = 0;
  while( timer[0]<250 ) frame; end;
  
  while(alpha>0) alpha-=aa; frame; end
  
  map_unload( file, s1 );

  s2 = png_load('../data/splash2.png');
  set_center( file, s2, 0, 0 );  
  graph = s2;
  while(alpha<255) alpha+=aa; frame; end
  frame;
  timer[0] = 0;
  while( timer[0]<250 ) frame; end;
  
  signal( bb, S_KILL );
  frame;
  while(alpha>0) alpha-=aa; frame; end
    
  map_unload( file, s2 );
end

process titlescreen()
local
  int im1, im2, f;
end
begin
  
  graph = map_new( scr_width(), scr_height(), 16);
  set_center( file, graph, 0, 0 );
  
  map_put( file, graph, img_title1 ,(scr_width() - graphic_info( file, img_title1, G_WIDTH) )/2, 2);
  map_put( file, graph, img_title2 ,(scr_width() - graphic_info( file, img_title2, G_WIDTH) )/2, scr_height() - graphic_info( file, img_title2, G_HEIGHT) -2);
  x = 0;
  y = 0;
  z = wm_z_inact+100;
  
  loop 
    frame;
  end;
  
onexit
  map_unload( file, graph );
//   map_unload( file, im1 );
//   map_unload( file, im2 );
end

process intermission( string labelfile )
local
  int lx,rx, lbl;
end
begin
  lx =0;
  rx = scr_width();
  x  = scr_width()/2;
  y  = scr_height()/2;
  z  = 2;
  
  lbl = png_load( labelfile );
  set_center ( file, lbl, graphic_info( file, img_inter_l, G_WIDTH), graphic_info( file, img_inter_l, G_HEIGHT)/2 );
  while( rx>0 )
    graph = map_new ( scr_width(), scr_height(), 16 );
    map_put( file, graph, img_inter_r, rx, y );
    map_put( file, graph, img_inter_l, lx, y );
    map_put( file, graph, lbl, lx, y );
    frame 70;
    rx-=10;
    lx+=10;
    map_unload( file, graph );
  end;

  graph = map_new ( scr_width(), scr_height(), 16 );
  map_put( file, graph, img_inter_r, rx, y );
  map_put( file, graph, img_inter_l, lx, y );
  map_put( file, graph, lbl, lx, y );
  
  rx = 60;
  while( rx-->=0 )
    frame;
  end
  
  while( lx>0 )
    graph = map_new ( scr_width(), scr_height(), 16 );
    map_put( file, graph, img_inter_r, rx, y );
    map_put( file, graph, img_inter_l, lx, y );
    map_put( file, graph, lbl, lx, y );
    frame 70;
    rx+=10;
    lx-=10;
    map_unload( file, graph );
  end;

  map_unload( file, graph );
  map_unload( file, lbl );
  
end


process final_animation()
local
end
begin
  graph = png_load( '../data/cup.png' );
  set_center( file, graph, 0, 0 );
  z = wm_z_inact - 1;
  loop
    x_spark( img_sparkle, 1,             scr_height()-2, rand( 10, 30  ), rand( -70, 0 ),  0 , 1, 0 );
    x_spark( img_sparkle, scr_width()-2, scr_height()-2, rand( -30, -10 ), rand( -70, 0 ), 0 , 1, 0 );
    frame;
  end;
onexit
  map_unload( file, graph );
end
