Working on RenderTarget example

This commit is contained in:
2022-10-19 00:43:16 -07:00
parent a86574128d
commit f0cbae4cf8
17 changed files with 235 additions and 26 deletions

View File

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

View File

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

View File

@ -4,35 +4,31 @@
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
#include "RenderTarget.hpp"
namespace Dawn {
class DawnGame;
class RenderManager {
class IRenderManager {
public:
std::weak_ptr<DawnGame> game;
/**
* Construct a new RenderManager for a game instance.
* Returns the primary render target (the backbuffer) that draws directly
* to the screen.
*
* @param game Game instance this render manager belongs to.
* @return Shared pointer to the backbuffer render target.
*/
RenderManager(std::weak_ptr<DawnGame> game);
virtual std::shared_ptr<RenderTarget> getBackBuffer() = 0;
/**
* Initialize this render manager.
*/
void init();
virtual void init() = 0;
/**
* Perform a synchronous frame update on the render manager.
*/
void update();
/**
* Destroy a previously initialized RenderManager.
*/
~RenderManager();
virtual void update() = 0;
};
}

View File

@ -20,9 +20,9 @@ namespace Dawn {
{
private:
std::shared_ptr<Scene> scene;
std::weak_ptr<DawnHost> host;
public:
std::weak_ptr<DawnHost> host;
RenderManager renderManager;
/**

View File

@ -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<SceneItem> item) {
this->item = item;
}
std::shared_ptr<Scene> SceneItemComponent::getScene() {
return this->item.lock()->scene.lock();
}
std::shared_ptr<DawnGame> SceneItemComponent::getGame() {
return this->item.lock()->scene.lock()->game.lock();
}
SceneItemComponent::~SceneItemComponent() {
}

View File

@ -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<Scene> getScene();
std::shared_ptr<DawnGame> getGame();
virtual ~SceneItemComponent();
};
}

View File

@ -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<RenderTarget> 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() {

View File

@ -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<RenderTarget> target = nullptr;
// Perspective
enum CameraType type = CAMERA_TYPE_PERSPECTIVE;
@ -42,6 +44,8 @@ namespace Dawn {
*/
void updateProjection();
std::shared_ptr<RenderTarget> getRenderTarget();
/**
* Returs the aspect ratio of the camera.
*

11
src/dawn/util/flag.hpp Normal file
View File

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