3 using System.Collections;
4 using System.Collections.Generic;
7 public class TileGroup {
9 public ArrayList Tiles = new ArrayList();
11 public void Write(LispWriter writer) {
12 writer.StartList("tilegroup");
14 writer.Write("name", Name);
15 writer.Write("tiles", Tiles);
17 writer.EndList("tilegroup");
20 public void Parse(Lisp.Parser parser) {
22 while(parser.Parse() && parser.Depth >= d) {
23 if(parser.Depth == d+1) {
24 if(parser.Type != Parser.LispType.SYMBOL)
25 throw new Exception("expected SYMBOL at supertux-tiles level, but got \"" + parser.StringValue + "\"");
26 string symbol = parser.SymbolValue;
30 Name = parser.StringValue;
34 Tiles.Add(parser.IntegerValue);
35 } while(parser.Parse()
36 && parser.Type == Parser.LispType.INTEGER);
39 Console.WriteLine("Unknown section " + symbol);
47 public class TileSet {
48 public const int TILE_WIDTH = 32;
49 public const int TILE_HEIGHT = 32;
51 private bool tooNew = false;
53 /// <summary>Whether version of tileset file is too new</summary>
60 public ArrayList Tiles = new ArrayList();
61 public ArrayList TileGroups = new ArrayList();
63 public void Write(string filename) {
64 FileStream fs = new FileStream(filename, FileMode.Create);
66 TextWriter tw = new StreamWriter(fs);
67 LispWriter writer = new LispWriter(tw);
69 writer.WriteComment("Generated by tiler");
70 writer.StartList("supertux-tiles");
71 foreach(TileGroup tilegroup in TileGroups) {
72 tilegroup.Write(writer);
74 foreach(Tile tile in Tiles) {
80 writer.EndList("supertux-tiles");
85 public void Parse(string filename) {
86 FileStream fs = new FileStream(filename, FileMode.Open);
87 StreamReader stream = new StreamReader(fs);
89 Lisp.Parser parser = new Lisp.Parser(stream);
91 if(parser.Type != Parser.LispType.START_LIST)
92 throw new Exception("Expected START_LIST");
94 if(parser.Type != Parser.LispType.SYMBOL)
95 throw new Exception("Expected symbol");
96 if(parser.SymbolValue != "supertux-tiles")
97 throw new Exception("not a supertux tile files but " +
105 public void ParseTiles(Lisp.Parser parser) {
107 int d = parser.Depth;
108 while(parser.Parse() && parser.Depth >= d) {
109 if(parser.Depth == d && parser.Type != Parser.LispType.START_LIST) {
110 Console.WriteLine("non-cons type in list...");
114 if(parser.Depth == d+1) {
115 if(parser.Type != Parser.LispType.SYMBOL) {
116 throw new Exception("Expected symbol in list element");
118 switch(parser.SymbolValue) {
123 TileGroup tilegroup = new TileGroup();
124 tilegroup.Parse(parser);
125 TileGroups.Add(tilegroup);
128 Tile tile = new Tile();
131 while(tile.ID >= Tiles.Count)
133 Tiles[tile.ID] = tile;
136 ParseMoreTiles(parser);
139 "Warning: new syntax of \"More tiles in one image\" file isn't currently supported");
141 "And this means: YOU WON'T BE ABLE TO SAVE YOUR CHANGES !!!");
144 throw new Exception("Unexpected listentry: " +
151 public void ParseMoreTiles(Lisp.Parser parser)
155 List<int> ids = new List<int>();
156 List<int> attributes = new List<int>();
157 List<int> datas = new List<int>();
158 List<string> imageNames = new List<string>();
161 int d = parser.Depth;
162 while(parser.Parse() && parser.Depth >= d) {
163 if(parser.Depth == d+1) {
164 if(parser.Type != Parser.LispType.SYMBOL)
165 throw new Exception("expected SYMBOL at supertux-tiles---tiles level, but got \"" + parser.StringValue + "\"");
166 string symbol = parser.SymbolValue;
170 blockWidth = parser.IntegerValue;
173 blockHeight = parser.IntegerValue;
176 Parser.ParseIntList(parser, ids);
179 Parser.ParseIntList(parser, attributes);
182 Parser.ParseIntList(parser, datas);
185 animFps = parser.FloatValue;
188 int subDepth = parser.Depth;
189 while(parser.Depth >= subDepth) {
190 imageNames.Add(parser.StringValue);
195 Console.WriteLine("Unknown tiles element " + symbol);
200 if(ids.Count != blockWidth * blockHeight)
201 throw new ApplicationException("Must have width*height ids in tiles block, but found " + ids.Count.ToString());
202 if((attributes.Count != blockWidth * blockHeight) && attributes.Count > 0) //missing atributes == all-are-0-attributes
203 throw new ApplicationException("Must have width*height attributes in tiles block");
204 if((datas.Count != blockWidth * blockHeight) && datas.Count > 0) //missing DATAs == all-are-0-DATAs
205 throw new ApplicationException("Must have width*height DATAs in tiles block");
208 for(int y = 0; y < blockHeight; ++y) {
209 for(int x = 0; x < blockWidth; ++x) {
211 Tile tile = new Tile();
213 tile.Images = new ArrayList();
214 foreach (string str in imageNames)
216 ImageRegion region = new ImageRegion();
217 region.ImageFile = str;
218 region.Region.X = x * TILE_WIDTH;
219 region.Region.Y = y * TILE_HEIGHT;
220 region.Region.Width = TILE_WIDTH;
221 region.Region.Height = TILE_HEIGHT;
222 tile.Images.Add(region);
225 tile.Attributes = (attributes.Count > 0)?attributes[id]:0; //missing atributes == all-are-0-attributes
226 tile.Data = (datas.Count > 0)?datas[id]:0; //missing DATAs == all-are-0-DATAs
227 tile.AnimFps = animFps;
229 while(Tiles.Count <= tile.ID)
232 Tiles[tile.ID] = tile;
240 private void SkipList(Lisp.Parser parser)
242 int d = parser.Depth;
243 while(parser.Parse() && parser.Depth >= d)