patterncsharpMinor
Load maps into game
Viewed 0 times
intomapsgameload
Problem
I'm currently designing a game using a game engine I created and I'm currently implementing a method that loads maps into my game. The maps are made using Tiled Map Editor, saved as an XML file and are loaded using TiledSharp.
At the moment, I'm trying to load a map, that contains 1437 'Game Objects' (These are just tiles, as far as tiled is concerned) and currently it takes around 360ms to load. I was just wondering what I can do to load my maps faster.
Here is the code that loads my maps:
At the moment, I'm trying to load a map, that contains 1437 'Game Objects' (These are just tiles, as far as tiled is concerned) and currently it takes around 360ms to load. I was just wondering what I can do to load my maps faster.
Here is the code that loads my maps:
public void LoadMap(string path)
{
Console.WriteLine("Loading map...");
var watch = new Stopwatch();
watch.Start();
// Import XML data using TiledSharp
var tmxMap = new TmxMap(path);
// Lets all the classes variables
width = tmxMap.Width;
height = tmxMap.Height;
tileWidth = tmxMap.TileWidth;
tileHeight = tmxMap.TileHeight;
wholeWidth = width * tileWidth;
wholeHeight = height * tileHeight;
// Now lets load all the sprite sheets that are used in this map
var spriteSheets = LoadSpriteSheets(tmxMap);
// A value that indicates what layer we're drawing to
int currentLayer = -1;
foreach (var layer in tmxMap.Layers)
{
// Increment a layer foreach layer we're in
currentLayer++;
foreach (var tile in layer.Tiles)
{
// Get the tiles x, y and global id
int x = tile.X;
int y = tile.Y;
int gid = tile.Gid;
// Now lets loop through the tilesets in the map
for (int i = 0; i ().Sprite.Depth = depth;
}
}
private List LoadSpriteSheets(TmxMap tmxMap)
{
List sheets = new List();
for (int i = 0; i < tmxMap.Tilesets.Count; i++)
sheets.Add(new SpriteSheet(tmxMap.Tilesets[i].Image.Source, tmxMap.TileWidth, tmxMap.TileHeight, Color.White));
return sheets;
}Solution
You should start with a profiler. Having said that, there is one easy optimization you can make without a lot of code changes.
Change the following line in LoadSpriteSheets from
to
The amount of elements in the list is known in advance so you can avoid the cost of repeatedly resizing the container.
Change the following line in LoadSpriteSheets from
List sheets = new List();to
List sheets = new List(tmxMap.Tilesets.Count);The amount of elements in the list is known in advance so you can avoid the cost of repeatedly resizing the container.
Context
StackExchange Code Review Q#160255, answer score: 2
Revisions (0)
No revisions yet.