twn_input: singleton rework, twn_control.h and fixes

This commit is contained in:
2024-10-08 10:12:30 +03:00
parent aef3f6444e
commit 0ede612bec
17 changed files with 677 additions and 647 deletions

View File

@ -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");
}

View File

@ -19,7 +19,6 @@ typedef struct State
{
Bunny bunnies[MAX_BUNNIES];
int bunniesCount;
InputState mouse_state;
} State;
#endif