working on callbacks / input

main
Brett 2023-12-15 14:44:56 -05:00
parent 20748a8e60
commit 0c8630ccff
3 changed files with 106 additions and 4 deletions

100
include/blt/gfx/input.h Normal file
View File

@ -0,0 +1,100 @@
#pragma once
/*
* Created by Brett on 15/12/23.
* Licensed under GNU General Public License V3.0
* See LICENSE file for license detail
*/
#ifndef BLT_WITH_GRAPHICS_TEMPLATE_INPUT_H
#define BLT_WITH_GRAPHICS_TEMPLATE_INPUT_H
#include <cstdint>
#include <algorithm>
#include <cstring>
namespace blt::gfx
{
enum class KEY_STATE : std::uint8_t
{
RELEASE = 0, PRESS = 1, REPEAT = 2
};
enum class MOUSE_STATE : std::uint8_t
{
RELEASE = 0, PRESS = 1
};
class input_manager
{
private:
KEY_STATE* key_state;
MOUSE_STATE* mouse_state;
std::size_t key_size = 256;
std::size_t mouse_size = 16;
void expand_keys(std::size_t min)
{
auto new_size = std::max(key_size + 256, min);
auto* new_key = new KEY_STATE[new_size];
std::memset(new_key, 0, new_size * sizeof(KEY_STATE));
std::memcpy(new_key, key_state, key_size * sizeof(KEY_STATE));
delete[] key_state;
key_state = new_key;
key_size = new_size;
}
void expand_mouse(std::size_t min)
{
auto new_size = std::max(mouse_size + 16, min);
auto* new_mouse = new MOUSE_STATE[new_size];
std::memset(new_mouse, 0, new_size * sizeof(MOUSE_STATE));
std::memcpy(new_mouse, mouse_state, mouse_size * sizeof(MOUSE_STATE));
delete[] mouse_state;
mouse_state = new_mouse;
mouse_size = new_size;
}
public:
input_manager()
{
key_state = new KEY_STATE[key_size];
mouse_state = new MOUSE_STATE[mouse_size];
std::memset(key_state, 0, key_size * sizeof(KEY_STATE));
std::memset(mouse_state, 0, mouse_size * sizeof(MOUSE_STATE));
}
KEY_STATE& key(std::size_t key)
{
if (key >= key_size)
expand_keys(key);
return key_state[key];
}
bool isKeyPressed(std::size_t key){
if (key >= key_size)
expand_keys(key);
return key_state[key] != KEY_STATE::RELEASE;
}
MOUSE_STATE& mouse(std::size_t mouse)
{
if (mouse >= mouse_size)
expand_mouse(mouse);
return mouse_state[mouse];
}
bool isMousePressed(std::size_t mouse){
if (mouse >= mouse_size)
expand_mouse(mouse);
return mouse_state[mouse] != MOUSE_STATE::RELEASE;
}
~input_manager()
{
delete[] key_state;
delete[] mouse_state;
}
};
}
#endif //BLT_WITH_GRAPHICS_TEMPLATE_INPUT_H

@ -1 +1 @@
Subproject commit d589056a8df584bebc8cdf0bc077613a56367221
Subproject commit bda044de1eda8a556c5174461227ae12c8c891d3

View File

@ -6,6 +6,7 @@
#include <blt/gfx/window.h>
#include <blt/std/assert.h>
#include <blt/std/logging.h>
#include <blt/gfx/input.h>
void error_callback(int error, const char* description)
{
@ -18,9 +19,10 @@ namespace blt::gfx
// because we aren't meant to have multiple GLFW windows (especially with GLAD) we will keep the window as global state.1
struct
{
GLFWwindow* window;
std::int32_t width;
std::int32_t height;
GLFWwindow* window = nullptr;
input_manager inputManager;
std::int32_t width = 0;
std::int32_t height = 0;
} window_state;
void create_callbacks()