Serializable, Table: Some general cleanups

This commit is contained in:
Jonas Kulla 2014-11-20 14:45:54 +01:00
parent 33c571e691
commit f236e34e2d
4 changed files with 74 additions and 110 deletions

View File

@ -105,12 +105,10 @@ int Color::serialSize() const
void Color::serialize(char *buffer) const void Color::serialize(char *buffer) const
{ {
char *buf = buffer; writeDouble(&buffer, red);
writeDouble(&buffer, green);
write_double(&buf, red); writeDouble(&buffer, blue);
write_double(&buf, green); writeDouble(&buffer, alpha);
write_double(&buf, blue);
write_double(&buf, alpha);
} }
Color *Color::deserialize(const char *data, int len) Color *Color::deserialize(const char *data, int len)
@ -119,12 +117,11 @@ Color *Color::deserialize(const char *data, int len)
throw Exception(Exception::ArgumentError, "Color: Serialized data invalid"); throw Exception(Exception::ArgumentError, "Color: Serialized data invalid");
Color *c = new Color(); Color *c = new Color();
uint i = 0;
c->red = read_double(data, i); c->red = readDouble(&data);
c->green = read_double(data, i); c->green = readDouble(&data);
c->blue = read_double(data, i); c->blue = readDouble(&data);
c->alpha = read_double(data, i); c->alpha = readDouble(&data);
c->updateInternal(); c->updateInternal();
return c; return c;
@ -241,12 +238,10 @@ int Tone::serialSize() const
void Tone::serialize(char *buffer) const void Tone::serialize(char *buffer) const
{ {
char *buf = buffer; writeDouble(&buffer, red);
writeDouble(&buffer, green);
write_double(&buf, red); writeDouble(&buffer, blue);
write_double(&buf, green); writeDouble(&buffer, gray);
write_double(&buf, blue);
write_double(&buf, gray);
} }
Tone *Tone::deserialize(const char *data, int len) Tone *Tone::deserialize(const char *data, int len)
@ -255,12 +250,11 @@ Tone *Tone::deserialize(const char *data, int len)
throw Exception(Exception::ArgumentError, "Tone: Serialized data invalid"); throw Exception(Exception::ArgumentError, "Tone: Serialized data invalid");
Tone *t = new Tone(); Tone *t = new Tone();
uint i = 0;
t->red = read_double(data, i); t->red = readDouble(&data);
t->green = read_double(data, i); t->green = readDouble(&data);
t->blue = read_double(data, i); t->blue = readDouble(&data);
t->gray = read_double(data, i); t->gray = readDouble(&data);
t->updateInternal(); t->updateInternal();
return t; return t;
@ -390,12 +384,10 @@ int Rect::serialSize() const
void Rect::serialize(char *buffer) const void Rect::serialize(char *buffer) const
{ {
char *buf = buffer; writeInt32(&buffer, x);
writeInt32(&buffer, y);
write_int32(&buf, x); writeInt32(&buffer, width);
write_int32(&buf, y); writeInt32(&buffer, height);
write_int32(&buf, width);
write_int32(&buf, height);
} }
Rect *Rect::deserialize(const char *data, int len) Rect *Rect::deserialize(const char *data, int len)
@ -404,12 +396,11 @@ Rect *Rect::deserialize(const char *data, int len)
throw Exception(Exception::ArgumentError, "Rect: Serialized data invalid"); throw Exception(Exception::ArgumentError, "Rect: Serialized data invalid");
Rect *r = new Rect(); Rect *r = new Rect();
uint i = 0;
r->x = read_int32(data, i); r->x = readInt32(&data);
r->y = read_int32(data, i); r->y = readInt32(&data);
r->width = read_int32(data, i); r->width = readInt32(&data);
r->height = read_int32(data, i); r->height = readInt32(&data);
return r; return r;
} }

View File

@ -27,67 +27,44 @@
#include <SDL_endian.h> #include <SDL_endian.h>
typedef unsigned uint;
#if SDL_BYTEORDER != SDL_LIL_ENDIAN #if SDL_BYTEORDER != SDL_LIL_ENDIAN
#error "Non little endian systems not supported" #error "Non little endian systems not supported"
#endif #endif
static inline int16_t
read_int16(const char *data, uint &i)
{
int16_t result;
memcpy(&result, &data[i], 2);
i += 2;
return result;
}
static inline int32_t static inline int32_t
read_int32(const char *data, uint &i) readInt32(const char **dataP)
{ {
int32_t result; int32_t result;
memcpy(&result, &data[i], 4); memcpy(&result, *dataP, 4);
i += 4; *dataP += 4;
return result; return result;
} }
static inline double static inline double
read_double(const char *data, uint &i) readDouble(const char **dataP)
{ {
double result; double result;
memcpy(&result, &data[i], 8); memcpy(&result, *dataP, 8);
i += 8; *dataP += 8;
return result; return result;
} }
static inline void static inline void
write_int16(char **data, int16_t value) writeInt32(char **dataP, int32_t value)
{ {
memcpy(*data, &value, 2); memcpy(*dataP, &value, 4);
*dataP += 4;
*data += 2;
} }
static inline void static inline void
write_int32(char **data, int32_t value) writeDouble(char **dataP, double value)
{ {
memcpy(*data, &value, 4); memcpy(*dataP, &value, 8);
*dataP += 8;
*data += 4;
}
static inline void
write_double(char **data, double value)
{
memcpy(*data, &value, 8);
*data += 8;
} }
#endif // SERIALUTIL_H #endif // SERIALUTIL_H

View File

@ -30,94 +30,92 @@
/* Init normally */ /* Init normally */
Table::Table(int x, int y /*= 1*/, int z /*= 1*/) Table::Table(int x, int y /*= 1*/, int z /*= 1*/)
: m_x(x), m_y(y), m_z(z), : xs(x), ys(y), zs(z),
data(x*y*z) data(x*y*z)
{} {}
Table::Table(const Table &other) Table::Table(const Table &other)
: m_x(other.m_x), m_y(other.m_y), m_z(other.m_z), : xs(other.xs), ys(other.ys), zs(other.zs),
data(other.data) data(other.data)
{} {}
int16_t Table::get(int x, int y, int z) const int16_t Table::get(int x, int y, int z) const
{ {
return data[m_x*m_y*z + m_x*y + x]; return data[xs*ys*z + xs*y + x];
} }
void Table::set(int16_t value, int x, int y, int z) void Table::set(int16_t value, int x, int y, int z)
{ {
if (x < 0 || x >= m_x if (x < 0 || x >= xs
|| y < 0 || y >= m_y || y < 0 || y >= ys
|| z < 0 || z >= m_z) || z < 0 || z >= zs)
{ {
return; return;
} }
data[m_x*m_y*z + m_x*y + x] = value; data[xs*ys*z + xs*y + x] = value;
modified(); modified();
} }
void Table::resize(int x, int y, int z) void Table::resize(int x, int y, int z)
{ {
if (x == m_x && y == m_y && z == m_z) if (x == xs && y == ys && z == zs)
return; return;
std::vector<int16_t> newData(x*y*z); std::vector<int16_t> newData(x*y*z);
for (int k = 0; k < std::min(z, m_z); ++k) for (int k = 0; k < std::min(z, zs); ++k)
for (int j = 0; j < std::min(y, m_y); ++j) for (int j = 0; j < std::min(y, ys); ++j)
for (int i = 0; i < std::min(x, m_x); ++i) for (int i = 0; i < std::min(x, xs); ++i)
newData[x*y*k + x*j + i] = at(i, j, k); newData[x*y*k + x*j + i] = at(i, j, k);
data.swap(newData); data.swap(newData);
m_x = x; xs = x;
m_y = y; ys = y;
m_z = z; zs = z;
return; return;
} }
void Table::resize(int x, int y) void Table::resize(int x, int y)
{ {
resize(x, y, m_z); resize(x, y, zs);
} }
void Table::resize(int x) void Table::resize(int x)
{ {
resize(x, m_y, m_z); resize(x, ys, zs);
} }
/* Serializable */ /* Serializable */
int Table::serialSize() const int Table::serialSize() const
{ {
/* header + data */ /* header + data */
return 20 + (m_x * m_y * m_z) * 2; return 20 + (xs * ys * zs) * 2;
} }
void Table::serialize(char *buffer) const void Table::serialize(char *buffer) const
{ {
char *buff_p = buffer;
/* Table dimensions: we don't care /* Table dimensions: we don't care
* about them but RMXP needs them */ * about them but RMXP needs them */
int dim = 1; int dim = 1;
int size = m_x * m_y * m_z; int size = xs * ys * zs;
if (m_y > 1) if (ys > 1)
dim = 2; dim = 2;
if (m_z > 1) if (zs > 1)
dim = 3; dim = 3;
write_int32(&buff_p, dim); writeInt32(&buffer, dim);
write_int32(&buff_p, m_x); writeInt32(&buffer, xs);
write_int32(&buff_p, m_y); writeInt32(&buffer, ys);
write_int32(&buff_p, m_z); writeInt32(&buffer, zs);
write_int32(&buff_p, size); writeInt32(&buffer, size);
memcpy(buff_p, dataPtr(data), sizeof(int16_t)*size); memcpy(buffer, dataPtr(data), sizeof(int16_t)*size);
} }
@ -126,13 +124,11 @@ Table *Table::deserialize(const char *data, int len)
if (len < 20) if (len < 20)
throw Exception(Exception::RGSSError, "Marshal: Table: bad file format"); throw Exception(Exception::RGSSError, "Marshal: Table: bad file format");
uint idx = 0; readInt32(&data);
int x = readInt32(&data);
read_int32(data, idx); int y = readInt32(&data);
int x = read_int32(data, idx); int z = readInt32(&data);
int y = read_int32(data, idx); int size = readInt32(&data);
int z = read_int32(data, idx);
int size = read_int32(data, idx);
if (size != x*y*z) if (size != x*y*z)
throw Exception(Exception::RGSSError, "Marshal: Table: bad file format"); throw Exception(Exception::RGSSError, "Marshal: Table: bad file format");
@ -141,7 +137,7 @@ Table *Table::deserialize(const char *data, int len)
throw Exception(Exception::RGSSError, "Marshal: Table: bad file format"); throw Exception(Exception::RGSSError, "Marshal: Table: bad file format");
Table *t = new Table(x, y, z); Table *t = new Table(x, y, z);
memcpy(dataPtr(t->data), &data[idx], sizeof(int16_t)*size); memcpy(dataPtr(t->data), data, sizeof(int16_t)*size);
return t; return t;
} }

View File

@ -36,9 +36,9 @@ public:
Table(const Table &other); Table(const Table &other);
virtual ~Table() {} virtual ~Table() {}
int xSize() const { return m_x; } int xSize() const { return xs; }
int ySize() const { return m_y; } int ySize() const { return ys; }
int zSize() const { return m_z; } int zSize() const { return zs; }
int16_t get(int x, int y = 0, int z = 0) const; int16_t get(int x, int y = 0, int z = 0) const;
void set(int16_t value, int x, int y = 0, int z = 0); void set(int16_t value, int x, int y = 0, int z = 0);
@ -54,18 +54,18 @@ public:
/* <internal */ /* <internal */
inline int16_t &at(int x, int y = 0, int z = 0) inline int16_t &at(int x, int y = 0, int z = 0)
{ {
return data[m_x*m_y*z + m_x*y + x]; return data[xs*ys*z + xs*y + x];
} }
inline const int16_t &at(int x, int y = 0, int z = 0) const inline const int16_t &at(int x, int y = 0, int z = 0) const
{ {
return data[m_x*m_y*z + m_x*y + x]; return data[xs*ys*z + xs*y + x];
} }
sigc::signal<void> modified; sigc::signal<void> modified;
private: private:
int m_x, m_y, m_z; int xs, ys, zs;
std::vector<int16_t> data; std::vector<int16_t> data;
}; };