DEADSOFTWARE

26281f6c81dfa5c16207c91ce810a6e5f3016192
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GameSaver.java
1 package ru.deadsoftware.cavedroid.game;
3 import com.badlogic.gdx.files.FileHandle;
5 import java.io.BufferedOutputStream;
6 import java.io.DataInputStream;
7 import java.io.IOException;
8 import java.nio.ByteBuffer;
10 //TODO rewrite saver
11 public class GameSaver {
13 private static final int VERSION = 0;
16 private static byte[] intToBytes(int i) {
17 return ByteBuffer.allocate(4).putInt(i).array();
18 }
20 private static void saveMap(FileHandle file, int[][] map) throws IOException {
21 int rl, bl;
22 int width = map.length;
23 int height = map[0].length;
24 BufferedOutputStream out = new BufferedOutputStream(file.write(false));
25 out.write(intToBytes(VERSION));
26 out.write(intToBytes(width));
27 out.write(intToBytes(height));
28 for (int y = 0; y < height; y++) {
29 bl = map[0][y];
30 rl = 0;
31 for (int[] ints : map) {
32 if (ints[y] != bl) {
33 out.write(intToBytes(rl));
34 out.write(intToBytes(bl));
35 rl = 0;
36 bl = ints[y];
37 }
38 rl++;
39 }
40 out.write(intToBytes(rl));
41 out.write(intToBytes(bl));
42 }
43 out.flush();
44 out.close();
45 }
48 private static int[][] loadMap(FileHandle file) throws Exception {
49 int[][] map;
50 int ver, width, height;
51 int rl, bl;
52 DataInputStream in = new DataInputStream(file.read());
53 ver = in.readInt();
54 if (VERSION == ver) {
55 width = in.readInt();
56 height = in.readInt();
57 map = new int[width][height];
58 for (int y = 0; y < height; y++) {
59 for (int x = 0; x < width; x += rl) {
60 rl = in.readInt();
61 bl = in.readInt();
62 for (int i = x; i < x + rl; i++) {
63 map[i][y] = bl;
64 }
65 }
66 }
67 in.close();
68 } else {
69 throw new Exception("version mismatch");
70 }
71 return map;
72 }
74 public static GameProc load() {
75 // FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav");
76 // GameProc gameProc = null;
77 // try {
78 // ObjectInputStream in = new ObjectInputStream(file.read());
79 // int ver = in.readInt();
80 // if (VERSION == ver) {
81 // gameProc = (GameProc) in.readObject();
82 // } else {
83 // throw new Exception("version mismatch");
84 // }
85 // in.close();
86 // gameProc.world = new GameWorld(
87 // loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav")),
88 // loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav"))
89 // );
90 // gameProc.physics = new GamePhysics();
91 // gameProc.input = new GameInput();
92 // } catch (Exception e) {
93 // Gdx.app.error("GameSaver", e.getMessage(), e);
94 // Gdx.app.exit();
95 // }
96 // return gameProc;
97 return null;
98 }
100 public static void save(GameProc gp) {
101 // FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/");
102 // file.mkdirs();
103 // file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav");
104 // try {
105 // ObjectOutputStream out = new ObjectOutputStream(file.write(false));
106 // out.writeInt(VERSION);
107 // out.writeObject(gp);
108 // out.close();
109 // saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav"), gp.world.getFullForeMap());
110 // saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav"), gp.world.getFullBackMap());
111 // } catch (Exception e) {
112 // e.printStackTrace();
113 // }
116 public static boolean exists() {
117 // return (Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav").exists() &&
118 // Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav").exists() &&
119 // Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav").exists());
120 return false;