twn_input: singleton rework, twn_control.h and fixes
This commit is contained in:
		| @@ -16,14 +16,17 @@ | ||||
| void handle_input(void) | ||||
| { | ||||
|     State *state = ctx.udata; | ||||
|     if (ctx.input.mouse_state == 1 && ctx.input.mouse_window_position.y > 60) | ||||
|  | ||||
|     if (ctx.mouse_window_position.y <= 60) | ||||
|         return; | ||||
|  | ||||
|     if (input_is_action_pressed("add_a_bit")) | ||||
|     { // Left click | ||||
|         for (int i = 0; i < LEFT_CLICK_ADD; i++) | ||||
|         { | ||||
|             if (state->bunniesCount < MAX_BUNNIES) | ||||
|             { | ||||
|                 state->bunnies[state->bunniesCount].position = | ||||
|                     (Vec2){(float)ctx.input.mouse_window_position.x, (float)ctx.input.mouse_window_position.y}; | ||||
|                 state->bunnies[state->bunniesCount].position = input_get_action_position("add_a_bit"); | ||||
|                 state->bunnies[state->bunniesCount].speed.x = (float)(rand() % 500 - 250) / 60.0; | ||||
|                 state->bunnies[state->bunniesCount].speed.y = (float)(rand() % 500 - 250) / 60.0; | ||||
|                 state->bunnies[state->bunniesCount].color = | ||||
| @@ -33,14 +36,13 @@ void handle_input(void) | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     if (ctx.input.mouse_state == 4) | ||||
|     if (input_is_action_pressed("add_a_lot")) | ||||
|     { // Right click | ||||
|         for (int i = 0; i < RIGHT_CLICK_ADD; i++) | ||||
|         { | ||||
|             if (state->bunniesCount < MAX_BUNNIES) | ||||
|             { | ||||
|                 state->bunnies[state->bunniesCount].position = | ||||
|                     (Vec2){(float)ctx.input.mouse_window_position.x, (float)ctx.input.mouse_window_position.y}; | ||||
|                 state->bunnies[state->bunniesCount].position = input_get_action_position("add_a_lot"); | ||||
|                 state->bunnies[state->bunniesCount].speed.x = (float)(rand() % 500 - 250) / 60.0; | ||||
|                 state->bunnies[state->bunniesCount].speed.y = (float)(rand() % 500 - 250) / 60.0; | ||||
|                 state->bunnies[state->bunniesCount].color = | ||||
| @@ -53,8 +55,7 @@ void handle_input(void) | ||||
|  | ||||
| void game_tick(void) | ||||
| { | ||||
|     static char bunny_count_text[64]; | ||||
|     static char bunny_path[64] = "wabbit_alpha.png"; | ||||
|     char bunny_count_text[64]; | ||||
|  | ||||
|     // State *state = ctx.udata; | ||||
|     if (ctx.initialization_needed) | ||||
| @@ -62,7 +63,12 @@ void game_tick(void) | ||||
|         // Allocating State struct to store data there | ||||
|         if (!ctx.udata) | ||||
|             ctx.udata = ccalloc(1, sizeof(State)); | ||||
|         ((State *)ctx.udata)->bunniesCount = 0; | ||||
|  | ||||
|         input_add_action("add_a_bit"); | ||||
|         input_bind_action_control("add_a_bit", CONTROL_LEFT_MOUSE); | ||||
|  | ||||
|         input_add_action("add_a_lot"); | ||||
|         input_bind_action_control("add_a_lot", CONTROL_RIGHT_MOUSE); | ||||
|     } | ||||
|  | ||||
|     State *state = ctx.udata; | ||||
| @@ -90,16 +96,16 @@ void game_tick(void) | ||||
|  | ||||
|     for (int i = 0; i < state->bunniesCount; i++) | ||||
|     { // Draw each bunny based on their position and color, also scale accordingly | ||||
|         m_sprite(m_set(path, bunny_path), | ||||
|         m_sprite(m_set(path, "wabbit_alpha.png"), | ||||
|                  m_set(rect, ((Rect){.x = (int)state->bunnies[i].position.x, | ||||
|                                      .y = (int)state->bunnies[i].position.y, | ||||
|                                      .w = BUNNY_W * SPRITE_SCALE, | ||||
|                                      .h = BUNNY_H * SPRITE_SCALE})), | ||||
|                  m_opt(color, (state->bunnies[i].color)), m_opt(stretch, true), ); | ||||
|     } | ||||
|  | ||||
|     // Formatting text to display, might want to add FPS here too | ||||
|     snprintf(bunny_count_text, 64, "Bunnies: %d", state->bunniesCount); | ||||
|  | ||||
|     draw_text(bunny_count_text, (Vec2){0, 0}, 40, BLACK, "/fonts/kenney-pixel.ttf"); | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -19,7 +19,6 @@ typedef struct State | ||||
| { | ||||
|     Bunny bunnies[MAX_BUNNIES]; | ||||
|     int bunniesCount; | ||||
|     InputState mouse_state; | ||||
| } State; | ||||
|  | ||||
| #endif | ||||
|   | ||||
| @@ -19,44 +19,44 @@ void game_tick(void) { | ||||
|             state->scene = title_scene(state); | ||||
|         } | ||||
|  | ||||
|         input_add_action(&ctx.input, "debug_toggle"); | ||||
|         input_bind_action_scancode(&ctx.input, "debug_toggle", SCANCODE_BACKSPACE); | ||||
|         input_add_action("debug_toggle"); | ||||
|         input_bind_action_control("debug_toggle", CONTROL_BACKSPACE); | ||||
|  | ||||
|         input_add_action(&ctx.input, "debug_dump_atlases"); | ||||
|         input_bind_action_scancode(&ctx.input, "debug_dump_atlases", SCANCODE_HOME); | ||||
|         input_add_action("debug_dump_atlases"); | ||||
|         input_bind_action_control("debug_dump_atlases", CONTROL_HOME); | ||||
|  | ||||
|         input_add_action(&ctx.input, "player_left"); | ||||
|         input_bind_action_scancode(&ctx.input, "player_left", SCANCODE_A); | ||||
|         input_add_action("player_left"); | ||||
|         input_bind_action_control("player_left", CONTROL_A); | ||||
|  | ||||
|         input_add_action(&ctx.input, "player_right"); | ||||
|         input_bind_action_scancode(&ctx.input, "player_right", SCANCODE_D); | ||||
|         input_add_action("player_right"); | ||||
|         input_bind_action_control("player_right", CONTROL_D); | ||||
|  | ||||
|         input_add_action(&ctx.input, "player_forward"); | ||||
|         input_bind_action_scancode(&ctx.input, "player_forward", SCANCODE_W); | ||||
|         input_add_action("player_forward"); | ||||
|         input_bind_action_control("player_forward", CONTROL_W); | ||||
|  | ||||
|         input_add_action(&ctx.input, "player_backward"); | ||||
|         input_bind_action_scancode(&ctx.input, "player_backward", SCANCODE_S); | ||||
|         input_add_action("player_backward"); | ||||
|         input_bind_action_control("player_backward", CONTROL_S); | ||||
|  | ||||
|         input_add_action(&ctx.input, "player_jump"); | ||||
|         input_bind_action_scancode(&ctx.input, "player_jump", SCANCODE_SPACE); | ||||
|         input_add_action("player_jump"); | ||||
|         input_bind_action_control("player_jump", CONTROL_SPACE); | ||||
|  | ||||
|         input_add_action(&ctx.input, "player_run"); | ||||
|         input_bind_action_scancode(&ctx.input, "player_run", SCANCODE_LSHIFT); | ||||
|         input_add_action("player_run"); | ||||
|         input_bind_action_control("player_run", CONTROL_LSHIFT); | ||||
|  | ||||
|         input_add_action(&ctx.input, "ui_accept"); | ||||
|         input_bind_action_scancode(&ctx.input, "ui_accept", SCANCODE_RETURN); | ||||
|         input_add_action("ui_accept"); | ||||
|         input_bind_action_control("ui_accept", CONTROL_RETURN); | ||||
|  | ||||
|         input_add_action(&ctx.input, "mouse_capture_toggle"); | ||||
|         input_bind_action_scancode(&ctx.input, "mouse_capture_toggle", SCANCODE_ESCAPE); | ||||
|         input_add_action("mouse_capture_toggle"); | ||||
|         input_bind_action_control("mouse_capture_toggle", CONTROL_ESCAPE); | ||||
|     } | ||||
|  | ||||
|     State *state = ctx.udata; | ||||
|  | ||||
|     if (input_is_action_just_pressed(&ctx.input, "debug_toggle")) { | ||||
|     if (input_is_action_just_pressed("debug_toggle")) { | ||||
|         ctx.debug = !ctx.debug; | ||||
|     } | ||||
|  | ||||
|     if (input_is_action_just_pressed(&ctx.input, "debug_dump_atlases")) { | ||||
|     if (input_is_action_just_pressed("debug_dump_atlases")) { | ||||
|         textures_dump_atlases(); | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -17,22 +17,22 @@ static void update_timers(Player *player) { | ||||
| } | ||||
|  | ||||
|  | ||||
| static void input_move(InputState *input, Player *player) { | ||||
| static void input_move(Player *player) { | ||||
|     /* apply horizontal damping when the player stops moving */ | ||||
|     /* in other words, make it decelerate to a standstill */ | ||||
|     if (!input_is_action_pressed(input, "player_left") && | ||||
|         !input_is_action_pressed(input, "player_right")) | ||||
|     if (!input_is_action_pressed("player_left") && | ||||
|         !input_is_action_pressed("player_right")) | ||||
|     { | ||||
|         player->dx *= player->horizontal_damping; | ||||
|     } | ||||
|  | ||||
|     int input_dir = 0; | ||||
|     if (input_is_action_pressed(input, "player_left")) | ||||
|     if (input_is_action_pressed("player_left")) | ||||
|         input_dir = -1; | ||||
|     if (input_is_action_pressed(input, "player_right")) | ||||
|     if (input_is_action_pressed("player_right")) | ||||
|         input_dir = 1; | ||||
|     if (input_is_action_pressed(input, "player_left") && | ||||
|         input_is_action_pressed(input, "player_right")) | ||||
|     if (input_is_action_pressed("player_left") && | ||||
|         input_is_action_pressed("player_right")) | ||||
|         input_dir = 0; | ||||
|  | ||||
|     player->dx += (float)input_dir * player->run_horizontal_speed; | ||||
| @@ -53,10 +53,10 @@ static void jump(Player *player) { | ||||
| } | ||||
|  | ||||
|  | ||||
| static void input_jump(InputState *input, Player *player) { | ||||
| static void input_jump(Player *player) { | ||||
|     player->current_gravity_multiplier = player->jump_default_multiplier; | ||||
|  | ||||
|     if (input_is_action_just_pressed(input, "player_jump")) { | ||||
|     if (input_is_action_just_pressed("player_jump")) { | ||||
|         player->jump_air_timer = 0; | ||||
|         player->jump_buffer_timer = player->jump_buffer_ticks; | ||||
|  | ||||
| @@ -65,7 +65,7 @@ static void input_jump(InputState *input, Player *player) { | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     if (input_is_action_pressed(input, "player_jump")) { | ||||
|     if (input_is_action_pressed("player_jump")) { | ||||
|         if (player->action != PLAYER_ACTION_GROUND && player->jump_air_timer > 0) { | ||||
|             player->current_gravity_multiplier = player->jump_boosted_multiplier; | ||||
|             player->dy += player->jump_force_increase; | ||||
| @@ -284,8 +284,8 @@ void player_destroy(Player *player) { | ||||
| void player_calc(Player *player) { | ||||
|     update_timers(player); | ||||
|  | ||||
|     input_move(&ctx.input, player); | ||||
|     input_jump(&ctx.input, player); | ||||
|     input_move(player); | ||||
|     input_jump(player); | ||||
|  | ||||
|     player->rect.x += player->dx; | ||||
|     update_collider_x(player); | ||||
|   | ||||
| @@ -16,22 +16,22 @@ static void ingame_tick(State *state) { | ||||
|  | ||||
|     const Vec3 right = m_vec_norm(m_vec_cross(scn->cam.target, scn->cam.up)); | ||||
|     const float speed = 0.04f; /* TODO: put this in a better place */ | ||||
|     if (input_is_action_pressed(&ctx.input, "player_left")) | ||||
|     if (input_is_action_pressed("player_left")) | ||||
|         scn->cam.pos = vec3_sub(scn->cam.pos, m_vec_scale(right, speed)); | ||||
|  | ||||
|     if (input_is_action_pressed(&ctx.input, "player_right")) | ||||
|     if (input_is_action_pressed("player_right")) | ||||
|         scn->cam.pos = vec3_add(scn->cam.pos, m_vec_scale(right, speed)); | ||||
|  | ||||
|     if (input_is_action_pressed(&ctx.input, "player_forward")) | ||||
|     if (input_is_action_pressed("player_forward")) | ||||
|         scn->cam.pos = vec3_add(scn->cam.pos, m_vec_scale(scn->cam.target, speed)); | ||||
|  | ||||
|     if (input_is_action_pressed(&ctx.input, "player_backward")) | ||||
|     if (input_is_action_pressed("player_backward")) | ||||
|         scn->cam.pos = vec3_sub(scn->cam.pos, m_vec_scale(scn->cam.target, speed)); | ||||
|  | ||||
|     if (input_is_action_pressed(&ctx.input, "player_jump")) | ||||
|     if (input_is_action_pressed("player_jump")) | ||||
|         scn->cam.pos.y += speed; | ||||
|  | ||||
|     if (input_is_action_pressed(&ctx.input, "player_run")) | ||||
|     if (input_is_action_pressed("player_run")) | ||||
|         scn->cam.pos.y -= speed; | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -14,7 +14,7 @@ static void title_tick(State *state) { | ||||
|     SceneTitle *scn = (SceneTitle *)state->scene; | ||||
|     (void)scn; | ||||
|  | ||||
|     if (input_is_action_just_pressed(&state->ctx->input, "ui_accept")) { | ||||
|     if (input_is_action_just_pressed("ui_accept")) { | ||||
|         switch_to(state, ingame_scene); | ||||
|         return; | ||||
|     } | ||||
|   | ||||
| @@ -20,44 +20,44 @@ void game_tick(void) { | ||||
|             state->scene = title_scene(state); | ||||
|         } | ||||
|  | ||||
|         input_add_action(&ctx.input, "debug_toggle"); | ||||
|         input_bind_action_scancode(&ctx.input, "debug_toggle", SCANCODE_BACKSPACE); | ||||
|         input_add_action("debug_toggle"); | ||||
|         input_bind_action_control("debug_toggle", CONTROL_BACKSPACE); | ||||
|  | ||||
|         input_add_action(&ctx.input, "debug_dump_atlases"); | ||||
|         input_bind_action_scancode(&ctx.input, "debug_dump_atlases", SCANCODE_HOME); | ||||
|         input_add_action("debug_dump_atlases"); | ||||
|         input_bind_action_control("debug_dump_atlases", CONTROL_HOME); | ||||
|  | ||||
|         input_add_action(&ctx.input, "player_left"); | ||||
|         input_bind_action_scancode(&ctx.input, "player_left", SCANCODE_A); | ||||
|         input_add_action("player_left"); | ||||
|         input_bind_action_control("player_left", CONTROL_A); | ||||
|  | ||||
|         input_add_action(&ctx.input, "player_right"); | ||||
|         input_bind_action_scancode(&ctx.input, "player_right", SCANCODE_D); | ||||
|         input_add_action("player_right"); | ||||
|         input_bind_action_control("player_right", CONTROL_D); | ||||
|  | ||||
|         input_add_action(&ctx.input, "player_forward"); | ||||
|         input_bind_action_scancode(&ctx.input, "player_forward", SCANCODE_W); | ||||
|         input_add_action("player_forward"); | ||||
|         input_bind_action_control("player_forward", CONTROL_W); | ||||
|  | ||||
|         input_add_action(&ctx.input, "player_backward"); | ||||
|         input_bind_action_scancode(&ctx.input, "player_backward", SCANCODE_S); | ||||
|         input_add_action("player_backward"); | ||||
|         input_bind_action_control("player_backward", CONTROL_S); | ||||
|  | ||||
|         input_add_action(&ctx.input, "player_jump"); | ||||
|         input_bind_action_scancode(&ctx.input, "player_jump", SCANCODE_SPACE); | ||||
|         input_add_action("player_jump"); | ||||
|         input_bind_action_control("player_jump", CONTROL_SPACE); | ||||
|  | ||||
|         input_add_action(&ctx.input, "player_run"); | ||||
|         input_bind_action_scancode(&ctx.input, "player_run", SCANCODE_LSHIFT); | ||||
|         input_add_action("player_run"); | ||||
|         input_bind_action_control("player_run", CONTROL_LSHIFT); | ||||
|  | ||||
|         input_add_action(&ctx.input, "ui_accept"); | ||||
|         input_bind_action_scancode(&ctx.input, "ui_accept", SCANCODE_RETURN); | ||||
|         input_add_action("ui_accept"); | ||||
|         input_bind_action_control("ui_accept", CONTROL_RETURN); | ||||
|  | ||||
|         input_add_action(&ctx.input, "mouse_capture_toggle"); | ||||
|         input_bind_action_scancode(&ctx.input, "mouse_capture_toggle", SCANCODE_ESCAPE); | ||||
|         input_add_action("mouse_capture_toggle"); | ||||
|         input_bind_action_control("mouse_capture_toggle", CONTROL_ESCAPE); | ||||
|     } | ||||
|  | ||||
|     State *state = ctx.udata; | ||||
|  | ||||
|     if (input_is_action_just_pressed(&ctx.input, "debug_toggle")) { | ||||
|     if (input_is_action_just_pressed("debug_toggle")) { | ||||
|         ctx.debug = !ctx.debug; | ||||
|     } | ||||
|  | ||||
|     if (input_is_action_just_pressed(&ctx.input, "debug_dump_atlases")) { | ||||
|     if (input_is_action_just_pressed("debug_dump_atlases")) { | ||||
|         textures_dump_atlases(); | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -15,10 +15,10 @@ | ||||
| static void ingame_tick(State *state) { | ||||
|     SceneIngame *scn = (SceneIngame *)state->scene; | ||||
|  | ||||
|     if (input_is_mouse_captured(&ctx.input)) { | ||||
|     if (input_is_mouse_captured()) { | ||||
|         const float sensitivity = 0.6f; /* TODO: put this in a better place */ | ||||
|         scn->yaw += (float)ctx.input.mouse_relative_position.x * sensitivity; | ||||
|         scn->pitch -= (float)ctx.input.mouse_relative_position.y * sensitivity; | ||||
|         scn->yaw += (float)ctx.mouse_relative_position.x * sensitivity; | ||||
|         scn->pitch -= (float)ctx.mouse_relative_position.y * sensitivity; | ||||
|         scn->pitch = clampf(scn->pitch, -89.0f, 89.0f); | ||||
|  | ||||
|         const float yaw_rad = scn->yaw * (float)DEG2RAD; | ||||
| @@ -33,27 +33,27 @@ static void ingame_tick(State *state) { | ||||
|  | ||||
|     const Vec3 right = m_vec_norm(m_vec_cross(scn->cam.target, scn->cam.up)); | ||||
|     const float speed = 0.04f; /* TODO: put this in a better place */ | ||||
|     if (input_is_action_pressed(&ctx.input, "player_left")) | ||||
|     if (input_is_action_pressed("player_left")) | ||||
|         scn->cam.pos = vec3_sub(scn->cam.pos, m_vec_scale(right, speed)); | ||||
|  | ||||
|     if (input_is_action_pressed(&ctx.input, "player_right")) | ||||
|     if (input_is_action_pressed("player_right")) | ||||
|         scn->cam.pos = vec3_add(scn->cam.pos, m_vec_scale(right, speed)); | ||||
|  | ||||
|     if (input_is_action_pressed(&ctx.input, "player_forward")) | ||||
|     if (input_is_action_pressed("player_forward")) | ||||
|         scn->cam.pos = vec3_add(scn->cam.pos, m_vec_scale(scn->cam.target, speed)); | ||||
|  | ||||
|     if (input_is_action_pressed(&ctx.input, "player_backward")) | ||||
|     if (input_is_action_pressed("player_backward")) | ||||
|         scn->cam.pos = vec3_sub(scn->cam.pos, m_vec_scale(scn->cam.target, speed)); | ||||
|  | ||||
|     if (input_is_action_pressed(&ctx.input, "player_jump")) | ||||
|     if (input_is_action_pressed("player_jump")) | ||||
|         scn->cam.pos.y += speed; | ||||
|  | ||||
|     if (input_is_action_pressed(&ctx.input, "player_run")) | ||||
|     if (input_is_action_pressed("player_run")) | ||||
|         scn->cam.pos.y -= speed; | ||||
|  | ||||
|     /* toggle mouse capture with end key */ | ||||
|     if (input_is_action_just_pressed(&ctx.input, "mouse_capture_toggle")) { | ||||
|         input_set_mouse_captured(&ctx.input, !input_is_mouse_captured(&ctx.input)); | ||||
|     if (input_is_action_just_pressed("mouse_capture_toggle")) { | ||||
|         input_set_mouse_captured(!input_is_mouse_captured()); | ||||
|     } | ||||
|  | ||||
|     draw_camera(&scn->cam); | ||||
| @@ -111,7 +111,7 @@ Scene *ingame_scene(State *state) { | ||||
|             m_opt(channel, "soundtrack"), | ||||
|             m_opt(repeat, true)); | ||||
|  | ||||
|     input_set_mouse_captured(&ctx.input, true); | ||||
|     input_set_mouse_captured(true); | ||||
|  | ||||
|     return (Scene *)new_scene; | ||||
| } | ||||
|   | ||||
| @@ -11,7 +11,7 @@ static void title_tick(State *state) { | ||||
|     SceneTitle *scn = (SceneTitle *)state->scene; | ||||
|     (void)scn; | ||||
|  | ||||
|     if (input_is_action_just_pressed(&state->ctx->input, "ui_accept")) { | ||||
|     if (input_is_action_just_pressed("ui_accept")) { | ||||
|         switch_to(state, ingame_scene); | ||||
|         return; | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user