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

{$I DEFINES.pas}

interface
uses
  Windows,
  OpenGL,

  ENGINE_VAR,
  ENGINE_GL,
  ENGINE_UTILS,

  G2D_SSPRITE,

  TEXTURE_LOADER,
  TEXTURE_MANAGER,

  MATH_FAST;

procedure Tex_BeginRenderIn( Texture : PTexture; SaveOld, RealSize, Clear : Boolean ); stdcall;
procedure Tex_EndRenderIn; stdcall;

procedure Scissor_Begin( X, Y, Width, Height : Integer ); stdcall;
procedure Scissor_End; stdcall;

var
  tCropX, tCropY : Integer;
  tCropW, tCropH : Integer;

//----------------------------------------------------------------------------//
                                 IMPLEMENTATION
//----------------------------------------------------------------------------//
var
  Tex   : Cardinal;
  TexID : PTexture;

  DWidth, DHeight : Integer;

procedure Tex_BeginRenderIn;
begin
DWidth  := M_Round( Texture^.Width / Texture^.U );       
DHeight := M_Round( Texture^.Height / Texture^.V );

Tex   := Texture^.ID;
TexID := Texture;

glLoadIdentity; 
glPushMatrix;
glViewport( 0, 0, DWidth, DHeight );

if Clear Then
  glClear( GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT );

if RealSize Then
  begin
    glScalef( ogl_Width / DWidth / scr_ScaleX, ogl_Height / DHeight / scr_ScaleY, 1 );
    if SaveOld Then SSprite_Draw( Texture, 0, 0, DWidth, DHeight, 255, 0, 1 );
  end else
    if SaveOld Then SSprite_Draw( Texture, 0, 0, ogl_Width, ogl_Height, 255, 0, 1 );
end;

procedure Tex_EndRenderIn;
begin
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, Tex );

{if TexID^.Flags and TEX_MIPMAP > 0 Then
  Tex_Filter( TexID^.Flags xor TEX_MIPMAP )
else
  Tex_Filter( TexID^.Flags );
glCopyTexImage2D( GL_TEXTURE_2D, 0,
                  GL_RGB   * Byte( scr_BPP = 32 ) or
                  GL_RGB8  * Byte( scr_BPP = 8  ) or
                  GL_RGB16 * Byte( scr_BPP = 16 ),
                  0, 0, DWidth, DHeight, 0 );}
glCopyTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 0, 0, TexID.Width, TexID.Height );
  
glViewport( 0, 0, ogl_Width, ogl_Height );
GL_Set2DMatrix;

glPopMatrix;

glClear( GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT );
glLoadIdentity;
glDisable( GL_TEXTURE_2D );
end;

procedure Scissor_Begin;
Begin
X := round( X * scr_ScaleX );
Y := round( Y * scr_ScaleY );
Width := round( Width * scr_ScaleX );
Height := round( Height * scr_ScaleY );

glEnable( GL_SCISSOR_TEST );
glScissor( X, ogl_Height - Y - Height, Width, Height );

Width := Width + X;
Height := Height + Y;

tCropX := CropX;
tCropY := CropY;
tCropW := CropW;
tCropH := CropH;

CropX := round( X / scr_ScaleX );
CropY := round( Y / scr_ScaleY );
CropW := round( Width / scr_ScaleX );
CropH := round( Height / scr_ScaleY );
End;

procedure Scissor_End;
Begin
glDisable( GL_SCISSOR_TEST );
CropX := tCropX;
CropY := tCropY;
CropW := tCropW;
CropH := tCropH;
End;

end.
