diff --git a/include/Cubed/app.hpp b/include/Cubed/app.hpp index 6000483..d28a55d 100644 --- a/include/Cubed/app.hpp +++ b/include/Cubed/app.hpp @@ -13,7 +13,7 @@ class App { public: App(); ~App(); - void handle_cursor_position(float xpos, float ypos); + void handle_mouse_move(float xpos, float ypos, float xrel, float yrel); void handle_sdl_key(SDL_Event& e); void handle_sdl_mouse_button(SDL_Event& e); void handle_window_focus(bool focused); diff --git a/include/Cubed/camera.hpp b/include/Cubed/camera.hpp index ae4e7c0..b5746da 100644 --- a/include/Cubed/camera.hpp +++ b/include/Cubed/camera.hpp @@ -39,7 +39,7 @@ public: void camera_init(ClientPlayer* player); void hot_reload(); void reset_camera(); - void update_cursor_position_camera(double xpos, double ypos); + void update_cursor_position_camera(float offset_x, float offset_y); const glm::mat4 get_camera_lookat() const; const glm::vec3& get_camera_pos() const; diff --git a/include/Cubed/input/event.hpp b/include/Cubed/input/event.hpp index 13b8535..efe26fb 100644 --- a/include/Cubed/input/event.hpp +++ b/include/Cubed/input/event.hpp @@ -11,8 +11,10 @@ namespace Cubed { struct MouseMoveEvent { float xpos; float ypos; - - MouseMoveEvent(float x, float y) : xpos(x), ypos(y) {} + float xrel; + float yrel; + MouseMoveEvent(float x, float y, float dx, float dy) + : xpos(x), ypos(y), xrel(dx), yrel(dy) {} }; struct MouseButtonEvent { diff --git a/include/Cubed/window.hpp b/include/Cubed/window.hpp index 94e919b..4285f73 100644 --- a/include/Cubed/window.hpp +++ b/include/Cubed/window.hpp @@ -41,14 +41,17 @@ public: void set_vsync(bool enable); + int width() const; + int height() const; + private: bool m_mouse_enable = true; bool m_imgui_init = false; bool m_game_running = false; SDL_Window* m_window; SDL_GLContext m_context; - int m_window_width; - int m_window_height; + int m_window_width = 0; + int m_window_height = 0; Config& m_config; Camera* m_camera = nullptr; bool m_imgui_enable = false; diff --git a/src/app.cpp b/src/app.cpp index 9f34985..c00eff2 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -136,9 +136,9 @@ void App::handle_argument(int argc, char** argv) { } } } -void App::handle_cursor_position(float xpos, float ypos) { +void App::handle_mouse_move(float xpos, float ypos, float xrel, float yrel) { - m_scene_manager.handle_event(MouseMoveEvent{xpos, ypos}); + m_scene_manager.handle_event(MouseMoveEvent{xpos, ypos, xrel, yrel}); } void App::handle_sdl_key(SDL_Event& e) { @@ -651,7 +651,7 @@ void App::handle_sdl_event(SDL_Event& e) { handle_sdl_mouse_button(e); break; case SDL_EVENT_MOUSE_MOTION: - handle_cursor_position(e.motion.x, e.motion.y); + handle_mouse_move(e.motion.x, e.motion.y, e.motion.xrel, e.motion.yrel); break; case SDL_EVENT_WINDOW_RESIZED: handle_window_resize(e.window.data1, e.window.data2); diff --git a/src/camera.cpp b/src/camera.cpp index 81c8b1d..c0ecfef 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -69,19 +69,12 @@ void Camera::hot_reload() {} void Camera::reset_camera() { m_firse_mouse = true; } -void Camera::update_cursor_position_camera(double xpos, double ypos) { +void Camera::update_cursor_position_camera(float offset_x, float offset_y) { if (m_firse_mouse) { - m_last_mouse_x = xpos; - m_last_mouse_y = ypos; m_firse_mouse = false; return; } - float offset_x = xpos - m_last_mouse_x; - float offset_y = m_last_mouse_y - ypos; - - m_last_mouse_x = xpos; - m_last_mouse_y = ypos; ASSERT_MSG(m_player, "nullptr"); m_player->update_front_vec(offset_x, offset_y); } @@ -174,7 +167,7 @@ bool Camera::handle_key_event(const KeyEvent& e) { return false; } bool Camera::handle_mouse_move_event(const MouseMoveEvent& e) { - update_cursor_position_camera(e.xpos, e.ypos); + update_cursor_position_camera(e.xrel, -e.yrel); return true; } diff --git a/src/window.cpp b/src/window.cpp index 6755def..310989e 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -12,7 +12,7 @@ namespace Cubed { static int windowed_xpos = 0, windowed_ypos = 0; -static int windowed_width = 800, windowed_height = 600; +static int windowed_width = 1280, windowed_height = 720; Window::Window(Config& config) : m_config(config) {} @@ -193,11 +193,12 @@ void Window::set_fullscreen(bool full) { } } void Window::enable_mouse() { - SDL_SetWindowRelativeMouseMode(m_window, true); + SDL_WarpMouseInWindow(m_window, width() / 2, height() / 2); + SDL_SetWindowRelativeMouseMode(m_window, false); m_mouse_enable = true; } void Window::disable_mouse() { - SDL_SetWindowRelativeMouseMode(m_window, false); + SDL_SetWindowRelativeMouseMode(m_window, true); if (m_camera) { m_camera->reset_camera(); } @@ -231,7 +232,8 @@ void Window::set_vsync(bool enable) { Logger::error("VSync Fail: {}", SDL_GetError()); } } - +int Window::width() const { return m_window_width; } +int Window::height() const { return m_window_height; } void Window::imgui_init() { float main_scale = SDL_GetWindowDisplayScale(m_window);