Base refactor

This commit is contained in:
2023-11-14 09:16:48 -06:00
parent a10c0ea851
commit cda4576a05
410 changed files with 749 additions and 20823 deletions

View File

@ -1,28 +1,28 @@
# Copyright (c) 2022 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Libs
target_link_libraries(${DAWN_TARGET_NAME}
PUBLIC
glfw
glad
)
# Platform variables
target_compile_definitions(${DAWN_TARGET_NAME}
PUBLIC
DAWN_OPENGL_GLSL=true
)
# Includes
target_include_directories(${DAWN_TARGET_NAME}
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
# Subdirs
add_subdirectory(host)
add_subdirectory(input)
add_subdirectory(time)
# Copyright (c) 2022 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Libs
target_link_libraries(${DAWN_TARGET_NAME}
PUBLIC
glfw
glad
)
# Platform variables
# target_compile_definitions(${DAWN_TARGET_NAME}
# PUBLIC
# DAWN_OPENGL_GLSL=true
# )
# Includes
target_include_directories(${DAWN_TARGET_NAME}
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
# Subdirs
add_subdirectory(display)
add_subdirectory(input)
# add_subdirectory(time)

View File

@ -6,5 +6,5 @@
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
TimeManager.cpp
RenderHost.cpp
)

View File

@ -0,0 +1,95 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "RenderHost.hpp"
#include "assert/assertgl.hpp"
#include "assert/assert.hpp"
using namespace Dawn;
RenderHost::RenderHost() {
}
void RenderHost::init() {
// Init GLFW
if(!glfwInit()) {
throw std::runtime_error("Failed to initialize GLFW!");
}
// Set the error callback for error handling.
glfwSetErrorCallback([](int error, const char *description) {
throw std::runtime_error(description);
});
// Setup window hints
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, false);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
// Create the window
window = glfwCreateWindow(
DAWN_GLFW_WINDOW_WIDTH_DEFAULT,
DAWN_GLFW_WINDOW_HEIGHT_DEFAULT,
"Dawn",
NULL,
NULL
);
// Validate window exists
if(!window) {
throw std::runtime_error("Failed to create GLFW window!");
}
// Load GLAD
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
assertNoGLError();
// Get the resolution and scale/dpi
int32_t fbWidth, fbHeight;
int32_t windowWidth, windowHeight;
glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
glfwGetWindowSize(window, &windowWidth, &windowHeight);
assertTrue(fbWidth > 0, "Detected framebuffer width is too small?");
assertTrue(fbWidth > 0, "Detected framebuffer height is too small?");
assertTrue(windowWidth > 0, "Detected window width is too small?");
assertTrue(windowHeight > 0, "Detected window height is too small?");
// Framebuffer callback
// glfwSetFramebufferSizeCallback(window, [&](
// GLFWwindow *window,
// int32_t width,
// int32_t height
// ) {
// if(this->window == nullptr || window != this->window) return;
// std::cout << "Resize" << std::endl;
// });
}
void RenderHost::update() {
// Tick the engine.
glfwSwapBuffers(window);
// Update events
glfwPollEvents();
}
bool_t RenderHost::isCloseRequested() {
if(this->window == nullptr) return false;
return glfwWindowShouldClose(this->window);
}
RenderHost::~RenderHost() {
if(this->window != nullptr) {
glfwDestroyWindow(this->window);
this->window = nullptr;
}
glfwTerminate();
}

View File

@ -0,0 +1,46 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#define DAWN_GLFW_WINDOW_WIDTH_DEFAULT 1280
#define DAWN_GLFW_WINDOW_HEIGHT_DEFAULT 720
namespace Dawn {
class RenderHost {
public:
GLFWwindow *window = nullptr;
/**
* Initializes the GLFW RenderHost.
*/
RenderHost();
/**
* Initializes GLFW and creates the window.
*/
void init();
/**
* Performs an update and renders the frame.
*/
void update();
/**
* Returns whether or not GLFW has been requested to close.
*
* @return True if GLFW has been requested to close.
*/
bool_t isCloseRequested();
/**
* Cleans up GLFW.
*/
virtual ~RenderHost();
};
}

View File

@ -1,10 +0,0 @@
# 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
DawnGLFWHost.cpp
)

View File

@ -1,244 +0,0 @@
/**
* Copyright (c) 2022 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "DawnGLFWHost.hpp"
#include "game/DawnGame.hpp"
#include "dawnopengl.hpp"
#include "display/BackBufferRenderTarget.hpp"
using namespace Dawn;
// Static declaration of the host, needed due to GLFW events being C-like
std::weak_ptr<DawnHost> DAWN_HOST;
// Host
DawnHost::DawnHost() {
this->data = std::make_shared<DawnHostData>();
this->data->window = nullptr;
}
int32_t DawnHost::init(std::shared_ptr<DawnGame> game) {
// Update values
DAWN_HOST = weak_from_this();
this->game = game;
// Init GLFW
if(!glfwInit()) return DAWN_GLFW_INIT_RESULT_INIT_FAILED;
glfwSetErrorCallback(&glfwOnError);
// Setup window hints
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, false);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
// Create Window
this->data->window = glfwCreateWindow(
DAWN_GLFW_WINDOW_WIDTH_DEFAULT,
DAWN_GLFW_WINDOW_HEIGHT_DEFAULT,
"Dawn", NULL, NULL
);
if(this->data->window == nullptr) {
glfwTerminate();
return DAWN_GLFW_INIT_RESULT_WINDOW_CREATE_FAILED;
}
// Load GLAD
glfwMakeContextCurrent(this->data->window);
glfwSwapInterval(1);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
assertNoGLError();
// Override the defaults
int32_t fbWidth, fbHeight;
int32_t windowWidth, windowHeight;
glfwGetFramebufferSize(this->data->window, &fbWidth, &fbHeight);
glfwGetWindowSize(this->data->window, &windowWidth, &windowHeight);
assertTrue(fbWidth > 0, "Detected framebuffer width is too small?");
assertTrue(fbWidth > 0, "Detected framebuffer height is too small?");
assertTrue(windowWidth > 0, "Detected window width is too small?");
assertTrue(windowHeight > 0, "Detected window height is too small?");
game->renderManager->backBuffer->setSize(fbWidth, fbHeight);
game->renderManager->backBuffer->scale = ((float_t)fbWidth) / ((float_t)windowWidth);
assertTrue(game->renderManager->backBuffer->scale > 0, "Back buffer scale is invalid");
assertNoGLError();
// Default keybinds
game->inputManager.bind(INPUT_BIND_ACCEPT, GLFW_KEY_ENTER);
game->inputManager.bind(INPUT_BIND_ACCEPT, GLFW_KEY_E);
game->inputManager.bind(INPUT_BIND_ACCEPT, GLFW_KEY_SPACE);
game->inputManager.bind(INPUT_BIND_POSITIVE_X, GLFW_KEY_D);
game->inputManager.bind(INPUT_BIND_NEGATIVE_X, GLFW_KEY_A);
game->inputManager.bind(INPUT_BIND_POSITIVE_Y, GLFW_KEY_S);
game->inputManager.bind(INPUT_BIND_NEGATIVE_Y, GLFW_KEY_W);
game->inputManager.bind(INPUT_BIND_CANCEL, GLFW_KEY_ESCAPE);
game->inputManager.bind(INPUT_BIND_CANCEL, GLFW_KEY_Q);
game->inputManager.bind(INPUT_BIND_MOUSE_CLICK, INPUT_MANAGER_AXIS_MOUSE_0);
game->inputManager.bind(INPUT_BIND_MOUSE_X, INPUT_MANAGER_AXIS_MOUSE_X);
game->inputManager.bind(INPUT_BIND_MOUSE_Y, INPUT_MANAGER_AXIS_MOUSE_Y);
// Initialize the game
auto result = game->init();
if(result != DAWN_GAME_INIT_RESULT_SUCCESS) return result;
// Hard-Load the first scene assets.
if(game->scene != nullptr) {
auto assets = game->scene->getRequiredAssets();
game->assetManager.queueLoad(assets);
game->assetManager.syncLoad();
game->scene->stage();
}
// Set up event listeners
glfwSetFramebufferSizeCallback(this->data->window, &glfwOnResize);
glfwSetKeyCallback(this->data->window, &glfwOnKey);
glfwSetCursorPosCallback(this->data->window, &glfwOnCursor);
glfwSetMouseButtonCallback(this->data->window, &glfwOnMouseButton);
return DAWN_HOST_INIT_RESULT_SUCCESS;
}
int32_t DawnHost::start(std::shared_ptr<DawnGame> game) {
double_t time, newTime;
float_t fDelta;
int32_t updateResult;
// Main Render Loop
time = 0.0f;
while(!glfwWindowShouldClose(this->data->window)) {
// Determine the delta.
newTime = glfwGetTime();
fDelta = (float_t)(newTime - time);
time = newTime;
// Perform update
updateResult = this->update(game, fDelta);
// Did the update complete successfully?
if(updateResult == DAWN_HOST_UPDATE_RESULT_EXIT) {
break;
} else if(updateResult != DAWN_HOST_UPDATE_RESULT_SUCCESS) {
return DAWN_GLFW_START_RESULT_UPDATE_FAILED;
}
// Tick the engine.
glfwSwapBuffers(this->data->window);
// Update events
glfwPollEvents();
}
return DAWN_HOST_START_RESULT_EXIT_SUCCESS;
}
int32_t DawnHost::update(std::shared_ptr<DawnGame> game, float_t delta) {
// Tick game.
auto ret = game->update(delta);
switch(ret) {
case DAWN_GAME_UPDATE_RESULT_SUCCESS:
return DAWN_HOST_UPDATE_RESULT_SUCCESS;
case DAWN_GAME_UPDATE_RESULT_EXIT:
return DAWN_HOST_UPDATE_RESULT_EXIT;
default:
return ret;
}
}
void DawnHost::unload(std::shared_ptr<DawnGame> game) {
assertNotNull(game, "DawnHost::unload: game must not be null");
assertNotNull(this->data, "DawnHost::unload: data must not be null");
if(this->data->window != nullptr) {
glfwDestroyWindow(this->data->window);
this->data->window = nullptr;
glfwTerminate();
}
}
DawnHost::~DawnHost() {
assertNotNull(this->data, "DawnHost::~DawnHost: data must not be null");
if(this->data->window != nullptr) {
glfwDestroyWindow(this->data->window);
this->data->window = nullptr;
glfwTerminate();
}
}
// GLFW Callbacks
void glfwOnError(int error, const char* description) {
fputs(description, stderr);
}
void glfwOnResize(GLFWwindow *window, int32_t w, int32_t h) {
auto host = DAWN_HOST.lock();
if(!host) return;
assertTrue(window == host->data->window, "glfwOnResize: Window mismatch");
auto bb = host->game->renderManager->backBuffer;
assertNotNull(bb, "glfwOnResize: Back buffer is not valid");
int32_t windowWidth, windowHeight;
glfwGetWindowSize(window, &windowWidth, &windowHeight);
// TODO: I may throttle this, it calls for every frame the window's resized.
bb->setSize((float_t)w, (float_t)h);
bb->scale = ((float_t)w) / ((float_t)windowWidth);
assertTrue(bb->scale > 0, "glfwOnResize: Back buffer scale is invalid");
}
void glfwOnKey(
GLFWwindow *window,
int32_t key,
int32_t scancode,
int32_t action,
int32_t mods
) {
auto host = DAWN_HOST.lock();
if(host == nullptr) return;
// Determine Value
float_t value;
if(action == GLFW_PRESS) {
value = 1.0f;
} else if(action == GLFW_RELEASE) {
value = 0.0f;
} else {
return;
}
// Determine the input axis
host->game->inputManager.rawInputValues[key] = value;
}
void glfwOnCursor(GLFWwindow* window, double xpos, double ypos) {
auto host = DAWN_HOST.lock();
if(host == nullptr) return;
host->game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_X] = mathClamp<float_t>(
(float_t)(xpos / host->game->renderManager->backBuffer->getWidth()),
0, 1
);
host->game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_Y] = mathClamp<float_t>(
(float_t)(ypos / host->game->renderManager->backBuffer->getHeight()),
0, 1
);
}
void glfwOnMouseButton(GLFWwindow* window, int button, int action, int mods) {
auto host = DAWN_HOST.lock();
if(host == nullptr) return;
assertTrue(window == host->data->window, "glfwOnMouseButton: Window mismatch");
float_t value = action == GLFW_PRESS ? 1 : 0;
host->game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_0 + button] = value;
}

View File

@ -1,35 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "host/DawnHost.hpp"
#define DAWN_GLFW_WINDOW_WIDTH_DEFAULT 1280
#define DAWN_GLFW_WINDOW_HEIGHT_DEFAULT 720
#define DAWN_GLFW_INIT_RESULT_INIT_FAILED -1
#define DAWN_GLFW_INIT_RESULT_WINDOW_CREATE_FAILED -2
#define DAWN_GLFW_START_RESULT_UPDATE_FAILED -1
namespace Dawn {
class DawnHostData {
public:
GLFWwindow *window;
};
}
// GLFW Callbacks
void glfwOnError(int error, const char* description);
void glfwOnResize(GLFWwindow *window, int32_t width, int32_t height);
void glfwOnKey(
GLFWwindow *window,
int32_t key, int32_t scancode, int32_t action, int32_t mods
);
void glfwOnCursor(GLFWwindow* window, double xpos, double ypos);
void glfwOnMouseButton(GLFWwindow* window, int button, int action, int mods);

View File

@ -1,9 +1,8 @@
# Copyright (c) 2022 Dominic Masters
# Copyright (c) 2023 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
InputManager.cpp

View File

@ -1,16 +1,21 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "InputManager.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
float_t InputManager::getInputValue(int32_t axis) {
auto exist = this->rawInputValues.find(axis);
if(exist == this->rawInputValues.end()) return 0.0f;
return exist->second;
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "InputManager.hpp"
#include "game/Game.hpp"
using namespace Dawn;
void InputManager::init(const std::shared_ptr<Game> game) {
}
float_t InputManager::getInputValue(int32_t axis) {
return 0.0f;
}
InputManager::~InputManager() {
// Nothing to do here.
}

View File

@ -1,21 +1,31 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "input/_InputManager.hpp"
#define INPUT_MANAGER_AXIS_MOUSE_X -580000
#define INPUT_MANAGER_AXIS_MOUSE_Y -580001
#define INPUT_MANAGER_AXIS_MOUSE_0 -590000
namespace Dawn {
class InputManager : public IInputManager<int32_t> {
protected:
float_t getInputValue(int32_t axis) override;
public:
std::map<int32_t, float_t> rawInputValues;
};
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "input/IInputManager.hpp"
namespace Dawn {
class Game;
class InputManager : public IInputManager<int32_t> {
protected:
std::unordered_map<int32_t, float_t> rawInputValues;
float_t getInputValue(int32_t axis) override;
public:
/**
* Initializes the input manager system.
*
* @param game Game that this input manager is for.
*/
void init(const std::shared_ptr<Game> game);
/**
* Cleans up the GLFW input manager.
*/
~InputManager();
};
}

View File

@ -1,16 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "TimeManager.hpp"
using namespace Dawn;
TimeManager::TimeManager() : ITimeManager() {
}
int64_t TimeManager::getTimestamp() {
return (int64_t)std::time(nullptr);
}

View File

@ -1,15 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "time/ITimeManager.hpp"
namespace Dawn {
class TimeManager : public ITimeManager {
public:
TimeManager();
int64_t getTimestamp() override;
};
}