refactor Events
This commit is contained in:
parent
ba83b27179
commit
911e828602
@ -98,7 +98,7 @@ void Engine::mainloop() {
|
||||
|
||||
Window::swapInterval(settings.display.swapInterval);
|
||||
Window::swapBuffers();
|
||||
Events::pullEvents();
|
||||
Events::pollEvents();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,8 +1,12 @@
|
||||
#include <iostream>
|
||||
#include "Events.h"
|
||||
#include <GL/glew.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <string.h>
|
||||
|
||||
const short KEYS_BUFFER_SIZE = 1032;
|
||||
const short _MOUSE_KEYS_OFFSET = 1024;
|
||||
|
||||
bool* Events::_keys;
|
||||
uint* Events::_frames;
|
||||
uint Events::_current = 0;
|
||||
@ -18,11 +22,11 @@ std::vector<int> Events::pressedKeys;
|
||||
std::unordered_map<std::string, Binding> Events::bindings;
|
||||
|
||||
int Events::initialize(){
|
||||
_keys = new bool[1032];
|
||||
_frames = new uint[1032];
|
||||
_keys = new bool[KEYS_BUFFER_SIZE];
|
||||
_frames = new uint[KEYS_BUFFER_SIZE];
|
||||
|
||||
memset(_keys, false, 1032*sizeof(bool));
|
||||
memset(_frames, 0, 1032*sizeof(uint));
|
||||
memset(_keys, false, KEYS_BUFFER_SIZE*sizeof(bool));
|
||||
memset(_frames, 0, KEYS_BUFFER_SIZE*sizeof(uint));
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -32,26 +36,28 @@ void Events::finalize(){
|
||||
delete[] _frames;
|
||||
}
|
||||
|
||||
// Returns bool repr. of key state
|
||||
bool Events::pressed(int keycode){
|
||||
if (keycode < 0 || keycode >= _MOUSE_BUTTONS)
|
||||
if (keycode < 0 || keycode >= KEYS_BUFFER_SIZE){
|
||||
// VERY bad behaviour and it happens constantly! (so console-printing is not a good idea)
|
||||
return false;
|
||||
}
|
||||
return _keys[keycode];
|
||||
}
|
||||
|
||||
bool Events::jpressed(int keycode){
|
||||
if (keycode < 0 || keycode >= _MOUSE_BUTTONS)
|
||||
return false;
|
||||
return _keys[keycode] && _frames[keycode] == _current;
|
||||
// Returns bool repr. of key state
|
||||
bool Events::jpressed(int keycode){
|
||||
return Events::pressed(keycode) && _frames[keycode] == _current;
|
||||
}
|
||||
|
||||
// Returns bool repr. of mouse key state
|
||||
bool Events::clicked(int button){
|
||||
int index = _MOUSE_BUTTONS+button;
|
||||
return _keys[index];
|
||||
return Events::pressed(_MOUSE_KEYS_OFFSET + button);
|
||||
}
|
||||
|
||||
// Returns bool repr. of mouse key state
|
||||
bool Events::jclicked(int button){
|
||||
int index = _MOUSE_BUTTONS+button;
|
||||
return _keys[index] && _frames[index] == _current;
|
||||
return Events::jpressed(_MOUSE_KEYS_OFFSET + button);
|
||||
}
|
||||
|
||||
void Events::toggleCursor(){
|
||||
@ -59,7 +65,7 @@ void Events::toggleCursor(){
|
||||
Window::setCursorMode(_cursor_locked ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
|
||||
}
|
||||
|
||||
void Events::pullEvents(){
|
||||
void Events::pollEvents(){
|
||||
_current++;
|
||||
deltaX = 0.0f;
|
||||
deltaY = 0.0f;
|
||||
|
||||
@ -10,6 +10,9 @@
|
||||
|
||||
typedef unsigned int uint;
|
||||
|
||||
extern const short KEYS_BUFFER_SIZE;
|
||||
extern const short _MOUSE_KEYS_OFFSET;
|
||||
|
||||
class Events {
|
||||
public:
|
||||
static bool* _keys;
|
||||
@ -28,7 +31,7 @@ public:
|
||||
|
||||
static int initialize();
|
||||
static void finalize();
|
||||
static void pullEvents();
|
||||
static void pollEvents();
|
||||
|
||||
static bool pressed(int keycode);
|
||||
static bool jpressed(int keycode);
|
||||
@ -43,6 +46,4 @@ public:
|
||||
static bool jactive(std::string name);
|
||||
};
|
||||
|
||||
#define _MOUSE_BUTTONS 1024
|
||||
|
||||
#endif /* WINDOW_EVENTS_H_ */
|
||||
|
||||
@ -34,23 +34,27 @@ void cursor_position_callback(GLFWwindow*, double xpos, double ypos) {
|
||||
|
||||
void mouse_button_callback(GLFWwindow*, int button, int action, int) {
|
||||
if (action == GLFW_PRESS) {
|
||||
Events::_keys[_MOUSE_BUTTONS + button] = true;
|
||||
Events::_frames[_MOUSE_BUTTONS + button] = Events::_current;
|
||||
// Unsafe assignments! (no checks)
|
||||
Events::_keys[_MOUSE_KEYS_OFFSET + button] = true;
|
||||
Events::_frames[_MOUSE_KEYS_OFFSET + button] = Events::_current;
|
||||
}
|
||||
else if (action == GLFW_RELEASE) {
|
||||
Events::_keys[_MOUSE_BUTTONS + button] = false;
|
||||
Events::_frames[_MOUSE_BUTTONS + button] = Events::_current;
|
||||
// Unsafe assignments! (no checks)
|
||||
Events::_keys[_MOUSE_KEYS_OFFSET + button] = false;
|
||||
Events::_frames[_MOUSE_KEYS_OFFSET + button] = Events::_current;
|
||||
}
|
||||
}
|
||||
|
||||
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::pressedKeys.push_back(key);
|
||||
}
|
||||
else if (action == GLFW_RELEASE) {
|
||||
// Unsafe assignments! (no checks)
|
||||
Events::_keys[key] = false;
|
||||
Events::_frames[key] = Events::_current;
|
||||
}
|
||||
|
||||
@ -3,96 +3,96 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace keycode {
|
||||
extern int ENTER;
|
||||
extern int TAB;
|
||||
extern int SPACE;
|
||||
extern int BACKSPACE;
|
||||
extern int LEFT_CONTROL;
|
||||
extern int LEFT_SHIFT;
|
||||
extern int LEFT_ALT;
|
||||
extern int RIGHT_CONTROL;
|
||||
extern int RIGHT_SHIFT;
|
||||
extern int RIGHT_ALT;
|
||||
extern int ESCAPE;
|
||||
extern int CAPS_LOCK;
|
||||
extern int LEFT;
|
||||
extern int RIGHT;
|
||||
extern int DOWN;
|
||||
extern int UP;
|
||||
extern int F1;
|
||||
extern int F2;
|
||||
extern int F3;
|
||||
extern int F4;
|
||||
extern int F5;
|
||||
extern int F6;
|
||||
extern int F7;
|
||||
extern int F8;
|
||||
extern int F9;
|
||||
extern int F10;
|
||||
extern int F11;
|
||||
extern int F12;
|
||||
extern int A;
|
||||
extern int B;
|
||||
extern int C;
|
||||
extern int D;
|
||||
extern int E;
|
||||
extern int F;
|
||||
extern int G;
|
||||
extern int H;
|
||||
extern int I;
|
||||
extern int J;
|
||||
extern int K;
|
||||
extern int L;
|
||||
extern int M;
|
||||
extern int N;
|
||||
extern int O;
|
||||
extern int P;
|
||||
extern int Q;
|
||||
extern int R;
|
||||
extern int S;
|
||||
extern int T;
|
||||
extern int U;
|
||||
extern int V;
|
||||
extern int W;
|
||||
extern int X;
|
||||
extern int Y;
|
||||
extern int Z;
|
||||
extern int NUM_0;
|
||||
extern int NUM_1;
|
||||
extern int NUM_2;
|
||||
extern int NUM_3;
|
||||
extern int NUM_4;
|
||||
extern int NUM_5;
|
||||
extern int NUM_6;
|
||||
extern int NUM_7;
|
||||
extern int NUM_8;
|
||||
extern int NUM_9;
|
||||
extern int MENU;
|
||||
extern int PAUSE;
|
||||
extern int INSERT;
|
||||
extern int LEFT_SUPER;
|
||||
extern int RIGHT_SUPER;
|
||||
extern int DELETE;
|
||||
extern int PAGE_UP;
|
||||
extern int PAGE_DOWN;
|
||||
extern int HOME;
|
||||
extern int END;
|
||||
extern int PRINT_SCREEN;
|
||||
extern int NUM_LOCK;
|
||||
extern int LEFT_BRACKET;
|
||||
extern int RIGHT_BRACKET;
|
||||
struct keycode {
|
||||
static int ENTER;
|
||||
static int TAB;
|
||||
static int SPACE;
|
||||
static int BACKSPACE;
|
||||
static int LEFT_CONTROL;
|
||||
static int LEFT_SHIFT;
|
||||
static int LEFT_ALT;
|
||||
static int RIGHT_CONTROL;
|
||||
static int RIGHT_SHIFT;
|
||||
static int RIGHT_ALT;
|
||||
static int ESCAPE;
|
||||
static int CAPS_LOCK;
|
||||
static int LEFT;
|
||||
static int RIGHT;
|
||||
static int DOWN;
|
||||
static int UP;
|
||||
static int F1;
|
||||
static int F2;
|
||||
static int F3;
|
||||
static int F4;
|
||||
static int F5;
|
||||
static int F6;
|
||||
static int F7;
|
||||
static int F8;
|
||||
static int F9;
|
||||
static int F10;
|
||||
static int F11;
|
||||
static int F12;
|
||||
static int A;
|
||||
static int B;
|
||||
static int C;
|
||||
static int D;
|
||||
static int E;
|
||||
static int F;
|
||||
static int G;
|
||||
static int H;
|
||||
static int I;
|
||||
static int J;
|
||||
static int K;
|
||||
static int L;
|
||||
static int M;
|
||||
static int N;
|
||||
static int O;
|
||||
static int P;
|
||||
static int Q;
|
||||
static int R;
|
||||
static int S;
|
||||
static int T;
|
||||
static int U;
|
||||
static int V;
|
||||
static int W;
|
||||
static int X;
|
||||
static int Y;
|
||||
static int Z;
|
||||
static int NUM_0;
|
||||
static int NUM_1;
|
||||
static int NUM_2;
|
||||
static int NUM_3;
|
||||
static int NUM_4;
|
||||
static int NUM_5;
|
||||
static int NUM_6;
|
||||
static int NUM_7;
|
||||
static int NUM_8;
|
||||
static int NUM_9;
|
||||
static int MENU;
|
||||
static int PAUSE;
|
||||
static int INSERT;
|
||||
static int LEFT_SUPER;
|
||||
static int RIGHT_SUPER;
|
||||
static int DELETE;
|
||||
static int PAGE_UP;
|
||||
static int PAGE_DOWN;
|
||||
static int HOME;
|
||||
static int END;
|
||||
static int PRINT_SCREEN;
|
||||
static int NUM_LOCK;
|
||||
static int LEFT_BRACKET;
|
||||
static int RIGHT_BRACKET;
|
||||
|
||||
extern const std::string name(int code);
|
||||
}
|
||||
static const std::string name(int code);
|
||||
};
|
||||
|
||||
namespace mousecode {
|
||||
extern int BUTTON_1;
|
||||
extern int BUTTON_2;
|
||||
extern int BUTTON_3;
|
||||
struct mousecode {
|
||||
static int BUTTON_1;
|
||||
static int BUTTON_2;
|
||||
static int BUTTON_3;
|
||||
|
||||
extern const std::string name(int code);
|
||||
}
|
||||
static const std::string name(int code);
|
||||
};
|
||||
|
||||
enum class inputtype {
|
||||
keyboard,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user