abstract user input, refactor #include's

This commit is contained in:
Tyler Murphy 2022-09-23 13:20:30 -04:00
parent 4ab67b4793
commit 9b7d143619
23 changed files with 294 additions and 118 deletions

View file

@ -1,17 +1,13 @@
#include "xe_engine.hpp" #include "xe_engine.hpp"
#include "xe_image.hpp"
#include <chrono>
#include <iostream>
#include <AL/alc.h>
#include <AL/alut.h>
namespace xe { namespace xe {
XeEngine::XeEngine(int width, int height, std::string name) : xeWindow{width, height, name}, XeEngine::XeEngine(int width, int height, std::string name)
: xeWindow{width, height, name},
xeDevice{xeWindow}, xeDevice{xeWindow},
xeRenderer{xeWindow, xeDevice}, xeRenderer{xeWindow, xeDevice},
xeCamera{} { xeCamera{},
xeInput{xeWindow} {
loadDescriptorPool(); loadDescriptorPool();
alutInit(0, NULL); alutInit(0, NULL);
std::cout << "Audio device: " << alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER) << "\n"; std::cout << "Audio device: " << alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER) << "\n";

View file

@ -1,14 +1,19 @@
#pragma once #pragma once
#include "xe_window.hpp"
#include "xe_device.hpp" #include "xe_device.hpp"
#include "xe_renderer.hpp" #include "xe_renderer.hpp"
#include "xe_camera.hpp" #include "xe_camera.hpp"
#include "xe_descriptors.hpp" #include "xe_descriptors.hpp"
#include "xe_image.hpp" #include "xe_image.hpp"
#include "xe_input.hpp"
#include "xe_sound.hpp"
#include <chrono> #include <chrono>
#include <string> #include <string>
#include <iostream>
#include <AL/alc.h>
#include <AL/alut.h>
namespace xe { namespace xe {
class XeEngine { class XeEngine {
@ -22,7 +27,7 @@ class XeEngine {
XeEngine(const XeEngine&) = delete; XeEngine(const XeEngine&) = delete;
XeEngine operator=(const XeEngine&) = delete; XeEngine operator=(const XeEngine&) = delete;
XeWindow& getWindow() {return xeWindow;} XeInput& getInput() {return xeInput;}
XeCamera& getCamera() {return xeCamera;} XeCamera& getCamera() {return xeCamera;}
std::shared_ptr<XeModel> loadModelFromFile(const std::string &filename); std::shared_ptr<XeModel> loadModelFromFile(const std::string &filename);
@ -44,6 +49,7 @@ class XeEngine {
XeDevice xeDevice; XeDevice xeDevice;
XeRenderer xeRenderer; XeRenderer xeRenderer;
XeCamera xeCamera; XeCamera xeCamera;
XeInput xeInput;
std::chrono::_V2::system_clock::time_point currentTime; std::chrono::_V2::system_clock::time_point currentTime;
float frameTime; float frameTime;

46
engine/xe_input.cpp Normal file
View file

@ -0,0 +1,46 @@
#include "xe_input.hpp"
#include <GLFW/glfw3.h>
namespace xe {
static XeInput* _instance;
XeInput::XeInput(XeWindow& window) : window{window} {
glfwSetKeyCallback(window.getGLFWwindow(), XeInput::key_callback);
glfwSetMouseButtonCallback(window.getGLFWwindow(), XeInput::mouse_callback);
_instance = this;
}
bool XeInput::isKeyPressed(int key) {
return glfwGetKey(window.getGLFWwindow(), key) == GLFW_PRESS;
}
bool XeInput::wasKeyPressed(int key) {
if(_pressed[key] == true) {
_pressed[key] = false;
return true;
}
return false;
}
bool XeInput::wasKeyReleased(int key) {
if(_released[key] == true) {
_released[key] = false;
return true;
}
return false;
}
void XeInput::key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if(action == GLFW_PRESS) _instance->_pressed[key] = true;
if(action == GLFW_RELEASE) _instance->_released[key] = true;
}
void XeInput::mouse_callback(GLFWwindow* window, int key, int action, int mods) {
if(action == GLFW_PRESS) _instance->_pressed[key] = true;
if(action == GLFW_RELEASE) _instance->_released[key] = true;
}
}

167
engine/xe_input.hpp Normal file
View file

@ -0,0 +1,167 @@
#pragma once
#include "xe_window.hpp"
#include <vector>
#include <map>
#include <GLFW/glfw3.h>
namespace xe {
class XeInput {
public:
bool isKeyPressed(int key);
bool wasKeyPressed(int key);
bool wasKeyReleased(int key);
private:
XeInput(XeWindow& window);
~XeInput() {};
void setIsKeyDown(int key, bool isDown);
std::map<int, bool> _pressed;
std::map<int, bool> _released;
XeWindow &window;
static void key_callback(
GLFWwindow* window, int key, int scancode, int action, int mods);
static void mouse_callback(
GLFWwindow* window, int key, int action, int mods);
friend class XeEngine;
};
}
#define KEY_SPACE 32
#define KEY_APOSTROPHE 39 /* ' */
#define KEY_COMMA 44 /* , */
#define KEY_MINUS 45 /* - */
#define KEY_PERIOD 46 /* . */
#define KEY_SLASH 47 /* / */
#define KEY_0 48
#define KEY_1 49
#define KEY_2 50
#define KEY_3 51
#define KEY_4 52
#define KEY_5 53
#define KEY_6 54
#define KEY_7 55
#define KEY_8 56
#define KEY_9 57
#define KEY_SEMICOLON 59 /* ; */
#define KEY_EQUAL 61 /* = */
#define KEY_A 65
#define KEY_B 66
#define KEY_C 67
#define KEY_D 68
#define KEY_E 69
#define KEY_F 70
#define KEY_G 71
#define KEY_H 72
#define KEY_I 73
#define KEY_J 74
#define KEY_K 75
#define KEY_L 76
#define KEY_M 77
#define KEY_N 78
#define KEY_O 79
#define KEY_P 80
#define KEY_Q 81
#define KEY_R 82
#define KEY_S 83
#define KEY_T 84
#define KEY_U 85
#define KEY_V 86
#define KEY_W 87
#define KEY_X 88
#define KEY_Y 89
#define KEY_Z 90
#define KEY_LEFT_BRACKET 91 /* [ */
#define KEY_BACKSLASH 92 /* \ */
#define KEY_RIGHT_BRACKET 93 /* ] */
#define KEY_GRAVE_ACCENT 96 /* ` */
#define KEY_WORLD_1 161 /* non-US #1 */
#define KEY_WORLD_2 162 /* non-US #2 */
/* Function keys */
#define KEY_ESCAPE 256
#define KEY_ENTER 257
#define KEY_TAB 258
#define KEY_BACKSPACE 259
#define KEY_INSERT 260
#define KEY_DELETE 261
#define KEY_RIGHT 262
#define KEY_LEFT 263
#define KEY_DOWN 264
#define KEY_UP 265
#define KEY_PAGE_UP 266
#define KEY_PAGE_DOWN 267
#define KEY_HOME 268
#define KEY_END 269
#define KEY_CAPS_LOCK 280
#define KEY_SCROLL_LOCK 281
#define KEY_NUM_LOCK 282
#define KEY_PRINT_SCREEN 283
#define KEY_PAUSE 284
#define KEY_F1 290
#define KEY_F2 291
#define KEY_F3 292
#define KEY_F4 293
#define KEY_F5 294
#define KEY_F6 295
#define KEY_F7 296
#define KEY_F8 297
#define KEY_F9 298
#define KEY_F10 299
#define KEY_F11 300
#define KEY_F12 301
#define KEY_F13 302
#define KEY_F14 303
#define KEY_F15 304
#define KEY_F16 305
#define KEY_F17 306
#define KEY_F18 307
#define KEY_F19 308
#define KEY_F20 309
#define KEY_F21 310
#define KEY_F22 311
#define KEY_F23 312
#define KEY_F24 313
#define KEY_F25 314
#define KEY_KP_0 320
#define KEY_KP_1 321
#define KEY_KP_2 322
#define KEY_KP_3 323
#define KEY_KP_4 324
#define KEY_KP_5 325
#define KEY_KP_6 326
#define KEY_KP_7 327
#define KEY_KP_8 328
#define KEY_KP_9 329
#define KEY_KP_DECIMAL 330
#define KEY_KP_DIVIDE 331
#define KEY_KP_MULTIPLY 332
#define KEY_KP_SUBTRACT 333
#define KEY_KP_ADD 334
#define KEY_KP_ENTER 335
#define KEY_KP_EQUAL 336
#define KEY_LEFT_SHIFT 340
#define KEY_LEFT_CONTROL 341
#define KEY_LEFT_ALT 342
#define KEY_LEFT_SUPER 343
#define KEY_RIGHT_SHIFT 344
#define KEY_RIGHT_CONTROL 345
#define KEY_RIGHT_ALT 346
#define KEY_RIGHT_SUPER 347
#define KEY_MENU 348
#define MOUSE_BUTTON_ONE 0
#define MOUSE_BUTTON_TWO 1
#define MOUSE_BUTTON_THREE 2

View file

@ -1,7 +1,5 @@
#include "xe_pipeline.hpp" #include "xe_pipeline.hpp"
#include "xe_model.hpp"
#include <cstdint> #include <cstdint>
#include <fstream> #include <fstream>
#include <stdexcept> #include <stdexcept>

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "xe_device.hpp" #include "xe_device.hpp"
#include "xe_model.hpp"
#include <cstdint> #include <cstdint>
#include <string> #include <string>

View file

@ -1,19 +1,5 @@
#include "xe_render_system.hpp" #include "xe_render_system.hpp"
#include <vulkan/vulkan.h>
#include "xe_device.hpp"
#include "xe_pipeline.hpp"
#include "xe_game_object.hpp"
#include "xe_swap_chain.hpp"
#include "xe_renderer.hpp"
#include "xe_descriptors.hpp"
#include "xe_engine.hpp"
#include <memory>
#include <stdexcept>
#include <iostream>
namespace xe { namespace xe {
XeRenderSystem::XeRenderSystem( XeRenderSystem::XeRenderSystem(

View file

@ -1,15 +1,17 @@
#pragma once #pragma once
#include <vulkan/vulkan.h>
#include "xe_device.hpp" #include "xe_device.hpp"
#include "xe_pipeline.hpp" #include "xe_pipeline.hpp"
#include "xe_game_object.hpp" #include "xe_game_object.hpp"
#include "xe_descriptors.hpp" #include "xe_swap_chain.hpp"
#include "xe_renderer.hpp" #include "xe_renderer.hpp"
#include "xe_descriptors.hpp"
#include "xe_engine.hpp" #include "xe_engine.hpp"
#include "xe_image.hpp"
#include <vulkan/vulkan.h>
#include <memory> #include <memory>
#include <stdexcept>
#include <iostream>
#include <map> #include <map>
namespace xe { namespace xe {

View file

@ -1,17 +1,5 @@
#include "xe_renderer.hpp" #include "xe_renderer.hpp"
#include "xe_device.hpp"
#include "xe_game_object.hpp"
#include "xe_swap_chain.hpp"
#include "xe_descriptors.hpp"
#include "xe_window.hpp"
#include <memory>
#include <vulkan/vulkan_core.h>
#include <array>
#include <cassert>
#include <stdexcept>
namespace xe { namespace xe {
XeRenderer::XeRenderer(XeWindow& window, XeDevice& device) : xeWindow{window}, xeDevice{device} { XeRenderer::XeRenderer(XeWindow& window, XeDevice& device) : xeWindow{window}, xeDevice{device} {

View file

@ -1,13 +1,16 @@
#pragma once #pragma once
#include "xe_swap_chain.hpp"
#include "xe_window.hpp"
#include "xe_device.hpp" #include "xe_device.hpp"
#include "xe_model.hpp" #include "xe_game_object.hpp"
#include "xe_swap_chain.hpp"
#include "xe_descriptors.hpp"
#include "xe_window.hpp"
#include <memory> #include <array>
#include <vector>
#include <cassert> #include <cassert>
#include <stdexcept>
#include <memory>
#include <vulkan/vulkan_core.h>
namespace xe { namespace xe {
class XeRenderer { class XeRenderer {

View file

@ -1,9 +1,5 @@
#include "xe_sound.hpp" #include "xe_sound.hpp"
#include <vector>
#include <stdexcept>
#include <iostream>
namespace xe { namespace xe {
XeSound::XeSound(const std::string& filename) { XeSound::XeSound(const std::string& filename) {

View file

@ -4,6 +4,9 @@
#include <AL/alc.h> #include <AL/alc.h>
#include <AL/alut.h> #include <AL/alut.h>
#include <string> #include <string>
#include <vector>
#include <stdexcept>
#include <iostream>
#define GLM_FORCE_RADIANS #define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE #define GLM_FORCE_DEPTH_ZERO_TO_ONE

View file

@ -1,14 +1,5 @@
#include "xe_swap_chain.hpp" #include "xe_swap_chain.hpp"
#include <array>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <limits>
#include <memory>
#include <set>
#include <stdexcept>
namespace xe { namespace xe {
bool XeSwapChain::initialSwapChainCreated = false; bool XeSwapChain::initialSwapChainCreated = false;

View file

@ -4,10 +4,14 @@
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
#include <array>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <limits>
#include <memory> #include <memory>
#include <string> #include <set>
#include <vector> #include <stdexcept>
#include <vulkan/vulkan_core.h>
namespace xe { namespace xe {

View file

@ -1,8 +1,4 @@
#include "xe_window.hpp" #include "xe_window.hpp"
#include <GLFW/glfw3.h>
#include <stdexcept>
#include <stdexcept>
namespace xe { namespace xe {

View file

@ -2,9 +2,12 @@
#include <vulkan/vulkan_core.h> #include <vulkan/vulkan_core.h>
#define GLFW_INCLUDE_VULKAN #define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include <GLFW/glfw3.h>
#include <GLFW/glfw3.h>
#include <stdexcept>
#include <string> #include <string>
namespace xe { namespace xe {
class XeWindow { class XeWindow {
@ -33,6 +36,7 @@ class XeWindow {
std::string windowName; std::string windowName;
GLFWwindow *window; GLFWwindow *window;
}; };
} }

1
lib/openal Submodule

@ -0,0 +1 @@
Subproject commit c52df6d78ab7131a543326cd2257f267036754e1

View file

@ -1,22 +1,5 @@
#include "first_app.hpp" #include "first_app.hpp"
#include "xe_camera.hpp"
#include "xe_engine.hpp"
#include "xe_game_object.hpp"
#include "xe_model.hpp"
#include "xe_render_system.hpp"
#include "keyboard_movement_controller.hpp"
#include "simple_renderer.hpp"
#include "xe_sound.hpp"
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp>
#include <glm/gtc/constants.hpp>
#include <array>
#include <string>
namespace app { namespace app {
FirstApp::FirstApp() : xeEngine{WIDTH, HEIGHT, "Xenon Vulkan Engine"} { FirstApp::FirstApp() : xeEngine{WIDTH, HEIGHT, "Xenon Vulkan Engine"} {
@ -45,7 +28,7 @@ void FirstApp::run() {
float frameTime = xeEngine.getFrameTime(); float frameTime = xeEngine.getFrameTime();
cameraController.moveInPlaneXZ(xeEngine.getWindow().getGLFWwindow(), frameTime, viewerObject); cameraController.update(xeEngine.getInput(), viewerObject, frameTime);
xeEngine.getCamera().setViewYXZ(viewerObject.transform.translation, viewerObject.transform.rotation); xeEngine.getCamera().setViewYXZ(viewerObject.transform.translation, viewerObject.transform.rotation);
if(xeEngine.beginFrame()) { if(xeEngine.beginFrame()) {

View file

@ -1,9 +1,16 @@
#pragma once #pragma once
#include "xe_engine.hpp" #include "xe_engine.hpp"
#include "xe_game_object.hpp"
#include "xe_descriptors.hpp"
#include "keyboard_movement_controller.hpp"
#include "simple_renderer.hpp"
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp>
#include <glm/gtc/constants.hpp>
#include <array>
#include <string>
#include <memory> #include <memory>
#include <vector> #include <vector>

View file

@ -1,17 +1,13 @@
#include "keyboard_movement_controller.hpp" #include "keyboard_movement_controller.hpp"
#include <glm/common.hpp>
#include <glm/fwd.hpp>
#include <glm/geometric.hpp>
#include <limits>
namespace app { namespace app {
void KeyboardMovementController::moveInPlaneXZ(GLFWwindow* window, float dt, xe::XeGameObject& gameObject) { void KeyboardMovementController::update(xe::XeInput &input, xe::XeGameObject& gameObject, float dt) {
glm::vec3 rotate{0}; glm::vec3 rotate{0};
if(glfwGetKey(window, keys.lookRight) == GLFW_PRESS) rotate.y += 1.f; if(input.isKeyPressed(keys.lookRight)) rotate.y += 1.f;
if(glfwGetKey(window, keys.lookLeft) == GLFW_PRESS) rotate.y -= 1.f; if(input.isKeyPressed(keys.lookLeft)) rotate.y -= 1.f;
if(glfwGetKey(window, keys.lookUp) == GLFW_PRESS) rotate.x -= 1.f; if(input.isKeyPressed(keys.lookUp)) rotate.x -= 1.f;
if(glfwGetKey(window, keys.lookDown) == GLFW_PRESS) rotate.x += 1.f; if(input.isKeyPressed(keys.lookDown)) rotate.x += 1.f;
if (glm::dot(rotate, rotate) > std::numeric_limits<float>::epsilon()) { if (glm::dot(rotate, rotate) > std::numeric_limits<float>::epsilon()) {
gameObject.transform.rotation += lookSpeed * dt * glm::normalize(rotate); gameObject.transform.rotation += lookSpeed * dt * glm::normalize(rotate);
@ -26,12 +22,12 @@ void KeyboardMovementController::moveInPlaneXZ(GLFWwindow* window, float dt, xe:
const glm::vec3 upDir{0.f, 01.f, 0.f}; const glm::vec3 upDir{0.f, 01.f, 0.f};
glm::vec3 moveDir{0}; glm::vec3 moveDir{0};
if(glfwGetKey(window, keys.moveForward) == GLFW_PRESS) moveDir += forwardDir; if(input.isKeyPressed(keys.moveForward)) moveDir += forwardDir;
if(glfwGetKey(window, keys.moveBackward) == GLFW_PRESS) moveDir -= forwardDir; if(input.isKeyPressed(keys.moveBackward)) moveDir -= forwardDir;
if(glfwGetKey(window, keys.moveRight) == GLFW_PRESS) moveDir += rightDir; if(input.isKeyPressed(keys.moveRight)) moveDir += rightDir;
if(glfwGetKey(window, keys.moveLeft) == GLFW_PRESS) moveDir -= rightDir; if(input.isKeyPressed(keys.moveLeft)) moveDir -= rightDir;
if(glfwGetKey(window, keys.moveUp) == GLFW_PRESS) moveDir += upDir; if(input.isKeyPressed(keys.moveUp)) moveDir += upDir;
if(glfwGetKey(window, keys.moveDown) == GLFW_PRESS) moveDir -= upDir; if(input.isKeyPressed(keys.moveDown)) moveDir -= upDir;
if (glm::dot(moveDir, moveDir) > std::numeric_limits<float>::epsilon()) { if (glm::dot(moveDir, moveDir) > std::numeric_limits<float>::epsilon()) {
gameObject.transform.translation += moveSpeed * dt * glm::normalize(moveDir); gameObject.transform.translation += moveSpeed * dt * glm::normalize(moveDir);

View file

@ -1,27 +1,31 @@
#pragma once #pragma once
#include "xe_game_object.hpp" #include "xe_game_object.hpp"
#include "xe_window.hpp" #include "xe_input.hpp"
#include <GLFW/glfw3.h>
#include <glm/common.hpp>
#include <glm/fwd.hpp>
#include <glm/geometric.hpp>
#include <limits>
namespace app { namespace app {
class KeyboardMovementController { class KeyboardMovementController {
public: public:
struct KeyMappings { struct KeyMappings {
int moveLeft = GLFW_KEY_A; int moveLeft = KEY_A;
int moveRight = GLFW_KEY_D; int moveRight = KEY_D;
int moveForward = GLFW_KEY_W; int moveForward = KEY_W;
int moveBackward = GLFW_KEY_S; int moveBackward = KEY_S;
int moveUp = GLFW_KEY_E; int moveUp = KEY_E;
int moveDown = GLFW_KEY_Q; int moveDown = KEY_Q;
int lookLeft = GLFW_KEY_LEFT; int lookLeft = KEY_LEFT;
int lookRight = GLFW_KEY_RIGHT; int lookRight = KEY_RIGHT;
int lookUp = GLFW_KEY_UP; int lookUp = KEY_UP;
int lookDown = GLFW_KEY_DOWN; int lookDown = KEY_DOWN;
}; };
void moveInPlaneXZ(GLFWwindow* window, float dt, xe::XeGameObject& gameObject); void update(xe::XeInput &input, xe::XeGameObject& gameObject, float dt);
KeyMappings keys{}; KeyMappings keys{};
float moveSpeed{3.f}; float moveSpeed{3.f};

View file

@ -1,7 +1,4 @@
#include "simple_renderer.hpp" #include "simple_renderer.hpp"
#include "xe_render_system.hpp"
#include <iostream>
namespace app { namespace app {

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "xe_render_system.hpp" #include "xe_render_system.hpp"
#include <string> #include <string>
namespace app { namespace app {