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

{$I DEFINES.pas}

interface
uses
  Windows,
  OpenGL,

  ENGINE_VAR,
  ENGINE_CONST;

type
  PTexture = ^TTexture;
  TTexture = record
    ID              : Cardinal;
    Width, Height   : Integer;
    U, V            : Single;
    Flags           : Integer;//DWORD;
    FramesX         : WORD;
    FramesY         : WORD;
    Next            : PTexture;
end;


procedure Tex_Free( var Texture : PTexture ); stdcall;
procedure Tex_FreeAll; stdcall;

function Tex_Add( Info : TTexture ) : Pointer; stdcall;
function Tex_Del( ID : Cardinal ) : Boolean; stdcall;

var
  TexManager : PTexture;
  TexCount   : Integer;

//----------------------------------------------------------------------------//
                                 IMPLEMENTATION
//----------------------------------------------------------------------------//
uses
  ENGINE_LOG, TEXTURE_LOADER, ENGINE_UTILS, UTILS;

procedure Tex_Free;
Begin

if Texture = nil Then exit;
glDeleteTextures( 1, @Texture^.ID );

if Tex_Del( Texture^.ID ) Then
  Texture := nil;

if app_Log Then
  LOG_AddMessage( LOGT_APP, LOG_FREE_TEXTURE );

End;

procedure Tex_FreeAll;
  var
    old : PTexture;
Begin
TexCount := 0;
while TexManager <> nil do
  begin
    old := TexManager.Next;
    glDeleteTextures( 1, @TexManager^.ID );
    Mem_Free( TexManager );
    TexManager := old;
    INC( TexCount );
  end;
  Mem_Get( Pointer( TexManager ), SizeOf( TTexture ) );
  TexManager.ID     := 0;
  TexManager.Width  := 0;
  TexManager.Height := 0;
  TexManager.Flags  := 0;
  TexManager.Next   := nil;

if ( app_Log ) and ( TexCount > 0 ) Then
  LOG_AddMessage( LOGT_ENGINE, PChar( LOG_FREE_TEXTURES + '[' + U_IntToStr( TexCount ) + ']' ) );
End;

function Tex_Add;
  var
    new : PTexture;
begin

Result := nil;

new := TexManager;
while new^.Next <> nil do
  new := new^.Next;

Mem_Get ( Pointer( new^.Next ), SizeOf( TTexture ) );
PTexture( new^.Next )^       := Info;
PTexture( new^.Next )^.Next  := nil;

INC( TexCount );
Result := new.Next;

End;

function Tex_Del;
  var
    old : PTexture;
    new : PTexture;
Begin
Result := FALSE;

old := TexManager;
while old^.Next^.ID <> ID do
  old := old^.Next;

new := old^.Next^.Next;

Mem_Free( old^.Next );

old^.Next := new;

DEC( TexCount );
Result := TRUE;
End;

initialization
  Mem_Get( Pointer( TexManager ), SizeOf( TTexture ) );
  TexManager.ID     := 0;
  TexManager.Width  := 0;
  TexManager.Height := 0;
  TexManager.Flags  := 0;
  TexManager.Next   := nil;

end.
