basic mouse input

This commit is contained in:
Joshua Ashton 2022-08-13 06:37:36 +00:00
parent dbeae98d71
commit 2c9d94647b
3 changed files with 61 additions and 0 deletions

View File

@ -7,6 +7,12 @@ struct SDL_Window;
namespace orange namespace orange
{ {
struct MouseDelta
{
int x = 0;
int y = 0;
};
class Window class Window
{ {
public: public:
@ -21,10 +27,18 @@ namespace orange
static Result<Window> Create(); static Result<Window> Create();
bool Update(); bool Update();
void EnableRelativeMouse(bool relative);
MouseDelta GetMouseDelta();
protected: protected:
friend Result<Window>; friend Result<Window>;
Window(SDL_Window* window); Window(SDL_Window* window);
private: private:
SDL_Window* m_window = nullptr; SDL_Window* m_window = nullptr;
bool m_relativeMouse = false;
bool m_hasFocus = false;
MouseDelta m_mouseDelta = {};
}; };
} }

View File

@ -596,8 +596,16 @@ int main(int argc, char** argv)
memcpy((uint8_t*)(r_buffer->ptr) + uniformSlice.offset, &uniformData, sizeof(uniformData)); memcpy((uint8_t*)(r_buffer->ptr) + uniformSlice.offset, &uniformData, sizeof(uniformData));
r_window->EnableRelativeMouse(true);
while (r_window->Update()) 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(); VkCommandBuffer cmdBuf = r_swapchain->CommandBuffer();
r_renderContext->BeginCommandBuffer(cmdBuf); r_renderContext->BeginCommandBuffer(cmdBuf);
{ {

View File

@ -67,9 +67,33 @@ namespace orange
{ {
switch (e.type) 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_KEYDOWN:
case SDL_KEYUP: case SDL_KEYUP:
break; 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: case SDL_QUIT:
quit = true; quit = true;
break; break;
@ -78,4 +102,19 @@ namespace orange
return !quit; 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;
}
} }