From cc74debaade792d96e384d777183f94fcf55c446 Mon Sep 17 00:00:00 2001 From: Alexis Maiquez Date: Tue, 13 Aug 2019 17:17:08 +0200 Subject: [PATCH] Started rewrite of the wallpaper interpreter to aim for a more separated approach, this way the whole JSON data can be parsed and kept separeted from the actual rendering logic # Implemented basic project file loading # Implemented basic camera settings loading Signed-off-by: Alexis Maiquez --- .idea/codeStyles/Project.xml | 29 ------------- wallpaperengine/core.cpp | 35 +++++++++------ wallpaperengine/core.h | 24 ++++------- wallpaperengine/core/project.cpp | 60 ++++++++++++++++++++++++++ wallpaperengine/core/project.h | 31 +++++++++++++ wallpaperengine/core/scene.cpp | 43 ++++++++++++++++++ wallpaperengine/core/scene.h | 32 ++++++++++++++ wallpaperengine/core/scenes/camera.cpp | 54 +++++++++++++++++++++++ wallpaperengine/core/scenes/camera.h | 25 +++++++++++ wallpaperengine/fs/utils.cpp | 2 +- 10 files changed, 276 insertions(+), 59 deletions(-) delete mode 100644 .idea/codeStyles/Project.xml create mode 100644 wallpaperengine/core/project.cpp create mode 100644 wallpaperengine/core/project.h create mode 100644 wallpaperengine/core/scene.cpp create mode 100644 wallpaperengine/core/scene.h create mode 100644 wallpaperengine/core/scenes/camera.cpp create mode 100644 wallpaperengine/core/scenes/camera.h diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml deleted file mode 100644 index 30aa626..0000000 --- a/.idea/codeStyles/Project.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/wallpaperengine/core.cpp b/wallpaperengine/core.cpp index 2668b96..46ffd8f 100644 --- a/wallpaperengine/core.cpp +++ b/wallpaperengine/core.cpp @@ -1,22 +1,29 @@ #include #include "core.h" -namespace wp +irr::core::vector3df wp::core::ato3vf(const char *str) { - irr::core::vector3df core::ato3vf(const char *str) - { - irr::f32 x = irr::core::fast_atof (str, &str); while (*str == ' ') str ++; - irr::f32 y = irr::core::fast_atof (str, &str); while (*str == ' ') str ++; - irr::f32 z = irr::core::fast_atof (str, &str); + irr::f32 x = irr::core::fast_atof (str, &str); while (*str == ' ') str ++; + irr::f32 y = irr::core::fast_atof (str, &str); while (*str == ' ') str ++; + irr::f32 z = irr::core::fast_atof (str, &str); - return irr::core::vector3df (x, y, z); - } + return irr::core::vector3df (x, y, z); +} - irr::core::vector2df core::ato2vf (const char *str) - { - irr::f32 x = irr::core::fast_atof (str, &str); while (*str == ' ') str ++; - irr::f32 y = irr::core::fast_atof (str, &str); +irr::core::vector2df wp::core::ato2vf (const char *str) +{ + irr::f32 x = irr::core::fast_atof (str, &str); while (*str == ' ') str ++; + irr::f32 y = irr::core::fast_atof (str, &str); - return irr::core::vector2df (x, y); - } + return irr::core::vector2df (x, y); +} + +irr::core::vector3df wp::core::ato3vf (const std::string& str) +{ + return wp::core::ato3vf (str.c_str ()); +} + +irr::core::vector2df wp::core::ato2vf (const std::string& str) +{ + return wp::core::ato2vf (str.c_str ()); } \ No newline at end of file diff --git a/wallpaperengine/core.h b/wallpaperengine/core.h index 43384b2..3b8a213 100644 --- a/wallpaperengine/core.h +++ b/wallpaperengine/core.h @@ -1,20 +1,14 @@ -#ifndef WALLENGINE_CORE_H -#define WALLENGINE_CORE_H +#pragma once -#include -#include -#include -#include +#include -namespace wp +#include + +namespace wp::core { + irr::core::vector3df ato3vf (const char *str); + irr::core::vector2df ato2vf (const char *str); - class core - { - public: - static irr::core::vector3df ato3vf (const char *str); - static irr::core::vector2df ato2vf (const char *str); - }; + irr::core::vector3df ato3vf (const std::string& str); + irr::core::vector2df ato2vf (const std::string& str); }; - -#endif //WALLENGINE_CORE_H diff --git a/wallpaperengine/core/project.cpp b/wallpaperengine/core/project.cpp new file mode 100644 index 0000000..b1b6d4c --- /dev/null +++ b/wallpaperengine/core/project.cpp @@ -0,0 +1,60 @@ +#include + +#include "project.h" + +#include "../fs/utils.h" + +using namespace wp::core; + +project::project (std::string title, std::string type, scene *scene) : + m_title (std::move (title)), + m_type (std::move (type)), + m_scene (scene) +{ + this->m_scene->setProject (this); +} + +project* project::fromFile (const irr::io::path& filename) +{ + json content = json::parse (wp::fs::utils::loadFullFile (filename)); + + json::const_iterator title = content.find ("title"); + json::const_iterator type = content.find ("type"); + json::const_iterator file = content.find ("file"); + + if (title == content.end ()) + { + throw std::runtime_error ("Project title missing"); + } + + if (type == content.end ()) + { + throw std::runtime_error ("Project type missing"); + } + + if (file == content.end ()) + { + throw std::runtime_error ("Project's main file missing"); + } + + return new project ( + *title, + *type, + scene::fromFile ((*file).get ().c_str ()) + ); +} + +scene* project::getScene () +{ + return this->m_scene; +} + +std::string project::getTitle () +{ + return this->m_title; +} + +std::string project::getType () +{ + return this->m_type; +} \ No newline at end of file diff --git a/wallpaperengine/core/project.h b/wallpaperengine/core/project.h new file mode 100644 index 0000000..c079c2c --- /dev/null +++ b/wallpaperengine/core/project.h @@ -0,0 +1,31 @@ +#pragma once + +#include +#include + +#include "scene.h" + +namespace wp::core +{ + using json = nlohmann::json; + + class scene; + + class project + { + public: + static project* fromFile (const irr::io::path& filename); + + scene* getScene (); + + std::string getTitle (); + std::string getType (); + protected: + project (std::string title, std::string type, scene* scene); + private: + std::string m_title; + std::string m_type; + scene* m_scene; + }; +}; + diff --git a/wallpaperengine/core/scene.cpp b/wallpaperengine/core/scene.cpp new file mode 100644 index 0000000..fe724ff --- /dev/null +++ b/wallpaperengine/core/scene.cpp @@ -0,0 +1,43 @@ +#include "scene.h" +#include "project.h" + +#include "../fs/utils.h" + +using namespace wp::core; + +scene::scene (scenes::camera* camera) : + m_camera (camera) +{ + +} + +scene* scene::fromFile (irr::io::path filename) +{ + json content = json::parse (wp::fs::utils::loadFullFile (filename)); + + json::const_iterator camera_it = content.find ("camera"); + + if (camera_it == content.end ()) + { + throw std::runtime_error ("Scenes must have a defined camera"); + } + + return new scene ( + scenes::camera::fromJSON (*camera_it) + ); +} + +project* scene::getProject () +{ + return this->m_project; +} + +void scene::setProject (project* project) +{ + this->m_project = project; +} + +scenes::camera* scene::getCamera () +{ + return this->m_camera; +} \ No newline at end of file diff --git a/wallpaperengine/core/scene.h b/wallpaperengine/core/scene.h new file mode 100644 index 0000000..35626c3 --- /dev/null +++ b/wallpaperengine/core/scene.h @@ -0,0 +1,32 @@ +#pragma once + +#include +#include + +#include "project.h" +#include "scenes/camera.h" + +namespace wp::core +{ + using json = nlohmann::json; + + class project; + + class scene + { + public: + static scene* fromFile (irr::io::path filename); + + project* getProject (); + protected: + friend class project; + + void setProject (project* project); + scenes::camera* getCamera (); + + scene (scenes::camera* camera); + private: + project* m_project; + scenes::camera* m_camera; + }; +}; diff --git a/wallpaperengine/core/scenes/camera.cpp b/wallpaperengine/core/scenes/camera.cpp new file mode 100644 index 0000000..be293c2 --- /dev/null +++ b/wallpaperengine/core/scenes/camera.cpp @@ -0,0 +1,54 @@ +#include "camera.h" +#include "../../core.h" + +using namespace wp::core::scenes; + +camera::camera (irr::core::vector3df center, irr::core::vector3df eye, irr::core::vector3df up) : + m_center (center), + m_eye (eye), + m_up (up) +{ +} + +irr::core::vector3df* camera::getCenter () +{ + return &this->m_center; +} + +irr::core::vector3df* camera::getEye () +{ + return &this->m_eye; +} + +irr::core::vector3df* camera::getUp () +{ + return &this->m_up; +} + +camera* camera::fromJSON (json data) +{ + json::const_iterator center_it = data.find ("center"); + json::const_iterator eye_it = data.find ("eye"); + json::const_iterator up_it = data.find ("up"); + + if (center_it == data.end ()) + { + throw std::runtime_error ("Camera must have a center position"); + } + + if (eye_it == data.end ()) + { + throw std::runtime_error ("Camera must have an eye position"); + } + + if (up_it == data.end ()) + { + throw std::runtime_error ("Camera must have a up position"); + } + + return new camera ( + wp::core::ato3vf (*center_it), + wp::core::ato3vf (*eye_it), + wp::core::ato3vf (*up_it) + ); +} \ No newline at end of file diff --git a/wallpaperengine/core/scenes/camera.h b/wallpaperengine/core/scenes/camera.h new file mode 100644 index 0000000..61b078a --- /dev/null +++ b/wallpaperengine/core/scenes/camera.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include + +namespace wp::core::scenes +{ + using json = nlohmann::json; + + class camera + { + public: + static camera* fromJSON (json data); + + irr::core::vector3df* getCenter (); + irr::core::vector3df* getEye (); + irr::core::vector3df* getUp (); + protected: + camera (irr::core::vector3df center, irr::core::vector3df eye, irr::core::vector3df up); + private: + irr::core::vector3df m_center; + irr::core::vector3df m_eye; + irr::core::vector3df m_up; + }; +}; diff --git a/wallpaperengine/fs/utils.cpp b/wallpaperengine/fs/utils.cpp index 4235aca..c4fd71a 100644 --- a/wallpaperengine/fs/utils.cpp +++ b/wallpaperengine/fs/utils.cpp @@ -16,7 +16,7 @@ namespace wp irr::io::IReadFile* reader = wp::irrlicht::device->getFileSystem ()->createAndOpenFile (file); if (reader == NULL) - return ""; + throw std::runtime_error ("Cannot open file " + std::string (file.c_str ()) + " for reading"); char* filedata = new char [reader->getSize () + 1]; memset (filedata, 0, reader->getSize () + 1);