dasdasdasdas

This commit is contained in:
quflax
2023-12-27 15:01:18 +02:00
parent 95c713bc4d
commit 4d281ff03b
8 changed files with 64 additions and 77 deletions
+27 -10
View File
@@ -7,21 +7,19 @@
bool Events::_keys[KEYS_BUFFER_SIZE] = {};
uint Events::_frames[KEYS_BUFFER_SIZE] = {};
uint Events::_current = 0;
float Events::deltaX = 0.0f;
float Events::deltaY = 0.0f;
float Events::x = 0.0f;
float Events::y = 0.0f;
int Events::scroll = 0;
glm::vec2 Events::delta = {};
glm::vec2 Events::cursor = {};
bool Events::cursor_drag = false;
bool Events::_cursor_locked = false;
bool Events::_cursor_started = false;
std::vector<uint> Events::codepoints;
std::vector<int> Events::pressedKeys;
std::unordered_map<std::string, Binding> Events::bindings;
// Returns bool repr. of key state
bool Events::pressed(int keycode){
if (keycode < 0 || keycode >= KEYS_BUFFER_SIZE){
// VERY bad behaviour and it happens constantly! (so console-printing is not a good idea)
bool Events::pressed(int keycode) {
if (keycode < 0 || keycode >= KEYS_BUFFER_SIZE) {
fprintf(stderr, "pressed %i\n", keycode);
return false;
}
return _keys[keycode];
@@ -49,8 +47,8 @@ void Events::toggleCursor(){
void Events::pollEvents(){
_current++;
deltaX = 0.0f;
deltaY = 0.0f;
delta.x = 0.f;
delta.y = 0.f;
scroll = 0;
codepoints.clear();
pressedKeys.clear();
@@ -99,3 +97,22 @@ bool Events::jactive(std::string name) {
}
return found->second.jactive();
}
void Events::setKey(int key, bool b) {
Events::_keys[key] = b;
Events::_frames[key] = Events::_current;
}
void Events::setButton(int button, bool b) {
setKey(_MOUSE_KEYS_OFFSET + button, b);
}
void Events::setPosition(float xpos, float ypos) {
if (Events::cursor_drag) {
Events::delta.x += xpos - Events::cursor.x;
Events::delta.y += ypos - Events::cursor.y;
} else
Events::cursor_drag = true;
Events::cursor.x = xpos;
Events::cursor.y = ypos;
}
+10 -7
View File
@@ -10,7 +10,7 @@
typedef unsigned int uint;
const short KEYS_BUFFER_SIZE = 1032;
const short KEYS_BUFFER_SIZE = 1036;
const short _MOUSE_KEYS_OFFSET = 1024;
class Events {
@@ -18,13 +18,11 @@ public:
static bool _keys[KEYS_BUFFER_SIZE];
static uint _frames[KEYS_BUFFER_SIZE];
static uint _current;
static float deltaX;
static float deltaY;
static float x;
static float y;
static int scroll;
static int scroll;
static glm::vec2 delta;
static glm::vec2 cursor;
static bool cursor_drag;
static bool _cursor_locked;
static bool _cursor_started;
static std::vector<uint> codepoints;
static std::vector<int> pressedKeys;
static std::unordered_map<std::string, Binding> bindings;
@@ -42,6 +40,11 @@ public:
static void bind(std::string name, inputtype type, int code);
static bool active(std::string name);
static bool jactive(std::string name);
static void setKey(int key, bool b);
static void setButton(int button, bool b);
static void setPosition(float xpos, float ypos);
};
#endif /* WINDOW_EVENTS_H_ */
+5 -27
View File
@@ -21,42 +21,21 @@ int Window::posX = 0;
int Window::posY = 0;
void cursor_position_callback(GLFWwindow*, double xpos, double ypos) {
if (Events::_cursor_started) {
Events::deltaX += xpos - Events::x;
Events::deltaY += ypos - Events::y;
}
else {
Events::_cursor_started = true;
}
Events::x = xpos;
Events::y = ypos;
Events::setPosition(xpos, ypos);
}
void mouse_button_callback(GLFWwindow*, int button, int action, int) {
if (action == GLFW_PRESS) {
// Unsafe assignments! (no checks)
Events::_keys[_MOUSE_KEYS_OFFSET + button] = true;
Events::_frames[_MOUSE_KEYS_OFFSET + button] = Events::_current;
}
else if (action == GLFW_RELEASE) {
// Unsafe assignments! (no checks)
Events::_keys[_MOUSE_KEYS_OFFSET + button] = false;
Events::_frames[_MOUSE_KEYS_OFFSET + button] = Events::_current;
}
Events::setButton(button, action == GLFW_PRESS);
}
void key_callback(GLFWwindow*, int key, int scancode, int action, int /*mode*/) {
if (key == GLFW_KEY_UNKNOWN) return;
if (action == GLFW_PRESS) {
// Unsafe assignments! (no checks)
Events::_keys[key] = true;
Events::_frames[key] = Events::_current;
Events::setKey(key, true);
Events::pressedKeys.push_back(key);
}
else if (action == GLFW_RELEASE) {
// Unsafe assignments! (no checks)
Events::_keys[key] = false;
Events::_frames[key] = Events::_current;
Events::setKey(key, false);
}
else if (action == GLFW_REPEAT) {
Events::pressedKeys.push_back(key);
@@ -288,8 +267,7 @@ void Window::toggleFullscreen(){
double xPos, yPos;
glfwGetCursorPos(window, &xPos, &yPos);
Events::x = xPos;
Events::y = yPos;
Events::setPosition(xPos, yPos);
}
bool Window::isFullscreen() {