Skip to content

Commit

Permalink
Merge pull request #4 from Jaskowicz1/input_manager
Browse files Browse the repository at this point in the history
feat: added controller support and project settings.
  • Loading branch information
Jaskowicz1 authored Dec 1, 2024
2 parents 546a7a6 + 8ab9adf commit 04a78a5
Show file tree
Hide file tree
Showing 13 changed files with 2,937 additions and 130 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ testing
*.xcscheme

# Apple stuff.
.DS_Store
.DS_Store

# Any zips because not needed
*.zip
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Define CMake Minimum.
cmake_minimum_required(VERSION 3.25)
cmake_minimum_required(VERSION 3.20)
# Project
project("Hexwave" VERSION 0.2 DESCRIPTION "An engine designed to make Interactive Films!")

Expand Down
2,046 changes: 2,046 additions & 0 deletions extras/gamecontrollerdb.txt

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions include/engine/project_settings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <string>
#include "utilities/json_utilities.h"

class project_settings {

public:
project_settings() = default;

void render_window();

bool show_window;

std::string start_id{};

std::string normal_button_path{};
std::string hovered_button_path{};
std::string selected_button_path{};

std::string button_sound_path{};

project_settings& fill_from_json(const json* j);

json to_json() const;

};
18 changes: 13 additions & 5 deletions include/engine/utilities/file_management.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#pragma once

#include "video_manager.h"

#ifdef _WIN32
#include <windows.h>
#endif

#include "video_manager.h"
#include "project_settings.h"

#include <glad/gl.h>

namespace utilities {

constexpr std::string_view hexwave_project_ext = ".hexw";
Expand Down Expand Up @@ -42,23 +45,28 @@ class linux_file {
};
#endif

// Simple helper function to load an image into a OpenGL texture with common settings
bool LoadTextureFromFile(const std::string& filename, GLuint* out_texture, int* out_width = nullptr, int* out_height = nullptr);

/**
* @brief Save the current project
*
* @param manager The video manager with the videos.
* @param settings The project settings to save.
*
* @return bool true if project was able to save
*/
bool save_project(video_manager& manager);
bool save_project(video_manager& manager, project_settings& settings);

/**
* @brief Load a project.
*
* @brief The video manager to load the videos to.
* @param manager The video manager to load the videos to.
* @param settings The project settings to load the settings to.
*
* @return bool true if project was loaded successfully.
*/
bool load_project(video_manager& manager);
bool load_project(video_manager& manager, project_settings& settings);

/**
* @brief Opens a file prompt to retrieve a file path.
Expand Down
113 changes: 113 additions & 0 deletions include/engine/utilities/input_manager.h
Original file line number Diff line number Diff line change
@@ -1 +1,114 @@
#pragma once

#include <functional>
#include <chrono>
#include "GLFW/glfw3.h"

namespace input {

/**
* @brief This is mapped to GLFW's GLFWgamepadstate buttons.
*
* @note This should match every controller. If not, check the gamecontrollerdb.txt in extras
*/
enum controller_inputs {
/* Button Inputs */

CONTROLLER_BUTTON_DOWN = 0,
CONTROLLER_BUTTON_RIGHT = 1,
CONTROLLER_BUTTON_LEFT = 2,
CONTROLLER_BUTTON_UP = 3,
CONTROLLER_BUTTON_LEFT_BUMPER = 4,
CONTROLLER_BUTTON_RIGHT_BUMPER = 5,
CONTROLLER_BUTTON_OPTION = 6,
CONTROLLER_BUTTON_START = 7,
CONTROLLER_BUTTON_SPECIAL = 8, // Xbox button, PS button, etc.
CONTROLLER_BUTTON_LEFT_JOYSTICK = 9,
CONTROLLER_BUTTON_RIGHT_JOYSTICK = 10,
CONTROLLER_BUTTON_DPAD_UP = 11,
CONTROLLER_BUTTON_DPAD_RIGHT = 12,
CONTROLLER_BUTTON_DPAD_DOWN = 13,
CONTROLLER_BUTTON_DPAD_LEFT = 14,

/* Axis Inputs */

INPUT_UP = 15,
INPUT_DOWN = 16,
INPUT_LEFT = 17,
INPUT_RIGHT = 18,
};

enum input_types {
KEYBOARDANDMOUSE,
CONTROLLER,
};

enum controller_types {
XBOX,
PLAYSTATION
};

class input_manager {

public:
/**
* @brief input_manager constructor.
* @param window The window reference to setup the input_manager.
*/
input_manager(GLFWwindow* window);

~input_manager();

void input_loop();

/**
* @brief Keyboard press event
*
* @param key The key that was pressed (look at `GLFW_KEY_`).
*
*/
std::function<void(int key)> on_keyboard_press { nullptr };

/**
* @brief Controller input event
*
* @param input The input of the controller as `controller_inputs`.
*/
std::function<void(controller_inputs input)> on_controller_input { nullptr };

/**
* @brief Controller button press event
*
* @param key The controller button as `controller_inputs`.
*/
std::function<void(controller_inputs key)> on_controller_button_press { nullptr };

std::vector<int> keys_held{};

input_types current_input_type { input::KEYBOARDANDMOUSE };
controller_types current_controller_type { input::XBOX };

private:

/**
* @brief Time of last input.
*
* @note This is to prevent spam of a button.
* @see time_required_between_input
*/
std::chrono::time_point<std::chrono::high_resolution_clock> last_input;

/**
* @brief Time (IN MS) required between last and current input.
*/
uint16_t time_required_between_input { 100 };

/**
* @brief a Dead-Zone for the axis inputs.
*
* @note This prevents the smallest of movements from triggering input.
*/
float dead_zone { 0.2 };
};

}
4 changes: 4 additions & 0 deletions include/engine/video_manager.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#pragma once

#ifdef _WIN32
#include <windows.h>
#endif

#include <vector>
#include <string_view>
#include <string>
Expand Down
26 changes: 24 additions & 2 deletions include/engine/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
#include <iostream>
#include "video_manager.h"
#include "glm/vec2.hpp"
#include "utilities/input_manager.h"
#include <glad/gl.h>

#include "miniaudio/miniaudio.h"
#include "project_settings.h"

class window {

public:
Expand All @@ -18,17 +22,35 @@ class window {

video_manager manager{};

project_settings settings{};

std::unique_ptr<input::input_manager> input_man{nullptr};

void reload_button_textures();

private:

struct GLFWwindow* glfw_window{nullptr};

uint16_t frame_width{0}; // vid_reader.height
uint16_t frame_height{0}; // vid_reader.height

bool testing_export{false};

GLuint video_texture;

glm::vec2 vertices[4] = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}};

void render_window_bar();

ma_result result;
ma_engine engine;

// This is a temp solution to forcing a video to play next frame.
std::string force_video_to_play{};

int button_texture_width = 0;
int button_texture_height = 0;
GLuint button_texture{};
GLuint hovered_button_texture{};
GLuint pressed_button_texture{};

};
Loading

0 comments on commit 04a78a5

Please sign in to comment.