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

{$I DEFINES.pas}

interface

type
  TCamera2D = record
    X, Y   : Single;
    Active : Boolean;
    Next   : Pointer;
end;
PCamera2D = ^TCamera2D;

function Cam2D_Create : PCamera2D; stdcall;
procedure Cam2D_Free( var YourCamera : PCamera2D ); stdcall;
procedure Cam2D_FreeAll; stdcall;

procedure Cam2D_Set( YourCamera : PCamera2D ); stdcall;

var
  // Текущая камера
  Cam2D      : PCamera2D;

  CamManager : PCamera2D;

//----------------------------------------------------------------------------//
                                 IMPLEMENTATION
//----------------------------------------------------------------------------//
uses
  ENGINE_VAR, ENGINE_CONST, ENGINE_LOG, ENGINE_UTILS, UTILS;

function Cam2D_Create;
  var
    new : PCamera2D;
begin

Result := nil;

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

Mem_Get  ( new^.Next, SizeOf( TCamera2D ) );
PCamera2D( new^.Next )^.X      := 0;
PCamera2D( new^.Next )^.Y      := 0;
PCamera2D( new^.Next )^.Active := TRUE;
PCamera2D( new^.Next )^.Next   := nil;

Result := new.Next;

End;

procedure Cam2D_Free;
  var
    old : PCamera2D;
Begin

if Cam2D = YourCamera Then
  Cam2D := CamManager;

old := CamManager;
while old^.Next <> YourCamera do
  old := old^.Next;

old^.Next := PCamera2D( old^.Next )^.Next;

Mem_Free( old^.Next );

YourCamera := nil;

End;

procedure Cam2D_Set( YourCamera : PCamera2D );
Begin

if Assigned( YourCamera ) Then
  Cam2D := YourCamera
else
  Cam2D := CamManager;

End;

procedure Cam2D_FreeAll;
  var
    oldCam   : PCamera2D;
    CamCount : Integer;
Begin
CamCount := 0;
oldCam := CamManager^.Next;
if oldCam = nil Then
  Mem_Free( CamManager );
while oldCam <> nil do
  begin
    CamManager := oldCam;
    oldCam     := oldCam^.Next;
    Mem_Free( CamManager );
    INC( CamCount );
  end;
if ( app_Log ) and ( CamCount > 0 ) Then
  LOG_AddMessage( LOGT_ENGINE, PChar( LOG_FREE_CAMERAS + '[' + U_IntToStr( CamCount ) + ']' ) );
End;

initialization
  Mem_Get( Pointer( CamManager ), SizeOf( TCamera2D ) );
  CamManager^.X      := 0;
  CamManager^.Y      := 0;
  CamManager^.Active := FALSE;
  CamManager^.Next   := nil;
  Cam2D              := CamManager;

end.
