add mostly-untested TileMap loading via json / Base64 deserialization

This commit is contained in:
Gered 2013-07-18 19:52:18 -04:00
parent 22172f8b48
commit 5234c732af
3 changed files with 71 additions and 0 deletions

View file

@ -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;

View file

@ -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)

View file

@ -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;
}
}