tiled_code_gen_0.1.0_c4ef09ee/src/tiled_code_gen.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
------------------------------------------------------------------------------
--                                                                          --
--                             tiled-code-gen                               --
--                                                                          --
--                    Copyright (C) 2018 Fabien Chouteau                    --
--                                                                          --
--                                                                          --
--  Redistribution and use in source and binary forms, with or without      --
--  modification, are permitted provided that the following conditions are  --
--  met:                                                                    --
--     1. Redistributions of source code must retain the above copyright    --
--        notice, this list of conditions and the following disclaimer.     --
--     2. Redistributions in binary form must reproduce the above copyright --
--        notice, this list of conditions and the following disclaimer in   --
--        the documentation and/or other materials provided with the        --
--        distribution.                                                     --
--     3. Neither the name of the copyright holder nor the names of its     --
--        contributors may be used to endorse or promote products derived   --
--        from this software without specific prior written permission.     --
--                                                                          --
--   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS    --
--   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT      --
--   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR  --
--   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT   --
--   HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --
--   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT       --
--   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  --
--   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  --
--   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT    --
--   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE  --
--   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.   --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Command_Line;
with Ada.Text_IO;
with Ada.Directories;  use Ada.Directories;

with TCG.Palette;
with TCG.Maps;
with TCG.Maps.Render;
with TCG.Maps.List;
with TCG.Outputs.PDF;
with TCG.Outputs.GESTE;
with TCG.Outputs.LibGBA;
with TCG.Outputs.RSTE;
with TCG.Tilesets;
use TCG;

with GNAT.Command_Line;         use GNAT.Command_Line;
with GNAT.Strings;              use GNAT.Strings;
with GNATCOLL.Utils;            use GNATCOLL.Utils;

procedure Tiled_Code_Gen is

   List : Maps.List.List;

   Config           : Command_Line_Configuration;
   PDF_Enabled      : aliased Boolean;
   GESTE_Enabled    : aliased Boolean;
   RSTE_Enabled     : aliased Boolean;
   GBA_Enabled      : aliased Boolean;
   BMP_Enabled      : aliased Boolean;
   Source_Out_Dir   : aliased String_Access := new String'("src");
   Doc_Out_Dir      : aliased String_Access := new String'("doc");
   Color_Format_Str : aliased String_Access := new String'("RGB565");
   Color_Format     : Palette.Output_Color_Format;
   Root_Package     : aliased String_Access := new String'("Game_Assets");

   Tileset          : Tilesets.Tileset_Id := Tilesets.Invalid_Tileset;
   use type Tilesets.Tileset_Id;
begin

   declare
   begin
      Define_Switch
        (Config, PDF_Enabled'Access, "-p",
         Long_Switch => "--pdf",
         Help => "Generate PDF documentation");

      Define_Switch
        (Config, GESTE_Enabled'Access, "-g",
         Long_Switch => "--geste",
         Help => "Generate code for GEneric Sprite and Tile Engine");

      Define_Switch
        (Config, RSTE_Enabled'Access, "",
         Long_Switch => "--rste",
         Help => "Generate code for Rust Sprite and Tile Engine");

      Define_Switch
        (Config, GBA_Enabled'Access, "",
         Long_Switch => "--libgba",
         Help => "Generate code for Rust Sprite and Tile Engine");

      Define_Switch
        (Config, BMP_Enabled'Access, "-b",
         Long_Switch => "--bmp",
         Help => "Generate bitmap images of the maps");

      Define_Switch
        (Config, Root_Package'Access, "-r:",
         Long_Switch => "--root-package-name=",
         Help => "Name of the root package of generated sources (default: " &
           Root_Package.all & ")");

      Define_Switch
        (Config, Source_Out_Dir'Access, "-o:",
         Long_Switch => "--source-out-dir=",
         Help => "Output directory for the generated sources (default: " &
           Source_Out_Dir .all & ")");

      Define_Switch
        (Config, Doc_Out_Dir'Access, "-d:",
         Long_Switch => "--doc-out-dir=",
         Help => "Output directory for the generated documentation " &
           "(default: " & Doc_Out_Dir.all & ")");

      Define_Switch
        (Config, Color_Format_Str'Access, "-f:",
         Long_Switch => "--color-format=",
         Help => "Color format used in generated sources (" &
           Palette.Supported_Formats & ") (default: " &
           Color_Format_Str.all & ")");

      Set_Usage
        (Config,
         "[switches] maps (.tmx) or tilesets (.tsx)",
         "Tiled-Code-Gen, a code generator for Tiled the map editor");

      Getopt (Config);
   exception
      when GNAT.Command_Line.Invalid_Switch =>
         Ada.Command_Line.Set_Exit_Status (1);
         return;
      when GNAT.Command_Line.Exit_From_Command_Line =>
         return;
   end;

   --  Checking output color format
   if Palette.Format_Supported (Color_Format_Str.all) then
      Color_Format := Palette.Convert (Color_Format_Str.all);
   else
      Ada.Text_IO.Put_Line
        (Ada.Text_IO.Standard_Error,
         "Unsuported color format: " & Color_Format_Str.all);
      Ada.Command_Line.Set_Exit_Status (1);
      return;
   end if;

   --  Processing each Tiled maps
   loop
      declare
         Arg    : constant String := Get_Argument (Do_Expansion => True);
      begin
         exit when Arg'Length = 0;
         if Ends_With (Arg, ".tmx") then
            List.Append (TCG.Maps.Load (Arg, Base_Name (Arg)));
         elsif Ends_With (Arg, ".tsx") then
            Tileset := Tilesets.Load (Arg);
            Tilesets.Fill_Master_Tileset (Tileset);
         else
            Ada.Text_IO.Put_Line
              (Ada.Text_IO.Standard_Error,
               "Unknown file format: '" & Arg & "'");
         end if;
      end;
   end loop;

   if List.Is_Empty and then Tileset = Tilesets.Invalid_Tileset then
      Display_Help (Config);
      return;
   end if;

   for Map of List loop
      TCG.Maps.Fill_Master_Tileset (Map);
   end loop;

   if GESTE_Enabled then
      Outputs.GESTE.Gen_GESTE_Source (Directory         => Source_Out_Dir.all,
                                      Root_Package_Name => Root_Package.all,
                                      Format            => Color_Format,
                                      Map_List          => List);
   end if;

   if GBA_Enabled then
      Outputs.LibGBA.Gen_LibGBA_Source
        (Directory         => Source_Out_Dir.all,
         Root_Package_Name => Root_Package.all,
         Map_List          => List);
   end if;

   if RSTE_Enabled then
      Outputs.RSTE.Gen_RSTE_Source (Directory        => Source_Out_Dir.all,
                                    Root_Module_Name => Root_Package.all,
                                    Format           => Color_Format,
                                    Map_List         => List);
   end if;

   if PDF_Enabled then
      Outputs.PDF.Gen_PDF_Doc (Doc_Out_Dir.all, "doc.pdf", List);
   end if;

   if BMP_Enabled then
      for M of List loop
         TCG.Maps.Render.To_BMP
           (M,
            Path       => Compose (Doc_Out_Dir.all, Maps.Name (M) & ".bmp"),
            Background => (255, 255, 255, 255));
      end loop;
   end if;
   if not GESTE_Enabled
     and then
      not GBA_Enabled
     and then
      not PDF_Enabled
     and then
      not BMP_Enabled
   then
      Ada.Text_IO.Put_Line
        (Ada.Text_IO.Standard_Error, "Warning: no generator enabled.");
      Ada.Text_IO.Put_Line
        (Ada.Text_IO.Standard_Error, "Use --help to see the list of options.");
   end if;

end Tiled_Code_Gen;