diff --git a/include/Orange/Render/Window.h b/include/Orange/Render/Window.h index 10d2570..df8ce31 100644 --- a/include/Orange/Render/Window.h +++ b/include/Orange/Render/Window.h @@ -7,6 +7,12 @@ struct SDL_Window; namespace orange { + struct MouseDelta + { + int x = 0; + int y = 0; + }; + class Window { public: @@ -21,10 +27,18 @@ namespace orange static Result Create(); bool Update(); + + void EnableRelativeMouse(bool relative); + + MouseDelta GetMouseDelta(); protected: friend Result; Window(SDL_Window* window); private: SDL_Window* m_window = nullptr; + bool m_relativeMouse = false; + bool m_hasFocus = false; + + MouseDelta m_mouseDelta = {}; }; } diff --git a/src/Apps/Tools/CubeTest.cpp b/src/Apps/Tools/CubeTest.cpp index 671a66e..2dde8c4 100644 --- a/src/Apps/Tools/CubeTest.cpp +++ b/src/Apps/Tools/CubeTest.cpp @@ -596,8 +596,16 @@ int main(int argc, char** argv) memcpy((uint8_t*)(r_buffer->ptr) + uniformSlice.offset, &uniformData, sizeof(uniformData)); + r_window->EnableRelativeMouse(true); + while (r_window->Update()) { + auto mouseDelta = r_window->GetMouseDelta(); + //if (mouseDelta.x || mouseDelta.y) + //{ + // log::info("mouse: %d %d", mouseDelta.x, mouseDelta.y); + //} + VkCommandBuffer cmdBuf = r_swapchain->CommandBuffer(); r_renderContext->BeginCommandBuffer(cmdBuf); { diff --git a/src/Orange/Render/Window.cpp b/src/Orange/Render/Window.cpp index 5a5ee2f..f84d468 100644 --- a/src/Orange/Render/Window.cpp +++ b/src/Orange/Render/Window.cpp @@ -67,9 +67,33 @@ namespace orange { switch (e.type) { + case SDL_MOUSEMOTION: + if (m_hasFocus) + { + m_mouseDelta.x += e.motion.xrel; + m_mouseDelta.y += e.motion.yrel; + } + break; case SDL_KEYDOWN: case SDL_KEYUP: break; + case SDL_WINDOWEVENT: + switch (e.window.event) + { + case SDL_WINDOWEVENT_FOCUS_GAINED: + { + m_hasFocus = true; + SDL_ShowCursor(m_relativeMouse ? SDL_FALSE : SDL_TRUE); + break; + } + case SDL_WINDOWEVENT_FOCUS_LOST: + { + m_hasFocus = false; + SDL_ShowCursor(SDL_TRUE); + break; + } + } + break; case SDL_QUIT: quit = true; break; @@ -78,4 +102,19 @@ namespace orange return !quit; } + + void Window::EnableRelativeMouse(bool relative) + { + m_relativeMouse = relative; + SDL_SetWindowGrab(m_window, relative ? SDL_TRUE : SDL_FALSE); + SDL_SetRelativeMouseMode(relative ? SDL_TRUE : SDL_FALSE); + SDL_ShowCursor(m_hasFocus && relative ? SDL_FALSE : SDL_TRUE); + } + + MouseDelta Window::GetMouseDelta() + { + MouseDelta delta = m_mouseDelta; + m_mouseDelta = MouseDelta{}; + return delta; + } }