Преглед на файлове

SRF stuff

opengl-renderer-broken
Martin Dørum преди 4 години
родител
ревизия
4279e11517
променени са 3 файла, в които са добавени 38 реда и са изтрити 21 реда
  1. 23
    17
      libswan/include/swan/SRF.h
  2. 1
    0
      libswan/include/swan/swan.h
  3. 14
    4
      libswan/src/SRF.cc

+ 23
- 17
libswan/include/swan/SRF.h Целия файл

@@ -6,34 +6,40 @@
#include <unordered_map>
#include <memory>
#include <string>
#include <initializer_list>
#include <utility>
#include <stdint.h>

namespace Swan {

struct SRFTag {
virtual ~SRFTag() = default;
struct SRFVal {
virtual ~SRFVal() = default;

virtual void serialize(std::ostream &os) = 0;
virtual void parse(std::istream &is) = 0;

static SRFTag *read(std::istream &is);
static SRFVal *read(std::istream &is);
};

struct SRFObject: SRFTag {
struct SRFObject: SRFVal {
SRFObject() {}
SRFObject(std::initializer_list<std::pair<std::string, SRFVal *>> &lst);
void serialize(std::ostream &os) override;
void parse(std::istream &os) override;

std::unordered_map<std::string, std::unique_ptr<SRFTag>> val;
std::unordered_map<std::string, std::unique_ptr<SRFVal>> val;
};

struct SRFArray: SRFTag {
struct SRFArray: SRFVal {
SRFArray() {}
SRFArray(std::initializer_list<SRFVal *> &lst);
void serialize(std::ostream &os) override;
void parse(std::istream &os) override;

std::vector<std::unique_ptr<SRFTag>> val;
std::vector<std::unique_ptr<SRFVal>> val;
};

struct SRFString: SRFTag {
struct SRFString: SRFVal {
SRFString(): val("") {}
SRFString(const std::string &v): val(v) {}

@@ -43,7 +49,7 @@ struct SRFString: SRFTag {
std::string val;
};

struct SRFInt: SRFTag {
struct SRFInt: SRFVal {
SRFInt(): val(0) {}
SRFInt(int32_t v): val(v) {}

@@ -53,7 +59,7 @@ struct SRFInt: SRFTag {
int32_t val;
};

struct SRFFloat: SRFTag {
struct SRFFloat: SRFVal {
SRFFloat(): val(0) {}
SRFFloat(float v): val(v) {}

@@ -63,7 +69,7 @@ struct SRFFloat: SRFTag {
float val;
};

struct SRFDouble: SRFTag {
struct SRFDouble: SRFVal {
SRFDouble(): val(0) {}
SRFDouble(double v): val(v) {}

@@ -73,12 +79,12 @@ struct SRFDouble: SRFTag {
double val;
};

struct SRFNone: SRFTag {
struct SRFNone: SRFVal {
void serialize(std::ostream &os) override;
void parse(std::istream &os) override;
};

struct SRFByteArray: SRFTag {
struct SRFByteArray: SRFVal {
SRFByteArray() = default;
SRFByteArray(std::vector<uint8_t> v): val(v) {}

@@ -88,7 +94,7 @@ struct SRFByteArray: SRFTag {
std::vector<uint8_t> val;
};

struct SRFWordArray: SRFTag {
struct SRFWordArray: SRFVal {
SRFWordArray() = default;
SRFWordArray(std::vector<uint16_t> v): val(v) {}

@@ -98,7 +104,7 @@ struct SRFWordArray: SRFTag {
std::vector<uint16_t> val;
};

struct SRFIntArray: SRFTag {
struct SRFIntArray: SRFVal {
SRFIntArray() = default;
SRFIntArray(std::vector<int32_t> v): val(v) {}

@@ -108,7 +114,7 @@ struct SRFIntArray: SRFTag {
std::vector<int32_t> val;
};

struct SRFFloatArray: SRFTag {
struct SRFFloatArray: SRFVal {
SRFFloatArray() = default;
SRFFloatArray(std::vector<float> v): val(v) {}

@@ -118,7 +124,7 @@ struct SRFFloatArray: SRFTag {
std::vector<float> val;
};

struct SRFDoubleArray: SRFTag {
struct SRFDoubleArray: SRFVal {
SRFDoubleArray() = default;
SRFDoubleArray(std::vector<double> v): val(v) {}


+ 1
- 0
libswan/include/swan/swan.h Целия файл

@@ -7,6 +7,7 @@
#include <swan/Entity.h>
#include <swan/Game.h>
#include <swan/Mod.h>
#include <swan/SRF.h>
#include <swan/Tile.h>
#include <swan/Timer.h>
#include <swan/Vector2.h>

+ 14
- 4
libswan/src/SRF.cc Целия файл

@@ -105,9 +105,9 @@ static std::string readString(std::istream &is) {
return str;
}

SRFTag *SRFTag::read(std::istream &is) {
SRFVal *SRFVal::read(std::istream &is) {
Type type = (Type)readByte(is);
SRFTag *tag;
SRFVal *tag;

switch (type) {
case Type::OBJECT:
@@ -140,6 +140,11 @@ SRFTag *SRFTag::read(std::istream &is) {
return tag;
}

SRFObject::SRFObject(std::initializer_list<std::pair<std::string, SRFVal *>> &lst) {
for (auto &pair: lst)
val[pair.first] = std::unique_ptr<SRFVal>(pair.second);
}

void SRFObject::serialize(std::ostream &os) {
writeByte(os, (uint8_t)Type::OBJECT);
writeInt(os, val.size());
@@ -155,10 +160,15 @@ void SRFObject::parse(std::istream &is) {

for (int32_t i = 0; i < count; ++i) {
std::string key = readString(is);
val[key] = std::unique_ptr<SRFTag>(SRFTag::read(is));
val[key] = std::unique_ptr<SRFVal>(SRFVal::read(is));
}
}

SRFArray::SRFArray(std::initializer_list<SRFVal *> &lst) {
for (auto &tag: lst)
val.push_back(std::unique_ptr<SRFVal>(tag));
}

void SRFArray::serialize(std::ostream &os) {
writeByte(os, (uint8_t)Type::ARRAY);
writeInt(os, val.size());
@@ -173,7 +183,7 @@ void SRFArray::parse(std::istream &is) {
val.resize(count);

for (int32_t i = 0; i < count; ++i) {
val[i] = std::unique_ptr<SRFTag>(SRFTag::read(is));
val[i] = std::unique_ptr<SRFVal>(SRFVal::read(is));
}
}


Loading…
Отказ
Запис