This repository was archived by the owner on May 10, 2018. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathTileRepository.cs
More file actions
executable file
·124 lines (102 loc) · 4.69 KB
/
Copy pathTileRepository.cs
File metadata and controls
executable file
·124 lines (102 loc) · 4.69 KB
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
// $Id: foo.cpp 2979 2006-01-10 00:00:04Z sommer $
//
// Cobble - A simple SuperTux level editor
// Copyright (C) 2006 Christoph Sommer <supertux@2006.expires.deltadevelopment.de>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using System.Collections;
namespace Cobble {
public class TileRepository {
public Dictionary<int, bool> isSolid = new Dictionary<int, bool>();
private Dictionary<int, Image> tileRepository = new Dictionary<int, Image>();
private TileSet tileset;
public TileRepository(string filename) {
isSolid[0] = false;
LoadingScreenForm loadingScreen = new LoadingScreenForm();
loadingScreen.setSubject("tileset");
loadingScreen.setMax(100);
loadingScreen.Show();
loadingScreen.Refresh();
if (!File.Exists(filename)) throw new ArgumentException("Tile file not found");
String basePath = System.IO.Path.GetDirectoryName(filename);
tileset = new TileSet();
tileset.Parse(filename);
// append a TileGroup (misc) with all tiles that are not yet in a TileGroup
TileGroup miscTiles = new TileGroup();
foreach (Tile tile in tileset.Tiles) {
if (tile == null) continue;
bool found = false;
foreach (TileGroup tileGroup in tileset.TileGroups) {
if (tileGroup.Tiles.Contains(tile.ID)) found = true;
}
if (found) continue;
miscTiles.Name = "(misc)";
miscTiles.Tiles.Add(tile.ID);
}
tileset.TileGroups.Add(miscTiles);
loadingScreen.setMax(tileset.Tiles.Count);
int progress = 0;
foreach (Tile tile in tileset.Tiles) {
progress++;
if (tile == null) continue;
addTile(tile, basePath);
loadingScreen.setProgress(progress);
}
loadingScreen.Close();
}
public ArrayList getTileGroups() {
return tileset.TileGroups;
}
public Image getTile(int id) {
if (!tileRepository.ContainsKey(id)) return null;
return tileRepository[id];
}
private void addTile(Tile tile, string basePath) {
if (tile.ID == 0) return;
Rectangle srcRect = new Rectangle(0, 0, 32, 32);
Bitmap srcImg = null;
if (tile.EditorImage != null) {
string fname = tile.EditorImage;
fname = System.IO.Path.Combine(basePath, fname.Replace('/', System.IO.Path.DirectorySeparatorChar));
srcImg = new Bitmap(fname);
} else if ((tile.Images != null) && (tile.Images.Count >= 1)) {
ImageRegion ir = (ImageRegion)tile.Images[0];
string fname = ir.ImageFile;
fname = System.IO.Path.Combine(basePath, fname.Replace('/', System.IO.Path.DirectorySeparatorChar));
srcImg = new Bitmap(fname);
if (ir.Region != Rectangle.Empty) {
srcRect = ir.Region;
} else if (ir.RelativeRegion != RectangleF.Empty) {
int x = (int)(ir.RelativeRegion.X * srcImg.Width);
int y = (int)(ir.RelativeRegion.Y * srcImg.Height);
int w = (int)(ir.RelativeRegion.Width * srcImg.Width);
int h = (int)(ir.RelativeRegion.Height * srcImg.Height);
srcRect = new Rectangle(x, y, w, h);
}
} else throw new ArgumentException("No usable images found for tile #"+tile.ID);
Bitmap dstImg = new Bitmap(32, 32);
Graphics gr = Graphics.FromImage(dstImg);
gr.DrawImage(srcImg, new Rectangle(0, 0, 32, 32), srcRect, GraphicsUnit.Pixel);
tileRepository[tile.ID] = dstImg;
isSolid[tile.ID] = tile.Solid;
}
}
}