Remove event system

This commit is contained in:
2022-06-05 13:50:01 +02:00
parent be0e3a88b1
commit 6f2e76a522
13 changed files with 68 additions and 417 deletions

View File

@@ -2,7 +2,6 @@ add_executable(Fall-Fever
main.cpp
Controller.cpp
Window.cpp
EventHandler.cpp
ShaderProgram.cpp
VertexArray.cpp
Camera.cpp

View File

@@ -1,5 +1,4 @@
#include "Camera.h"
#include "definitions/eventActions.h"
#include <glm/ext/matrix_transform.hpp>
#include <glm/gtc/matrix_transform.hpp>
@@ -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)
{

View File

@@ -1,7 +1,5 @@
#pragma once
#include "definitions/eventActions.h"
#include <glm/glm.hpp>
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);

View File

@@ -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 <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
Controller::Controller() : m_gameWindow(std::unique_ptr<Window>(new Window))
Controller::Controller()
: m_gameWindow(std::make_shared<Window>()),
m_camera(std::make_shared<Camera>(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<Window>(new Window))
m_scene = std::make_shared<Scene>(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<std::sha
Log::logger().critical("Shaderprogram could not be found by name \"{}\"", name);
return {};
}
void Controller::setMaxFps(uint16_t fps)
{
m_MAX_FPS = fps;
}

View File

@@ -6,26 +6,17 @@
class ShaderProgram;
class Window;
class EventHandler;
class Scene;
class Camera;
class Menu;
class FrameBuffer;
namespace Imgui {
class Handler;
}
class Controller
{
public:
Controller();
~Controller();
void run();
void setMaxFps(uint16_t fps);
// TODO remove...
std::shared_ptr<ShaderProgram> getShaderProgramByName(const std::string &name);
static std::shared_ptr<ShaderProgram>
@@ -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<Window> m_gameWindow;
EventHandler *m_gameEventHandler;
std::shared_ptr<Window> m_gameWindow;
std::shared_ptr<Scene> m_scene;
Camera *m_camera;
std::shared_ptr<Camera> m_camera;
std::vector<std::shared_ptr<ShaderProgram>> m_shaderPrograms;
std::shared_ptr<FrameBuffer> m_postProcessFrameBuffer;
uint16_t m_MAX_FPS = 60;
static constexpr unsigned MAX_FPS = 60;
double m_deltaTime;
float m_exposure = 1.0f;

View File

@@ -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;
}

View File

@@ -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;
};

View File

@@ -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;
}

View File

@@ -1,9 +1,5 @@
#pragma once
#include "definitions/eventActions.h"
#include <memory>
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);

View File

@@ -1,11 +0,0 @@
#pragma once
#include <cstddef>
struct EnumClassHash
{
template <typename T> std::size_t operator()(T t) const
{
return static_cast<std::size_t>(t);
}
};

View File

@@ -1,43 +0,0 @@
#pragma once
#include "enumHash.h"
#include <unordered_map>
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<CameraAction, bool, EnumClassHash> CameraActionMap;
typedef std::unordered_map<CameraMouseAction, double, EnumClassHash> CameraMouseActionMap;
typedef std::unordered_map<WindowAction, bool, EnumClassHash> WindowActionMap;
typedef std::unordered_map<MouseButtonAction, bool, EnumClassHash> MouseButtonActionMap;

View File

@@ -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;

View File

@@ -1,8 +1,9 @@
#include <GLFW/glfw3.h>
#include <iostream>
#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;
}