Skip to content

Commit

Permalink
dasdasdasdas
Browse files Browse the repository at this point in the history
  • Loading branch information
QuFlax committed Dec 27, 2023
1 parent 95c713b commit 4d281ff
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 77 deletions.
8 changes: 4 additions & 4 deletions src/files/WorldFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,8 @@ void WorldFiles::writePlayer(Player* player){
posarr.put(position.z);

json::JArray& rotarr = root.putArray("rotation");
rotarr.put(player->camX);
rotarr.put(player->camY);
rotarr.put(player->cam.x);
rotarr.put(player->cam.y);

root.put("flight", player->flight);
root.put("noclip", player->noclip);
Expand All @@ -479,8 +479,8 @@ bool WorldFiles::readPlayer(Player* player) {
player->camera->position = position;

json::JArray* rotarr = root->arr("rotation");
player->camX = rotarr->num(0);
player->camY = rotarr->num(1);
player->cam.x = rotarr->num(0);
player->cam.y = rotarr->num(1);

root->flag("flight", player->flight);
root->flag("noclip", player->noclip);
Expand Down
12 changes: 4 additions & 8 deletions src/frontend/gui/GUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ PagesControl* GUI::getMenu() {
}

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) {
this->hover->hover(false);
}
Expand All @@ -58,7 +56,7 @@ void GUI::actMouse(float delta) {
if (Events::jclicked(0)) {
if (pressed == nullptr && this->hover) {
pressed = hover;
pressed->click(this, mx, my);
pressed->click(this, Events::cursor.x, Events::cursor.y);
if (focus && focus != pressed) {
focus->defocus();
}
Expand All @@ -72,7 +70,7 @@ void GUI::actMouse(float delta) {
focus = nullptr;
}
} else if (pressed) {
pressed->mouseRelease(this, mx, my);
pressed->mouseRelease(this, Events::cursor.x, Events::cursor.y);
pressed = nullptr;
}
}
Expand Down Expand Up @@ -100,9 +98,7 @@ void GUI::act(float delta) {

if (!Events::_cursor_locked) {
if (Events::clicked(mousecode::BUTTON_1)) {
int mx = Events::x;
int my = Events::y;
focus->mouseMove(this, mx, my);
focus->mouseMove(this, Events::cursor.x, Events::cursor.y);
}
if (prevfocus == focus){
for (int i = mousecode::BUTTON_1; i < mousecode::BUTTON_1+12; i++) {
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ void HudRenderer::drawContentAccess(const GfxContext& ctx, Player* player) {
int ys = inv_y + pad_y;

vec4 tint = vec4(1.0f);
int mx = Events::x;
int my = Events::y;
int mx = Events::cursor.x;
int my = Events::cursor.y;

// background
batch->texture(nullptr);
Expand Down
28 changes: 11 additions & 17 deletions src/logic/PlayerController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,22 @@ void CameraControl::refresh() {
}

void CameraControl::updateMouse(PlayerInput& input) {
float sensitivity = settings.sensitivity;
float rotX = -Events::deltaX / Window::height * sensitivity;
float rotY = -Events::deltaY / Window::height * sensitivity;
glm::vec2 &cam = player->cam;
if (input.zoom) {
cam += -Events::delta / (float)Window::height * settings.sensitivity / 4.f;
} else {
cam += -Events::delta / (float)Window::height * settings.sensitivity;
}

if (input.zoom){
rotX /= 4;
rotY /= 4;
if (cam.y < -glm::radians(89.9f)) {
cam.y = -glm::radians(89.9f);
}

float& camX = player->camX;
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);
if (cam.y > glm::radians(89.9f)) {
cam.y = glm::radians(89.9f);
}

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) {
Expand Down
3 changes: 1 addition & 2 deletions src/objects/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ class Player {
int chosenBlock;
voxel selectedVoxel {0, 0};

float camX = 0.0f;
float camY = 0.0f;
glm::vec2 cam = {};

Player(glm::vec3 position, float speed);
~Player();
Expand Down
37 changes: 27 additions & 10 deletions src/window/Events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
17 changes: 10 additions & 7 deletions src/window/Events.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,19 @@

typedef unsigned int uint;

const short KEYS_BUFFER_SIZE = 1032;
const short KEYS_BUFFER_SIZE = 1036;
const short _MOUSE_KEYS_OFFSET = 1024;

class Events {
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;
Expand All @@ -42,6 +40,11 @@ class Events {
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_ */
32 changes: 5 additions & 27 deletions src/window/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 4d281ff

Please sign in to comment.