merge in input

This commit is contained in:
tylermurphy534 2022-09-25 12:13:07 -04:00
commit ffd2d8220d
22 changed files with 291 additions and 117 deletions

View file

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

View file

@ -1,14 +1,19 @@
#pragma once
#include "xe_window.hpp"
#include "xe_device.hpp"
#include "xe_renderer.hpp"
#include "xe_camera.hpp"
#include "xe_descriptors.hpp"
#include "xe_image.hpp"
#include "xe_input.hpp"
#include "xe_sound.hpp"
#include <chrono>
#include <string>
#include <iostream>
#include <AL/alc.h>
#include <AL/alut.h>
namespace xe {
class Engine {
@ -22,7 +27,7 @@ class Engine {
Engine(const Engine&) = delete;
Engine operator=(const Engine&) = delete;
Window& getWindow() {return xeWindow;}
Input& getInput() {return xeInput;}
Camera& getCamera() {return xeCamera;}
std::shared_ptr<Model> loadModelFromFile(const std::string &filename);
@ -44,6 +49,7 @@ class Engine {
Device xeDevice;
Renderer xeRenderer;
Camera xeCamera;
Input xeInput;
std::chrono::_V2::system_clock::time_point currentTime;
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 Input* _instance;
Input::Input(Window& window) : window{window} {
glfwSetKeyCallback(window.getGLFWwindow(), Input::key_callback);
glfwSetMouseButtonCallback(window.getGLFWwindow(), Input::mouse_callback);
_instance = this;
}
bool Input::isKeyPressed(int key) {
return glfwGetKey(window.getGLFWwindow(), key) == GLFW_PRESS;
}
bool Input::wasKeyPressed(int key) {
if(_pressed[key] == true) {
_pressed[key] = false;
return true;
}
return false;
}
bool Input::wasKeyReleased(int key) {
if(_released[key] == true) {
_released[key] = false;
return true;
}
return false;
}
void Input::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 Input::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 Input {
public:
bool isKeyPressed(int key);
bool wasKeyPressed(int key);
bool wasKeyReleased(int key);
private:
Input(Window& window);
~Input() {};
void setIsKeyDown(int key, bool isDown);
std::map<int, bool> _pressed;
std::map<int, bool> _released;
Window &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 Engine;
};
}
#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_model.hpp"
#include <cstdint>
#include <fstream>
#include <stdexcept>

View file

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

View file

@ -1,19 +1,5 @@
#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 {
RenderSystem::RenderSystem(

View file

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

View file

@ -1,17 +1,5 @@
#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 {
Renderer::Renderer(Window& window, Device& device) : xeWindow{window}, xeDevice{device} {

View file

@ -1,13 +1,16 @@
#pragma once
#include "xe_swap_chain.hpp"
#include "xe_window.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 <vector>
#include <array>
#include <cassert>
#include <stdexcept>
#include <memory>
#include <vulkan/vulkan_core.h>
namespace xe {
class Renderer {

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,22 +1,5 @@
#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 {
FirstApp::FirstApp() : xeEngine{WIDTH, HEIGHT, "Xenon Vulkan Engine"} {
@ -45,7 +28,7 @@ void FirstApp::run() {
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);
if(xeEngine.beginFrame()) {

View file

@ -1,9 +1,16 @@
#pragma once
#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 <vector>

View file

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

View file

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

View file

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

View file

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