Merge pull request #74 from QuFlax/my

Fix and Optimize Events
This commit is contained in:
MihailRis
2023-12-27 18:07:58 +03:00
committed by GitHub
8 changed files with 69 additions and 104 deletions
+4 -4
View File
@@ -454,8 +454,8 @@ void WorldFiles::writePlayer(Player* player){
posarr.put(position.z); posarr.put(position.z);
json::JArray& rotarr = root.putArray("rotation"); json::JArray& rotarr = root.putArray("rotation");
rotarr.put(player->camX); rotarr.put(player->cam.x);
rotarr.put(player->camY); rotarr.put(player->cam.y);
root.put("flight", player->flight); root.put("flight", player->flight);
root.put("noclip", player->noclip); root.put("noclip", player->noclip);
@@ -479,8 +479,8 @@ bool WorldFiles::readPlayer(Player* player) {
player->camera->position = position; player->camera->position = position;
json::JArray* rotarr = root->arr("rotation"); json::JArray* rotarr = root->arr("rotation");
player->camX = rotarr->num(0); player->cam.x = rotarr->num(0);
player->camY = rotarr->num(1); player->cam.y = rotarr->num(1);
root->flag("flight", player->flight); root->flag("flight", player->flight);
root->flag("noclip", player->noclip); root->flag("noclip", player->noclip);
+4 -8
View File
@@ -40,10 +40,8 @@ PagesControl* GUI::getMenu() {
} }
void GUI::actMouse(float delta) { void GUI::actMouse(float delta) {
int mx = Events::x;
int my = Events::y;
auto hover = container->getAt(vec2(mx, my), nullptr); auto hover = container->getAt(Events::cursor, nullptr);
if (this->hover && this->hover != hover) { if (this->hover && this->hover != hover) {
this->hover->hover(false); this->hover->hover(false);
} }
@@ -58,7 +56,7 @@ void GUI::actMouse(float delta) {
if (Events::jclicked(0)) { if (Events::jclicked(0)) {
if (pressed == nullptr && this->hover) { if (pressed == nullptr && this->hover) {
pressed = hover; pressed = hover;
pressed->click(this, mx, my); pressed->click(this, Events::cursor.x, Events::cursor.y);
if (focus && focus != pressed) { if (focus && focus != pressed) {
focus->defocus(); focus->defocus();
} }
@@ -72,7 +70,7 @@ void GUI::actMouse(float delta) {
focus = nullptr; focus = nullptr;
} }
} else if (pressed) { } else if (pressed) {
pressed->mouseRelease(this, mx, my); pressed->mouseRelease(this, Events::cursor.x, Events::cursor.y);
pressed = nullptr; pressed = nullptr;
} }
} }
@@ -100,9 +98,7 @@ void GUI::act(float delta) {
if (!Events::_cursor_locked) { if (!Events::_cursor_locked) {
if (Events::clicked(mousecode::BUTTON_1)) { if (Events::clicked(mousecode::BUTTON_1)) {
int mx = Events::x; focus->mouseMove(this, Events::cursor.x, Events::cursor.y);
int my = Events::y;
focus->mouseMove(this, mx, my);
} }
if (prevfocus == focus){ if (prevfocus == focus){
for (int i = mousecode::BUTTON_1; i < mousecode::BUTTON_1+12; i++) { for (int i = mousecode::BUTTON_1; i < mousecode::BUTTON_1+12; i++) {
+2 -2
View File
@@ -229,8 +229,8 @@ void HudRenderer::drawContentAccess(const GfxContext& ctx, Player* player) {
int ys = inv_y + pad_y; int ys = inv_y + pad_y;
vec4 tint = vec4(1.0f); vec4 tint = vec4(1.0f);
int mx = Events::x; int mx = Events::cursor.x;
int my = Events::y; int my = Events::cursor.y;
// background // background
batch->texture(nullptr); batch->texture(nullptr);
+11 -17
View File
@@ -41,28 +41,22 @@ void CameraControl::refresh() {
} }
void CameraControl::updateMouse(PlayerInput& input) { void CameraControl::updateMouse(PlayerInput& input) {
float sensitivity = settings.sensitivity; glm::vec2 &cam = player->cam;
float rotX = -Events::deltaX / Window::height * sensitivity; if (input.zoom) {
float rotY = -Events::deltaY / Window::height * sensitivity; cam += -Events::delta / (float)Window::height * settings.sensitivity / 4.f;
} else {
cam += -Events::delta / (float)Window::height * settings.sensitivity;
}
if (input.zoom){ if (cam.y < -glm::radians(89.9f)) {
rotX /= 4; cam.y = -glm::radians(89.9f);
rotY /= 4;
} }
if (cam.y > glm::radians(89.9f)) {
float& camX = player->camX; cam.y = glm::radians(89.9f);
float& camY = player->camY;
camX += rotX;
camY += rotY;
if (camY < -glm::radians(89.9f)){
camY = -glm::radians(89.9f);
}
if (camY > glm::radians(89.9f)){
camY = glm::radians(89.9f);
} }
camera->rotation = glm::mat4(1.0f); camera->rotation = glm::mat4(1.0f);
camera->rotate(camY, camX, 0); camera->rotate(cam.y, cam.x, 0);
} }
void CameraControl::update(PlayerInput& input, float delta, Chunks* chunks) { void CameraControl::update(PlayerInput& input, float delta, Chunks* chunks) {
+1 -2
View File
@@ -38,8 +38,7 @@ public:
int chosenBlock; int chosenBlock;
voxel selectedVoxel {0, 0}; voxel selectedVoxel {0, 0};
float camX = 0.0f; glm::vec2 cam = {};
float camY = 0.0f;
Player(glm::vec3 position, float speed); Player(glm::vec3 position, float speed);
~Player(); ~Player();
+29 -30
View File
@@ -4,42 +4,22 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <string.h> #include <string.h>
const short KEYS_BUFFER_SIZE = 1032; bool Events::_keys[KEYS_BUFFER_SIZE] = {};
const short _MOUSE_KEYS_OFFSET = 1024; uint Events::_frames[KEYS_BUFFER_SIZE] = {};
bool* Events::_keys;
uint* Events::_frames;
uint Events::_current = 0; 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; 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_locked = false;
bool Events::_cursor_started = false;
std::vector<uint> Events::codepoints; std::vector<uint> Events::codepoints;
std::vector<int> Events::pressedKeys; std::vector<int> Events::pressedKeys;
std::unordered_map<std::string, Binding> Events::bindings; std::unordered_map<std::string, Binding> Events::bindings;
int Events::initialize(){
_keys = new bool[KEYS_BUFFER_SIZE];
_frames = new uint[KEYS_BUFFER_SIZE];
memset(_keys, false, KEYS_BUFFER_SIZE*sizeof(bool));
memset(_frames, 0, KEYS_BUFFER_SIZE*sizeof(uint));
return 0;
}
void Events::finalize(){
delete[] _keys;
delete[] _frames;
}
// Returns bool repr. of key state // Returns bool repr. of key state
bool Events::pressed(int keycode){ bool Events::pressed(int keycode) {
if (keycode < 0 || keycode >= KEYS_BUFFER_SIZE){ if (keycode < 0 || keycode >= KEYS_BUFFER_SIZE) {
// VERY bad behaviour and it happens constantly! (so console-printing is not a good idea) fprintf(stderr, "pressed %i\n", keycode);
return false; return false;
} }
return _keys[keycode]; return _keys[keycode];
@@ -67,8 +47,8 @@ void Events::toggleCursor(){
void Events::pollEvents(){ void Events::pollEvents(){
_current++; _current++;
deltaX = 0.0f; delta.x = 0.f;
deltaY = 0.0f; delta.y = 0.f;
scroll = 0; scroll = 0;
codepoints.clear(); codepoints.clear();
pressedKeys.clear(); pressedKeys.clear();
@@ -117,3 +97,22 @@ bool Events::jactive(std::string name) {
} }
return found->second.jactive(); 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;
}
+13 -12
View File
@@ -10,27 +10,23 @@
typedef unsigned int uint; typedef unsigned int uint;
extern const short KEYS_BUFFER_SIZE; const short KEYS_BUFFER_SIZE = 1036;
extern const short _MOUSE_KEYS_OFFSET; const short _MOUSE_KEYS_OFFSET = 1024;
class Events { class Events {
public: public:
static bool* _keys; static bool _keys[KEYS_BUFFER_SIZE];
static uint* _frames; static uint _frames[KEYS_BUFFER_SIZE];
static uint _current; static uint _current;
static float deltaX; static int scroll;
static float deltaY; static glm::vec2 delta;
static float x; static glm::vec2 cursor;
static float y; static bool cursor_drag;
static int scroll;
static bool _cursor_locked; static bool _cursor_locked;
static bool _cursor_started;
static std::vector<uint> codepoints; static std::vector<uint> codepoints;
static std::vector<int> pressedKeys; static std::vector<int> pressedKeys;
static std::unordered_map<std::string, Binding> bindings; static std::unordered_map<std::string, Binding> bindings;
static int initialize();
static void finalize();
static void pollEvents(); static void pollEvents();
static bool pressed(int keycode); static bool pressed(int keycode);
@@ -44,6 +40,11 @@ public:
static void bind(std::string name, inputtype type, int code); static void bind(std::string name, inputtype type, int code);
static bool active(std::string name); static bool active(std::string name);
static bool jactive(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_ */ #endif /* WINDOW_EVENTS_H_ */
+5 -29
View File
@@ -21,42 +21,21 @@ int Window::posX = 0;
int Window::posY = 0; int Window::posY = 0;
void cursor_position_callback(GLFWwindow*, double xpos, double ypos) { void cursor_position_callback(GLFWwindow*, double xpos, double ypos) {
if (Events::_cursor_started) { Events::setPosition(xpos, ypos);
Events::deltaX += xpos - Events::x;
Events::deltaY += ypos - Events::y;
}
else {
Events::_cursor_started = true;
}
Events::x = xpos;
Events::y = 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) { Events::setButton(button, 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;
}
} }
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::setKey(key, true);
Events::_keys[key] = true;
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::setKey(key, false);
Events::_keys[key] = false;
Events::_frames[key] = Events::_current;
} }
else if (action == GLFW_REPEAT) { else if (action == GLFW_REPEAT) {
Events::pressedKeys.push_back(key); Events::pressedKeys.push_back(key);
@@ -163,7 +142,6 @@ int Window::initialize(DisplaySettings& settings){
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Events::initialize();
glfwSetKeyCallback(window, key_callback); glfwSetKeyCallback(window, key_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback); glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetCursorPosCallback(window, cursor_position_callback); glfwSetCursorPosCallback(window, cursor_position_callback);
@@ -255,7 +233,6 @@ void Window::popScissor() {
} }
void Window::terminate(){ void Window::terminate(){
Events::finalize();
glfwTerminate(); glfwTerminate();
} }
@@ -290,8 +267,7 @@ void Window::toggleFullscreen(){
double xPos, yPos; double xPos, yPos;
glfwGetCursorPos(window, &xPos, &yPos); glfwGetCursorPos(window, &xPos, &yPos);
Events::x = xPos; Events::setPosition(xPos, yPos);
Events::y = yPos;
} }
bool Window::isFullscreen() { bool Window::isFullscreen() {