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>
This commit is contained in:
Alexis Maiquez 2019-08-13 17:17:08 +02:00
parent bdf37b1952
commit cc74debaad
10 changed files with 276 additions and 59 deletions

View File

@ -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>

View File

@ -1,22 +1,29 @@
#include <irrlicht/fast_atof.h> #include <irrlicht/fast_atof.h>
#include "core.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 x = irr::core::fast_atof (str, &str); while (*str == ' ') str ++; irr::f32 z = irr::core::fast_atof (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::core::vector2df wp::core::ato2vf (const char *str)
{ {
irr::f32 x = irr::core::fast_atof (str, &str); while (*str == ' ') str ++; irr::f32 x = irr::core::fast_atof (str, &str); while (*str == ' ') str ++;
irr::f32 y = irr::core::fast_atof (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 ());
} }

View File

@ -1,20 +1,14 @@
#ifndef WALLENGINE_CORE_H #pragma once
#define WALLENGINE_CORE_H
#include <iostream> #include <string>
#include <irrlicht/vector3d.h>
#include <irrlicht/vector2d.h>
#include <irrlicht/matrix4.h>
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 irr::core::vector3df ato3vf (const std::string& str);
{ irr::core::vector2df ato2vf (const std::string& str);
public:
static irr::core::vector3df ato3vf (const char *str);
static irr::core::vector2df ato2vf (const char *str);
};
}; };
#endif //WALLENGINE_CORE_H

View File

@ -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;
}

View File

@ -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;
};
};

View File

@ -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;
}

View File

@ -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;
};
};

View File

@ -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)
);
}

View File

@ -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;
};
};

View File

@ -16,7 +16,7 @@ namespace wp
irr::io::IReadFile* reader = wp::irrlicht::device->getFileSystem ()->createAndOpenFile (file); irr::io::IReadFile* reader = wp::irrlicht::device->getFileSystem ()->createAndOpenFile (file);
if (reader == NULL) 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]; char* filedata = new char [reader->getSize () + 1];
memset (filedata, 0, reader->getSize () + 1); memset (filedata, 0, reader->getSize () + 1);