Merge pull request #34 from A-lex-Ra/main

Лёгкий рефакторинг, взят курс на избавление от define`ов
This commit is contained in:
MihailRis 2023-12-04 18:05:25 +03:00 committed by GitHub
commit 24b3048c73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 124 additions and 113 deletions

View File

@ -98,7 +98,7 @@ void Engine::mainloop() {
Window::swapInterval(settings.display.swapInterval); Window::swapInterval(settings.display.swapInterval);
Window::swapBuffers(); Window::swapBuffers();
Events::pullEvents(); Events::pollEvents();
} }
} }

View File

@ -3,12 +3,12 @@
#include "../typedefs.h" #include "../typedefs.h"
#define BLOCK_DIR_X 0x1 const int BLOCK_DIR_X = 0x1;
#define BLOCK_DIR_Y 0x0 const int BLOCK_DIR_Y = 0x0;
#define BLOCK_DIR_Z 0x2 const int BLOCK_DIR_Z = 0x2;
// limited to 16 block orientations // limited to 16 block orientations
#define BLOCK_ROT_MASK 0xF const int BLOCK_ROT_MASK = 0xF;
struct voxel { struct voxel {
blockid_t id; blockid_t id;

View File

@ -1,8 +1,12 @@
#include <iostream>
#include "Events.h" #include "Events.h"
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <string.h> #include <string.h>
const short KEYS_BUFFER_SIZE = 1032;
const short _MOUSE_KEYS_OFFSET = 1024;
bool* Events::_keys; bool* Events::_keys;
uint* Events::_frames; uint* Events::_frames;
uint Events::_current = 0; uint Events::_current = 0;
@ -18,11 +22,11 @@ std::vector<int> Events::pressedKeys;
std::unordered_map<std::string, Binding> Events::bindings; std::unordered_map<std::string, Binding> Events::bindings;
int Events::initialize(){ int Events::initialize(){
_keys = new bool[1032]; _keys = new bool[KEYS_BUFFER_SIZE];
_frames = new uint[1032]; _frames = new uint[KEYS_BUFFER_SIZE];
memset(_keys, false, 1032*sizeof(bool)); memset(_keys, false, KEYS_BUFFER_SIZE*sizeof(bool));
memset(_frames, 0, 1032*sizeof(uint)); memset(_frames, 0, KEYS_BUFFER_SIZE*sizeof(uint));
return 0; return 0;
} }
@ -32,26 +36,28 @@ void Events::finalize(){
delete[] _frames; delete[] _frames;
} }
// Returns bool repr. of key state
bool Events::pressed(int keycode){ 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 false;
}
return _keys[keycode]; return _keys[keycode];
} }
// Returns bool repr. of key state
bool Events::jpressed(int keycode){ bool Events::jpressed(int keycode){
if (keycode < 0 || keycode >= _MOUSE_BUTTONS) return Events::pressed(keycode) && _frames[keycode] == _current;
return false;
return _keys[keycode] && _frames[keycode] == _current;
} }
// Returns bool repr. of mouse key state
bool Events::clicked(int button){ bool Events::clicked(int button){
int index = _MOUSE_BUTTONS+button; return Events::pressed(_MOUSE_KEYS_OFFSET + button);
return _keys[index];
} }
// Returns bool repr. of mouse key state
bool Events::jclicked(int button){ bool Events::jclicked(int button){
int index = _MOUSE_BUTTONS+button; return Events::jpressed(_MOUSE_KEYS_OFFSET + button);
return _keys[index] && _frames[index] == _current;
} }
void Events::toggleCursor(){ void Events::toggleCursor(){
@ -59,7 +65,7 @@ void Events::toggleCursor(){
Window::setCursorMode(_cursor_locked ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL); Window::setCursorMode(_cursor_locked ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
} }
void Events::pullEvents(){ void Events::pollEvents(){
_current++; _current++;
deltaX = 0.0f; deltaX = 0.0f;
deltaY = 0.0f; deltaY = 0.0f;

View File

@ -10,6 +10,9 @@
typedef unsigned int uint; typedef unsigned int uint;
extern const short KEYS_BUFFER_SIZE;
extern const short _MOUSE_KEYS_OFFSET;
class Events { class Events {
public: public:
static bool* _keys; static bool* _keys;
@ -28,7 +31,7 @@ public:
static int initialize(); static int initialize();
static void finalize(); static void finalize();
static void pullEvents(); static void pollEvents();
static bool pressed(int keycode); static bool pressed(int keycode);
static bool jpressed(int keycode); static bool jpressed(int keycode);
@ -43,6 +46,4 @@ public:
static bool jactive(std::string name); static bool jactive(std::string name);
}; };
#define _MOUSE_BUTTONS 1024
#endif /* WINDOW_EVENTS_H_ */ #endif /* WINDOW_EVENTS_H_ */

View File

@ -34,23 +34,27 @@ void cursor_position_callback(GLFWwindow*, double xpos, double ypos) {
void mouse_button_callback(GLFWwindow*, int button, int action, int) { void mouse_button_callback(GLFWwindow*, int button, int action, int) {
if (action == GLFW_PRESS) { if (action == GLFW_PRESS) {
Events::_keys[_MOUSE_BUTTONS + button] = true; // Unsafe assignments! (no checks)
Events::_frames[_MOUSE_BUTTONS + button] = Events::_current; Events::_keys[_MOUSE_KEYS_OFFSET + button] = true;
Events::_frames[_MOUSE_KEYS_OFFSET + button] = Events::_current;
} }
else if (action == GLFW_RELEASE) { else if (action == GLFW_RELEASE) {
Events::_keys[_MOUSE_BUTTONS + button] = false; // Unsafe assignments! (no checks)
Events::_frames[_MOUSE_BUTTONS + button] = Events::_current; 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*/) { void key_callback(GLFWwindow*, int key, int scancode, int action, int /*mode*/) {
if (key == GLFW_KEY_UNKNOWN) return; if (key == GLFW_KEY_UNKNOWN) return;
if (action == GLFW_PRESS) { if (action == GLFW_PRESS) {
// Unsafe assignments! (no checks)
Events::_keys[key] = true; Events::_keys[key] = true;
Events::_frames[key] = Events::_current; Events::_frames[key] = Events::_current;
Events::pressedKeys.push_back(key); Events::pressedKeys.push_back(key);
} }
else if (action == GLFW_RELEASE) { else if (action == GLFW_RELEASE) {
// Unsafe assignments! (no checks)
Events::_keys[key] = false; Events::_keys[key] = false;
Events::_frames[key] = Events::_current; Events::_frames[key] = Events::_current;
} }

View File

@ -3,96 +3,96 @@
#include <string> #include <string>
namespace keycode { struct keycode {
extern int ENTER; static int ENTER;
extern int TAB; static int TAB;
extern int SPACE; static int SPACE;
extern int BACKSPACE; static int BACKSPACE;
extern int LEFT_CONTROL; static int LEFT_CONTROL;
extern int LEFT_SHIFT; static int LEFT_SHIFT;
extern int LEFT_ALT; static int LEFT_ALT;
extern int RIGHT_CONTROL; static int RIGHT_CONTROL;
extern int RIGHT_SHIFT; static int RIGHT_SHIFT;
extern int RIGHT_ALT; static int RIGHT_ALT;
extern int ESCAPE; static int ESCAPE;
extern int CAPS_LOCK; static int CAPS_LOCK;
extern int LEFT; static int LEFT;
extern int RIGHT; static int RIGHT;
extern int DOWN; static int DOWN;
extern int UP; static int UP;
extern int F1; static int F1;
extern int F2; static int F2;
extern int F3; static int F3;
extern int F4; static int F4;
extern int F5; static int F5;
extern int F6; static int F6;
extern int F7; static int F7;
extern int F8; static int F8;
extern int F9; static int F9;
extern int F10; static int F10;
extern int F11; static int F11;
extern int F12; static int F12;
extern int A; static int A;
extern int B; static int B;
extern int C; static int C;
extern int D; static int D;
extern int E; static int E;
extern int F; static int F;
extern int G; static int G;
extern int H; static int H;
extern int I; static int I;
extern int J; static int J;
extern int K; static int K;
extern int L; static int L;
extern int M; static int M;
extern int N; static int N;
extern int O; static int O;
extern int P; static int P;
extern int Q; static int Q;
extern int R; static int R;
extern int S; static int S;
extern int T; static int T;
extern int U; static int U;
extern int V; static int V;
extern int W; static int W;
extern int X; static int X;
extern int Y; static int Y;
extern int Z; static int Z;
extern int NUM_0; static int NUM_0;
extern int NUM_1; static int NUM_1;
extern int NUM_2; static int NUM_2;
extern int NUM_3; static int NUM_3;
extern int NUM_4; static int NUM_4;
extern int NUM_5; static int NUM_5;
extern int NUM_6; static int NUM_6;
extern int NUM_7; static int NUM_7;
extern int NUM_8; static int NUM_8;
extern int NUM_9; static int NUM_9;
extern int MENU; static int MENU;
extern int PAUSE; static int PAUSE;
extern int INSERT; static int INSERT;
extern int LEFT_SUPER; static int LEFT_SUPER;
extern int RIGHT_SUPER; static int RIGHT_SUPER;
extern int DELETE; static int DELETE;
extern int PAGE_UP; static int PAGE_UP;
extern int PAGE_DOWN; static int PAGE_DOWN;
extern int HOME; static int HOME;
extern int END; static int END;
extern int PRINT_SCREEN; static int PRINT_SCREEN;
extern int NUM_LOCK; static int NUM_LOCK;
extern int LEFT_BRACKET; static int LEFT_BRACKET;
extern int RIGHT_BRACKET; static int RIGHT_BRACKET;
extern const std::string name(int code); static const std::string name(int code);
} };
namespace mousecode { struct mousecode {
extern int BUTTON_1; static int BUTTON_1;
extern int BUTTON_2; static int BUTTON_2;
extern int BUTTON_3; static int BUTTON_3;
extern const std::string name(int code); static const std::string name(int code);
} };
enum class inputtype { enum class inputtype {
keyboard, keyboard,