diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a8c840..b27f136 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,12 @@ option(VECTOR_3D_BUILD_EXECUTABLE "Build the executable" ON) add_subdirectory(include/plog EXCLUDE_FROM_ALL) add_definitions(-DBOOST_ALL_NO_LIB) -find_package(Boost REQUIRED) +set(Boost_USE_STATIC_LIBS ON) +set(Boost_USE_DEBUG_LIBS OFF) +set(Boost_USE_RELEASE_LIBS ON) +set(Boost_USE_MULTITHREADED ON) +set(Boost_USE_STATIC_RUNTIME OFF) +find_package(Boost REQUIRED COMPONENTS serialization) # Vulkan set(Vulkan_ROOT "$ENV{VULKAN_SDK}") @@ -67,7 +72,7 @@ message(STATUS "CHRONO_STATIC = ${CHRONO_STATIC}") # ENDIF() SET(ENGINE_INCLUDE ${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/include/OBJ-Loader-master/Source ${CMAKE_SOURCE_DIR}/src/thirdparty/imgui ${Boost_INCLUDE_DIRS} ${Chrono_INCLUDE_DIR} ${EiGEN3_INCLUDE_DIR} ${CHRONO_INCLUDE_DIRS}) -set(vector_3d_LIBS glfw ${GLFW_LIBRARIES} ${OPENGL_LIBRARIES} Vulkan::Vulkan glm::glm plog ${CHRONO_TARGETS} ${WINMM_LIB_PATH} ${CHRONO_LIBRARIES} ${X11_X11_LIB} assimp) +set(vector_3d_LIBS glfw ${GLFW_LIBRARIES} ${OPENGL_LIBRARIES} Vulkan::Vulkan glm::glm plog ${CHRONO_TARGETS} ${WINMM_LIB_PATH} ${CHRONO_LIBRARIES} ${X11_X11_LIB} assimp ${Boost_LIBRARIES}) set(RESOURCES_DIR ${CMAKE_SOURCE_DIR}/resources) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6169b3b..a929d60 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -30,6 +30,7 @@ set(vector_3d_core_HEADERS ${CMAKE_SOURCE_DIR}/src/object_ptr.hpp ${CMAKE_SOURCE_DIR}/src/observer_ptr.hpp ${CMAKE_SOURCE_DIR}/src/scene.h + ${CMAKE_SOURCE_DIR}/src/serialization.hpp ${CMAKE_SOURCE_DIR}/src/transform.h ${CMAKE_SOURCE_DIR}/src/window.h ) @@ -75,6 +76,7 @@ set(vector_3d_editor_FILES message(STATUS "-- INPUT") set(vector_3d_input_SOURCES + ${CMAKE_SOURCE_DIR}/src/input/KeyboardDevice.cpp ) set(vector_3d_input_HEADERS @@ -82,7 +84,7 @@ set(vector_3d_input_HEADERS ${CMAKE_SOURCE_DIR}/src/input/InputKeyCodes.hpp ${CMAKE_SOURCE_DIR}/src/input/InputKeys.hpp ${CMAKE_SOURCE_DIR}/src/input/InputManager.h - ${CMAKE_SOURCE_DIR}/src/input/KeyboardDevice.hpp + ${CMAKE_SOURCE_DIR}/src/input/KeyboardDevice.h ) set(vector_3d_input_FILES diff --git a/src/engine.cpp b/src/engine.cpp index 0c607e2..f5761b8 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -16,7 +17,7 @@ #include "backends/imgui_impl_glfw.h" #include "backends/imgui_impl_opengl3.h" #include "imgui.h" -#include "input/KeyboardDevice.hpp" +#include "input/KeyboardDevice.h" #include "physics/Vehicle.h" #include "physics/VehicleInteractiveController.h" #include "physics/collider.h" @@ -24,6 +25,7 @@ #include "plog/Severity.h" #include "rendering/mesh_renderer.h" #include "rendering/null_graphics_backend.hpp" +#include "serialization.hpp" #include "transform.h" // ------------------------------- TEMP ---------------------------------- @@ -295,6 +297,18 @@ void Engine::mainLoop() { if (ImGui::InputInt("Target FPS", &targetFPS, 1, 10)) { m_targetFrameRate = targetFPS; } + ImGui::Spacing(); + + if (ImGui::Button("Test save keyboard bindings")) { + m_inputManager.storeDevice(0, "KeyboardConfig.txt"); + } + if (ImGui::Button("Test save scene")) { + saveScene("testSceneSave.xml"); + } + if (ImGui::Button("Test load scene")) { + loadScene("testSceneSave.xml"); + } + ImGui::Spacing(); if (ImGui::CollapsingHeader("Physics")) m_phSystem.renderDebbugGUI(); ImGui::Spacing(); @@ -345,4 +359,18 @@ void Engine::registerComponents( } } +void Engine::saveScene(std::string filename) { + std::ofstream ofs(filename); + boost::archive::text_oarchive oa(ofs); + oa << boost::serialization::make_nvp("scene", m_scene); +} + +void Engine::loadScene(std::string filename) { + std::ifstream ifs(filename); + boost::archive::text_iarchive ia(ifs); + Scene* scene; + ia >> boost::serialization::make_nvp("scene", m_scene); + scene->init(); +} + } // namespace v3d diff --git a/src/engine.h b/src/engine.h index 909e00a..9e45b22 100644 --- a/src/engine.h +++ b/src/engine.h @@ -125,5 +125,8 @@ class Engine { /// @param componentRegistry void registerComponents(Scene* scene, editor::EditorComponentRegistry* componentRegistry); + + void saveScene(std::string filename); + void loadScene(std::string filename); }; } // namespace v3d diff --git a/src/entity.h b/src/entity.h index 718283b..7413ff4 100644 --- a/src/entity.h +++ b/src/entity.h @@ -14,6 +14,7 @@ class RigidBody; class Entity : public IEditorGUISelectable { friend class Scene; + friend class boost::serialization::access; public: std::string getName() const { return m_name; } @@ -110,6 +111,15 @@ class Entity : public IEditorGUISelectable { private: void removeChild(entity_ptr child); void addChild(entity_ptr child); + + template + void serialize(Archive& ar, const unsigned int version) { + ar & m_id; + ar & m_name; + ar & m_parent; + ar & m_childs; + ar & m_components; + } }; } // namespace v3d diff --git a/src/input/InputDevice.hpp b/src/input/InputDevice.hpp index 4faceb8..6ce868b 100644 --- a/src/input/InputDevice.hpp +++ b/src/input/InputDevice.hpp @@ -5,6 +5,7 @@ #include #include +#include "serialization.hpp" #include "utils/utils.hpp" #include "window.h" @@ -19,6 +20,11 @@ struct InputKey { constexpr bool operator!=(const InputKey& other) const noexcept { return code != other.code; } + + template + void serialize(Archive& ar, const unsigned int version) { + ar & code; + } }; struct InputKeyResult { @@ -41,6 +47,11 @@ struct InputAction { constexpr bool operator!=(const InputAction& other) const noexcept { return code != other.code; } + + template + void serialize(Archive& ar, const unsigned int version) { + ar & code; + } }; struct InputActionHasher { @@ -57,9 +68,17 @@ inline constexpr InputAction makeInputActionID(std::string_view name) { struct InputMap { InputAction m_action; InputKey m_key; + + template + void serialize(Archive& ar, const unsigned int version) { + ar & m_key; + ar & m_action; + } }; class InputProfile { + friend class boost::serialization::access; + public: InputProfile() = default; ~InputProfile() = default; @@ -74,6 +93,11 @@ class InputProfile { private: std::unordered_map m_mappings; + + template + void serialize(Archive& ar, const unsigned int version) { + ar & m_mappings; + } }; enum InputDeviceType { @@ -84,6 +108,8 @@ enum InputDeviceType { }; class InputDevice { + friend class boost::serialization::access; + public: InputDevice(Window* window, InputProfile profile) : m_window(window), m_profile(std::move(profile)) {}; @@ -106,6 +132,12 @@ class InputDevice { Window* m_window; InputProfile m_profile; bool muted = false; + + template + void serialize(Archive& ar, const unsigned int version) { + ar & m_profile; + ar & m_profile; + } }; } // namespace input diff --git a/src/input/InputManager.h b/src/input/InputManager.h index b92222a..112f5e9 100644 --- a/src/input/InputManager.h +++ b/src/input/InputManager.h @@ -1,6 +1,10 @@ #pragma once +#include + #include "input/InputDevice.hpp" +#include "input/KeyboardDevice.h" +#include namespace v3d { class Engine; @@ -52,5 +56,12 @@ class InputManager { inline std::size_t getNumDevices() const noexcept { return m_devices.size(); } + + void storeDevice(uint8_t deviceId, std::string filename){ + std::ofstream ofs(filename); + boost::archive::text_oarchive oa(ofs); + oa.register_type(); + oa << m_devices[deviceId]; + } }; } // namespace v3d diff --git a/src/input/KeyboardDevice.cpp b/src/input/KeyboardDevice.cpp new file mode 100644 index 0000000..a2063a5 --- /dev/null +++ b/src/input/KeyboardDevice.cpp @@ -0,0 +1,5 @@ +// #include "input/KeyboardDevice.h" + +// #include + +// BOOST_CLASS_EXPORT_GUID(v3d::input::KeyboardDevice, "input::KeyboardDevice") diff --git a/src/input/KeyboardDevice.hpp b/src/input/KeyboardDevice.h similarity index 80% rename from src/input/KeyboardDevice.hpp rename to src/input/KeyboardDevice.h index 7ae4f38..637623f 100644 --- a/src/input/KeyboardDevice.hpp +++ b/src/input/KeyboardDevice.h @@ -1,11 +1,15 @@ #pragma once #include "input/InputKeys.hpp" +#include namespace v3d { namespace input { class KeyboardDevice : public InputDevice { + friend class boost::serialization::access; + public: + KeyboardDevice() : InputDevice(nullptr, InputProfile()) {} KeyboardDevice(Window* window, InputProfile profile) : InputDevice(window, profile) {} void update() override { @@ -48,6 +52,12 @@ class KeyboardDevice : public InputDevice { private: // std::unordered_map m_keyStates; + + template + void serialize(Archive& ar, const unsigned int version) { + // serialize base class information + ar& boost::serialization::base_object(*this); + } }; } // namespace input diff --git a/src/object_ptr.hpp b/src/object_ptr.hpp index ad038db..a3527d8 100644 --- a/src/object_ptr.hpp +++ b/src/object_ptr.hpp @@ -6,6 +6,7 @@ #include #include #include +#include namespace v3d { class Entity; @@ -13,6 +14,8 @@ class Scene; template class object_ptr { + friend class boost::serialization::access; + public: object_ptr() : m_vec(nullptr) {} object_ptr(Container& vec, key index) : m_vec(&vec), m_index(index) { @@ -130,6 +133,12 @@ class object_ptr { private: Container* m_vec; key m_index; + + template + void serialize(Archive& ar, const unsigned int version) { + ar & m_index; + ar & m_vec; + } }; } // namespace v3d diff --git a/src/scene.h b/src/scene.h index b98894e..6ff0dbe 100644 --- a/src/scene.h +++ b/src/scene.h @@ -2,6 +2,10 @@ #include #include +<<<<<<< HEAD +======= +#include +>>>>>>> 329ddcc (WIP: Serialization) #include #include #include @@ -13,6 +17,7 @@ #include "component.h" #include "entity.h" #include "object_ptr.hpp" +#include "serialization.hpp" #include "utils/utils.hpp" namespace v3d { @@ -23,6 +28,7 @@ const uint8_t MAX_ENTITY_NESTED_DEPTH = 255; class Scene { friend class Engine; + friend class boost::serialization::access; struct Private { explicit Private() = default; @@ -34,6 +40,7 @@ class Scene { * @param */ Scene(Private) {}; + Scene() {}; ~Scene() {}; /** @@ -116,7 +123,11 @@ class Scene { componentID_t insertEntityComponent( entity_ptr entity, std::unique_ptr component) { return insertEntityComponent(entity, std::move(component), +<<<<<<< HEAD boost::uuids::random_generator()()); +======= + boost::uuids::random_generator()()); +>>>>>>> 329ddcc (WIP: Serialization) } template @@ -233,6 +244,43 @@ class Scene { EntityMap m_entities; ComponentMap m_components; + template + void serialize(Archive & ar, const unsigned int version) + { + int degrees = 32, minutes = 64, seconds = 128, v = version; + ar & BOOST_SERIALIZATION_NVP(degrees); + ar & BOOST_SERIALIZATION_NVP(minutes); + ar & BOOST_SERIALIZATION_NVP(seconds); + ar & BOOST_SERIALIZATION_NVP(v); + } + + // template + // void serialize(Archive& ar, const unsigned int version) { + // // ar & m_components; + // // ar & m_entities; + // // ar & m_root; + // std::cout << "Serializing file, version: " << version << "\n" ; + // int pollo = 1024; + // ar & pollo; + // } + + // template + // void save(Archive& ar, const unsigned int version) const { + // ar << m_entities; + // ar << m_root; + // } + + // template + // void load(Archive& ar, const unsigned int version) { + // ar >> m_entities; + // ar >> m_root; + // } + + // template + // void serialize(Archive& ar, const unsigned int file_version) { + // boost::serialization::split_member(ar, *this, file_version); + // } + void init(); entity_ptr createEntity() { @@ -263,5 +311,6 @@ class Scene { }); } }; - } // namespace v3d + +// BOOST_CLASS_VERSION(v3d::Scene, 1); diff --git a/src/serialization.hpp b/src/serialization.hpp new file mode 100644 index 0000000..4d93277 --- /dev/null +++ b/src/serialization.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace v3d +{ + +} // namespace v3d