Skip to content
Permalink
Browse files

GameData Copy Construct (#1538)

The basic idea is to copy construct the game proto and flatten the tree into vectors using move semantics.
  • Loading branch information...
RobertBColton committed Feb 7, 2019
1 parent 9829c32 commit 2281c1db8f29201a834a7e2ecc97ff90424c6922
Showing with 66 additions and 10 deletions.
  1. +53 −0 CompilerSource/backend/GameData.cpp
  2. +13 −10 CompilerSource/backend/GameData.h
@@ -16,6 +16,8 @@ using std::cout;
using std::cerr;
using std::endl;

using TypeCase = buffers::TreeNode::TypeCase;

static inline uint32_t javaColor(int c) {
if (!(c & 0xFF)) return 0xFFFFFFFF;
return ((c & 0xFF0000) >> 8) | ((c & 0xFF00) << 8) | ((c & 0xFF000000) >> 24);
@@ -46,6 +48,8 @@ struct ESLookup {
object(es->gmObjects, es->gmObjectCount, "object") {}
};

SpriteData::SpriteData(const buffers::resources::Sprite &q, const std::string& name, const std::vector<ImageData>& subimages):
buffers::resources::Sprite(q), name(name), image_data(subimages) {}
SpriteData::SpriteData(const ::Sprite &sprite):
name(sprite.name) {
set_id(sprite.id);
@@ -71,6 +75,8 @@ SpriteData::SpriteData(const ::Sprite &sprite):
image_data.emplace_back(sprite.subImages[i].image);
}

SoundData::SoundData(const buffers::resources::Sound &q, const std::string& name, const BinaryData& data):
buffers::resources::Sound(q), name(name), audio(data) {}
SoundData::SoundData(const ::Sound &sound):
name(sound.name),
audio(sound.data, sound.data + sound.size) {
@@ -90,6 +96,8 @@ SoundData::SoundData(const ::Sound &sound):
set_preload(sound.preload);
}

BackgroundData::BackgroundData(const buffers::resources::Background &q, const std::string& name, const ImageData& image):
buffers::resources::Background(q), name(name), image_data(image) {}
BackgroundData::BackgroundData(const ::Background &background):
name(background.name),
image_data(background.backgroundImage) {
@@ -109,6 +117,8 @@ BackgroundData::BackgroundData(const ::Background &background):
set_vertical_spacing(background.vSep);
}

FontData::FontData(const buffers::resources::Font &q, const std::string& name):
buffers::resources::Font(q), name(name) {}
FontData::FontData(const ::Font &font):
name(font.name) {
set_id(font.id);
@@ -145,6 +155,8 @@ FontData::GlyphData::GlyphData(const ::Glyph &glyph):
metrics.set_height(glyph.height);
}

PathData::PathData(const buffers::resources::Path &q, const std::string& name):
buffers::resources::Path(q), name(name) {}
PathData::PathData(const ::Path &path):
name(path.name) {
set_id(path.id);
@@ -165,12 +177,16 @@ PathData::PathData(const ::Path &path):
}
}

ScriptData::ScriptData(const buffers::resources::Script &q, const std::string& name):
buffers::resources::Script(q), name(name) {}
ScriptData::ScriptData(const ::Script &script):
name(script.name) {
set_id(script.id);
set_code(script.code);
}

ShaderData::ShaderData(const buffers::resources::Shader &q, const std::string& name):
buffers::resources::Shader(q), name(name) {}
ShaderData::ShaderData(const ::Shader &shader):
name(shader.name) {
set_id(shader.id);
@@ -182,6 +198,8 @@ ShaderData::ShaderData(const ::Shader &shader):

}

TimelineData::TimelineData(const buffers::resources::Timeline &q, const std::string& name):
buffers::resources::Timeline(q), name(name) {}
TimelineData::TimelineData(const ::Timeline &timeline):
name(timeline.name) {
set_id(timeline.id);
@@ -193,6 +211,8 @@ TimelineData::TimelineData(const ::Timeline &timeline):
}
}

ObjectData::ObjectData(const buffers::resources::Object &q, const std::string& name):
buffers::resources::Object(q), name(name) {}
ObjectData::ObjectData(const ::GmObject &object, const ESLookup &lookup):
name(object.name) {
set_id(object.id);
@@ -218,6 +238,8 @@ ObjectData::ObjectData(const ::GmObject &object, const ESLookup &lookup):
}
}

RoomData::RoomData(const buffers::resources::Room &q, const std::string& name):
buffers::resources::Room(q), name(name) {}
RoomData::RoomData(const ::Room &room, const ESLookup &lookup):
name(room.name) {
cout << "Import room." << endl;
@@ -422,3 +444,34 @@ GameData::GameData(EnigmaStruct *es): filename(es->filename ?: "") {

cout << "Transfer complete." << endl << endl;
}

GameData::GameData(const buffers::Project &proj): filename("") {
cout << "Flattening tree." << endl;

FlattenTree(proj.game().root());

cout << "Transfer complete." << endl << endl;
}

void GameData::FlattenTree(const buffers::TreeNode &root) {
switch (root.type_case()) {
case TypeCase::kFolder: break;
case TypeCase::kSprite: /*sprites.emplace_back(root.sprite(), root.name());*/ break;
case TypeCase::kSound: /*sounds.emplace_back(root.sound(), root.name());*/ break;
case TypeCase::kBackground: /*backgrounds.emplace_back(root.background(), root.name());*/ break;
case TypeCase::kPath: paths.emplace_back(root.path(), root.name()); break;
case TypeCase::kScript: scripts.emplace_back(root.script(), root.name()); break;
case TypeCase::kShader: shaders.emplace_back(root.shader(), root.name()); break;
case TypeCase::kFont: fonts.emplace_back(root.font(), root.name()); break;
case TypeCase::kTimeline: timelines.emplace_back(root.timeline(), root.name()); break;
case TypeCase::kObject: objects.emplace_back(root.object(), root.name()); break;
case TypeCase::kRoom: rooms.emplace_back(root.room(), root.name()); break;
case TypeCase::kInclude: /*includes.emplace_back(root.include());*/ break;
case TypeCase::kSettings: /*settings.emplace_back(root.settings());*/ break;
default: cout << "- Not transferring " << root.name() << endl; break;
}

for (auto child : root.child()) {
FlattenTree(child);
}
}
@@ -34,15 +34,15 @@ struct SpriteData : buffers::resources::Sprite {
std::string name;
std::vector<ImageData> image_data;

SpriteData(const buffers::resources::Sprite &sprite);
SpriteData(const buffers::resources::Sprite &sprite, const std::string& name, const std::vector<ImageData>& subimages);
SpriteData(const ::Sprite &sprite);
};

struct SoundData : buffers::resources::Sound {
std::string name;
BinaryData audio;

SoundData(const buffers::resources::Sound &sound);
SoundData(const buffers::resources::Sound &sound, const std::string& name, const BinaryData& data);
SoundData(const ::Sound &sound);
};

@@ -51,7 +51,7 @@ struct BackgroundData : buffers::resources::Background {
ImageData image_data;
bool legacy_transparency;

BackgroundData(const buffers::resources::Background &background);
BackgroundData(const buffers::resources::Background &background, const std::string& name, const ImageData& image);
BackgroundData(const ::Background &background);
};

@@ -74,38 +74,38 @@ struct FontData : buffers::resources::Font {
};
std::vector<NormalizedRange> normalized_ranges;

FontData(const buffers::resources::Font &font);
FontData(const buffers::resources::Font &font, const std::string& name);
FontData(const ::Font &font);
};

struct PathData : buffers::resources::Path {
std::string name;
PathData(const buffers::resources::Path &q);
PathData(const buffers::resources::Path &q, const std::string& name);
PathData(const ::Path &path);
};
struct ScriptData : buffers::resources::Script {
std::string name;
ScriptData(const buffers::resources::Script &q);
ScriptData(const buffers::resources::Script &q, const std::string& name);
ScriptData(const ::Script &script);
};
struct ShaderData : buffers::resources::Shader {
std::string name;
ShaderData(const buffers::resources::Shader &q);
ShaderData(const buffers::resources::Shader &q, const std::string& name);
ShaderData(const ::Shader &shader);
};
struct TimelineData : buffers::resources::Timeline {
std::string name;
TimelineData(const buffers::resources::Timeline &q);
TimelineData(const buffers::resources::Timeline &q, const std::string& name);
TimelineData(const ::Timeline &timeline);
};
struct ObjectData : buffers::resources::Object {
std::string name;
ObjectData(const buffers::resources::Object &q);
ObjectData(const buffers::resources::Object &q, const std::string& name);
ObjectData(const ::GmObject &object, const ESLookup &lookup);
};
struct RoomData : buffers::resources::Room {
std::string name;
RoomData(const buffers::resources::Room &q);
RoomData(const buffers::resources::Room &q, const std::string& name);
RoomData(const ::Room &room, const ESLookup &lookup);
};

@@ -146,6 +146,9 @@ struct GameData {

GameData(struct EnigmaStruct *es);
GameData(const buffers::Project &proj);

private:
void FlattenTree(const buffers::TreeNode &root);
};

#endif

0 comments on commit 2281c1d

Please sign in to comment.
You can’t perform that action at this time.