4 using System.Collections;
5 using System.Collections.Generic;
8 public class TileGroup {
10 public ArrayList Tiles = new ArrayList();
12 public void Write(LispWriter writer) {
13 writer.StartList("tilegroup");
15 writer.Write("name", Name);
16 writer.Write("tiles", Tiles);
18 writer.EndList("tilegroup");
21 public void Parse(Lisp.Parser parser) {
23 while(parser.Parse() && parser.Depth >= d) {
24 if(parser.Depth == d+1) {
25 if(parser.Type != Parser.LispType.SYMBOL)
26 throw new Exception("expected SYMBOL at supertux-tiles level, but got \"" + parser.StringValue + "\"");
27 string symbol = parser.SymbolValue;
31 Name = parser.StringValue;
35 Tiles.Add(parser.IntegerValue);
36 } while(parser.Parse()
37 && parser.Type == Parser.LispType.INTEGER);
40 Console.WriteLine("Unknown section " + symbol);
48 public class TileSet {
49 public const int TILE_WIDTH = 32;
50 public const int TILE_HEIGHT = 32;
52 private bool tooNew = false;
54 /// <summary>Whether version of tileset file is too new</summary>
61 public ArrayList Tiles = new ArrayList();
62 public ArrayList TileGroups = new ArrayList();
64 public void Write(string filename) {
65 FileStream fs = new FileStream(filename, FileMode.Create);
67 TextWriter tw = new StreamWriter(fs);
68 LispWriter writer = new LispWriter(tw);
70 writer.WriteComment("Generated by tiler");
71 writer.StartList("supertux-tiles");
72 foreach(TileGroup tilegroup in TileGroups) {
73 tilegroup.Write(writer);
75 foreach(Tile tile in Tiles) {
81 writer.EndList("supertux-tiles");
86 public void Parse(string filename) {
87 FileStream fs = new FileStream(filename, FileMode.Open);
88 StreamReader stream = new StreamReader(fs);
90 Lisp.Parser parser = new Lisp.Parser(stream);
92 if(parser.Type != Parser.LispType.START_LIST)
93 throw new Exception("Expected START_LIST");
95 if(parser.Type != Parser.LispType.SYMBOL)
96 throw new Exception("Expected symbol");
97 if(parser.SymbolValue != "supertux-tiles")
98 throw new Exception("not a supertux tile files but " +
106 public void ParseTiles(Lisp.Parser parser) {
108 int d = parser.Depth;
109 while(parser.Parse() && parser.Depth >= d) {
110 if(parser.Depth == d && parser.Type != Parser.LispType.START_LIST) {
111 Console.WriteLine("non-cons type in list...");
115 if(parser.Depth == d+1) {
116 if(parser.Type != Parser.LispType.SYMBOL) {
117 throw new Exception("Expected symbol in list element");
119 switch(parser.SymbolValue) {
124 TileGroup tilegroup = new TileGroup();
125 tilegroup.Parse(parser);
126 TileGroups.Add(tilegroup);
129 Tile tile = new Tile();
132 while(tile.ID >= Tiles.Count)
134 Tiles[tile.ID] = tile;
137 ParseMoreTiles(parser);
140 "Warning: new syntax of \"More tiles in one image\" file isn't currently supported");
142 "And this means: YOU WON'T BE ABLE TO SAVE YOUR CHANGES !!!");
145 throw new Exception("Unexpected listentry: " +
152 public void ParseMoreTiles(Lisp.Parser parser)
156 List<int> ids = new List<int>();
157 List<int> attributes = new List<int>();
158 List<int> datas = new List<int>();
159 List<string> imageNames = new List<string>();
162 int d = parser.Depth;
163 while(parser.Parse() && parser.Depth >= d) {
164 if(parser.Depth == d+1) {
165 if(parser.Type != Parser.LispType.SYMBOL)
166 throw new Exception("expected SYMBOL at supertux-tiles---tiles level, but got \"" + parser.StringValue + "\"");
167 string symbol = parser.SymbolValue;
171 blockWidth = parser.IntegerValue;
174 blockHeight = parser.IntegerValue;
177 Parser.ParseIntList(parser, ids);
180 Parser.ParseIntList(parser, attributes);
183 Parser.ParseIntList(parser, datas);
186 animFps = parser.FloatValue;
189 int subDepth = parser.Depth;
190 while(parser.Depth >= subDepth) {
191 imageNames.Add(parser.StringValue);
196 Console.WriteLine("Unknown tiles element " + symbol);
201 if(ids.Count != blockWidth * blockHeight)
202 throw new ApplicationException("Must have width*height ids in tiles block, but found " + ids.Count.ToString());
203 if((attributes.Count != blockWidth * blockHeight) && attributes.Count > 0) //missing atributes == all-are-0-attributes
204 throw new ApplicationException("Must have width*height attributes in tiles block");
205 if((datas.Count != blockWidth * blockHeight) && datas.Count > 0) //missing DATAs == all-are-0-DATAs
206 throw new ApplicationException("Must have width*height DATAs in tiles block");
209 for(int y = 0; y < blockHeight; ++y) {
210 for(int x = 0; x < blockWidth; ++x) {
212 Tile tile = new Tile();
214 tile.Images = new ArrayList();
215 foreach (string str in imageNames)
217 ImageRegion region = new ImageRegion();
218 region.ImageFile = str;
219 region.Region.X = x * TILE_WIDTH;
220 region.Region.Y = y * TILE_HEIGHT;
221 region.Region.Width = TILE_WIDTH;
222 region.Region.Height = TILE_HEIGHT;
223 tile.Images.Add(region);
226 tile.Attributes = (attributes.Count > 0)?attributes[id]:0; //missing atributes == all-are-0-attributes
227 tile.Data = (datas.Count > 0)?datas[id]:0; //missing DATAs == all-are-0-DATAs
228 tile.AnimFps = animFps;
230 while(Tiles.Count <= tile.ID)
233 Tiles[tile.ID] = tile;
241 private void SkipList(Lisp.Parser parser)
243 int d = parser.Depth;
244 while(parser.Parse() && parser.Depth >= d)