yet another api rework, removal of integer types in public api, optionals at the end, some cleaning
This commit is contained in:
@ -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
|
||||
);
|
||||
}
|
||||
|
@ -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 };
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
@ -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,
|
||||
|
Reference in New Issue
Block a user