diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-10 20:25:21 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-10 20:25:21 +0200 |
| commit | bfcf30e800c68e0289f17b3967081280fca6592c (patch) | |
| tree | 96e29101ab4cd85d144744307e82ae436e87726a /src | |
| parent | 70d2db07ec4dd5034c6d888258a3fd945c67e4d4 (diff) | |
Fixed Minor Portability Issues
Diffstat (limited to 'src')
| -rw-r--r-- | src/menu.c | 58 | ||||
| -rw-r--r-- | src/picow.c | 3 |
2 files changed, 29 insertions, 32 deletions
@@ -59,23 +59,20 @@ void pw_menu_free(struct pw_menu *menu) { SDL_free(menu); } -static size_t saturating_add(size_t a, ssize_t b) { - // TODO: yuck... - if (b < 0) { - size_t c = (size_t)(-b); - if (a < c) { - return 0; - } else { - return a - c; - } - } else { - size_t c = a + (size_t)(b); - if (c < a) { - return SIZE_MAX; - } else { - return c; - } +static size_t saturating_add(size_t a, size_t b) { + size_t r = a + b; + if (r < a) { + return SIZE_MAX; + } + return r; +} + +static size_t saturating_sub(size_t a, size_t b) { + size_t r = a - b; + if (r > a) { + return 0; } + return r; } bool pw_menu_event(struct pw_menu *menu, SDL_Event *event) { @@ -92,22 +89,22 @@ bool pw_menu_event(struct pw_menu *menu, SDL_Event *event) { return true; } - ssize_t delta = 0; + size_t next = menu->selection.next; switch (event->type) { case SDL_EVENT_GAMEPAD_AXIS_MOTION: switch (event->gaxis.axis) { case SDL_GAMEPAD_AXIS_LEFTX: if (event->gaxis.value < -PW_AXIS_THRESHOLD) { - delta -= 1; + next = saturating_sub(next, 1); } else if (event->gaxis.value > PW_AXIS_THRESHOLD) { - delta += 1; + next = saturating_add(next, 1); } break; case SDL_GAMEPAD_AXIS_LEFTY: if (event->gaxis.value < -PW_AXIS_THRESHOLD) { - delta -= 10; + next = saturating_sub(next, 10); } else if (event->gaxis.value > PW_AXIS_THRESHOLD) { - delta += 10; + next = saturating_add(next, 10); } break; } @@ -123,16 +120,16 @@ bool pw_menu_event(struct pw_menu *menu, SDL_Event *event) { menu->selection.selected = true; break; case SDL_GAMEPAD_BUTTON_DPAD_UP: - delta -= 10; + next = saturating_sub(next, 10); break; case SDL_GAMEPAD_BUTTON_DPAD_DOWN: - delta += 10; + next = saturating_add(next, 10); break; case SDL_GAMEPAD_BUTTON_DPAD_LEFT: - delta -= 1; + next = saturating_sub(next, 1); break; case SDL_GAMEPAD_BUTTON_DPAD_RIGHT: - delta += 1; + next = saturating_add(next, 1); break; } break; @@ -145,25 +142,24 @@ bool pw_menu_event(struct pw_menu *menu, SDL_Event *event) { break; case SDLK_UP: case SDLK_K: - delta -= 10; + next = saturating_sub(next, 10); break; case SDLK_DOWN: case SDLK_J: - delta += 10; + next = saturating_add(next, 10); break; case SDLK_LEFT: case SDLK_H: - delta -= 1; + next = saturating_sub(next, 1); break; case SDLK_RIGHT: case SDLK_L: - delta += 1; + next = saturating_add(next, 1); break; } } + menu->selection.next = SDL_min(next, menu->carts->count - 1); - menu->selection.next = SDL_min(saturating_add(menu->selection.next, delta), - menu->carts->count - 1); return true; } diff --git a/src/picow.c b/src/picow.c index 0e6bdde..599fea2 100644 --- a/src/picow.c +++ b/src/picow.c @@ -1,3 +1,4 @@ +#include <stdio.h> #include <unistd.h> #define SDL_MAIN_USE_CALLBACKS @@ -185,5 +186,5 @@ void SDL_AppQuit(void *appstate, SDL_AppResult result) { SDL_DestroyWindow(state->window); SDL_free(state); - SDL_Log(0, "bye"); + SDL_Log("bye"); } |
