1. with Ada.Unchecked_Deallocation; 
  2. with Allegro.Bitmaps;                   use Allegro.Bitmaps; 
  3. with Ada.Real_Time;                     use Ada.Real_Time; 
  4. with Ada.Streams;                       use Ada.Streams; 
  5. with Objects;                           use Objects; 
  6. with Physics;                           use Physics; 
  7.  
  8. private with Ada.Containers.Doubly_Linked_Lists; 
  9. private with Ada.Strings.Unbounded; 
  10. private with Archives; 
  11. private with Locking_Objects; 
  12. private with Resources; 
  13.  
  14. package Tiles is 
  15.  
  16.     -- Initializes the Tiles system, allowing tile libraries to be loaded. 
  17.     procedure Initialize; 
  18.  
  19.     -- Finalizes the Tiles system before application exit. Calling this without 
  20.     -- calling Initialize first will have no effect. 
  21.     procedure Finalize; 
  22.  
  23.     ---------------------------------------------------------------------------- 
  24.  
  25.     -- An array of tile ids 
  26.     type Tile_Id_Array is array (Integer range <>) of Natural; 
  27.     type A_Tile_Id_Array is access all Tile_Id_Array; 
  28.  
  29.     function A_Tile_Id_Array_Input( stream : access Root_Stream_Type'Class ) return A_Tile_Id_Array; 
  30.     for A_Tile_Id_Array'Input use A_Tile_Id_Array_Input; 
  31.  
  32.     procedure A_Tile_Id_Array_Output( stream : access Root_Stream_Type'Class; tia : A_Tile_Id_Array ); 
  33.     for A_Tile_Id_Array'Output use A_Tile_Id_Array_Output; 
  34.  
  35.     -- Returns a copy of 'src'. 
  36.     function Copy( src : A_Tile_Id_Array ) return A_Tile_Id_Array; 
  37.     pragma Postcondition( Copy'Result /= src or else src = null ); 
  38.  
  39.     -- Deletes the Tile_Id_Array. 
  40.     procedure Delete is new Ada.Unchecked_Deallocation( Tile_Id_Array, A_Tile_Id_Array ); 
  41.  
  42.     ---------------------------------------------------------------------------- 
  43.  
  44.     -- A Tile_Object represents a tile, the most basic element of a world map. 
  45.     -- It is uniquely identified within its library by an integer Id. Each tile 
  46.     -- is represented by a bitmap, can be animated using bitmaps from other 
  47.     -- tiles, and contains a set of attributes the define how entities interact 
  48.     -- with it in the world. 
  49.     type Tile_Object is abstract new Object with private; 
  50.     type A_Tile is access all Tile_Object'Class; 
  51.  
  52.     -- Creates a new tile using register allocator. Returns null if no allocator 
  53.     -- has been registered. 
  54.     function Create_Tile return A_Tile; 
  55.  
  56.     -- Returns the animation frame delay for the tile. 
  57.     function Get_Anm_Delay( this : not null access Tile_Object'Class ) return Time_Span; 
  58.  
  59.     -- Returns the id of the tile within its library. 
  60.     function Get_Id( this : not null access Tile_Object'Class ) return Natural; 
  61.  
  62.     -- Returns the name (bitmap filename) of the tile. Tile names are not unique 
  63.     -- within a library. 
  64.     function Get_Name( this : not null access Tile_Object'Class ) return String; 
  65.  
  66.     -- Returns the id of the next frame in the tile's animation if it's a frame 
  67.     -- in a single shot animation. 
  68.     function Get_Next_Frame( this : not null access Tile_Object'Class ) return Natural; 
  69.  
  70.     -- Returns a reference to the tile's looped animation frame list, or null if 
  71.     -- the tile does not have a looping animation. Do not delete the array, it 
  72.     -- belongs to the tile. 
  73.     function Get_Frame_List( this : not null access Tile_Object'Class ) return A_Tile_Id_Array; 
  74.  
  75.     -- Returns True if the file is animated. 
  76.     function Is_Animated( this : not null access Tile_Object'Class ) return Boolean; 
  77.  
  78.     function Object_Input( stream : access Root_Stream_Type'Class ) return Tile_Object is abstract; 
  79.  
  80.     procedure Object_Output( stream : access Root_Stream_Type'Class; obj : Tile_Object ) is abstract; 
  81.  
  82.     -- Sets an attribute on the tile by name. Only certain attribute names are 
  83.     -- supported and each has its own value constraints. False is returned in 
  84.     -- 'found' if the attribute name is not recognized. An exception is raised 
  85.     -- if the value is invalid or if there is a semantic error caused by setting 
  86.     -- the attribute. 
  87.     procedure Set_Attribute( this  : in out Tile_Object; 
  88.                              found : out Boolean; 
  89.                              name  : String; 
  90.                              val   : String := "" ); 
  91.  
  92.     -- Sets the id of the tile. Tile ids are unique within a library. Adding 
  93.     -- this tile to a library that already contains a tile with the same id will 
  94.     -- result in an error. 
  95.     procedure Set_Id( this : access Tile_Object; id : Natural ); 
  96.  
  97.     -- Sets the name (bitmap filename) of the tile. Tile names are not unique 
  98.     -- within a library. 
  99.     procedure Set_Name( this : access Tile_Object; name : String ); 
  100.     pragma Precondition( name'Length > 0 ); 
  101.  
  102.     -- Deletes the Tile. 
  103.     procedure Delete( this : in out A_Tile ); 
  104.     pragma Postcondition( this = null ); 
  105.  
  106.     ATTRIBUTE_ERROR : exception; 
  107.  
  108. private 
  109.  
  110.     use Ada.Strings.Unbounded; 
  111.     use Archives; 
  112.     use Locking_Objects; 
  113.     use Resources; 
  114.  
  115.     ---------------------------------------------------------------------------- 
  116.  
  117.     type Tile_Object is abstract new Object with 
  118.         record 
  119.             id        : Natural := 0; 
  120.             name      : Unbounded_String; 
  121.  
  122.             lock      : A_Locking_Object := null;    -- protects bmp, priority 
  123.             bmp       : A_Bitmap := null; 
  124.             priority  : Integer := 0; 
  125.  
  126.             loadState : A_Async_Operation := null; 
  127.             clipping  : Clip_Type := Passive; 
  128.             anmDelay  : Time_Span := Time_Span_Zero; -- animation frame delay in ms 
  129.             nextFrame : Natural := 0;                -- tile id of next frame in one-shot animation 
  130.             frames    : A_Tile_Id_Array := null;     -- tile id list in looping animation 
  131.         end record; 
  132.  
  133.     -- Raises COPY_NOT_ALLOWED. 
  134.     procedure Adjust( this : access Tile_Object ); 
  135.  
  136.     procedure Construct( this : access Tile_Object ); 
  137.  
  138.     procedure Delete( this : in out Tile_Object ); 
  139.  
  140.     -- Returns null if the tile's bitmap file isn't found or hasn't loaded yet. 
  141.     function Get_Bitmap( this : not null access Tile_Object'Class ) return A_Bitmap; 
  142.  
  143.     -- Returns the physics clipping type that the tile represents. 
  144.     function Get_Clipping( this : not null access Tile_Object'Class ) return Clip_Type; 
  145.  
  146.     -- Returns True if the tile's bitmap has been loaded, successfully or otherwise. 
  147.     function Is_Loaded( this : not null access Tile_Object'Class ) return Boolean; 
  148.  
  149.     -- Loads the tile's bitmap from an archive. 
  150.     procedure Load_Bitmap( this    : not null access Tile_Object'Class; 
  151.                            archive : not null A_Archive ); 
  152.  
  153.     -- Blocks the caller until the tile's bitmap has been loaded. 
  154.     procedure Wait_For_Load( this : not null access Tile_Object'Class ); 
  155.  
  156.     procedure Object_Read( stream : access Root_Stream_Type'Class; obj : out Tile_Object ); 
  157.     for Tile_Object'Read use Object_Read; 
  158.  
  159.     procedure Object_Write( stream : access Root_Stream_Type'Class; obj : Tile_Object ); 
  160.     for Tile_Object'Write use Object_Write; 
  161.  
  162.     ---------------------------------------------------------------------------- 
  163.  
  164.     function A_Tile_Input( stream : access Root_Stream_Type'Class ) return A_Tile; 
  165.     for A_Tile'Input use A_Tile_Input; 
  166.  
  167.     procedure A_Tile_Output( stream : access Root_Stream_Type'Class; tile : A_Tile ); 
  168.     for A_Tile'Output use A_Tile_Output; 
  169.  
  170.     procedure A_Tile_Read( stream : access Root_Stream_Type'Class; tile : out A_Tile ); 
  171.     for A_Tile'Read use A_Tile_Read; 
  172.  
  173.     procedure A_Tile_Write( stream : access Root_Stream_Type'Class; tile : A_Tile ); 
  174.     for A_Tile'Write use A_Tile_Write; 
  175.  
  176.     ---------------------------------------------------------------------------- 
  177.  
  178.     -- Defines a list of Tile objects. 
  179.     package Tile_Lists is new Ada.Containers.Doubly_Linked_Lists( A_Tile, "=" ); 
  180.     use Tile_Lists; 
  181.  
  182.     ---------------------------------------------------------------------------- 
  183.  
  184.     -- An allocator function for creating a new Tile. 
  185.     type Allocator is access function return A_Tile; 
  186.  
  187.     -- Returns a string that identifies the type of concrete Tile class. This 
  188.     -- is used in the file format identifier of tile index files, where Tile 
  189.     -- objects are written to disk. 
  190.     function Tile_Identifier return String; 
  191.     pragma Postcondition( Tile_Identifier'Result'Length > 0 ); 
  192.  
  193.     -- Returns the version number of the Tile subclass' stream representation. 
  194.     -- Returns 0 if a tile version has not been registered. 
  195.     -- See Register_Version. 
  196.     function Tile_Version return Natural; 
  197.  
  198.     -- Registers the allocator used to create new Tile instances. 
  199.     procedure Register_Allocator( allocate : not null Allocator ); 
  200.  
  201.     -- Sets the concrete Tile class identifier string returned by 
  202.     -- Tile_Identifier. 
  203.     procedure Register_Identifier( identifier : String ); 
  204.     pragma Precondition( identifier'Length > 0 ); 
  205.  
  206.     -- Sets the version number of the Tile subclass' stream representation. This 
  207.     -- should be incremented when a change is made to the streaming 
  208.     -- representation of the Tile subclass but the same class with the same 
  209.     -- tile identifier is still being used. This will prevent us from attempting 
  210.     -- to load an outdated Tile representation from a stream and crashing. 
  211.     procedure Register_Version( version : Positive ); 
  212.  
  213. end Tiles;