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 isNew = 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);
141 throw new Exception("Unexpected listentry: " +
148 public void ParseMoreTiles(Lisp.Parser parser)
152 List<int> ids = new List<int>();
153 List<int> attributes = new List<int>();
154 List<int> datas = new List<int>();
155 List<string> imageNames = new List<string>();
158 int d = parser.Depth;
159 while(parser.Parse() && parser.Depth >= d) {
160 if(parser.Depth == d+1) {
161 if(parser.Type != Parser.LispType.SYMBOL)
162 throw new Exception("expected SYMBOL at supertux-tiles---tiles level, but got \"" + parser.StringValue + "\"");
163 string symbol = parser.SymbolValue;
167 blockWidth = parser.IntegerValue;
170 blockHeight = parser.IntegerValue;
173 Parser.ParseIntList(parser, ids);
176 Parser.ParseIntList(parser, attributes);
179 Parser.ParseIntList(parser, datas);
182 animFps = parser.FloatValue;
185 int subDepth = parser.Depth;
186 while(parser.Depth >= subDepth) {
187 imageNames.Add(parser.StringValue);
192 Console.WriteLine("Unknown tiles element " + symbol);
197 if(ids.Count != blockWidth * blockHeight)
198 throw new ApplicationException("Must have width*height ids in tiles block, but found " + ids.Count.ToString());
199 if((attributes.Count != blockWidth * blockHeight) && attributes.Count > 0) //missing atributes == all-are-0-attributes
200 throw new ApplicationException("Must have width*height attributes in tiles block");
201 if((datas.Count != blockWidth * blockHeight) && datas.Count > 0) //missing DATAs == all-are-0-DATAs
202 throw new ApplicationException("Must have width*height DATAs in tiles block");
205 for(int y = 0; y < blockHeight; ++y) {
206 for(int x = 0; x < blockWidth; ++x) {
208 Tile tile = new Tile();
210 tile.Images = new ArrayList();
211 foreach (string str in imageNames)
213 ImageRegion region = new ImageRegion();
214 region.ImageFile = str;
215 region.Region.X = x * TILE_WIDTH;
216 region.Region.Y = y * TILE_HEIGHT;
217 region.Region.Width = TILE_WIDTH;
218 region.Region.Height = TILE_HEIGHT;
219 tile.Images.Add(region);
222 tile.Attributes = (attributes.Count > 0)?attributes[id]:0; //missing atributes == all-are-0-attributes
223 tile.Data = (datas.Count > 0)?datas[id]:0; //missing DATAs == all-are-0-DATAs
224 tile.AnimFps = animFps;
226 while(Tiles.Count <= tile.ID)
229 Tiles[tile.ID] = tile;
237 private void SkipList(Lisp.Parser parser)
239 int d = parser.Depth;
240 while(parser.Parse() && parser.Depth >= d)