forked from libgdx/libgdx
-
Notifications
You must be signed in to change notification settings - Fork 0
Saved game serialization
molp edited this page Dec 20, 2013
·
4 revisions
The JSON class can automatically convert Java objects to and from JSON.
Kryo can be used to automatically and efficiently serialize game state.
Kryo can handle most POJOs and other classes, but some classes need special handling. Below are a few serializers for libgdx classes.
Note that classes like Texture should not be serialized in most cases. It would be better to not have a string instead of a Texture object in your object graph. After serializing you would process the objects and look up the texture using the string path.
kryo.register(Array.class, new Serializer<Array>() {
{
setAcceptsNull(true);
}
private Class genericType;
public void setGenerics (Kryo kryo, Class[] generics) {
if (kryo.isFinal(generics[0])) genericType = generics[0];
}
public void write (Kryo kryo, Output output, Array array) {
int length = array.size;
output.writeInt(length, true);
if (length == 0) return;
if (genericType != null) {
Serializer serializer = kryo.getSerializer(genericType);
genericType = null;
for (Object element : array)
kryo.writeObjectOrNull(output, element, serializer);
} else {
for (Object element : array)
kryo.writeClassAndObject(output, element);
}
}
public Array read (Kryo kryo, Input input, Class<Array> type) {
Array array = new Array();
kryo.reference(array);
int length = input.readInt(true);
array.ensureCapacity(length);
if (genericType != null) {
Class elementClass = genericType;
Serializer serializer = kryo.getSerializer(genericType);
genericType = null;
for (int i = 0; i < length; i++)
array.add(kryo.readObjectOrNull(input, elementClass, serializer));
} else {
for (int i = 0; i < length; i++)
array.add(kryo.readClassAndObject(input));
}
return array;
}
});
kryo.register(IntArray.class, new Serializer<IntArray>() {
{
setAcceptsNull(true);
}
public void write (Kryo kryo, Output output, IntArray array) {
int length = array.size;
output.writeInt(length, true);
if (length == 0) return;
for (int i = 0, n = array.size; i < n; i++)
output.writeInt(array.get(i), true);
}
public IntArray read (Kryo kryo, Input input, Class<IntArray> type) {
IntArray array = new IntArray();
kryo.reference(array);
int length = input.readInt(true);
array.ensureCapacity(length);
for (int i = 0; i < length; i++)
array.add(input.readInt(true));
return array;
}
});
kryo.register(FloatArray.class, new Serializer<FloatArray>() {
{
setAcceptsNull(true);
}
public void write (Kryo kryo, Output output, FloatArray array) {
int length = array.size;
output.writeInt(length, true);
if (length == 0) return;
for (int i = 0, n = array.size; i < n; i++)
output.writeFloat(array.get(i));
}
public FloatArray read (Kryo kryo, Input input, Class<FloatArray> type) {
FloatArray array = new FloatArray();
kryo.reference(array);
int length = input.readInt(true);
array.ensureCapacity(length);
for (int i = 0; i < length; i++)
array.add(input.readFloat());
return array;
}
});
kryo.register(Color.class, new Serializer<Color>() {
public Color read (Kryo kryo, Input input, Class<Color> type) {
Color color = new Color();
Color.rgba8888ToColor(color, input.readInt());
return color;
}
public void write (Kryo kryo, Output output, Color color) {
output.writeInt(Color.rgba8888(color));
}
});
-
Developer's Guide
- Introduction
- Goals & Features
- Community & Support
- Contributing
- Games Built with Libgdx
- Prerequisites
- Gradle Project Setup, Running, Debugging and Packaging
- Project Setup, Running & Debugging
- Third Party Services
- Working from Source
- Using libgdx with other JVM languages
- The Application Framework
- A Simple Game
- File Handling
- Networking
- Preferences
- Input Handling
- Memory Management
- Audio
-
Graphics
- Configuration & Querying Graphics ??
- Fullscreen & VSync
- Continuous & Non-Continuous Rendering
- Clearing the Screen
- Take a Screenshot
- OpenGL ES Support * Configuration & Querying OpenGL ?? * Direct Access ?? * Utility Classes * Rendering Shapes * Textures & TextureRegions * Meshes * Shaders * Frame Buffer Objects
- 2D Graphics * SpriteBatch, TextureRegions, and Sprite * 2D Animation * Clipping, with the use of ScissorStack * Orthographic camera * Mapping Touch Coordinates ?? * Viewports * NinePatches * Bitmap Fonts * Distance field fonts * Using TextureAtlases * Pixmaps * Packing Atlases Offline * Packing Atlases at Runtime * 2D Particle Effects * Tile Maps * scene2d * scene2d.ui * Skin
- 3D Graphics * Quick Start * Models * Material and environment * 3D animations and skinning * Importing Blender models in LibGDX * Perspective Camera ?? * Picking ??
- Managing Your Assets
- Utilities
-
Math Utilities
- Interpolation
- Vectors, Matrices, Quaternions
- Circles, Planes, Rays, etc.
- Path interface & Splines
- Bounding Volumes ??
- Intersection & Overlap Testing ??
- Physics
- Tools
- Extensions
- Deploying your Application
- Building Libgdx ??
- Known Issues
- Articles
- Deprecated (May be outdated)