Skip to content

Commit

Permalink
save stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioJPinto committed May 27, 2024
1 parent 669f0b8 commit 7799b9c
Show file tree
Hide file tree
Showing 6 changed files with 40,718 additions and 4 deletions.
12 changes: 12 additions & 0 deletions engine/include/save.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef SAVE_HPP
#define SAVE_HPP

#include <rapidxml/rapidxml.hpp>
#include <rapidxml/rapidxml_ext.hpp>
#include <rapidxml/rapidxml_print.hpp>
#include "parse.hpp"
#include "Camera.hpp"

void savetoFile(std::string original_scene, const Camera& cam);

#endif
12 changes: 11 additions & 1 deletion engine/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extern "C" {
#include "parse.hpp"
#include "input.hpp"
#include "controller.hpp"
#include "save.hpp"

std::string filename;
bool simple = false;
Expand Down Expand Up @@ -124,6 +125,10 @@ void setupModels(Group& group) {
}
}

void saveCurrent() {
savetoFile(filename, camera);
}

void hotReload() {
setupConfig((char*) filename.c_str());
setupModels(c.group);
Expand Down Expand Up @@ -172,10 +177,15 @@ void renderMenu() {
resetCamera();
}
ImGui::SameLine();
ImGui::Button("Hot Reload", ImVec2(100, 20));
ImGui::Button("Hot Reload", ImVec2(80, 20));
if (ImGui::IsItemClicked()) {
hotReload();
}
ImGui::SameLine();
ImGui::Button("Save", ImVec2(50, 20));
if (ImGui::IsItemClicked()) {
saveCurrent();
}
ImGui::End();
}
if(models_menus){
Expand Down
1 change: 0 additions & 1 deletion engine/src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ Configuration parseConfig(std::string filename) {

// camera information
rapidxml::xml_node<>* camera = root->first_node("camera");

rapidxml::xml_node<>* position_n = camera->first_node("position");
glm::vec3 position = glm::vec3(std::stof(position_n->first_attribute("x")->value()),
std::stof(position_n->first_attribute("y")->value()),
Expand Down
42 changes: 42 additions & 0 deletions engine/src/save.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "save.hpp"

void savetoFile(std::string original_scene, const Camera& cam) {
std::ifstream file(original_scene);
std::string xmlContent((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
file.close();

rapidxml::xml_document<> doc;
doc.parse<0>(&xmlContent[0]);

rapidxml::xml_node<>* root = doc.first_node("world");
rapidxml::xml_node<>* camera = root->first_node("camera");
rapidxml::xml_node<>* position_n = camera->first_node("position");
rapidxml::xml_node<>* lookat_n = camera->first_node("lookAt");

auto set_or_update_attribute = [&doc](rapidxml::xml_node<>* node, const char* attr_name, float value) {
rapidxml::xml_attribute<>* attr = node->first_attribute(attr_name);
if (!attr) {
char* attr_value = doc.allocate_string(std::to_string(value).c_str());
attr = doc.allocate_attribute(attr_name, attr_value);
node->append_attribute(attr);
} else {
attr->value(doc.allocate_string(std::to_string(value).c_str()));
}
};

set_or_update_attribute(position_n, "x", cam.position.x);
set_or_update_attribute(position_n, "y", cam.position.y);
set_or_update_attribute(position_n, "z", cam.position.z);

glm::vec3 lookat = cam.position + cam.forward;

set_or_update_attribute(lookat_n, "x", lookat.x);
set_or_update_attribute(lookat_n, "y", lookat.y);
set_or_update_attribute(lookat_n, "z", lookat.z);

std::ofstream out_file("scenes/last_scene.xml");

out_file << doc;
out_file.close();
}
4 changes: 2 additions & 2 deletions imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ Size=325,139
Collapsed=0

[Window][Options]
Pos=15,169
Pos=16,168
Size=312,124
Collapsed=0

[Window][Models]
Pos=17,306
Size=254,69
Size=297,183
Collapsed=0

Loading

0 comments on commit 7799b9c

Please sign in to comment.