From f236e34e2dd3bc536ecdf6a4ae36bf01f1434625 Mon Sep 17 00:00:00 2001 From: Jonas Kulla Date: Thu, 20 Nov 2014 14:45:54 +0100 Subject: [PATCH] Serializable, Table: Some general cleanups --- src/etc.cpp | 57 +++++++++++++++++---------------------- src/serial-util.h | 47 +++++++++----------------------- src/table.cpp | 68 ++++++++++++++++++++++------------------------- src/table.h | 12 ++++----- 4 files changed, 74 insertions(+), 110 deletions(-) diff --git a/src/etc.cpp b/src/etc.cpp index de8ab87..ed1aad4 100644 --- a/src/etc.cpp +++ b/src/etc.cpp @@ -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; } diff --git a/src/serial-util.h b/src/serial-util.h index 6d221eb..bea7815 100644 --- a/src/serial-util.h +++ b/src/serial-util.h @@ -27,67 +27,44 @@ #include -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 diff --git a/src/table.cpp b/src/table.cpp index a51338b..0d7dc6c 100644 --- a/src/table.cpp +++ b/src/table.cpp @@ -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 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; } diff --git a/src/table.h b/src/table.h index 6c20b34..dbf7e45 100644 --- a/src/table.h +++ b/src/table.h @@ -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: /* modified; private: - int m_x, m_y, m_z; + int xs, ys, zs; std::vector data; };