Actually managed to add SDL2 support somehow

This commit is contained in:
2023-01-16 11:10:18 -08:00
parent 7585891276
commit f2a0d3b3bb
17 changed files with 347 additions and 1 deletions

3
.gitmodules vendored
View File

@ -7,3 +7,6 @@
[submodule "lib/stb"]
path = lib/stb
url = https://github.com/nothings/stb
[submodule "lib/SDL"]
path = lib/SDL
url = https://github.com/libsdl-org/SDL.git

View File

@ -6,7 +6,7 @@
# Check for build target, or default
if(NOT DEFINED DAWN_BUILD_TARGET)
if(WIN32)
set(DAWN_BUILD_TARGET "target-pokergame-win32-glfw")
set(DAWN_BUILD_TARGET "target-pokergame-win32-sdl2")
elseif(UNIX AND NOT APPLE)
set(DAWN_BUILD_TARGET "target-pokergame-linux64-glfw")
endif()

View File

@ -0,0 +1,8 @@
# Copyright (c) 2022 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
set(DAWN_BUILDING dawnpokergame CACHE INTERNAL ${DAWN_CACHE_TARGET})
set(DAWN_TARGET_WIN32 true CACHE INTERNAL ${DAWN_CACHE_TARGET})
set(DAWN_TARGET_SDL2 true CACHE INTERNAL ${DAWN_CACHE_TARGET})

View File

@ -12,6 +12,9 @@ add_subdirectory(glfw)
# GLM
add_subdirectory(glm)
# SDL
add_subdirectory(SDL)
# STB
add_library(stb INTERFACE)
target_include_directories(stb INTERFACE stb)

1
lib/SDL Submodule

Submodule lib/SDL added at 5d1e6b28d9

View File

@ -36,4 +36,9 @@ target_link_libraries(${DAWN_TARGET_NAME}
if(DAWN_TARGET_GLFW)
add_subdirectory(dawnglfw)
add_subdirectory(dawnopengl)
endif()
if(DAWN_TARGET_SDL2)
add_subdirectory(dawnsdl2)
add_subdirectory(dawnopengl)
endif()

View File

@ -0,0 +1,22 @@
# 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
SDL2-static
glad
)
# Includes
target_include_directories(${DAWN_TARGET_NAME}
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
# Subdirs
add_subdirectory(host)
add_subdirectory(input)
add_subdirectory(time)

View File

@ -0,0 +1,7 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include <glad/glad.h>

View File

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

View File

@ -0,0 +1,172 @@
/**
* Copyright (c) 2022 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "DawnSDL2Host.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
DawnHost *DAWN_HOST = nullptr;
// Host
DawnHost::DawnHost() {
this->data = new DawnHostData();
this->data->window = nullptr;
}
int32_t DawnHost::init(DawnGame *game) {
// Update values
this->game = game;
DAWN_HOST = this;
// Init SDL
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
return DAWN_SDL2_INIT_RESULT_INIT_FAILED;
}
// Request OpenGL 4.5
SDL_GL_LoadLibrary(NULL);
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
// Create Window
game->host->data->window = SDL_CreateWindow(
"Dawn",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
DAWN_SDL2_WINDOW_WIDTH_DEFAULT, DAWN_SDL2_WINDOW_HEIGHT_DEFAULT,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
);
if(game->host->data->window == NULL) {
return DAWN_SDL2_INIT_RESULT_WINDOW_CREATE_FAILED;
}
// Create OpenGL Context and attach GLAD
game->host->data->glContext = SDL_GL_CreateContext(game->host->data->window);
gladLoadGLLoader(SDL_GL_GetProcAddress);
// Override engine defaults appropriately.
game->renderManager.backBuffer.setSize(
DAWN_SDL2_WINDOW_WIDTH_DEFAULT,
DAWN_SDL2_WINDOW_HEIGHT_DEFAULT
);
// Default keybinds
game->inputManager.bind(INPUT_BIND_ACCEPT, SDLK_RETURN);
game->inputManager.bind(INPUT_BIND_ACCEPT, SDLK_e);
game->inputManager.bind(INPUT_BIND_ACCEPT, SDLK_SPACE);
// 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();
}
// Use v-sync
SDL_GL_SetSwapInterval(1);
return DAWN_HOST_INIT_RESULT_SUCCESS;
}
int32_t DawnHost::start(DawnGame *game) {
uint32_t ticks;
int32_t updateResult;
SDL_Event event;
game->host->data->running = true;
// Main Render Loop
ticks = SDL_GetTicks();
while(game->host->data->running) {
// Ticks
uint32_t newTicks = SDL_GetTicks();
float_t delta = (newTicks - ticks) / 1000.0f;
ticks = newTicks;
// Perform update
updateResult = this->update(game, delta);
// Did the update complete successfully?
if(updateResult == DAWN_HOST_UPDATE_RESULT_EXIT) {
break;
} else if(updateResult != DAWN_HOST_UPDATE_RESULT_SUCCESS) {
return DAWN_SDL2_START_RESULT_UPDATE_FAILED;
}
// Swap buffers
SDL_GL_SwapWindow(game->host->data->window);
// Check events.
while (SDL_PollEvent(&event)) {
switch( event.type ){
case SDL_KEYDOWN:
game->inputManager.rawInputValues[event.key.keysym.sym] = 1.0f;
break;
case SDL_KEYUP:
game->inputManager.rawInputValues[event.key.keysym.sym] = 0.0f;
break;
case SDL_QUIT:
game->host->data->running = false;
break;
case SDL_WINDOWEVENT:
if(event.window.event == SDL_WINDOWEVENT_RESIZED) {
this->game->renderManager.backBuffer.setSize(
(float_t)event.window.data1, (float_t)event.window.data2
);
}
break;
default:
break;
}
}
}
return DAWN_HOST_START_RESULT_EXIT_SUCCESS;
}
int32_t DawnHost::update(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(DawnGame *game) {
SDL_GL_DeleteContext(game->host->data->glContext);
SDL_DestroyWindow( game->host->data->window );
this->data->window = nullptr;
SDL_Quit();
}
DawnHost::~DawnHost() {
delete this->data;
DAWN_HOST = nullptr;
}

View File

@ -0,0 +1,25 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include <SDL.h>
#include "host/DawnHost.hpp"
#define DAWN_SDL2_WINDOW_WIDTH_DEFAULT 1280
#define DAWN_SDL2_WINDOW_HEIGHT_DEFAULT 720
#define DAWN_SDL2_INIT_RESULT_INIT_FAILED -1
#define DAWN_SDL2_INIT_RESULT_WINDOW_CREATE_FAILED -2
#define DAWN_SDL2_START_RESULT_UPDATE_FAILED -3
namespace Dawn {
class DawnHostData {
public:
SDL_Window *window;
SDL_GLContext glContext;
bool_t running;
};
}

View File

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

View File

@ -0,0 +1,20 @@
// 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;
InputManager::InputManager(DawnGame *game) : IInputManager<int32_t>(game) {
}
float_t InputManager::getInputValue(int32_t axis) {
auto exist = this->rawInputValues.find(axis);
if(exist == this->rawInputValues.end()) return 0.0f;
return exist->second;
}

View File

@ -0,0 +1,19 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "input/_InputManager.hpp"
namespace Dawn {
class InputManager : public IInputManager<int32_t> {
protected:
float_t getInputValue(int32_t axis) override;
public:
std::map<int32_t, float_t> rawInputValues;
InputManager(DawnGame *game);
};
}

View File

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

View File

@ -0,0 +1,16 @@
// 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

@ -0,0 +1,15 @@
// 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;
};
}