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
{
char *buf = buffer;
write_double(&buf, red);
write_double(&buf, green);
write_double(&buf, blue);
write_double(&buf, alpha);
writeDouble(&buffer, red);
writeDouble(&buffer, green);
writeDouble(&buffer, blue);
writeDouble(&buffer, alpha);
}
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");
Color *c = new Color();
uint i = 0;
c->red = read_double(data, i);
c->green = read_double(data, i);
c->blue = read_double(data, i);
c->alpha = read_double(data, i);
c->red = readDouble(&data);
c->green = readDouble(&data);
c->blue = readDouble(&data);
c->alpha = readDouble(&data);
c->updateInternal();
return c;
@ -241,12 +238,10 @@ int Tone::serialSize() const
void Tone::serialize(char *buffer) const
{
char *buf = buffer;
write_double(&buf, red);
write_double(&buf, green);
write_double(&buf, blue);
write_double(&buf, gray);
writeDouble(&buffer, red);
writeDouble(&buffer, green);
writeDouble(&buffer, blue);
writeDouble(&buffer, gray);
}
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");
Tone *t = new Tone();
uint i = 0;
t->red = read_double(data, i);
t->green = read_double(data, i);
t->blue = read_double(data, i);
t->gray = read_double(data, i);
t->red = readDouble(&data);
t->green = readDouble(&data);
t->blue = readDouble(&data);
t->gray = readDouble(&data);
t->updateInternal();
return t;
@ -390,12 +384,10 @@ int Rect::serialSize() const
void Rect::serialize(char *buffer) const
{
char *buf = buffer;
write_int32(&buf, x);
write_int32(&buf, y);
write_int32(&buf, width);
write_int32(&buf, height);
writeInt32(&buffer, x);
writeInt32(&buffer, y);
writeInt32(&buffer, width);
writeInt32(&buffer, height);
}
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");
Rect *r = new Rect();
uint i = 0;
r->x = read_int32(data, i);
r->y = read_int32(data, i);
r->width = read_int32(data, i);
r->height = read_int32(data, i);
r->x = readInt32(&data);
r->y = readInt32(&data);
r->width = readInt32(&data);
r->height = readInt32(&data);
return r;
}

View File

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

View File

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

View File

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