unit uTitle;

interface

uses  uStateManager;

var TitleScene : rState;

implementation

uses  QuadEngine,
      SysUtils,
      uQEHelp,
      uSMath,
      uVar,
      uConst,
      uStar,
      uStarsFon,
      uMainMenu;

const
  UP_TEXT_ALPHA_TIME    = 1.5;
  DOWN_TEXT_ALPHA_TIME  = 1.5;

var startTime       : Double;
    stUpTextAlpha   : Boolean;
    stDownTextAlpha : Boolean;
    textAlpha       : Double;
    titleText       : string;

procedure TitleInit( param: Pointer );
begin
  startTime := Timer.GetWholeTime;

  titleText :=  'Special for IGDC.RU' + #13 +
                '#72 Space Shooter' + #13 + #13 +
                'Autor and programming:'+ #13 +
                '   BlooV aka Petrouski Artyom'+ #13 + #13 +
                'Additional programming:'+ #13 +
                '   Darthman aka Prokhodtsev Ilya'+ #13 + #13 +
                'Graphics:'+ #13 +
                '   http://www.pixeljoint.com/'+ #13 + #13 +
                'Thank you for your attention.'+ #13 +
                '       Good luck!' + #13 + #13 + #13 + #13 + #13 +
                'Press SPACE to back to menu';

  stUpTextAlpha   := True;
  stDownTextAlpha := False;
  textAlpha       := 0.0;
  startTime       := Timer.GetWholeTime;
end;

procedure TitleDraw;
begin
  Quad.BeginRender;
    Quad.RenderToTexture( True, 1 );
      Quad.Rectangle( 0, 0, AppScrW, AppScrH,
                      GetARGB( 255, 0, 0, 0 ), GetARGB( 255, 0, 0, 0 ),
                      GetARGB( 255, 0, 52, 104 ), GetARGB( 255, 0, 52, 104 ) );
      Quad.SetBlendMode( qbmSrcAlpha );
      DrawAllStars;
    Quad.RenderToTexture( False, 1 );

    Quad.SetBlendMode( qbmNone );
    Quad.RenderToTexture( True, 0 );
      StarsBloomShader.SetShaderState( UseShaders );
        ScreenBuf_1.Draw( 0, 0, $FFFFFFFF );
      StarsBloomShader.SetShaderState( False );
    Quad.RenderToTexture( False, 0 );

    Quad.RenderToTexture( True, 1 );
      ScreenBuf_0.Draw( 0, 0, $FFFFFFFF );
      Quad.SetBlendMode( qbmSrcAlpha );
      Font_3.TextOutCentered( AppScrW * 0.5, 20, 1.0,
                              PAnsiChar( 'Your Score is ' + IntToStr( GameScore ) ),
                              GetARGB( Round( 255 * textAlpha ), 255, 255, 255 ) );
      Font_1.TextOutCentered( AppScrW * 0.5, 105, 1.0, PAnsiChar( titleText ),
                              GetARGB( Round( 255 * textAlpha ), 255, 255, 255 ) );
    Quad.RenderToTexture( False, 1 );

    OldTVShader.SetShaderState( UseShaders );
      ScreenBuf_1.Draw( 0, 0, $FFFFFFFF );
    OldTVShader.SetShaderState( False );
  Quad.EndRender;
end;

procedure TitleUpdate( dt: Double; currentTime: Double );
begin
    if stUpTextAlpha then begin
    textAlpha := ( currentTime - startTime ) / UP_TEXT_ALPHA_TIME;
    if currentTime - startTime >= UP_TEXT_ALPHA_TIME then begin
      stUpTextAlpha := False;
      textAlpha     := 1.0;
    end;
  end;

  if stDownTextAlpha then begin
    textAlpha := 1.0 - ( currentTime - startTime ) / DOWN_TEXT_ALPHA_TIME;
    if currentTime - startTime >= DOWN_TEXT_ALPHA_TIME then begin
      stDownTextAlpha := False;
      textAlpha       := 0.0;
      StateManager.Pop( True );
      StateManager.MakeInit( @startTime );
    end;
  end;

  UpdateAllStars( dt, False );
end;

procedure TitleUpdateFrames( dt: Double; currentTime: Double );
begin
  UpdateAllFrames( dt );
end;

procedure TitleFree;
begin

end;

procedure TitleKeyUp( key: Byte );
begin
  if ( key = K_SPACE ) and not stDownTextAlpha and not stUpTextAlpha then begin
    stDownTextAlpha := True;
    startTime       := Timer.GetWholeTime;
  end;
end;

initialization
  TitleScene.Name    := 'Title Scene';
  TitleScene.onInit  := @TitleInit;
  TitleScene.onDraw  := @TitleDraw;
  SetLength( TitleScene.onTimer, 2 );
  TitleScene.onTimer[ 0 ]  := @TitleUpdate;
  TitleScene.onTimer[ 1 ]  := @TitleUpdateFrames;
  TitleScene.onKill        := @TitleFree;
  TitleScene.onKeyDown     := nil;
  TitleScene.onUpdate    := nil;
  TitleScene.onActivate  := nil;
  TitleScene.onKeyUp     := @TitleKeyUp;
  TitleScene.onKeyChar   := nil;
  TitleScene.onMouseDown := nil;
  TitleScene.onMouseUp   := nil;
  TitleScene.onMouseMove := nil;
end.
