unit uMain;

interface

procedure AppInit;
procedure AppDraw;
procedure AppTimer( var delta: Double );
procedure AppTimer_2( var delta: Double );
procedure AppFree;

implementation

uses  QuadEngine,
      Windows,
      uStateManager,
      uConst,
      uVar,
      uLogo,
      uMainMenu,
      uBass;

procedure AppInit;
begin
  //////////////////////////////////////////////////////////////////////////////
  //font
  Font_1  := Quad.CreateFont;
  Font_1.LoadFromFile( DataDir + 'fnt\font_1.png', DataDir + 'fnt\font_1.qef' );

  Font_2  := Quad.CreateFont;
  Font_2.LoadFromFile( DataDir + 'fnt\font_2.png', DataDir + 'fnt\font_2.qef' );

  Font_3  := Quad.CreateFont;
  Font_3.LoadFromFile( DataDir + 'fnt\font_3.png', DataDir + 'fnt\font_3.qef' );
  //////////////////////////////////////////////////////////////////////////////
  //sprites
//  DarkSprite := Quad.CreateTexture;
//  DarkSprite.LoadFromFile( 0, GfxDir + 'dark.jpg' );

  QuadLogo := Quad.CreateTexture;
  QuadLogo.LoadFromFile( 0, GfxDir + 'quad.png' );

//  Sky := Quad.CreateTexture;
//  Sky.LoadFromFile( 0, GfxDir + 'sky.png' );

//  SkyGradient := Quad.CreateTexture;
//  SkyGradient.LoadFromFile( 0, GfxDir + 'gradient.png' );

//  TextGradient := Quad.CreateTexture;
//  TextGradient.LoadFromFile( 0, GfxDir + 'gradient_2.png' );

  Star := Quad.CreateTexture;
  Star.LoadFromFile( 0, GfxDir + 'star.png', 64, 64 );

  Player      := Quad.CreateTexture;
  Player.LoadFromFile( 0, GfxDir + 'player.png', 128, 128 );

  Enemy_1     := Quad.CreateTexture;
  Enemy_1.LoadFromFile( 0, GfxDir + 'enemy_1.png', 128, 128 );

  Enemy_2     := Quad.CreateTexture;
  Enemy_2.LoadFromFile( 0, GfxDir + 'enemy_2.png', 128, 64 );

  Enemy_2_flm := Quad.CreateTexture;
  Enemy_2_flm.LoadFromFile( 0, GfxDir + 'enemy_2_flame.png', 128, 64 );

  Enemy_3     := Quad.CreateTexture;
  Enemy_3.LoadFromFile( 0, GfxDir + 'enemy_3.png', 92, 36 );

  Enemy_3_flm := Quad.CreateTexture;
  Enemy_3_flm.LoadFromFile( 0, GfxDir + 'enemy_3_flame.png', 92, 36 );

  Enemy_2_2     := Quad.CreateTexture;
  Enemy_2_2.LoadFromFile( 0, GfxDir + 'enemy_2_2.png', 128, 64 );

  Enemy_2_2_flm := Quad.CreateTexture;
  Enemy_2_2_flm.LoadFromFile( 0, GfxDir + 'enemy_2_2_flame.png', 128, 64 );

//  Enemy_4     := Quad.CreateTexture;
//  Enemy_4.LoadFromFile( 0, GfxDir + 'enemy_4.png' );

//  Enemy_5     := Quad.CreateTexture;
//  Enemy_5.LoadFromFile( 0, GfxDir + 'enemy_5.png', 44, 39 );

  Enemy_6     := Quad.CreateTexture;
  Enemy_6.LoadFromFile( 0, GfxDir + 'enemy_6.png' );

  Bullet_1 := Quad.CreateTexture;
  Bullet_1.LoadFromFile( 0, GfxDir + 'plasma_2_small_frame.png', 32, 32 );

  Bullet_2 := Quad.CreateTexture;
  Bullet_2.LoadFromFile( 0, GfxDir + 'plasma_1_small_frame.png', 32, 32 );

  Bullet_3 := Quad.CreateTexture;
  Bullet_3.LoadFromFile( 0, GfxDir + 'plasma_3_small_frame.png', 32, 32 );

  Bullet_4 := Quad.CreateTexture;
  Bullet_4.LoadFromFile( 0, GfxDir + 'plasma_4_small_frame.png', 32, 32 );

  Explosion_1 := Quad.CreateTexture;
  Explosion_1.LoadFromFile( 0, GfxDir + 'explosion_1.png', 120, 259 );

  //Explosion_2 := Quad.CreateTexture;
  //Explosion_2.LoadFromFile( 0, GfxDir + 'explosion_2.png', 49, 30 );

  //Explosion_3 := Quad.CreateTexture;
  //Explosion_3.LoadFromFile( 0, GfxDir + 'explosion_3.png', 48, 48 );

  //Explosion_4 := Quad.CreateTexture;
  //Explosion_4.LoadFromFile( 0, GfxDir + 'explosion_4.png', 100, 50 );

  //Explosion_5 := Quad.CreateTexture;
  //Explosion_5.LoadFromFile( 0, GfxDir + 'explosion_5.png', 81, 32 );

  //Explosion_6 := Quad.CreateTexture;
  //Explosion_6.LoadFromFile( 0, GfxDir + 'explosion_6.png', 96, 32 );

  Explosion_7 := Quad.CreateTexture;
  Explosion_7.LoadFromFile( 0, GfxDir + 'explosion_7.png', 44, 44 );

  //Explosion_8 := Quad.CreateTexture;
  //Explosion_8.LoadFromFile( 0, GfxDir + 'explosion_8.png', 58, 63 );

  Explosion_9 := Quad.CreateTexture;
  Explosion_9.LoadFromFile( 0, GfxDir + 'explosion_9.png', 100, 80 );

  //Explosion_10:= Quad.CreateTexture;
  //Explosion_10.LoadFromFile( 0, GfxDir + 'explosion_10.png', 45, 44 );

  Explosion_11:= Quad.CreateTexture;
  Explosion_11.LoadFromFile( 0, GfxDir + 'explosion_11.png', 64, 64 ); 

  //////////////////////////////////////////////////////////////////////////////
  //shaders
  StarsBloomShader  := Quad.CreateShader;
  StarsBloomShader.LoadPixelShader( ShdDir + 'stars_bloom.bin' );

  OldTVShader       := Quad.CreateShader;
  OldTVShader.LoadPixelShader( ShdDir + 'oldTV.bin' );
  OldTVShader.BindVariableToPS( 1, @AppPixelSizeOldTV, SizeOf( AppPixelSizeOldTV ) );

  //BlurCHShader      := Quad.CreateShader;
  //BlurCHShader.LoadPixelShader( ShdDir + 'blur_ch.bin' );
  //BlurCHShader.BindVariableToPS( 1, @AppPixelWidth, SizeOf( AppPixelWidth ) );

  //BlurCVShader      := Quad.CreateShader;
  //BlurCVShader.LoadPixelShader( ShdDir + 'blur_cv.bin' );
  //BlurCVShader.BindVariableToPS( 1, @AppPixelHeight, SizeOf( AppPixelHeight ) );

  BloomShader       := Quad.CreateShader;
  BloomShader.LoadPixelShader( ShdDir + 'bloom_st.bin' );
  BloomShader.BindVariableToPS( 1, @AppPixelSizeBlur, SizeOf( AppPixelSizeBlur ) );

  BlurHShader       := Quad.CreateShader;
  BlurHShader.LoadPixelShader( ShdDir + 'blur_h.bin' );
  //BlurHShader.BindVariableToPS( 1, @AppPixelWidth, SizeOf( AppPixelWidth ) );
  BlurHShader.BindVariableToPS( 1, @AppBlurPO_Y, SizeOf( AppBlurPO_Y ) );

  BlurVShader       := Quad.CreateShader;
  BlurVShader.LoadPixelShader( ShdDir + 'blur_v.bin' );
  //BlurVShader.BindVariableToPS( 1, @AppPixelHeight, SizeOf( AppPixelHeight ) );
  BlurVShader.BindVariableToPS( 1, @AppBlurPO_X, SizeOf( AppBlurPO_X ) );

  //////////////////////////////////////////////////////////////////////////////
  //render targets
  Quad.CreateRenderTexture( AppScrW, AppScrH, 0 );
  ScreenBuf_0 := Quad.CreateTexture;
  ScreenBuf_0.CreateFromRenderTarget( 0, 0 );

  Quad.CreateRenderTexture( AppScrW, AppScrH, 1 );
  ScreenBuf_1 := Quad.CreateTexture;
  ScreenBuf_1.CreateFromRenderTarget( 1, 0 );

  Quad.CreateRenderTexture( AppScrW, AppScrH, 2 );
  ScreenBuf_2 := Quad.CreateTexture;
  ScreenBuf_2.CreateFromRenderTarget( 2, 0 );

  Quad.CreateRenderTexture( AppScrW div 2, AppScrH div 2, 3 );
  BloomBuf_1 := Quad.CreateTexture;
  BloomBuf_1.CreateFromRenderTarget( 3, 0 );

  Quad.CreateRenderTexture( AppScrW div 2, AppScrH div 2, 4 );
  BloomBuf_2 := Quad.CreateTexture;
  BloomBuf_2.CreateFromRenderTarget( 4, 0 );

  Quad.CreateRenderTexture( AppScrW div 2, AppScrH div 2, 5 );
  BloomBuf_3 := Quad.CreateTexture;
  BloomBuf_3.CreateFromRenderTarget( 5, 0 );

  Quad.CreateRenderTexture( AppScrW, AppScrH, 6 );
  ScreenBuf_3 := Quad.CreateTexture;
  ScreenBuf_3.CreateFromRenderTarget( 6, 0 );

  //////////////////////////////////////////////////////////////////////////////
  //other
  InitializeCriticalSection( Section_EnemyDel );
  InitializeCriticalSection( Section_FrameUpdt );

  Load_Sound( Music, DataDir + 'sounds\music.mp3', 1, True );
  Play_Sound( Music );

  StateManager.Push( @MainMenuScene );
  StateManager.Push( @LogoScene );
  StateManager.MakeInit;  

  AppInited := True;
end;

procedure AppDraw;
begin
  if not AppInited then Exit;  

  AppDrawTime := Timer.GetWholeTime;
    StateManager.MakeDraw;
  AppDrawTime := Timer.GetWholeTime - AppDrawTime;
end;

procedure AppTimer( var delta: Double );
begin
  if not AppInited then Exit;

  StateManager.MakeTimer( 0, delta, Timer.GetWholeTime );

  AppDraw;
end;

procedure AppTimer_2( var delta: Double );
begin
  if not AppInited then Exit;

  StateManager.MakeTimer( 1, delta, Timer_2.GetWholeTime );
end;

procedure AppFree;
begin
  StateManager.PopAll( True );

  Kill_Sounds;

  DeleteCriticalSection( Section_EnemyDel );
  DeleteCriticalSection( Section_FrameUpdt );
end;

end.
