Skip to content

Commit

Permalink
Add some serialization methods
Browse files Browse the repository at this point in the history
  • Loading branch information
anticitizn committed Jan 17, 2023
1 parent 88ccc4d commit 7f284bb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
17 changes: 9 additions & 8 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ extern Coordinator coordinator;

int main(int argc, char *argv[])
{
pugi::xml_document doc;
auto declarationNode = doc.append_child(pugi::node_declaration);

auto root = doc.append_child("root");
auto nodeChild = root.append_child("child1");
nodeChild.append_child(pugi::node_pcdata).set_value(to_string(15.374).c_str());
doc.save_file("test2.xml", PUGIXML_TEXT(" "));

coordinator.RegisterComponent<Texture>();
coordinator.RegisterComponent<Quad>();

Expand All @@ -50,13 +42,22 @@ int main(int argc, char *argv[])
coordinator.SetSystemSignature<MovingSystem>(signature);
}

Entity testEntity;
for (int i = 0; i < 10; i++)
{
Entity entity = coordinator.CreateEntity();
testEntity = entity;
coordinator.AddComponent<Quad>(entity, Quad {50.0f * i, 50.0f * i, 0, 50, 50, 255, 255, 255, 0});
coordinator.AddComponent<Texture>(entity, Texture{"", 0});
}

Quad& quad = coordinator.GetComponent<Quad>(testEntity);
pugi::xml_document doc;
auto declarationNode = doc.append_child(pugi::node_declaration);

auto root = doc.append_child("root");
quad.archive(root);
doc.save_file("test.xml", PUGIXML_TEXT(" "));
while(true)
{
movingSystem->Update();
Expand Down
27 changes: 27 additions & 0 deletions src/components/Quad.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

#pragma once

#include <external/pugixml/pugixml.hpp>

#include <src/utils/macros.hpp>

struct Quad
{
float posX;
Expand All @@ -12,4 +16,27 @@ struct Quad
float g;
float b;
float rot;

void archive(pugi::xml_node& root)
{
pugi::xml_node quad = root.append_child(VAR_STR(posX));

archiveVar<float>(quad, posX);
archiveVar<float>(quad, posY);
archiveVar<float>(quad, posZ);
archiveVar<float>(quad, sizeX);
archiveVar<float>(quad, sizeY);
archiveVar<float>(quad, r);
archiveVar<float>(quad, g);
archiveVar<float>(quad, b);
archiveVar<float>(quad, rot);
}

// Convert to macro to preserve variable name
template <class T>
void archiveVar(pugi::xml_node& root, T t)
{
pugi::xml_node nodeChild = root.append_child(VAR_STR(t));
nodeChild.append_child(pugi::node_pcdata).set_value(to_string(t).c_str());
}
};
4 changes: 4 additions & 0 deletions src/utils/macros.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#pragma once

#define VAR_STR(Variable) (void(Variable),#Variable)

0 comments on commit 7f284bb

Please sign in to comment.