Adding some components.
This commit is contained in:
@ -84,6 +84,30 @@ void assertTrueImplement(
|
||||
map.find(key) != map.end(), __VA_ARGS__ \
|
||||
)
|
||||
|
||||
/**
|
||||
* Asserts that a given value has a specific flag turned off.
|
||||
*
|
||||
* @param value Value to check.
|
||||
* @param flag Flag to check for.
|
||||
* @param message Message (sprintf format) to send to the logger.
|
||||
* @param args Optional TParam args for the sprintf message to accept.
|
||||
*/
|
||||
#define assertFlagOff(value, flag, ...) assertTrue( \
|
||||
(value & flag) == 0, __VA_ARGS__ \
|
||||
)
|
||||
|
||||
/**
|
||||
* Asserts that a given value has a specific flag turned on.
|
||||
*
|
||||
* @param value Value to check.
|
||||
* @param flag Flag to check for.
|
||||
* @param message Message (sprintf format) to send to the logger.
|
||||
* @param args Optional TParam args for the sprintf message to accept.
|
||||
*/
|
||||
#define assertFlagOn(value, flag, ...) assertTrue( \
|
||||
(value & flag) == flag, __VA_ARGS__ \
|
||||
)
|
||||
|
||||
/**
|
||||
* Asserts that the current code is deprecated and should not be used anymore.
|
||||
* @deprecated
|
||||
|
@ -6,4 +6,5 @@
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
Camera.cpp
|
||||
MeshRenderer.cpp
|
||||
)
|
@ -3,6 +3,7 @@
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "assert/assert.hpp"
|
||||
#include "Camera.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
@ -11,6 +12,11 @@ void Camera::onInit() {
|
||||
std::cout << "Camera" << std::endl;
|
||||
}
|
||||
|
||||
void Camera::onDispose() {
|
||||
std::cout << "~Camera" << std::endl;
|
||||
// renderTarget = nullptr;
|
||||
}
|
||||
|
||||
glm::mat4 Camera::getProjection() {
|
||||
switch(this->type) {
|
||||
case CameraType::ORTHOGONAL:
|
||||
@ -34,4 +40,8 @@ glm::mat4 Camera::getProjection() {
|
||||
|
||||
assertUnreachable("Invalid Camera Type!");
|
||||
return glm::mat4(1.0f);
|
||||
}
|
||||
|
||||
float_t Camera::getAspect() {
|
||||
return 1.0f;
|
||||
}
|
@ -28,6 +28,7 @@ namespace Dawn {
|
||||
// std::shared_ptr<RenderTarget> renderTarget;
|
||||
|
||||
void onInit() override;
|
||||
void onDispose() override;
|
||||
|
||||
/**
|
||||
* Returns the aspect ratio that the camera is using. In future I may
|
||||
|
16
src/dawn/component/display/MeshRenderer.cpp
Normal file
16
src/dawn/component/display/MeshRenderer.cpp
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "MeshRenderer.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
void MeshRenderer::onInit() {
|
||||
|
||||
}
|
||||
|
||||
void MeshRenderer::onDispose() {
|
||||
mesh = nullptr;
|
||||
}
|
@ -4,6 +4,7 @@
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "display/mesh/Mesh.hpp"
|
||||
#include "scene/SceneItem.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
@ -12,5 +13,6 @@ namespace Dawn {
|
||||
std::shared_ptr<Mesh> mesh;
|
||||
|
||||
void onInit() override;
|
||||
void onDispose() override;
|
||||
};
|
||||
}
|
15
src/dawn/display/CMakeLists.txt
Normal file
15
src/dawn/display/CMakeLists.txt
Normal file
@ -0,0 +1,15 @@
|
||||
# Copyright (c) 2022 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
Color.cpp
|
||||
)
|
||||
|
||||
# Subdirs
|
||||
# add_subdirectory(font)
|
||||
# add_subdirectory(mesh)
|
||||
# add_subdirectory(shader)
|
82
src/dawn/display/Color.cpp
Normal file
82
src/dawn/display/Color.cpp
Normal file
@ -0,0 +1,82 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "Color.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
struct Color Color::fromString(const std::string str) {
|
||||
// Convert to lowercase
|
||||
auto lower = stringToLowercase(str);
|
||||
|
||||
if(stringIncludes(lower, "cornflower")) {
|
||||
return COLOR_CORNFLOWER_BLUE;
|
||||
|
||||
} else if(stringIncludes(lower, "magenta")) {
|
||||
return COLOR_MAGENTA;
|
||||
|
||||
} else if(stringIncludes(lower, "white")) {
|
||||
return COLOR_WHITE;
|
||||
|
||||
} else if(stringIncludes(lower, "black")) {
|
||||
return COLOR_BLACK;
|
||||
|
||||
} else if(stringIncludes(lower, "red")) {
|
||||
return COLOR_RED;
|
||||
|
||||
} else if(stringIncludes(lower, "green")) {
|
||||
return COLOR_GREEN;
|
||||
|
||||
} else if(stringIncludes(lower, "blue")) {
|
||||
return COLOR_BLUE;
|
||||
|
||||
} else if(stringIncludes(lower, "transparent")) {
|
||||
return COLOR_TRANSPARENT;
|
||||
}
|
||||
|
||||
// Hex code?
|
||||
if(lower[0] == '#') {
|
||||
// Remove the hash
|
||||
lower = lower.substr(1);
|
||||
|
||||
// Convert to RGB
|
||||
if(lower.length() == 3) {
|
||||
// Convert to 6 digit hex
|
||||
lower = lower[0] + lower[0] + lower[1] + lower[1] + lower[2] + lower[2];
|
||||
}
|
||||
|
||||
// Convert to RGB
|
||||
return {
|
||||
(float_t)std::stoi(lower.substr(0, 2), nullptr, 16) / 255.0f,
|
||||
(float_t)std::stoi(lower.substr(2, 2), nullptr, 16) / 255.0f,
|
||||
(float_t)std::stoi(lower.substr(4, 2), nullptr, 16) / 255.0f,
|
||||
1.0f
|
||||
};
|
||||
}
|
||||
|
||||
// Split by comma
|
||||
auto splitByComma = stringSplit(str, ",");
|
||||
if(splitByComma.size() == 3) {
|
||||
// RGB
|
||||
return {
|
||||
(float_t)std::stof(splitByComma[0]),
|
||||
(float_t)std::stof(splitByComma[1]),
|
||||
(float_t)std::stof(splitByComma[2]),
|
||||
1.0f
|
||||
};
|
||||
} else if(splitByComma.size() == 4) {
|
||||
// RGBA
|
||||
return {
|
||||
(float_t)std::stof(splitByComma[0]),
|
||||
(float_t)std::stof(splitByComma[1]),
|
||||
(float_t)std::stof(splitByComma[2]),
|
||||
(float_t)std::stof(splitByComma[3])
|
||||
};
|
||||
}
|
||||
|
||||
// TODO: Parse other kinds of colors
|
||||
assertUnreachable("Failed to find a color match for %s", str);
|
||||
return {};
|
||||
}
|
89
src/dawn/display/Color.hpp
Normal file
89
src/dawn/display/Color.hpp
Normal file
@ -0,0 +1,89 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
struct ColorU8 {
|
||||
uint8_t r, g, b, a;
|
||||
};
|
||||
|
||||
#pragma pack(push, 4)
|
||||
struct Color final {
|
||||
/**
|
||||
* Returns a color from a string.
|
||||
*
|
||||
* @param str String to parse.
|
||||
* @return Color parsed.
|
||||
*/
|
||||
static struct Color fromString(const std::string str);
|
||||
|
||||
float_t r, g, b, a;
|
||||
|
||||
const struct Color& operator = (const struct Color &val) {
|
||||
this->r = val.r;
|
||||
this->g = val.g;
|
||||
this->b = val.b;
|
||||
this->a = val.a;
|
||||
return *this;
|
||||
}
|
||||
|
||||
struct Color operator * (const float_t &x) {
|
||||
return {
|
||||
r * x,
|
||||
g * x,
|
||||
b * x,
|
||||
a * x
|
||||
};
|
||||
}
|
||||
|
||||
struct Color operator - (const struct Color &color) {
|
||||
return {
|
||||
r - color.r,
|
||||
g - color.g,
|
||||
b - color.b,
|
||||
a - color.a
|
||||
};
|
||||
}
|
||||
|
||||
struct Color operator + (const struct Color &color) {
|
||||
return {
|
||||
r + color.r,
|
||||
g + color.g,
|
||||
b + color.b,
|
||||
a + color.a
|
||||
};
|
||||
}
|
||||
|
||||
const bool_t operator == (const struct Color &other) {
|
||||
return r == other.r && g == other.g && b == other.b && a == other.a;
|
||||
}
|
||||
|
||||
operator struct ColorU8() const {
|
||||
return {
|
||||
(uint8_t)(r * 255),
|
||||
(uint8_t)(g * 255),
|
||||
(uint8_t)(b * 255),
|
||||
(uint8_t)(a * 255)
|
||||
};
|
||||
}
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
#define COLOR_DEF(r,g,b,a) { r, g, b, a }
|
||||
#define COLOR_WHITE COLOR_DEF(1.0f, 1.0f, 1.0f, 1.0f)
|
||||
#define COLOR_RED COLOR_DEF(1.0f, 0, 0, 1.0f)
|
||||
#define COLOR_GREEN COLOR_DEF(0, 1.0f, 0, 1.0f)
|
||||
#define COLOR_BLUE COLOR_DEF(0, 0, 1.0f, 1.0f)
|
||||
#define COLOR_BLACK COLOR_DEF(0, 0, 0, 1.0f)
|
||||
#define COLOR_MAGENTA COLOR_DEF(1.0f, 0, 1.0f, 1.0f)
|
||||
#define COLOR_DARK_GREY COLOR_DEF(0.2f, 0.2f, 0.2f, 1.0f)
|
||||
#define COLOR_LIGHT_GREY COLOR_DEF(0.8f, 0.8f, 0.8f, 1.0f)
|
||||
#define COLOR_CORNFLOWER_BLUE COLOR_DEF(0.4f, 0.6f, 0.9f, 1.0f)
|
||||
#define COLOR_WHITE_TRANSPARENT COLOR_DEF(1.0f, 1.0f, 1.0f, 0.0f)
|
||||
#define COLOR_BLACK_TRANSPARENT COLOR_DEF(0.0f, 0.0f, 0.0f, 0.0f)
|
||||
#define COLOR_TRANSPARENT COLOR_WHITE_TRANSPARENT
|
||||
}
|
72
src/dawn/display/RenderTarget.hpp
Normal file
72
src/dawn/display/RenderTarget.hpp
Normal file
@ -0,0 +1,72 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
|
||||
enum RenderTargetClearFlag {
|
||||
COLOR = 1 << 0,
|
||||
DEPTH = 1 << 0
|
||||
};
|
||||
|
||||
namespace Dawn {
|
||||
class RenderTarget {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Return the width of the render target.
|
||||
*
|
||||
* @return The width of the render target.
|
||||
*/
|
||||
virtual float_t getWidth() = 0;
|
||||
|
||||
/**
|
||||
* Return the height of the render target.
|
||||
*
|
||||
* @return The height of the render target.
|
||||
*/
|
||||
virtual float_t getHeight() = 0;
|
||||
|
||||
/**
|
||||
* Returns the scale (as in pixel density) of the render target. This is
|
||||
* typically 1.0f, but on high DPI displays this may be 2.0f or higher.
|
||||
*
|
||||
* @return The scale of the render target.
|
||||
*/
|
||||
virtual float_t getScale() = 0;
|
||||
|
||||
/**
|
||||
* Sets the clear color of the render target when the clear method for
|
||||
* the color buffer is requested.
|
||||
*
|
||||
* @param color Color to use for the clear operation.
|
||||
*/
|
||||
virtual void setClearColor(const struct Color color) = 0;
|
||||
|
||||
/**
|
||||
* Request the existing data in the render target to be cleared out. We
|
||||
* typically assume the render target can support multiple buffer types,
|
||||
* so you can opt to only clear certain buffer types.
|
||||
*
|
||||
* @param clearFlags Flags to request what is going to be cleared.
|
||||
*/
|
||||
virtual void clear(const flag8_t clearFlags) = 0;
|
||||
|
||||
/**
|
||||
* Bind the render target for rendering to. The proceeding render requests
|
||||
* will want to render to this render target directly. In future I may
|
||||
* see if we can have multiple render targets bound at once to make this
|
||||
* operation perform faster.
|
||||
*/
|
||||
virtual void bind() = 0;
|
||||
|
||||
/**
|
||||
* Destroys the render target.
|
||||
*/
|
||||
virtual RenderTarget() {
|
||||
|
||||
}
|
||||
};
|
||||
}
|
@ -97,7 +97,6 @@ namespace Dawn {
|
||||
* Cleanup a previously initiated mesh.
|
||||
*/
|
||||
virtual ~IMesh() {
|
||||
this->disposeBuffers();
|
||||
}
|
||||
};
|
||||
}
|
@ -3,15 +3,58 @@
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "assert/assert.hpp"
|
||||
#include "util/Flag.hpp"
|
||||
#include "SceneComponent.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
void SceneComponent::init(const std::shared_ptr<SceneItem> item) {
|
||||
assertFlagOff(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_INIT,
|
||||
"SceneComponent is already initialized!"
|
||||
);
|
||||
Flag::turnOn<uint_fast8_t>(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_INIT
|
||||
);
|
||||
this->item = item;
|
||||
this->onInit();
|
||||
}
|
||||
|
||||
void SceneComponent::dispose() {
|
||||
assertFlagOn(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_INIT,
|
||||
"SceneComponent is not initialized!"
|
||||
);
|
||||
assertFlagOff(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_DISPOSED,
|
||||
"SceneComponent is already disposed!"
|
||||
);
|
||||
Flag::turnOn<uint_fast8_t>(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_DISPOSED
|
||||
);
|
||||
this->onDispose();
|
||||
this->item.reset();
|
||||
}
|
||||
|
||||
std::shared_ptr<SceneItem> SceneComponent::getItem() {
|
||||
return this->item.lock();
|
||||
}
|
||||
|
||||
SceneComponent::~SceneComponent() {
|
||||
if(Flag::isOn<uint_fast8_t>(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_INIT
|
||||
)) {
|
||||
assertFlagOn(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_DISPOSED,
|
||||
"SceneComponent is initialized but was not properly disposed!"
|
||||
);
|
||||
}
|
||||
}
|
@ -6,12 +6,16 @@
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
|
||||
#define SCENE_COMPONENT_STATE_INIT 0x01
|
||||
#define SCENE_COMPONENT_STATE_DISPOSED 0x02
|
||||
|
||||
namespace Dawn {
|
||||
class SceneItem;
|
||||
|
||||
class SceneComponent : std::enable_shared_from_this<SceneComponent> {
|
||||
private:
|
||||
std::weak_ptr<SceneItem> item;
|
||||
uint_fast8_t sceneComponentState = 0;
|
||||
|
||||
protected:
|
||||
/**
|
||||
@ -20,6 +24,12 @@ namespace Dawn {
|
||||
*/
|
||||
virtual void onInit() = 0;
|
||||
|
||||
/**
|
||||
* Custom component listener that is invoked when the component is meant
|
||||
* to dispose.
|
||||
*/
|
||||
virtual void onDispose() = 0;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Initializes this scene component.
|
||||
@ -28,11 +38,21 @@ namespace Dawn {
|
||||
*/
|
||||
void init(const std::shared_ptr<SceneItem> item);
|
||||
|
||||
/**
|
||||
* Disposes this scene component.
|
||||
*/
|
||||
void dispose();
|
||||
|
||||
/**
|
||||
* Returns the scene item that this scene component belongs to.
|
||||
*
|
||||
* @return Reference to the scene item that this component belongs to.
|
||||
*/
|
||||
std::shared_ptr<SceneItem> getItem();
|
||||
|
||||
/**
|
||||
* Disposes this scene component.
|
||||
*/
|
||||
virtual ~SceneComponent();
|
||||
};
|
||||
}
|
@ -24,5 +24,11 @@ std::shared_ptr<Scene> SceneItem::getScene() {
|
||||
}
|
||||
|
||||
SceneItem::~SceneItem() {
|
||||
|
||||
std::for_each(
|
||||
components.begin(),
|
||||
components.end(),
|
||||
[](auto &component) {
|
||||
component->dispose();
|
||||
}
|
||||
);
|
||||
}
|
@ -16,6 +16,7 @@ void SceneItemComponents::removeComponent(
|
||||
) {
|
||||
auto it = std::find(components.begin(), components.end(), component);
|
||||
if(it == components.end()) return; //Not found?
|
||||
it->get()->dispose();
|
||||
components.erase(it);
|
||||
}
|
||||
|
||||
|
32
src/dawn/util/Flag.hpp
Normal file
32
src/dawn/util/Flag.hpp
Normal file
@ -0,0 +1,32 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class Flag final {
|
||||
public:
|
||||
template<typename T>
|
||||
static void turnOn(T &flag, const T check) {
|
||||
flag |= check;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static void turnOff(T &flag, const T check) {
|
||||
flag &= ~check;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static bool_t isOn(const T flag, const T check) {
|
||||
return (flag & check) == check;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static bool_t isOff(const T flag, const T check) {
|
||||
return (flag & check) == 0;
|
||||
}
|
||||
};
|
||||
}
|
@ -11,7 +11,7 @@
|
||||
#define MATH_PI 3.1415926535897f
|
||||
|
||||
namespace Dawn {
|
||||
class Math {
|
||||
class Math final {
|
||||
/**
|
||||
* Returns the largest of the two provided int32 numbers.
|
||||
*
|
||||
@ -32,7 +32,7 @@ namespace Dawn {
|
||||
* @return Smaller of the two numbers.
|
||||
*/
|
||||
template<typename T>
|
||||
static T mathMin(T left, T right) {
|
||||
static T min(T left, T right) {
|
||||
return left < right ? left : right;
|
||||
}
|
||||
|
||||
@ -46,7 +46,7 @@ namespace Dawn {
|
||||
* @return The value, or the closest clamped value.
|
||||
*/
|
||||
template<typename T>
|
||||
static T mathClamp(T val, T min, T max) {
|
||||
static T clamp(T val, T min, T max) {
|
||||
return mathMin<T>(mathMax<T>(val, min), max);
|
||||
}
|
||||
|
||||
@ -58,7 +58,7 @@ namespace Dawn {
|
||||
* @return The absolute value (-value if value < 0)
|
||||
*/
|
||||
template<typename T>
|
||||
static T mathAbs(T value) {
|
||||
static T abs(T value) {
|
||||
return value < 0 ? -value : value;
|
||||
}
|
||||
|
||||
@ -70,11 +70,11 @@ namespace Dawn {
|
||||
* @returns The modulo result.
|
||||
*/
|
||||
template<typename T>
|
||||
static inline T mathMod(T value, T modulo) {
|
||||
static inline T mod(T value, T modulo) {
|
||||
return ((value % modulo) + modulo) % modulo;
|
||||
}
|
||||
|
||||
static inline float_t mathMod(float_t value, float_t modulo) {
|
||||
static inline float_t fmod(float_t value, float_t modulo) {
|
||||
float_t n = fmod(value, modulo);
|
||||
return n;
|
||||
}
|
||||
@ -85,7 +85,7 @@ namespace Dawn {
|
||||
* @param n Degrees to convert.
|
||||
* @returns The number in radians.
|
||||
*/
|
||||
static float_t mathDeg2Rad(float_t degrees) {
|
||||
static float_t deg2rad(float_t degrees) {
|
||||
return degrees * (MATH_PI / 180.0f);
|
||||
}
|
||||
|
||||
@ -94,7 +94,7 @@ namespace Dawn {
|
||||
* @param n Radians to convert.
|
||||
* @returns The number in degrees.
|
||||
*/
|
||||
static float_t mathRad2Deg(float_t n) {
|
||||
static float_t rad2deg(float_t n) {
|
||||
return (n * 180.0f) / MATH_PI;
|
||||
}
|
||||
|
||||
@ -104,7 +104,7 @@ namespace Dawn {
|
||||
* @return Rounded number.
|
||||
*/
|
||||
template<typename T>
|
||||
static T mathRound(float_t n) {
|
||||
static T round(float_t n) {
|
||||
return (T)roundf(n);
|
||||
}
|
||||
|
||||
@ -114,7 +114,7 @@ namespace Dawn {
|
||||
* @return Rounded number.
|
||||
*/
|
||||
template<typename T>
|
||||
static T mathFloor(float_t n) {
|
||||
static T floor(float_t n) {
|
||||
return (T)floorf(n);
|
||||
}
|
||||
};
|
||||
|
@ -4,6 +4,7 @@
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnopengl.hpp"
|
||||
#include "display/mesh/IMesh.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
@ -44,5 +45,7 @@ namespace Dawn {
|
||||
const int32_t start,
|
||||
const int32_t count
|
||||
) override;
|
||||
|
||||
~Mesh();
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user