Skip to content

Commit

Permalink
Added mouse press state
Browse files Browse the repository at this point in the history
  • Loading branch information
KiritoDv committed Dec 3, 2024
1 parent d6a9e1f commit 07357c0
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/graphic/Fast3D/Fast3dWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ Ship::Coords Fast3dWindow::GetMouseDelta() {
return {x, y};
}

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

uint32_t Fast3dWindow::GetCurrentRefreshRate() {
uint32_t refreshRate;
mWindowManagerApi->get_active_window_refresh_rate(&refreshRate);
Expand Down
1 change: 1 addition & 0 deletions src/graphic/Fast3D/Fast3dWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Fast3dWindow : public Ship::Window {
int32_t GetPosY() override;
Ship::Coords GetMousePos() override;
Ship::Coords GetMouseDelta() override;
bool GetMouseState(Ship::MouseBtn btn) override;
uint32_t GetCurrentRefreshRate() override;
bool SupportsWindowedFullscreen() override;
bool CanDisableVerticalSync() override;
Expand Down
24 changes: 24 additions & 0 deletions src/graphic/Fast3D/gfx_dxgi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ static struct {
bool use_timer;
bool tearing_support;
bool is_vsync_enabled;
bool mouse_pressed[3];
LARGE_INTEGER previous_present_time;

void (*on_fullscreen_changed)(bool is_now_fullscreen);
Expand Down Expand Up @@ -356,6 +357,24 @@ 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_DROPFILES:
DragQueryFileA((HDROP)w_param, 0, fileName, 256);
Ship::Context::GetInstance()->GetConsoleVariables()->SetString(CVAR_DROPPED_FILE, fileName);
Expand Down Expand Up @@ -502,6 +521,10 @@ static void gfx_dxgi_get_mouse_delta(int32_t* x, int32_t* y) {
SetCursorPos(dxgi.current_width / 2, dxgi.current_height / 2);
}

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

static void gfx_dxgi_set_fullscreen(bool enable) {
toggle_borderless_window_full_screen(enable, true);
}
Expand Down Expand Up @@ -931,6 +954,7 @@ extern "C" struct GfxWindowManagerAPI gfx_dxgi_api = { gfx_dxgi_init,
gfx_dxgi_set_cursor_visibility,
gfx_dxgi_get_mouse_pos,
gfx_dxgi_get_mouse_delta,
gfx_dxgi_get_mouse_state,
gfx_dxgi_get_dimensions,
gfx_dxgi_handle_events,
gfx_dxgi_start_frame,
Expand Down
15 changes: 7 additions & 8 deletions src/graphic/Fast3D/gfx_sdl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,17 +439,15 @@ static void gfx_sdl_set_cursor_visibility(bool visible) {
}

static void gfx_sdl_get_mouse_pos(int32_t* x, int32_t* y) {
int mx, my;
SDL_GetMouseState(&mx, &my);
*x = mx;
*y = my;
SDL_GetMouseState(x, y);
}

static void gfx_sdl_get_mouse_delta(int32_t* x, int32_t* y) {
int mx, my;
SDL_GetRelativeMouseState(&mx, &my);
*x = mx;
*y = my;
SDL_GetRelativeMouseState(x, y);
}

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

static void gfx_sdl_set_keyboard_callbacks(bool (*on_key_down)(int scancode), bool (*on_key_up)(int scancode),
Expand Down Expand Up @@ -641,6 +639,7 @@ struct GfxWindowManagerAPI gfx_sdl = { gfx_sdl_init,
gfx_sdl_set_cursor_visibility,
gfx_sdl_get_mouse_pos,
gfx_sdl_get_mouse_delta,
gfx_sdl_get_mouse_state,
gfx_sdl_get_dimensions,
gfx_sdl_handle_events,
gfx_sdl_start_frame,
Expand Down
1 change: 1 addition & 0 deletions src/graphic/Fast3D/gfx_window_manager_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct GfxWindowManagerAPI {
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);
bool (*get_mouse_state)(uint32_t btn);
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
4 changes: 4 additions & 0 deletions src/window/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

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;
Expand All @@ -36,6 +39,7 @@ class Window {
virtual int32_t GetPosY() = 0;
virtual Coords GetMousePos() = 0;
virtual Coords GetMouseDelta() = 0;
virtual bool GetMouseState(MouseBtn btn) = 0;
virtual uint32_t GetCurrentRefreshRate() = 0;
virtual bool SupportsWindowedFullscreen() = 0;
virtual bool CanDisableVerticalSync() = 0;
Expand Down

0 comments on commit 07357c0

Please sign in to comment.