Converted a couple more things to shared pointers.

This commit is contained in:
2023-11-10 20:12:18 -06:00
parent 0beb1d9cb7
commit 732a90931c
16 changed files with 114 additions and 79 deletions

View File

@ -13,18 +13,18 @@
using namespace Dawn;
// Static declaration of the host, needed due to GLFW events being C-like
DawnHost *DAWN_HOST = nullptr;
std::weak_ptr<DawnHost> DAWN_HOST;
// Host
DawnHost::DawnHost() {
this->data = new DawnHostData();
this->data = std::make_shared<DawnHostData>();
this->data->window = nullptr;
}
int32_t DawnHost::init(DawnGame *game) {
int32_t DawnHost::init(std::shared_ptr<DawnGame> game) {
// Update values
DAWN_HOST = weak_from_this();
this->game = game;
DAWN_HOST = this;
// Init GLFW
if(!glfwInit()) return DAWN_GLFW_INIT_RESULT_INIT_FAILED;
@ -65,9 +65,9 @@ int32_t DawnHost::init(DawnGame *game) {
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");
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
@ -106,7 +106,7 @@ int32_t DawnHost::init(DawnGame *game) {
return DAWN_HOST_INIT_RESULT_SUCCESS;
}
int32_t DawnHost::start(DawnGame *game) {
int32_t DawnHost::start(std::shared_ptr<DawnGame> game) {
double_t time, newTime;
float_t fDelta;
int32_t updateResult;
@ -139,7 +139,7 @@ int32_t DawnHost::start(DawnGame *game) {
return DAWN_HOST_START_RESULT_EXIT_SUCCESS;
}
int32_t DawnHost::update(DawnGame *game, float_t delta) {
int32_t DawnHost::update(std::shared_ptr<DawnGame> game, float_t delta) {
// Tick game.
auto ret = game->update(delta);
switch(ret) {
@ -154,7 +154,7 @@ int32_t DawnHost::update(DawnGame *game, float_t delta) {
}
}
void DawnHost::unload(DawnGame *game) {
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) {
@ -171,8 +171,6 @@ DawnHost::~DawnHost() {
this->data->window = nullptr;
glfwTerminate();
}
delete this->data;
DAWN_HOST = nullptr;
}
@ -182,10 +180,11 @@ void glfwOnError(int error, const char* description) {
}
void glfwOnResize(GLFWwindow *window, int32_t w, int32_t h) {
if(DAWN_HOST == nullptr) return;
assertTrue(window == DAWN_HOST->data->window, "glfwOnResize: Window mismatch");
auto host = DAWN_HOST.lock();
if(!host) return;
assertTrue(window == host->data->window, "glfwOnResize: Window mismatch");
auto backBuffer = ((BackBufferRenderTarget*)&DAWN_HOST->game->renderManager.backBuffer);
auto backBuffer = ((BackBufferRenderTarget*)&host->game->renderManager.backBuffer);
assertNotNull(backBuffer, "glfwOnResize: Back buffer is not valid");
int32_t windowWidth, windowHeight;
@ -204,7 +203,8 @@ void glfwOnKey(
int32_t action,
int32_t mods
) {
if(DAWN_HOST == nullptr) return;
auto host = DAWN_HOST.lock();
if(host == nullptr) return;
// Determine Value
float_t value;
@ -217,26 +217,28 @@ void glfwOnKey(
}
// Determine the input axis
DAWN_HOST->game->inputManager.rawInputValues[key] = value;
host->game->inputManager.rawInputValues[key] = value;
}
void glfwOnCursor(GLFWwindow* window, double xpos, double ypos) {
if(DAWN_HOST == nullptr) return;
auto host = DAWN_HOST.lock();
if(host == nullptr) return;
DAWN_HOST->game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_X] = mathClamp<float_t>(
(float_t)(xpos / DAWN_HOST->game->renderManager.backBuffer.getWidth()),
host->game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_X] = mathClamp<float_t>(
(float_t)(xpos / host->game->renderManager.backBuffer->getWidth()),
0, 1
);
DAWN_HOST->game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_Y] = mathClamp<float_t>(
(float_t)(ypos / DAWN_HOST->game->renderManager.backBuffer.getHeight()),
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) {
if(DAWN_HOST == nullptr) return;
assertTrue(window == DAWN_HOST->data->window, "glfwOnMouseButton: Window mismatch");
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;
DAWN_HOST->game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_0 + button] = value;
host->game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_0 + button] = value;
}