[Add] Some serialization work on Body/Star systems. Still a fair bit to

do.
This commit is contained in:
Allanis 2018-01-31 22:26:05 +00:00
parent 0e55efb47e
commit b733631d8c
2 changed files with 64 additions and 4 deletions

View File

@ -1,3 +1,4 @@
#include "star_system.h"
#include "serializer.h"
#include "l3d.h"
#include "frame.h"
@ -6,6 +7,8 @@
namespace Serializer {
static std::vector<Frame*> g_frames;
static std::vector<Body*>g_bodies;
static std::vector<StarSystem::SBody*>g_sbodies;
Frame* LookupFrame(int index) {
return g_frames[index];
@ -29,7 +32,43 @@ static void AddFrame(Frame* f) {
void IndexFrames(void) {
g_frames.clear();
AddFrame(Space::rootFrame);
printf("%d frames indexed\n", g_frames.size());
}
StarSystem::SBody* LookupSystemBody(int index) { return (index == -1 ? 0 : g_sbodies[index]); }
int LookupSystemBody(StarSystem::SBody* b) {
if(!b) return -1;
for(unsigned int i = 0; i < g_sbodies.size(); i++) {
if(g_sbodies[i] == b) return i;
}
assert(0);
}
static void add_sbody(StarSystem::SBody* b) {
g_sbodies.push_back(b);
for(unsigned int i = 0; i < b->children.size(); i++) {
add_sbody(b->children[i]);
}
}
void IndexSystemBodies(StarSystem* s) {
g_sbodies.clear();
add_sbody(s->rootBody);
}
Body* LookupBody(int index) { return (index == -1 ? 0 : g_bodies[index]); }
int LookupBody(Body* b) {
if(!b) return -1;
for(unsigned int i = 0; i < g_bodies.size(); i++) {
if(g_bodies[i] == b) return 1;
}
assert(0);
}
void IndexBodies(void) {
g_bodies.clear();
for(Space::bodiesIter_t i = Space::bodies.begin(); i != Space::bodies.end(); ++i) {
g_bodies.push_back(*i);
}
}
namespace Write {
@ -37,7 +76,6 @@ namespace Write {
static FILE* sfptr;
bool Game(const char* filename) {
struct Object* o;
sfptr = fopen(filename, "wb");
if(sfptr == NULL) {
@ -68,6 +106,10 @@ namespace Write {
checksum += x;
}
void wr_bool(bool x) {
wr_byte((unsigned char)x);
}
void wr_short(short x) {
wr_byte((unsigned char)(x & 0xff));
wr_byte((unsigned char)((x>>8) & 0xff));
@ -120,7 +162,7 @@ namespace Write {
wr_string(s.c_str());
}
void wr_vector3d(vector3d& vec) {
void wr_vector3d(vector3d vec) {
wr_double(vec.x);
wr_double(vec.y);
wr_double(vec.z);
@ -177,6 +219,10 @@ namespace Read {
return x;
}
bool rd_bool(void) {
return(bool)rd_byte();
}
short rd_short(void) {
int t1, t2;
t2 = rd_byte();

View File

@ -5,13 +5,26 @@
#define SAVEFILE_VERSION 1
class Frame;
class Body;
class StarSystem;
class StarSystem::SBody;
namespace Serializer {
void IndexFrames(void);
Frame* LookupFrame(Frame* f);
void IndexBodies(void);
Body* LookupBody(int index);
int LookupSystemBody(StarSystem::SBody*);
int LookupBody(Body*);
void IndexSystemBodies(StarSystem*);
StarSystem::SBody* LookupSystemBody(int index);
int LookupSystemBody(StarSystem::SBody*);
namespace Write {
bool Game(const char* filename);
void wr_bool(bool x);
void wr_byte(unsigned char x);
void wr_short(short x);
void wr_int(int x);
@ -19,12 +32,13 @@ namespace Serializer {
void wr_double(double f);
void wr_cstring(const char* s);
void wr_string(std::string& s);
void wr_vector3d(vector3d& vec);
void wr_vector3d(vector3d vec);
}
namespace Read {
bool Game(const char* filename);
bool is_olderthan(int version);
bool rd_bool(void);
unsigned char rd_byte(void);
short rd_short(void);
int rd_int(void);