浏览代码

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 <almamu@almamu.com>

Alexis Maiquez 7 年之前
父节点
当前提交
cc74debaad

+ 0 - 29
.idea/codeStyles/Project.xml

@@ -1,29 +0,0 @@
-<component name="ProjectCodeStyleConfiguration">
-  <code_scheme name="Project" version="173">
-    <Objective-C-extensions>
-      <file>
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Import" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Macro" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Typedef" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Enum" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Constant" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Global" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Struct" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="FunctionPredecl" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Function" />
-      </file>
-      <class>
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Property" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Synthesize" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InitMethod" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="StaticMethod" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InstanceMethod" />
-        <option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="DeallocMethod" />
-      </class>
-      <extensions>
-        <pair source="cpp" header="h" fileNamingConvention="NONE" />
-        <pair source="c" header="h" fileNamingConvention="NONE" />
-      </extensions>
-    </Objective-C-extensions>
-  </code_scheme>
-</component>

+ 21 - 14
wallpaperengine/core.cpp

@@ -1,22 +1,29 @@
 #include <irrlicht/fast_atof.h>
 #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 ());
 }

+ 9 - 15
wallpaperengine/core.h

@@ -1,20 +1,14 @@
-#ifndef WALLENGINE_CORE_H
-#define WALLENGINE_CORE_H
+#pragma once
 
-#include <iostream>
-#include <irrlicht/vector3d.h>
-#include <irrlicht/vector2d.h>
-#include <irrlicht/matrix4.h>
+#include <string>
 
-namespace wp
+#include <irrlicht/irrlicht.h>
+
+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

+ 60 - 0
wallpaperengine/core/project.cpp

@@ -0,0 +1,60 @@
+#include <wallpaperengine/fs/utils.h>
+
+#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 <std::string> ().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;
+}

+ 31 - 0
wallpaperengine/core/project.h

@@ -0,0 +1,31 @@
+#pragma once
+
+#include <irrlicht/irrlicht.h>
+#include <nlohmann/json.hpp>
+
+#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;
+    };
+};
+

+ 43 - 0
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;
+}

+ 32 - 0
wallpaperengine/core/scene.h

@@ -0,0 +1,32 @@
+#pragma once
+
+#include <irrlicht/irrlicht.h>
+#include <nlohmann/json.hpp>
+
+#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;
+    };
+};

+ 54 - 0
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)
+    );
+}

+ 25 - 0
wallpaperengine/core/scenes/camera.h

@@ -0,0 +1,25 @@
+#pragma once
+
+#include <nlohmann/json.hpp>
+#include <irrlicht/irrlicht.h>
+
+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;
+    };
+};

+ 1 - 1
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);