From 5234c732af85e59c7e8ce5aff07dbd3d57bd6dab Mon Sep 17 00:00:00 2001 From: gered Date: Thu, 18 Jul 2013 19:52:18 -0400 Subject: [PATCH] add mostly-untested TileMap loading via json / Base64 deserialization --- src/com/blarg/gdx/tilemap3d/TileMap.java | 4 ++ .../serialization/TileChunkSerializer.java | 2 + .../serialization/TileMapLoader.java | 65 +++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 src/com/blarg/gdx/tilemap3d/serialization/TileMapLoader.java diff --git a/src/com/blarg/gdx/tilemap3d/TileMap.java b/src/com/blarg/gdx/tilemap3d/TileMap.java index 9d7788e..83360bb 100644 --- a/src/com/blarg/gdx/tilemap3d/TileMap.java +++ b/src/com/blarg/gdx/tilemap3d/TileMap.java @@ -27,6 +27,10 @@ public class TileMap extends TileContainer implements Disposable { public byte ambientLightValue; public byte skyLightValue; + public TileChunk[] getChunks() { + return chunks; + } + @Override public int getWidth() { return widthInChunks * chunkWidth; diff --git a/src/com/blarg/gdx/tilemap3d/serialization/TileChunkSerializer.java b/src/com/blarg/gdx/tilemap3d/serialization/TileChunkSerializer.java index 37ca641..2c71365 100644 --- a/src/com/blarg/gdx/tilemap3d/serialization/TileChunkSerializer.java +++ b/src/com/blarg/gdx/tilemap3d/serialization/TileChunkSerializer.java @@ -6,6 +6,8 @@ import com.blarg.gdx.tilemap3d.TileChunk; import java.nio.ByteBuffer; public class TileChunkSerializer { + public static final int TILE_SIZE_BYTES = 10; // TODO: is there some kind of java sizeof() type thing? + public static void serialize(TileChunk chunk, ByteBuffer buffer) { Tile[] tiles = chunk.getData(); for (int i = 0; i < tiles.length; ++i) diff --git a/src/com/blarg/gdx/tilemap3d/serialization/TileMapLoader.java b/src/com/blarg/gdx/tilemap3d/serialization/TileMapLoader.java new file mode 100644 index 0000000..b5a9a28 --- /dev/null +++ b/src/com/blarg/gdx/tilemap3d/serialization/TileMapLoader.java @@ -0,0 +1,65 @@ +package com.blarg.gdx.tilemap3d.serialization; + +import com.badlogic.gdx.files.FileHandle; +import com.badlogic.gdx.utils.Base64Coder; +import com.badlogic.gdx.utils.Json; +import com.blarg.gdx.Strings; +import com.blarg.gdx.tilemap3d.ChunkVertexGenerator; +import com.blarg.gdx.tilemap3d.TileChunk; +import com.blarg.gdx.tilemap3d.TileMap; +import com.blarg.gdx.tilemap3d.lighting.TileMapLighter; +import com.blarg.gdx.tilemap3d.tilemesh.TileMeshCollection; +import sun.reflect.generics.reflectiveObjects.NotImplementedException; + +import java.nio.ByteBuffer; + +public class TileMapLoader { + public static TileMap load(FileHandle mapFile, TileMeshCollection tileMeshes) { + if (mapFile == null) + throw new IllegalArgumentException(); + if (tileMeshes == null) + throw new IllegalArgumentException(); + + Json json = new Json(); + JsonTileMap jsonMap = json.fromJson(JsonTileMap.class, mapFile); + + if (jsonMap.chunks == null || jsonMap.chunks.size() == 0) + throw new RuntimeException("Invalid map: no chunks."); + int numChunks = (jsonMap.widthInChunks * jsonMap.heightInChunks * jsonMap.depthInChunks); + if (jsonMap.chunks.size() != numChunks) + throw new RuntimeException("Inconsistent map dimensions and number of chunks."); + + ChunkVertexGenerator chunkVertexGenerator = null; + TileMapLighter lighter = null; + + if (Strings.isNullOrEmpty(jsonMap.lightingMode)) { + chunkVertexGenerator = new ChunkVertexGenerator(); + lighter = null; + } else if (jsonMap.lightingMode.equalsIgnoreCase("simple")) { + throw new NotImplementedException(); + } else if (jsonMap.lightingMode.equalsIgnoreCase("skyAndSources")) { + throw new NotImplementedException(); + } else + throw new RuntimeException("Invalid lighting mode."); + + TileMap tileMap = new TileMap( + jsonMap.chunkWidth, jsonMap.chunkHeight, jsonMap.chunkDepth, + jsonMap.widthInChunks, jsonMap.heightInChunks, jsonMap.depthInChunks, + tileMeshes, + chunkVertexGenerator, + lighter + ); + + for (int i = 0; i < jsonMap.chunks.size(); ++i) { + String encodedChunk = jsonMap.chunks.get(i); + TileChunk outputChunk = tileMap.getChunks()[i]; + + byte[] chunkBytes = Base64Coder.decode(encodedChunk); + ByteBuffer buffer = ByteBuffer.wrap(chunkBytes); + + TileChunkSerializer.deserialize(buffer, outputChunk); + } + + return tileMap; + } +}