unit uLogo;

interface

uses  uStateManager;

var LogoScene : rState;

implementation

uses  QuadEngine,
      uQEHelp,
      uSMath,
      uVar,
      uStar,
      uStarsFon,
      uGame,
      uTitle;

const
  WAIT_DARK_TIME        = 0.100;
  DRAW_DARK_TIME        = 1.000;
  WAIT_TO_LOGO_TIME     = 1.000;
  UP_LOGO_ALPHA_TIME    = 2.500;
  WAIT_LOGO_TIME        = 3.000;
  DOWN_LOGO_ALPHA_TIME  = 2.500;
  WAIT_TO_SKY_TIME      = 1.000;
  DOWN_SKY_TIME         = 2 / STARS_SPEED_COF;
  STOP_STARS_TIME       = 1.500;
{  WAIT_DARK_TIME        = 0.200;
  DRAW_DARK_TIME        = 0.500;
  WAIT_TO_LOGO_TIME     = 0.500;
  UP_LOGO_ALPHA_TIME    = 0.500;
  WAIT_LOGO_TIME        = 1.000;
  DOWN_LOGO_ALPHA_TIME  = 0.500;
  WAIT_TO_SKY_TIME      = 0.500;
  DOWN_SKY_TIME         = 0.3 / STARS_SPEED_COF;
  STOP_STARS_TIME       = 0.500;}

var startTime     : Double;
    startTime2    : Double;

    darkAlpha     : Single;
    waitDark      : Boolean;
    drawDark      : Boolean;

    logoAlpha     : Single;
    waitToLogo    : Boolean;
    upLogoAlpha   : Boolean;
    waitLogo      : Boolean;
    downLogoAlpha : Boolean;

    skyY          : Single;
    waitToSky     : Boolean;
    downSky       : Boolean;

    stopStars     : Boolean;
    goToMenu      : Boolean;

procedure LogoInit( param: Pointer );
begin
  /////////////////////////////
  /// stars
  spaceRect.X := -MIN_DELTA_X;
  spaceRect.Y := -MIN_DELTA_X;
  spaceRect.Width := AppScrW + 2 * MIN_DELTA_X;
  spaceRect.Height:= AppScrH + 2 * MIN_DELTA_X;

  currentSpeed := Vec2f( 0, AppScrH * STARS_SPEED_COF );

  GenerateFullSpace;

  ////////////////////////////
  /// params
  darkAlpha := 1.0;
  waitDark  := True;
  drawDark  := False;

  logoAlpha     := 0.0;
  upLogoAlpha   := False;
  waitLogo      := False;
  waitToLogo    := False;
  downLogoAlpha := False;

  skyY          := -2 * AppScrH;
  waitToSky     := True;
  downSky       := False;

  stopStars     := False;
  goToMenu      := False;

  startTime := Timer.GetWholeTime;
  startTime2:= startTime;
end;

procedure LogoDraw;
begin
  Quad.BeginRender;
    Quad.RenderToTexture( True, 1 );
      Quad.Rectangle( 0, skyY, AppScrW, skyY + AppScrH,
                      GetARGB( 255, 0, 0, 0 ), GetARGB( 255, 0, 0, 0 ),
                      GetARGB( 255, 0, 52, 104 ), GetARGB( 255, 0, 52, 104 ) );
      Quad.Rectangle( 0, skyY + AppScrH, AppScrW, skyY + 3 * AppScrH, 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 );

      if upLogoAlpha or waitLogo or downLogoAlpha then begin
        Quad.SetBlendMode( qbmSrcAlpha );
        QuadLogo.DrawRot( AppScrW / 2, AppScrH /2, 0, 1.0,
                          GetARGB( Round( 255 * logoAlpha ), 255, 255, 255 ) );
      end;

      if waitDark or drawDark then begin
        Quad.SetBlendMode( qbmSrcAlpha );
        Quad.Rectangle( 0, 0,AppScrW, AppScrH, GetARGB( Round( 255 * darkAlpha ), 0, 0, 0 ) );
      end;
    Quad.RenderToTexture( False, 1 );

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

procedure LogoUpdate( dt: Double; currentTime: Double );
begin
  if waitDark then begin
    darkAlpha := 1.0;
    if currentTime - startTime >= WAIT_DARK_TIME then begin
      waitDark  := False;
      drawDark  := True;
      startTime := currentTime;
    end;
  end;

  if drawDark then begin
    darkAlpha := 1.0 - ( currentTime - startTime ) / DRAW_DARK_TIME;
    if currentTime - startTime >= DRAW_DARK_TIME then begin
      drawDark    := False;
      waitToLogo  := True;
      startTime   := currentTime;
    end;
  end;

  if waitToLogo then begin
    if currentTime - startTime >= WAIT_TO_LOGO_TIME then begin
      waitToLogo  := False;
      upLogoAlpha := True;
      startTime   := currentTime;
    end;
  end;

  if upLogoAlpha then begin
    logoAlpha := ( currentTime - startTime ) / UP_LOGO_ALPHA_TIME;
    if currentTime - startTime >= UP_LOGO_ALPHA_TIME then begin
      upLogoAlpha := False;
      waitLogo    := True;
      logoAlpha   := 1.0;
      startTime   := currentTime;
    end;
  end;

  if waitLogo then begin
    if currentTime - startTime >= WAIT_LOGO_TIME then begin
      waitLogo      := False;
      downLogoAlpha := True;
      startTime     := currentTime;
    end;
  end;

  if downLogoAlpha then begin
    logoAlpha := 1.0 - ( currentTime - startTime ) / DOWN_LOGO_ALPHA_TIME;
    if currentTime - startTime >= DOWN_LOGO_ALPHA_TIME then begin
      downLogoAlpha := False;
      //waitToSky     := True;
      logoAlpha     := 0.0;
      startTime     := currentTime;
    end;
  end;

  if waitToSky then begin
    if currentTime - startTime2 >= WAIT_TO_SKY_TIME then begin
      waitToSky   := False;
      downSky     := True;
      startTime2  := currentTime;
    end;
  end;

  if downSky then begin
    skyY := - 2 * AppScrH + 2 * AppScrH * ( currentTime - startTime2 ) / DOWN_SKY_TIME;
    if currentTime - startTime2 >= DOWN_SKY_TIME then begin
      downSky     := False;
      stopStars   := True;
      skyY        := 0;
      startTime2  := currentTime;
      startTime   := currentTime;
    end;
  end;

  if stopStars then begin
    currentSpeed := Vec2f(  0, AppScrH * STARS_SPEED_COF * ( 1.0 - ( currentTime - startTime ) / STOP_STARS_TIME ) );
    if currentTime - startTime >= STOP_STARS_TIME then begin
      currentSpeed  := Vec2fZero;
      StateManager.Pop;
//      StateManager.Push( @GameScene );
//      StateManager.Push( @TitleScene );
      StateManager.MakeInit;
      Exit;
    end;
  end;

  UpdateAllStars( dt );
end;

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

procedure LogoFree;
begin

end;

procedure LogoKeyDown( key: Byte );
begin

end;

initialization
  LogoScene.Name    := 'Logo Scene';
  LogoScene.onInit  := @LogoInit;
  LogoScene.onDraw  := @LogoDraw;
  SetLength( LogoScene.onTimer, 2 );
  LogoScene.onTimer[ 0 ]  := @LogoUpdate;
  LogoScene.onTimer[ 1 ]  := @LogoUpdateFrames;
  LogoScene.onKill        := @LogoFree;
  LogoScene.onKeyDown     := @LogoKeyDown;
  LogoScene.onUpdate    := nil;
  LogoScene.onActivate  := nil;
  LogoScene.onKeyUp     := nil;
  LogoScene.onKeyChar   := nil;
  LogoScene.onMouseDown := nil;
  LogoScene.onMouseUp   := nil;
  LogoScene.onMouseMove := nil;
end.
