rename TileMapLoader and add saving support

This commit is contained in:
Gered 2013-08-29 23:15:41 -04:00
parent 9bd3a42744
commit bd55d43ab3
3 changed files with 64 additions and 9 deletions

View file

@ -220,7 +220,7 @@
<Compile Include="Graphics\Atlas\Json\TextureAtlasLoader.cs" />
<Compile Include="TileMap\TileDataSerializer.cs" />
<Compile Include="TileMap\Json\JsonTileMap.cs" />
<Compile Include="TileMap\Json\TileMapLoader.cs" />
<Compile Include="TileMap\Json\TileMapSerializer.cs" />
<Compile Include="IO\FileOpenMode.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />

View file

@ -1,23 +1,27 @@
using System;
using Blarg.GameFramework.TileMap.Meshes;
using Blarg.GameFramework.TileMap;
using System.IO;
using Newtonsoft.Json;
using Blarg.GameFramework.IO;
using Blarg.GameFramework.TileMap;
using Blarg.GameFramework.TileMap.Lighting;
using Blarg.GameFramework.TileMap.Meshes;
using System.Collections.Generic;
namespace Blarg.GameFramework.TileMap.Json
{
public static class TileMapLoader
public static class TileMapSerializer
{
public static TileMap Load(string file, TileMeshCollection tileMeshes)
{
var stream = Framework.FileSystem.Open(file);
using (var stream = Framework.FileSystem.Open(file))
{
string path = null;
if (file.Contains("/"))
path = file.Substring(0, file.LastIndexOf('/') + 1);
return Load(stream, tileMeshes, path);
}
}
public static TileMap Load(Stream file, TileMeshCollection tileMeshes, string path = null)
{
@ -68,6 +72,55 @@ namespace Blarg.GameFramework.TileMap.Json
return tileMap;
}
public static void Save(TileMap tileMap, string file)
{
using (var stream = Framework.FileSystem.Open(file, FileOpenMode.Create))
{
Save(tileMap, stream);
}
}
public static void Save(TileMap tileMap, Stream file)
{
if (tileMap == null)
throw new ArgumentNullException("tileMap");
if (file == null)
throw new ArgumentNullException("file");
var map = new JsonTileMap();
map.ChunkWidth = tileMap.ChunkWidth;
map.ChunkHeight = tileMap.ChunkHeight;
map.ChunkDepth = tileMap.ChunkDepth;
map.WidthInChunks = tileMap.WidthInChunks;
map.HeightInChunks = tileMap.HeightInChunks;
map.DepthInChunks = tileMap.DepthInChunks;
// TODO: figure out real lighting mode from the types of vertex generator / lighter objects set
map.LightingMode = null;
// each serialized chunk will be the same size in bytes (same number of tiles in each)
int chunkSizeInBytes = tileMap.Chunks[0].Data.Length * TileDataSerializer.TILE_SIZE_BYTES;
map.Chunks = new List<string>(tileMap.Chunks.Length);
for (int i = 0; i < tileMap.Chunks.Length; ++i)
{
var chunk = tileMap.Chunks[i];
var buffer = new MemoryStream(chunkSizeInBytes);
using (var byteWriter = new BinaryWriter(buffer))
{
TileDataSerializer.Serialize(chunk, byteWriter);
}
map.Chunks.Add(Convert.ToBase64String(buffer.ToArray()));
}
using (var writer = new StreamWriter(file))
{
writer.Write(JsonConvert.SerializeObject(map, Formatting.Indented));
}
}
}
}

View file

@ -5,6 +5,8 @@ namespace Blarg.GameFramework.TileMap
{
public static class TileDataSerializer
{
public const int TILE_SIZE_BYTES = 17; // because sizeof() only works with value types
public static void Serialize(TileRawDataContainer src, BinaryWriter writer)
{
var tiles = src.Data;