///////////////////////////////////////////////////////////////////////
// Copyright (C) Andru 2007                                          //
// mail: dr.andru@gmail.com                                          //
// site: http://andru.2x4.ru                                         //
//                                                                   //
// OMEGA Engine is free software.                                    //
//                                                                   //
// You may 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 module 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 module.  If not, write to:                        //
// 	The Free Software Foundation, Inc.,                              //
// 	51 Franklin Street, Fifth Floor                                  //
// 	Boston, MA  02110-1301, USA.                                     //
///////////////////////////////////////////////////////////////////////
unit FX_BLUR;

{$I DEFINES.pas}

interface
uses
  ENGINE_VAR,
  G2D_SSPRITE,
  G2D_CAMERA,
  G2D_FX,
  ENGINE_GL,
  ENGINE_FLAGS,
  GL_UTILS,
  TEXTURE_MANAGER,
  MATH_FAST;

var
  blur_Prev  : Single  = 0;
  blur_Count : Integer = 1;
  blur_MoveX : Integer = 0;
  blur_MoveY : Integer = 0;
  blur_Tex   : PTexture;

  rblur_MoveX : Integer = 0;
  rblur_MoveY : Integer = 0;
  rblur_Times : Integer = 1;
  rblur_Tex   : PTexture;

procedure FX_Blur_SetTex( TexToRender : PTexture ); stdcall;
procedure FX_Blur_SetParams( Count, MoveX, MoveY : Integer ); stdcall;
procedure FX_Blur_Calc( RenderProc : Pointer;
                        Delta      : Single;
                        Alpha      : Byte;
                        BlendMode  : Integer ); stdcall;
procedure FX_Blur_Draw( Color : Integer; Alpha : Byte ); stdcall;

procedure FX_RadBlur_SetTex( TexToRender : PTexture ); stdcall;
procedure FX_RadBlur_SetParams( Times, MoveX, MoveY : Integer ); stdcall;
procedure FX_RadBlur_Calc( RenderProc : Pointer; Alpha : Byte; BlendMode : Integer ); stdcall;
procedure FX_RadBlur_Draw( Color : Integer; Alpha : Byte ); stdcall;

//----------------------------------------------------------------------------//
                                 IMPLEMENTATION
//----------------------------------------------------------------------------//

procedure FX_Blur_SetTex;
Begin
blur_Tex := TexToRender;
End;

procedure FX_Blur_SetParams;
Begin
blur_Count := Count;
blur_MoveX := MoveX;
blur_MoveY := MoveY;
End;

procedure FX_Blur_Calc;
  var
    x, y : Integer;
    CA   : Boolean;
    RC   : Boolean;
Begin

if blur_Tex = nil Then Exit;
Tex_BeginRenderIn( blur_Tex, FALSE, FALSE, TRUE );

asm call RenderProc end;

CA := Cam2D.Active;
if CA Then Cam2D.Active := FALSE;
RC := F_ResolutionCorrect > 0;
if RC Then F_ResolutionCorrect := 0;

blur_Prev := blur_Prev + Delta;
if blur_Prev > blur_Count Then blur_Prev := blur_Count;
X := blur_MoveX * M_Round( blur_Prev * Delta );
Y := blur_MoveY * M_Round( blur_Prev * Delta );
GL_Set2DMatrix;
FX_SetBlendMode( BlendMode );
SSprite_Draw( blur_Tex, -X, -Y, ogl_Width + X * 2, ogl_Height + Y * 2, 255 - Alpha, 0, 1 );
FX_SetBlendMode( 0 );

if CA Then Cam2D.Active := TRUE;
if RC Then F_ResolutionCorrect := 1;

Tex_EndRenderIn;

End;

procedure FX_Blur_Draw;
  var
    CA : Boolean;
    RC : Boolean;
Begin

CA := Cam2D.Active;
if CA Then Cam2D.Active := FALSE;
RC := F_ResolutionCorrect > 0;
if RC Then F_ResolutionCorrect := 0;

FX_SetColorMix( Color );
if blur_Tex <> nil Then
  SSprite_Draw( blur_Tex, 0, 0, ogl_Width, ogl_Height, Alpha, 0, 1 or FX_COLORMIX );

if CA Then Cam2D.Active := TRUE;
if RC Then F_ResolutionCorrect := 1;

End;

procedure FX_RadBlur_SetTex;
Begin
rblur_Tex := TexToRender;
End;

procedure FX_RadBlur_SetParams;
Begin
rblur_Times := Times;
rblur_MoveX := MoveX;
rblur_MoveY := MoveY;
End;

procedure FX_RadBlur_Calc;
  var
    x, y : Integer;
    i    : Integer;
    CA   : Boolean;
    RC   : Boolean;
Begin

if rblur_Tex = nil Then Exit;
Tex_BeginRenderIn( rblur_Tex, FALSE, FALSE, TRUE );

asm call RenderProc end;

CA := Cam2D.Active;
if CA Then Cam2D.Active := FALSE;
RC := F_ResolutionCorrect > 0;
if RC Then F_ResolutionCorrect := 0;

GL_Set2DMatrix;
FX_SetBlendMode( BlendMode );
for i := 0 to rblur_Times - 1 do
  begin
    X := rblur_MoveX * i;
    Y := rblur_MoveY * i;
    if rblur_Tex <> nil Then
      SSprite_Draw( rblur_Tex, -X, -Y, ogl_Width + X * 2, ogl_Height + Y * 2, Alpha, 0, 1 );
  end;
FX_SetBlendMode( 0 );

if CA Then Cam2D.Active := TRUE;
if RC Then F_ResolutionCorrect := 1;

Tex_EndRenderIn;
End;

procedure FX_RadBlur_Draw;
  var
    CA : Boolean;
    RC : Boolean;
Begin

CA := Cam2D.Active;
if CA Then Cam2D.Active := FALSE;
RC := F_ResolutionCorrect > 0;
if RC Then F_ResolutionCorrect := 0;

FX_SetColorMix( Color );
if rblur_Tex <> nil Then
  SSprite_Draw( rblur_Tex, 0, 0, ogl_Width, ogl_Height, Alpha, 0, 1 or FX_COLORMIX );

if CA Then Cam2D.Active := TRUE;
if RC Then F_ResolutionCorrect := 0;

End;

end.
