///////////////////////////////////////////////////////////////////////
// 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 G2D_CSPRITE;

{$I DEFINES.pas}

interface
uses
  OpenGL,

  ENGINE_VAR,
  ENGINE_SCREEN,

  ENGINE_FLAGS,

  G2D_CAMERA,

  G2D_FX,
  G2D_PRIMITIVES,

  TEXTURE_MANAGER,
  TEXTURE_LOADER,

  MATH_FAST;

procedure CSprite_Draw(
                        Tex           : PTexture;
                        X, Y          : Single;
                        Width, Height : Single;
                        CutRect       : oePRect;
                        Alpha         : Byte;
                        Angle         : Single;
                        Flags         : Integer
                      ); stdcall;

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

procedure CSprite_Draw;
  var
    FU, FV : Single;
    cX, cY : Single;
    cW, cH : Single;
Begin

if Cam2D^.Active Then
  begin
    X := X - Cam2D^.X;
    Y := Y - Cam2D^.Y;
  end;

if Flags and FX_SCALE > 0 Then
  begin
    X      := X + ( Width  - Width  * FX_SX ) / 2;
    Y      := Y + ( Height - Height * FX_SY ) / 2;
    Width  := Width  * FX_SX;
    Height := Height * FX_SY;
  end;

if F_CropUnvisibleObj = 1 Then
  if Angle = 0 Then
    begin
      if not ( ( X + Width  >= CropX ) and ( X <= CropW ) and
               ( Y + Height >= CropY ) and ( Y <= CropH ) ) Then Exit;
    end else
      if not ( ( X + Width  + Height >= CropX ) and ( X - Width - Height <= CropW ) and
               ( Y + Height + Width  >= CropY ) and ( Y - Width - Height <= CropH ) ) Then Exit;

glPushMatrix;

if F_ResolutionCorrect = 1 Then
  glScalef( scr_ScaleX, scr_ScaleY, 1 );

if Flags and FX_BLEND = 0 Then
  glEnable( GL_ALPHA_TEST )
else
  glEnable( GL_BLEND );

if Flags and FX_COLORMIX = 0 Then
  glColor4ub( 255, 255, 255, alpha )
else
  glColor4ub( FX_R, FX_G, FX_B, alpha );

cX := ( 1 / ( Tex^.Width  / Tex^.U ) ) * CutRect.X;
cY := ( 1 / ( Tex^.Height / Tex^.V ) ) * ( Tex^.Height - CutRect.Y );
cW := cX + ( 1 / ( Tex^.Width  / Tex^.U ) ) * CutRect.Width;
cH := cY + ( 1 / ( Tex^.Height / Tex^.V ) ) * ( - CutRect.Height );

if Flags and FX_FLIPX > 0 Then FU := cW - cX else FU := 0;
if Flags and FX_FLIPY > 0 Then FV := cH - cY else FV := 0;


glTranslatef( X + Width / 2, Y + Height / 2, 0 );
glRotatef   ( Angle, 0, 0, 1 );
glTranslatef( -( X + Width / 2 ), -( Y + Height / 2 ), 0 );

glEnable( GL_TEXTURE_2D );

glBindTexture( GL_TEXTURE_2D, Tex^.ID );

if Tex^.Flags and TEX_CLAMP > 0 Then
  begin
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
  end;

glBegin( GL_QUADS );
if Flags and FX_VCHANGE = 0 Then
  begin
    if Flags and FX_VCA > 0 Then glColor4ub( FX_VR1, FX_VG1, FX_VB1, FX_VA1 );
    glTexCoord2f( cX + FU, cY + FV ); glVertex2f(         X,          Y );
    if Flags and FX_VCA > 0 Then glColor4ub( FX_VR2, FX_VG2, FX_VB2, FX_VA2 );
    glTexCoord2f( cW - FU, cY + FV ); glVertex2f( X + Width,          Y );
    if Flags and FX_VCA > 0 Then glColor4ub( FX_VR3, FX_VG3, FX_VB3, FX_VA3 );
    glTexCoord2f( cW - FU, cH - FV ); glVertex2f( X + Width, Y + Height );
    if Flags and FX_VCA > 0 Then glColor4ub( FX_VR4, FX_VG4, FX_VB4, FX_VA4 );
    glTexCoord2f( cX + FU, cH - FV ); glVertex2f(         X, Y + Height );
  end else
    begin
      if Flags and FX_VCA > 0 Then glColor4ub( FX_VR1, FX_VG1, FX_VB1, FX_VA1 );
      glTexCoord2f( cX + FU, cY + FV ); glVertex2f( X +         FX_VX1, Y          + FX_VY1 );
      if Flags and FX_VCA > 0 Then glColor4ub( FX_VR2, FX_VG2, FX_VB2, FX_VA2 );
      glTexCoord2f( cW - FU, cY + FV ); glVertex2f( X + Width + FX_VX2, Y          + FX_VY2 );
      if Flags and FX_VCA > 0 Then glColor4ub( FX_VR3, FX_VG3, FX_VB3, FX_VA3 );
      glTexCoord2f( cW - FU, cH - FV ); glVertex2f( X + Width + FX_VX3, Y + Height + FX_VY3 );
      if Flags and FX_VCA > 0 Then glColor4ub( FX_VR4, FX_VG4, FX_VB4, FX_VA4 );
      glTexCoord2f( cX + FU, cH - FV ); glVertex2f( X         + FX_VX4, Y + Height + FX_VY4 );
    end;
glEnd;

glDisable( GL_TEXTURE_2D );
glDisable( GL_BLEND );

glPopMatrix;

End;

end.
