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

{$I DEFINES.pas}

interface
uses
  OpenGL,

  ENGINE_VAR,
  ENGINE_SCREEN,

  ENGINE_FLAGS,

  G2D_CAMERA,

  G2D_FX,

  TEXTURE_MANAGER,
  TEXTURE_LOADER,

  MATH_FAST;

procedure STile_Draw(
                      Tex            : PTexture;
                      X, Y           : Single;
                      Width, Height  : Single;
                      TilesX, TilesY : Integer;
                      Alpha          : Byte;
                      Flags          : Integer
                    ); stdcall;

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

procedure STile_Draw;
  var
    i, j           : Integer;
    aI, aJ, bI, bJ : Integer;
    tX, tY         : Single;
    FU, FV         : Single;
Begin

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

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 );

if Flags and FX_FLIPX > 0 Then FU := Tex^.U else FU := 0;
if Flags and FX_FLIPY > 0 Then FV := Tex^.V else FV := 0;

glEnable( GL_TEXTURE_2D );

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;

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;

DEC( TilesX );
DEC( TilesY );

aI := 1 - M_Round( X ) div M_Round( Width );
if aI < 1 Then aI := 1;

bI := CropW div M_Round( Width ) + aI;
if bI > TilesX Then bI := TilesX;

aJ := 1 - M_Round( Y ) div M_Round( Height );
if aJ < 1 Then aJ := 1;

bJ := CropH div M_Round( Height ) + aJ;
if bJ > TilesY Then bJ := TilesY;

tX := X;
tY := Y;

glBegin( GL_QUADS );
for i := aI - 1 to bI do
  for j := aJ - 1 to bJ do
    begin
      X := Tx + i * Width;
      Y := Ty + j * Height;

      if Flags and FX_VCA > 0 Then glColor4ub( FX_VR1, FX_VG1, FX_VB1, FX_VA1 );
      glTexCoord2f(          FU, Tex^.V - FV ); glVertex2f(         X,          Y );
      if Flags and FX_VCA > 0 Then glColor4ub( FX_VR2, FX_VG2, FX_VB2, FX_VA2 );
      glTexCoord2f( Tex^.U - FU, Tex^.V - FV ); glVertex2f( X + Width,          Y );
      if Flags and FX_VCA > 0 Then glColor4ub( FX_VR3, FX_VG3, FX_VB3, FX_VA3 );
      glTexCoord2f( Tex^.U - FU,          FV ); glVertex2f( X + Width, Y + Height );
      if Flags and FX_VCA > 0 Then glColor4ub( FX_VR4, FX_VG4, FX_VB4, FX_VA4 );
      glTexCoord2f(          FU,          FV ); glVertex2f(         X, Y + Height );
    end;
glEnd;

glDisable( GL_TEXTURE_2D );
glDisable( GL_BLEND );

glPopMatrix;

End;

end.
