diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d7b4104..2418028 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,7 +2,6 @@ add_executable(Fall-Fever main.cpp Controller.cpp Window.cpp - EventHandler.cpp ShaderProgram.cpp VertexArray.cpp Camera.cpp diff --git a/src/Camera.cpp b/src/Camera.cpp index 5de4730..e557f29 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -1,5 +1,4 @@ #include "Camera.h" -#include "definitions/eventActions.h" #include #include @@ -40,58 +39,58 @@ void Camera::lookForward() m_viewMatrix = glm::lookAt(m_position, m_position + m_frontVec, m_upVec); } -void Camera::updatePositionFromKeyboardInput(const CameraActionMap &cameraActionMap, float deltaTime) -{ - glm::vec3 frontVecWithoutY = glm::vec3(m_frontVec.x, 0.0f, m_frontVec.z); +// void Camera::updatePositionFromKeyboardInput(const CameraActionMap &cameraActionMap, float deltaTime) +// { +// glm::vec3 frontVecWithoutY = glm::vec3(m_frontVec.x, 0.0f, m_frontVec.z); - glm::vec3 deltaPos = glm::vec3(0.0f, 0.0f, 0.0f); +// glm::vec3 deltaPos = glm::vec3(0.0f, 0.0f, 0.0f); - if (cameraActionMap.at(CameraAction::Forward)) { - deltaPos += m_speed * deltaTime * glm::normalize(frontVecWithoutY); - } - if (cameraActionMap.at(CameraAction::Backward)) { - deltaPos -= m_speed * deltaTime * glm::normalize(frontVecWithoutY); - } - if (cameraActionMap.at(CameraAction::Left)) { - deltaPos -= m_speed * deltaTime * glm::normalize(glm::cross(m_frontVec, m_upVec)); - } - if (cameraActionMap.at(CameraAction::Right)) { - deltaPos += m_speed * deltaTime * glm::normalize(glm::cross(m_frontVec, m_upVec)); - } - if (cameraActionMap.at(CameraAction::Up)) { - deltaPos += m_speed * deltaTime * m_upVec; - } - if (cameraActionMap.at(CameraAction::Down)) { - deltaPos -= m_speed * deltaTime * m_upVec; - } +// if (cameraActionMap.at(CameraAction::Forward)) { +// deltaPos += m_speed * deltaTime * glm::normalize(frontVecWithoutY); +// } +// if (cameraActionMap.at(CameraAction::Backward)) { +// deltaPos -= m_speed * deltaTime * glm::normalize(frontVecWithoutY); +// } +// if (cameraActionMap.at(CameraAction::Left)) { +// deltaPos -= m_speed * deltaTime * glm::normalize(glm::cross(m_frontVec, m_upVec)); +// } +// if (cameraActionMap.at(CameraAction::Right)) { +// deltaPos += m_speed * deltaTime * glm::normalize(glm::cross(m_frontVec, m_upVec)); +// } +// if (cameraActionMap.at(CameraAction::Up)) { +// deltaPos += m_speed * deltaTime * m_upVec; +// } +// if (cameraActionMap.at(CameraAction::Down)) { +// deltaPos -= m_speed * deltaTime * m_upVec; +// } - m_position += deltaPos; -} +// m_position += deltaPos; +// } -void Camera::updateDirectionFromMouseInput(const CameraMouseActionMap &cameraMouseActionMap) -{ +// void Camera::updateDirectionFromMouseInput(const CameraMouseActionMap &cameraMouseActionMap) +// { - if (cameraMouseActionMap.at(CameraMouseAction::DeltaX) == 0 && - cameraMouseActionMap.at(CameraMouseAction::DeltaY) == 0) { - return; - } +// if (cameraMouseActionMap.at(CameraMouseAction::DeltaX) == 0 && +// cameraMouseActionMap.at(CameraMouseAction::DeltaY) == 0) { +// return; +// } - m_yaw += cameraMouseActionMap.at(CameraMouseAction::DeltaX); - m_pitch += cameraMouseActionMap.at(CameraMouseAction::DeltaY); +// m_yaw += cameraMouseActionMap.at(CameraMouseAction::DeltaX); +// m_pitch += cameraMouseActionMap.at(CameraMouseAction::DeltaY); - if (m_pitch > 89.0f) { - m_pitch = 89.0f; - } - if (m_pitch < -89.0f) { - m_pitch = -89.0f; - } +// if (m_pitch > 89.0f) { +// m_pitch = 89.0f; +// } +// if (m_pitch < -89.0f) { +// m_pitch = -89.0f; +// } - glm::vec3 direction; - direction.x = cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch)); - direction.y = sin(glm::radians(m_pitch)); - direction.z = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch)); - m_frontVec = glm::normalize(direction); -} +// glm::vec3 direction; +// direction.x = cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch)); +// direction.y = sin(glm::radians(m_pitch)); +// direction.z = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch)); +// m_frontVec = glm::normalize(direction); +// } void Camera::setPosition(glm::vec3 position) { diff --git a/src/Camera.h b/src/Camera.h index cb38399..9e244f7 100644 --- a/src/Camera.h +++ b/src/Camera.h @@ -1,7 +1,5 @@ #pragma once -#include "definitions/eventActions.h" - #include class Camera @@ -12,8 +10,8 @@ public: void updateVPM(); void updateAspectRatio(float aspectRatio); - void updatePositionFromKeyboardInput(const CameraActionMap &cameraActionMap, float deltaTime); - void updateDirectionFromMouseInput(const CameraMouseActionMap &cameraMouseActionMap); + // void updatePositionFromKeyboardInput(const CameraActionMap &cameraActionMap, float deltaTime); + // void updateDirectionFromMouseInput(const CameraMouseActionMap &cameraMouseActionMap); void translate(glm::vec3 translateVector); diff --git a/src/Controller.cpp b/src/Controller.cpp index 24883e2..10d996f 100644 --- a/src/Controller.cpp +++ b/src/Controller.cpp @@ -1,7 +1,6 @@ #include "Controller.h" #include "Camera.h" #include "Entity.h" -#include "EventHandler.h" #include "Helper.h" #include "Light.h" #include "Scene.h" @@ -19,12 +18,10 @@ #include #include -Controller::Controller() : m_gameWindow(std::unique_ptr(new Window)) +Controller::Controller() + : m_gameWindow(std::make_shared()), + m_camera(std::make_shared(90.0f, m_gameWindow->getWindowAspectRatio())) { - m_gameEventHandler = new EventHandler(m_gameWindow->getGLFWwindow()); - - m_camera = new Camera(90.0f, m_gameWindow->getWindowAspectRatio()); - std::array shaderProgramPrototypes{ ShaderProgram::Prototype{"defaultProgram", "data/shaders/basic.vert", "data/shaders/basic.frag", ""}, ShaderProgram::Prototype{"lightProgram", "data/shaders/light.vert", "data/shaders/light.frag", ""}, @@ -49,18 +46,6 @@ Controller::Controller() : m_gameWindow(std::unique_ptr(new Window)) m_scene = std::make_shared(m_shaderPrograms); } -Controller::~Controller() -{ - // for (auto program : m_shaderPrograms) { - // delete program; - // } - - // delete m_scene; - delete m_camera; - // delete m_postProcessFrameBuffer; - delete m_gameEventHandler; -} - void Controller::run() { updateExposure(*getShaderProgramByName("postProcessingProgram")); @@ -125,14 +110,7 @@ void Controller::run() } // --- Check events, handle input --- - m_gameEventHandler->handleEvents(); - - m_camera->updatePositionFromKeyboardInput(m_gameEventHandler->getCameraActionMap(), m_deltaTime); - if (m_gameWindow->getMouseIsCatched()) { - m_camera->updateDirectionFromMouseInput(m_gameEventHandler->getCameraMouseActionMap()); - } - - m_gameWindow->handleWindowActionMap(m_gameEventHandler->getWindowActionMap()); + glfwPollEvents(); } } @@ -143,7 +121,7 @@ void Controller::limit_framerate() lastTime = glfwGetTime() - startingTime; - double frameTime = 1 / (double)m_MAX_FPS; + double frameTime = 1 / (double)MAX_FPS; if (frameTime > lastTime) { Helper::sleep((frameTime - lastTime) * 1000000); } @@ -156,7 +134,7 @@ void Controller::limit_framerate() void Controller::updateWindowDimensions() { m_camera->updateAspectRatio(m_gameWindow->getWindowAspectRatio()); - m_gameEventHandler->setFirstMouseInput(1); + // m_gameEventHandler->setFirstMouseInput(1); m_postProcessFrameBuffer->changeDimensions(m_gameWindow->getWindowWidth(), m_gameWindow->getWindowHeight()); } @@ -184,8 +162,3 @@ Controller::getShaderProgramByName(const std::string &name, std::vector getShaderProgramByName(const std::string &name); static std::shared_ptr @@ -35,24 +26,18 @@ public: private: void limit_framerate(); - void updateWindowDimensions(); - void renderImGui(Scene *world, glm::vec3 *lightColor, bool *rotateEntity, bool *rotateLightSource, - ShaderProgram *postProcessingProgram, float *intensity, bool *drawShadows); - - std::unique_ptr m_gameWindow; - EventHandler *m_gameEventHandler; - + std::shared_ptr m_gameWindow; std::shared_ptr m_scene; - Camera *m_camera; + std::shared_ptr m_camera; std::vector> m_shaderPrograms; std::shared_ptr m_postProcessFrameBuffer; - uint16_t m_MAX_FPS = 60; + static constexpr unsigned MAX_FPS = 60; double m_deltaTime; float m_exposure = 1.0f; diff --git a/src/EventHandler.cpp b/src/EventHandler.cpp deleted file mode 100644 index 5759022..0000000 --- a/src/EventHandler.cpp +++ /dev/null @@ -1,174 +0,0 @@ -#include "EventHandler.h" - -#include "GLFW/glfw3.h" - -CameraActionMap EventHandler::s_cameraActionMap = {{CameraAction::Forward, false}, {CameraAction::Backward, false}, - {CameraAction::Up, false}, {CameraAction::Down, false}, - {CameraAction::Left, false}, {CameraAction::Right, false}}; - -CameraMouseActionMap EventHandler::s_cameraMouseActionMap = {{CameraMouseAction::DeltaX, 0.0}, - {CameraMouseAction::DeltaY, 0.0}}; - -WindowActionMap EventHandler::s_windowActionMap = {{WindowAction::WireFrameToggle, false}, - {WindowAction::MouseCatchToggle, false}, - {WindowAction::WindowShouldClose, false}}; - -MouseButtonActionMap EventHandler::s_mouseButtonActionMap = {{MouseButtonAction::LeftClicked, false}, - {MouseButtonAction::RightClicked, false}, - {MouseButtonAction::MiddleClicked, false}}; - -bool EventHandler::s_firstMouseInput = 1; -float EventHandler::s_mouseSensitivity = 0.15f; - -EventHandler::EventHandler(GLFWwindow *p_window) : m_window(p_window) -{ - glfwSetKeyCallback(m_window, key_callback); - glfwSetCursorPosCallback(m_window, mouse_callback); - glfwSetMouseButtonCallback(m_window, mouse_button_callback); -} - -void EventHandler::handleEvents() -{ - clearActionRegisters(); - glfwPollEvents(); -} - -void EventHandler::clearActionRegisters() -{ - for (auto &element : s_cameraMouseActionMap) { - element.second = 0.0; - } - - for (auto &element : s_windowActionMap) { - element.second = 0.0; - } - - for (auto &element : s_mouseButtonActionMap) { - element.second = 0.0; - } -} - -void EventHandler::key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) -{ - // Silence warnings of unused variables. - (void)window; - (void)scancode; - (void)mods; - - if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { - s_windowActionMap[WindowAction::WindowShouldClose] = 1; - } - - if (key == GLFW_KEY_O && action == GLFW_PRESS) { - s_windowActionMap[WindowAction::WireFrameToggle] = 1; - } - if (key == GLFW_KEY_LEFT_CONTROL && action == GLFW_PRESS) { - s_windowActionMap[WindowAction::MouseCatchToggle] = 1; - s_firstMouseInput = 1; - } - - // Movement press - if (key == GLFW_KEY_W && action == GLFW_PRESS) { - s_cameraActionMap[CameraAction::Forward] = 1; - } - if (key == GLFW_KEY_S && action == GLFW_PRESS) { - s_cameraActionMap[CameraAction::Backward] = 1; - } - if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) { - s_cameraActionMap[CameraAction::Up] = 1; - } - if (key == GLFW_KEY_LEFT_SHIFT && action == GLFW_PRESS) { - s_cameraActionMap[CameraAction::Down] = 1; - } - if (key == GLFW_KEY_A && action == GLFW_PRESS) { - s_cameraActionMap[CameraAction::Left] = 1; - } - if (key == GLFW_KEY_D && action == GLFW_PRESS) { - s_cameraActionMap[CameraAction::Right] = 1; - } - - // Movement release - if (key == GLFW_KEY_W && action == GLFW_RELEASE) { - s_cameraActionMap[CameraAction::Forward] = 0; - } - if (key == GLFW_KEY_S && action == GLFW_RELEASE) { - s_cameraActionMap[CameraAction::Backward] = 0; - } - if (key == GLFW_KEY_SPACE && action == GLFW_RELEASE) { - s_cameraActionMap[CameraAction::Up] = 0; - } - if (key == GLFW_KEY_LEFT_SHIFT && action == GLFW_RELEASE) { - s_cameraActionMap[CameraAction::Down] = 0; - } - if (key == GLFW_KEY_A && action == GLFW_RELEASE) { - s_cameraActionMap[CameraAction::Left] = 0; - } - if (key == GLFW_KEY_D && action == GLFW_RELEASE) { - s_cameraActionMap[CameraAction::Right] = 0; - } -} - -void EventHandler::mouse_callback(GLFWwindow *window, double xpos, double ypos) -{ - (void)window; - - static double lastCursorPosX = 0.0; - static double lastCursorPosY = 0.0; - - double deltaCursorPosX = xpos - lastCursorPosX; - double deltaCursorPosY = -(ypos - lastCursorPosY); - - lastCursorPosX = xpos; - lastCursorPosY = ypos; - - // Check if this is the first VALID mouse event after window being resized - if (s_firstMouseInput && !(deltaCursorPosX == 0 && deltaCursorPosY == 0)) { - s_firstMouseInput = 0; - deltaCursorPosX = 0.0; - deltaCursorPosY = 0.0; - } - - deltaCursorPosX *= s_mouseSensitivity; - deltaCursorPosY *= s_mouseSensitivity; - - s_cameraMouseActionMap[CameraMouseAction::DeltaX] += deltaCursorPosX; - s_cameraMouseActionMap[CameraMouseAction::DeltaY] += deltaCursorPosY; -} - -void EventHandler::mouse_button_callback(GLFWwindow *window, int button, int action, int mods) -{ - (void)window; - (void)mods; - - if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) - s_mouseButtonActionMap[MouseButtonAction::LeftClicked] = true; - if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) - s_mouseButtonActionMap[MouseButtonAction::RightClicked] = true; - if (button == GLFW_MOUSE_BUTTON_MIDDLE && action == GLFW_PRESS) - s_mouseButtonActionMap[MouseButtonAction::MiddleClicked] = true; -} - -const CameraActionMap &EventHandler::getCameraActionMap() const -{ - return s_cameraActionMap; -} - -WindowActionMap &EventHandler::getWindowActionMap() const -{ - return s_windowActionMap; -} - -const MouseButtonActionMap &EventHandler::getMouseButtonActionMap() const -{ - return s_mouseButtonActionMap; -} - -const CameraMouseActionMap &EventHandler::getCameraMouseActionMap() const -{ - return s_cameraMouseActionMap; -} - -void EventHandler::setFirstMouseInput(bool val) -{ - s_firstMouseInput = val; -} diff --git a/src/EventHandler.h b/src/EventHandler.h deleted file mode 100644 index 49af75e..0000000 --- a/src/EventHandler.h +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -#include "definitions/enumHash.h" -#include "definitions/eventActions.h" - -class GLFWwindow; - -class EventHandler -{ -public: - EventHandler(GLFWwindow *p_window); - ~EventHandler() = default; - - void handleEvents(); - - WindowActionMap &getWindowActionMap() const; - const CameraActionMap &getCameraActionMap() const; - const MouseButtonActionMap &getMouseButtonActionMap() const; - const CameraMouseActionMap &getCameraMouseActionMap() const; - void setFirstMouseInput(bool val); - -private: - void clearActionRegisters(); - - static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods); - static void mouse_button_callback(GLFWwindow *window, int button, int action, int mods); - static void mouse_callback(GLFWwindow *window, double xpos, double ypos); - - static CameraActionMap s_cameraActionMap; - static CameraMouseActionMap s_cameraMouseActionMap; - static WindowActionMap s_windowActionMap; - static MouseButtonActionMap s_mouseButtonActionMap; - - static float s_mouseSensitivity; - static bool s_firstMouseInput; - - GLFWwindow *m_window; -}; diff --git a/src/Window.cpp b/src/Window.cpp index 87a33d1..c0286ed 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -80,12 +80,14 @@ Window::Window() Window::~Window() { glfwDestroyWindow(m_window); - glfwTerminate(); } bool Window::isWindowResized() { - int new_width, new_height, new_posx, new_posy; + int new_width{}; + int new_height{}; + int new_posx{}; + int new_posy{}; glfwGetFramebufferSize(m_window, &new_width, &new_height); glfwGetWindowPos(m_window, &new_posx, &new_posy); @@ -109,29 +111,6 @@ void Window::setCatchedCursor(bool value) } } -void Window::handleWindowActionMap(WindowActionMap &windowActionMap) -{ - if (windowActionMap.at(WindowAction::WireFrameToggle)) { - windowActionMap[WindowAction::WireFrameToggle] = false; - m_wireFrameMode = !m_wireFrameMode; - if (m_wireFrameMode) { - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - } else { - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - } - } - - if (windowActionMap.at(WindowAction::MouseCatchToggle)) { - windowActionMap[WindowAction::MouseCatchToggle] = false; - m_mouseCatched = !m_mouseCatched; - setCatchedCursor(m_mouseCatched); - } - - if (windowActionMap.at(WindowAction::WindowShouldClose)) { - glfwSetWindowShouldClose(m_window, true); - } -} - // GLFW error function void Window::glfw_error_callback(int error, const char *description) { @@ -164,8 +143,3 @@ float Window::getWindowAspectRatio() { return (float)m_width / (float)m_height; } - -bool Window::getMouseIsCatched() -{ - return m_mouseCatched; -} diff --git a/src/Window.h b/src/Window.h index e2dc2ee..e47776b 100644 --- a/src/Window.h +++ b/src/Window.h @@ -1,9 +1,5 @@ #pragma once -#include "definitions/eventActions.h" - -#include - class GLFWwindow; class Window @@ -17,13 +13,10 @@ public: int getWindowWidth(); int getWindowHeight(); float getWindowAspectRatio(); - bool getMouseIsCatched(); bool isWindowResized(); void updateWindowDimensions(); - void handleWindowActionMap(WindowActionMap &windowActionMap); - private: static void glfw_error_callback(int error, const char *description); static void framebuffer_size_callback(GLFWwindow *window, int width, int height); diff --git a/src/definitions/enumHash.h b/src/definitions/enumHash.h deleted file mode 100644 index 052dd1f..0000000 --- a/src/definitions/enumHash.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#include - -struct EnumClassHash -{ - template std::size_t operator()(T t) const - { - return static_cast(t); - } -}; diff --git a/src/definitions/eventActions.h b/src/definitions/eventActions.h deleted file mode 100644 index 7ee1786..0000000 --- a/src/definitions/eventActions.h +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#include "enumHash.h" -#include - -enum class CameraAction -{ - Up, - Down, - Forward, - Backward, - Left, - Right, - CAMERA_ACTION_NUM_ITEMS -}; - -enum class CameraMouseAction -{ - DeltaX, - DeltaY, - CAMERA_MOUSE_ACTION_NUM_ITEMS -}; - -enum class WindowAction -{ - WireFrameToggle, - MouseCatchToggle, - WindowShouldClose, - WINDOW_ACTION_NUM_ITEMS -}; - -enum class MouseButtonAction -{ - LeftClicked, - RightClicked, - MiddleClicked, - MOUSE_BUTTON_ACTION_NUM_ITEMS -}; - -typedef std::unordered_map CameraActionMap; -typedef std::unordered_map CameraMouseActionMap; -typedef std::unordered_map WindowActionMap; -typedef std::unordered_map MouseButtonActionMap; diff --git a/src/definitions/window.h b/src/definitions/window.h index a4b0a4e..f2d75f6 100644 --- a/src/definitions/window.h +++ b/src/definitions/window.h @@ -1,4 +1,4 @@ #pragma once -#define INIT_WINDOW_WIDTH 1280 -#define INIT_WINDOW_HEIGHT 720 +static constexpr unsigned INIT_WINDOW_WIDTH = 1280; +static constexpr unsigned INIT_WINDOW_HEIGHT = 720; diff --git a/src/main.cpp b/src/main.cpp index bc6a54c..a9ca7f8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,9 @@ +#include #include #include "Controller.h" -int main(int argc, char **argv) +auto main(int argc, char **argv) -> int { // Suppress warning about unused variable (void)argc; @@ -12,17 +13,12 @@ int main(int argc, char **argv) std::cout << "[Debug Mode]" << std::endl; #endif - // Create controller - Controller controller; - - const char *fps_env = std::getenv("MAXFPS"); - if (fps_env) { - uint16_t maxfps = std::stoul(fps_env); - controller.setMaxFps(maxfps); - std::cout << "[Warning] Default max FPS overridden with " << maxfps << " by environment." << std::endl; + { + // Create controller + Controller controller; + controller.run(); } - controller.run(); - + glfwTerminate(); return 0; }