yet another api rework, removal of integer types in public api, optionals at the end, some cleaning

This commit is contained in:
veclavtalica
2024-10-29 12:25:24 +03:00
parent 6464d14b3e
commit 9121da0675
30 changed files with 191 additions and 342 deletions

View File

@ -20,13 +20,13 @@ static void handle_input(void)
if (ctx.mouse_position.y <= 60)
return;
if (input_is_action_pressed("add_a_bit"))
if (input_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 = input_get_action_position("add_a_bit");
state->bunnies[state->bunniesCount].position = input_action_position("add_a_bit");
state->bunnies[state->bunniesCount].speed.x = (float)(rand() % 500 - 250) / 60.0f;
state->bunnies[state->bunniesCount].speed.y = (float)(rand() % 500 - 250) / 60.0f;
state->bunnies[state->bunniesCount].color =
@ -38,13 +38,13 @@ static void handle_input(void)
}
}
if (input_is_action_pressed("add_a_lot"))
if (input_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 = input_get_action_position("add_a_lot");
state->bunnies[state->bunniesCount].position = input_action_position("add_a_lot");
state->bunnies[state->bunniesCount].speed.x = (float)(rand() % 500 - 250) / 60.0f;
state->bunnies[state->bunniesCount].speed.y = (float)(rand() % 500 - 250) / 60.0f;
state->bunnies[state->bunniesCount].color =
@ -69,8 +69,8 @@ void game_tick(void)
ctx.udata = ccalloc(1, sizeof(State));
}
input_bind_action_control("add_a_bit", CONTROL_LEFT_MOUSE);
input_bind_action_control("add_a_lot", CONTROL_RIGHT_MOUSE);
input_action("add_a_bit", CONTROL_LEFT_MOUSE);
input_action("add_a_lot", CONTROL_RIGHT_MOUSE);
State *state = ctx.udata;
@ -94,7 +94,7 @@ 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, "wabbit_alpha.png"),
m_sprite(m_set(texture, "wabbit_alpha.png"),
m_set(rect, ((Rect){.x = state->bunnies[i].position.x,
.y = state->bunnies[i].position.y,
.w = BUNNY_W * SPRITE_SCALE,

View File

@ -22,14 +22,14 @@ void game_tick(void) {
State *state = ctx.udata;
input_bind_action_control("debug_toggle", CONTROL_BACKSPACE);
input_bind_action_control("debug_dump_atlases", CONTROL_HOME);
input_action("debug_toggle", CONTROL_BACKSPACE);
input_action("debug_dump_atlases", CONTROL_HOME);
if (input_is_action_just_pressed("debug_toggle")) {
if (input_action_just_pressed("debug_toggle")) {
ctx.debug = !ctx.debug;
}
if (input_is_action_just_pressed("debug_dump_atlases")) {
if (input_action_just_pressed("debug_dump_atlases")) {
textures_dump_atlases();
}

View File

@ -20,19 +20,19 @@ static void update_timers(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("player_left") &&
!input_is_action_pressed("player_right"))
if (!input_action_pressed("player_left") &&
!input_action_pressed("player_right"))
{
player->dx *= player->horizontal_damping;
}
int input_dir = 0;
if (input_is_action_pressed("player_left"))
if (input_action_pressed("player_left"))
input_dir = -1;
if (input_is_action_pressed("player_right"))
if (input_action_pressed("player_right"))
input_dir = 1;
if (input_is_action_pressed("player_left") &&
input_is_action_pressed("player_right"))
if (input_action_pressed("player_left") &&
input_action_pressed("player_right"))
input_dir = 0;
player->dx += (float)input_dir * player->run_horizontal_speed;
@ -56,7 +56,7 @@ static void jump(Player *player) {
static void input_jump(Player *player) {
player->current_gravity_multiplier = player->jump_default_multiplier;
if (input_is_action_just_pressed("player_jump")) {
if (input_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(Player *player) {
}
}
if (input_is_action_pressed("player_jump")) {
if (input_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;
@ -147,7 +147,7 @@ static bool corner_correct(Player *player, Rect collision) {
static void calc_collisions_x(Player *player) {
Rect collision;
bool is_colliding = world_find_intersect_frect(player->world, player->collider_x, &collision);
bool is_colliding = world_find_rect_intersects(player->world, player->collider_x, &collision);
if (!is_colliding) return;
float player_center_x = player->collider_x.x + (player->collider_x.w / 2);
@ -164,7 +164,7 @@ static void calc_collisions_x(Player *player) {
static void calc_collisions_y(Player *player) {
Rect collision;
bool is_colliding = world_find_intersect_frect(player->world, player->collider_y, &collision);
bool is_colliding = world_find_rect_intersects(player->world, player->collider_y, &collision);
if (!is_colliding) return;
float player_center_y = player->collider_y.y + (player->collider_y.h / 2);

View File

@ -11,35 +11,15 @@
static void ingame_tick(State *state) {
SceneIngame *scn = (SceneIngame *)state->scene;
input_bind_action_control("player_left", CONTROL_A);
input_bind_action_control("player_right", CONTROL_D);
input_bind_action_control("player_forward", CONTROL_W);
input_bind_action_control("player_backward", CONTROL_S);
input_bind_action_control("player_jump", CONTROL_SPACE);
input_bind_action_control("player_run", CONTROL_LSHIFT);
input_action("player_left", CONTROL_A);
input_action("player_right", CONTROL_D);
input_action("player_forward", CONTROL_W);
input_action("player_backward", CONTROL_S);
input_action("player_jump", CONTROL_SPACE);
input_action("player_run", CONTROL_LSHIFT);
world_drawdef(scn->world);
player_calc(scn->player);
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("player_left"))
scn->cam.pos = vec3_sub(scn->cam.pos, m_vec_scale(right, speed));
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("player_forward"))
scn->cam.pos = vec3_add(scn->cam.pos, m_vec_scale(scn->cam.target, speed));
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("player_jump"))
scn->cam.pos.y += speed;
if (input_is_action_pressed("player_run"))
scn->cam.pos.y -= speed;
}
@ -61,7 +41,5 @@ Scene *ingame_scene(State *state) {
new_scene->world = world_create();
new_scene->player = player_create(new_scene->world);
new_scene->cam = (Camera){ .pos = { 32, 0, 1 }, .up = { 0, 1, 0 }, .fov = (float)M_PI_2 };
return (Scene *)new_scene;
}

View File

@ -15,8 +15,6 @@ typedef struct SceneIngame {
World *world;
Player *player;
Camera cam;
/* TODO: put this in a better place */
float yaw;
float pitch;

View File

@ -14,9 +14,9 @@ static void title_tick(State *state) {
SceneTitle *scn = (SceneTitle *)state->scene;
(void)scn;
input_bind_action_control("ui_accept", CONTROL_RETURN);
input_action("ui_accept", CONTROL_RETURN);
if (input_is_action_just_pressed("ui_accept")) {
if (input_action_just_pressed("ui_accept")) {
switch_to(state, ingame_scene);
return;
}
@ -25,13 +25,13 @@ static void title_tick(State *state) {
((float)ctx.resolution.x / 2) - ((float)320 / 2), 64, 320, 128 }));
/* draw the tick count as an example of dynamic text */
size_t text_str_len = snprintf(NULL, 0, "%lu", state->ctx->frame_number) + 1;
size_t text_str_len = snprintf(NULL, 0, "%llu", (unsigned long long)state->ctx->frame_number) + 1;
char *text_str = cmalloc(text_str_len);
snprintf(text_str, text_str_len, "%lu", state->ctx->frame_number);
snprintf(text_str, text_str_len, "%llu", (unsigned long long)state->ctx->frame_number);
const char *font = "fonts/kenney-pixel.ttf";
int text_h = 32;
int text_w = draw_text_width(text_str, text_h, font);
float text_h = 32;
float text_w = draw_text_width(text_str, text_h, font);
draw_rectangle(
(Rect) {

View File

@ -12,11 +12,11 @@ static void update_tiles(struct World *world) {
for (size_t row = 0; row < world->tilemap_height; ++row) {
for (size_t col = 0; col < world->tilemap_width; ++col) {
world->tiles[(row * world->tilemap_width) + col] = (struct Tile) {
.rect = (Recti) {
.x = (int)col * world->tile_size,
.y = (int)row * world->tile_size,
.w = world->tile_size,
.h = world->tile_size,
.rect = (Rect) {
.x = (float)(col * world->tile_size),
.y = (float)(row * world->tile_size),
.w = (float)world->tile_size,
.h = (float)world->tile_size,
},
.type = world->tilemap[row][col],
};
@ -25,10 +25,10 @@ static void update_tiles(struct World *world) {
}
static Vec2i to_grid_location(struct World *world, float x, float y) {
return (Vec2i) {
.x = (int)floor(x / (float)world->tile_size),
.y = (int)floor(y / (float)world->tile_size),
static Vec2 to_grid_location(struct World *world, float x, float y) {
return (Vec2) {
.x = floor(x / (float)world->tile_size),
.y = floor(y / (float)world->tile_size),
};
}
@ -39,8 +39,7 @@ static void drawdef_debug(struct World *world) {
for (size_t i = 0; i < world->tilemap_height * world->tilemap_width; ++i) {
if (world->tiles[i].type == TILE_TYPE_VOID) continue;
draw_rectangle(to_rect(world->tiles[i].rect),
(Color) { 255, 0, 255, 128 });
draw_rectangle(world->tiles[i].rect, (Color) { 255, 0, 255, 128 });
}
}
@ -106,14 +105,14 @@ void world_drawdef(struct World *world) {
if (world->tiles[i].type == TILE_TYPE_VOID)
continue;
m_sprite("/assets/white.png", to_rect(world->tiles[i].rect));
m_sprite("/assets/white.png", world->tiles[i].rect);
}
drawdef_debug(world);
}
bool world_find_intersect_frect(struct World *world, Rect rect, Rect *intersection) {
bool world_find_rect_intersects(struct World *world, Rect rect, Rect *intersection) {
bool is_intersecting = false;
const size_t tile_count = world->tilemap_height * world->tilemap_width;
@ -121,12 +120,12 @@ bool world_find_intersect_frect(struct World *world, Rect rect, Rect *intersecti
if (world->tiles[i].type == TILE_TYPE_VOID)
continue;
Rect const tile_frect = to_rect(world->tiles[i].rect);
Rect const tile_frect = world->tiles[i].rect;
is_intersecting = intersect_rect(rect, tile_frect);
is_intersecting = rect_intersects(rect, tile_frect);
if (intersection)
*intersection = overlap_rect(rect, tile_frect);
*intersection = rect_overlap(rect, tile_frect);
if (is_intersecting)
break;
@ -136,44 +135,21 @@ bool world_find_intersect_frect(struct World *world, Rect rect, Rect *intersecti
}
bool world_find_intersect_rect(struct World *world, Recti rect, Recti *intersection) {
bool is_intersecting = false;
const size_t tile_count = world->tilemap_height * world->tilemap_width;
for (size_t i = 0; i < tile_count; ++i) {
if (world->tiles[i].type == TILE_TYPE_VOID)
continue;
Recti const tile_rect = world->tiles[i].rect;
is_intersecting = intersect_recti(rect, tile_rect);
if (intersection)
*intersection = overlap_recti(rect, tile_rect);
if (is_intersecting)
break;
}
return is_intersecting;
}
bool world_is_tile_at(struct World *world, float x, float y) {
Vec2i position_in_grid = to_grid_location(world, x, y);
return world->tilemap[position_in_grid.y][position_in_grid.x] != TILE_TYPE_VOID;
Vec2 position_in_grid = to_grid_location(world, x, y);
return world->tilemap[(int32_t)position_in_grid.y][(int32_t)position_in_grid.x] != TILE_TYPE_VOID;
}
void world_place_tile(struct World *world, float x, float y) {
Vec2i position_in_grid = to_grid_location(world, x, y);
world->tilemap[position_in_grid.y][position_in_grid.x] = TILE_TYPE_SOLID;
Vec2 position_in_grid = to_grid_location(world, x, y);
world->tilemap[(int32_t)position_in_grid.y][(int32_t)position_in_grid.x] = TILE_TYPE_SOLID;
update_tiles(world);
}
void world_remove_tile(struct World *world, float x, float y) {
Vec2i position_in_grid = to_grid_location(world, x, y);
world->tilemap[position_in_grid.y][position_in_grid.x] = TILE_TYPE_VOID;
Vec2 position_in_grid = to_grid_location(world, x, y);
world->tilemap[(int32_t)position_in_grid.y][(int32_t)position_in_grid.x] = TILE_TYPE_VOID;
update_tiles(world);
}

View File

@ -14,7 +14,7 @@ typedef enum TileType {
typedef struct Tile {
Recti rect;
Rect rect;
TileType type;
} Tile;
@ -34,8 +34,7 @@ typedef struct World {
World *world_create(void);
void world_destroy(World *world);
void world_drawdef(World *world);
bool world_find_intersect_frect(World *world, Rect rect, Rect *intersection);
bool world_find_intersect_rect(World *world, Recti rect, Recti *intersection);
bool world_find_rect_intersects(World *world, Rect rect, Rect *intersection);
bool world_is_tile_at(World *world, float x, float y);
void world_place_tile(World *world, float x, float y);
void world_remove_tile(World *world, float x, float y);

View File

@ -23,14 +23,14 @@ void game_tick(void) {
State *state = ctx.udata;
input_bind_action_control("debug_toggle", CONTROL_BACKSPACE);
input_bind_action_control("debug_dump_atlases", CONTROL_HOME);
input_action("debug_toggle", CONTROL_BACKSPACE);
input_action("debug_dump_atlases", CONTROL_HOME);
if (input_is_action_just_pressed("debug_toggle")) {
if (input_action_just_pressed("debug_toggle")) {
ctx.debug = !ctx.debug;
}
if (input_is_action_just_pressed("debug_dump_atlases")) {
if (input_action_just_pressed("debug_dump_atlases")) {
textures_dump_atlases();
}

View File

@ -15,13 +15,13 @@
static void ingame_tick(State *state) {
SceneIngame *scn = (SceneIngame *)state->scene;
input_bind_action_control("player_left", CONTROL_A);
input_bind_action_control("player_right", CONTROL_D);
input_bind_action_control("player_forward", CONTROL_W);
input_bind_action_control("player_backward", CONTROL_S);
input_bind_action_control("player_jump", CONTROL_SPACE);
input_bind_action_control("player_run", CONTROL_LSHIFT);
input_bind_action_control("mouse_capture_toggle", CONTROL_ESCAPE);
input_action("player_left", CONTROL_A);
input_action("player_right", CONTROL_D);
input_action("player_forward", CONTROL_W);
input_action("player_backward", CONTROL_S);
input_action("player_jump", CONTROL_SPACE);
input_action("player_run", CONTROL_LSHIFT);
input_action("mouse_capture_toggle", CONTROL_ESCAPE);
if (scn->mouse_captured) {
const float sensitivity = 0.4f * (float)DEG2RAD; /* TODO: put this in a better place */
@ -35,36 +35,36 @@ static void ingame_tick(State *state) {
const Vec3 right = m_vec_norm(m_vec_cross(dir_and_up.direction, dir_and_up.up));
const float speed = 0.04f; /* TODO: put this in a better place */
if (input_is_action_pressed("player_left"))
if (input_action_pressed("player_left"))
scn->pos = vec3_sub(scn->pos, m_vec_scale(right, speed));
if (input_is_action_pressed("player_right"))
if (input_action_pressed("player_right"))
scn->pos = vec3_add(scn->pos, m_vec_scale(right, speed));
if (input_is_action_pressed("player_forward"))
if (input_action_pressed("player_forward"))
scn->pos = vec3_add(scn->pos, m_vec_scale(dir_and_up.direction, speed));
if (input_is_action_pressed("player_backward"))
if (input_action_pressed("player_backward"))
scn->pos = vec3_sub(scn->pos, m_vec_scale(dir_and_up.direction, speed));
if (input_is_action_pressed("player_jump"))
if (input_action_pressed("player_jump"))
scn->pos.y += speed;
if (input_is_action_pressed("player_run"))
if (input_action_pressed("player_run"))
scn->pos.y -= speed;
/* toggle mouse capture with end key */
if (input_is_action_just_pressed("mouse_capture_toggle"))
if (input_action_just_pressed("mouse_capture_toggle"))
scn->mouse_captured = !scn->mouse_captured;
input_set_mouse_captured(scn->mouse_captured);
input_mouse_captured(scn->mouse_captured);
#define TERRAIN_FREQUENCY 0.1f
for (int ly = 64; ly--;) {
for (int lx = 64; lx--;) {
float x = SDL_truncf(scn->pos.x + 32 - (float)lx);
float y = SDL_truncf(scn->pos.z + 32 - (float)ly);
for (int ly = 128; ly--;) {
for (int lx = 128; lx--;) {
float x = SDL_truncf(scn->pos.x + 64 - (float)lx);
float y = SDL_truncf(scn->pos.z + 64 - (float)ly);
float d0 = stb_perlin_noise3((float)x * TERRAIN_FREQUENCY, (float)y * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 6;
float d1 = stb_perlin_noise3((float)(x + 1) * TERRAIN_FREQUENCY, (float)y * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 6;
@ -112,7 +112,5 @@ Scene *ingame_scene(State *state) {
m_opt(channel, "soundtrack"),
m_opt(repeat, true));
input_set_mouse_captured(true);
return (Scene *)new_scene;
}

View File

@ -11,9 +11,9 @@ static void title_tick(State *state) {
SceneTitle *scn = (SceneTitle *)state->scene;
(void)scn;
input_bind_action_control("ui_accept", CONTROL_RETURN);
input_action("ui_accept", CONTROL_RETURN);
if (input_is_action_just_pressed("ui_accept")) {
if (input_action_just_pressed("ui_accept")) {
switch_to(state, ingame_scene);
return;
}
@ -22,20 +22,20 @@ static void title_tick(State *state) {
((float)ctx.resolution.x / 2) - ((float)320 / 2), 64, 320, 128 }));
/* draw the tick count as an example of dynamic text */
size_t text_str_len = snprintf(NULL, 0, "%llu", state->ctx->frame_number) + 1;
size_t text_str_len = snprintf(NULL, 0, "%llu", (unsigned long long)state->ctx->frame_number) + 1;
char *text_str = cmalloc(text_str_len);
snprintf(text_str, text_str_len, "%llu", state->ctx->frame_number);
snprintf(text_str, text_str_len, "%llu", (unsigned long long)state->ctx->frame_number);
const char *font = "/fonts/kenney-pixel.ttf";
int text_h = 32;
int text_w = draw_text_width(text_str, text_h, font);
float text_h = 32;
float text_w = draw_text_width(text_str, text_h, font);
draw_rectangle(
(Rect) {
.x = 0,
.y = 0,
.w = (float)text_w,
.h = (float)text_h,
.w = text_w,
.h = text_h,
},
(Color) { 0, 0, 0, 255 }
);