From 0ca305f2301fa1087a74de121be7d37c69a93951 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Sun, 16 Jan 2022 14:26:29 +0000 Subject: [PATCH] Add control rebinding --- Makefile | 7 ++- rebind_jump1_tile.txt | 13 +++++ rebind_jump2_tile.txt | 13 +++++ rebind_left1_tile.txt | 13 +++++ rebind_left2_tile.txt | 13 +++++ rebind_rebind1_alt_tile.txt | 13 +++++ rebind_rebind1_tile.txt | 13 +++++ rebind_rebind2_tile.txt | 13 +++++ rebind_rebind3_tile.txt | 13 +++++ rebind_right1_tile.txt | 13 +++++ rebind_right2_tile.txt | 13 +++++ switcher.c | 100 ++++++++++++++++++++++++++++++------ 12 files changed, 220 insertions(+), 17 deletions(-) create mode 100644 rebind_jump1_tile.txt create mode 100644 rebind_jump2_tile.txt create mode 100644 rebind_left1_tile.txt create mode 100644 rebind_left2_tile.txt create mode 100644 rebind_rebind1_alt_tile.txt create mode 100644 rebind_rebind1_tile.txt create mode 100644 rebind_rebind2_tile.txt create mode 100644 rebind_rebind3_tile.txt create mode 100644 rebind_right1_tile.txt create mode 100644 rebind_right2_tile.txt diff --git a/Makefile b/Makefile index ab5402f..085962b 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,12 @@ TILES = player_stand_tile.inc \ grass_tile.inc dirt_tile.inc flag_tile.inc \ switch1_off_tile.inc switch1_on_tile.inc \ tile1_off_tile.inc tile1_on_tile.inc \ - stage1_tile.inc stage2_tile.inc star_tile.inc + stage1_tile.inc stage2_tile.inc star_tile.inc \ + rebind_rebind1_tile.inc rebind_rebind2_tile.inc rebind_rebind3_tile.inc \ + rebind_left1_tile.inc rebind_left2_tile.inc \ + rebind_right1_tile.inc rebind_right2_tile.inc \ + rebind_jump1_tile.inc rebind_jump2_tile.inc \ + rebind_rebind1_alt_tile.inc all: $(NAME) diff --git a/rebind_jump1_tile.txt b/rebind_jump1_tile.txt new file mode 100644 index 0000000..f243ad6 --- /dev/null +++ b/rebind_jump1_tile.txt @@ -0,0 +1,13 @@ +xTEXT + BG +---------- + + + x + x x x + x x x xx + x x x x + x x xx x + x x x x + x +x diff --git a/rebind_jump2_tile.txt b/rebind_jump2_tile.txt new file mode 100644 index 0000000..9bf11fe --- /dev/null +++ b/rebind_jump2_tile.txt @@ -0,0 +1,13 @@ +xTEXT + BG +---------- + + + + xxx +x xx x x + x x x x + x x x x + x x xxx + x + x diff --git a/rebind_left1_tile.txt b/rebind_left1_tile.txt new file mode 100644 index 0000000..c5d58e9 --- /dev/null +++ b/rebind_left1_tile.txt @@ -0,0 +1,13 @@ +xTEXT + BG +---------- +x x +x x +x x +x xx xxx +x x x x +x xxx x +x x x +x xx x + + diff --git a/rebind_left2_tile.txt b/rebind_left2_tile.txt new file mode 100644 index 0000000..3d8e715 --- /dev/null +++ b/rebind_left2_tile.txt @@ -0,0 +1,13 @@ +xTEXT + BG +---------- + + + x + xxx + x + x + x + x + + diff --git a/rebind_rebind1_alt_tile.txt b/rebind_rebind1_alt_tile.txt new file mode 100644 index 0000000..54af490 --- /dev/null +++ b/rebind_rebind1_alt_tile.txt @@ -0,0 +1,13 @@ +xTEXT + BG +---------- + + + +x x xx +xx x x x +x xxx +x x +x xx + + diff --git a/rebind_rebind1_tile.txt b/rebind_rebind1_tile.txt new file mode 100644 index 0000000..8b65d48 --- /dev/null +++ b/rebind_rebind1_tile.txt @@ -0,0 +1,13 @@ +xTEXT + BG +---------- +xxx +x x +xxx +x x xx +x x x x +x x xxx +x x x +x x xx + + diff --git a/rebind_rebind2_tile.txt b/rebind_rebind2_tile.txt new file mode 100644 index 0000000..9661e3f --- /dev/null +++ b/rebind_rebind2_tile.txt @@ -0,0 +1,13 @@ +xTEXT + BG +---------- +x +x +x x +xxx x +x x x xxx +x x x x +x x x x +xxx x x + + diff --git a/rebind_rebind3_tile.txt b/rebind_rebind3_tile.txt new file mode 100644 index 0000000..4a03130 --- /dev/null +++ b/rebind_rebind3_tile.txt @@ -0,0 +1,13 @@ +xTEXT + BG +---------- + x + x + x + xxx + x x +x x x +x x x +x xxx + + diff --git a/rebind_right1_tile.txt b/rebind_right1_tile.txt new file mode 100644 index 0000000..c0854e7 --- /dev/null +++ b/rebind_right1_tile.txt @@ -0,0 +1,13 @@ +xTEXT + BG +---------- + + + x +x x xx +xx x x x +x x x +x x x +x x xx + + x diff --git a/rebind_right2_tile.txt b/rebind_right2_tile.txt new file mode 100644 index 0000000..addb5b4 --- /dev/null +++ b/rebind_right2_tile.txt @@ -0,0 +1,13 @@ +xTEXT + BG +---------- + x + x + x x +x xxx xxx +x x x x +x x x x +x x x x +x x x x +x + diff --git a/switcher.c b/switcher.c index 6bd66df..517931f 100644 --- a/switcher.c +++ b/switcher.c @@ -56,6 +56,17 @@ static enum palette playfield[PLAYFIELD_SIDE * PLAYFIELD_SIDE]; #include "stage2_tile.inc" #include "star_tile.inc" +#include "rebind_rebind1_tile.inc" +#include "rebind_rebind2_tile.inc" +#include "rebind_rebind3_tile.inc" +#include "rebind_left1_tile.inc" +#include "rebind_left2_tile.inc" +#include "rebind_right1_tile.inc" +#include "rebind_right2_tile.inc" +#include "rebind_jump1_tile.inc" +#include "rebind_jump2_tile.inc" +#include "rebind_rebind1_alt_tile.inc" + static char tilemap[TILES * TILES + 1]; static char *stages[] = { " " @@ -99,19 +110,49 @@ static bool left_held, right_held, jump_held; static double jump_maxheight = 21; static double jump_duration = 0.4; +static int left_key = KBKEY_LEFT; +static int right_key = KBKEY_RIGHT; +static int jump_key = KBKEY_SPACE; +static int rebind_key = KBKEY_R; + +enum rebind_state { PLAYING, LEFT, RIGHT, JUMP, REBIND }; +enum rebind_state rebind_state; + static void on_keyboard(void *context, uint32_t window, uint32_t codepoint) { (void) context; if (window != main_window) return; int key = KBKEY_DECODE(codepoint); - switch (key) { - // TODO: rebinding - case KBKEY_LEFT: left_held = true; break; - case -KBKEY_LEFT: left_held = false; break; - case KBKEY_RIGHT: right_held = true; break; - case -KBKEY_RIGHT: right_held = false; break; - case KBKEY_SPACE: jump_held = true; break; - case -KBKEY_SPACE: jump_held = false; break; + switch (rebind_state) { + case PLAYING: + if (key == -rebind_key) rebind_state = LEFT; + else if (key == left_key) left_held = true; + else if (key == -left_key) left_held = false; + else if (key == right_key) right_held = true; + else if (key == -right_key) right_held = false; + else if (key == jump_key) jump_held = true; + else if (key == -jump_key) jump_held = false; + break; + case LEFT: + if (key >= 0) return; + left_key = -key; + rebind_state = RIGHT; + break; + case RIGHT: + if (key >= 0) return; + right_key = -key; + rebind_state = JUMP; + break; + case JUMP: + if (key >= 0) return; + jump_key = -key; + rebind_state = REBIND; + break; + case REBIND: + if (key >= 0) return; + rebind_key = -key; + rebind_state = PLAYING; + break; } } @@ -185,7 +226,7 @@ static void initialize(void) { switch (game_mode) { case TITLESCREEN: initialize_titlescreen(); break; case STAGE: initialize_stage(); break; - default: printf("%i\n", game_mode); break; + default: printf("game_mode %i\n", game_mode); } } @@ -347,10 +388,11 @@ static void update_titlescreen(struct timespec now, struct timespec dt_timespec) } static void update(struct timespec now, struct timespec dt_timespec) { + if (rebind_state != PLAYING) return; switch (game_mode) { case TITLESCREEN: update_titlescreen(now, dt_timespec); break; case STAGE: update_stage(now, dt_timespec); break; - default: printf("%i\n", game_mode); break; + default: printf("game_mode %i\n", game_mode); } } @@ -385,7 +427,6 @@ static void draw_tiles(void) { } static void draw_stage(struct timespec now) { - memset(playfield, 0, sizeof(playfield)); draw_tiles(); if (right_held && now.tv_nsec % 500000000L < 250000000L && on_ground) draw_tile(player_walk2_tile, player_x, player_y); @@ -401,7 +442,6 @@ static void draw_stage(struct timespec now) { static void draw_titlescreen(struct timespec now) { (void) now; - memset(playfield, 0, sizeof(playfield)); for (size_t i = 0; i < STAGES; i++) { size_t x = i * TILE_SIDE + TILE_SIDE; size_t y = 0; @@ -417,10 +457,38 @@ static void draw_titlescreen(struct timespec now) { } static void draw(struct timespec now) { - switch (game_mode) { - case TITLESCREEN: draw_titlescreen(now); break; - case STAGE: draw_stage(now); break; - default: printf("%i\n", game_mode); break; + memset(playfield, 0, sizeof(playfield)); + if (rebind_state == PLAYING) { + switch (game_mode) { + case TITLESCREEN: draw_titlescreen(now); break; + case STAGE: draw_stage(now); break; + default: printf("game_mode %i\n", game_mode); + } + } else { + draw_tile(rebind_rebind1_tile, 0 * TILE_SIDE, 0); + draw_tile(rebind_rebind2_tile, 1 * TILE_SIDE, 0); + draw_tile(rebind_rebind3_tile, 2 * TILE_SIDE, 0); + switch (rebind_state) { + case LEFT: + draw_tile(rebind_left1_tile, 3 * TILE_SIDE, 0); + draw_tile(rebind_left2_tile, 4 * TILE_SIDE, 0); + break; + case RIGHT: + draw_tile(rebind_right1_tile, 3 * TILE_SIDE, 0); + draw_tile(rebind_right2_tile, 4 * TILE_SIDE, 0); + break; + case JUMP: + draw_tile(rebind_jump1_tile, 3 * TILE_SIDE, 0); + draw_tile(rebind_jump2_tile, 4 * TILE_SIDE, 0); + break; + case REBIND: + draw_tile(rebind_rebind1_alt_tile, 3 * TILE_SIDE, 0); + draw_tile(rebind_rebind2_tile, 4 * TILE_SIDE, 0); + draw_tile(rebind_rebind3_tile, 5 * TILE_SIDE, 0); + break; + default: + printf("rebind_state %i\n", rebind_state); + } } }