Skip to content

Commit

Permalink
finalize sdl3 and fix mouse
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherHX committed Jan 20, 2024
1 parent 6427efe commit b14a8c0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
4 changes: 4 additions & 0 deletions include/game_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ class GameWindow {

virtual void setSwapInterval(int interval) = 0;

virtual void startTextInput() {}

virtual void stopTextInput() {}

void setDrawCallback(DrawCallback callback) { drawCallback = std::move(callback); }

void setWindowSizeCallback(WindowSizeCallback callback) { windowSizeCallback = std::move(callback); }
Expand Down
31 changes: 28 additions & 3 deletions src/window_sdl3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ void SDL3GameWindow::makeCurrent(bool c) {
}

SDL3GameWindow::~SDL3GameWindow() {
SDL_DestroyWindow(window);
if(window) {
SDL_DestroyWindow(window);
window = nullptr;
}
}

void SDL3GameWindow::setIcon(std::string const& iconPath) {
// TODO:

}

void SDL3GameWindow::setRelativeScale() {
Expand Down Expand Up @@ -76,6 +79,10 @@ void SDL3GameWindow::show() {
}

void SDL3GameWindow::close() {
if(window) {
SDL_DestroyWindow(window);
window = nullptr;
}
}

static GamepadButtonId getKeyGamePad(int btn) {
Expand Down Expand Up @@ -136,6 +143,24 @@ static GamepadAxisId getAxisGamepad(int btn) {
}
}

static int getMouseButton(int btn) {
switch (btn)
{
case SDL_BUTTON_LEFT:
return 1;
case SDL_BUTTON_RIGHT:
return 2;
case SDL_BUTTON_MIDDLE:
return 3;
case SDL_BUTTON_X1:
return 4;
case SDL_BUTTON_X2:
return 5;
default:
return 0;
}
}

void SDL3GameWindow::pollEvents() {
SDL_Event ev;
while(SDL_PollEvent(&ev)) {
Expand All @@ -153,7 +178,7 @@ void SDL3GameWindow::pollEvents() {
break;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
case SDL_EVENT_MOUSE_BUTTON_UP:
onMouseButton(ev.button.x, ev.button.y, ev.button.button, ev.type == SDL_EVENT_MOUSE_BUTTON_DOWN ? MouseButtonAction::PRESS : MouseButtonAction::RELEASE);
onMouseButton(ev.button.x, ev.button.y, getMouseButton(ev.button.button), ev.type == SDL_EVENT_MOUSE_BUTTON_DOWN ? MouseButtonAction::PRESS : MouseButtonAction::RELEASE);
break;
case SDL_EVENT_FINGER_DOWN:
onTouchStart(ev.tfinger.fingerId, ev.tfinger.x, ev.tfinger.y);
Expand Down

0 comments on commit b14a8c0

Please sign in to comment.