TileRepository.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // $Id: foo.cpp 2979 2006-01-10 00:00:04Z sommer $
  2. //
  3. // Cobble - A simple SuperTux level editor
  4. // Copyright (C) 2006 Christoph Sommer <supertux@2006.expires.deltadevelopment.de>
  5. //
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU General Public License
  8. // as published by the Free Software Foundation; either version 2
  9. // of the License, or (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. // 02111-1307, USA.
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Text;
  23. using System.Drawing;
  24. using System.IO;
  25. using System.Collections;
  26. namespace Cobble {
  27. public class TileRepository {
  28. public Dictionary<int, bool> isSolid = new Dictionary<int, bool>();
  29. private Dictionary<int, Image> tileRepository = new Dictionary<int, Image>();
  30. private TileSet tileset;
  31. public TileRepository(string filename) {
  32. isSolid[0] = false;
  33. LoadingScreenForm loadingScreen = new LoadingScreenForm();
  34. loadingScreen.setSubject("tileset");
  35. loadingScreen.setMax(100);
  36. loadingScreen.Show();
  37. loadingScreen.Refresh();
  38. if (!File.Exists(filename)) throw new ArgumentException("Tile file not found");
  39. String basePath = System.IO.Path.GetDirectoryName(filename);
  40. tileset = new TileSet();
  41. tileset.Parse(filename);
  42. // append a TileGroup (misc) with all tiles that are not yet in a TileGroup
  43. TileGroup miscTiles = new TileGroup();
  44. foreach (Tile tile in tileset.Tiles) {
  45. if (tile == null) continue;
  46. bool found = false;
  47. foreach (TileGroup tileGroup in tileset.TileGroups) {
  48. if (tileGroup.Tiles.Contains(tile.ID)) found = true;
  49. }
  50. if (found) continue;
  51. miscTiles.Name = "(misc)";
  52. miscTiles.Tiles.Add(tile.ID);
  53. }
  54. tileset.TileGroups.Add(miscTiles);
  55. loadingScreen.setMax(tileset.Tiles.Count);
  56. int progress = 0;
  57. foreach (Tile tile in tileset.Tiles) {
  58. progress++;
  59. if (tile == null) continue;
  60. addTile(tile, basePath);
  61. loadingScreen.setProgress(progress);
  62. }
  63. loadingScreen.Close();
  64. }
  65. public ArrayList getTileGroups() {
  66. return tileset.TileGroups;
  67. }
  68. public Image getTile(int id) {
  69. if (!tileRepository.ContainsKey(id)) return null;
  70. return tileRepository[id];
  71. }
  72. private void addTile(Tile tile, string basePath) {
  73. if (tile.ID == 0) return;
  74. Rectangle srcRect = new Rectangle(0, 0, 32, 32);
  75. Bitmap srcImg = null;
  76. if (tile.EditorImage != null) {
  77. string fname = tile.EditorImage;
  78. fname = System.IO.Path.Combine(basePath, fname.Replace('/', System.IO.Path.DirectorySeparatorChar));
  79. srcImg = new Bitmap(fname);
  80. } else if ((tile.Images != null) && (tile.Images.Count >= 1)) {
  81. ImageRegion ir = (ImageRegion)tile.Images[0];
  82. string fname = ir.ImageFile;
  83. fname = System.IO.Path.Combine(basePath, fname.Replace('/', System.IO.Path.DirectorySeparatorChar));
  84. srcImg = new Bitmap(fname);
  85. if (ir.Region != Rectangle.Empty) {
  86. srcRect = ir.Region;
  87. } else if (ir.RelativeRegion != RectangleF.Empty) {
  88. int x = (int)(ir.RelativeRegion.X * srcImg.Width);
  89. int y = (int)(ir.RelativeRegion.Y * srcImg.Height);
  90. int w = (int)(ir.RelativeRegion.Width * srcImg.Width);
  91. int h = (int)(ir.RelativeRegion.Height * srcImg.Height);
  92. srcRect = new Rectangle(x, y, w, h);
  93. }
  94. } else throw new ArgumentException("No usable images found for tile #"+tile.ID);
  95. Bitmap dstImg = new Bitmap(32, 32);
  96. Graphics gr = Graphics.FromImage(dstImg);
  97. gr.DrawImage(srcImg, new Rectangle(0, 0, 32, 32), srcRect, GraphicsUnit.Pixel);
  98. tileRepository[tile.ID] = dstImg;
  99. isSolid[tile.ID] = tile.Solid;
  100. }
  101. }
  102. }