Convert back to a static library

This commit is contained in:
Bailey Harrison 2022-10-02 16:34:51 +01:00
parent 50ca206428
commit d7a79abb1c
10 changed files with 751 additions and 720 deletions

View File

@ -6,7 +6,7 @@ project(engine LANGUAGES CXX
VERSION "0.1.0"
)
add_library(${PROJECT_NAME} SHARED
add_library(${PROJECT_NAME} STATIC
"src/engine.cpp"
"src/window.cpp"
@ -74,6 +74,8 @@ add_library(${PROJECT_NAME} SHARED
target_compile_definitions(${PROJECT_NAME} PRIVATE DEFINITIONS "ENGINE_EXPORTS")
set_property(TARGET ${PROJECT_NAME} PROPERTY WINDOWS_EXPORT_ALL_SYMBOLS ON)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 20)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
@ -143,8 +145,8 @@ add_subdirectory(dependencies/glm)
target_include_directories(${PROJECT_NAME} PUBLIC dependencies/glm)
# spdlog
set(SPDLOG_BUILD_SHARED ON CACHE INTERNAL "" FORCE)
set(BUILD_SHARED_LIBS ON)
set(SPDLOG_BUILD_SHARED OFF CACHE INTERNAL "" FORCE)
set(BUILD_SHARED_LIBS OFF)
add_subdirectory(dependencies/spdlog)
target_link_libraries(${PROJECT_NAME} PUBLIC spdlog)
target_include_directories(${PROJECT_NAME} PUBLIC dependencies/spdlog/include)

View File

@ -2,9 +2,12 @@
#include "engine_api.h"
namespace engine {
class Window;
class Input;
}
class Object;
class Window;
class Input;
class ResourceManager;
class ENGINE_API Component {
@ -30,8 +33,8 @@ public:
Object& parent;
protected:
Window& win;
Input& inp;
engine::Window& win;
engine::Input& inp;
ResourceManager& res;
private:

View File

@ -11,3 +11,5 @@
# define ENGINE_API
# endif
#endif
#define ENGINE_API

View File

@ -8,7 +8,7 @@
struct SDL_Window;
namespace engine::gfx {
namespace engine {
class ENGINE_API GFXDevice {

View File

@ -9,19 +9,21 @@
#include <array>
#include <string>
class Window;
namespace engine {
enum class InputDevice : int {
class Window;
enum class InputDevice : int {
MOUSE,
KEYBOARD,
CONTROLLER,
SIZE
};
};
// This class should be used to get platform/input-device independent input
class ENGINE_API Input {
// This class should be used to get platform/input-device independent input
class ENGINE_API Input {
public:
public:
// requires a window reference to get input from
Input(const Window& win);
@ -49,7 +51,7 @@ public:
bool getButtonRelease(const std::string& buttonName) const;
private:
private:
struct ButtonEntry {
std::string name;
@ -86,4 +88,6 @@ private:
void addInputAxis(const std::string& name, InputDevice device, int axis);
void addInputButtonAsAxis(const std::string& name, InputDevice device, int high, int low);
};
};
}

View File

@ -12,8 +12,10 @@
#include <memory>
#include <stdexcept>
class Window;
class Input;
namespace engine {
class Window;
class Input;
}
class ResourceManager;
class SceneRoot;
@ -28,8 +30,8 @@ namespace components {
}
struct GameIO {
Window * const win;
Input * const input;
engine::Window * const win;
engine::Input * const input;
ResourceManager * const resMan;
};
@ -43,8 +45,8 @@ public:
Object& operator=(const Object&) = delete;
~Object();
Window& win;
Input& inp;
engine::Window& win;
engine::Input& inp;
ResourceManager& res;
SceneRoot& root;

View File

@ -17,9 +17,11 @@
ENGINE_API extern const uint64_t BILLION;
class ENGINE_API Window {
namespace engine {
public:
class ENGINE_API Window {
public:
Window(const std::string& title);
Window(const Window&) = delete;
Window& operator=(const Window&) = delete;
@ -48,7 +50,7 @@ public:
// Returns true if the window should remain open
bool isRunning() const;
void setFullscreen(bool fullscreen, bool exclusive=true);
void setFullscreen(bool fullscreen, bool exclusive = true);
void toggleFullscreen();
bool isFullscreen() const;
@ -127,10 +129,10 @@ public:
/* STATIC METHODS */
static void errorBox(const std::string& message);
public:
public:
SDL_Window* m_handle;
private:
private:
bool m_shouldClose = false;
@ -196,4 +198,6 @@ private:
void onMouseButtonEvent(SDL_MouseButtonEvent& e);
void onMouseMotionEvent(SDL_MouseMotionEvent& e);
void onMouseWheelEvent(SDL_MouseWheelEvent& e);
};
};
}

View File

@ -14,11 +14,12 @@
#include <assert.h>
#include <array>
#include <iostream>
#include <optional>
#include <unordered_set>
namespace engine::gfx {
namespace engine {
static std::vector<const char*> getRequiredVulkanExtensions(SDL_Window* window)
{

View File

@ -5,19 +5,21 @@
#include <string>
#include <stdexcept>
Input::Input(const Window &win) : m_win(win)
{
namespace engine {
Input::Input(const Window& win) : m_win(win)
{
m_enabledDevices.fill(true);
}
}
Input::~Input()
{
}
Input::~Input()
{
}
// private methods
// private methods
float Input::getDeviceAxis(enum InputDevice device, int axis) const
{
float Input::getDeviceAxis(enum InputDevice device, int axis) const
{
switch (device) {
case InputDevice::MOUSE:
switch (static_cast<inputs::MouseAxis>(axis)) {
@ -39,10 +41,10 @@ float Input::getDeviceAxis(enum InputDevice device, int axis) const
default: break;
}
throw std::runtime_error("Error getting device axis");
}
}
bool Input::getDeviceButton(enum InputDevice device, int button) const
{
bool Input::getDeviceButton(enum InputDevice device, int button) const
{
switch (device) {
case InputDevice::MOUSE:
return m_win.getButton(static_cast<inputs::MouseButton>(button));
@ -53,10 +55,10 @@ bool Input::getDeviceButton(enum InputDevice device, int button) const
default: break;
}
throw std::runtime_error("Error getting device button");
}
}
bool Input::getDeviceButtonDown(enum InputDevice device, int button) const
{
bool Input::getDeviceButtonDown(enum InputDevice device, int button) const
{
switch (device) {
case InputDevice::MOUSE:
return m_win.getButtonPress(static_cast<enum inputs::MouseButton>(button));
@ -67,10 +69,10 @@ bool Input::getDeviceButtonDown(enum InputDevice device, int button) const
default: break;
}
throw std::runtime_error("Error getting device button");
}
}
bool Input::getDeviceButtonUp(enum InputDevice device, int button) const
{
bool Input::getDeviceButtonUp(enum InputDevice device, int button) const
{
switch (device) {
case InputDevice::MOUSE:
return m_win.getButtonRelease(static_cast<enum inputs::MouseButton>(button));
@ -81,107 +83,108 @@ bool Input::getDeviceButtonUp(enum InputDevice device, int button) const
default: break;
}
throw std::runtime_error("Error getting device button");
}
}
float Input::getButtonAxis(enum InputDevice device, int high, int low) const
{
float Input::getButtonAxis(enum InputDevice device, int high, int low) const
{
float value = 0.0f;
if (getDeviceButton(device, high)) value += 1.0f;
if (low != 0) {
if (getDeviceButton(device, low)) value += -1.0f;
}
return value;
}
}
// public methods
// public methods
void Input::addInputButton(const std::string& name, InputDevice device, int button)
{
m_buttonEntries.push_back( { name, device, button } );
}
void Input::addInputButton(const std::string& name, InputDevice device, int button)
{
m_buttonEntries.push_back({ name, device, button });
}
void Input::addInputAxis(const std::string& name, InputDevice device, int axis)
{
m_axisEntries.push_back( { name, device, axis, false, 0, 0 } );
}
void Input::addInputAxis(const std::string& name, InputDevice device, int axis)
{
m_axisEntries.push_back({ name, device, axis, false, 0, 0 });
}
void Input::addInputButtonAsAxis(const std::string& name, InputDevice device, int high, int low)
{
m_axisEntries.push_back( { name, device, 0, true, high, low } );
}
void Input::addInputButtonAsAxis(const std::string& name, InputDevice device, int high, int low)
{
m_axisEntries.push_back({ name, device, 0, true, high, low });
}
// OVERLOADS:
// OVERLOADS:
// Add a mouse input
void Input::addInputButton(const std::string& name, inputs::MouseButton button)
{
// Add a mouse input
void Input::addInputButton(const std::string& name, inputs::MouseButton button)
{
addInputButton(name, InputDevice::MOUSE, static_cast<int>(button));
}
}
void Input::addInputAxis(const std::string& name, inputs::MouseAxis axis)
{
void Input::addInputAxis(const std::string& name, inputs::MouseAxis axis)
{
addInputAxis(name, InputDevice::MOUSE, static_cast<int>(axis));
}
}
void Input::addInputButtonAsAxis(const std::string& name, inputs::MouseButton high, inputs::MouseButton low)
{
void Input::addInputButtonAsAxis(const std::string& name, inputs::MouseButton high, inputs::MouseButton low)
{
addInputButtonAsAxis(name, InputDevice::MOUSE, static_cast<int>(high), static_cast<int>(low));
}
}
// Add a keyboard input (TODO: add KeyboardButton enum class)
void Input::addInputButton(const std::string& name, inputs::Key button)
{
// Add a keyboard input (TODO: add KeyboardButton enum class)
void Input::addInputButton(const std::string& name, inputs::Key button)
{
addInputButton(name, InputDevice::KEYBOARD, static_cast<int>(button));
}
}
void Input::addInputButtonAsAxis(const std::string& name, inputs::Key high, inputs::Key low)
{
void Input::addInputButtonAsAxis(const std::string& name, inputs::Key high, inputs::Key low)
{
addInputButtonAsAxis(name, InputDevice::KEYBOARD, static_cast<int>(high), static_cast<int>(low));
}
}
void Input::delInputButton(int index)
{
void Input::delInputButton(int index)
{
std::vector<struct ButtonEntry>::iterator it = m_buttonEntries.begin();
std::advance(it, index);
m_buttonEntries.erase(it);
}
}
void Input::delInputAxis(int index)
{
void Input::delInputAxis(int index)
{
std::vector<struct AxisEntry>::iterator it = m_axisEntries.begin();
std::advance(it, index);
m_axisEntries.erase(it);
}
}
void Input::setDeviceActive(enum InputDevice device, bool active)
{
void Input::setDeviceActive(enum InputDevice device, bool active)
{
m_enabledDevices[static_cast<int>(device)] = active;
}
}
bool Input::getDeviceActive(enum InputDevice device) const
{
bool Input::getDeviceActive(enum InputDevice device) const
{
return m_enabledDevices[static_cast<int>(device)];
}
}
float Input::getAxis(const std::string& axisName) const
{
float Input::getAxis(const std::string& axisName) const
{
for (const AxisEntry& e : m_axisEntries) {
if (e.name == axisName) {
if (m_enabledDevices[static_cast<int>(e.device)]) {
if (e.isButtonAxis) {
return getButtonAxis(e.device, e.high, e.low);
} else {
}
else {
return getDeviceAxis(e.device, e.axis);
}
}
}
}
return 0.0f; // instead of throwing an exception, just return nothing
// throw std::runtime_error("Unable to find mapping in input table");
}
// throw std::runtime_error("Unable to find mapping in input table");
}
bool Input::getButton(const std::string& buttonName) const
{
bool Input::getButton(const std::string& buttonName) const
{
bool isDown = false;
for (const ButtonEntry& e : m_buttonEntries) {
@ -195,10 +198,10 @@ bool Input::getButton(const std::string& buttonName) const
}
}
return isDown;
}
}
bool Input::getButtonPress(const std::string& buttonName) const
{
bool Input::getButtonPress(const std::string& buttonName) const
{
bool isPressed = false;
for (const ButtonEntry& e : m_buttonEntries) {
@ -212,10 +215,10 @@ bool Input::getButtonPress(const std::string& buttonName) const
}
}
return isPressed;
}
}
bool Input::getButtonRelease(const std::string& buttonName) const
{
bool Input::getButtonRelease(const std::string& buttonName) const
{
bool isReleased = false;
for (const ButtonEntry& e : m_buttonEntries) {
@ -229,4 +232,6 @@ bool Input::getButtonRelease(const std::string& buttonName) const
}
}
return isReleased;
}
}

View File

@ -7,8 +7,10 @@
const uint64_t BILLION = 1000000000;
Window::Window(const std::string& title) : m_title(title)
{
namespace engine {
Window::Window(const std::string& title) : m_title(title)
{
// init SDL
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
@ -61,29 +63,29 @@ Window::Window(const std::string& title) : m_title(title)
}
*/
// onResize(m_winSize.x, m_winSize.y);
// onResize(m_winSize.x, m_winSize.y);
}
}
Window::~Window()
{
Window::~Window()
{
SDL_DestroyWindow(m_handle);
SDL_Quit();
}
}
// private methods
// private methods
void Window::onResize(Sint32 width, Sint32 height)
{
void Window::onResize(Sint32 width, Sint32 height)
{
// get window size
m_winSize.x = static_cast<int>(width);
m_winSize.y = static_cast<int>(height);
m_justResized = true;
}
}
void Window::resetInputDeltas()
{
void Window::resetInputDeltas()
{
m_justResized = false;
m_keyboard.deltas.fill(ButtonDelta::SAME);
@ -93,12 +95,12 @@ void Window::resetInputDeltas()
m_mouse.dy = 0;
m_mouse.xscroll = 0.0f;
m_mouse.yscroll = 0.0f;
}
}
// TODO event methods (like callbacks)
// TODO event methods (like callbacks)
void Window::onWindowEvent(SDL_WindowEvent &e)
{
void Window::onWindowEvent(SDL_WindowEvent& e)
{
switch (e.event) {
case SDL_WINDOWEVENT_SIZE_CHANGED:
@ -111,20 +113,20 @@ void Window::onWindowEvent(SDL_WindowEvent &e)
m_keyboardFocus = false;
break;
}
}
}
void Window::onKeyEvent(SDL_KeyboardEvent &e)
{
void Window::onKeyEvent(SDL_KeyboardEvent& e)
{
bool keyWasDown = m_keyboard.keys[e.keysym.scancode];
bool keyIsDown = (e.state == SDL_PRESSED);
m_keyboard.keys[e.keysym.scancode] = keyIsDown;
if (keyIsDown != keyWasDown) { // (if key was pressed or released)
m_keyboard.deltas[e.keysym.scancode] = keyIsDown ? ButtonDelta::PRESSED : ButtonDelta::RELEASED;
}
}
}
void Window::onMouseButtonEvent(SDL_MouseButtonEvent &e)
{
void Window::onMouseButtonEvent(SDL_MouseButtonEvent& e)
{
enum inputs::MouseButton button = inputs::MouseButton::M_INVALID;
switch (e.button) {
case SDL_BUTTON_LEFT:
@ -153,36 +155,37 @@ void Window::onMouseButtonEvent(SDL_MouseButtonEvent &e)
m_mouse.deltas[buttonIndex] = buttonIsDown ? ButtonDelta::PRESSED : ButtonDelta::RELEASED;
}
}
}
}
void Window::onMouseMotionEvent(SDL_MouseMotionEvent &e)
{
void Window::onMouseMotionEvent(SDL_MouseMotionEvent& e)
{
m_mouse.x = e.x;
m_mouse.y = e.y;
m_mouse.dx = e.xrel;
m_mouse.dy = e.yrel;
}
}
void Window::onMouseWheelEvent(SDL_MouseWheelEvent &e)
{
void Window::onMouseWheelEvent(SDL_MouseWheelEvent& e)
{
if (e.direction == SDL_MOUSEWHEEL_NORMAL) {
m_mouse.xscroll = e.preciseX;
m_mouse.yscroll = e.preciseY;
} else { // flipped
}
else { // flipped
m_mouse.xscroll = -e.preciseX;
m_mouse.yscroll = -e.preciseY;
}
}
}
// public methods
// public methods
std::string Window::getTitle() const
{
std::string Window::getTitle() const
{
return m_title;
}
}
void Window::getInputAndEvents()
{
void Window::getInputAndEvents()
{
m_frames++;
uint64_t currentFrameStamp = getNanos();
@ -225,51 +228,51 @@ void Window::getInputAndEvents()
}
}
}
}
void Window::setTitle(std::string title)
{
void Window::setTitle(std::string title)
{
SDL_SetWindowTitle(m_handle, title.c_str());
}
}
bool Window::getWindowResized() const
{
bool Window::getWindowResized() const
{
return m_justResized;
}
}
void Window::show()
{
void Window::show()
{
SDL_ShowWindow(m_handle);
}
}
void Window::hide()
{
void Window::hide()
{
SDL_HideWindow(m_handle);
}
}
void Window::focus()
{
void Window::focus()
{
SDL_RaiseWindow(m_handle);
m_keyboardFocus = true;
}
}
bool Window::hasFocus() const
{
bool Window::hasFocus() const
{
return m_keyboardFocus;
}
}
void Window::setCloseFlag()
{
void Window::setCloseFlag()
{
m_shouldClose = true;
}
}
bool Window::isRunning() const
{
bool Window::isRunning() const
{
return !m_shouldClose;
}
}
void Window::setFullscreen(bool fullscreen, bool exclusive)
{
void Window::setFullscreen(bool fullscreen, bool exclusive)
{
if (SDL_SetWindowFullscreen(m_handle, fullscreen ? (exclusive ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_FULLSCREEN_DESKTOP) : 0) != 0) {
throw std::runtime_error("Unable to set window to fullscreen/windowed");
}
@ -279,176 +282,181 @@ void Window::setFullscreen(bool fullscreen, bool exclusive)
SDL_GetWindowSize(m_handle, &width, &height);
onResize(width, height);
}
}
}
void Window::toggleFullscreen()
{
void Window::toggleFullscreen()
{
setFullscreen(!m_fullscreen);
}
}
bool Window::isFullscreen() const
{
bool Window::isFullscreen() const
{
return m_fullscreen;
}
}
bool Window::setRelativeMouseMode(bool enabled)
{
bool Window::setRelativeMouseMode(bool enabled)
{
m_mouse.captured = enabled;
int code = SDL_SetRelativeMouseMode(static_cast<SDL_bool>(enabled));
if (code != 0) {
throw std::runtime_error("Unable to set relative mouse mode");
} else {
}
else {
return true;
}
}
}
bool Window::mouseCaptured()
{
bool Window::mouseCaptured()
{
return m_mouse.captured;
}
}
// getting input
// getting input
bool Window::getKey(inputs::Key key) const
{
bool Window::getKey(inputs::Key key) const
{
return m_keyboard.keys[static_cast<int>(key)];
}
}
bool Window::getKeyPress(inputs::Key key) const
{
bool Window::getKeyPress(inputs::Key key) const
{
return m_keyboard.deltas[static_cast<int>(key)] == ButtonDelta::PRESSED;
}
}
bool Window::getKeyRelease(inputs::Key key) const
{
bool Window::getKeyRelease(inputs::Key key) const
{
return m_keyboard.deltas[static_cast<int>(key)] == ButtonDelta::RELEASED;
}
}
// TODO mouse input
// TODO mouse input
bool Window::getButton(inputs::MouseButton button) const
{
bool Window::getButton(inputs::MouseButton button) const
{
return m_mouse.buttons[static_cast<int>(button)];
}
}
bool Window::getButtonPress(inputs::MouseButton button) const
{
bool Window::getButtonPress(inputs::MouseButton button) const
{
return m_mouse.deltas[static_cast<int>(button)] == ButtonDelta::PRESSED;
}
}
bool Window::getButtonRelease(inputs::MouseButton button) const
{
bool Window::getButtonRelease(inputs::MouseButton button) const
{
return m_mouse.deltas[static_cast<int>(button)] == ButtonDelta::RELEASED;
}
}
int Window::getMouseX() const
{
int Window::getMouseX() const
{
return static_cast<int>(m_mouse.x);
}
}
int Window::getMouseY() const
{
int Window::getMouseY() const
{
return static_cast<int>(m_mouse.y);
}
}
float Window::getMouseNormX() const
{
float Window::getMouseNormX() const
{
return ((float)m_mouse.x * 2.0f / (float)m_winSize.x) - 1.0f;
}
}
float Window::getMouseNormY() const
{
float Window::getMouseNormY() const
{
return ((float)m_mouse.y * -2.0f / (float)m_winSize.y) + 1.0f;
}
}
int Window::getMouseDX() const
{
int Window::getMouseDX() const
{
return static_cast<int>(m_mouse.dx);
}
}
int Window::getMouseDY() const
{
int Window::getMouseDY() const
{
return static_cast<int>(m_mouse.dy);
}
}
float Window::getMouseScrollX() const
{
float Window::getMouseScrollX() const
{
return m_mouse.xscroll;
}
}
float Window::getMouseScrollY() const
{
float Window::getMouseScrollY() const
{
return m_mouse.yscroll;
}
}
// TODO game pad
// TODO game pad
// get timer value
uint64_t Window::getNanos() const
{
// get timer value
uint64_t Window::getNanos() const
{
uint64_t count;
count = SDL_GetPerformanceCounter();
if (m_counterFreq == BILLION) {
return count;
} else {
}
else {
return count * (BILLION / m_counterFreq);
}
}
}
uint64_t Window::getLastFrameStamp() const
{
uint64_t Window::getLastFrameStamp() const
{
return m_lastFrameStamp;
}
}
uint64_t Window::getFrameCount() const
{
uint64_t Window::getFrameCount() const
{
return m_frames;
}
}
uint64_t Window::getStartTime() const
{
uint64_t Window::getStartTime() const
{
return m_startTime;
}
}
float Window::dt() const
{
float Window::dt() const
{
return (float)m_lastFrameTime / (float)BILLION;
}
}
uint64_t Window::getFPS() const
{
uint64_t Window::getFPS() const
{
if (m_lastFrameTime == 0) return 0;
return BILLION / m_lastFrameTime;
}
}
uint64_t Window::getAvgFPS() const
{
uint64_t Window::getAvgFPS() const
{
uint64_t delta_t = getNanos() - m_avgFpsStart;
if (delta_t == 0) return 0;
return BILLION * (m_frames - m_avgFpsStartCount) / delta_t;
}
}
void Window::resetAvgFPS()
{
void Window::resetAvgFPS()
{
m_avgFpsStart = getNanos();
m_avgFpsStartCount = getFrameCount();
}
}
bool Window::infoBox(const std::string& title, const std::string& msg)
{
bool Window::infoBox(const std::string& title, const std::string& msg)
{
if (isFullscreen() == false) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, title.c_str(), msg.c_str(), m_handle);
return true;
} else {
}
else {
return false;
}
}
}
/* STATIC METHODS */
/* STATIC METHODS */
// Display an error message box
void Window::errorBox(const std::string& message)
{
// Display an error message box
void Window::errorBox(const std::string& message)
{
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Game Error", message.c_str(), NULL);
}
}