diff options
| -rw-r--r-- | carts/pico8.p8 | 2 | ||||
| -rw-r--r-- | src/picow.c | 218 |
2 files changed, 150 insertions, 70 deletions
diff --git a/carts/pico8.p8 b/carts/pico8.p8 index b288696..75c7407 100644 --- a/carts/pico8.p8 +++ b/carts/pico8.p8 @@ -1,7 +1,7 @@ pico-8 cartridge // http://www.pico-8.com version 42 __lua__ --- PICO-8 +-- pico-8 -- by @nlordell function _init() diff --git a/src/picow.c b/src/picow.c index 90c762c..b912899 100644 --- a/src/picow.c +++ b/src/picow.c @@ -28,6 +28,16 @@ struct pw_pxtexture { SDL_FRect target; }; +enum pw_button { + PW_BUTTON_LEFT, + PW_BUTTON_RIGHT, + PW_BUTTON_UP, + PW_BUTTON_DOWN, + PW_BUTTON_O, + PW_BUTTON_X, + PW_BUTTON_MENU, +}; + struct pw_gamepad { SDL_Gamepad *handle; union pw_gamepad_state { @@ -56,6 +66,7 @@ struct pw_appstate { char (*carts)[256]; int ncarts; int cartidx; + int cartidx2; }; #define FAIL(msg) \ @@ -67,7 +78,8 @@ static bool init_pxtexture(SDL_Renderer *renderer, struct pw_pxtexture *texture, texture->handle = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STREAMING, w, h); - return texture->handle != NULL; + return texture->handle != NULL && + SDL_SetTextureScaleMode(texture->handle, SDL_SCALEMODE_NEAREST); } static bool update_pxtexture_target(struct pw_pxtexture *texture, int w, int h) { @@ -174,6 +186,52 @@ static bool load_unknown_png(struct pw_pxtexture *texture) { return blit_cart_png(texture, &png); } +static void update_gamepad_state(struct pw_appstate *state, union pw_gamepad_state *gamepad, + enum pw_button button, bool pressed) { + switch (button) { + case PW_BUTTON_LEFT: + gamepad->b.left = pressed; + break; + case PW_BUTTON_RIGHT: + gamepad->b.right = pressed; + break; + case PW_BUTTON_UP: + gamepad->b.up = pressed; + break; + case PW_BUTTON_DOWN: + gamepad->b.down = pressed; + break; + case PW_BUTTON_O: + gamepad->b.o = pressed; + break; + case PW_BUTTON_X: + gamepad->b.x = pressed; + break; + case PW_BUTTON_MENU: + gamepad->b.menu = pressed; + break; + } + + if (state->ncarts > 0 && pressed) { + switch (button) { + case PW_BUTTON_LEFT: + state->cartidx2 = SDL_max(0, state->cartidx2 - 1); + break; + case PW_BUTTON_RIGHT: + state->cartidx2 = SDL_min(state->ncarts - 1, state->cartidx2 + 1); + break; + case PW_BUTTON_UP: + state->cartidx2 = SDL_max(0, state->cartidx2 - 10); + break; + case PW_BUTTON_DOWN: + state->cartidx2 = SDL_min(state->ncarts - 1, state->cartidx2 + 10); + break; + default: + break; + } + } +} + static SDL_AppResult update_target_rect(struct pw_appstate *state, int w, int h) { if (!update_pxtexture_target(&state->framebuffer, w, h)) { return FAIL("failed to update framebuffer target"); @@ -244,15 +302,18 @@ static SDL_AppResult handle_axis(struct pw_appstate *state, return SDL_APP_CONTINUE; } -#define MAP_AXIS(sdlaxis, minus, plus) \ - case sdlaxis: \ - gamepad->b.minus = event->value < -10000; \ - gamepad->b.plus = event->value > 10000; \ - break + bool minus = event->value < -10000; + bool plus = event->value > 10000; switch (event->axis) { - MAP_AXIS(SDL_GAMEPAD_AXIS_LEFTX, left, right); - MAP_AXIS(SDL_GAMEPAD_AXIS_LEFTY, up, down); + case SDL_GAMEPAD_AXIS_LEFTX: + update_gamepad_state(state, gamepad, PW_BUTTON_LEFT, minus); + update_gamepad_state(state, gamepad, PW_BUTTON_RIGHT, plus); + break; + case SDL_GAMEPAD_AXIS_LEFTY: + update_gamepad_state(state, gamepad, PW_BUTTON_UP, minus); + update_gamepad_state(state, gamepad, PW_BUTTON_DOWN, plus); + break; } // We also update the gamepad for player 2, which receives @@ -261,15 +322,23 @@ static SDL_AppResult handle_axis(struct pw_appstate *state, if (gamepad == &state->gamepads[0].state && state->gamepads[1].handle == NULL) { gamepad++; switch (event->axis) { - MAP_AXIS(SDL_GAMEPAD_AXIS_RIGHTX, left, right); - MAP_AXIS(SDL_GAMEPAD_AXIS_RIGHTY, up, down); - MAP_AXIS(SDL_GAMEPAD_AXIS_LEFT_TRIGGER, reserved, o); - MAP_AXIS(SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, reserved, x); + case SDL_GAMEPAD_AXIS_RIGHTX: + update_gamepad_state(state, gamepad, PW_BUTTON_LEFT, minus); + update_gamepad_state(state, gamepad, PW_BUTTON_RIGHT, plus); + break; + case SDL_GAMEPAD_AXIS_RIGHTY: + update_gamepad_state(state, gamepad, PW_BUTTON_UP, minus); + update_gamepad_state(state, gamepad, PW_BUTTON_DOWN, plus); + break; + case SDL_GAMEPAD_AXIS_LEFT_TRIGGER: + update_gamepad_state(state, gamepad, PW_BUTTON_O, plus); + break; + case SDL_GAMEPAD_AXIS_RIGHT_TRIGGER: + update_gamepad_state(state, gamepad, PW_BUTTON_X, plus); + break; } } -#undef MAP_AXIS - return SDL_APP_CONTINUE; } @@ -280,73 +349,77 @@ static SDL_AppResult handle_button(struct pw_appstate *state, return SDL_APP_CONTINUE; } -#define MAP_BUTTON(sdlbtn, gbtn) \ - case sdlbtn: \ - gamepad->b.gbtn = event->down; \ - break - switch (event->button) { - MAP_BUTTON(SDL_GAMEPAD_BUTTON_SOUTH, o); - MAP_BUTTON(SDL_GAMEPAD_BUTTON_EAST, x); - MAP_BUTTON(SDL_GAMEPAD_BUTTON_WEST, x); - MAP_BUTTON(SDL_GAMEPAD_BUTTON_NORTH, o); - MAP_BUTTON(SDL_GAMEPAD_BUTTON_BACK, menu); - MAP_BUTTON(SDL_GAMEPAD_BUTTON_START, menu); - MAP_BUTTON(SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, o); - MAP_BUTTON(SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, x); - MAP_BUTTON(SDL_GAMEPAD_BUTTON_DPAD_UP, up); - MAP_BUTTON(SDL_GAMEPAD_BUTTON_DPAD_DOWN, down); - MAP_BUTTON(SDL_GAMEPAD_BUTTON_DPAD_LEFT, left); - MAP_BUTTON(SDL_GAMEPAD_BUTTON_DPAD_RIGHT, right); - } - -#undef MAP_BUTTON + case SDL_GAMEPAD_BUTTON_SOUTH: + update_gamepad_state(state, gamepad, PW_BUTTON_O, event->down); + break; + case SDL_GAMEPAD_BUTTON_EAST: + update_gamepad_state(state, gamepad, PW_BUTTON_X, event->down); + break; + case SDL_GAMEPAD_BUTTON_WEST: + update_gamepad_state(state, gamepad, PW_BUTTON_X, event->down); + break; + case SDL_GAMEPAD_BUTTON_NORTH: + update_gamepad_state(state, gamepad, PW_BUTTON_O, event->down); + break; + case SDL_GAMEPAD_BUTTON_BACK: + update_gamepad_state(state, gamepad, PW_BUTTON_MENU, event->down); + break; + case SDL_GAMEPAD_BUTTON_START: + update_gamepad_state(state, gamepad, PW_BUTTON_MENU, event->down); + break; + case SDL_GAMEPAD_BUTTON_LEFT_SHOULDER: + update_gamepad_state(state, gamepad, PW_BUTTON_O, event->down); + break; + case SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER: + update_gamepad_state(state, gamepad, PW_BUTTON_X, event->down); + break; + case SDL_GAMEPAD_BUTTON_DPAD_UP: + update_gamepad_state(state, gamepad, PW_BUTTON_UP, event->down); + break; + case SDL_GAMEPAD_BUTTON_DPAD_DOWN: + update_gamepad_state(state, gamepad, PW_BUTTON_DOWN, event->down); + break; + case SDL_GAMEPAD_BUTTON_DPAD_LEFT: + update_gamepad_state(state, gamepad, PW_BUTTON_LEFT, event->down); + break; + case SDL_GAMEPAD_BUTTON_DPAD_RIGHT: + update_gamepad_state(state, gamepad, PW_BUTTON_RIGHT, event->down); + break; + } return SDL_APP_CONTINUE; } static SDL_AppResult handle_keyboard(struct pw_appstate *state, SDL_KeyboardEvent *event) { -#define MAP_KEY(sdlk, gbtn) \ - case sdlk: \ - state->gamepads[0].state.b.gbtn = event->down; \ - break + union pw_gamepad_state *gamepad = &state->gamepads[0].state; switch (event->key) { case SDLK_ESCAPE: case SDLK_Q: return SDL_APP_SUCCESS; - MAP_KEY(SDLK_Z, o); - MAP_KEY(SDLK_X, x); - MAP_KEY(SDLK_UP, up); - MAP_KEY(SDLK_DOWN, down); - MAP_KEY(SDLK_LEFT, left); - MAP_KEY(SDLK_RIGHT, right); - MAP_KEY(SDLK_RETURN, menu); - } - -#undef MAP_BUTTON - - if (state->ncarts > 0 && event->down) { - int cartidx = state->cartidx; - switch (event->key) { - case SDLK_LEFT: - state->cartidx = SDL_max(0, state->cartidx - 1); - break; - case SDLK_RIGHT: - state->cartidx = SDL_min(state->ncarts - 1, state->cartidx + 1); - break; - case SDLK_UP: - state->cartidx = SDL_max(0, state->cartidx - 10); - break; - case SDLK_DOWN: - state->cartidx = SDL_min(state->ncarts - 1, state->cartidx + 10); - break; - } - - if (state->cartidx != cartidx && !load_cart(&state->cartpng, &state->cart, state->carts[state->cartidx])) { - return FAIL("failed to load cart png"); - } + case SDLK_Z: + update_gamepad_state(state, gamepad, PW_BUTTON_O, event->down); + break; + case SDLK_X: + update_gamepad_state(state, gamepad, PW_BUTTON_X, event->down); + break; + case SDLK_UP: + update_gamepad_state(state, gamepad, PW_BUTTON_UP, event->down); + break; + case SDLK_DOWN: + update_gamepad_state(state, gamepad, PW_BUTTON_DOWN, event->down); + break; + case SDLK_LEFT: + update_gamepad_state(state, gamepad, PW_BUTTON_LEFT, event->down); + break; + case SDLK_RIGHT: + update_gamepad_state(state, gamepad, PW_BUTTON_RIGHT, event->down); + break; + case SDLK_RETURN: + update_gamepad_state(state, gamepad, PW_BUTTON_MENU, event->down); + break; } return SDL_APP_CONTINUE; @@ -450,6 +523,13 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { SDL_AppResult SDL_AppIterate(void *appstate) { struct pw_appstate *state = appstate; + if (state->cartidx != state->cartidx2) { + state->cartidx = state->cartidx2; + if (!load_cart(&state->cartpng, &state->cart, state->carts[state->cartidx])) { + return FAIL("failed to load cart png"); + } + } + void *fb; int pitch; if (!SDL_LockTexture(state->framebuffer.handle, NULL, &fb, &pitch)) { |
