diff --git a/src/dawn/display/Color.hpp b/src/dawn/display/Color.hpp new file mode 100644 index 00000000..d3b35051 --- /dev/null +++ b/src/dawn/display/Color.hpp @@ -0,0 +1,24 @@ +// 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 Color { + float_t r, g, b, a; + }; + + #define COLOR_WHITE { 1, 1, 1, 1 } + #define COLOR_RED { 1, 0, 0, 1 } + #define COLOR_GREEN { 0, 255, 0, 1 } + #define COLOR_BLUE { 0, 0, 255, 1 } + #define COLOR_BLACK { 0, 0, 0, 1 } + #define COLOR_MAGENTA { 1, 0, 1, 1 } + #define COLOR_CORNFLOWER_BLUE { 0.39215686274f, 0.58431372549f, 0.9294117647f, 1.0f } + #define COLOR_WHITE_TRANSPARENT { 1, 1, 1, 0 } + #define COLOR_BLACK_TRANSPARENT { 0, 0, 0, 0 } + #define COLOR_TRANSPARENT COLOR_BLACK_TRANSPARENT +} \ No newline at end of file diff --git a/src/dawn/display/RenderTarget.hpp b/src/dawn/display/RenderTarget.hpp new file mode 100644 index 00000000..dfc5f5b2 --- /dev/null +++ b/src/dawn/display/RenderTarget.hpp @@ -0,0 +1,22 @@ +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "util/flag.hpp" +#include "display/Color.hpp" + +#define RENDER_TARGET_CLEAR_FLAG_COLOR FLAG_DEFINE(0) +#define RENDER_TARGET_CLEAR_FLAG_DEPTH FLAG_DEFINE(1) + +namespace Dawn { + class RenderTarget { + public: + virtual float_t getWidth() = 0; + virtual float_t getHeight() = 0; + virtual void setClearColor(struct Color color) = 0; + virtual void clear(flag8_t clearFlags) = 0; + virtual void bind() = 0; + }; +} \ No newline at end of file diff --git a/src/dawn/display/_RenderManager.hpp b/src/dawn/display/_RenderManager.hpp new file mode 100644 index 00000000..8b95a66a --- /dev/null +++ b/src/dawn/display/_RenderManager.hpp @@ -0,0 +1,34 @@ +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "RenderTarget.hpp" + +namespace Dawn { + class DawnGame; + + class IRenderManager { + public: + std::weak_ptr game; + + /** + * Returns the primary render target (the backbuffer) that draws directly + * to the screen. + * + * @return Shared pointer to the backbuffer render target. + */ + virtual std::shared_ptr getBackBuffer() = 0; + + /** + * Initialize this render manager. + */ + virtual void init() = 0; + + /** + * Perform a synchronous frame update on the render manager. + */ + virtual void update() = 0; + }; +} \ No newline at end of file diff --git a/src/dawn/game/DawnGame.hpp b/src/dawn/game/DawnGame.hpp index e19145a6..5f901a23 100644 --- a/src/dawn/game/DawnGame.hpp +++ b/src/dawn/game/DawnGame.hpp @@ -20,9 +20,9 @@ namespace Dawn { { private: std::shared_ptr scene; - std::weak_ptr host; public: + std::weak_ptr host; RenderManager renderManager; /** diff --git a/src/dawn/scene/SceneItemComponent.cpp b/src/dawn/scene/SceneItemComponent.cpp index 3c161eea..d8cf952d 100644 --- a/src/dawn/scene/SceneItemComponent.cpp +++ b/src/dawn/scene/SceneItemComponent.cpp @@ -5,6 +5,8 @@ #include "SceneItemComponent.hpp" #include "SceneItem.hpp" +#include "Scene.hpp" +#include "game/DawnGame.hpp" using namespace Dawn; @@ -12,6 +14,14 @@ SceneItemComponent::SceneItemComponent(std::weak_ptr item) { this->item = item; } +std::shared_ptr SceneItemComponent::getScene() { + return this->item.lock()->scene.lock(); +} + +std::shared_ptr SceneItemComponent::getGame() { + return this->item.lock()->scene.lock()->game.lock(); +} + SceneItemComponent::~SceneItemComponent() { } \ No newline at end of file diff --git a/src/dawn/scene/SceneItemComponent.hpp b/src/dawn/scene/SceneItemComponent.hpp index 0dd25a52..c7587a97 100644 --- a/src/dawn/scene/SceneItemComponent.hpp +++ b/src/dawn/scene/SceneItemComponent.hpp @@ -8,6 +8,8 @@ namespace Dawn { class SceneItem; + class Scene; + class DawnGame; class SceneItemComponent { public: @@ -17,6 +19,9 @@ namespace Dawn { virtual void start() = 0; + std::shared_ptr getScene(); + std::shared_ptr getGame(); + virtual ~SceneItemComponent(); }; } \ No newline at end of file diff --git a/src/dawn/scene/components/display/Camera.cpp b/src/dawn/scene/components/display/Camera.cpp index 63f90749..3fa5684c 100644 --- a/src/dawn/scene/components/display/Camera.cpp +++ b/src/dawn/scene/components/display/Camera.cpp @@ -4,6 +4,8 @@ // https://opensource.org/licenses/MIT #include "Camera.hpp" +#include "scene/Scene.hpp" +#include "game/DawnGame.hpp" using namespace Dawn; @@ -34,8 +36,17 @@ void Camera::updateProjection() { } } +std::shared_ptr Camera::getRenderTarget() { + if(this->target == nullptr) { + return this->getGame()->renderManager.getBackBuffer(); + } + + return this->target; +} + float_t Camera::getAspect() { - return 16.0f / 9.0f; + auto target = this->getRenderTarget(); + return target->getWidth() / target->getHeight(); } void Camera::start() { diff --git a/src/dawn/scene/components/display/Camera.hpp b/src/dawn/scene/components/display/Camera.hpp index 4cb028f5..0ffad9e9 100644 --- a/src/dawn/scene/components/display/Camera.hpp +++ b/src/dawn/scene/components/display/Camera.hpp @@ -5,6 +5,7 @@ #pragma once #include "scene/SceneItemComponent.hpp" +#include "display/RenderTarget.hpp" namespace Dawn { enum CameraType { @@ -15,6 +16,7 @@ namespace Dawn { class Camera : public SceneItemComponent { public: glm::mat4 projection; + std::shared_ptr target = nullptr; // Perspective enum CameraType type = CAMERA_TYPE_PERSPECTIVE; @@ -42,6 +44,8 @@ namespace Dawn { */ void updateProjection(); + std::shared_ptr getRenderTarget(); + /** * Returs the aspect ratio of the camera. * diff --git a/src/dawn/util/flag.hpp b/src/dawn/util/flag.hpp new file mode 100644 index 00000000..09045b3c --- /dev/null +++ b/src/dawn/util/flag.hpp @@ -0,0 +1,11 @@ +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "dawnlibs.hpp" + +typedef uint_fast8_t flag8_t; + +#define FLAG_DEFINE(n) (1 << n) \ No newline at end of file diff --git a/src/dawnglfw/host/DawnGLFWHost.cpp b/src/dawnglfw/host/DawnGLFWHost.cpp index 2dcefe19..1ad63329 100644 --- a/src/dawnglfw/host/DawnGLFWHost.cpp +++ b/src/dawnglfw/host/DawnGLFWHost.cpp @@ -8,6 +8,7 @@ #include "DawnGLFWHost.hpp" #include "game/DawnGame.hpp" #include "dawnopengl.hpp" +#include "display/BackBufferRenderTarget.hpp" using namespace Dawn; @@ -53,6 +54,11 @@ int32_t DawnHost::init(std::weak_ptr game) { if(auto g = game.lock()) { auto result = g->init(); if(result != DAWN_GAME_INIT_RESULT_SUCCESS) return result; + + g->renderManager.renderTarget->setSize( + DAWN_GLFW_WINDOW_WIDTH_DEFAULT, + DAWN_GLFW_WINDOW_HEIGHT_DEFAULT + ); } else { return DAWN_GLFW_INIT_RESULT_GAME_LOCK_FAILED; } @@ -107,6 +113,8 @@ int32_t DawnHost::update(std::weak_ptr game, float_t delta) { return ret; } } + + return DAWN_GLFW_UPDATE_RESULT_GAME_LOCK_FAILED; } void DawnHost::unload(std::weak_ptr game) { diff --git a/src/dawnglfw/host/DawnGLFWHost.hpp b/src/dawnglfw/host/DawnGLFWHost.hpp index 722eb14b..cc8f2602 100644 --- a/src/dawnglfw/host/DawnGLFWHost.hpp +++ b/src/dawnglfw/host/DawnGLFWHost.hpp @@ -18,6 +18,8 @@ #define DAWN_GLFW_START_RESULT_UPDATE_FAILED -1 +#define DAWN_GLFW_UPDATE_RESULT_GAME_LOCK_FAILED -1 + namespace Dawn { class DawnHostData { public: diff --git a/src/dawnopengl/display/BackBufferRenderTarget.cpp b/src/dawnopengl/display/BackBufferRenderTarget.cpp new file mode 100644 index 00000000..6fb5b4be --- /dev/null +++ b/src/dawnopengl/display/BackBufferRenderTarget.cpp @@ -0,0 +1,46 @@ +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "BackBufferRenderTarget.hpp" + +using namespace Dawn; + +BackBufferRenderTarget::BackBufferRenderTarget( + std::weak_ptr renderManager +) { + this->renderManager = renderManager; +} + +float_t BackBufferRenderTarget::getWidth() { + return this->width; +} + +float_t BackBufferRenderTarget::getHeight() { + return this->height; +} + +void BackBufferRenderTarget::setSize(float_t width, float_t height) { + this->width = width; + this->height = height; +} + +void BackBufferRenderTarget::setClearColor(struct Color color) { + this->clearColor = color; +} + +void BackBufferRenderTarget::clear(flag8_t clearFlags) { + glClearColor( + this->clearColor.r, + this->clearColor.g, + this->clearColor.b, + this->clearColor.a + ); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +} + +void BackBufferRenderTarget::bind() { + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glViewport(0, 0, (GLsizei)this->width, (GLsizei)this->height); +} \ No newline at end of file diff --git a/src/dawnopengl/display/BackBufferRenderTarget.hpp b/src/dawnopengl/display/BackBufferRenderTarget.hpp new file mode 100644 index 00000000..b0c23cef --- /dev/null +++ b/src/dawnopengl/display/BackBufferRenderTarget.hpp @@ -0,0 +1,32 @@ +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "dawnopengl.hpp" +#include "display/RenderTarget.hpp" +#include "display/RenderManager.hpp" + +namespace Dawn { + class BackBufferRenderTarget : public RenderTarget { + private: + std::weak_ptr renderManager; + float_t width = 1; + float_t height = 1; + struct Color clearColor = COLOR_CORNFLOWER_BLUE; + + public: + BackBufferRenderTarget(std::weak_ptr renderManager); + + float_t getWidth() override; + float_t getHeight() override; + + void setSize(float_t width, float_t height); + + void setClearColor(struct Color color); + + void clear(flag8_t clearFlags) override; + void bind() override; + }; +} \ No newline at end of file diff --git a/src/dawnopengl/display/CMakeLists.txt b/src/dawnopengl/display/CMakeLists.txt index 9cf9350c..6b7df8f7 100644 --- a/src/dawnopengl/display/CMakeLists.txt +++ b/src/dawnopengl/display/CMakeLists.txt @@ -7,6 +7,7 @@ target_sources(${DAWN_TARGET_NAME} PRIVATE RenderManager.cpp + BackBufferRenderTarget.cpp ) # Subdirs diff --git a/src/dawnopengl/display/RenderManager.cpp b/src/dawnopengl/display/RenderManager.cpp index 6ef7aa1a..60a98c36 100644 --- a/src/dawnopengl/display/RenderManager.cpp +++ b/src/dawnopengl/display/RenderManager.cpp @@ -6,26 +6,28 @@ #include "dawnopengl.hpp" #include "game/DawnGame.hpp" #include "display/RenderManager.hpp" +#include "display/BackBufferRenderTarget.hpp" using namespace Dawn; RenderManager::RenderManager(std::weak_ptr game) { this->game = game; + this->renderTarget=std::make_shared(weak_from_this()); } void RenderManager::init() { - glClearColor( - 0.39215686274f, - 0.58431372549f, - 0.9294117647f, - 1.0f - ); +} - glViewport(0, 0, 1280, 720); +std::shared_ptr RenderManager::getBackBuffer() { + return this->renderTarget; } void RenderManager::update() { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + this->getBackBuffer()->bind(); + this->getBackBuffer()->clear( + RENDER_TARGET_CLEAR_FLAG_COLOR | + RENDER_TARGET_CLEAR_FLAG_DEPTH + ); } RenderManager::~RenderManager() { diff --git a/src/dawn/display/RenderManager.hpp b/src/dawnopengl/display/RenderManager.hpp similarity index 57% rename from src/dawn/display/RenderManager.hpp rename to src/dawnopengl/display/RenderManager.hpp index 1a9a85dc..61e41772 100644 --- a/src/dawn/display/RenderManager.hpp +++ b/src/dawnopengl/display/RenderManager.hpp @@ -4,15 +4,19 @@ // https://opensource.org/licenses/MIT #pragma once -#include "dawnlibs.hpp" +#include "display/_RenderManager.hpp" namespace Dawn { - class DawnGame; + class BackBufferRenderTarget; + + class RenderManager : + public IRenderManager, + public std::enable_shared_from_this + { - class RenderManager { public: - std::weak_ptr game; - + std::shared_ptr renderTarget; + /** * Construct a new RenderManager for a game instance. * @@ -20,16 +24,10 @@ namespace Dawn { */ RenderManager(std::weak_ptr game); - /** - * Initialize this render manager. - */ - void init(); - - /** - * Perform a synchronous frame update on the render manager. - */ - void update(); - + std::shared_ptr getBackBuffer() override; + void init() override; + void update() override; + /** * Destroy a previously initialized RenderManager. */ diff --git a/src/dawnpokergame/game/DawnPokerGame.cpp b/src/dawnpokergame/game/DawnPokerGame.cpp index 575e503a..0bcf3121 100644 --- a/src/dawnpokergame/game/DawnPokerGame.cpp +++ b/src/dawnpokergame/game/DawnPokerGame.cpp @@ -35,7 +35,6 @@ int32_t DawnGame::update(float_t delta) { auto meshRenderer = this->scene->findComponent(); if(meshRenderer != nullptr) { - std::cout << "REndering" << std::endl; meshRenderer->mesh->draw(MESH_DRAW_MODE_TRIANGLES, 0, -1); }