Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added mouse api interface #731

Merged
merged 9 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/graphic/Fast3D/Fast3dWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,32 @@ int32_t Fast3dWindow::GetPosY() {
return posY;
}

Ship::Coords Fast3dWindow::GetMousePos() {
int32_t x, y;
mWindowManagerApi->get_mouse_pos(&x, &y);
return { x, y };
}

Ship::Coords Fast3dWindow::GetMouseDelta() {
int32_t x, y;
mWindowManagerApi->get_mouse_delta(&x, &y);
return { x, y };
}

Ship::CoordsF Fast3dWindow::GetMouseWheel() {
float x, y;
mWindowManagerApi->get_mouse_wheel(&x, &y);
return { x, y };
}

bool Fast3dWindow::GetMouseState(Ship::MouseBtn btn) {
return mWindowManagerApi->get_mouse_state(static_cast<uint32_t>(btn));
}

void Fast3dWindow::SetMouseCapture(bool capture) {
mWindowManagerApi->set_mouse_capture(capture);
}

uint32_t Fast3dWindow::GetCurrentRefreshRate() {
uint32_t refreshRate;
mWindowManagerApi->get_active_window_refresh_rate(&refreshRate);
Expand Down
5 changes: 5 additions & 0 deletions src/graphic/Fast3D/Fast3dWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class Fast3dWindow : public Ship::Window {
uint32_t GetHeight() override;
int32_t GetPosX() override;
int32_t GetPosY() override;
Ship::Coords GetMousePos() override;
Ship::Coords GetMouseDelta() override;
Ship::CoordsF GetMouseWheel() override;
bool GetMouseState(Ship::MouseBtn btn) override;
void SetMouseCapture(bool capture) override;
uint32_t GetCurrentRefreshRate() override;
bool SupportsWindowedFullscreen() override;
bool CanDisableVerticalSync() override;
Expand Down
65 changes: 65 additions & 0 deletions src/graphic/Fast3D/gfx_dxgi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ static struct {
bool use_timer;
bool tearing_support;
bool is_vsync_enabled;
bool mouse_pressed[3];
float mouse_wheel[2];
LARGE_INTEGER previous_present_time;

void (*on_fullscreen_changed)(bool is_now_fullscreen);
Expand Down Expand Up @@ -356,6 +358,28 @@ static LRESULT CALLBACK gfx_dxgi_wnd_proc(HWND h_wnd, UINT message, WPARAM w_par
case WM_KEYUP:
onkeyup(w_param, l_param);
break;
case WM_LBUTTONDOWN:
dxgi.mouse_pressed[0] = true;
break;
case WM_LBUTTONUP:
dxgi.mouse_pressed[0] = false;
break;
case WM_MBUTTONDOWN:
dxgi.mouse_pressed[1] = true;
break;
case WM_MBUTTONUP:
dxgi.mouse_pressed[1] = false;
break;
case WM_RBUTTONDOWN:
dxgi.mouse_pressed[2] = true;
break;
case WM_RBUTTONUP:
dxgi.mouse_pressed[2] = false;
break;
case WM_MOUSEWHEEL:
dxgi.mouse_wheel[0] = GET_WHEEL_DELTA_WPARAM(w_param) / WHEEL_DELTA;
dxgi.mouse_wheel[1] = 0;
break;
case WM_DROPFILES:
DragQueryFileA((HDROP)w_param, 0, fileName, 256);
Ship::Context::GetInstance()->GetConsoleVariables()->SetString(CVAR_DROPPED_FILE, fileName);
Expand Down Expand Up @@ -485,6 +509,42 @@ static void gfx_dxgi_set_cursor_visibility(bool visible) {
}
}

static void gfx_dxgi_get_mouse_pos(int32_t* x, int32_t* y) {
POINT p;
GetCursorPos(&p);
ScreenToClient(dxgi.h_wnd, &p);
*x = p.x;
*y = p.y;
}

static void gfx_dxgi_get_mouse_delta(int32_t* x, int32_t* y) {
POINT p;
GetCursorPos(&p);
ScreenToClient(dxgi.h_wnd, &p);
*x = p.x - dxgi.current_width / 2;
*y = p.y - dxgi.current_height / 2;
SetCursorPos(dxgi.current_width / 2, dxgi.current_height / 2);
}

static void gfx_dxgi_get_mouse_wheel(float* x, float* y) {
*x = dxgi.mouse_wheel[0];
*y = dxgi.mouse_wheel[1];
dxgi.mouse_wheel[0] = 0;
dxgi.mouse_wheel[1] = 0;
}

static bool gfx_dxgi_get_mouse_state(uint32_t btn) {
return dxgi.mouse_pressed[btn];
}

static void gfx_dxgi_set_mouse_capture(bool capture) {
if (capture) {
SetCapture(dxgi.h_wnd);
} else {
ReleaseCapture();
}
}

static void gfx_dxgi_set_fullscreen(bool enable) {
toggle_borderless_window_full_screen(enable, true);
}
Expand Down Expand Up @@ -912,6 +972,11 @@ extern "C" struct GfxWindowManagerAPI gfx_dxgi_api = { gfx_dxgi_init,
gfx_dxgi_set_fullscreen,
gfx_dxgi_get_active_window_refresh_rate,
gfx_dxgi_set_cursor_visibility,
gfx_dxgi_get_mouse_pos,
gfx_dxgi_get_mouse_delta,
gfx_dxgi_get_mouse_wheel,
gfx_dxgi_get_mouse_state,
gfx_dxgi_set_mouse_capture,
gfx_dxgi_get_dimensions,
gfx_dxgi_handle_events,
gfx_dxgi_start_frame,
Expand Down
34 changes: 34 additions & 0 deletions src/graphic/Fast3D/gfx_sdl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ static SDL_GLContext ctx;
static SDL_Renderer* renderer;
static int sdl_to_lus_table[512];
static bool vsync_enabled = true;
static float mouse_wheel_x = 0.0f;
static float mouse_wheel_y = 0.0f;
// OTRTODO: These are redundant. Info can be queried from SDL.
static int window_width = DESIRED_SCREEN_WIDTH;
static int window_height = DESIRED_SCREEN_HEIGHT;
Expand Down Expand Up @@ -438,6 +440,29 @@ static void gfx_sdl_set_cursor_visibility(bool visible) {
}
}

static void gfx_sdl_get_mouse_pos(int32_t* x, int32_t* y) {
SDL_GetMouseState(x, y);
}

static void gfx_sdl_get_mouse_delta(int32_t* x, int32_t* y) {
SDL_GetRelativeMouseState(x, y);
}

static void gfx_sdl_get_mouse_wheel(float* x, float* y) {
*x = mouse_wheel_x;
*y = mouse_wheel_y;
mouse_wheel_x = 0.0f;
mouse_wheel_y = 0.0f;
}

static bool gfx_sdl_get_mouse_state(uint32_t btn) {
return SDL_GetMouseState(NULL, NULL) & (1 << btn);
}

static void gfx_sdl_set_mouse_capture(bool capture) {
SDL_SetRelativeMouseMode(static_cast<SDL_bool>(capture));
}

static void gfx_sdl_set_keyboard_callbacks(bool (*on_key_down)(int scancode), bool (*on_key_up)(int scancode),
void (*on_all_keys_up)(void)) {
on_key_down_callback = on_key_down;
Expand Down Expand Up @@ -494,6 +519,10 @@ static void gfx_sdl_handle_single_event(SDL_Event& event) {
case SDL_KEYUP:
gfx_sdl_onkeyup(event.key.keysym.scancode);
break;
case SDL_MOUSEWHEEL:
mouse_wheel_x = event.wheel.x;
mouse_wheel_y = event.wheel.y;
break;
#endif
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
Expand Down Expand Up @@ -625,6 +654,11 @@ struct GfxWindowManagerAPI gfx_sdl = { gfx_sdl_init,
gfx_sdl_set_fullscreen,
gfx_sdl_get_active_window_refresh_rate,
gfx_sdl_set_cursor_visibility,
gfx_sdl_get_mouse_pos,
gfx_sdl_get_mouse_delta,
gfx_sdl_get_mouse_wheel,
gfx_sdl_get_mouse_state,
gfx_sdl_set_mouse_capture,
gfx_sdl_get_dimensions,
gfx_sdl_handle_events,
gfx_sdl_start_frame,
Expand Down
5 changes: 5 additions & 0 deletions src/graphic/Fast3D/gfx_window_manager_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ struct GfxWindowManagerAPI {
void (*set_fullscreen)(bool enable);
void (*get_active_window_refresh_rate)(uint32_t* refresh_rate);
void (*set_cursor_visibility)(bool visible);
void (*get_mouse_pos)(int32_t* x, int32_t* y);
void (*get_mouse_delta)(int32_t* x, int32_t* y);
void (*get_mouse_wheel)(float* x, float* y);
bool (*get_mouse_state)(uint32_t btn);
void (*set_mouse_capture)(bool capture);
void (*get_dimensions)(uint32_t* width, uint32_t* height, int32_t* posX, int32_t* posY);
void (*handle_events)(void);
bool (*start_frame)(void);
Expand Down
17 changes: 17 additions & 0 deletions src/window/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
namespace Ship {
enum class WindowBackend { FAST3D_DXGI_DX11, FAST3D_SDL_OPENGL, FAST3D_SDL_METAL, WINDOW_BACKEND_COUNT };

enum class MouseBtn { LEFT, MIDDLE, RIGHT, MOUSE_BTN_COUNT };

struct Coords {
int32_t x;
int32_t y;
};

struct CoordsF {
float x;
float y;
};

class Config;

class Window {
Expand All @@ -30,6 +42,11 @@ class Window {
virtual uint32_t GetHeight() = 0;
virtual int32_t GetPosX() = 0;
virtual int32_t GetPosY() = 0;
virtual Coords GetMousePos() = 0;
virtual Coords GetMouseDelta() = 0;
virtual CoordsF GetMouseWheel() = 0;
virtual bool GetMouseState(MouseBtn btn) = 0;
virtual void SetMouseCapture(bool capture) = 0;
virtual uint32_t GetCurrentRefreshRate() = 0;
virtual bool SupportsWindowedFullscreen() = 0;
virtual bool CanDisableVerticalSync() = 0;
Expand Down
Loading