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

View File

@ -4,13 +4,22 @@ offset = { x = 0, y = 0 }
angle = 0
function game_tick()
rectangle {
draw.rectangle {
rect = { x = 0, y = 0, w = 640, h = 360 },
color = { r = 127, g = 0, b = 127, a = 255 },
}
sprite {
path = "/assets/title.png",
input.action {
name = "move_up",
control = input.CONTROL_L,
}
if input.action_pressed "move_up" then
draw.text { string = "BOO!" }
end
draw.sprite {
texture = "/assets/title.png",
rect = {
x = 320 - (320 / 2),
y = 180 - (128 / 2),
@ -19,7 +28,7 @@ function game_tick()
},
}
text {
draw.text {
string = "IT KEEPS HAPPENING",
position = offset,
font = "/fonts/kenney-pixel.ttf",

View File

@ -1,8 +1,6 @@
#ifndef STATE_H
#define STATE_H
#include "twn_game_api.h"
#include <lua.h>

View File

@ -16,7 +16,7 @@ TWN_API void audio_play(const char *path,
float panning); /* default: 0.0f, range: -1.0 to 1.0f */
/* possible parameter options: "volume", "panning", "repeat" */
TWN_API void audio_set_parameter(const char *channel, const char *parameter, float value);
TWN_API void audio_parameter(const char *channel, const char *parameter, float value);
/* TODO */
// TWN_API bool audio_ended(const char *channel);

View File

@ -25,9 +25,9 @@ typedef struct Context {
/* resolution is set from config and dictates both logical and drawing space, as they're related */
/* even if scaling is done, game logic should never change over that */
Vec2i resolution;
Vec2i mouse_position;
Vec2i mouse_movement;
Vec2 resolution;
Vec2 mouse_position;
Vec2 mouse_movement;
/* is set on startup, should be used as source of randomness */
uint64_t random_seed;

View File

@ -8,14 +8,14 @@
#include <stdbool.h>
/* pushes a sprite onto the sprite render queue */
TWN_API void draw_sprite(char const *path,
TWN_API void draw_sprite(char const *texture,
Rect rect,
Rect const *texture_region, /* optional, default: NULL */
Color color, /* optional, default: all 255 */
float rotation, /* optional, default: 0 */
bool flip_x, /* optional, default: false */
bool flip_y, /* optional, default: false */
bool stretch); /* optional, default: false */
bool stretch); /* optional, default: true */
/* pushes a filled rectangle onto the rectangle render queue */
TWN_API void draw_rectangle(Rect rect, Color color);
@ -27,26 +27,24 @@ TWN_API void draw_circle(Vec2 position, float radius, Color color);
/* TODO: have font optional, with something minimal coming embedded */
TWN_API void draw_text(char const *string,
Vec2 position,
int height_px, /* optional, default: 22 */
Color color, /* optional, default: all 0 */
char const *font);
float height, /* optional, default: 22 */
Color color, /* optional, default: all 0 */
char const *font); /* optional, default: NULL */
TWN_API int draw_text_width(char const *string,
int height_px, /* TODO: make optional */
char const *font);
TWN_API float draw_text_width(char const *string,
float height, /* optional, default: 22 */
char const *font); /* optional, default: NULL */
TWN_API void draw_nine_slice(char const *texture_path,
int texture_w,
int texture_h,
int border_thickness,
TWN_API void draw_nine_slice(char const *texture,
Vec2 corners,
Rect rect,
Color color); /* TODO: make optional */
float border_thickness, /* optional, default: 0 */
Color color); /* optional, default: all 255 */
/* pushes a textured 3d triangle onto the render queue */
/* vertices are in absolute coordinates, relative to world origin */
/* texture coordinates are in pixels */
TWN_API void draw_triangle(char const *path,
TWN_API void draw_triangle(char const *texture,
Vec3 v0,
Vec3 v1,
Vec3 v2,
@ -68,12 +66,9 @@ TWN_API void draw_triangle(char const *path,
// Color c1,
// Color c2);
// TODO:
// http://www.lighthouse3d.com/opengl/billboarding/index.php?billCheat2
// void unfurl_billboard(const char *path,
// Vec2 position,
// Vec2 scaling,
// Rect uvs);
TWN_API void draw_billboard(const char *path,
Vec3 position,
Vec2 size);
/* sets a perspective 3d camera to be used for all 3d commands */
TWN_API void draw_camera(Vec3 position, float fov, Vec3 up, Vec3 direction);
@ -100,7 +95,7 @@ TWN_API void draw_fog(float start, float end, float density, Color color);
#ifndef TWN_NOT_C
typedef struct DrawSpriteArgs {
char const *path;
char const *texture;
Rect rect;
m_option_list(

View File

@ -3,11 +3,11 @@
#define TWN_GAME_API_H
#include "twn_input.h"
#include "twn_context.h"
#include "twn_draw.h"
#include "twn_audio.h"
#include "twn_engine_api.h"
#include "twn_util.h"
#include "twn_context.h"
#ifndef TWN_NOT_C

View File

@ -10,11 +10,11 @@
#include <stddef.h>
TWN_API void input_bind_action_control(const char *action_name, Control control);
TWN_API bool input_is_action_pressed(const char *action_name);
TWN_API bool input_is_action_just_pressed(const char *action_name);
TWN_API bool input_is_action_just_released(const char *action_name);
TWN_API Vec2 input_get_action_position(const char *action_name);
TWN_API void input_set_mouse_captured(bool enabled);
TWN_API void input_action(const char *name, Control control);
TWN_API bool input_action_pressed(const char *name);
TWN_API bool input_action_just_pressed(const char *name);
TWN_API bool input_action_just_released(const char *name);
TWN_API Vec2 input_action_position(const char *name);
TWN_API void input_mouse_captured(bool enabled);
#endif

View File

@ -6,13 +6,6 @@
/* plain data aggregates that are accepted between public procedure boundaries */
/* a point in some space (integer) */
typedef struct Vec2i {
int32_t x;
int32_t y;
} Vec2i;
/* a point in some space (floating point) */
typedef struct Vec2 {
float x;
@ -48,15 +41,6 @@ typedef struct Color {
} Color;
/* a rectangle with the origin at the upper left (integer) */
typedef struct Recti {
int32_t x;
int32_t y;
int32_t w;
int32_t h;
} Recti;
/* a rectangle with the origin at the upper left (floating point) */
typedef struct Rect {
float x;

View File

@ -52,19 +52,10 @@
#endif /* TWN_NOT_C */
/* calculates the overlap of two rectangles */
TWN_API Recti overlap_recti(const Recti a, const Recti b);
TWN_API Rect overlap_rect(const Rect a, const Rect b);
TWN_API Rect rect_overlap(Rect a, Rect b);
/* returns true if two rectangles are intersecting */
TWN_API bool intersect_recti(const Recti a, const Recti b);
TWN_API bool intersect_rect(const Rect a, const Rect b);
/* TODO: generics and specials (see m_vec2_from() for an example)*/
TWN_API Recti to_recti(Rect rect);
TWN_API Rect to_rect(Recti rect);
TWN_API Vec2i center_recti(Recti rect);
TWN_API Vec2 center_rect(Rect rect);
TWN_API bool rect_intersects(Rect a, Rect b);
TWN_API Vec2 rect_center(Rect rect);
/* decrements an integer value, stopping at 0 */
/* meant for tick-based timers in game logic */

View File

@ -9,15 +9,6 @@
#include <math.h>
/* aren't macros to prevent double evaluation with side effects */
/* maybe could be inlined? i hope LTO will resolve this */
static inline Vec2 vec2_from_vec2i(Vec2i vec) {
return (Vec2) {
.x = (float)vec.x,
.y = (float)vec.y,
};
}
static inline Vec3 vec3_add(Vec3 a, Vec3 b) {
return (Vec3) { a.x + b.x, a.y + b.y, a.z + b.z };
}

View File

@ -36,8 +36,8 @@ void render_queue_clear(void) {
}
void draw_nine_slice(const char *texture_path, int texture_w, int texture_h, int border_thickness, Rect rect, Color color) {
const float bt = (float)border_thickness; /* i know! */
void draw_nine_slice(const char *texture, Vec2 corners, Rect rect, float border_thickness, Color color) {
const float bt = border_thickness;
const float bt2 = bt * 2; /* combined size of the two borders in an axis */
@ -49,7 +49,7 @@ void draw_nine_slice(const char *texture_path, int texture_w, int texture_h, int
};
m_sprite(
m_set(path, texture_path),
m_set(texture, texture),
m_set(rect, top_left),
m_opt(texture_region, ((Rect) { 0, 0, bt, bt })),
m_opt(color, color),
@ -64,9 +64,9 @@ void draw_nine_slice(const char *texture_path, int texture_w, int texture_h, int
};
m_sprite(
m_set(path, texture_path),
m_set(texture, texture),
m_set(rect, top_center),
m_opt(texture_region, ((Rect) { bt, 0, (float)texture_w - bt2, bt })),
m_opt(texture_region, ((Rect) { bt, 0, corners.x - bt2, bt })),
m_opt(color, color),
);
@ -79,9 +79,9 @@ void draw_nine_slice(const char *texture_path, int texture_w, int texture_h, int
};
m_sprite(
m_set(path, texture_path),
m_set(texture, texture),
m_set(rect, top_right),
m_opt(texture_region, ((Rect) { (float)texture_w - bt, 0, bt, bt })),
m_opt(texture_region, ((Rect) { corners.x - bt, 0, bt, bt })),
m_opt(color, color),
);
@ -94,9 +94,9 @@ void draw_nine_slice(const char *texture_path, int texture_w, int texture_h, int
};
m_sprite(
m_set(path, texture_path),
m_set(texture, texture),
m_set(rect, center_left),
m_opt(texture_region, ((Rect) { 0, bt, bt, (float)texture_h - bt2 })),
m_opt(texture_region, ((Rect) { 0, bt, bt, corners.y - bt2 })),
m_opt(color, color),
);
@ -109,9 +109,9 @@ void draw_nine_slice(const char *texture_path, int texture_w, int texture_h, int
};
m_sprite(
m_set(path, texture_path),
m_set(texture, texture),
m_set(rect, center_right),
m_opt(texture_region, ((Rect) { (float)texture_w - bt, bt, bt, (float)texture_h - bt2 })),
m_opt(texture_region, ((Rect) { corners.x - bt, bt, bt, corners.y - bt2 })),
m_opt(color, color),
);
@ -124,9 +124,9 @@ void draw_nine_slice(const char *texture_path, int texture_w, int texture_h, int
};
m_sprite(
m_set(path, texture_path),
m_set(texture, texture),
m_set(rect, bottom_left),
m_opt(texture_region, ((Rect) { 0, (float)texture_h - bt, bt, bt })),
m_opt(texture_region, ((Rect) { 0, corners.y - bt, bt, bt })),
m_opt(color, color),
);
@ -139,9 +139,9 @@ void draw_nine_slice(const char *texture_path, int texture_w, int texture_h, int
};
m_sprite(
m_set(path, texture_path),
m_set(texture, texture),
m_set(rect, bottom_center),
m_opt(texture_region, ((Rect) { bt, (float)texture_h - bt, (float)texture_w - bt2, bt })),
m_opt(texture_region, ((Rect) { bt, corners.y - bt, corners.x - bt2, bt })),
m_opt(color, color),
);
@ -154,9 +154,9 @@ void draw_nine_slice(const char *texture_path, int texture_w, int texture_h, int
};
m_sprite(
m_set(path, texture_path),
m_set(texture, texture),
m_set(rect, bottom_right),
m_opt(texture_region, ((Rect) { (float)texture_w - bt, (float)texture_h - bt, bt, bt })),
m_opt(texture_region, ((Rect) { corners.x - bt, corners.y - bt, bt, bt })),
m_opt(color, color),
);
@ -169,9 +169,9 @@ void draw_nine_slice(const char *texture_path, int texture_w, int texture_h, int
};
m_sprite(
m_set(path, texture_path),
m_set(texture, texture),
m_set(rect, center),
m_opt(texture_region, ((Rect) { bt, bt, (float)texture_w - bt2, (float)texture_h - bt2 })),
m_opt(texture_region, ((Rect) { bt, bt, corners.x - bt2, corners.y - bt2 })),
m_opt(color, color),
);
}
@ -358,18 +358,18 @@ void render(void) {
float ratio = (float)ctx.window_dims.y / (float)ctx.base_render_height;
int w = (int)((float)ctx.base_render_width * ratio);
setup_viewport(
ctx.window_dims.x / 2 - w / 2,
(int)ctx.window_dims.x / 2 - w / 2,
0,
w,
ctx.window_dims.y
(int)ctx.window_dims.y
);
} else {
float ratio = (float)ctx.window_dims.x / (float)ctx.base_render_width;
int h = (int)((float)ctx.base_render_height * ratio);
setup_viewport(
0,
ctx.window_dims.y / 2 - h / 2,
ctx.window_dims.x,
(int)ctx.window_dims.y / 2 - h / 2,
(int)ctx.window_dims.x,
h
);
}

View File

@ -56,7 +56,7 @@ void draw_sprite_args(const DrawSpriteArgs args) {
bool const stretch = m_or(args, stretch, false);
Rect const *texture_region = m_is_set(args, texture_region) ? &args.texture_region_opt : NULL;
draw_sprite(args.path, args.rect, texture_region, color, rotation, flip_x, flip_y, stretch);
draw_sprite(args.texture, args.rect, texture_region, color, rotation, flip_x, flip_y, stretch);
}
@ -216,7 +216,7 @@ void render_sprite_batch(const Primitive2D primitives[],
#pragma GCC diagnostic pop
const Vec2 c = center_rect(sprite.rect);
const Vec2 c = rect_center(sprite.rect);
const Vec2 t = fast_cossine(sprite.rotation + (float)M_PI_4);
const Vec2 d = {
.x = t.x * sprite.rect.w * (float)M_SQRT1_2,
@ -231,7 +231,7 @@ void render_sprite_batch(const Primitive2D primitives[],
} else {
/* rotated non-square case*/
const Vec2 c = center_rect(sprite.rect);
const Vec2 c = rect_center(sprite.rect);
const Vec2 t = fast_cossine(sprite.rotation);
const Vec2 h = { sprite.rect.w / 2, sprite.rect.h / 2 };

View File

@ -275,8 +275,8 @@ void text_cache_reset_arena(TextCache *cache) {
}
void draw_text(const char *string, Vec2 position, int height_px, Color color, const char *font_path) {
ensure_font_cache(font_path, height_px);
void draw_text(const char *string, Vec2 position, float height, Color color, const char *font) {
ensure_font_cache(font, (int)height);
/* the original string might not be around by the time it's used, so copy it */
size_t str_length = SDL_strlen(string) + 1;
@ -290,8 +290,8 @@ void draw_text(const char *string, Vec2 position, int height_px, Color color, co
.color = color,
.position = position,
.text = dup_string,
.font = font_path,
.height_px = height_px,
.font = font,
.height_px = (int)height,
};
Primitive2D primitive = {
@ -303,9 +303,9 @@ void draw_text(const char *string, Vec2 position, int height_px, Color color, co
}
int draw_text_width(const char *string, int height_px, const char *font_path) {
ensure_font_cache(font_path, height_px);
FontData *font_data = get_font_data(font_path, height_px);
float draw_text_width(const char *string, float height, const char *font) {
ensure_font_cache(font, (int)height);
FontData *font_data = get_font_data(font, (int)height);
int length = 0;
for (const char *p = string; *p != '\0'; ++p) {
@ -316,5 +316,5 @@ int draw_text_width(const char *string, int height_px, const char *font_path) {
length += advance_width;
}
return (int)((float)length * font_data->scale_factor);
return (float)length * font_data->scale_factor;
}

View File

@ -191,7 +191,7 @@ void audio_play(const char *path,
}
TWN_API void audio_set_parameter(const char *channel, const char *param, float value) {
TWN_API void audio_parameter(const char *channel, const char *param, float value) {
AudioChannelItem *pair = shgetp_null(ctx.audio_channels, channel);
if (!pair) {
log_warn("No channel by the name of %s to set a parameter for", channel);

View File

@ -29,7 +29,7 @@ typedef struct EngineContext {
/* where the app was run from, used as the root for packs */
char *base_dir;
Vec2i window_dims;
Vec2 window_dims;
/* configuration */
toml_table_t *config_table;

View File

@ -221,12 +221,14 @@ void input_state_update(InputState *input) {
if (SDL_SetRelativeMouseMode(input->mouse_captured) != 0)
log_warn("(%s) Mouse capture isn't supported.", __func__);
input->keyboard_state = SDL_GetKeyboardState(NULL);
input->mouse_state = SDL_GetMouseState(&input->mouse_window_position.x,
&input->mouse_window_position.y);
int x, y;
SDL_GetRelativeMouseState(&input->mouse_relative_position.x,
&input->mouse_relative_position.y);
input->keyboard_state = SDL_GetKeyboardState(NULL);
input->mouse_state = SDL_GetMouseState(&x, &y);
input->mouse_window_position = (Vec2){ (float)x, (float)y };
SDL_GetRelativeMouseState(&x, &y);
input->mouse_relative_position = (Vec2){ (float)x, (float)y };
ctx.game.mouse_position = input->mouse_window_position;
ctx.game.mouse_movement = input->mouse_relative_position;
@ -253,7 +255,7 @@ void input_state_update(InputState *input) {
}
void input_bind_action_control(char const *action_name,
void input_action(char const *action_name,
Control control)
{
SDL_assert_always(action_name);
@ -275,29 +277,7 @@ void input_bind_action_control(char const *action_name,
}
// static void input_unbind_action_control(char const *action_name,
// Control control)
// {
// SDL_assert_always(action_name);
// if (CONTROL_SCANCODE_START <= control && control < CONTROL_SCANCODE_LIMIT)
// input_unbind_code_from_action(&ctx.input,
// action_name,
// BUTTON_SOURCE_KEYBOARD_PHYSICAL,
// (union ButtonCode) { .scancode = (SDL_Scancode)control });
// else if (CONTROL_MOUSECODE_START <= control && control < CONTROL_MOUSECODE_LIMIT) {
// uint8_t const mouse_button = (uint8_t)(control - CONTROL_MOUSECODE_START);
// input_unbind_code_from_action(&ctx.input,
// action_name,
// BUTTON_SOURCE_MOUSE,
// (union ButtonCode) { .mouse_button = (uint8_t)SDL_BUTTON(mouse_button)});
// } else
// log_warn("(%s) Invalid control value given: %i.", __func__, control);
// }
bool input_is_action_pressed(char const *action_name) {
bool input_action_pressed(char const *action_name) {
SDL_assert_always(action_name);
ActionHashItem *action = shgetp_null(ctx.input.action_hash, action_name);
@ -309,7 +289,7 @@ bool input_is_action_pressed(char const *action_name) {
}
bool input_is_action_just_pressed(char const *action_name) {
bool input_action_just_pressed(char const *action_name) {
SDL_assert_always(action_name);
ActionHashItem *action = shgetp_null(ctx.input.action_hash, action_name);
@ -321,7 +301,7 @@ bool input_is_action_just_pressed(char const *action_name) {
}
bool input_is_action_just_released(char const *action_name) {
bool input_action_just_released(char const *action_name) {
SDL_assert_always(action_name);
ActionHashItem *action = shgetp_null(ctx.input.action_hash, action_name);
@ -333,7 +313,7 @@ bool input_is_action_just_released(char const *action_name) {
}
Vec2 input_get_action_position(char const *action_name) {
Vec2 input_action_position(char const *action_name) {
SDL_assert_always(action_name);
ActionHashItem *action = shgetp_null(ctx.input.action_hash, action_name);
@ -346,7 +326,7 @@ Vec2 input_get_action_position(char const *action_name) {
}
void input_set_mouse_captured(bool enabled) {
void input_mouse_captured(bool enabled) {
ctx.input.mouse_captured = enabled;
}

View File

@ -61,8 +61,8 @@ typedef struct ActionHashItem {
typedef struct InputState {
const uint8_t *keyboard_state; /* array of booleans indexed by scancode */
ActionHashItem *action_hash;
Vec2i mouse_window_position;
Vec2i mouse_relative_position;
Vec2 mouse_window_position;
Vec2 mouse_relative_position;
uint32_t mouse_state; /* SDL mouse button bitmask */
ButtonSource last_active_source;
bool is_anything_just_pressed;

View File

@ -37,8 +37,8 @@ static int event_callback(void *userdata, SDL_Event *event) {
switch (event->window.event) {
case SDL_WINDOWEVENT_SIZE_CHANGED:
ctx.window_dims.x = event->window.data1;
ctx.window_dims.y = event->window.data2;
ctx.window_dims.x = (float)event->window.data1;
ctx.window_dims.y = (float)event->window.data2;
ctx.resync_flag = true;
break;
@ -444,7 +444,7 @@ static bool initialize(void) {
goto fail;
}
ctx.base_render_width = datum_base_render_width.u.i;
ctx.game.resolution.x = (int)ctx.base_render_width;
ctx.game.resolution.x = (float)ctx.base_render_width;
toml_datum_t datum_base_render_height = toml_int_in(game, "base_render_height");
if (!datum_base_render_height.ok) {
@ -452,7 +452,7 @@ static bool initialize(void) {
goto fail;
}
ctx.base_render_height = datum_base_render_height.u.i;
ctx.game.resolution.y = (int)ctx.base_render_height;
ctx.game.resolution.y = (float)ctx.base_render_height;
ctx.window = SDL_CreateWindow(datum_title.u.s,
SDL_WINDOWPOS_CENTERED,
@ -508,8 +508,8 @@ static bool initialize(void) {
/* TODO: */
// SDL_GetRendererOutputSize(ctx.renderer, &ctx.window_w, &ctx.window_h);
ctx.window_dims.x = (int)ctx.base_render_width;
ctx.window_dims.y = (int)ctx.base_render_height;
ctx.window_dims.x = (float)ctx.base_render_width;
ctx.window_dims.y = (float)ctx.base_render_height;
/* add a watcher for immediate updates on window size */
SDL_AddEventWatch(event_callback, NULL);

View File

@ -198,19 +198,7 @@ bool strends(const char *str, const char *suffix) {
/* TODO: have our own */
Recti overlap_recti(const Recti a, const Recti b) {
SDL_Rect a_sdl = { a.x, a.y, a.w, a.h };
SDL_Rect b_sdl = { b.x, b.y, b.w, b.h };
SDL_Rect result_sdl = { 0 };
(void)SDL_IntersectRect(&a_sdl, &b_sdl, &result_sdl);
return (Recti){ result_sdl.x, result_sdl.y, result_sdl.w, result_sdl.h };
}
/* TODO: have our own */
Rect overlap_rect(const Rect a, const Rect b) {
Rect rect_overlap(const Rect a, const Rect b) {
SDL_FRect a_sdl = { a.x, a.y, a.w, a.h };
SDL_FRect b_sdl = { b.x, b.y, b.w, b.h };
SDL_FRect result_sdl = { 0 };
@ -222,50 +210,14 @@ Rect overlap_rect(const Rect a, const Rect b) {
/* TODO: have our own */
bool intersect_recti(const Recti a, const Recti b) {
SDL_Rect a_sdl = { a.x, a.y, a.w, a.h };
SDL_Rect b_sdl = { b.x, b.y, b.w, b.h };
return SDL_HasIntersection(&a_sdl, &b_sdl);
}
/* TODO: have our own */
bool intersect_rect(const Rect a, const Rect b) {
bool rect_intersects(const Rect a, const Rect b) {
SDL_FRect a_sdl = { a.x, a.y, a.w, a.h };
SDL_FRect b_sdl = { b.x, b.y, b.w, b.h };
return SDL_HasIntersectionF(&a_sdl, &b_sdl);
}
Recti to_recti(Rect rect) {
return (Recti) {
.h = (int32_t)rect.h,
.w = (int32_t)rect.w,
.x = (int32_t)rect.x,
.y = (int32_t)rect.y,
};
}
Rect to_rect(Recti rect) {
return (Rect) {
.h = (float)rect.h,
.w = (float)rect.w,
.x = (float)rect.x,
.y = (float)rect.y,
};
}
Vec2i center_recti(Recti rect) {
return (Vec2i){
.x = rect.x + rect.w / 2,
.y = rect.y + rect.h / 2,
};
}
Vec2 center_rect(Rect rect) {
Vec2 rect_center(Rect rect) {
return (Vec2){
.x = rect.x + rect.w / 2,
.y = rect.y + rect.h / 2,