Initial commit - restart from existing code
This commit is contained in:
25
src/Engine/Configurations/Configuration/CConfiguration.cpp
Normal file
25
src/Engine/Configurations/Configuration/CConfiguration.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include "CConfiguration.hpp"
|
||||
#include "nlohmann/json.hpp"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
|
||||
namespace CosmicCore {
|
||||
CModuleConfig CConfiguration::moduleConfiguration;
|
||||
|
||||
|
||||
void CModuleConfig::parse(nlohmann::json& jsonModule)
|
||||
{
|
||||
modulesDirectory = jsonModule["modulesDirectory"].get<std::filesystem::path>();
|
||||
}
|
||||
|
||||
void CConfiguration::init(std::filesystem::path configFilePath)
|
||||
{
|
||||
std::ifstream fileConf(configFilePath);
|
||||
auto json = nlohmann::json::parse(fileConf);
|
||||
if(json.contains("Modules"))
|
||||
{
|
||||
moduleConfiguration.parse(json["Modules"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
22
src/Engine/Configurations/Configuration/CConfiguration.hpp
Normal file
22
src/Engine/Configurations/Configuration/CConfiguration.hpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef CCONFIGURATION_HPP
|
||||
#define CCONFIGURATION_HPP
|
||||
|
||||
#include "nlohmann/json_fwd.hpp"
|
||||
#include <filesystem>
|
||||
namespace CosmicCore {
|
||||
class CModuleConfig{
|
||||
private:
|
||||
std::filesystem::path modulesDirectory;
|
||||
public:
|
||||
std::filesystem::path getModulesDirectory(){return modulesDirectory;};
|
||||
void parse(nlohmann::json& jsonModule);
|
||||
};
|
||||
|
||||
class CConfiguration
|
||||
{
|
||||
public:
|
||||
static CModuleConfig moduleConfiguration;
|
||||
static void init(std::filesystem::path configFilePath = "engineConfiguration.json");
|
||||
};
|
||||
}
|
||||
#endif
|
||||
25
src/Engine/Core/Component/CAbstractComponent.cpp
Normal file
25
src/Engine/Core/Component/CAbstractComponent.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include "CAbstractComponent.hpp"
|
||||
|
||||
namespace CosmicCore {
|
||||
constexpr std::string typeToString(EComponentType type)
|
||||
{
|
||||
switch (type) {
|
||||
case EComponentType::COMPONENT_SCRIPT:
|
||||
return "Script";
|
||||
case EComponentType::COMPONENT_CAMERA:
|
||||
return "Camera";
|
||||
case EComponentType::COMPONENT_LIGHT:
|
||||
return "Light";
|
||||
case EComponentType::COMPONENT_RIGIDBODY:
|
||||
return "Rigidbody";
|
||||
case EComponentType::COMPONENT_COLLIDER:
|
||||
return "Collider";
|
||||
case EComponentType::COMPONENT_CUSTOM:
|
||||
return "Custom";
|
||||
case EComponentType::COMPONENT_META:
|
||||
return "Meta";
|
||||
default:
|
||||
return "UNKNOWN_COMPONENT";
|
||||
}
|
||||
}
|
||||
}
|
||||
43
src/Engine/Core/Component/CAbstractComponent.hpp
Normal file
43
src/Engine/Core/Component/CAbstractComponent.hpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#ifndef CABSTRACT_COMPONENT_HPP
|
||||
#define CABSTRACT_COMPONENT_HPP
|
||||
|
||||
#include "../Utils/CSerializable.hpp"
|
||||
#include "../Entity/CEntity.hpp"
|
||||
namespace CosmicCore {
|
||||
|
||||
/**
|
||||
* @brief Enum representing every type of component that can be created.
|
||||
*/
|
||||
enum class EComponentType : unsigned char {
|
||||
COMPONENT_SCRIPT,
|
||||
COMPONENT_CAMERA,
|
||||
COMPONENT_LIGHT,
|
||||
COMPONENT_RIGIDBODY,
|
||||
COMPONENT_COLLIDER,
|
||||
COMPONENT_CUSTOM,
|
||||
COMPONENT_RENDERER,
|
||||
COMPONENT_META,
|
||||
COMPONENT_COUNT
|
||||
};
|
||||
|
||||
constexpr std::string typeToString(EComponentType type);
|
||||
|
||||
class CAbstractComponent : public CSerializable{
|
||||
public:
|
||||
CAbstractComponent(CEntity& entity): m_owningEntity(entity){};
|
||||
virtual ~CAbstractComponent() = default;
|
||||
|
||||
CEntity& getEntity() {
|
||||
return m_owningEntity;
|
||||
}
|
||||
|
||||
virtual nlohmann::json to_json() = 0;
|
||||
private:
|
||||
/**
|
||||
* @brief The entity to which the component is linked.
|
||||
*/
|
||||
CEntity m_owningEntity;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
10
src/Engine/Core/Component/Camera/CAbstractCamera.hpp
Normal file
10
src/Engine/Core/Component/Camera/CAbstractCamera.hpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef CABSTRACTCAMERA_HPP
|
||||
#define CABSTRACTCAMERA_HPP
|
||||
#include "../CAbstractComponent.hpp"
|
||||
namespace CosmicCore {
|
||||
class CAbstractCamera: CAbstractComponent
|
||||
{
|
||||
|
||||
};
|
||||
}
|
||||
#endif
|
||||
10
src/Engine/Core/Component/Collider/CAbstractCollider.hpp
Normal file
10
src/Engine/Core/Component/Collider/CAbstractCollider.hpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef CABSTRACTCOLLIDER_HPP
|
||||
#define CABSTRACTCOLLIDER_HPP
|
||||
#include "../CAbstractComponent.hpp"
|
||||
namespace CosmicCore {
|
||||
class CAbstractCollider: public CAbstractComponent
|
||||
{
|
||||
|
||||
};
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,34 @@
|
||||
#include "CAbstractCustomComponent.hpp"
|
||||
|
||||
namespace CosmicCore {
|
||||
CAbstractCustomComponent::CAbstractCustomComponent(CEntity& entity): CAbstractComponent(entity)
|
||||
{
|
||||
}
|
||||
|
||||
void CAbstractCustomComponent::addProperty(std::string name, float value)
|
||||
{
|
||||
m_properties[name] = value;
|
||||
}
|
||||
|
||||
float CAbstractCustomComponent::getProperty(std::string name)
|
||||
{
|
||||
return m_properties[name];
|
||||
}
|
||||
|
||||
void CAbstractCustomComponent::setProperty(std::string name, float value)
|
||||
{
|
||||
if (m_properties.count(name) != 0)
|
||||
{
|
||||
m_properties[name] = value;
|
||||
}
|
||||
}
|
||||
|
||||
nlohmann::json CAbstractCustomComponent::to_json()
|
||||
{
|
||||
nlohmann::json j;
|
||||
j["ComponentType"] = EComponentType::COMPONENT_CUSTOM;
|
||||
return j;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef CABSTRACTCUSTOMCOMPONENT_HPP
|
||||
#define CABSTRACTCUSTOMCOMPONENT_HPP
|
||||
|
||||
#include "../CAbstractComponent.hpp"
|
||||
#include <unordered_map>
|
||||
|
||||
namespace CosmicCore {
|
||||
/**
|
||||
* @brief Class representing a Data Dictonnary. Is a component.
|
||||
*/
|
||||
class CAbstractCustomComponent : public CAbstractComponent{
|
||||
private:
|
||||
std::unordered_map<std::string, float> m_properties;
|
||||
public:
|
||||
|
||||
/**
|
||||
* @brief CData's default constructor, disabled.
|
||||
*/
|
||||
CAbstractCustomComponent() = delete;
|
||||
virtual ~CAbstractCustomComponent() = default;
|
||||
|
||||
/**
|
||||
* @brief CData's constructor.
|
||||
* @param[in, out] entity The entity to which we want to attach specific informations.
|
||||
*/
|
||||
CAbstractCustomComponent(CEntity& entity);
|
||||
|
||||
|
||||
void addProperty(std::string name, float value);
|
||||
|
||||
float getProperty(std::string name);
|
||||
|
||||
void setProperty(std::string name, float value);
|
||||
|
||||
nlohmann::json to_json();
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
180
src/Engine/Core/Component/Geometry/CTransform.cpp
Normal file
180
src/Engine/Core/Component/Geometry/CTransform.cpp
Normal file
@@ -0,0 +1,180 @@
|
||||
#include "CTransform.hpp"
|
||||
|
||||
namespace CosmicCore
|
||||
{
|
||||
|
||||
void CTransform::calcTranslation(void) {
|
||||
m_translation = glm::translate(glm::mat4(1.0f), m_center);
|
||||
}
|
||||
|
||||
void CTransform::calcRotation(void) {
|
||||
m_rotation = glm::toMat4(m_orientation);
|
||||
}
|
||||
|
||||
void CTransform::calcHomothety(void) {
|
||||
m_homothety = glm::scale(glm::mat4(1.0f), m_scale);
|
||||
}
|
||||
|
||||
void CTransform::calcTranformation(void) {
|
||||
m_transformation = m_translation * m_rotation * m_homothety;
|
||||
}
|
||||
|
||||
CTransform::CTransform(CEntity& entity) : CAbstractComponent(entity),
|
||||
m_center(0.0f, 0.0f, 0.0f),
|
||||
m_scale(1.0f, 1.0f, 1.0f),
|
||||
m_orientation(),
|
||||
m_observers() {
|
||||
calcTranslation();
|
||||
calcRotation();
|
||||
calcHomothety();
|
||||
calcTranformation();
|
||||
}
|
||||
|
||||
CTransform::CTransform(CEntity& entity, glm::vec3 center, glm::vec3 scale, glm::vec3 eulerAngle) : CAbstractComponent(entity),
|
||||
m_center(center),
|
||||
m_scale(scale),
|
||||
m_orientation(eulerAngle),
|
||||
m_observers() {
|
||||
calcTranslation();
|
||||
calcRotation();
|
||||
calcHomothety();
|
||||
calcTranformation();
|
||||
}
|
||||
|
||||
glm::mat4 CTransform::getTranslation(void) const {
|
||||
return m_translation;
|
||||
}
|
||||
|
||||
glm::mat4 CTransform::getRotation(void) const {
|
||||
return m_rotation;
|
||||
}
|
||||
|
||||
glm::mat4 CTransform::getHomothety(void) const {
|
||||
return m_homothety;
|
||||
}
|
||||
|
||||
glm::mat4 CTransform::getTransormation(void) const {
|
||||
return m_transformation;
|
||||
}
|
||||
|
||||
glm::mat4 CTransform::getInheritedTransformation(void) const {
|
||||
/*const auto& entity = getEntity();
|
||||
if(entity)
|
||||
{
|
||||
if (entity->getParent().expired()) {
|
||||
return m_transformation;
|
||||
}
|
||||
else {
|
||||
auto p = entity->getParent().lock();
|
||||
return p->getTransform().getInheritedTransformation() * m_transformation;
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
glm::vec3 CTransform::getCenter(void) const {
|
||||
return m_center;
|
||||
}
|
||||
|
||||
glm::vec3* CTransform::getCenterReference(void)
|
||||
{
|
||||
return &m_center;
|
||||
}
|
||||
|
||||
glm::vec3 CTransform::getScale(void) const {
|
||||
return m_scale;
|
||||
}
|
||||
|
||||
glm::vec3& CTransform::getScalePtr(void)
|
||||
{
|
||||
return m_scale;
|
||||
}
|
||||
|
||||
glm::vec3 CTransform::getEulerAngle(void) const {
|
||||
return glm::eulerAngles(m_orientation);
|
||||
}
|
||||
|
||||
glm::quat CTransform::getOrientation(void) const
|
||||
{
|
||||
return m_orientation;
|
||||
}
|
||||
|
||||
void CTransform::setCenter(glm::vec3 newCenter) {
|
||||
m_center = newCenter;
|
||||
calcTranslation();
|
||||
calcTranformation();
|
||||
for (auto const& obs : m_observers) {
|
||||
if(auto ptrObs = obs.lock())
|
||||
ptrObs->onCenterChange();
|
||||
}
|
||||
}
|
||||
|
||||
void CTransform::setUniformScale(float newScale) {
|
||||
m_scale = glm::vec3(newScale, newScale, newScale);
|
||||
calcHomothety();
|
||||
calcTranformation();
|
||||
for (auto const& obs : m_observers) {
|
||||
if(auto ptrObs = obs.lock())
|
||||
ptrObs->onScaleChange();
|
||||
}
|
||||
}
|
||||
|
||||
void CTransform::setScale(glm::vec3 newScale) {
|
||||
m_scale = newScale;
|
||||
calcHomothety();
|
||||
calcTranformation();
|
||||
for (auto const& obs : m_observers) {
|
||||
if(auto ptrObs = obs.lock())
|
||||
ptrObs->onScaleChange();
|
||||
}
|
||||
}
|
||||
|
||||
void CTransform::rotate(glm::vec3 axis, float angle) {
|
||||
m_orientation = glm::rotate(m_orientation, angle, axis);
|
||||
calcRotation();
|
||||
calcTranformation();
|
||||
for (auto const& obs : m_observers) {
|
||||
if(auto ptrObs = obs.lock())
|
||||
ptrObs->onRotationChange();
|
||||
}
|
||||
}
|
||||
|
||||
void CTransform::setEulerAngle(glm::vec3 newEulerAngle) {
|
||||
m_orientation = glm::quat(newEulerAngle);
|
||||
calcRotation();
|
||||
calcTranformation();
|
||||
for (auto const& obs : m_observers) {
|
||||
if(auto ptrObs = obs.lock())
|
||||
ptrObs->onRotationChange();
|
||||
}
|
||||
}
|
||||
|
||||
void CTransform::setQuaternion(glm::quat newQuaternion) {
|
||||
m_orientation = newQuaternion;
|
||||
calcRotation();
|
||||
calcTranformation();
|
||||
for (auto const& obs : m_observers) {
|
||||
if(auto ptrObs = obs.lock())
|
||||
ptrObs->onRotationChange();
|
||||
}
|
||||
}
|
||||
|
||||
void CTransform::forward(glm::vec3 value) {
|
||||
glm::vec3 relValue = m_orientation * value;
|
||||
m_center += relValue;
|
||||
calcTranslation();
|
||||
calcTranformation();
|
||||
for (auto const& obs : m_observers) {
|
||||
if(auto ptrObs = obs.lock())
|
||||
ptrObs->onCenterChange();
|
||||
}
|
||||
}
|
||||
|
||||
void CTransform::addObserver(std::weak_ptr<CTransformObserver> newObserver) {
|
||||
m_observers.emplace_back(newObserver);
|
||||
}
|
||||
|
||||
void CTransform::removeObserver(std::weak_ptr<CTransformObserver> obs)
|
||||
{
|
||||
//TODO
|
||||
}
|
||||
}
|
||||
245
src/Engine/Core/Component/Geometry/CTransform.hpp
Normal file
245
src/Engine/Core/Component/Geometry/CTransform.hpp
Normal file
@@ -0,0 +1,245 @@
|
||||
#ifndef CTRANSFORM_HPP
|
||||
#define CTRANSFORM_HPP
|
||||
|
||||
#include "../../Observer/CTransformObserver.hpp"
|
||||
#include "../CAbstractComponent.hpp"
|
||||
#include <memory>
|
||||
|
||||
#define GLM_ENABLE_EXPERIMENTAL
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
#include <glm/gtx/quaternion.hpp>
|
||||
|
||||
namespace CosmicCore
|
||||
{
|
||||
/**
|
||||
* @brief Class representing the transformations of the object in the space.
|
||||
*/
|
||||
class CTransform : public CAbstractComponent {
|
||||
private:
|
||||
|
||||
/**
|
||||
* @brief A 3D vector representing the position of the entity .
|
||||
*/
|
||||
glm::vec3 m_center;
|
||||
|
||||
/**
|
||||
* @brief A 3D vector representing the scale of the entity on each axis.
|
||||
*/
|
||||
glm::vec3 m_scale;
|
||||
|
||||
/**
|
||||
* @brief A quaternion which represent the rotation.
|
||||
*/
|
||||
glm::quat m_orientation;
|
||||
|
||||
/**
|
||||
* @brief A mat4 matrix representing the translation transformation.
|
||||
*/
|
||||
glm::mat4 m_translation;
|
||||
|
||||
/**
|
||||
* @brief A mat4 matrix representing the rotation transformation.
|
||||
*/
|
||||
glm::mat4 m_rotation;
|
||||
|
||||
/**
|
||||
* @brief A mat4 representing the scale of the object on each axis.
|
||||
*/
|
||||
glm::mat4 m_homothety;
|
||||
|
||||
/**
|
||||
* @brief Full transformation of the object.
|
||||
* result of m_translation * m_rotation * m_homothety.
|
||||
*/
|
||||
glm::mat4 m_transformation;
|
||||
|
||||
/**
|
||||
* @brief a list of observer.
|
||||
*/
|
||||
std::vector<std::weak_ptr<CTransformObserver>> m_observers;
|
||||
|
||||
/**
|
||||
* @brief Compute the translation matrix.
|
||||
*/
|
||||
void calcTranslation(void);
|
||||
|
||||
/**
|
||||
* @brief Compute the rotation matrix.
|
||||
*/
|
||||
void calcRotation(void);
|
||||
|
||||
/**
|
||||
* @brief Compute the homothety matrix.
|
||||
*/
|
||||
void calcHomothety(void);
|
||||
|
||||
/**
|
||||
* @brief Compute the full transformation of the object.
|
||||
*/
|
||||
void calcTranformation(void);
|
||||
public:
|
||||
|
||||
/**
|
||||
* @brief CTransform's constructor. Assign the center to (0,0,0) and the scale to (1,1,1).
|
||||
* @param[in, out] m_entity The entity to which the transform belongs.
|
||||
*/
|
||||
CTransform(CEntity& m_entity);
|
||||
|
||||
/**
|
||||
* @brief CTransform's constructor.
|
||||
* @param[in, out] m_entity The entity to which the transform belongs.
|
||||
* @param[in] center A 3D vector which represent the initial position of the entity.
|
||||
* @param[in] scale A 3D vector which represent the scale of the entity on each axis.
|
||||
* @param[in] eulerAngle A 3D vector wich represent the initial rotation of the entity.
|
||||
*/
|
||||
CTransform(CEntity& m_entity, glm::vec3 center, glm::vec3 scale, glm::vec3 eulerAngle);
|
||||
|
||||
/**
|
||||
* @brief Getter to the translation matrix.
|
||||
* @return A mat4 equal to m_translation.
|
||||
*/
|
||||
glm::mat4 getTranslation(void) const;
|
||||
|
||||
/**
|
||||
* @brief Getter to the rotation matrix.
|
||||
* @return A mat4 equal to m_rotation.
|
||||
*/
|
||||
glm::mat4 getRotation(void) const;
|
||||
|
||||
/**
|
||||
* @brief Getter to the homothety.
|
||||
* @return A mat4 equal to m_homothety .
|
||||
*/
|
||||
glm::mat4 getHomothety(void) const;
|
||||
|
||||
/**
|
||||
* @brief Getter to the full transformation.
|
||||
* @return A mat4 equal to m_transformation.
|
||||
*/
|
||||
glm::mat4 getTransormation(void) const;
|
||||
|
||||
/**
|
||||
* @brief Getter to the full inherited transformation.
|
||||
* Return the current entity's transformation if there is no parent or the transformation of each parent recursively times the current transformation.
|
||||
* @return A mat4 representing the actual transformation of the entity (in the global scale).
|
||||
*/
|
||||
glm::mat4 getInheritedTransformation(void) const;
|
||||
|
||||
/**
|
||||
* @brief Getter to the entity's center.
|
||||
* @return The 3D vector of the position.
|
||||
*/
|
||||
glm::vec3 getCenter(void) const;
|
||||
|
||||
/**
|
||||
* @brief Getter to the entity's center by its pointer.
|
||||
* @return The 3D vector of the position.
|
||||
*/
|
||||
glm::vec3* getCenterReference(void);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Getter to the scale.
|
||||
* @return The 3D vector of the scale.
|
||||
*/
|
||||
glm::vec3 getScale(void) const;
|
||||
|
||||
glm::vec3& getScalePtr(void);
|
||||
|
||||
/**
|
||||
* @brief Getter to the angles of the rotation on each axis.
|
||||
* @return The 3D vector of the rotation angles, determining the orientation of the object.
|
||||
*/
|
||||
glm::vec3 getEulerAngle(void) const;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Getter to the quaternion.
|
||||
* @return A quaternion representing the orientation of the object.
|
||||
*/
|
||||
glm::quat getOrientation(void) const;
|
||||
|
||||
/**
|
||||
* @brief Setter of the position.
|
||||
* @param[in] newCenter The new position of the object.
|
||||
*/
|
||||
void setCenter(glm::vec3 newCenter);
|
||||
|
||||
/**
|
||||
* @brief Setter of the uniform scale (same scale on the 3-axis).
|
||||
* @param[in] newScale The new uniform scale.
|
||||
*/
|
||||
void setUniformScale(float newScale);
|
||||
|
||||
/**
|
||||
* @brief Setter of the scale.
|
||||
* @param[in] newScale The new scale on each axis (3D vector).
|
||||
*/
|
||||
void setScale(glm::vec3 newScale);
|
||||
|
||||
/**
|
||||
* @brief Rotate the object on a given axis with the given angle.
|
||||
* @param[in] axis The axis on which the rotation will occur.
|
||||
* @param[in] angle The angle the rotation must reach.
|
||||
*/
|
||||
void rotate(glm::vec3 axis, float angle);
|
||||
|
||||
/**
|
||||
* @brief Translate the object on the given axis with the given value on each axis.
|
||||
* @param[in] value The 3D vector of the translation.
|
||||
*/
|
||||
void forward(glm::vec3 value);
|
||||
|
||||
/**
|
||||
* @brief Setter of the Angles.
|
||||
* @param[in] newEulerAngle The new orientation of the object.
|
||||
*/
|
||||
void setEulerAngle(glm::vec3 newEulerAngle);
|
||||
|
||||
/**
|
||||
* @brief Setter of the orientation quaternion.
|
||||
* @param[in] newQuaternion The new orientation of the object.
|
||||
*/
|
||||
void setQuaternion(glm::quat newQuaternion);
|
||||
|
||||
/**
|
||||
* @brief Add an observer on the transform. Can be from any class if the said class is child class of CTransformObserver.
|
||||
* @param[in, out] newObserver The observer to add.
|
||||
*/
|
||||
void addObserver(std::weak_ptr<CTransformObserver> newObserver);
|
||||
|
||||
/**
|
||||
* @brief Remove an observer of the transform. Does nothing if the observer doesn't exist.
|
||||
* @param[in, out] obs The observer to remove.
|
||||
*/
|
||||
void removeObserver(std::weak_ptr<CTransformObserver> obs);
|
||||
|
||||
nlohmann::json to_json() {
|
||||
nlohmann::json j;
|
||||
j["Position"]["x"] = m_center.x;
|
||||
j["Position"]["y"] = m_center.y;
|
||||
j["Position"]["z"] = m_center.z;
|
||||
|
||||
j["Rotation"]["x"] = getEulerAngle().x;
|
||||
j["Rotation"]["y"] = getEulerAngle().y;
|
||||
j["Rotation"]["z"] = getEulerAngle().z;
|
||||
|
||||
j["Scale"]["x"] = getScale().x;
|
||||
j["Scale"]["y"] = getScale().y;
|
||||
j["Scale"]["z"] = getScale().z;
|
||||
return j;
|
||||
};
|
||||
|
||||
static CTransform from_json(CEntity entity,nlohmann::json& j)
|
||||
{
|
||||
CTransform t(entity);
|
||||
t.setCenter(glm::vec3(j["Position"]["x"], j["Position"]["y"], j["Position"]["z"]));
|
||||
t.setEulerAngle(glm::vec3(j["Rotation"]["x"], j["Rotation"]["y"], j["Rotation"]["z"]));
|
||||
t.setScale(glm::vec3(j["Scale"]["x"], j["Scale"]["y"], j["Scale"]["z"]));
|
||||
return t;
|
||||
};
|
||||
};
|
||||
}
|
||||
#endif
|
||||
12
src/Engine/Core/Component/Graphics/CAbstractRenderer.hpp
Normal file
12
src/Engine/Core/Component/Graphics/CAbstractRenderer.hpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef CABSTRACTRENDERER_HPP
|
||||
#define CABSTRACTRENDERER_HPP
|
||||
#include "../CAbstractComponent.hpp"
|
||||
namespace CosmicCore {
|
||||
class CAbstractRenderer : public CAbstractComponent
|
||||
{
|
||||
public:
|
||||
CAbstractRenderer(CEntity& e): CAbstractComponent(e){};
|
||||
virtual void render() = 0;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
44
src/Engine/Core/Component/Graphics/CModel.cpp
Normal file
44
src/Engine/Core/Component/Graphics/CModel.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "CModel.hpp"
|
||||
|
||||
CModel::CModel(std::vector<CMesh*> meshes) :
|
||||
m_meshes(meshes),
|
||||
m_loaded(false) {
|
||||
}
|
||||
|
||||
CModel::~CModel(void) {
|
||||
for (unsigned int i = 0; i < m_meshes.size(); i++)
|
||||
{
|
||||
delete m_meshes[i];
|
||||
}
|
||||
}
|
||||
|
||||
void CModel::load(void) {
|
||||
// For each meshes of the model.
|
||||
for (unsigned int i = 0; i < m_meshes.size(); i++) {
|
||||
m_meshes[i]->load();
|
||||
}
|
||||
|
||||
m_loaded = true;
|
||||
}
|
||||
|
||||
void CModel::draw(glm::mat4 model, glm::mat4 view, glm::mat4 projection, glm::vec3 lightPos, float intensity) {
|
||||
// For each meshes of the model.
|
||||
for (unsigned int i = 0; i < m_meshes.size(); i++) {
|
||||
m_meshes[i]->draw(model, view, projection, lightPos, intensity);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<CMesh*>& CModel::getMeshes()
|
||||
{
|
||||
return m_meshes;
|
||||
}
|
||||
|
||||
void CModel::setMeshes(std::vector<CMesh*> meshes)
|
||||
{
|
||||
m_meshes = meshes;
|
||||
}
|
||||
|
||||
bool CModel::isLoaded()
|
||||
{
|
||||
return m_loaded;
|
||||
}
|
||||
78
src/Engine/Core/Component/Graphics/CModel.hpp
Normal file
78
src/Engine/Core/Component/Graphics/CModel.hpp
Normal file
@@ -0,0 +1,78 @@
|
||||
#ifndef CMODEL_HPP
|
||||
#define CMODEL_HPP
|
||||
|
||||
#include "Material/SMaterial.hpp"
|
||||
#include "Mesh/CMesh.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
/**
|
||||
* @brief Class representing a 3D model.
|
||||
*/
|
||||
class CModel {
|
||||
private:
|
||||
/**
|
||||
* @brief The meshes, a vector which contains all the meshes of the object.
|
||||
*/
|
||||
std::vector<CMesh*> m_meshes;
|
||||
|
||||
/**
|
||||
* @brief A boolean representing the loading state of the model.
|
||||
*/
|
||||
bool m_loaded;
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* @brief CModel's default constructor, disabled.
|
||||
*/
|
||||
CModel(void) = delete;
|
||||
|
||||
/**
|
||||
* @brief CModel constructor.
|
||||
* @param meshes : The list of the meshes of the object.
|
||||
*/
|
||||
CModel(std::vector<CMesh*> meshes);
|
||||
|
||||
/**
|
||||
* @brief CModel's destructor.
|
||||
*/
|
||||
~CModel(void);
|
||||
|
||||
/**
|
||||
* @brief Load the model in the GPU.
|
||||
*/
|
||||
void load(void);
|
||||
|
||||
/**
|
||||
* @brief Render the model on the screen.
|
||||
* @param[in] model The transform of the object.
|
||||
* @param[in] view The view of the object.
|
||||
* @param[in] projection The projection of the object.
|
||||
* @param[in] lightPos The light's position.
|
||||
* @param[in] intensity The light's intensity.
|
||||
*/
|
||||
void draw(glm::mat4 model, glm::mat4 view, glm::mat4 projection, glm::vec3 lightPos, float intensity);
|
||||
|
||||
/**
|
||||
* @brief Getter to the meshes of the model.
|
||||
* @return A vector which represent the list of meshes of the model, it's a direct reference to the member m_meshes.
|
||||
*/
|
||||
std::vector<CMesh*>& getMeshes(void);
|
||||
|
||||
/**
|
||||
* @brief Setter to the meshes.
|
||||
* @param[in] meshes The list of meshes to set to the model.
|
||||
*/
|
||||
void setMeshes(std::vector<CMesh*> meshes);
|
||||
|
||||
/**
|
||||
* @brief Getter to m_loaded member, say if the model is loaded or not.
|
||||
* @return A boolean representing the state of the model, loaded or not.
|
||||
*/
|
||||
bool isLoaded(void);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
10
src/Engine/Core/Component/Graphics/CTestRenderer.cpp
Normal file
10
src/Engine/Core/Component/Graphics/CTestRenderer.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#include "CTestRenderer.hpp"
|
||||
#include <iostream>
|
||||
namespace CosmicCore {
|
||||
void CTestRenderer::render()
|
||||
{
|
||||
int a = 2*25;
|
||||
int b = a +2;
|
||||
std::cout << b << std::endl;
|
||||
};
|
||||
}
|
||||
17
src/Engine/Core/Component/Graphics/CTestRenderer.hpp
Normal file
17
src/Engine/Core/Component/Graphics/CTestRenderer.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef CTESTRENDERER_HPP
|
||||
#define CTESTRENDERER_HPP
|
||||
#include "CAbstractRenderer.hpp"
|
||||
|
||||
namespace CosmicCore {
|
||||
class CTestRenderer: public CAbstractRenderer
|
||||
{
|
||||
public:
|
||||
CTestRenderer() = delete;
|
||||
CTestRenderer(CEntity& e): CAbstractRenderer(e){};
|
||||
void render() override;
|
||||
nlohmann::json to_json(){return nlohmann::json();};
|
||||
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
39
src/Engine/Core/Component/Graphics/Material/SColor.hpp
Normal file
39
src/Engine/Core/Component/Graphics/Material/SColor.hpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef SCOLOR_HPP
|
||||
#define SCOLOR_HPP
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
/**
|
||||
* @file SColor.hpp
|
||||
* @brief File for the color struct of a material.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief the number of type in the enum EColorType.
|
||||
*/
|
||||
#define COLOR_TYPE_NUMBER 2
|
||||
|
||||
/**
|
||||
* @brief The enum for color type in the material.
|
||||
*/
|
||||
enum EColorType {
|
||||
COLOR_DIFFUSE,
|
||||
COLOR_SPECULAR,
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief The struct color of the material.
|
||||
*/
|
||||
typedef struct {
|
||||
/**
|
||||
* @brief 4D Vector representing the RGBA color.
|
||||
*/
|
||||
glm::vec4 m_vector;
|
||||
|
||||
/**
|
||||
* @brief The color's type.
|
||||
*/
|
||||
EColorType m_type;
|
||||
} SColor;
|
||||
|
||||
#endif
|
||||
39
src/Engine/Core/Component/Graphics/Material/SMaterial.hpp
Normal file
39
src/Engine/Core/Component/Graphics/Material/SMaterial.hpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef SMATERIAL_HPP
|
||||
#define SMATERIAL_HPP
|
||||
|
||||
#include "../Shader/CShader.hpp"
|
||||
#include "../Texture/CAbstractTexture.hpp"
|
||||
#include "SColor.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* @file SMaterial.hpp
|
||||
* @brief File for the material struct.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief The struct material.
|
||||
*/
|
||||
typedef struct SMaterial {
|
||||
/**
|
||||
* @brief List of textures for the material.
|
||||
*/
|
||||
std::vector<CAbstractTexture*> m_textures;
|
||||
|
||||
/**
|
||||
* @brief List of colors for the material.
|
||||
*/
|
||||
std::vector<SColor> m_colors;
|
||||
|
||||
/**
|
||||
* @brief The shader used to render.
|
||||
*/
|
||||
CShader* m_shader = nullptr;
|
||||
|
||||
~SMaterial() { for (unsigned int i = 0; i < m_textures.size(); ++i) {
|
||||
delete m_textures[i];
|
||||
} };
|
||||
} SMaterial;
|
||||
|
||||
#endif
|
||||
216
src/Engine/Core/Component/Graphics/Mesh/CMesh.cpp
Normal file
216
src/Engine/Core/Component/Graphics/Mesh/CMesh.cpp
Normal file
@@ -0,0 +1,216 @@
|
||||
#include "CMesh.hpp"
|
||||
|
||||
void CMesh::bindMaterial(glm::mat4 model, glm::mat4 view, glm::mat4 projection, glm::vec3 lightPos, float intensity) {
|
||||
// Test the material.
|
||||
if (m_material == nullptr) {
|
||||
throw CLogicException("This mesh is not linked to a material.");
|
||||
}
|
||||
|
||||
// Active the shader.
|
||||
m_material->m_shader->use();
|
||||
|
||||
// Give the matrix to the shader.
|
||||
m_material->m_shader->setMat4("model", model);
|
||||
m_material->m_shader->setMat4("view", view);
|
||||
m_material->m_shader->setMat4("projection", projection);
|
||||
m_material->m_shader->setVec3("lightPos", lightPos);
|
||||
m_material->m_shader->setFloat("intensity", intensity);
|
||||
|
||||
// Counter for each Texture Type :
|
||||
unsigned int countTex[TEXTURE_TYPE_NUMBER];
|
||||
for (unsigned int i = 0; i < TEXTURE_TYPE_NUMBER; i++) {
|
||||
countTex[i] = 0;
|
||||
}
|
||||
|
||||
// For each texture in the material :
|
||||
for (unsigned int i = 0; i < m_material->m_textures.size(); i++) {
|
||||
|
||||
// Active the shader unit.
|
||||
glActiveTexture(GL_TEXTURE0 + i);
|
||||
|
||||
// Retrieve texture name.
|
||||
std::string name;
|
||||
|
||||
switch (m_material->m_textures[i]->getType()) {
|
||||
case TEXTURE_DIFFUSE:
|
||||
name = "textureDiffuse" + std::to_string(countTex[0]);
|
||||
countTex[0]++;
|
||||
break;
|
||||
case TEXTURE_SPECULAR:
|
||||
name = "textureSpecular" + std::to_string(countTex[1]);
|
||||
countTex[1]++;
|
||||
break;
|
||||
case TEXTURE_NORMAL:
|
||||
name = "textureNormal" + std::to_string(countTex[2]);
|
||||
countTex[2]++;
|
||||
break;
|
||||
case TEXTURE_HEIGHT:
|
||||
name = "textureHeight" + std::to_string(countTex[3]);
|
||||
countTex[3]++;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Get the uniform storage space of the shader.
|
||||
glUniform1i(glGetUniformLocation(m_material->m_shader->getId(), name.c_str()), i);
|
||||
|
||||
// And finally bind the texture.
|
||||
glBindTexture(GL_TEXTURE_2D, m_material->m_textures[i]->getId());
|
||||
}
|
||||
|
||||
// Always good practice to set everything back to defaults once configured.
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
|
||||
|
||||
|
||||
// Counter for each color Type :
|
||||
unsigned int countCol[COLOR_TYPE_NUMBER];
|
||||
for (unsigned int i = 0; i < COLOR_TYPE_NUMBER; i++) {
|
||||
countCol[i] = 0;
|
||||
}
|
||||
|
||||
// For each color in the material :
|
||||
for (unsigned int i = 0; i < m_material->m_colors.size(); i++) {
|
||||
|
||||
// Retrieve texture name.
|
||||
std::string name;
|
||||
|
||||
switch (m_material->m_colors[i].m_type) {
|
||||
case COLOR_DIFFUSE:
|
||||
name = "colorDiffuse" + std::to_string(countCol[0]);
|
||||
countCol[0]++;
|
||||
break;
|
||||
case COLOR_SPECULAR:
|
||||
name = "colorSpecular" + std::to_string(countCol[1]);
|
||||
countCol[1]++;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Give the color as a vector 4.
|
||||
m_material->m_shader->setVec4(name, m_material->m_colors[i].m_vector);
|
||||
}
|
||||
|
||||
// Active the shader.
|
||||
//m_material->m_shader->use();
|
||||
}
|
||||
|
||||
CMesh::CMesh(void) :
|
||||
m_vertexes(),
|
||||
m_indexes(),
|
||||
m_material(nullptr),
|
||||
m_vertexBufferObject(0),
|
||||
m_elementBufferObject(0),
|
||||
m_vertexArrayObject(0) {
|
||||
}
|
||||
|
||||
CMesh::~CMesh(void) {
|
||||
// Delete the VAO.
|
||||
if (m_vertexArrayObject != 0) {
|
||||
glDeleteVertexArrays(1, &m_vertexArrayObject);
|
||||
}
|
||||
|
||||
if (m_material != nullptr)
|
||||
{
|
||||
delete m_material;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<SVertex>& CMesh::getVertexes(void) {
|
||||
return m_vertexes;
|
||||
}
|
||||
|
||||
std::vector<unsigned int>& CMesh::getIndexes(void) {
|
||||
return m_indexes;
|
||||
}
|
||||
|
||||
SMaterial* CMesh::getMaterial(void) {
|
||||
return m_material;
|
||||
}
|
||||
|
||||
void CMesh::setMaterial(SMaterial* material) {
|
||||
m_material = material;
|
||||
}
|
||||
|
||||
void CMesh::setVertices(std::vector<SVertex> vertices)
|
||||
{
|
||||
m_vertexes = vertices;
|
||||
}
|
||||
|
||||
void CMesh::setIndexes(std::vector<unsigned int> indexes)
|
||||
{
|
||||
m_indexes = indexes;
|
||||
}
|
||||
|
||||
void CMesh::load(void) {
|
||||
if (m_vertexes.empty() || m_indexes.empty()) {
|
||||
throw CLogicException("This mesh do no contain vertex data.");
|
||||
}
|
||||
|
||||
// Create the VAO.
|
||||
glGenVertexArrays(1, &m_vertexArrayObject);
|
||||
|
||||
// Create the VBO.
|
||||
glGenBuffers(1, &m_vertexBufferObject);
|
||||
|
||||
// Create the EBO.
|
||||
glGenBuffers(1, &m_elementBufferObject);
|
||||
|
||||
|
||||
// Open the VAO.
|
||||
glBindVertexArray(m_vertexArrayObject);
|
||||
|
||||
// Open the VBO, and link it to the VAO.
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferObject);
|
||||
|
||||
// Give the block of memory of vertexes vector (so, it works like a c list), and the struct is transparent du to their conception).
|
||||
glBufferData(GL_ARRAY_BUFFER, m_vertexes.size() * sizeof(SVertex), &m_vertexes[0], GL_STATIC_DRAW);
|
||||
|
||||
// Open the EBO, and link it to the VAO.
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_elementBufferObject);
|
||||
|
||||
// Give the block of memory of indexes vector (so, it works like a c list), and the struct is transparent du to their conception).
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indexes.size() * sizeof(unsigned int), &m_indexes[0], GL_STATIC_DRAW);
|
||||
|
||||
|
||||
// Set the vertex attribute pointers.
|
||||
// Vertex Positions.
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(SVertex), (void*)0);
|
||||
// Vertex normals.
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(SVertex), (void*)offsetof(SVertex, m_normal));
|
||||
// Vertex texture coords.
|
||||
glEnableVertexAttribArray(2);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(SVertex), (void*)offsetof(SVertex, m_texCoords));
|
||||
// Vertex tangent.
|
||||
glEnableVertexAttribArray(3);
|
||||
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(SVertex), (void*)offsetof(SVertex, m_tangent));
|
||||
// Vertex bitangent.
|
||||
glEnableVertexAttribArray(4);
|
||||
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(SVertex), (void*)offsetof(SVertex, m_bitangent));
|
||||
|
||||
|
||||
// Unbind the VAO.
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void CMesh::draw(glm::mat4 model, glm::mat4 view, glm::mat4 projection, glm::vec3 lightPos, float intensity) {
|
||||
if (m_vertexArrayObject == 0) {
|
||||
throw CLogicException("This mesh is not loaded in GPU, you can not draw it.");
|
||||
}
|
||||
|
||||
// Bind the material.
|
||||
bindMaterial(model, view, projection, lightPos, intensity);
|
||||
|
||||
// Enable the vertex array object, so the shader use the vertex attribute in this vao.
|
||||
glBindVertexArray(m_vertexArrayObject);
|
||||
|
||||
// Render the triangles in the vertex array object.
|
||||
glDrawElements(GL_TRIANGLES, (GLsizei)m_indexes.size(), GL_UNSIGNED_INT, 0);
|
||||
|
||||
// Disable the vertex array object.
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
132
src/Engine/Core/Component/Graphics/Mesh/CMesh.hpp
Normal file
132
src/Engine/Core/Component/Graphics/Mesh/CMesh.hpp
Normal file
@@ -0,0 +1,132 @@
|
||||
#ifndef CMESH_HPP
|
||||
#define CMESH_HPP
|
||||
|
||||
#include "../Material/SMaterial.hpp"
|
||||
#include "../Texture/CAbstractTexture.hpp"
|
||||
#include "SVertex.hpp"
|
||||
#include "../../../Controller/Exception/CLogicException.hpp"
|
||||
|
||||
#ifdef WIN32
|
||||
#include <gl/glew.h>
|
||||
#else
|
||||
#include <GL/glew.h>
|
||||
#endif
|
||||
|
||||
#include <vector>
|
||||
#define GLM_ENABLE_EXPERIMENTAL
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
/**
|
||||
* @file CMesh.hpp
|
||||
* @brief File for the mesh struct.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Class CMesh, used to represent a mesh, a set of vertex (which can be indexed), ready to be drawn.
|
||||
*/
|
||||
class CMesh {
|
||||
private:
|
||||
/**
|
||||
* @brief List of the vertices of the mesh.
|
||||
*/
|
||||
std::vector<SVertex> m_vertexes;
|
||||
|
||||
/**
|
||||
* @brief List of the indices of the mesh.
|
||||
*/
|
||||
std::vector<unsigned int> m_indexes;
|
||||
|
||||
/**
|
||||
* @brief A pointer to the material containing textures, colors and the shader used for the mesh.
|
||||
*/
|
||||
SMaterial* m_material;
|
||||
|
||||
/**
|
||||
* @brief The vertex buffer.
|
||||
*/
|
||||
unsigned int m_vertexBufferObject;
|
||||
|
||||
/**
|
||||
* @brief The index buffer.
|
||||
*/
|
||||
unsigned int m_elementBufferObject;
|
||||
|
||||
/**
|
||||
* @brief Vertex array object, all the data.
|
||||
*/
|
||||
unsigned int m_vertexArrayObject;
|
||||
|
||||
/**
|
||||
* @brief Apply material to use for drawing.
|
||||
* @param[in] model the model matrix.
|
||||
* @param[in] view the view matrix.
|
||||
* @param[in] projection the projection matrix.
|
||||
* @param[in] lightPos the position of the light source.
|
||||
* @param[in] intensity the intensity of the light source.
|
||||
*/
|
||||
void bindMaterial(glm::mat4 model, glm::mat4 view, glm::mat4 projection, glm::vec3 lightPos, float intensity);
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief the constructor.
|
||||
*/
|
||||
CMesh(void);
|
||||
|
||||
/**
|
||||
* @brief the destructor.
|
||||
*/
|
||||
~CMesh(void);
|
||||
|
||||
/**
|
||||
* @brief The vertex array getter.
|
||||
* @return std::vector<SVertex>&, A reference to the list of vertex.
|
||||
*/
|
||||
std::vector<SVertex>& getVertexes(void);
|
||||
|
||||
/**
|
||||
* @brief The index array getter.
|
||||
* @return std::vector<unsigned int>&, A reference to the list of index.
|
||||
*/
|
||||
std::vector<unsigned int>& getIndexes(void);
|
||||
|
||||
/**
|
||||
* @brief The material getter.
|
||||
* @return SMaterial*, the material.
|
||||
*/
|
||||
SMaterial* getMaterial(void);
|
||||
|
||||
/**
|
||||
* @brief The material setter.
|
||||
* @param[in] material the wanted material.
|
||||
*/
|
||||
void setMaterial(SMaterial* material);
|
||||
|
||||
/**
|
||||
* @brief The vertex setter.
|
||||
* @param[in] vertices list of the vertices to give to the GPU.
|
||||
*/
|
||||
void setVertices(std::vector<SVertex> vertices);
|
||||
|
||||
/**
|
||||
* @brief The indexes setter.
|
||||
* @param[in] indexes list of the indexes for the index buffer
|
||||
*/
|
||||
void setIndexes(std::vector<unsigned int> indexes);
|
||||
|
||||
/**
|
||||
* @brief Load the mesh in GPU.
|
||||
*/
|
||||
void load(void);
|
||||
|
||||
/**
|
||||
* @brief draw the mesh.
|
||||
* @param[in] model the model matrix.
|
||||
* @param[in] view the view matrix.
|
||||
* @param[in] projection the projection matrix.
|
||||
* @param[in] lightPos the position of the light source.
|
||||
* @param[in] intensity the intensity of the light source.
|
||||
*/
|
||||
void draw(glm::mat4 model, glm::mat4 view, glm::mat4 projection, glm::vec3 lightPos, float intensity);
|
||||
};
|
||||
|
||||
#endif
|
||||
48
src/Engine/Core/Component/Graphics/Mesh/SVertex.hpp
Normal file
48
src/Engine/Core/Component/Graphics/Mesh/SVertex.hpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#ifndef SVERTEX_HPP
|
||||
#define SVERTEX_HPP
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
/**
|
||||
* @file SVertex.hpp
|
||||
* @brief File for the vertex struct.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief the struct Vertex, use to represent vertex and vertex attribute.
|
||||
*/
|
||||
typedef struct {
|
||||
/**
|
||||
* @brief Position attribute.
|
||||
* 3D vector.
|
||||
*/
|
||||
glm::vec3 m_position;
|
||||
|
||||
/**
|
||||
* @brief Normal attribute.
|
||||
* 3D vector.
|
||||
*/
|
||||
glm::vec3 m_normal;
|
||||
|
||||
/**
|
||||
* @brief Texture coordinates attribute.
|
||||
* 2D vector.
|
||||
*/
|
||||
glm::vec2 m_texCoords;
|
||||
|
||||
/**
|
||||
* @brief Tangent attribute.
|
||||
* 3D vector.
|
||||
*/
|
||||
glm::vec3 m_tangent;
|
||||
|
||||
/**
|
||||
* @brief Bitangent attribute.
|
||||
* 3D vector (perpendicular to m_tangent vector).
|
||||
*/
|
||||
glm::vec3 m_bitangent;
|
||||
|
||||
// m_normal, m_tangent, m_bitangent are supposed to create a vector base to compute normal map.
|
||||
} SVertex;
|
||||
|
||||
#endif
|
||||
6
src/Engine/Core/Component/Graphics/Shader/CShader.cpp
Normal file
6
src/Engine/Core/Component/Graphics/Shader/CShader.cpp
Normal file
@@ -0,0 +1,6 @@
|
||||
#include "CShader.hpp"
|
||||
|
||||
CShader::~CShader()
|
||||
{
|
||||
|
||||
}
|
||||
159
src/Engine/Core/Component/Graphics/Shader/CShader.hpp
Normal file
159
src/Engine/Core/Component/Graphics/Shader/CShader.hpp
Normal file
@@ -0,0 +1,159 @@
|
||||
#ifndef CSHADER_HPP
|
||||
#define CSHADER_HPP
|
||||
|
||||
#include <string>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
/**
|
||||
* @file CShader.hpp
|
||||
* @brief File of CShader, a class to manage complete shader program, composed of a vertex shader,
|
||||
* a fragment shader and sometime a geometry shader.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Class to manage complete shader program, composed of a vertex shader,
|
||||
* a fragment shader and sometime a geometry shader.
|
||||
*/
|
||||
class CShader {
|
||||
public:
|
||||
virtual ~CShader();
|
||||
/**
|
||||
* @brief Get the id of the shader.
|
||||
* @return unsigned int, the id.
|
||||
*/
|
||||
virtual unsigned int getId(void) const = 0;
|
||||
|
||||
/**
|
||||
* @brief Get the name of the shader.
|
||||
* @return std::string, the name.
|
||||
*/
|
||||
virtual std::string getName(void) const = 0;
|
||||
|
||||
/**
|
||||
* @brief Get the vertex of the shader.
|
||||
* @return std::string, the path.
|
||||
*/
|
||||
virtual std::string getVertexPath(void) const = 0;
|
||||
|
||||
/**
|
||||
* @brief Get the geometry of the shader.
|
||||
* @return std::string, the path.
|
||||
*/
|
||||
virtual std::string getGeomPath(void) const = 0;
|
||||
|
||||
/**
|
||||
* @brief Get the fragment of the shader.
|
||||
* @return std::string, the path.
|
||||
*/
|
||||
virtual std::string getFragmentPath(void) const = 0;
|
||||
|
||||
/**
|
||||
* @brief set the name of the shader.
|
||||
* @param[in] name the name.
|
||||
*/
|
||||
virtual void setName(std::string name) = 0;
|
||||
|
||||
/**
|
||||
* @brief Get the vertex source path of the shader.
|
||||
* @param[in] vp the path.
|
||||
*/
|
||||
virtual void setVertexPath(std::string vp) = 0;
|
||||
|
||||
/**
|
||||
* @brief Get the geometry source path of the shader.
|
||||
* @param[in] gp the path.
|
||||
*/
|
||||
virtual void setGeomPath(std::string gp) = 0;
|
||||
|
||||
/**
|
||||
* @brief Get the fragment source path of the shader.
|
||||
* @param[in] fp the path.
|
||||
*/
|
||||
virtual void setFragmentPath(std::string fp) = 0;
|
||||
|
||||
/**
|
||||
* @brief Return true if the shader is ready to use (compiled and linked).
|
||||
* @return bool.
|
||||
*/
|
||||
virtual bool isReady(void) const = 0;
|
||||
|
||||
/**
|
||||
* @brief Return true if the shader program has a geometry shader.
|
||||
* @return bool.
|
||||
*/
|
||||
virtual bool hasGeom(void) const = 0;
|
||||
|
||||
/**
|
||||
* @brief Set this shader as active, so it will be used for the next draw.
|
||||
*/
|
||||
virtual void use(void) = 0;
|
||||
|
||||
/**
|
||||
* @brief Init the shader to make it ready to use (read the fiel, compile, link...).
|
||||
*/
|
||||
virtual void init(void) = 0;
|
||||
|
||||
/**
|
||||
* @brief give a const value to a shader.
|
||||
* @param[in] name The name of the uniform constant.
|
||||
* @param[in] value The value.
|
||||
*/
|
||||
virtual void setBool(const std::string& name, bool value) const = 0;
|
||||
|
||||
/**
|
||||
* @brief give a const value to a shader.
|
||||
* @param[in] name The name of the uniform constant.
|
||||
* @param[in] value The value.
|
||||
*/
|
||||
virtual void setInt(const std::string& name, int value) const = 0;
|
||||
|
||||
/**
|
||||
* @brief give a const value to a shader.
|
||||
* @param[in] name The name of the uniform constant.
|
||||
* @param[in] value The value.
|
||||
*/
|
||||
virtual void setFloat(const std::string& name, float value) const = 0;
|
||||
|
||||
/**
|
||||
* @brief give a const value to a shader.
|
||||
* @param[in] name The name of the uniform constant.
|
||||
* @param[in] value The value.
|
||||
*/
|
||||
virtual void setMat2(const std::string& name, glm::mat2 value) const = 0;
|
||||
|
||||
/**
|
||||
* @brief give a const value to a shader.
|
||||
* @param[in] name The name of the uniform constant.
|
||||
* @param[in] value The value.
|
||||
*/
|
||||
virtual void setMat3(const std::string& name, glm::mat3 value) const = 0;
|
||||
|
||||
/**
|
||||
* @brief give a const value to a shader.
|
||||
* @param[in] name The name of the uniform constant.
|
||||
* @param[in] value The value.
|
||||
*/
|
||||
virtual void setMat4(const std::string& name, glm::mat4 value) const = 0;
|
||||
|
||||
/**
|
||||
* @brief give a const value to a shader.
|
||||
* @param[in] name The name of the uniform constant.
|
||||
* @param[in] value The value.
|
||||
*/
|
||||
virtual void setVec2(const std::string& name, glm::vec2 value) const = 0;
|
||||
|
||||
/**
|
||||
* @brief give a const value to a shader.
|
||||
* @param[in] name The name of the uniform constant.
|
||||
* @param[in] value The value.
|
||||
*/
|
||||
virtual void setVec3(const std::string& name, glm::vec3 value) const = 0;
|
||||
|
||||
/**
|
||||
* @brief give a const value to a shader.
|
||||
* @param[in] name The name of the uniform constant.
|
||||
* @param[in] value The value.
|
||||
*/
|
||||
virtual void setVec4(const std::string& name, glm::vec4 value) const = 0;
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,18 @@
|
||||
#include "CAbstractTexture.hpp"
|
||||
|
||||
void CAbstractTexture::setId(unsigned int newId) {
|
||||
m_glId = newId;
|
||||
}
|
||||
|
||||
CAbstractTexture::CAbstractTexture(ETextureType type) :
|
||||
m_glId(0),
|
||||
m_type(type) {
|
||||
}
|
||||
|
||||
unsigned int CAbstractTexture::getId(void) {
|
||||
return m_glId;
|
||||
}
|
||||
|
||||
ETextureType CAbstractTexture::getType(void) {
|
||||
return m_type;
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
#ifndef CABSTRACTTEXTURE_HPP
|
||||
#define CABSTRACTTEXTURE_HPP
|
||||
|
||||
#define TEXTURE_TYPE_NUMBER 4
|
||||
|
||||
/**
|
||||
* @brief enum representing every type of texture available.
|
||||
*/
|
||||
enum ETextureType {
|
||||
TEXTURE_DIFFUSE,
|
||||
TEXTURE_SPECULAR,
|
||||
TEXTURE_NORMAL,
|
||||
TEXTURE_HEIGHT,
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Class representing a texture.
|
||||
*/
|
||||
class CAbstractTexture {
|
||||
private:
|
||||
|
||||
/**
|
||||
* @brief OpenGL ID.
|
||||
*/
|
||||
unsigned int m_glId;
|
||||
|
||||
/**
|
||||
* @brief The texture's type.
|
||||
*/
|
||||
ETextureType m_type;
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
* @brief Setter to the m_glId member.
|
||||
* @param newId : OpenGL id to set.
|
||||
*/
|
||||
void setId(unsigned int newId);
|
||||
public:
|
||||
|
||||
/**
|
||||
* @brief CAbstractTexture's default constructor, disabled.
|
||||
*/
|
||||
CAbstractTexture(void) = delete;
|
||||
|
||||
/**
|
||||
* @brief CAbstractTexture's constructor.
|
||||
* @param[in] type The texture's type.
|
||||
*/
|
||||
CAbstractTexture(ETextureType type);
|
||||
|
||||
virtual ~CAbstractTexture() {};
|
||||
|
||||
/**
|
||||
* @brief Getter to the m_glId member.
|
||||
* @return The m_glId member.
|
||||
*/
|
||||
unsigned int getId(void);
|
||||
|
||||
/**
|
||||
* @brief Getter to the texture's type.
|
||||
* @return The texture's type.
|
||||
*/
|
||||
ETextureType getType(void);
|
||||
|
||||
/**
|
||||
* @brief Load the texture in the GPU (uses only redefined function in the child classes).
|
||||
*/
|
||||
virtual void init(void) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
74
src/Engine/Core/Component/Graphics/Texture/CImageTexture.cpp
Normal file
74
src/Engine/Core/Component/Graphics/Texture/CImageTexture.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
#include "CImageTexture.hpp"
|
||||
|
||||
CImageTexture::CImageTexture(ETextureType type, std::string filePath) :
|
||||
CAbstractTexture(type),
|
||||
m_filePath(filePath) {
|
||||
}
|
||||
|
||||
std::string CImageTexture::getFilePath(void) {
|
||||
return m_filePath;
|
||||
}
|
||||
|
||||
void CImageTexture::setFilePath(std::string filePath) {
|
||||
m_filePath = filePath;
|
||||
}
|
||||
|
||||
void CImageTexture::init(void) {
|
||||
// Read the image file.
|
||||
SDL_Surface* sdlSurface = IMG_Load(m_filePath.c_str());
|
||||
|
||||
if (sdlSurface == NULL) {
|
||||
throw CLibException(std::string("Image file \"") + m_filePath + std::string("\" can not be opened : ") + std::string(SDL_GetError()));
|
||||
}
|
||||
|
||||
// Create the id.
|
||||
unsigned int id;
|
||||
glGenTextures(1, &id);
|
||||
setId(id);
|
||||
|
||||
// Image format.
|
||||
GLenum internalFormat(0);
|
||||
GLenum externalFormat(0);
|
||||
|
||||
if (sdlSurface->format->BytesPerPixel == 3) {
|
||||
// We use RGB as internal format.
|
||||
internalFormat = GL_RGB;
|
||||
|
||||
// Choose the external format.
|
||||
if (sdlSurface->format->Rmask == 0xff)
|
||||
externalFormat = GL_RGB;
|
||||
else
|
||||
externalFormat = GL_BGR;
|
||||
}
|
||||
else if (sdlSurface->format->BytesPerPixel == 4) {
|
||||
// We use RGBA as internal format.
|
||||
internalFormat = GL_RGBA;
|
||||
|
||||
// Choose the external format.
|
||||
if (sdlSurface->format->Rmask == 0xff)
|
||||
externalFormat = GL_RGBA;
|
||||
else
|
||||
externalFormat = GL_BGRA;
|
||||
}
|
||||
else {
|
||||
SDL_FreeSurface(sdlSurface);
|
||||
throw CRuntimeException("Unknow image internal format.");
|
||||
}
|
||||
|
||||
// Lock the texture to use it.
|
||||
glBindTexture(GL_TEXTURE_2D, getId());
|
||||
|
||||
// Fill the GL texture.
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, sdlSurface->w, sdlSurface->h, 0, externalFormat, GL_UNSIGNED_BYTE, sdlSurface->pixels);
|
||||
|
||||
// Set filters : the near texture have a linear filter.
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
// The far texture have a nearest filter, meaning no filter.
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
// Unlock the texture.
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
// Free the surface.
|
||||
SDL_FreeSurface(sdlSurface);
|
||||
}
|
||||
31
src/Engine/Core/Component/Graphics/Texture/CImageTexture.hpp
Normal file
31
src/Engine/Core/Component/Graphics/Texture/CImageTexture.hpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef CIMAGETEXTURE_HPP
|
||||
#define CIMAGETEXTURE_HPP
|
||||
|
||||
#include "../../../Controller/Exception/CLibException.hpp"
|
||||
#include "../../../Controller/Exception/CRuntimeException.hpp"
|
||||
#include "../../../Controller/Exception/CLogicException.hpp"
|
||||
#include "CAbstractTexture.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <SDL.h>
|
||||
#include <SDL_image.h>
|
||||
#ifdef WIN32
|
||||
#include <gl/glew.h>
|
||||
#else
|
||||
#include <GL/glew.h>
|
||||
#endif
|
||||
|
||||
class CImageTexture : public CAbstractTexture {
|
||||
private:
|
||||
// Path of the file.
|
||||
std::string m_filePath;
|
||||
public:
|
||||
CImageTexture(void) = delete;
|
||||
~CImageTexture(void) {};
|
||||
CImageTexture(ETextureType type, std::string filePath);
|
||||
std::string getFilePath(void);
|
||||
void setFilePath(std::string filePath);
|
||||
virtual void init(void);
|
||||
};
|
||||
|
||||
#endif
|
||||
111
src/Engine/Core/Component/Graphics/Texture/CTextTexture.cpp
Normal file
111
src/Engine/Core/Component/Graphics/Texture/CTextTexture.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
#include "CTextTexture.hpp"
|
||||
|
||||
CTextTexture::CTextTexture(ETextureType type, std::string filePath, std::string text, unsigned int fontSize, glm::vec3 fontColor, glm::bvec4 fontStyle) :
|
||||
CAbstractTexture(type),
|
||||
m_fontFilePath(filePath),
|
||||
m_renderText(text),
|
||||
m_fontSize(fontSize),
|
||||
m_fontColor(fontColor),
|
||||
m_fontStyle(fontStyle) {
|
||||
}
|
||||
|
||||
std::string CTextTexture::getFontFilePath(void) {
|
||||
return m_fontFilePath;
|
||||
}
|
||||
|
||||
void CTextTexture::setFontFilePath(std::string filePath) {
|
||||
m_fontFilePath = filePath;
|
||||
}
|
||||
|
||||
std::string CTextTexture::getText(void) {
|
||||
return m_renderText;
|
||||
}
|
||||
|
||||
void CTextTexture::setText(std::string text) {
|
||||
m_renderText = text;
|
||||
}
|
||||
|
||||
void CTextTexture::init(void) {
|
||||
// Read the font file.
|
||||
TTF_Font* font = TTF_OpenFont(m_fontFilePath.c_str(), m_fontSize);
|
||||
|
||||
SDL_Color color;
|
||||
color.r = m_fontColor.x / 255u;
|
||||
color.g = m_fontColor.y / 255u;
|
||||
color.b = m_fontColor.z / 255u;
|
||||
|
||||
int style = TTF_STYLE_NORMAL;
|
||||
if (m_fontStyle.x) {
|
||||
style |= TTF_STYLE_BOLD;
|
||||
}
|
||||
if (m_fontStyle.y) {
|
||||
style |= TTF_STYLE_ITALIC;
|
||||
}
|
||||
if (m_fontStyle.z) {
|
||||
style |= TTF_STYLE_UNDERLINE;
|
||||
}
|
||||
if (m_fontStyle.z) {
|
||||
style |= TTF_STYLE_STRIKETHROUGH;
|
||||
}
|
||||
TTF_SetFontStyle(font, style);
|
||||
|
||||
SDL_Surface* sdlSurface = TTF_RenderText_Blended(font, m_renderText.c_str(), color);
|
||||
|
||||
if (sdlSurface == NULL) {
|
||||
throw CLibException(std::string("Can not render the text : ") + std::string(SDL_GetError()));
|
||||
}
|
||||
|
||||
// Create the id.
|
||||
unsigned int id;
|
||||
glGenTextures(1, &id);
|
||||
setId(id);
|
||||
|
||||
// Image format.
|
||||
GLenum internalFormat(0);
|
||||
GLenum externalFormat(0);
|
||||
|
||||
if (sdlSurface->format->BytesPerPixel == 3) {
|
||||
// We use RGB as internal format.
|
||||
internalFormat = GL_RGB;
|
||||
|
||||
// Choose the external format.
|
||||
if (sdlSurface->format->Rmask == 0xff)
|
||||
externalFormat = GL_RGB;
|
||||
else
|
||||
externalFormat = GL_BGR;
|
||||
}
|
||||
else if (sdlSurface->format->BytesPerPixel == 4) {
|
||||
// We use RGBA as internal format.
|
||||
internalFormat = GL_RGBA;
|
||||
|
||||
// Choose the external format.
|
||||
if (sdlSurface->format->Rmask == 0xff)
|
||||
externalFormat = GL_RGBA;
|
||||
else
|
||||
externalFormat = GL_BGRA;
|
||||
}
|
||||
else {
|
||||
SDL_FreeSurface(sdlSurface);
|
||||
throw CRuntimeException("Unknow image internal format.");
|
||||
}
|
||||
|
||||
// Lock the texture to use it.
|
||||
glBindTexture(GL_TEXTURE_2D, getId());
|
||||
|
||||
// Fill the GL texture.
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, sdlSurface->w, sdlSurface->h, 0, externalFormat, GL_UNSIGNED_BYTE, sdlSurface->pixels);
|
||||
|
||||
// Set filters : the near texture have a linear filter.
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
// The far texture have a nearest filter, meaning no filter.
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
// Unlock the texture.
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
// Free the font.
|
||||
TTF_CloseFont(font);
|
||||
|
||||
// Free the surface.
|
||||
SDL_FreeSurface(sdlSurface);
|
||||
}
|
||||
47
src/Engine/Core/Component/Graphics/Texture/CTextTexture.hpp
Normal file
47
src/Engine/Core/Component/Graphics/Texture/CTextTexture.hpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#ifndef CTEXTTEXTURE_HPP
|
||||
#define CTEXTTEXTURE_HPP
|
||||
|
||||
#include "../../../Controller/Exception/CLibException.hpp"
|
||||
#include "../../../Controller/Exception/CRuntimeException.hpp"
|
||||
#include "../../../Controller/Exception/CLogicException.hpp"
|
||||
#include "CAbstractTexture.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <SDL.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <SDL_ttf.h>
|
||||
#ifdef WIN32
|
||||
#include <gl/glew.h>
|
||||
#else
|
||||
#include <GL/glew.h>
|
||||
#endif
|
||||
|
||||
class CTextTexture : CAbstractTexture {
|
||||
private:
|
||||
// Path of the file.
|
||||
std::string m_fontFilePath;
|
||||
|
||||
// The size of the font.
|
||||
unsigned int m_fontSize;
|
||||
|
||||
// The color of the font.
|
||||
glm::vec3 m_fontColor;
|
||||
|
||||
// The style of the font (x is bold, y is italic, z is underline, w is strikethrough).
|
||||
glm::bvec4 m_fontStyle;
|
||||
|
||||
// The text to render.
|
||||
std::string m_renderText;
|
||||
|
||||
public:
|
||||
CTextTexture(void) = delete;
|
||||
CTextTexture(ETextureType type, std::string filePath, std::string text, unsigned int fontSize, glm::vec3 fontColor, glm::bvec4 fontStyle);
|
||||
~CTextTexture() {};
|
||||
std::string getFontFilePath(void);
|
||||
void setFontFilePath(std::string filePath);
|
||||
std::string getText(void);
|
||||
void setText(std::string text);
|
||||
virtual void init(void);
|
||||
};
|
||||
|
||||
#endif
|
||||
0
src/Engine/Core/Component/Light/CAbstractLight.cpp
Normal file
0
src/Engine/Core/Component/Light/CAbstractLight.cpp
Normal file
10
src/Engine/Core/Component/Light/CAbstractLight.hpp
Normal file
10
src/Engine/Core/Component/Light/CAbstractLight.hpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef CABSTRACTLIGHT_HPP
|
||||
#define CABSTRACTLIGHT_HPP
|
||||
#include "../CAbstractComponent.hpp"
|
||||
namespace CosmicCore {
|
||||
class CAbstractLight: public CAbstractComponent
|
||||
{
|
||||
|
||||
};
|
||||
}
|
||||
#endif
|
||||
23
src/Engine/Core/Component/Meta/CMetaData.hpp
Normal file
23
src/Engine/Core/Component/Meta/CMetaData.hpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef CMETADATA_HPP
|
||||
#define CMETADATA_HPP
|
||||
|
||||
#include "../CAbstractComponent.hpp"
|
||||
#include "nlohmann/json_fwd.hpp"
|
||||
#include <string>
|
||||
|
||||
namespace CosmicCore {
|
||||
class CMetaData : public CAbstractComponent{
|
||||
private:
|
||||
std::string m_name;
|
||||
std::string m_description;
|
||||
public:
|
||||
CMetaData(CEntity& entity): CAbstractComponent(entity){}
|
||||
CMetaData(CEntity& entity, std::string na, std::string desc): CAbstractComponent(entity), m_name(std::move(na)), m_description(std::move(desc)){}
|
||||
nlohmann::json to_json(){return nlohmann::json();}
|
||||
void setName(std::string name){m_name = std::move(name);}
|
||||
void setDescription(std::string desc){m_description = std::move(desc);}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,6 @@
|
||||
#include "CAbstractRigidbody.hpp"
|
||||
namespace CosmicCore {
|
||||
CAbstractRigidBody::CAbstractRigidBody(CEntity& entity): CAbstractComponent(entity)
|
||||
{
|
||||
}
|
||||
}
|
||||
17
src/Engine/Core/Component/Rigidbody/CAbstractRigidbody.hpp
Normal file
17
src/Engine/Core/Component/Rigidbody/CAbstractRigidbody.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef CABSTRACTRIGIDBODY_HPP
|
||||
#define CABSTRACTRIGIDBODY_HPP
|
||||
#include "../CAbstractComponent.hpp"
|
||||
#include "nlohmann/json_fwd.hpp"
|
||||
namespace CosmicCore {
|
||||
class CAbstractRigidBody: public CAbstractComponent
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief CData's constructor.
|
||||
* @param[in, out] entity The entity to which we want to attach specific informations.
|
||||
*/
|
||||
CAbstractRigidBody(CEntity& entity);
|
||||
nlohmann::json to_json(){return nlohmann::json();};
|
||||
};
|
||||
}
|
||||
#endif
|
||||
32
src/Engine/Core/Component/Script/CAbstractScript.cpp
Normal file
32
src/Engine/Core/Component/Script/CAbstractScript.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "CAbstractScript.hpp"
|
||||
//#include "../../../Controller/CKernel.hpp"
|
||||
|
||||
namespace CosmicCore {
|
||||
//CPlugin<CAbstractScript>* CAbstractScript::m_scriptPLugin = nullptr;
|
||||
|
||||
CAbstractScript::CAbstractScript(CEntity& entity) : CAbstractComponent(entity)
|
||||
{
|
||||
//m_kernelPTR = CKernel::m_kernel;
|
||||
}
|
||||
|
||||
nlohmann::json CAbstractScript::to_json()
|
||||
{
|
||||
nlohmann::json absScript;
|
||||
absScript["scriptName"] = m_scriptName;
|
||||
absScript["ComponentType"] = EComponentType::COMPONENT_SCRIPT;
|
||||
return absScript;
|
||||
}
|
||||
|
||||
CAbstractScript* CAbstractScript::from_json(CEntity& entity, nlohmann::json& j)
|
||||
{
|
||||
/*std::string name = j["scriptName"];
|
||||
CAbstractScript* script = CAbstractScript::m_scriptPLugin->useCreator("instantiate"+name);
|
||||
script->setEntity(entity);
|
||||
script->setKernel(CKernel::m_kernel);
|
||||
script->start();
|
||||
return script;*/
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
33
src/Engine/Core/Component/Script/CAbstractScript.hpp
Normal file
33
src/Engine/Core/Component/Script/CAbstractScript.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef CABSTRACTSCRIPT_HPP
|
||||
#define CABSTRACTSCRIPT_HPP
|
||||
|
||||
#include "../CAbstractComponent.hpp"
|
||||
#include "../../Entity/CEntity.hpp"
|
||||
#include <memory>
|
||||
//#include "../../../Modules/CPlugin.hpp"
|
||||
//#include "../../../Controller/CKernel.fwd.hpp"
|
||||
namespace CosmicCore {
|
||||
//TODO
|
||||
class CAbstractScript : public CAbstractComponent
|
||||
{
|
||||
private:
|
||||
std::string m_scriptName;
|
||||
//CKernel* m_kernelPTR;
|
||||
public:
|
||||
//static CPlugin<CAbstractScript>* m_scriptPLugin;
|
||||
CAbstractScript(CEntity& entity);
|
||||
virtual void start() = 0;
|
||||
virtual void update() = 0;
|
||||
nlohmann::json to_json();
|
||||
static CAbstractScript* from_json(CEntity& entity, nlohmann::json& j);
|
||||
virtual ~CAbstractScript(){};
|
||||
std::string getName() { return m_scriptName; };
|
||||
//CKernel* getKernel() { return m_kernelPTR; };
|
||||
//void setKernel(CKernel* kernel) { m_kernelPTR = kernel; };
|
||||
void setName(std::string name) { m_scriptName = name; };
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
86
src/Engine/Core/Context/CContext.cpp
Normal file
86
src/Engine/Core/Context/CContext.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
#include "CContext.hpp"
|
||||
|
||||
bool CContext::m_screenSaver = false;
|
||||
|
||||
void CContext::init(unsigned int sdlFlags, unsigned int imageFlags) {
|
||||
// Log initialisation.
|
||||
initLog();
|
||||
DEBUG_LOG(trace, "Kernel", "Context", "Log initialized!")
|
||||
|
||||
// SDL Initialization.
|
||||
DEBUG_LOG(trace, "Kernel", "Context", "SDL initialization...")
|
||||
if (SDL_Init(sdlFlags) < 0) {
|
||||
SDL_Quit();
|
||||
HandleException(CLibException(std::string("Unable to initialize SDL: ") + SDL_GetError()), true);
|
||||
}
|
||||
DEBUG_LOG(trace, "Kernel", "Context", "SDL initialized!")
|
||||
|
||||
|
||||
// SDL_Image Initialization.
|
||||
int initted = IMG_Init(imageFlags);
|
||||
DEBUG_LOG(trace, "Kernel", "Context", "SDL_Image initialization...")
|
||||
if ((initted & imageFlags) != imageFlags) {
|
||||
HandleException(CLibException(std::string("Unable to initialize SDL Image: ") + IMG_GetError()), true);
|
||||
}
|
||||
DEBUG_LOG(trace, "Kernel", "Context", "SDL_Image initialized!")
|
||||
|
||||
// SDL_TTF Initialization.
|
||||
DEBUG_LOG(trace, "Kernel", "Context", "SDL_TTF initialization...")
|
||||
if (TTF_Init() == -1) {
|
||||
HandleException(CLibException(std::string("Unable to initialize SDL TTF: ") + TTF_GetError()), true);
|
||||
}
|
||||
DEBUG_LOG(trace, "Kernel", "Context", "SDL_TTF initialized!")
|
||||
}
|
||||
|
||||
bool CContext::isScreenSaverEnable(void) {
|
||||
return m_screenSaver;
|
||||
}
|
||||
|
||||
void CContext::setScreenSaverEnable(bool newScreenSaver) {
|
||||
m_screenSaver = newScreenSaver;
|
||||
if (m_screenSaver) {
|
||||
SDL_EnableScreenSaver();
|
||||
}
|
||||
else {
|
||||
SDL_DisableScreenSaver();
|
||||
}
|
||||
}
|
||||
|
||||
void CContext::quit(void) {
|
||||
TTF_Quit();
|
||||
IMG_Quit();
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
void CContext::initLog(void) {
|
||||
static const std::string COMMON_FMT("[%TimeStamp%][%Severity%]%Message%");
|
||||
|
||||
boost::log::register_simple_formatter_factory< boost::log::trivial::severity_level, char >("Severity");
|
||||
|
||||
// Output message to console.
|
||||
boost::log::add_console_log(
|
||||
std::cout,
|
||||
boost::log::keywords::format = COMMON_FMT,
|
||||
boost::log::keywords::auto_flush = true
|
||||
);
|
||||
|
||||
// Output message to file, rotates when file reached 1mb or at midnight every day. Each log file
|
||||
// is capped at 1mb and total is 50mb.
|
||||
boost::log::add_file_log(
|
||||
boost::log::keywords::file_name = LOG_FILE,
|
||||
boost::log::keywords::rotation_size = LOG_ROTATION_SIZE,
|
||||
boost::log::keywords::max_size = LOG_MAX_SIZE,
|
||||
boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0, 0, 0),
|
||||
boost::log::keywords::format = COMMON_FMT,
|
||||
boost::log::keywords::auto_flush = true
|
||||
);
|
||||
|
||||
boost::log::add_common_attributes();
|
||||
|
||||
// Only output message with INFO or higher severity in Release.
|
||||
#ifndef _DEBUG
|
||||
boost::log::core::get()->set_filter(
|
||||
boost::log::trivial::severity >= boost::log::trivial::info
|
||||
);
|
||||
#endif
|
||||
}
|
||||
72
src/Engine/Core/Context/CContext.hpp
Normal file
72
src/Engine/Core/Context/CContext.hpp
Normal file
@@ -0,0 +1,72 @@
|
||||
#ifndef CCONTEXT_HPP
|
||||
#define CCONTEXT_HPP
|
||||
|
||||
#include "../Controller/Exception/CLibException.hpp"
|
||||
#include "../Controller/Exception/CExceptionManager.hpp"
|
||||
|
||||
#define BOOST_LOG_DYN_LINK 1
|
||||
#include <boost/log/trivial.hpp>
|
||||
#include <boost/log/utility/setup.hpp>
|
||||
|
||||
#include <SDL.h>
|
||||
#ifdef WIN32
|
||||
#include <SDL_Image.h>
|
||||
#else
|
||||
#include <SDL_image.h>
|
||||
#endif
|
||||
#include <SDL_ttf.h>
|
||||
#include <string>
|
||||
|
||||
#ifdef _DEBUG
|
||||
#define DEBUG_LOG(level, object, component, message) \
|
||||
BOOST_LOG_TRIVIAL(level) << "[" << object << "]" << "[" << component << "] : " << message;
|
||||
#else
|
||||
#define DEBUG_LOG(level, object, component, message)
|
||||
|
||||
#endif
|
||||
#define ERROR_LOG(level, object, component, message) \
|
||||
BOOST_LOG_TRIVIAL(level) << "[" << object << "]" << "[" << component << "] : " << message;
|
||||
|
||||
#define LOG_FILE "CosmicEngine_%3N.log"
|
||||
#define LOG_ROTATION_SIZE 1 * 1024 * 1024
|
||||
#define LOG_MAX_SIZE 50 * 1024 * 1024
|
||||
|
||||
/**
|
||||
* @file CContext.hpp
|
||||
* @brief File of CContext, a class reprensenting a SDL context, using SDL2 lib.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Class reprensenting a SDL context, using SDL2 lib.
|
||||
*/
|
||||
class CContext {
|
||||
private:
|
||||
// Say if the screenSaver is enabled.
|
||||
static bool m_screenSaver;
|
||||
|
||||
public:
|
||||
CContext(void) = delete;
|
||||
|
||||
static void init(unsigned int sdlFlags, unsigned int imageFlags);
|
||||
|
||||
static void quit(void);
|
||||
/**
|
||||
* @brief Say if the screen saver is enabled.
|
||||
* @return bool, a boolean which say the screen saver status.
|
||||
*/
|
||||
static bool isScreenSaverEnable(void);
|
||||
|
||||
/**
|
||||
* @brief Set the screen saver status.
|
||||
* @param[in] newScreenSaver New value of screen saver status.
|
||||
*/
|
||||
static void setScreenSaverEnable(bool newScreenSaver);
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief Init the logging system.
|
||||
*/
|
||||
static void initLog(void);
|
||||
};
|
||||
|
||||
#endif
|
||||
132
src/Engine/Core/Context/Window/CGameWindow.cpp
Normal file
132
src/Engine/Core/Context/Window/CGameWindow.cpp
Normal file
@@ -0,0 +1,132 @@
|
||||
#include "CGameWindow.hpp"
|
||||
|
||||
CGameWindow::CGameWindow(std::string title, std::string iconPath, unsigned int width, unsigned int height, bool resizable, bool fullscreen) :
|
||||
CWindow(title, iconPath, width, height, false, false, SDL_WINDOW_OPENGL),
|
||||
m_grabCursor(false),
|
||||
m_relativeCursor(false),
|
||||
m_msaa(0) {
|
||||
}
|
||||
|
||||
CGameWindow::CGameWindow(std::string title, SDL_Window* window) :
|
||||
CWindow(title, window, SDL_WINDOW_OPENGL, 0),
|
||||
m_grabCursor(false),
|
||||
m_relativeCursor(false),
|
||||
m_msaa(0) {
|
||||
}
|
||||
|
||||
CGameWindow::~CGameWindow(void) {
|
||||
}
|
||||
|
||||
SDL_GLContext CGameWindow::getOpenGLContext(void) {
|
||||
if (getWindow() == nullptr) {
|
||||
throw CLogicException("The window must be initialized before using this method !");
|
||||
}
|
||||
else {
|
||||
SDL_GLContext out = SDL_GL_CreateContext(getWindow());
|
||||
if (out == nullptr) {
|
||||
throw CLibException(std::string("Unable to create OpenGL context: ") + SDL_GetError());
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
bool CGameWindow::isCursorGrabbed(void) const {
|
||||
return m_grabCursor;
|
||||
}
|
||||
|
||||
void CGameWindow::setCursorGrabbed(bool newMouseGrab) {
|
||||
m_grabCursor = newMouseGrab;
|
||||
|
||||
if (m_relativeCursor && !m_grabCursor) {
|
||||
setCursorRelative(false);
|
||||
}
|
||||
|
||||
if (getWindow() != nullptr) {
|
||||
if (m_grabCursor) {
|
||||
SDL_SetWindowGrab(getWindow(), SDL_TRUE);
|
||||
}
|
||||
else {
|
||||
SDL_SetWindowGrab(getWindow(), SDL_FALSE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool CGameWindow::isCursorRelative(void) const {
|
||||
return m_relativeCursor;
|
||||
}
|
||||
|
||||
void CGameWindow::setCursorRelative(bool newMouseRelative) {
|
||||
m_relativeCursor = newMouseRelative;
|
||||
|
||||
if (m_relativeCursor && !m_grabCursor) {
|
||||
setCursorGrabbed(true);
|
||||
}
|
||||
|
||||
if (getWindow() != nullptr) {
|
||||
if (m_relativeCursor) {
|
||||
SDL_SetRelativeMouseMode(SDL_TRUE);
|
||||
}
|
||||
else {
|
||||
SDL_SetRelativeMouseMode(SDL_FALSE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char CGameWindow::getAntiAliasing(void) const {
|
||||
return m_msaa;
|
||||
}
|
||||
|
||||
void CGameWindow::setAntiAliasing(unsigned char newMsaa) {
|
||||
if (getWindow() != nullptr) {
|
||||
throw CLogicException("The window must be uninitialized before using this method !");
|
||||
}
|
||||
else {
|
||||
if (newMsaa == 2 || newMsaa == 4 || newMsaa == 8 || newMsaa == 16) {
|
||||
m_msaa = newMsaa;
|
||||
}
|
||||
else {
|
||||
m_msaa = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CGameWindow::preinitialization(void) {
|
||||
// We want OpenGL version 3.3.
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
|
||||
|
||||
// Anti-aliasing Configuration.
|
||||
if (m_msaa == 0) {
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
|
||||
}
|
||||
else {
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, m_msaa);
|
||||
}
|
||||
|
||||
// Double Buffer.
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
|
||||
// Depht Buffer.
|
||||
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
||||
|
||||
}
|
||||
|
||||
void CGameWindow::postinitialization(void) {
|
||||
|
||||
// Set the cursorGrab property.
|
||||
if (m_grabCursor) {
|
||||
SDL_SetWindowGrab(getWindow(), SDL_TRUE);
|
||||
}
|
||||
else {
|
||||
SDL_SetWindowGrab(getWindow(), SDL_FALSE);
|
||||
}
|
||||
|
||||
// Set the cursorRelative property.
|
||||
if (m_relativeCursor) {
|
||||
SDL_SetRelativeMouseMode(SDL_TRUE);
|
||||
}
|
||||
else {
|
||||
SDL_SetRelativeMouseMode(SDL_FALSE);
|
||||
}
|
||||
}
|
||||
119
src/Engine/Core/Context/Window/CGameWindow.hpp
Normal file
119
src/Engine/Core/Context/Window/CGameWindow.hpp
Normal file
@@ -0,0 +1,119 @@
|
||||
#ifndef CGAMEWINDOW_HPP
|
||||
#define CGAMEWINDOW_HPP
|
||||
|
||||
#include "CWindow.hpp"
|
||||
#include "../../Controller/Exception/CLibException.hpp"
|
||||
#include "../../Controller/Exception/CLogicException.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <exception>
|
||||
#include <stdexcept>
|
||||
#include <SDL.h>
|
||||
#ifdef WIN32
|
||||
#include <SDL_Image.h>
|
||||
#else
|
||||
#include <SDL_image.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @file CGameWindow.hpp
|
||||
* @brief File of CGameWindow, a class reprensenting a OpenGL Window, using SDL2 lib.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Class reprensenting a OpenGL Window, using SDL2 lib.
|
||||
*/
|
||||
class CGameWindow : public CWindow {
|
||||
private:
|
||||
// Say if the mouse is grabbed.
|
||||
bool m_grabCursor;
|
||||
|
||||
// Say if the mouse is in relative mode.
|
||||
bool m_relativeCursor;
|
||||
|
||||
// The anti-aliasing.
|
||||
unsigned char m_msaa;
|
||||
|
||||
public:
|
||||
CGameWindow(void) = delete;
|
||||
CGameWindow(const CGameWindow& param) = delete;
|
||||
CGameWindow& operator=(const CGameWindow& param) = delete;
|
||||
|
||||
/**
|
||||
* @brief The default constructor, build a non visible window with size of widthxheight.
|
||||
* @param[in] title The title of the window.
|
||||
* @param[in] iconPath The iconPath of the window.
|
||||
* @param[in] width The width of the window.
|
||||
* @param[in] height The height of the window.
|
||||
* @param[in] resizable The define the resizable property of the window.
|
||||
* @param[in] fullscreen The define the fullscreen property of the window.
|
||||
* @param[in] sdlFlags Allow to add SDL flags.
|
||||
* @param[in] imageFlags Allow to add SDL_Image flags.
|
||||
*/
|
||||
CGameWindow(std::string title, std::string iconPath, unsigned int width, unsigned int height, bool resizable, bool fullscreen);
|
||||
|
||||
CGameWindow(std::string title, SDL_Window* window);
|
||||
|
||||
/**
|
||||
* @brief The destructor.
|
||||
*/
|
||||
~CGameWindow(void);
|
||||
|
||||
/**
|
||||
* @brief Say if the mouse is grabbed.
|
||||
* @return bool, a boolean which say the mouse grab status.
|
||||
*/
|
||||
bool isCursorGrabbed(void) const;
|
||||
|
||||
/**
|
||||
* @brief Set the mouse grab status.
|
||||
* @param[in] newMouseGrab New value of mouse grab status.
|
||||
*/
|
||||
void setCursorGrabbed(bool newMouseGrab);
|
||||
|
||||
/**
|
||||
* @brief Say if the mouse is relative.
|
||||
* @return bool, a boolean which say the mouse relative status.
|
||||
*/
|
||||
bool isCursorRelative(void) const;
|
||||
|
||||
/**
|
||||
* @brief Set the mouse relative status, if true, the cursor grabb is also forced to true if not.
|
||||
* @param[in] newMouseRelative New value of mouse relative status.
|
||||
*/
|
||||
void setCursorRelative(bool newMouseRelative);
|
||||
|
||||
/**
|
||||
* @brief Get the MSAA value.
|
||||
* @return unsigned char, the MSAA value.
|
||||
*/
|
||||
unsigned char getAntiAliasing(void) const;
|
||||
|
||||
/**
|
||||
* @brief Set the MSAA of the window.
|
||||
* @param[in] newMsaa New value of MSAA, must be 2, 4, 8, 16.
|
||||
* @post if newMsaa not equal 2, 4, 8, 16, the value is set to 0.
|
||||
*/
|
||||
void setAntiAliasing(unsigned char newMsaa);
|
||||
|
||||
/**
|
||||
* @brief Launched before the creation of the window, usefull for setting OpenGL attribute.
|
||||
*/
|
||||
virtual void preinitialization();
|
||||
|
||||
/**
|
||||
* @brief Launched after the creation of the window, usefull for setting others attribute.
|
||||
*/
|
||||
virtual void postinitialization();
|
||||
|
||||
/**
|
||||
* @brief Get the OpenGLContext of the window.
|
||||
* @return SDL_GLContext the OpenGL context of the window, in SDL2 format, we need to destroy it after using, with SDL_GL_DeleteContext(SDL_GLContext);.
|
||||
* @post Give a error if the window is not initialized.
|
||||
*/
|
||||
SDL_GLContext getOpenGLContext(void);
|
||||
};
|
||||
|
||||
#endif
|
||||
40
src/Engine/Core/Context/Window/CLoadingWindow.cpp
Normal file
40
src/Engine/Core/Context/Window/CLoadingWindow.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include "CLoadingWindow.hpp"
|
||||
|
||||
CLoadingWindow::CLoadingWindow(std::string title, std::string iconPath, std::string splashPath, unsigned int width, unsigned int height) :
|
||||
CWindow(title, iconPath, width, height, false, false, SDL_WINDOW_BORDERLESS | SDL_WINDOW_ALWAYS_ON_TOP),
|
||||
m_splashPath(splashPath),
|
||||
m_psurface(nullptr) {
|
||||
}
|
||||
|
||||
CLoadingWindow::~CLoadingWindow(void) {
|
||||
}
|
||||
|
||||
void CLoadingWindow::preinitialization(void) {
|
||||
}
|
||||
|
||||
void CLoadingWindow::postinitialization(void) {
|
||||
m_psurface = SDL_GetWindowSurface(getWindow());
|
||||
|
||||
// Set the icon.
|
||||
SDL_Surface* splash;
|
||||
splash = IMG_Load(m_splashPath.c_str());
|
||||
if (splash != nullptr) {
|
||||
SDL_Renderer* renderer = SDL_CreateRenderer(getWindow(), -1, SDL_RENDERER_ACCELERATED);
|
||||
if (renderer == NULL) {
|
||||
throw CLibException(std::string("Unable to create renderer: ") + SDL_GetError());
|
||||
}
|
||||
|
||||
SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, splash);
|
||||
if (tex == NULL) {
|
||||
throw CLibException(std::string("Unable to create texture: ") + SDL_GetError());
|
||||
}
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
|
||||
|
||||
SDL_FreeSurface(splash);
|
||||
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_RenderCopy(renderer, tex, NULL, NULL);
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
}
|
||||
67
src/Engine/Core/Context/Window/CLoadingWindow.hpp
Normal file
67
src/Engine/Core/Context/Window/CLoadingWindow.hpp
Normal file
@@ -0,0 +1,67 @@
|
||||
#ifndef CLOADINGWINDOW_HPP
|
||||
#define CLOADINGWINDOW_HPP
|
||||
|
||||
#include "CWindow.hpp"
|
||||
#include "../../Controller/Exception/CLibException.hpp"
|
||||
#include "../../Controller/Exception/CLogicException.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <exception>
|
||||
#include <stdexcept>
|
||||
#include <SDL.h>
|
||||
#ifdef WIN32
|
||||
#include <SDL_Image.h>
|
||||
#else
|
||||
#include <SDL_image.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @file CLoadingWindow.hpp
|
||||
* @brief File of CWindow, a class reprensenting a loading Window, using SDL2 lib.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Class reprensenting a loading Window, using SDL2 lib.
|
||||
*/
|
||||
class CLoadingWindow : public CWindow {
|
||||
private:
|
||||
// The path to the splash image.
|
||||
std::string m_splashPath;
|
||||
|
||||
// The pointer to the surface.
|
||||
SDL_Surface* m_psurface;
|
||||
|
||||
public:
|
||||
CLoadingWindow(void) = delete;
|
||||
CLoadingWindow(const CLoadingWindow& param) = delete;
|
||||
CLoadingWindow& operator=(const CLoadingWindow& param) = delete;
|
||||
|
||||
/**
|
||||
* @brief The default constructor, build a non visible window with size of widthxheight.
|
||||
* @param[in] title The title of the window.
|
||||
* @param[in] iconPath The iconPath of the window.
|
||||
* @param[in] splashPath The splashPath of the window.
|
||||
* @param[in] width The width of the window.
|
||||
* @param[in] height The height of the window.
|
||||
*/
|
||||
CLoadingWindow(std::string title, std::string iconPath, std::string splashPath, unsigned int width, unsigned int height);
|
||||
|
||||
/**
|
||||
* @brief The destructor.
|
||||
*/
|
||||
~CLoadingWindow(void);
|
||||
|
||||
/**
|
||||
* @brief Launched before the creation of the window, usefull for setting OpenGL attribute.
|
||||
*/
|
||||
virtual void preinitialization();
|
||||
|
||||
/**
|
||||
* @brief Launched after the creation of the window, usefull for setting others attribute.
|
||||
*/
|
||||
virtual void postinitialization();
|
||||
};
|
||||
|
||||
#endif
|
||||
183
src/Engine/Core/Context/Window/CWindow.cpp
Normal file
183
src/Engine/Core/Context/Window/CWindow.cpp
Normal file
@@ -0,0 +1,183 @@
|
||||
#include "CWindow.hpp"
|
||||
|
||||
CWindow::CWindow(std::string title, std::string iconPath, unsigned int width, unsigned int height, bool resizable, bool fullscreen, unsigned int sdlFlags) :
|
||||
m_title(title),
|
||||
m_iconPath(iconPath),
|
||||
m_size(width, height),
|
||||
m_visible(false),
|
||||
m_fullscreen(fullscreen),
|
||||
m_resizable(resizable),
|
||||
m_sdlFlags(sdlFlags) {
|
||||
m_pwindow = nullptr;
|
||||
}
|
||||
|
||||
CWindow::CWindow(std::string title, SDL_Window* window, unsigned int sdlFlags, unsigned int imageFlags): m_sdlFlags(sdlFlags), m_size(600,400)
|
||||
{
|
||||
m_title = title;
|
||||
m_pwindow = window;
|
||||
}
|
||||
|
||||
CWindow::~CWindow(void) {
|
||||
if (m_pwindow != nullptr) {
|
||||
SDL_DestroyWindow(m_pwindow);
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Window* CWindow::getWindow(void) {
|
||||
return m_pwindow;
|
||||
}
|
||||
|
||||
bool CWindow::isVisible(void) const {
|
||||
return m_visible;
|
||||
}
|
||||
|
||||
void CWindow::setVisible(bool newVisible) {
|
||||
m_visible = newVisible;
|
||||
if (m_pwindow != nullptr) {
|
||||
if (m_visible) {
|
||||
SDL_ShowWindow(m_pwindow);
|
||||
}
|
||||
else {
|
||||
SDL_HideWindow(m_pwindow);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string CWindow::getTitle(void) const {
|
||||
return m_title;
|
||||
}
|
||||
|
||||
void CWindow::setTitle(std::string newTitle) {
|
||||
m_title = newTitle;
|
||||
if (m_pwindow != nullptr) {
|
||||
SDL_SetWindowTitle(m_pwindow, m_title.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
std::string CWindow::getIconPath(void) const {
|
||||
return m_iconPath;
|
||||
}
|
||||
|
||||
void CWindow::setIconPath(std::string newIconPath) {
|
||||
m_iconPath = newIconPath;
|
||||
if (m_pwindow != nullptr) {
|
||||
SDL_Surface* image;
|
||||
image = IMG_Load(m_iconPath.c_str());
|
||||
if (image != nullptr) {
|
||||
SDL_SetWindowIcon(m_pwindow, image);
|
||||
SDL_FreeSurface(image);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int CWindow::getWidth(void) const {
|
||||
return m_size.first;
|
||||
}
|
||||
|
||||
unsigned int CWindow::getHeight(void) const {
|
||||
return m_size.second;
|
||||
}
|
||||
|
||||
std::pair<unsigned int, unsigned int> CWindow::getSize(void) const {
|
||||
return m_size;
|
||||
}
|
||||
|
||||
void CWindow::setSize(std::pair<unsigned int, unsigned int> newSize) {
|
||||
m_size = newSize;
|
||||
if (m_pwindow != nullptr) {
|
||||
SDL_SetWindowSize(m_pwindow, m_size.first, m_size.second);
|
||||
}
|
||||
}
|
||||
|
||||
void CWindow::setSize(unsigned int newWidth, unsigned int newHeight) {
|
||||
m_size.first = newWidth;
|
||||
m_size.second = newHeight;
|
||||
if (m_pwindow != nullptr) {
|
||||
SDL_SetWindowSize(m_pwindow, m_size.first, m_size.second);
|
||||
}
|
||||
}
|
||||
|
||||
bool CWindow::isResizable(void) const {
|
||||
return m_resizable;
|
||||
}
|
||||
|
||||
void CWindow::setResizable(bool newResizable) {
|
||||
m_resizable = newResizable;
|
||||
if (m_pwindow != nullptr) {
|
||||
if (m_resizable) {
|
||||
SDL_SetWindowResizable(m_pwindow, SDL_TRUE);
|
||||
}
|
||||
else {
|
||||
SDL_SetWindowResizable(m_pwindow, SDL_FALSE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool CWindow::isFullscreen(void) const {
|
||||
return m_fullscreen;
|
||||
}
|
||||
|
||||
void CWindow::setFullscreen(bool newFullscreen) {
|
||||
m_fullscreen = newFullscreen;
|
||||
if (m_pwindow != nullptr) {
|
||||
// Set the fullscreen property.
|
||||
if (m_fullscreen) {
|
||||
SDL_SetWindowFullscreen(m_pwindow, SDL_WINDOW_FULLSCREEN);
|
||||
}
|
||||
else {
|
||||
SDL_SetWindowFullscreen(m_pwindow, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool CWindow::isInitialized(void) const {
|
||||
return m_pwindow != nullptr;
|
||||
}
|
||||
|
||||
void CWindow::initialization(void) {
|
||||
// Lancement de la preinitialisation.
|
||||
preinitialization();
|
||||
|
||||
// Création de la fenetre.
|
||||
m_pwindow = SDL_CreateWindow(m_title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, m_size.first, m_size.second, SDL_WINDOW_HIDDEN | m_sdlFlags);
|
||||
|
||||
// Handle error.
|
||||
if (m_pwindow == nullptr) {
|
||||
throw CLibException(std::string("Unable to create window: ") + SDL_GetError());
|
||||
}
|
||||
|
||||
// Set the icon.
|
||||
SDL_Surface* image;
|
||||
image = IMG_Load(m_iconPath.c_str());
|
||||
if (image != nullptr) {
|
||||
SDL_SetWindowIcon(m_pwindow, image);
|
||||
SDL_FreeSurface(image);
|
||||
}
|
||||
|
||||
// Set the rezisable property.
|
||||
if (m_resizable) {
|
||||
SDL_SetWindowResizable(m_pwindow, SDL_TRUE);
|
||||
}
|
||||
else {
|
||||
SDL_SetWindowResizable(m_pwindow, SDL_FALSE);
|
||||
}
|
||||
|
||||
// Set the fullscreen property.
|
||||
if (m_fullscreen) {
|
||||
SDL_SetWindowFullscreen(m_pwindow, SDL_WINDOW_FULLSCREEN);
|
||||
}
|
||||
else {
|
||||
SDL_SetWindowFullscreen(m_pwindow, 0);
|
||||
}
|
||||
|
||||
// Set the visible property.
|
||||
if (m_visible) {
|
||||
SDL_ShowWindow(m_pwindow);
|
||||
}
|
||||
else {
|
||||
SDL_HideWindow(m_pwindow);
|
||||
}
|
||||
|
||||
// Lancement de la postinitialisation.
|
||||
postinitialization();
|
||||
}
|
||||
204
src/Engine/Core/Context/Window/CWindow.hpp
Normal file
204
src/Engine/Core/Context/Window/CWindow.hpp
Normal file
@@ -0,0 +1,204 @@
|
||||
#ifndef CWINDOW_HPP
|
||||
#define CWINDOW_HPP
|
||||
|
||||
#include "../../Controller/Exception/CLibException.hpp"
|
||||
#include "../../Controller/Exception/CLogicException.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <exception>
|
||||
#include <stdexcept>
|
||||
#include <SDL.h>
|
||||
#ifdef WIN32
|
||||
#include <SDL_Image.h>
|
||||
#else
|
||||
#include <SDL_image.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @file CWindow.hpp
|
||||
* @brief File of CWindow, a class reprensenting a Window, using SDL2 lib.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Class reprensenting a Window, using SDL2 lib.
|
||||
*/
|
||||
class CWindow {
|
||||
private:
|
||||
// The title of the window.
|
||||
std::string m_title;
|
||||
|
||||
// The path to the icon.
|
||||
std::string m_iconPath;
|
||||
|
||||
// First is the width, second is the height.
|
||||
std::pair<unsigned int, unsigned int> m_size;
|
||||
|
||||
// Say if the window is fullscreen.
|
||||
bool m_fullscreen;
|
||||
|
||||
// Say if the window is visible.
|
||||
bool m_visible;
|
||||
|
||||
// Say if the window is resizable.
|
||||
bool m_resizable;
|
||||
|
||||
// SDL flags for creating the window.
|
||||
unsigned int m_sdlFlags;
|
||||
|
||||
// The pointer to the window.
|
||||
SDL_Window* m_pwindow;
|
||||
|
||||
public:
|
||||
CWindow(void) = delete;
|
||||
CWindow(const CWindow& param) = delete;
|
||||
CWindow& operator=(const CWindow& param) = delete;
|
||||
|
||||
/**
|
||||
* @brief The default constructor, build a non visible window with size of widthxheight.
|
||||
* @param[in] title The title of the window.
|
||||
* @param[in] iconPath The iconPath of the window.
|
||||
* @param[in] width The width of the window.
|
||||
* @param[in] height The height of the window.
|
||||
* @param[in] sdlFlags Allow to add SDL flags.
|
||||
*/
|
||||
CWindow(std::string title, std::string iconPath, unsigned int width, unsigned int height, bool resizable, bool fullscreen, unsigned int sdlFlags);
|
||||
|
||||
/**
|
||||
* @brief The default constructor, build a non visible window with size of widthxheight.
|
||||
* @param[in] title The title of the window.
|
||||
* @param[in] The window to embed the game in.
|
||||
* @param[in] sdlFlags Allow to add SDL flags.
|
||||
* @param[in] imageFlags Allow to add SDL_Image flags.
|
||||
*/
|
||||
CWindow(std::string title, SDL_Window* window, unsigned int sdlFlags, unsigned int imageFlags);
|
||||
|
||||
|
||||
/**
|
||||
* @brief The destructor.
|
||||
*/
|
||||
~CWindow(void);
|
||||
|
||||
/**
|
||||
* @brief Get the SDL_Window* of the window.
|
||||
* @return SDL_Window The window, in SDL2 format.
|
||||
*/
|
||||
SDL_Window* getWindow(void);
|
||||
|
||||
void setWindow(SDL_Window* window){m_pwindow = window; };
|
||||
/**
|
||||
* @brief Say if the window is fullscreen.
|
||||
* @return bool, a boolean which say the fullscreen status.
|
||||
*/
|
||||
bool isFullscreen(void) const;
|
||||
|
||||
/**
|
||||
* @brief Set the fullscreen status.
|
||||
* @param[in] newFullscreen New value of fullscreen status.
|
||||
*/
|
||||
void setFullscreen(bool newFullscreen);
|
||||
|
||||
/**
|
||||
* @brief Say if the window is visible.
|
||||
* @return bool, a boolean which say the visible status.
|
||||
*/
|
||||
bool isVisible(void) const;
|
||||
|
||||
/**
|
||||
* @brief Set the visibility status.
|
||||
* @param[in] newVisible New value of visibility status.
|
||||
*/
|
||||
void setVisible(bool newVisible);
|
||||
|
||||
/**
|
||||
* @brief Get the window icon path.
|
||||
* @return std::string, the path.
|
||||
*/
|
||||
std::string getIconPath(void) const;
|
||||
|
||||
/**
|
||||
* @brief Get the window title.
|
||||
* @return std::string, the title.
|
||||
*/
|
||||
std::string getTitle(void) const;
|
||||
|
||||
/**
|
||||
* @brief Change the window title.
|
||||
* @param[in] newTitle New title of the window.
|
||||
*/
|
||||
void setTitle(std::string newTitle);
|
||||
|
||||
/**
|
||||
* @brief Change the window icon.
|
||||
* @param[in] newIconPath New icon path of the window.
|
||||
*/
|
||||
void setIconPath(std::string newIconPath);
|
||||
|
||||
/**
|
||||
* @brief Get the window width.
|
||||
* @return unsigned int, the window width (in pixel).
|
||||
*/
|
||||
unsigned int getWidth(void) const;
|
||||
|
||||
/**
|
||||
* @brief Get the window height.
|
||||
* @return unsigned int, the window height (in pixel).
|
||||
*/
|
||||
unsigned int getHeight(void) const;
|
||||
|
||||
/**
|
||||
* @brief Get the window size.
|
||||
* @return std::pair<unsigned int, unsigned int>, the window size (in pixel).
|
||||
*/
|
||||
std::pair<unsigned int, unsigned int> getSize(void) const;
|
||||
|
||||
/**
|
||||
* @brief Set the window size.
|
||||
* @param[in] newSize New value of window size.
|
||||
*/
|
||||
void setSize(std::pair<unsigned int, unsigned int> newSize);
|
||||
|
||||
/**
|
||||
* @brief Set the window size.
|
||||
* @param[in] newWidth New value of window width.
|
||||
* @param[in] newHeight New value of window height.
|
||||
*/
|
||||
void setSize(unsigned int newWidth, unsigned int newHeight);
|
||||
|
||||
/**
|
||||
* @brief Say if the window is resizable.
|
||||
* @return bool, a boolean which say the resizable status.
|
||||
*/
|
||||
bool isResizable(void) const;
|
||||
|
||||
/**
|
||||
* @brief Set the rezisable status.
|
||||
* @param[in] newResizable New value of rezisable status.
|
||||
*/
|
||||
void setResizable(bool newResizable);
|
||||
|
||||
/**
|
||||
* @brief Say if the window is initialized.
|
||||
* @return bool, a boolean which say the initialization status.
|
||||
*/
|
||||
bool isInitialized(void) const;
|
||||
|
||||
/**
|
||||
* @brief Initialize the window.
|
||||
* @post Give a error if the windows is already init.
|
||||
*/
|
||||
void initialization();
|
||||
|
||||
/**
|
||||
* @brief Launched before the creation of the window, usefull for setting OpenGL attribute.
|
||||
*/
|
||||
virtual void preinitialization() = 0;
|
||||
|
||||
/**
|
||||
* @brief Launched after the creation of the window, usefull for setting others attribute.
|
||||
*/
|
||||
virtual void postinitialization() = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
32
src/Engine/Core/Entity/CEntity.cpp
Normal file
32
src/Engine/Core/Entity/CEntity.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "CEntity.hpp"
|
||||
#include "nlohmann/json_fwd.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace CosmicCore
|
||||
{
|
||||
|
||||
CEntity::CEntity(ECManager& registry, EntityId handle) : CSerializable(), m_registryReference(registry), m_handle(handle)
|
||||
{
|
||||
}
|
||||
|
||||
CEntity::CEntity(const CEntity& cop): m_registryReference(cop.m_registryReference), m_handle(cop.m_handle){
|
||||
|
||||
}
|
||||
|
||||
std::weak_ptr<CScene> CEntity::getScene()
|
||||
{
|
||||
return m_scene;
|
||||
}
|
||||
|
||||
void CEntity::setScene(std::weak_ptr<CScene> s)
|
||||
{
|
||||
m_scene = s;
|
||||
}
|
||||
|
||||
CEntity CEntity::getParent() {
|
||||
//to implement
|
||||
}
|
||||
|
||||
nlohmann::json CEntity::to_json(){return nlohmann::json();};
|
||||
}
|
||||
77
src/Engine/Core/Entity/CEntity.hpp
Normal file
77
src/Engine/Core/Entity/CEntity.hpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#ifndef CENTITY_HPP
|
||||
#define CENTITY_HPP
|
||||
|
||||
#include <forward_list>
|
||||
#include <memory>
|
||||
#include "../Utils/CSerializable.hpp"
|
||||
#include <entt/entt.hpp>
|
||||
|
||||
namespace CosmicCore
|
||||
{
|
||||
class CScene;
|
||||
|
||||
class CEntity : public CSerializable {
|
||||
using ECManager = entt::registry;
|
||||
using EntityId = entt::entity;
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* @brief A pointer to the scene containing the entity.
|
||||
*/
|
||||
std::weak_ptr<CScene> m_scene;
|
||||
|
||||
ECManager& m_registryReference;
|
||||
|
||||
EntityId m_handle;
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* @brief CEntity's default constructor, disabled.
|
||||
*/
|
||||
CEntity() = delete;
|
||||
|
||||
CEntity(ECManager& registry, EntityId handle);
|
||||
|
||||
CEntity(const CEntity& cop);
|
||||
/**
|
||||
* @brief CEntity's destructor.
|
||||
*/
|
||||
~CEntity(void) = default;
|
||||
|
||||
/**
|
||||
* @brief Getter to the parent entity.
|
||||
* @return a pointer to the parent entity m_parent. nullptr if there is not.
|
||||
*/
|
||||
CEntity getParent();
|
||||
|
||||
template<typename compType, typename... Args>
|
||||
void addComponent(std::forward_list<Args...> args)
|
||||
{
|
||||
m_registryReference.emplace<compType>(m_handle, *this, args);
|
||||
}
|
||||
|
||||
template<typename compType>
|
||||
void addComponent()
|
||||
{
|
||||
m_registryReference.emplace<compType>(m_handle, *this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Getter to the entity's scene.
|
||||
* @return The pointer m_scene.
|
||||
*/
|
||||
std::weak_ptr<CScene> getScene();
|
||||
|
||||
/**
|
||||
* @brief Setter of the scene.
|
||||
* @param[in, out] e The scene in which the entity will be set.
|
||||
*/
|
||||
void setScene(std::weak_ptr<CScene> s);
|
||||
|
||||
nlohmann::json to_json();
|
||||
};
|
||||
|
||||
}
|
||||
#endif
|
||||
56
src/Engine/Core/Exception/CException.cpp
Normal file
56
src/Engine/Core/Exception/CException.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "CException.hpp"
|
||||
|
||||
void CException::makeFullMessage(void) {
|
||||
std::stringstream sstring;
|
||||
sstring << "Error : " << m_code << std::endl << m_message << std::endl;
|
||||
boost::stacktrace::stacktrace stacktrace = boost::stacktrace::stacktrace();
|
||||
std::size_t frames = stacktrace.size();
|
||||
|
||||
for (size_t i = 4; i < frames; i++) {
|
||||
sstring << " #" << i - 4 << " : " << stacktrace[i].name() << " : ";
|
||||
if (stacktrace[i].source_file().empty()) {
|
||||
sstring << "no source";
|
||||
}
|
||||
else {
|
||||
sstring << stacktrace[i].source_file() << " at line : " << stacktrace[i].source_line();
|
||||
}
|
||||
sstring << " (" << stacktrace[i].address() << ')' << std::endl;
|
||||
}
|
||||
|
||||
m_fullMessage = sstring.str();
|
||||
}
|
||||
|
||||
CException::CException(void) noexcept :
|
||||
std::exception(),
|
||||
m_message("A error occurred"),
|
||||
m_code(0),
|
||||
m_fullMessage() {
|
||||
makeFullMessage();
|
||||
}
|
||||
|
||||
CException::CException(std::string message, unsigned int code) noexcept :
|
||||
std::exception(),
|
||||
m_message(message),
|
||||
m_code(code),
|
||||
m_fullMessage() {
|
||||
makeFullMessage();
|
||||
}
|
||||
|
||||
CException::~CException(void) noexcept {
|
||||
}
|
||||
|
||||
const char* CException::what() const noexcept {
|
||||
return m_fullMessage.c_str();
|
||||
}
|
||||
|
||||
unsigned int CException::getCode() const noexcept {
|
||||
return m_code;
|
||||
}
|
||||
|
||||
std::string CException::getMessage() const noexcept {
|
||||
return m_message;
|
||||
}
|
||||
|
||||
void CException::setMessage(std::string newMessage) noexcept {
|
||||
m_message = newMessage;
|
||||
}
|
||||
77
src/Engine/Core/Exception/CException.hpp
Normal file
77
src/Engine/Core/Exception/CException.hpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#ifndef CEXCEPTION_HPP
|
||||
#define CEXCEPTION_HPP
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <boost/stacktrace.hpp>
|
||||
|
||||
/**
|
||||
* @file CException.hpp
|
||||
* @brief File of CException, a class to handle program exception.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Class to handle program exception.
|
||||
*/
|
||||
class CException : public std::exception {
|
||||
|
||||
private:
|
||||
// The message of the exception.
|
||||
std::string m_message;
|
||||
|
||||
// The code of the exception.
|
||||
unsigned int m_code;
|
||||
|
||||
// The full message of the exception.
|
||||
std::string m_fullMessage;
|
||||
|
||||
/**
|
||||
* @brief Create the full message.
|
||||
*/
|
||||
void makeFullMessage(void);
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief The default constructor, build a exception with default message : "An error occurred" and the code 0.
|
||||
*/
|
||||
CException(void) noexcept;
|
||||
|
||||
/**
|
||||
* @brief The usage constructor, build a exception with a given message and the given code.
|
||||
* @param[in] message The wanted message.
|
||||
* @param[in] code The wanted code.
|
||||
*/
|
||||
CException(const std::string message, const unsigned int code) noexcept;
|
||||
|
||||
/**
|
||||
* @brief The destructor.
|
||||
*/
|
||||
~CException(void) noexcept;
|
||||
|
||||
/**
|
||||
* @brief Give some point about the current exception.
|
||||
* @return char*, Ready to print string, error description.
|
||||
*/
|
||||
virtual const char* what(void) const noexcept;
|
||||
|
||||
/**
|
||||
* @brief The error code getter.
|
||||
* @return unsigned int, The code of the error.
|
||||
*/
|
||||
unsigned int getCode(void) const noexcept;
|
||||
|
||||
/**
|
||||
* @brief The error message getter.
|
||||
* @return std::string, The message of the error.
|
||||
*/
|
||||
std::string getMessage(void) const noexcept;
|
||||
|
||||
/**
|
||||
* @brief The error message setter.
|
||||
* @param[in] newMessage The new error message.
|
||||
*/
|
||||
void setMessage(const std::string newMessage) noexcept;
|
||||
};
|
||||
|
||||
#endif
|
||||
34
src/Engine/Core/Exception/CExceptionManager.cpp
Normal file
34
src/Engine/Core/Exception/CExceptionManager.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "CExceptionManager.hpp"
|
||||
|
||||
void HandleException(const CException& e, bool fatal) {
|
||||
|
||||
std::string windowMessage(e.getMessage());
|
||||
windowMessage += "\nException reported in \"";
|
||||
windowMessage += DEFAULT_REPORT_PATH;
|
||||
windowMessage += "\".";
|
||||
|
||||
CFileManager fm(DEFAULT_REPORT_PATH);
|
||||
|
||||
try {
|
||||
fm.write(e.what());
|
||||
}
|
||||
catch (const std::exception& nothandled) {
|
||||
(void)nothandled;
|
||||
}
|
||||
|
||||
if (!fatal) {
|
||||
// Just warning.
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING,
|
||||
"Error !",
|
||||
windowMessage.c_str(),
|
||||
NULL);
|
||||
} else {
|
||||
// Fatal error.
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
|
||||
"Fatal error !",
|
||||
windowMessage.c_str(),
|
||||
NULL);
|
||||
// It's fatal, so we quit.
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
27
src/Engine/Core/Exception/CExceptionManager.hpp
Normal file
27
src/Engine/Core/Exception/CExceptionManager.hpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef CEXCEPTIONMANAGER_HPP
|
||||
#define CEXCEPTIONMANAGER_HPP
|
||||
|
||||
#include "CException.hpp"
|
||||
#include "../File/CFileManager.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <SDL.h>
|
||||
|
||||
/**
|
||||
* @file CExceptionManager.hpp
|
||||
* @brief File contain some function to handle program exception.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @def DEFAULT_PATH default path for crash-report file.
|
||||
*/
|
||||
#define DEFAULT_REPORT_PATH "../exception-report.txt"
|
||||
|
||||
/**
|
||||
* @brief Simple function to handle program exception
|
||||
* @param[in] e The Exception instance to hanlde.
|
||||
* @param[in] fatal A flag to make this Exception as fatal, so the program will quit.
|
||||
*/
|
||||
void HandleException(const CException& e, bool fatal);
|
||||
|
||||
#endif
|
||||
9
src/Engine/Core/Exception/CFileException.cpp
Normal file
9
src/Engine/Core/Exception/CFileException.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "CFileException.hpp"
|
||||
|
||||
CFileException::CFileException(void) noexcept :
|
||||
CException("A file error occurred", FILE_EXC_CODE) {
|
||||
}
|
||||
|
||||
CFileException::CFileException(std::string message) noexcept :
|
||||
CException(message, FILE_EXC_CODE){
|
||||
}
|
||||
40
src/Engine/Core/Exception/CFileException.hpp
Normal file
40
src/Engine/Core/Exception/CFileException.hpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef CFILEEXCEPTION_HPP
|
||||
#define CFILEEXCEPTION_HPP
|
||||
|
||||
#include "CException.hpp"
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* @file CFileException.hpp
|
||||
* @brief File of CFileException, a class to handle program file exception.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @def FILE_EXC_CODE default code for this kind of Exception.
|
||||
*/
|
||||
#define FILE_EXC_CODE 4
|
||||
|
||||
/**
|
||||
* @brief Class to handle program file exception.
|
||||
*/
|
||||
class CFileException : public CException {
|
||||
|
||||
private:
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief The default constructor, build a exception with default message : "A file error occurred" and the code FILE_EXC_CODE.
|
||||
*/
|
||||
CFileException(void) noexcept;
|
||||
|
||||
/**
|
||||
* @brief The usage constructor, build a exception with a given message and the code FILE_EXC_CODE.
|
||||
* @param[in] message The wanted message.
|
||||
*/
|
||||
CFileException(const std::string message) noexcept;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
9
src/Engine/Core/Exception/CJsonException.cpp
Normal file
9
src/Engine/Core/Exception/CJsonException.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "CJsonException.hpp"
|
||||
|
||||
CJsonException::CJsonException(void) noexcept :
|
||||
CException("A json error occurred", JSON_EXC_CODE) {
|
||||
}
|
||||
|
||||
CJsonException::CJsonException(std::string message) noexcept :
|
||||
CException(message, JSON_EXC_CODE){
|
||||
}
|
||||
40
src/Engine/Core/Exception/CJsonException.hpp
Normal file
40
src/Engine/Core/Exception/CJsonException.hpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef CJSONEXCEPTION_HPP
|
||||
#define CJSONEXCEPTION_HPP
|
||||
|
||||
#include "CException.hpp"
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* @file CJsonException.hpp
|
||||
* @brief File of CJsonException, a class to handle program json parse/handle exception.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @def JSON_EXC_CODE default code for this kind of Exception.
|
||||
*/
|
||||
#define JSON_EXC_CODE 5
|
||||
|
||||
/**
|
||||
* @brief Class to handle program file exception.
|
||||
*/
|
||||
class CJsonException : public CException {
|
||||
|
||||
private:
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief The default constructor, build a exception with default message : "A json error occurred" and the code JSON_EXC_CODE.
|
||||
*/
|
||||
CJsonException(void) noexcept;
|
||||
|
||||
/**
|
||||
* @brief The usage constructor, build a exception with a given message and the code JSON_EXC_CODE.
|
||||
* @param[in] message The wanted message.
|
||||
*/
|
||||
CJsonException(const std::string message) noexcept;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
9
src/Engine/Core/Exception/CLibException.cpp
Normal file
9
src/Engine/Core/Exception/CLibException.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "CLibException.hpp"
|
||||
|
||||
CLibException::CLibException(void) noexcept :
|
||||
CException("A lib error occurred", LIB_EXC_CODE) {
|
||||
}
|
||||
|
||||
CLibException::CLibException(std::string message) noexcept :
|
||||
CException(message, LIB_EXC_CODE){
|
||||
}
|
||||
40
src/Engine/Core/Exception/CLibException.hpp
Normal file
40
src/Engine/Core/Exception/CLibException.hpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef CLIBEXCEPTION_HPP
|
||||
#define CLIBEXCEPTION_HPP
|
||||
|
||||
#include "CException.hpp"
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* @file CLibException.hpp
|
||||
* @brief File of CLibException, a class to handle program library runtine exception.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @def LIB_EXC_CODE default code for this kind of Exception.
|
||||
*/
|
||||
#define LIB_EXC_CODE 3
|
||||
|
||||
/**
|
||||
* @brief Class to handle program file exception.
|
||||
*/
|
||||
class CLibException : public CException {
|
||||
|
||||
private:
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief The default constructor, build a exception with default message : "A library error occurred" and the code LIB_EXC_CODE.
|
||||
*/
|
||||
CLibException(void) noexcept;
|
||||
|
||||
/**
|
||||
* @brief The usage constructor, build a exception with a given message and the code LIB_EXC_CODE.
|
||||
* @param[in] message The wanted message.
|
||||
*/
|
||||
CLibException(const std::string message) noexcept;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
9
src/Engine/Core/Exception/CLogicException.cpp
Normal file
9
src/Engine/Core/Exception/CLogicException.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "CLogicException.hpp"
|
||||
|
||||
CLogicException::CLogicException(void) noexcept :
|
||||
CException("A logic error occurred", LOGIC_EXC_CODE) {
|
||||
}
|
||||
|
||||
CLogicException::CLogicException(std::string message) noexcept :
|
||||
CException(message, LOGIC_EXC_CODE){
|
||||
}
|
||||
40
src/Engine/Core/Exception/CLogicException.hpp
Normal file
40
src/Engine/Core/Exception/CLogicException.hpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef CLOGICEXCEPTION_HPP
|
||||
#define CLOGICEXCEPTION_HPP
|
||||
|
||||
#include "CException.hpp"
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* @file CLogicException.hpp
|
||||
* @brief File of CLogicException, a class to handle program logic exception, it's mean error caused by bad use of function/class by user.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @def LOGIC_EXC_CODE default code for this kind of Exception.
|
||||
*/
|
||||
#define LOGIC_EXC_CODE 1
|
||||
|
||||
/**
|
||||
* @brief Class to handle program file exception.
|
||||
*/
|
||||
class CLogicException : public CException {
|
||||
|
||||
private:
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief The default constructor, build a exception with default message : "A logic error occurred" and the code LOGIC_EXC_CODE.
|
||||
*/
|
||||
CLogicException(void) noexcept;
|
||||
|
||||
/**
|
||||
* @brief The usage constructor, build a exception with a given message and the code LOGIC_EXC_CODE.
|
||||
* @param[in] message The wanted message.
|
||||
*/
|
||||
CLogicException(const std::string message) noexcept;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
9
src/Engine/Core/Exception/CRuntimeException.cpp
Normal file
9
src/Engine/Core/Exception/CRuntimeException.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "CRuntimeException.hpp"
|
||||
|
||||
CRuntimeException::CRuntimeException(void) noexcept :
|
||||
CException("A runtime error occurred", RUNTIME_EXC_CODE) {
|
||||
}
|
||||
|
||||
CRuntimeException::CRuntimeException(std::string message) noexcept :
|
||||
CException(message, RUNTIME_EXC_CODE){
|
||||
}
|
||||
40
src/Engine/Core/Exception/CRuntimeException.hpp
Normal file
40
src/Engine/Core/Exception/CRuntimeException.hpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef CRUNTIMEEXCEPTION_HPP
|
||||
#define CRUNTIMEEXCEPTION_HPP
|
||||
|
||||
#include "CException.hpp"
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* @file CRuntimeException.hpp
|
||||
* @brief File of CRuntimeException, a class to handle program runtime exception.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @def LOGIC_EXC_CODE default code for this kind of Exception.
|
||||
*/
|
||||
#define RUNTIME_EXC_CODE 2
|
||||
|
||||
/**
|
||||
* @brief Class to handle program file exception.
|
||||
*/
|
||||
class CRuntimeException : public CException {
|
||||
|
||||
private:
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief The default constructor, build a exception with default message : "A runtime error occurred" and the code RUNTIME_EXC_CODE.
|
||||
*/
|
||||
CRuntimeException(void) noexcept;
|
||||
|
||||
/**
|
||||
* @brief The usage constructor, build a exception with a given message and the code RUNTIME_EXC_CODE.
|
||||
* @param[in] message The wanted message.
|
||||
*/
|
||||
CRuntimeException(const std::string message) noexcept;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
87
src/Engine/Core/Graphics/Shader/CShadersManager.cpp
Normal file
87
src/Engine/Core/Graphics/Shader/CShadersManager.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
#include "CShadersManager.hpp"
|
||||
|
||||
#include "CShaderFactory.hpp"
|
||||
#include "../CKernel.hpp"
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
CShadersManager::CShadersManager(void) :m_shadersMap(), m_shaderList(nullptr) {
|
||||
|
||||
}
|
||||
|
||||
CShadersManager::~CShadersManager(void) {
|
||||
for (std::pair<const std::string, CShader*>& it : m_shadersMap)
|
||||
{
|
||||
delete it.second;
|
||||
}
|
||||
free(m_shaderList);
|
||||
}
|
||||
|
||||
CShader* CShadersManager::get(std::string name) const {
|
||||
std::map<std::string, CShader*>::const_iterator it = m_shadersMap.find(name);
|
||||
if (it == m_shadersMap.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
else {
|
||||
return it.operator*().second;
|
||||
}
|
||||
}
|
||||
|
||||
void CShadersManager::create(void) {
|
||||
if (std::filesystem::is_directory(CEngineConfiguration::configuration->getGlobalShadersPath())) {
|
||||
for (std::filesystem::directory_iterator itr(CEngineConfiguration::configuration->getGlobalShadersPath()); itr != std::filesystem::directory_iterator(); ++itr) {
|
||||
if (std::filesystem::is_regular_file(itr->status())) {
|
||||
DEBUG_LOG(trace, "Kernel", "ShadersManager", "Shader : " + itr->path().string() + " found.")
|
||||
|
||||
CFileManager jsonfm(itr->path().string());
|
||||
std::string json = jsonfm.read();
|
||||
try {
|
||||
nlohmann::json shaderJson = nlohmann::json::parse(json);
|
||||
std::string name = shaderJson["name"];
|
||||
std::string vert = shaderJson["vert"];
|
||||
std::string frag = shaderJson["frag"];
|
||||
|
||||
CShader* newShader = CShaderFactory::createShader(CKernel::m_kernel ,name, CEngineConfiguration::configuration->getGlobalShadersPath().string() + std::string("/") + vert, CEngineConfiguration::configuration->getGlobalShadersPath().string() + std::string("/") + frag);
|
||||
|
||||
m_shadersMap.insert(std::pair<std::string, CShader*>(name, newShader));
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
throw CJsonException(std::string("Failed to parse and retrieve data from the json file ") + itr->path().string() + ": " + e.what());
|
||||
}
|
||||
|
||||
DEBUG_LOG(trace, "Kernel", "ShadersManager", "Shader : " + itr->path().string() + " successfully retrieved.")
|
||||
}
|
||||
}
|
||||
|
||||
m_shaderList = (CShader**)malloc(sizeof(CShader*) * m_shadersMap.size());
|
||||
|
||||
if (m_shaderList == NULL) {
|
||||
throw CRuntimeException("unable to alloc shaders list");
|
||||
}
|
||||
|
||||
unsigned int i = 0;
|
||||
|
||||
for (std::pair<std::string, CShader*> it : m_shadersMap)
|
||||
{
|
||||
m_shaderList[i] = it.second;
|
||||
i++;
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
throw CFileException(std::string("Failed to find the shaders folder : ") + CKernel::m_kernel->getEngineConfig()->getGlobalShadersPath().string());
|
||||
}
|
||||
}
|
||||
|
||||
void CShadersManager::compile(void) {
|
||||
DEBUG_LOG(trace, "Kernel", "ShadersManager", "Starting compiling shaders files.");
|
||||
|
||||
unsigned int nbShader = m_shadersMap.size();
|
||||
|
||||
for (unsigned int i = 0; i < nbShader; i++)
|
||||
{
|
||||
m_shaderList[i]->init();
|
||||
}
|
||||
|
||||
DEBUG_LOG(trace, "Kernel", "ShadersManager", "All shaders are ready to use.")
|
||||
}
|
||||
68
src/Engine/Core/Graphics/Shader/CShadersManager.hpp
Normal file
68
src/Engine/Core/Graphics/Shader/CShadersManager.hpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#ifndef CSHADERSMANAGER_HPP
|
||||
#define CSHADERSMANAGER_HPP
|
||||
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <map>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "../../Model/Graphic/Shader/CShader.hpp"
|
||||
#include "../../Context/CContext.hpp"
|
||||
|
||||
#include "../Exception/CJsonException.hpp"
|
||||
#include "../Exception/CFileException.hpp"
|
||||
#include "../Exception/CRuntimeException.hpp"
|
||||
|
||||
/*#ifdef WIN32
|
||||
#define SHADERS_FOLDER "../assets/shaders"
|
||||
#else
|
||||
#define SHADERS_FOLDER "../../assets/shaders"
|
||||
#endif*/
|
||||
|
||||
/**
|
||||
* @brief Class to manage shaders.
|
||||
*/
|
||||
class CShadersManager {
|
||||
private:
|
||||
/**
|
||||
* @brief map to store in a proper way the shaders.
|
||||
*/
|
||||
std::map<std::string, CShader*> m_shadersMap;
|
||||
|
||||
CShader** m_shaderList;
|
||||
public:
|
||||
/**
|
||||
* @brief constructor.
|
||||
*/
|
||||
CShadersManager(void);
|
||||
|
||||
CShadersManager(const CShadersManager& param) = delete;
|
||||
|
||||
/**
|
||||
* @brief destructor, destroy the shaders of the list.
|
||||
*/
|
||||
~CShadersManager(void);
|
||||
|
||||
CShadersManager& operator=(const CShadersManager& param) = delete;
|
||||
|
||||
/**
|
||||
* @brief get a shader by its name.
|
||||
* @param[in] name shader name.
|
||||
* @return the shader.
|
||||
*/
|
||||
CShader* get(std::string name) const;
|
||||
|
||||
/**
|
||||
* @brief compile and link all the shaders to make them ready to use.
|
||||
*/
|
||||
void compile(void);
|
||||
|
||||
/**
|
||||
* @brief search the folder assets/shaders and create the shaders specifieds by a json file.
|
||||
* @post throw an FileException if the shaders folder is not found.
|
||||
*/
|
||||
void create(void);
|
||||
};
|
||||
|
||||
#endif
|
||||
165
src/Engine/Core/Input/CInput.cpp
Normal file
165
src/Engine/Core/Input/CInput.cpp
Normal file
@@ -0,0 +1,165 @@
|
||||
#include "CInput.hpp"
|
||||
|
||||
#include "../CKernel.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
CInput::CInput(CKernel* kernel) :
|
||||
m_kernel(kernel),
|
||||
m_event(),
|
||||
m_mouse(),
|
||||
m_keyboard(),
|
||||
m_controllers(){
|
||||
m_keyboard.addObserver(this);
|
||||
}
|
||||
|
||||
CInput::~CInput(void) {
|
||||
for (std::map<unsigned int, CController*>::iterator it = m_controllers.begin(); it != m_controllers.end(); ++it) {
|
||||
delete it->second;
|
||||
}
|
||||
}
|
||||
|
||||
void CInput::updateEvent(void) {
|
||||
// Reset the mouse relative pos.
|
||||
m_mouse.reset();
|
||||
|
||||
// For each event in the SDL event queue.
|
||||
while(SDL_PollEvent(&m_event) == 1) {
|
||||
|
||||
// Switch on the event type.
|
||||
switch(m_event.type) {
|
||||
unsigned int id;
|
||||
CController* controller;
|
||||
|
||||
// On key button pressed.
|
||||
case SDL_KEYDOWN:
|
||||
m_keyboard.onKeyDown(m_event.key.keysym.scancode);
|
||||
break;
|
||||
|
||||
// On key button released.
|
||||
case SDL_KEYUP:
|
||||
m_keyboard.onKeyUp(m_event.key.keysym.scancode);
|
||||
break;
|
||||
|
||||
// On mouse button pressed.
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
m_mouse.onButtonDown(m_event.button.button);
|
||||
break;
|
||||
|
||||
// On mouse button released.
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
m_mouse.onButtonUp(m_event.button.button);
|
||||
break;
|
||||
|
||||
// On mouse cursor moved.
|
||||
case SDL_MOUSEMOTION:
|
||||
m_mouse.onMove(m_event.motion.x, m_event.motion.y, m_event.motion.xrel, m_event.motion.yrel);
|
||||
break;
|
||||
|
||||
// On mouse wheel moved.
|
||||
case SDL_MOUSEWHEEL:
|
||||
m_mouse.onWheel(m_event.wheel.x, m_event.wheel.y);
|
||||
break;
|
||||
|
||||
// On joystick unplugged.
|
||||
case SDL_JOYDEVICEREMOVED:
|
||||
id = m_event.jdevice.which;
|
||||
controller = m_controllers[id];
|
||||
delete controller;
|
||||
m_controllers.erase(id);
|
||||
break;
|
||||
|
||||
// On joystick plugged.
|
||||
case SDL_JOYDEVICEADDED:
|
||||
try {
|
||||
// Give a test id for opening.
|
||||
id = m_event.jdevice.which;
|
||||
CController* controller = new CController(id);
|
||||
// Get the final id.
|
||||
id = controller->getId();
|
||||
// Push the controller in the list.
|
||||
m_controllers.insert(std::pair<unsigned int, CController*>(id, controller));
|
||||
}
|
||||
catch (const CException& e) {
|
||||
HandleException(e, false);
|
||||
}
|
||||
break;
|
||||
|
||||
// On joystick axis moved.
|
||||
case SDL_JOYAXISMOTION:
|
||||
controller = m_controllers[m_event.jaxis.which];
|
||||
controller->onAxis(m_event.jaxis.axis, m_event.jaxis.value);
|
||||
break;
|
||||
|
||||
// On joystick hat moved.
|
||||
case SDL_JOYHATMOTION:
|
||||
controller = m_controllers[m_event.jhat.which];
|
||||
controller->onHat(m_event.jhat.hat, m_event.jhat.value);
|
||||
break;
|
||||
|
||||
// On joystick button pressed.
|
||||
case SDL_JOYBUTTONDOWN:
|
||||
controller = m_controllers[m_event.jaxis.which];
|
||||
controller->onButtonDown(m_event.jbutton.button);
|
||||
break;
|
||||
|
||||
// On joystick button released.
|
||||
case SDL_JOYBUTTONUP:
|
||||
controller = m_controllers[m_event.jaxis.which];
|
||||
controller->onButtonUp(m_event.jbutton.button);
|
||||
break;
|
||||
|
||||
// Case of window event.
|
||||
case SDL_WINDOWEVENT:
|
||||
|
||||
// If the window is closed.
|
||||
if (m_event.window.event == SDL_WINDOWEVENT_CLOSE) {
|
||||
m_kernel->quit();
|
||||
}
|
||||
|
||||
// If the window is focused.
|
||||
if (m_event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) {
|
||||
m_kernel->focus();
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CKeyboard* CInput::getKeyboard(void) {
|
||||
return &m_keyboard;
|
||||
}
|
||||
|
||||
CMouse* CInput::getMouse(void) {
|
||||
return &m_mouse;
|
||||
}
|
||||
|
||||
CController* CInput::getController(unsigned int id) {
|
||||
return m_controllers[id];
|
||||
}
|
||||
|
||||
std::map<unsigned int, CController*> CInput::getControllers(void) {
|
||||
return m_controllers;
|
||||
}
|
||||
|
||||
void CInput::onKKeyDown(unsigned int which) {
|
||||
// 40 is enter.
|
||||
if (which == 40) {
|
||||
// If 226 is pressed.
|
||||
if (m_keyboard.getKey(226)) {
|
||||
m_kernel->fullscreen();
|
||||
}
|
||||
}
|
||||
|
||||
// 227 is command/windows/meta.
|
||||
if (which == 227) {
|
||||
m_kernel->desktop();
|
||||
}
|
||||
}
|
||||
|
||||
void CInput::onKKeyUp(unsigned int which) {
|
||||
(void)which;
|
||||
}
|
||||
100
src/Engine/Core/Input/CInput.hpp
Normal file
100
src/Engine/Core/Input/CInput.hpp
Normal file
@@ -0,0 +1,100 @@
|
||||
#ifndef CINPUT_HPP
|
||||
#define CINPUT_HPP
|
||||
|
||||
#include "../../Model/Input/CMouse.hpp"
|
||||
#include "../../Model/Input/CKeyboard.hpp"
|
||||
#include "../../Model/Input/CController.hpp"
|
||||
#include "../Exception/CException.hpp"
|
||||
#include "../../Model/Observer/CKeyObserver.hpp"
|
||||
#include "../Exception/CExceptionManager.hpp"
|
||||
#include "../CKernel.fwd.hpp"
|
||||
|
||||
#include <SDL.h>
|
||||
#include <utility>
|
||||
#include <map>
|
||||
|
||||
/**
|
||||
* @file CInput.hpp
|
||||
* @brief file of the CInput class, class to handle all input event.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Class to handle all input event.
|
||||
*/
|
||||
class CInput : public CKeyObserver {
|
||||
private:
|
||||
// A pointer to the game.
|
||||
CKernel* m_kernel;
|
||||
|
||||
// The event handler.
|
||||
SDL_Event m_event;
|
||||
|
||||
// The mouse model.
|
||||
CMouse m_mouse;
|
||||
|
||||
// The keyboard model.
|
||||
CKeyboard m_keyboard;
|
||||
|
||||
// The controllers model, sorted by id.
|
||||
std::map<unsigned int, CController*> m_controllers;
|
||||
|
||||
public:
|
||||
CInput(void) = delete;
|
||||
CInput(const CInput& param) = delete;
|
||||
CInput& operator=(const CInput& param) = delete;
|
||||
|
||||
/**
|
||||
* @brief The usage constructor.
|
||||
* @param[in] game A pointer to the game object, to handle specific event, like quit.
|
||||
*/
|
||||
CInput(CKernel* game);
|
||||
|
||||
/**
|
||||
* @brief The destructor.
|
||||
*/
|
||||
~CInput(void);
|
||||
|
||||
/**
|
||||
* @brief Function to handle all event of the game, SDL and CGame must be initialized. This function is call once per game frame.
|
||||
*/
|
||||
void updateEvent(void);
|
||||
|
||||
/**
|
||||
* @brief The keyboard getter.
|
||||
* @return CKeyboard*, The keyboard.
|
||||
*/
|
||||
CKeyboard* getKeyboard(void);
|
||||
|
||||
/**
|
||||
* @brief The mouse getter.
|
||||
* @return CMouse*, The mouse.
|
||||
*/
|
||||
CMouse* getMouse(void);
|
||||
|
||||
/**
|
||||
* @brief The controller getter.
|
||||
* @param[in] id The id of the wanted controller.
|
||||
* @return CController*, The controller.
|
||||
*/
|
||||
CController* getController(unsigned int id);
|
||||
|
||||
/**
|
||||
* @brief The controllers getter.
|
||||
* @return std::map<unsigned int, CController*>, All the controllers, ordered by id.
|
||||
*/
|
||||
std::map<unsigned int, CController*> getControllers(void);
|
||||
|
||||
/**
|
||||
* @brief Handle key down event.
|
||||
* @param[in] which The code of the key.
|
||||
*/
|
||||
virtual void onKKeyDown(unsigned int which);
|
||||
|
||||
/**
|
||||
* @brief Handle key down event.
|
||||
* @param[in] which The code of the key.
|
||||
*/
|
||||
virtual void onKKeyUp(unsigned int which);
|
||||
};
|
||||
|
||||
#endif
|
||||
258
src/Engine/Core/Kernel/CKernel.cpp
Normal file
258
src/Engine/Core/Kernel/CKernel.cpp
Normal file
@@ -0,0 +1,258 @@
|
||||
#include "CKernel.hpp"
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "../../Configurations/Configuration/CConfiguration.hpp"
|
||||
#include "../../Modules/Module/CModuleLoader.hpp"
|
||||
#include "../Component/Graphics/CTestRenderer.hpp"
|
||||
|
||||
//CKernel* CKernel::m_kernel;
|
||||
//CShader* CKernel::m_mainShader;
|
||||
|
||||
// #define NAME "Cosmic Engine"
|
||||
// #define LOGO "../assets/spaceEngineIcon.png"
|
||||
// #define SPLASH "../assets/shrekt.png"
|
||||
// #define SPLASH_HEIGHT 512
|
||||
// #define SPLASH_WIDTH 512
|
||||
// #define GAME_HEIGHT 360
|
||||
// #define GAME_WIDTH 640
|
||||
|
||||
namespace CosmicCore {
|
||||
|
||||
CKernel::CKernel(std::string name, std::string gameName) :
|
||||
m_gameName(gameName)
|
||||
{
|
||||
//m_kernel = this;
|
||||
//CContext::init(SDL_INIT_EVENTS | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC,IMG_INIT_PNG | IMG_INIT_JPG);
|
||||
m_activeScene = std::make_unique<CScene>("Scene1");
|
||||
}
|
||||
|
||||
CKernel::~CKernel()
|
||||
{
|
||||
/*for(std::pair<const std::string, CScene*>& scenePair : m_sceneList)
|
||||
{
|
||||
delete scenePair.second;
|
||||
}*/
|
||||
}
|
||||
|
||||
/*void CKernel::setContext(SDL_GLContext context)
|
||||
{
|
||||
m_GLcontext = context;
|
||||
}*/
|
||||
|
||||
/*CScene* CKernel::getActiveScene()
|
||||
{
|
||||
return m_activeScene;
|
||||
}*/
|
||||
|
||||
/*void CKernel::initGL()
|
||||
{
|
||||
m_window.setCursorGrabbed(false);
|
||||
m_window.setAntiAliasing(m_config.getMsaa());
|
||||
m_window.initialization();
|
||||
m_GLcontext = m_window.getOpenGLContext();
|
||||
SDL_GL_MakeCurrent(m_window.getWindow(), m_GLcontext);
|
||||
m_window.setVisible(true);
|
||||
SDL_GL_SetSwapInterval(1);
|
||||
}*/
|
||||
|
||||
void CKernel::start(bool isPreview) {
|
||||
|
||||
//DEBUG_LOG(trace, "Kernel", "Context", "Starting " + m_gameName + "...")
|
||||
CConfiguration::init();
|
||||
//CComponentFactory::registerStandardProvidedAbstractComponents();
|
||||
std::cout << CModuleLoader::loadAll() << std::endl;
|
||||
if(!isPreview)
|
||||
{
|
||||
// Open the splash window.
|
||||
//DEBUG_LOG(trace, "Kernel", "Context", "Loading splash window...")
|
||||
//m_loadingWindow.setVisible(true);
|
||||
//m_loadingWindow.initialization();
|
||||
|
||||
// Load the config file.
|
||||
//m_engineConfig.load();
|
||||
//m_config.load();
|
||||
|
||||
// Load the main window.
|
||||
/*DEBUG_LOG(trace, "Kernel", "Context", "Loading main window...")
|
||||
m_window.setAntiAliasing(m_config.getMsaa());
|
||||
m_window.initialization();
|
||||
m_window.setCursorGrabbed(m_config.getFullscreen());
|
||||
m_GLcontext = m_window.getOpenGLContext();
|
||||
SDL_GL_SetSwapInterval(1);
|
||||
m_window.setFullscreen(m_config.getFullscreen());
|
||||
if (m_config.getFullscreen()) {
|
||||
m_window.setSize(m_config.getFullScreenSize());
|
||||
glViewport(0, 0, m_config.getFullScreenSize().first, m_config.getFullScreenSize().second);
|
||||
}
|
||||
else {
|
||||
m_window.setSize(m_config.getWindowSize());
|
||||
glViewport(0, 0, m_config.getWindowSize().first, m_config.getWindowSize().second);
|
||||
}*/
|
||||
}
|
||||
|
||||
// On initialise GLEW.
|
||||
/*DEBUG_LOG(trace, "Kernel", "Context", "GLEW initialization...")
|
||||
GLenum initialisationGLEW(glewInit());
|
||||
|
||||
if (initialisationGLEW != GLEW_OK) {
|
||||
// On affiche l'erreur grace a la fonction : glewGetErrorString(GLenum code)
|
||||
HandleException(CLibException(std::string("Erreur d'initialisation de GLEW : ") + (char*)glewGetErrorString(initialisationGLEW)), true);
|
||||
}
|
||||
DEBUG_LOG(trace, "Kernel", "Context", "GLEW initialized!")
|
||||
|
||||
try {
|
||||
m_shaderManager.create();
|
||||
m_shaderManager.compile();
|
||||
}
|
||||
catch (const CException& exception) {
|
||||
HandleException(exception, true);
|
||||
}
|
||||
|
||||
m_mainShader = m_shaderManager.get("basicColor");
|
||||
|
||||
//double ratio;
|
||||
//if (isFullscreen()) {
|
||||
// ratio = ((double)m_fullscreenSize.first) / ((double)m_fullscreenSize.second);
|
||||
//}
|
||||
//else {
|
||||
// ratio = ((double)m_windowSize.first) / ((double)m_windowSize.second);
|
||||
//}
|
||||
// m_projection = glm::perspective(m_fov, ratio, 1.0, 100.0);
|
||||
// m_modelView = glm::mat4(1.0);
|
||||
|
||||
if(!isPreview)
|
||||
{
|
||||
m_loadingWindow.setVisible(false);
|
||||
m_window.setVisible(true);
|
||||
}
|
||||
|
||||
DEBUG_LOG(trace, "Kernel", "Context", m_gameName + " Started !")*/
|
||||
}
|
||||
|
||||
void CKernel::loop() {
|
||||
while(1)
|
||||
{
|
||||
int addOrNot = 0;
|
||||
std::cout << "Create test renderer to add to the scene via an entity ?" << std::endl;
|
||||
std::cin >> addOrNot;
|
||||
if(addOrNot == 1)
|
||||
{
|
||||
auto entity = m_activeScene->addEntity("test1", "testDesc");
|
||||
entity.addComponent<CTestRenderer>();
|
||||
std::cout << "nb Entities : " << m_activeScene->getNumEntity() << std::endl;
|
||||
}
|
||||
else if(addOrNot == 2)
|
||||
{
|
||||
//m_activeScene.get()->getEntityComponentManager().getEntities().clear();
|
||||
//m_activeScene.get()->getEntityComponentManager().addComponent<CTestRenderer>(m_activeScene.get()->getEntityComponentManager().getEntities().size()-1, EComponentType::COMPONENT_RENDERER);
|
||||
}
|
||||
m_activeScene->render();
|
||||
}
|
||||
/*unsigned int frameRate(1000 / m_config.getTargetedFramerate());
|
||||
unsigned int beginIterationTime(0);
|
||||
unsigned int endIterationTime(0);
|
||||
unsigned int spendedIterationTime(0);
|
||||
unsigned int lastFrame = 0;
|
||||
|
||||
// Activation du Depth Buffer
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
//lancement des scripts
|
||||
unsigned int numEntities = m_activeScene->getNumEntity();
|
||||
for (unsigned int i = 0; i < numEntities; i++)
|
||||
{
|
||||
unsigned int numComponents = m_activeScene->getEntity(i)->getComponents(EComponentType::COMPONENT_SCRIPT).size();
|
||||
for (unsigned int j = 0; j < numComponents; j++)
|
||||
{
|
||||
((CAbstractScript*)(m_activeScene->getEntity(i)->getComponents(EComponentType::COMPONENT_SCRIPT)[j]))->start();
|
||||
}
|
||||
|
||||
}
|
||||
while (!m_finished) {
|
||||
// Define the starting time.
|
||||
beginIterationTime = SDL_GetTicks();
|
||||
|
||||
// Compute the delta time.
|
||||
unsigned int currentFrame = beginIterationTime;
|
||||
m_deltaTime = currentFrame - lastFrame;
|
||||
lastFrame = currentFrame;
|
||||
|
||||
// Update event system.
|
||||
m_inputHandler.updateEvent();
|
||||
// ####### start render #######
|
||||
|
||||
// Nettoyage de l'<27>cran.
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
// Update des scripts.
|
||||
m_activeScene->updateScript();
|
||||
|
||||
// Actualisation du monde physique.
|
||||
//todo fonction wrap => pour rendre valide même si c'est pas ODE
|
||||
dSpaceCollide(m_activeScene->getTangibleWorld()->getSpace(), m_activeScene->getTangibleWorld(), &(m_activeScene->getTangibleWorld())->callback);
|
||||
m_activeScene->getTangibleWorld()->updateWorld(0.12f);
|
||||
|
||||
|
||||
// Rendu.
|
||||
m_activeScene->render();
|
||||
|
||||
// Actualisation de la fen<65>tre.
|
||||
SDL_GL_SwapWindow(m_window.getWindow());
|
||||
|
||||
// ####### end render #######
|
||||
|
||||
// Compute the end and spended time .
|
||||
endIterationTime = SDL_GetTicks();
|
||||
spendedIterationTime = endIterationTime - beginIterationTime;
|
||||
|
||||
// If need, we pause the programm to have the right fps amount.
|
||||
if (spendedIterationTime < frameRate) {
|
||||
SDL_Delay(frameRate - spendedIterationTime);
|
||||
}
|
||||
}
|
||||
if(m_isOriginOfContext)
|
||||
CContext::quit();*/
|
||||
}
|
||||
|
||||
void CKernel::quit() {
|
||||
m_finished = true;
|
||||
}
|
||||
|
||||
|
||||
void CKernel::fullscreen() {
|
||||
/*if (!m_config.getFullscreen()) {
|
||||
m_window.setSize(m_config.getFullScreenSize());
|
||||
m_window.setCursorGrabbed(true);
|
||||
m_window.setFullscreen(true);
|
||||
m_config.setFullscreen(true);
|
||||
glViewport(0, 0, m_config.getFullScreenSize().first, m_config.getFullScreenSize().second);
|
||||
}
|
||||
else {
|
||||
m_window.setSize(m_config.getWindowSize());
|
||||
m_window.setCursorGrabbed(true);
|
||||
m_window.setFullscreen(false);
|
||||
m_config.setFullscreen(false);
|
||||
glViewport(0, 0, m_config.getWindowSize().first, m_config.getWindowSize().second);
|
||||
}
|
||||
// Sauvegarde la nouvelle conf.
|
||||
m_config.save();*/
|
||||
}
|
||||
|
||||
void CKernel::desktop() {
|
||||
/*if (m_config.getFullscreen()) {
|
||||
m_window.setSize(m_config.getWindowSize());
|
||||
m_window.setCursorGrabbed(false);
|
||||
m_window.setFullscreen(false);
|
||||
m_config.setFullscreen(false);
|
||||
glViewport(0, 0, m_config.getWindowSize().first, m_config.getWindowSize().second);
|
||||
}
|
||||
m_window.setCursorGrabbed(false);*/
|
||||
}
|
||||
|
||||
void CKernel::focus() {
|
||||
//m_window.setCursorGrabbed(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
155
src/Engine/Core/Kernel/CKernel.hpp
Normal file
155
src/Engine/Core/Kernel/CKernel.hpp
Normal file
@@ -0,0 +1,155 @@
|
||||
#ifndef CKERNEL_HPP
|
||||
#define CKERNEL_HPP
|
||||
|
||||
#include <memory>
|
||||
#define GLM_ENABLE_EXPERIMENTAL
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtx/transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
//#include "../Context/CContext.hpp"
|
||||
//#include "../Context/Window/CGameWindow.hpp"
|
||||
//#include "../Context/Window/CLoadingWindow.hpp"
|
||||
|
||||
//#include "../Controller/Configuration/CGameConfiguration.hpp"
|
||||
//#include "../Controller/Configuration/CEngineConfiguration.hpp"
|
||||
|
||||
//#include "../Controller/Exception/CExceptionManager.hpp"
|
||||
|
||||
//#include "Input/CInput.hpp"
|
||||
#include "../Scene/CScene.hpp"
|
||||
//#include "../Model/Node/Components/Graphic/CAbstractRenderer.hpp"
|
||||
//#include "../Model/Node/Components/CAbstractScript.hpp"
|
||||
//#include "../Model/Node/Components/Graphic/CCamera.hpp"
|
||||
//#include "../Model/Graphic/Shader/CShader.hpp"
|
||||
//#include "Shader/CShadersManager.hpp"
|
||||
//#include "../Utils/CSerializable.hpp"
|
||||
//#include <boost/atomic/atomic.hpp>
|
||||
|
||||
namespace CosmicCore {
|
||||
|
||||
/**
|
||||
* @file CKernel.hpp
|
||||
* @brief Central class of the game, handle window and I/O.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Central class of the game, handle window and I/O.
|
||||
*/
|
||||
class CKernel {
|
||||
private:
|
||||
std::string m_configPath;
|
||||
std::string m_gameName;
|
||||
// The config.
|
||||
//CGameConfiguration m_config;
|
||||
//CEngineConfiguration m_engineConfig;
|
||||
|
||||
// The windows.
|
||||
//CLoadingWindow m_loadingWindow;
|
||||
//CGameWindow m_window;
|
||||
|
||||
// Global game var.
|
||||
bool m_finished;
|
||||
unsigned int m_deltaTime;
|
||||
|
||||
// The Scenes.
|
||||
std::unique_ptr<CScene> m_activeScene;
|
||||
//std::map<std::string, CScene*> m_sceneList;
|
||||
|
||||
//CInput m_inputHandler;
|
||||
//SDL_GLContext m_GLcontext;
|
||||
//bool m_isOriginOfContext;
|
||||
|
||||
//CShadersManager m_shaderManager;
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief A pointer to the simulation which is accessible from anywhere.
|
||||
*/
|
||||
//static CKernel* m_kernel;
|
||||
|
||||
/**
|
||||
* @brief A pointer to the shader used, accessible from anywhere.
|
||||
*/
|
||||
//static CShader* m_mainShader;
|
||||
|
||||
CKernel() = delete;
|
||||
CKernel(std::string name, std::string gameName);
|
||||
|
||||
//CKernel(std::string name, std::string gameName, SDL_Window* window);
|
||||
~CKernel();
|
||||
|
||||
std::string getName() { return m_gameName; };
|
||||
//CGameConfiguration* getConfig() { return &m_config; };
|
||||
//CEngineConfiguration* getEngineConfig() { return &m_engineConfig; };
|
||||
|
||||
//void addScene(std::shared_ptr<CScene>& scene);
|
||||
//void setActiveScene(std::string scene);
|
||||
//void setContext(SDL_GLContext context);
|
||||
//CScene* getActiveScene();
|
||||
//unsigned int getNbScene() { return m_sceneList.size(); };
|
||||
//std::map<std::string, CScene*>& getScenes() { return m_sceneList; };
|
||||
|
||||
/*CKeyboard* getKeyboard(void) {
|
||||
return m_inputHandler.getKeyboard();
|
||||
}*/
|
||||
|
||||
/*CMouse* getMouse(void) {
|
||||
return m_inputHandler.getMouse();
|
||||
}*/
|
||||
|
||||
unsigned int getDeltatime(void) {
|
||||
return m_deltaTime;
|
||||
}
|
||||
|
||||
void setDeltatime(unsigned int delta){
|
||||
m_deltaTime = delta;
|
||||
}
|
||||
|
||||
/*void updateInput(){
|
||||
m_inputHandler.updateEvent();
|
||||
}*/
|
||||
/*CShader* getShader(std::string shader)
|
||||
{
|
||||
return m_shaderManager.get(shader);
|
||||
}*/
|
||||
//CGameWindow& getGameWindow(){return m_window;};
|
||||
//SDL_GLContext getContext(){return m_GLcontext;};
|
||||
//void initGL();
|
||||
void start(bool isPreview = false);
|
||||
void loop();
|
||||
void quit();
|
||||
void fullscreen();
|
||||
void desktop();
|
||||
void focus();
|
||||
|
||||
//nlohmann::json to_json();
|
||||
|
||||
/*static CKernel* from_json(SDL_Window* window, SDL_GLContext context, nlohmann::json& j)
|
||||
{
|
||||
CKernel* g = new CKernel(j["ProjectName"], j["ProjectName"], window);
|
||||
g->setContext(context);
|
||||
g->start(true);
|
||||
for (nlohmann::json& scene : j["Scenes"])
|
||||
{
|
||||
g->addScene(CScene::from_json(scene));
|
||||
}
|
||||
g->setActiveScene(j["ActiveScene"]);
|
||||
return g;
|
||||
};*/
|
||||
|
||||
/*static CKernel* from_json(nlohmann::json& j)
|
||||
{
|
||||
CKernel* g = new CKernel(j["ProjectName"], j["ProjectName"]);
|
||||
g->initGL();
|
||||
for (nlohmann::json& scene : j["Scenes"])
|
||||
{
|
||||
g->addScene(CScene::from_json(scene));
|
||||
}
|
||||
g->setActiveScene(j["ActiveScene"]);
|
||||
return g;
|
||||
};*/
|
||||
};
|
||||
|
||||
}
|
||||
#endif
|
||||
39
src/Engine/Core/Observer/CControllerObserver.hpp
Normal file
39
src/Engine/Core/Observer/CControllerObserver.hpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef CCONTROLLEROBSERVER_HPP
|
||||
#define CCONTROLLEROBSERVER_HPP
|
||||
|
||||
/**
|
||||
* @file CControllerObserver.hpp
|
||||
* @brief file of the CControllerObserver class, abstract class to extend, alloy the daugther class to handle controller event.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Abstract class to extend, alloy the daugther class to handle controller event.
|
||||
*/
|
||||
class CControllerObserver {
|
||||
private:
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Handle button down event.
|
||||
* @param[in] controllerId The controller id.
|
||||
* @param[in] which The code of the button.
|
||||
*/
|
||||
virtual void onCButtonDown(unsigned int controllerId, unsigned int which) = 0;
|
||||
|
||||
/**
|
||||
* @brief Handle button up event.
|
||||
* @param[in] controllerId The controller id.
|
||||
* @param[in] which The code of the button.
|
||||
*/
|
||||
virtual void onCButtonUp(unsigned int controllerId, unsigned int which) = 0;
|
||||
|
||||
/**
|
||||
* @brief Handle axis event.
|
||||
* @param[in] controllerId The controller id.
|
||||
* @param[in] which The id of the axis.
|
||||
* @param[in] value the new Value of the axis.
|
||||
*/
|
||||
virtual void onCAxis(unsigned int controllerId, unsigned int which, int value) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
29
src/Engine/Core/Observer/CKeyObserver.hpp
Normal file
29
src/Engine/Core/Observer/CKeyObserver.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef CKEYOBSERVER_HPP
|
||||
#define CKEYOBSERVER_HPP
|
||||
|
||||
/**
|
||||
* @file CKeyObserver.hpp
|
||||
* @brief file of the CKeyObserver class, abstract class to extend, alloy the daugther class to handle key event.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Abstract class to extend, alloy the daugther class to handle key event.
|
||||
*/
|
||||
class CKeyObserver {
|
||||
private:
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Handle key down event.
|
||||
* @param[in] which The code of the key.
|
||||
*/
|
||||
virtual void onKKeyDown(unsigned int which) = 0;
|
||||
|
||||
/**
|
||||
* @brief Handle key down event.
|
||||
* @param[in] which The code of the key.
|
||||
*/
|
||||
virtual void onKKeyUp(unsigned int which) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
45
src/Engine/Core/Observer/CMouseObserver.hpp
Normal file
45
src/Engine/Core/Observer/CMouseObserver.hpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#ifndef CMOUSEOBSERVER_HPP
|
||||
#define CMOUSEOBSERVER_HPP
|
||||
|
||||
/**
|
||||
* @file CMouseObserver.hpp
|
||||
* @brief file of the CMouseObserver class, abstract class to extend, alloy the daugther class to handle mouse event.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Abstract class to extend, alloy the daugther class to handle mouse event.
|
||||
*/
|
||||
class CMouseObserver {
|
||||
private:
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Handle key down event.
|
||||
* @param[in] which The code of the button.
|
||||
*/
|
||||
virtual void onMButtonDown(unsigned int which) = 0;
|
||||
|
||||
/**
|
||||
* @brief Handle key up event.
|
||||
* @param[in] which The code of the button.
|
||||
*/
|
||||
virtual void onMButtonUp(unsigned int which) = 0;
|
||||
|
||||
/**
|
||||
* @brief Handle wheel event.
|
||||
* @param[in] x The relative x value.
|
||||
* @param[in] y The relative y value.
|
||||
*/
|
||||
virtual void onMWheel(int x, int y) = 0;
|
||||
|
||||
/**
|
||||
* @brief Handle move event.
|
||||
* @param[in] x The new x value.
|
||||
* @param[in] y The new y value.
|
||||
* @param[in] xRel The relative x value.
|
||||
* @param[in] yRel The relative y value.
|
||||
*/
|
||||
virtual void onMMove(int x, int y, int xRel, int yRel) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
30
src/Engine/Core/Observer/CTransformObserver.hpp
Normal file
30
src/Engine/Core/Observer/CTransformObserver.hpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef CTRANSFORMOBSERVER_HPP
|
||||
#define CTRANSFORMOBSERVER_HPP
|
||||
|
||||
/**
|
||||
* @file CTransformObserver.hpp
|
||||
* @brief file of the CTransformObserver class, abstract class to extend, alloy the daugther class to handle transform change event.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Abstract class to extend, alloy the daugther class to handle transform change event.
|
||||
*/
|
||||
class CTransformObserver {
|
||||
public:
|
||||
/**
|
||||
* @brief Handle center change event.
|
||||
*/
|
||||
virtual void onCenterChange(void) = 0;
|
||||
|
||||
/**
|
||||
* @brief Handle scale change event.
|
||||
*/
|
||||
virtual void onScaleChange(void) = 0;
|
||||
|
||||
/**
|
||||
* @brief Handle rotation change event.
|
||||
*/
|
||||
virtual void onRotationChange(void) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
14
src/Engine/Core/Physics/CAbstractTangibleWorld.cpp
Normal file
14
src/Engine/Core/Physics/CAbstractTangibleWorld.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include "CAbstractTangibleWorld.hpp"
|
||||
|
||||
namespace CosmicCore {
|
||||
|
||||
CAbstractTangibleWorld::CAbstractTangibleWorld()
|
||||
{
|
||||
}
|
||||
|
||||
CAbstractTangibleWorld::~CAbstractTangibleWorld()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
74
src/Engine/Core/Physics/CAbstractTangibleWorld.hpp
Normal file
74
src/Engine/Core/Physics/CAbstractTangibleWorld.hpp
Normal file
@@ -0,0 +1,74 @@
|
||||
#ifndef CABSTRACTTANGIBLEWORLD_HPP
|
||||
#define CABSTRACTTANGIBLEWORLD_HPP
|
||||
|
||||
namespace CosmicCore {
|
||||
/**
|
||||
* @brief Class representing any physical world - abstract.
|
||||
*/
|
||||
class CAbstractTangibleWorld {
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* @brief The ODE world ID.
|
||||
*/
|
||||
//dWorldID m_world;
|
||||
|
||||
/**
|
||||
* @brief The ODE collision space.
|
||||
*/
|
||||
//dSpaceID m_space;
|
||||
|
||||
/**
|
||||
* @brief The ODE joint Group for the collisions.
|
||||
*/
|
||||
//dJointGroupID m_jointGroup;
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* @brief CTangibleWorld's default constructor. Does nothing.
|
||||
*/
|
||||
CAbstractTangibleWorld();
|
||||
|
||||
/**
|
||||
* @brief CTangibleWorld's destructor.
|
||||
*/
|
||||
~CAbstractTangibleWorld();
|
||||
|
||||
/**
|
||||
* @brief Update the physical world, move the bodies.
|
||||
* @param[in] seconds The time to spend in the current step of simulation.
|
||||
* If an object falls and seconds equals to 1, the object's position will correspond to 1 second of falling after the call of the function.
|
||||
*/
|
||||
virtual void updateWorld(float seconds) = 0;
|
||||
|
||||
/**
|
||||
* @brief Getter to the ODE world ID.
|
||||
* @return The ODE world ID, actually return m_world.
|
||||
*/
|
||||
//dWorldID getWorld();
|
||||
|
||||
/**
|
||||
* @brief Getter to the ODE space ID.
|
||||
* @return The ODE space ID, actually return m_space.
|
||||
*/
|
||||
//dSpaceID getSpace(void);
|
||||
|
||||
virtual void initWorld() = 0;
|
||||
|
||||
/**
|
||||
* @brief Getter to the ODE joint group.
|
||||
* @return The ODE joint group , actually return m_jointGroup.
|
||||
*/
|
||||
//dJointGroupID getJointGroup(void);
|
||||
|
||||
/**
|
||||
* @brief The collision callback of the ODE space.
|
||||
* Is called each time a collision occurs when the ODE world is updated.
|
||||
*/
|
||||
//static dNearCallback callback;
|
||||
|
||||
};
|
||||
}
|
||||
#endif
|
||||
17
src/Engine/Core/Resource/CAbstractResource.cpp
Normal file
17
src/Engine/Core/Resource/CAbstractResource.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "CAbstractResource.hpp"
|
||||
|
||||
namespace CosmicCore {
|
||||
|
||||
|
||||
const char* typeToString(EResourceType type)
|
||||
{
|
||||
switch (type) {
|
||||
case EResourceType::RESOURCE_MESH:
|
||||
return "Mesh";
|
||||
case EResourceType::RESOURCE_MATERIAL:
|
||||
return "Material";
|
||||
default:
|
||||
return "UNKNOWN_RESOURCE";
|
||||
}
|
||||
}
|
||||
}
|
||||
25
src/Engine/Core/Resource/CAbstractResource.hpp
Normal file
25
src/Engine/Core/Resource/CAbstractResource.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef CABSTRACT_RESOURCE_HPP
|
||||
#define CABSTRACT_RESOURCE_HPP
|
||||
#include "../Utils/CSerializable.hpp"
|
||||
|
||||
namespace CosmicCore {
|
||||
/**
|
||||
* @brief Enum representing every type of resources that can be created.
|
||||
*/
|
||||
enum class EResourceType : unsigned char {
|
||||
RESOURCE_MESH,
|
||||
RESOURCE_MATERIAL,
|
||||
RESOURCE_COUNT
|
||||
};
|
||||
|
||||
const char* typeToString(EResourceType type);
|
||||
|
||||
class CAbstractResource : public CSerializable{
|
||||
public:
|
||||
virtual ~CAbstractResource() = default;
|
||||
virtual nlohmann::json to_json() = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,6 @@
|
||||
#include "CAbstractMaterial.hpp"
|
||||
namespace CosmicCore {
|
||||
CAbstractMaterial::CAbstractMaterial(): CAbstractResource()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifndef CABSTRACTMATERIAL_HPP
|
||||
#define CABSTRACTMATERIAL_HPP
|
||||
#include "../../CAbstractResource.hpp"
|
||||
#include "nlohmann/json_fwd.hpp"
|
||||
namespace CosmicCore {
|
||||
class CAbstractMaterial: public CAbstractResource
|
||||
{
|
||||
public:
|
||||
CAbstractMaterial();
|
||||
nlohmann::json to_json(){return nlohmann::json();};
|
||||
};
|
||||
}
|
||||
#endif
|
||||
9
src/Engine/Core/Resource/Model/Mesh/CAbstractMesh.cpp
Normal file
9
src/Engine/Core/Resource/Model/Mesh/CAbstractMesh.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "CAbstractMesh.hpp"
|
||||
|
||||
namespace CosmicCore {
|
||||
CAbstractMesh::CAbstractMesh(): CAbstractResource()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
13
src/Engine/Core/Resource/Model/Mesh/CAbstractMesh.hpp
Normal file
13
src/Engine/Core/Resource/Model/Mesh/CAbstractMesh.hpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef CABSTRACTMESH_HPP
|
||||
#define CABSTRACTMESH_HPP
|
||||
#include "../../CAbstractResource.hpp"
|
||||
#include "nlohmann/json_fwd.hpp"
|
||||
namespace CosmicCore {
|
||||
class CAbstractMesh: public CAbstractResource
|
||||
{
|
||||
public:
|
||||
CAbstractMesh();
|
||||
nlohmann::json to_json(){return nlohmann::json();};
|
||||
};
|
||||
}
|
||||
#endif
|
||||
61
src/Engine/Core/Scene/CScene.cpp
Normal file
61
src/Engine/Core/Scene/CScene.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#include "CScene.hpp"
|
||||
#include "nlohmann/json_fwd.hpp"
|
||||
#include "../Component/Graphics/CTestRenderer.hpp"
|
||||
//#include "../../Controller/Exception/CRuntimeException.hpp"
|
||||
//#include "Components/CAbstractScript.hpp"
|
||||
|
||||
namespace CosmicCore {
|
||||
|
||||
CScene::CScene(std::string name) : CSerializable(),
|
||||
m_name(std::move(name)) {
|
||||
}
|
||||
|
||||
|
||||
std::string CScene::getName(void) const {
|
||||
return m_name;
|
||||
}
|
||||
|
||||
unsigned int CScene::getNumEntity(void) const {
|
||||
return this->m_ecManager.view<CMetaData>()->size();
|
||||
}
|
||||
|
||||
void CScene::removeEntity(unsigned int index, bool destroy)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void CScene::render(){
|
||||
auto renderers = m_ecManager.view<CTestRenderer>();
|
||||
for(auto beg = renderers->begin(); beg != renderers->end(); ++beg)
|
||||
{
|
||||
beg->render();
|
||||
}
|
||||
//std::chrono::steady_clock::time_point begin1 = std::chrono::steady_clock::now();
|
||||
//auto eh = m_ecManager.create();
|
||||
//m_ecManager.emplace<CTestRenderer>(eh, nullptr);
|
||||
//std::chrono::steady_clock::time_point end1 = std::chrono::steady_clock::now();
|
||||
//std::cout << "added entity and comptest : " <<std::chrono::duration_cast<std::chrono::seconds>(end1 - begin1).count() << std::endl;
|
||||
//static unsigned int entitynb = 0;
|
||||
//entitynb++;
|
||||
//std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
|
||||
//m_ecManager.view<CTestRenderer>().each([](auto e, CTestRenderer& comp){comp.render();});
|
||||
//std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
|
||||
//std::cout << "render func : " << std::chrono::duration_cast<std::chrono::seconds>(end - begin).count() << std::endl;
|
||||
//std::cout << "nb : " << entitynb << std::endl;
|
||||
}
|
||||
|
||||
// void CScene::setMask(unsigned int mask)
|
||||
// {
|
||||
// m_maskLayer = mask;
|
||||
// }
|
||||
|
||||
// unsigned int CScene::getMask(void)
|
||||
// {
|
||||
// return m_maskLayer;
|
||||
// }
|
||||
|
||||
nlohmann::json CScene::to_json()
|
||||
{
|
||||
return nlohmann::json();
|
||||
}
|
||||
}
|
||||
93
src/Engine/Core/Scene/CScene.hpp
Normal file
93
src/Engine/Core/Scene/CScene.hpp
Normal file
@@ -0,0 +1,93 @@
|
||||
#ifndef CSCENE_HPP
|
||||
#define CSCENE_HPP
|
||||
|
||||
#include "../Component/Geometry/CTransform.hpp"
|
||||
#include "../Component/Meta/CMetaData.hpp"
|
||||
|
||||
#include <entt/entt.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace CosmicCore {;
|
||||
class CScene : public CSerializable {
|
||||
using ECManager = entt::registry;
|
||||
private:
|
||||
// The name of the scene.
|
||||
std::string m_name;
|
||||
ECManager m_ecManager;
|
||||
|
||||
public:
|
||||
CScene() = delete;
|
||||
CScene(std::string name);
|
||||
~CScene() = default;
|
||||
|
||||
unsigned int getNumEntity() const;
|
||||
|
||||
CEntity addEntity(std::string name, std::string description){
|
||||
auto handle = m_ecManager.create();
|
||||
CEntity handler(m_ecManager, handle);
|
||||
m_ecManager.emplace<CTransform>(handle, handler);
|
||||
m_ecManager.emplace<CMetaData>(handle, handler, std::move(name), std::move(description));
|
||||
return handler;
|
||||
}
|
||||
|
||||
std::string getName() const;
|
||||
|
||||
void removeEntity(unsigned int index, bool destroy = true);
|
||||
|
||||
//CEntity* getActiveCamera();
|
||||
//void setActiveCamera(CEntity* newCamera);
|
||||
|
||||
/**
|
||||
* @brief Getter to the light.
|
||||
* @return The pointer m_mainLight.
|
||||
*/
|
||||
//CEntity* getMainLight();
|
||||
|
||||
/**
|
||||
* @brief Setter of the light.
|
||||
* @param light : The light to set as main light.
|
||||
*/
|
||||
//void setMainLight(CEntity* light);
|
||||
|
||||
/**
|
||||
* @brief Setter of the ODE world handling class.
|
||||
* @param[in, out] world The pointer to the physical world.
|
||||
*/
|
||||
//void setTangibleWorld(CTangibleWorld* world);
|
||||
|
||||
/**
|
||||
* @brief Getter to the physical world of the environment.
|
||||
* @return The pointer m_tangibleWorld.
|
||||
*/
|
||||
//std::weak_ptr<CAbstractTangibleWorld> getTangibleWorld();
|
||||
|
||||
/**
|
||||
* @brief Setter of the layer mask.
|
||||
* @param[in] mask The new mask value.
|
||||
*/
|
||||
void setMask(unsigned int mask);
|
||||
|
||||
/**
|
||||
* @brief Getter to the layer mask.
|
||||
* @return The value of m_maskLayer.
|
||||
*/
|
||||
//unsigned int getMask();
|
||||
|
||||
void render();
|
||||
//void updateScript();
|
||||
|
||||
nlohmann::json to_json();
|
||||
|
||||
/*static CScene* from_json(nlohmann::json& j)
|
||||
{
|
||||
CScene* s = new CScene(j["Name"]);
|
||||
for (nlohmann::json& entity : j["Entities"])
|
||||
{
|
||||
CEntity* e = CEntity::from_json(s, entity);
|
||||
s->addEntity(e);
|
||||
}
|
||||
return s;
|
||||
};*/
|
||||
};
|
||||
}
|
||||
#endif
|
||||
10
src/Engine/Core/Utils/CSerializable.hpp
Normal file
10
src/Engine/Core/Utils/CSerializable.hpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef CSERIALIZABLE_HPP
|
||||
#define CSERIALIZABLE_HPP
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
class CSerializable {
|
||||
public:
|
||||
virtual nlohmann::json to_json() = 0;
|
||||
};
|
||||
#endif
|
||||
62
src/Engine/Core/Utils/File/CFileManager.cpp
Normal file
62
src/Engine/Core/Utils/File/CFileManager.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#include "CFileManager.hpp"
|
||||
|
||||
void CFileManager::open(const char mode) {
|
||||
DEBUG_LOG(trace, "Kernel", "FileManager", "file " + m_filePath + " opened.")
|
||||
|
||||
switch (mode) {
|
||||
case 'r':
|
||||
m_filePtr = fopen(m_filePath.c_str(), "r");
|
||||
break;
|
||||
case 'w':
|
||||
m_filePtr = fopen(m_filePath.c_str(), "w");
|
||||
break;
|
||||
default:
|
||||
throw std::invalid_argument("\'mode\' parameter must be \'r\' (read) or \'w\' (write)");
|
||||
}
|
||||
if (m_filePtr == NULL) {
|
||||
throw CFileException("File \"" + m_filePath + "\" can't be opened");
|
||||
}
|
||||
}
|
||||
|
||||
void CFileManager::close(void) {
|
||||
DEBUG_LOG(trace, "Kernel", "FileManager", "file " + m_filePath + " closed.")
|
||||
|
||||
if (m_filePtr != NULL) {
|
||||
fclose(m_filePtr);
|
||||
}
|
||||
}
|
||||
|
||||
CFileManager::CFileManager(std::string path) :
|
||||
m_filePath(path),
|
||||
m_filePtr(NULL) {
|
||||
}
|
||||
|
||||
CFileManager::~CFileManager(void) {
|
||||
}
|
||||
|
||||
std::string CFileManager::getPath(void) const {
|
||||
return m_filePath;
|
||||
}
|
||||
|
||||
void CFileManager::write(std::string content) {
|
||||
open('w');
|
||||
|
||||
fputs(content.c_str(), m_filePtr);
|
||||
|
||||
close();
|
||||
}
|
||||
|
||||
std::string CFileManager::read(void) {
|
||||
open('r');
|
||||
|
||||
std::string out("");
|
||||
char ch;
|
||||
|
||||
while ((ch = fgetc(m_filePtr)) != EOF) {
|
||||
out += ch;
|
||||
}
|
||||
|
||||
close();
|
||||
|
||||
return out;
|
||||
}
|
||||
80
src/Engine/Core/Utils/File/CFileManager.hpp
Normal file
80
src/Engine/Core/Utils/File/CFileManager.hpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#ifndef CFILEMANAGER_HPP
|
||||
#define CFILEMANAGER_HPP
|
||||
|
||||
#include "../Exception/CFileException.hpp"
|
||||
#include "../../Context/CContext.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @file CFileManager.hpp
|
||||
* @brief File of CException, a class to manage program file.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Class to manage file.
|
||||
*/
|
||||
class CFileManager {
|
||||
private:
|
||||
// The pointer to file.
|
||||
FILE* m_filePtr;
|
||||
|
||||
// The file path.
|
||||
std::string m_filePath;
|
||||
|
||||
/**
|
||||
* @brief Open the file with the given mode
|
||||
* @param[in] mode The mode of opening file, must be 'r' or 'w';
|
||||
* @post throw a std::invalid_argument if mode is invalid, or a CFileException if the file can't be opened.
|
||||
*/
|
||||
void open(const char mode);
|
||||
|
||||
/**
|
||||
* @brief Close the file
|
||||
* @post Do nothing if the file isn't opened.
|
||||
*/
|
||||
void close(void);
|
||||
|
||||
public:
|
||||
CFileManager(void) = delete;
|
||||
CFileManager(const CFileManager& param) = delete;
|
||||
CFileManager& operator=(const CFileManager& param) = delete;
|
||||
|
||||
/**
|
||||
* @brief Build a file manager to the file's given path.
|
||||
* @param[in] path The path of the file;
|
||||
*/
|
||||
CFileManager(std::string path);
|
||||
|
||||
/**
|
||||
* @brief the destructor.
|
||||
* @pre The file must be close before destroying this.
|
||||
*/
|
||||
~CFileManager(void);
|
||||
|
||||
/**
|
||||
* @brief The file path getter.
|
||||
* @return std::string The path of the file;
|
||||
*/
|
||||
std::string getPath(void) const;
|
||||
|
||||
/**
|
||||
* @brief Write the file function.
|
||||
* @param[in] content The new content of the file.
|
||||
*
|
||||
* This function erase the old content of the file and replace it by "content"
|
||||
*/
|
||||
void write(std::string content);
|
||||
|
||||
/**
|
||||
* @brief Read the file function.
|
||||
* @return std::string, The content of the file.
|
||||
*/
|
||||
std::string read(void);
|
||||
};
|
||||
|
||||
#endif
|
||||
150
src/Engine/Core/Utils/File/CModelLoader.cpp
Normal file
150
src/Engine/Core/Utils/File/CModelLoader.cpp
Normal file
@@ -0,0 +1,150 @@
|
||||
#include "CModelLoader.hpp"
|
||||
#include <iostream>
|
||||
|
||||
CModel* CModelLoader::loadModel(std::string fileName, SMaterial* material)
|
||||
{
|
||||
Assimp::Importer import;
|
||||
const aiScene* scene = import.ReadFile(fileName, aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_MakeLeftHanded | aiProcess_FlipWindingOrder | aiProcess_JoinIdenticalVertices);
|
||||
|
||||
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
|
||||
{
|
||||
std::cout << std::string(import.GetErrorString()) << std::endl;
|
||||
DEBUG_LOG(trace, "Kernel", "ModelLoader", std::string("Assimp error : ") + std::string(import.GetErrorString()))
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::vector<CMesh*> meshes;
|
||||
processNode(scene->mRootNode, scene, &meshes, material);
|
||||
|
||||
CModel* model = new CModel(meshes);
|
||||
model->load();
|
||||
return model;
|
||||
}
|
||||
|
||||
void CModelLoader::processNode(aiNode* node, const aiScene* scene, std::vector<CMesh*>* meshes, SMaterial* material)
|
||||
{
|
||||
// process all the node's meshes (if any)
|
||||
for (unsigned int i = 0; i < node->mNumMeshes; i++)
|
||||
{
|
||||
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
|
||||
processMesh(mesh, scene, meshes, material);
|
||||
}
|
||||
// then do the same for each of its children
|
||||
for (unsigned int i = 0; i < node->mNumChildren; i++)
|
||||
{
|
||||
processNode(node->mChildren[i], scene, meshes, material);
|
||||
}
|
||||
}
|
||||
|
||||
void CModelLoader::processMesh(aiMesh* mesh, const aiScene* scene, std::vector<CMesh*>* meshes, SMaterial* material)
|
||||
{
|
||||
std::vector<SVertex> vertices;
|
||||
std::vector<unsigned int> indices;
|
||||
SVertex vertex;
|
||||
for (unsigned int i = 0; i < mesh->mNumVertices; i++)
|
||||
{
|
||||
vertex.m_position = glm::vec3(mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z);
|
||||
vertex.m_normal = glm::vec3(mesh->mNormals[i].x, mesh->mNormals[i].y, mesh->mNormals[i].z);
|
||||
|
||||
if (mesh->mTextureCoords[0]) // if the mesh contains texture coordinates
|
||||
{
|
||||
vertex.m_texCoords.x = mesh->mTextureCoords[0][i].x;
|
||||
vertex.m_texCoords.y = mesh->mTextureCoords[0][i].y;
|
||||
}
|
||||
else
|
||||
{
|
||||
vertex.m_texCoords = glm::vec2(0.0f, 0.0f);
|
||||
}
|
||||
|
||||
if (mesh->mBitangents)// if the mesh contains bitangent coordinates
|
||||
{
|
||||
vertex.m_bitangent.x = mesh->mBitangents->x;
|
||||
vertex.m_bitangent.y = mesh->mBitangents->y;
|
||||
vertex.m_bitangent.z = mesh->mBitangents->z;
|
||||
}
|
||||
else
|
||||
{
|
||||
vertex.m_bitangent = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
}
|
||||
|
||||
if (mesh->mTangents)// if the mesh contains tangent coordinates
|
||||
{
|
||||
vertex.m_tangent.x = mesh->mTangents->x;
|
||||
vertex.m_tangent.y = mesh->mTangents->y;
|
||||
vertex.m_tangent.z = mesh->mTangents->z;
|
||||
}
|
||||
else
|
||||
{
|
||||
vertex.m_tangent = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
}
|
||||
|
||||
vertices.push_back(vertex);
|
||||
}
|
||||
// process indices
|
||||
|
||||
for (unsigned int i = 0; i < mesh->mNumFaces; i++)
|
||||
{
|
||||
aiFace face = mesh->mFaces[i];
|
||||
for (unsigned int j = 0; j < face.mNumIndices; j++)
|
||||
indices.push_back(face.mIndices[j]);
|
||||
}
|
||||
// process material
|
||||
std::vector<CImageTexture*> diffuseMaps;
|
||||
std::vector<CImageTexture*> specularMaps;
|
||||
std::vector<CAbstractTexture*> textures;
|
||||
SMaterial* mat = new SMaterial;
|
||||
if (mesh->mMaterialIndex >= 0 && material == nullptr)
|
||||
{
|
||||
aiMaterial* amaterial = scene->mMaterials[mesh->mMaterialIndex];
|
||||
diffuseMaps = loadMaterialTextures(amaterial,
|
||||
aiTextureType_DIFFUSE, ETextureType::TEXTURE_DIFFUSE);
|
||||
specularMaps = loadMaterialTextures(amaterial,
|
||||
aiTextureType_SPECULAR, ETextureType::TEXTURE_SPECULAR);
|
||||
|
||||
for (size_t i = 0; i < diffuseMaps.size(); i++)
|
||||
{
|
||||
textures.push_back((CAbstractTexture*)diffuseMaps[i]);
|
||||
}
|
||||
for (size_t j = 0; j < specularMaps.size(); j++)
|
||||
{
|
||||
textures.push_back((CAbstractTexture*)specularMaps[j]);
|
||||
}
|
||||
std::vector<SColor> colorsC; //= { {glm::vec4(1.0f, 1.0f, 1.0f, 1.0f), EColorType::COLOR_DIFFUSE} };
|
||||
aiColor4D color(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
amaterial->Get(AI_MATKEY_COLOR_DIFFUSE, color);
|
||||
SColor diffuseColor = { glm::vec4(color.r, color.g, color.b, color.a), EColorType::COLOR_DIFFUSE };
|
||||
colorsC.push_back(diffuseColor);
|
||||
|
||||
*mat = { textures, colorsC, CKernel::m_mainShader };
|
||||
}
|
||||
else// if the material is given, override the material of the wavefront obj file
|
||||
{
|
||||
delete mat;
|
||||
mat = material;
|
||||
}
|
||||
|
||||
|
||||
CMesh* m = new CMesh();
|
||||
|
||||
m->setMaterial(mat);
|
||||
|
||||
m->setIndexes(indices);
|
||||
m->setVertices(vertices);
|
||||
|
||||
meshes->push_back(m);
|
||||
|
||||
}
|
||||
|
||||
std::vector<CImageTexture*> CModelLoader::loadMaterialTextures(aiMaterial* mat, aiTextureType type, ETextureType typeName)
|
||||
{
|
||||
std::vector<CImageTexture*> textures;
|
||||
for (unsigned int i = 0; i < mat->GetTextureCount(type); i++)
|
||||
{
|
||||
aiString str;
|
||||
mat->GetTexture(type, i, &str);
|
||||
CImageTexture* text = new CImageTexture(typeName, str.C_Str());
|
||||
text->init();//Load the texture in GPU before adding it the texture list
|
||||
textures.push_back(text);
|
||||
}
|
||||
return textures;
|
||||
}
|
||||
59
src/Engine/Core/Utils/File/CModelLoader.hpp
Normal file
59
src/Engine/Core/Utils/File/CModelLoader.hpp
Normal file
@@ -0,0 +1,59 @@
|
||||
#ifndef CMODELLOADER_HPP
|
||||
#define CMODELLOADER_HPP
|
||||
|
||||
|
||||
#include <assimp/Importer.hpp>
|
||||
#include <assimp/scene.h>
|
||||
#include <assimp/postprocess.h>
|
||||
#include "../../Model/Graphic/Texture/CImageTexture.hpp"
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include "../../Model/Graphic/Mesh/CMesh.hpp"
|
||||
#include "../../Model/Graphic/CModel.hpp"
|
||||
#include "../../Model/Node/CScene.hpp"
|
||||
#include "../../Controller/CKernel.hpp"
|
||||
#include "../../Context/CContext.hpp"
|
||||
|
||||
/**
|
||||
* @brief Class allowing to load models from obj files.
|
||||
*/
|
||||
class CModelLoader {
|
||||
private:
|
||||
/**
|
||||
* @brief Compute every node in the loaded meshes.
|
||||
* @param[in] node The assimp created node.
|
||||
* @param[in] scene The assimp created scene.
|
||||
* @param[in] meshes The mesh list.
|
||||
* @param[in] material A pointer to a material. It's optionnal and override the obj's material.
|
||||
*/
|
||||
static void processNode(aiNode* node, const aiScene* scene, std::vector<CMesh*>* meshes, SMaterial* material);
|
||||
|
||||
/**
|
||||
* @brief Compute every meshes.
|
||||
* @param[in] mesh The assimp created mesh.
|
||||
* @param[in] scene The assimp created scene.
|
||||
* @param[in] meshes The list of meshes.
|
||||
* @param[in] material A pointer to a material. It's optionnal and override the obj's material.
|
||||
*/
|
||||
static void processMesh(aiMesh* mesh, const aiScene* scene, std::vector<CMesh*>* meshes, SMaterial* material);
|
||||
|
||||
/**
|
||||
* @brief Load Textures from the obj file.
|
||||
* @param[in] mat The assimp created material.
|
||||
* @param[in] type The assimp's texture type.
|
||||
* @param[in] typeName The texture's type.
|
||||
*/
|
||||
static std::vector<CImageTexture*> loadMaterialTextures(aiMaterial* mat, aiTextureType type, ETextureType typeName);
|
||||
public:
|
||||
|
||||
/**
|
||||
* @brief main function of the class, starts loading the obj file.
|
||||
* @param[in] fileName Path to the OBJ file.
|
||||
* @param[in] material Pointer to a material. It's optionnal and override the obj's material.
|
||||
* @return A dynamically allocated CModel.
|
||||
*/
|
||||
static CModel* loadModel(std::string fileName, SMaterial* material);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
5
src/Engine/Modules/Module/CModuleLoader.cpp
Normal file
5
src/Engine/Modules/Module/CModuleLoader.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
#include "CModuleLoader.hpp"
|
||||
namespace CosmicCore {
|
||||
std::vector<void*> CModuleLoader::handles;
|
||||
}
|
||||
66
src/Engine/Modules/Module/CModuleLoader.hpp
Normal file
66
src/Engine/Modules/Module/CModuleLoader.hpp
Normal file
@@ -0,0 +1,66 @@
|
||||
|
||||
#ifndef CMODULELOADER_HPP
|
||||
#define CMODULELOADER_HPP
|
||||
#include <dlfcn.h>
|
||||
#include <iostream>
|
||||
#include <filesystem>
|
||||
#include "../Configuration/CConfiguration.hpp"
|
||||
namespace CosmicCore
|
||||
{
|
||||
class CModuleLoader {
|
||||
public:
|
||||
static std::vector<void*> handles;
|
||||
|
||||
static int loadAll(){
|
||||
std::filesystem::directory_entry rootDir(CConfiguration::moduleConfiguration.getModulesDirectory());
|
||||
if(rootDir.exists() && rootDir.is_directory())
|
||||
{
|
||||
std::filesystem::directory_iterator iteR(rootDir);
|
||||
for(auto fileEntry : iteR)
|
||||
{
|
||||
if(fileEntry.is_regular_file())
|
||||
{
|
||||
auto ext = fileEntry.path().extension().string();
|
||||
if(ext == ".so")
|
||||
{
|
||||
if(load(fileEntry.path()))
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int load(std::filesystem::path moduleFile){
|
||||
void* handle = dlopen(moduleFile.c_str(), RTLD_LAZY);
|
||||
if (!handle) {
|
||||
std::cerr << "Failed to load shared library: " << dlerror() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
// Get the registration function
|
||||
typedef void (*RegisterModuleFunc)();
|
||||
auto registerModule = (RegisterModuleFunc)dlsym(handle, "registerModule");
|
||||
if (!registerModule) {
|
||||
std::cerr << "Failed to find registration function: " << dlerror() << std::endl;
|
||||
dlclose(handle);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Register the components
|
||||
registerModule();
|
||||
handles.emplace_back(handle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int releaseAll()
|
||||
{
|
||||
for(auto handle : handles)
|
||||
{
|
||||
dlclose(handle);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
#endif
|
||||
124
src/Engine/Utils/Factory/CEntityFactory.cpp
Normal file
124
src/Engine/Utils/Factory/CEntityFactory.cpp
Normal file
@@ -0,0 +1,124 @@
|
||||
/*#include "CEntityFactory.hpp"
|
||||
#include "CEntity.hpp"
|
||||
#include "Components/Graphic/Renderers/CMeshRenderer.hpp"
|
||||
#include "Components/Physic/CRigidBody.hpp"
|
||||
#include "Components/Physic/CCollider.hpp"
|
||||
#include "../Graphic/Material/SMaterial.hpp"
|
||||
#include "../Graphic/Material/SColor.hpp"
|
||||
|
||||
CEntity* CEntityFactory::createCube(CScene* e) {
|
||||
std::string name = "Cube" + std::to_string(CEntity::entityNumber);
|
||||
CEntity* cube = new CEntity(name, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3(0.0f, 0.0f, 0.0f));
|
||||
cube->setScene(e);
|
||||
CRigidBody* body = new CRigidBody(cube);
|
||||
CCollider* geom = new CCollider(cube, GEOM_BOX, body->getMass());
|
||||
CMeshRenderer* renderer = new CMeshRenderer(cube);
|
||||
renderer->createPremadeMesh(4, MODEL_CUBE);
|
||||
cube->attachComponent(body);
|
||||
cube->attachComponent(geom);
|
||||
cube->attachComponent(renderer);
|
||||
e->addEntity(cube);
|
||||
return cube;
|
||||
}
|
||||
|
||||
CEntity* CEntityFactory::createSphere(CScene* e) {
|
||||
std::string name = "Sphere" + std::to_string(CEntity::entityNumber);
|
||||
CEntity* sphere = new CEntity(name, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3(0.0f, 0.0f, 0.0f));
|
||||
sphere->setScene(e);
|
||||
CRigidBody* body = new CRigidBody(sphere);
|
||||
CCollider* geom = new CCollider(sphere, GEOM_SPHERE, body->getMass());
|
||||
CMeshRenderer* renderer = new CMeshRenderer(sphere);
|
||||
renderer->createPremadeMesh(4, MODEL_SPHERE);
|
||||
sphere->attachComponent(body);
|
||||
sphere->attachComponent(geom);
|
||||
sphere->attachComponent(renderer);
|
||||
e->addEntity(sphere);
|
||||
return sphere;
|
||||
}
|
||||
|
||||
CEntity* CEntityFactory::createCylinder(CScene* e) {
|
||||
std::string name = "Cylindre" + std::to_string(CEntity::entityNumber);
|
||||
CEntity* cylindre = new CEntity(name, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3(0.0f, 0.0f, 0.0f));
|
||||
cylindre->setScene(e);
|
||||
CRigidBody* body = new CRigidBody(cylindre);
|
||||
CCollider* geom = new CCollider(cylindre, GEOM_CYLINDRE, body->getMass());
|
||||
CMeshRenderer* renderer = new CMeshRenderer(cylindre);
|
||||
renderer->createPremadeMesh(4, MODEL_CYLINDRE);
|
||||
cylindre->attachComponent(body);
|
||||
cylindre->attachComponent(geom);
|
||||
cylindre->attachComponent(renderer);
|
||||
e->addEntity(cylindre);
|
||||
return cylindre;
|
||||
}
|
||||
|
||||
CEntity* CEntityFactory::createPlane(CScene* e) {
|
||||
std::string name = "Ground";
|
||||
CEntity* ground = new CEntity(name, glm::vec3(0.0f, 0.0f, -1.2f), glm::vec3(100.0f, 100.0f, 100.0f), glm::vec3(glm::radians(270.0f), glm::radians(180.0f), glm::radians(270.0f)));
|
||||
ground->setScene(e);
|
||||
CCollider* geom = new CCollider(ground, GEOM_PLANE);
|
||||
CMeshRenderer* renderer = new CMeshRenderer(ground);
|
||||
renderer->createPremadeMesh(4, MODEL_PLANE);
|
||||
ground->attachComponent(geom);
|
||||
ground->attachComponent(renderer);
|
||||
e->addEntity(ground);
|
||||
return ground;
|
||||
}
|
||||
|
||||
|
||||
CEntity* CEntityFactory::createReferenceAxis(CScene* e) {
|
||||
|
||||
std::string name = std::to_string(CEntity::entityNumber);
|
||||
|
||||
CEntity* r0 = new CEntity("R0" + name, glm::vec3(0.0f, -40.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3(0.0f, 0.0f, 0.0f));
|
||||
|
||||
CEntity* r1 = new CEntity("Ex" + name, glm::vec3(5.0f, 0.0f, 0.0f), glm::vec3(5.0f, 1.0f, 1.0f), glm::vec3(0.0f, 0.0f, 0.0f));
|
||||
|
||||
CEntity* r2 = new CEntity("Ey" + name, glm::vec3(0.0f, 5.0f, 0.0f), glm::vec3(1.0f, 5.0f, 1.0f), glm::vec3(0.0f, 0.0f, 0.0f));
|
||||
|
||||
CEntity* r3 = new CEntity("Ez" + name, glm::vec3(0.0f, 0.0f, 5.0f), glm::vec3(1.0f, 1.0f, 5.0f), glm::vec3(0.0f, 0.0f, 0.0f));
|
||||
|
||||
r0->setScene(e);
|
||||
r1->setScene(e);
|
||||
r2->setScene(e);
|
||||
r3->setScene(e);
|
||||
|
||||
r0->addChild(r1);
|
||||
r0->addChild(r2);
|
||||
r0->addChild(r3);
|
||||
|
||||
SMaterial* xMat = new SMaterial;
|
||||
xMat->m_shader = CKernel::m_mainShader;
|
||||
SColor xColor;
|
||||
xColor.m_type = EColorType::COLOR_DIFFUSE;
|
||||
xColor.m_vector = glm::vec4(1.0f, 0.0f, 0.0f, 1.0f);
|
||||
xMat->m_colors.push_back(xColor);
|
||||
SMaterial* yMat = new SMaterial;
|
||||
yMat->m_shader = CKernel::m_mainShader;
|
||||
SColor yColor;
|
||||
yColor.m_type = EColorType::COLOR_DIFFUSE;
|
||||
yColor.m_vector = glm::vec4(0.0f, 1.0f, 0.0f, 1.0f);
|
||||
yMat->m_colors.push_back(yColor);
|
||||
SMaterial* zMat = new SMaterial;
|
||||
zMat->m_shader = CKernel::m_mainShader;
|
||||
SColor zColor;
|
||||
zColor.m_type = EColorType::COLOR_DIFFUSE;
|
||||
zColor.m_vector = glm::vec4(0.0f, 0.0f, 1.0f, 1.0f);
|
||||
zMat->m_colors.push_back(zColor);
|
||||
|
||||
|
||||
CMeshRenderer* xRender = new CMeshRenderer(r1);
|
||||
CMeshRenderer* yRender = new CMeshRenderer(r2);
|
||||
CMeshRenderer* zRender = new CMeshRenderer(r3);
|
||||
xRender->createPremadeMesh(4, MODEL_CUBE, xMat);
|
||||
yRender->createPremadeMesh(4, MODEL_CUBE, yMat);
|
||||
zRender->createPremadeMesh(4, MODEL_CUBE, zMat);
|
||||
|
||||
r1->attachComponent(xRender);
|
||||
r2->attachComponent(yRender);
|
||||
r3->attachComponent(zRender);
|
||||
|
||||
e->addEntity(r0);
|
||||
|
||||
return r0;
|
||||
}
|
||||
*/
|
||||
17
src/Engine/Utils/Factory/CEntityFactory.hpp
Normal file
17
src/Engine/Utils/Factory/CEntityFactory.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
//#include "CEntity.fwd.hpp"
|
||||
//#include "CScene.fwd.hpp"
|
||||
|
||||
/*
|
||||
#include <string>
|
||||
|
||||
namespace CEntityFactory {
|
||||
CEntity* createCube(CScene* e);
|
||||
|
||||
CEntity* createSphere(CScene* e);
|
||||
|
||||
CEntity* createCylinder(CScene* e);
|
||||
|
||||
CEntity* createPlane(CScene* e);
|
||||
|
||||
CEntity* createReferenceAxis(CScene* e);
|
||||
}*/
|
||||
36
src/Engine/Utils/Factory/CShaderFactory.cpp
Normal file
36
src/Engine/Utils/Factory/CShaderFactory.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include "CShaderFactory.hpp"
|
||||
#include "../CKernel.hpp"
|
||||
#include "../../Specific/Graphic/OpenGLImpl/CShaderOpenGLImpl.hpp"
|
||||
|
||||
CShader* CShaderFactory::createShader(CKernel* e, std::string name, std::string vertexPath, std::string fragmentPath) {
|
||||
if (e == nullptr) {
|
||||
throw CLogicException("CKernel is null");
|
||||
}
|
||||
return createShader(e->getEngineConfig()->getGraphicImplementation(), name, vertexPath, fragmentPath);
|
||||
}
|
||||
|
||||
CShader* CShaderFactory::createShader(CKernel* e, std::string name, std::string vertexPath, std::string fragmentPath, std::string geomPath) {
|
||||
if (e == nullptr) {
|
||||
throw CLogicException("CKernel is null");
|
||||
}
|
||||
return createShader(e->getEngineConfig()->getGraphicImplementation(), name, vertexPath, fragmentPath, geomPath);
|
||||
}
|
||||
|
||||
CShader* CShaderFactory::createShader(EGraphicImplementation e, std::string name, std::string vertexPath, std::string fragmentPath) {
|
||||
switch (e) {
|
||||
case EGraphicImplementation::GRAPHIC_OPENGL:
|
||||
return new CShaderOpenGLImpl(name, vertexPath, fragmentPath);
|
||||
break;
|
||||
}
|
||||
throw CLogicException("Unexpected error while creating shader");
|
||||
|
||||
}
|
||||
|
||||
CShader* CShaderFactory::createShader(EGraphicImplementation e, std::string name, std::string vertexPath, std::string fragmentPath, std::string geomPath) {
|
||||
switch (e) {
|
||||
case EGraphicImplementation::GRAPHIC_OPENGL:
|
||||
return new CShaderOpenGLImpl(name, vertexPath, fragmentPath, geomPath);
|
||||
break;
|
||||
}
|
||||
throw CLogicException("Unexpected error while creating shader");
|
||||
}
|
||||
18
src/Engine/Utils/Factory/CShaderFactory.hpp
Normal file
18
src/Engine/Utils/Factory/CShaderFactory.hpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef CSHADERFACTORY_HPP
|
||||
#define CSHADERFACTORY_HPP
|
||||
|
||||
#include "../Configuration/EImplementations.hpp"
|
||||
#include "../../Model/Graphic/Shader/CShader.hpp"
|
||||
#include "../CKernel.fwd.hpp"
|
||||
|
||||
namespace CShaderFactory {
|
||||
CShader* createShader(CKernel* e, std::string name, std::string vertexPath, std::string fragmentPath);
|
||||
|
||||
CShader* createShader(EGraphicImplementation e, std::string name, std::string vertexPath, std::string fragmentPath);
|
||||
|
||||
CShader* createShader(CKernel* e, std::string name, std::string vertexPath, std::string fragmentPath, std::string geomPath);
|
||||
|
||||
CShader* createShader(EGraphicImplementation e, std::string name, std::string vertexPath, std::string fragmentPath, std::string geomPath);
|
||||
}
|
||||
|
||||
#endif
|
||||
11
src/Engine/Utils/Factory/ComponentFactory.cpp
Normal file
11
src/Engine/Utils/Factory/ComponentFactory.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "ComponentFactory.hpp"
|
||||
|
||||
namespace CosmicCore {
|
||||
CComponentFactory& globalComponentFactory = CComponentFactory::instance();
|
||||
|
||||
CComponentFactory& CComponentFactory::instance() {
|
||||
static CComponentFactory factory;
|
||||
return factory;
|
||||
}
|
||||
}
|
||||
|
||||
95
src/Engine/Utils/Factory/ComponentFactory.hpp
Normal file
95
src/Engine/Utils/Factory/ComponentFactory.hpp
Normal file
@@ -0,0 +1,95 @@
|
||||
#ifndef CCOMPONENTFACTORY_HPP
|
||||
#define CCOMPONENTFACTORY_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <typeindex>
|
||||
#include <utility>
|
||||
#include "../Component/CAbstractComponent.hpp"
|
||||
|
||||
//TODO modifier pour la scène soit obligatoire, sans scène pas d'entité, sans entité pas de composants
|
||||
namespace CosmicCore
|
||||
{
|
||||
class CEntity;
|
||||
class CComponentFactory {
|
||||
public:
|
||||
using ComponentCreator = std::function<std::shared_ptr<CAbstractComponent>(std::weak_ptr<CEntity>&, const std::vector<std::any>&)>;
|
||||
using ResourceComponentCreator = std::function<std::shared_ptr<CAbstractComponent>(const std::vector<std::any>&)>;
|
||||
|
||||
static CComponentFactory& instance();
|
||||
|
||||
static void registerStandardProvidedAbstractComponents()
|
||||
{
|
||||
//auto& ins = instance();
|
||||
//ins.registerComponent<CAbstractComponent>();//todo
|
||||
}
|
||||
|
||||
template <typename T, typename... Args>
|
||||
void registerComponent() {
|
||||
creators[typeid(T)] = [](std::weak_ptr<CEntity> entity, const std::vector<std::any>& args) {
|
||||
return createComponent<T, Args...>(entity, args, std::index_sequence_for<Args...>{});
|
||||
};
|
||||
}
|
||||
|
||||
template <typename T, typename... Args>
|
||||
void registerResourceComponent() {
|
||||
resourceCreators[typeid(T)] = [](const std::vector<std::any>& args) {
|
||||
return createComponent<T, Args...>(args, std::index_sequence_for<Args...>{});
|
||||
};
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::shared_ptr<T> create(std::weak_ptr<CEntity> entity, const std::vector<std::any>& params) {
|
||||
auto it = creators.find(typeid(T));
|
||||
if (it != creators.end()) {
|
||||
return std::dynamic_pointer_cast<T>(it->second(entity, params));
|
||||
}
|
||||
throw std::runtime_error("Component not registered");
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::shared_ptr<T> createResource(const std::vector<std::any>& params) {
|
||||
//ajouter au cache des resources pour réutilisation
|
||||
auto it = resourceCreators.find(typeid(T));
|
||||
if (it != resourceCreators.end()) {
|
||||
return std::dynamic_pointer_cast<T>(it->second(params));
|
||||
}
|
||||
throw std::runtime_error("Resource Component not registered: " + std::string(typeid(T).name()));
|
||||
}
|
||||
|
||||
std::unordered_map<std::type_index, ComponentCreator>& getRegisteredComponents(){return creators;};
|
||||
|
||||
std::unordered_map<std::type_index, ResourceComponentCreator>& getRegisteredResourceComponents(){return resourceCreators;};
|
||||
|
||||
private:
|
||||
std::unordered_map<std::type_index, ComponentCreator> creators;
|
||||
std::unordered_map<std::type_index, ResourceComponentCreator> resourceCreators;
|
||||
|
||||
|
||||
template <typename T, typename... Args, std::size_t... Is>
|
||||
static std::shared_ptr<T> createComponent(std::weak_ptr<CEntity>& entity, const std::vector<std::any>& args,
|
||||
std::index_sequence<Is...>) {
|
||||
try {
|
||||
return std::make_shared<T>(entity, std::any_cast<Args>(args[Is])...);
|
||||
} catch (const std::bad_any_cast& e) {
|
||||
throw std::runtime_error("Type mismatch in component arguments: " + std::string(e.what()));
|
||||
}
|
||||
}
|
||||
template <typename T, typename... Args, std::size_t... Is>
|
||||
static std::shared_ptr<T> createComponent(const std::vector<std::any>& args,
|
||||
std::index_sequence<Is...>) {
|
||||
try {
|
||||
return std::make_shared<T>(std::any_cast<Args>(args[Is])...);
|
||||
} catch (const std::bad_any_cast& e) {
|
||||
throw std::runtime_error("Type mismatch in component arguments: " + std::string(e.what()));
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
extern CComponentFactory& globalComponentFactory;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
35
src/main.cpp
Normal file
35
src/main.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
|
||||
//#include "Engine/Headers/Core.hpp"
|
||||
//#include "Engine/Headers/Components.hpp"
|
||||
|
||||
//#include <cstdlib>
|
||||
|
||||
/*#include <sndfile.h>
|
||||
|
||||
#include <al.h>
|
||||
#include <alc.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdlib>
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <omp.h>
|
||||
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/chrono.hpp>
|
||||
#include <boost/thread.hpp>
|
||||
#include <boost/array.hpp>
|
||||
#include <boost/shared_ptr.hpp>*/
|
||||
|
||||
#include "Engine/Core/Kernel/CKernel.hpp"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
// test();
|
||||
using namespace CosmicCore;
|
||||
CKernel g("Mon jeu", "Jeu");
|
||||
//CScene s("Scene1");
|
||||
g.start();
|
||||
g.loop();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user