Base layout for the input manager

Feature/InputManager
Nigel Barink 2022-06-19 20:01:31 +02:00
parent 85f9c78adf
commit 3c30bf7fb7
17 changed files with 224 additions and 191 deletions

View File

@ -1,5 +1,4 @@
#include "BarinkEngine.h" #include "BarinkEngine.h"
#include <phonon.h>
EngineStatistics* ES; EngineStatistics* ES;
BarinkEngine::InputManager InputSystem; BarinkEngine::InputManager InputSystem;
@ -9,17 +8,33 @@ int main(int argc, char* argv[]) {
PerfomanceSamplerInit(); PerfomanceSamplerInit();
// Startup services // Create the window
BarinkWindow MainWindow = BarinkWindow(800, 600); BarinkWindow MainWindow = BarinkWindow(800, 600);
// =================================================
// Startup services
// =================================================
// Startup Renderer
BarinkEngine::Renderer renderer = BarinkEngine::Renderer(); BarinkEngine::Renderer renderer = BarinkEngine::Renderer();
// Startup InputManager
InputSystem = BarinkEngine::InputManager(); InputSystem = BarinkEngine::InputManager();
InputSystem.attach(&MainWindow); InputSystem.attach(&MainWindow);
InputSystem.setupGLFWInput(MainWindow.windowptr());
// Startup GUI System
GUIManager GUISystem = GUIManager(&MainWindow); GUIManager GUISystem = GUIManager(&MainWindow);
// Enable depth testing
// NOTE: TODO Move this into the renderer
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
@ -58,6 +73,9 @@ int main(int argc, char* argv[]) {
// Shutdown Services // Shutdown Services
delete ES; delete ES;
InputSystem.detach(&MainWindow);
return 0; return 0;
} }

View File

@ -11,16 +11,13 @@
#include <string> #include <string>
class ModelImporter { class ModelImporter {
public:
static std::vector<BarinkEngine::Mesh> Import(std::string path);
private: private:
void ImportFBX(std::string path);
void ImportBlend(std::string path);
void ImportGLTF(std::string path);
void ImportOBJ(std::string path);
static BarinkEngine::Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene); static BarinkEngine::Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene);
static std::vector<BarinkEngine::Mesh> ModelImporter::processNode(aiNode* node, const aiScene* scene); static std::vector<BarinkEngine::Mesh> ModelImporter::processNode(aiNode* node, const aiScene* scene);
public:
void Import(std::string path);
static std::vector<BarinkEngine::Mesh> Test();
}; };

View File

@ -13,6 +13,8 @@
#include "Graphics/Renderer.h" #include "Graphics/Renderer.h"
#include "Graphics/GUI/GUIManager.h" #include "Graphics/GUI/GUIManager.h"
#include "Scene.h" #include "Scene.h"
#include "PerfCounter.h" #include "PerfCounter.h"

View File

@ -5,7 +5,5 @@ struct Event
{ {
public: public:
std::string name; std::string name;
int argc;
void** argv;
}; };

View File

@ -6,10 +6,12 @@ class EventEmitter {
public: public:
void Subscribe (EventListener& subscriber); void Subscribe (EventListener& subscriber);
void Unsubscribe(EventListener& subscriber); void Unsubscribe(EventListener& subscriber);
void EmitEvent(Event& incident);
protected: protected:
std::list<EventListener*> subscribers; std::list<EventListener*> subscribers;
void EmitEvent(Event& incident);
EventEmitter(); EventEmitter();

View File

@ -0,0 +1,18 @@
#pragma once
#include "Event.h"
struct KEY_DOWN_EVENT : public Event {
public:
int scancode;
int keycode;
int mods;
};
struct KEY_UP_EVENT : public Event {
public:
int scancode;
int keycode;
int mods;
};

View File

@ -28,10 +28,10 @@ public:
~Renderable(); ~Renderable();
static Renderable* Load(); static Renderable* Load(std::string& path);
void Draw(); void Draw();
private: private:
std::vector<BarinkEngine::Mesh> meshes; std::vector<BarinkEngine::Mesh> meshes;
Renderable(); Renderable(std::string& path);
}; };

View File

@ -0,0 +1,14 @@
#pragma once
#include "GLFW/glfw3.h"
namespace BarinkEngine {
namespace Input {
void BE_GLFW_KEYS(GLFWwindow* window, int key, int scancode, int action, int mods);
void BE_GLFW_CURSOR_POSITION(GLFWwindow* window, double x, double y);
void BE_GLFW_CURSOR_ENTER(GLFWwindow* window, int entered);
void BE_GLFW_MOUSE_BUTTON(GLFWwindow* window, int button, int action, int mods);
void BE_GLFW_SCROLL(GLFWwindow* window, double xoffset, double yoffset);
}
}

View File

@ -1,27 +1,29 @@
#pragma once #pragma once
#include <vector> #include <list>
#include "Graphics/Window.h" #include "Graphics/Window.h"
#include "EventSystem/EventEmitter.h" #include "EventSystem/EventEmitter.h"
#include "../Include/Input/GLFWInput.h"
#include "BarinkEngine.h"
namespace BarinkEngine { namespace BarinkEngine {
class InputManager : EventEmitter { class InputManager : public EventEmitter {
public: public:
InputManager(); InputManager();
void PollEvents(); void PollEvents();
void attach(BarinkWindow* window); void attach(BarinkWindow* window);
void detach(BarinkWindow* window);
void setupGLFWInput(GLFWwindow* window);
// GLFW Handlers
static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
static void CursorPositionCallback(GLFWwindow* window, double x, double y);
static void CursorEnterCallback(GLFWwindow* window, int entered);
static void MouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
static void ScrollCallback(GLFWwindow* window, double xoffset, double yoffset);
private: private:
std::vector<BarinkWindow*> windows; std::list<BarinkWindow*> windows;
}; };
} }

View File

@ -0,0 +1,104 @@
#include "BarinkEngine.h"
#include "../Include/Input/GLFWInput.h"
#include "../Include/EventSystem/InputSystemEvents.h"
#include "../Include/Input/InputManager.h"
namespace BarinkEngine {
namespace Input {
void BE_GLFW_KEYS(GLFWwindow* window, int key, int scancode, int action, int mods)
{
switch (action)
{
case GLFW_KEY_DOWN: {
KEY_DOWN_EVENT keydown{};
keydown.name = "KEY::DOWN";
keydown.mods = mods;
keydown.scancode = scancode;
keydown.keycode = key;
InputSystem.EmitEvent(keydown);
break;
}
case GLFW_KEY_UP: {
KEY_UP_EVENT keyup{};
keyup.name = "KEY::DOWN";
keyup.mods = mods;
keyup.scancode = scancode;
keyup.keycode = key;
InputSystem.EmitEvent(keyup);
break;
}
default:
Event KeyEvent{};
KeyEvent.name = "KEY";
InputSystem.EmitEvent(KeyEvent);
break;
}
}
void BE_GLFW_CURSOR_POSITION(GLFWwindow* window, double x, double y)
{
Event CursorPosUpdate{};
CursorPosUpdate.name = "UPDATE::CURSOR:POSITION";
InputSystem.EmitEvent(CursorPosUpdate);
}
void BE_GLFW_CURSOR_ENTER(GLFWwindow* window, int entered)
{
if (entered) {
Event mouseEntered{};
mouseEntered.name = "Mouse Entered Window's confines!";
InputSystem.EmitEvent(mouseEntered);
}
else {
Event mouseLeft{};
mouseLeft.name = "Mouse Left Window's confines!";
InputSystem.EmitEvent(mouseLeft);
}
}
void BE_GLFW_MOUSE_BUTTON(GLFWwindow* window, int button, int action, int mods)
{
Event MouseButtonEvent{};
MouseButtonEvent.name = "MOUSEBUTTON";
InputSystem.EmitEvent(MouseButtonEvent);
}
void BE_GLFW_SCROLL(GLFWwindow* window, double xoffset, double yoffset)
{
Event ScrollEvent{};
ScrollEvent.name = "SCROLL";
InputSystem.EmitEvent(ScrollEvent);
}
}
}

View File

@ -1,118 +1,44 @@
#include "BarinkEngine.h"
#include "Input/InputManager.h" #include "Input/InputManager.h"
#include "GLFW/glfw3.h"
#include "spdlog/spdlog.h"
#include <iostream>
void BarinkEngine::InputManager::PollEvents()
{
for (auto it = windows.begin(); it != windows.end(); ++it) {
(*it)->Poll();
}
}
void BarinkEngine::InputManager::KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) namespace BarinkEngine {
{
Event KeyEvent{}; void InputManager::PollEvents()
KeyEvent.name = "KEY";
InputSystem.EmitEvent(KeyEvent);
if (key == GLFW_KEY_A && action == GLFW_PRESS)
{ {
for (auto it = windows.begin(); it != windows.end(); ++it) {
std::cout << "'a' key was pressed" << std::endl; (*it)->Poll();
}
} }
}
void BarinkEngine::InputManager::CursorPositionCallback(GLFWwindow* window, double x, double y) void InputManager::setupGLFWInput(GLFWwindow* window) {
{ // Attach callbacks
//std::cout << "Cursor Position x: " << x << ", y: " << y << std::endl; glfwSetKeyCallback(window, BarinkEngine::Input::BE_GLFW_KEYS);
Event CursorPosUpdate{}; glfwSetCursorPosCallback(window, BarinkEngine::Input::BE_GLFW_CURSOR_POSITION);
CursorPosUpdate.name = "UPDATE::CURSOR:POSITION"; glfwSetCursorEnterCallback(window, BarinkEngine::Input::BE_GLFW_CURSOR_ENTER);
glfwSetMouseButtonCallback(window, BarinkEngine::Input::BE_GLFW_MOUSE_BUTTON);
InputSystem.EmitEvent(CursorPosUpdate); glfwSetScrollCallback(window, BarinkEngine::Input::BE_GLFW_SCROLL);
}
void BarinkEngine::InputManager::CursorEnterCallback(GLFWwindow* window, int entered)
{
if (entered) {
Event mouseEntered {};
mouseEntered.name = "Mouse Entered Window's confines!";
mouseEntered.argc = 0;
InputSystem.EmitEvent(mouseEntered);
}
else {
Event mouseLeft{};
mouseLeft.name = "Mouse Left Window's confines!";
mouseLeft.argc = 0;
InputSystem.EmitEvent(mouseLeft);
}
}
void BarinkEngine::InputManager::MouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
{
Event MouseButtonEvent{};
MouseButtonEvent.name = "MOUSEBUTTON";
InputSystem.EmitEvent(MouseButtonEvent);
if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) {
std::cout << "Right mouse button was pressed!" << std::endl;
} }
}
void BarinkEngine::InputManager::ScrollCallback(GLFWwindow* window, double xoffset, double yoffset) void InputManager::attach(BarinkWindow* window)
{ {
std::cout << "Scroll: x: " << xoffset << ", y: " << yoffset << std::endl; windows.push_back(window);
this->Subscribe((EventListener&)(*window));
Event ScrollEvent{};
ScrollEvent.name = "SCROLL";
InputSystem.EmitEvent(ScrollEvent); }
void InputManager::detach(BarinkWindow* window)
{
windows.remove(window);
this->Unsubscribe((EventListener&)*window);
}
InputManager::InputManager() : EventEmitter()
{
windows = std::list<BarinkWindow*>();
}
} }
void BarinkEngine::InputManager::attach(BarinkWindow* window)
{
windows.push_back(window);
// Attach callbacks
glfwSetKeyCallback(window->windowptr(), KeyCallback);
glfwSetCursorPosCallback(window->windowptr(), CursorPositionCallback);
glfwSetCursorEnterCallback(window->windowptr(), CursorEnterCallback);
glfwSetMouseButtonCallback(window->windowptr(), MouseButtonCallback);
glfwSetScrollCallback(window->windowptr(), ScrollCallback);
this->Subscribe( (EventListener&)(*window));
}
BarinkEngine::InputManager::InputManager() : EventEmitter ()
{
windows = std::vector<BarinkWindow*>();
}

View File

@ -1,66 +1,13 @@
#include "AssetManager/ModelImporter.h" #include "AssetManager/ModelImporter.h"
void ModelImporter::ImportFBX(std::string path) std::vector<BarinkEngine::Mesh> ModelImporter::Import(std::string path)
{ {
//spdlog::warn("ImportFBX not implemented!");
}
void ModelImporter::ImportBlend(std::string path)
{
//spdlog::warn("ImportBlend not implemented!");
}
void ModelImporter::ImportGLTF(std::string path)
{
//spdlog::warn("ImportGLTF not implemented!");
}
void ModelImporter::ImportOBJ(std::string path)
{
//spdlog::warn("ImportOBJ not implemented!");
}
void ModelImporter::Import(std::string path)
{
//spdlog::warn("Import not implemented!");
}
std::vector<BarinkEngine::Mesh> ModelImporter::Test() {
/*
spdlog::info("====== Tiny GLTF ======");
tinygltf::Model loadedModel;
tinygltf::TinyGLTF loader;
std::string error;
std::string warn;
bool ret = loader.LoadASCIIFromFile(&loadedModel, &error, &warn, "./Build/SandboxApplication/Debug/sponza.gltf");
if (!warn.empty())
spdlog::warn("TinyGLTF Warning: {}", warn);
if (!error.empty())
spdlog::error("TinyGLTF Error: {}", error);
if (!ret) {
spdlog::error("TinyGLTF Error: Failed to parse glTF");
exit(-1);
}
spdlog::info("Meshes in model: {}", loadedModel.meshes.size());
spdlog::info("Primitives in mesh: {}", loadedModel.meshes[0].primitives.size());
*/
//spdlog::info("======= Assimp ======");
Assimp::Importer importer; Assimp::Importer importer;
const aiScene* scene = importer.ReadFile("build/SandboxApplication/Debug/Models/Cube.obj", aiProcess_Triangulate | aiProcess_FlipUVs); const aiScene* scene = importer.ReadFile(path.c_str(), aiProcess_Triangulate | aiProcess_FlipUVs);
aiNode* currentNode = scene->mRootNode; aiNode* currentNode = scene->mRootNode;
return processNode(currentNode, scene); return processNode(currentNode, scene);
} }
std::vector<BarinkEngine::Mesh> ModelImporter::processNode(aiNode* node, const aiScene* scene) { std::vector<BarinkEngine::Mesh> ModelImporter::processNode(aiNode* node, const aiScene* scene) {

View File

@ -3,14 +3,14 @@
#include "PerfCounter.h" #include "PerfCounter.h"
Renderable* Renderable::Load() Renderable* Renderable::Load(std::string& path)
{ {
return new Renderable(); return new Renderable(path);
} }
Renderable::Renderable() Renderable::Renderable(std::string& path)
{ {
meshes = ModelImporter::Test(); meshes = ModelImporter::Import(path);
transform.Scale = glm::vec3(1.0f); transform.Scale = glm::vec3(1.0f);
transform.Rotation = glm::vec3(0.0f, 0.0f, 0.0f); transform.Rotation = glm::vec3(0.0f, 0.0f, 0.0f);

View File

@ -79,6 +79,9 @@ void BarinkWindow::SwapBuffers()
void BarinkWindow::ReceiveEvent(Event& incident) void BarinkWindow::ReceiveEvent(Event& incident)
{ {
std::cout << "EVENT RECEIVED: " << incident.name << std::endl; std::cout << "EVENT RECEIVED: " << incident.name << std::endl;
} }

BIN
Manuals/GLSL.std.450.pdf Normal file

Binary file not shown.

BIN
Manuals/SPIRV.pdf Normal file

Binary file not shown.

View File

@ -66,11 +66,13 @@ void Start() {
matCube2 = new Material(*shader); matCube2 = new Material(*shader);
matCube2->Color = glm::vec3(0.0, 1.0f, 0.0); matCube2->Color = glm::vec3(0.0, 1.0f, 0.0);
std::string cubePath = "build/SandboxApplication/Debug/Models/Cube.obj";
std::string lanternPath = "build/SandboxApplication/Debug/Models/Latern.gltf";
/* /*
* load meshes * load meshes
*/ */
Cube = Renderable::Load(); Cube = Renderable::Load(lanternPath);
Cube2 = Renderable::Load(); Cube2 = Renderable::Load(cubePath);
Cube->addChild(*Cube2); Cube->addChild(*Cube2);
Cube->shader = shader; Cube->shader = shader;