add the long awaited push_9slice

This commit is contained in:
wanp 2024-10-02 14:18:07 -03:00
parent 452ecd72fe
commit dbbf6e25f0
3 changed files with 144 additions and 1 deletions

View File

@ -24,7 +24,7 @@ static void title_tick(State *state) {
char *text_str = cmalloc(text_str_len); char *text_str = cmalloc(text_str_len);
snprintf(text_str, text_str_len, "%lu", state->ctx->tick_count); snprintf(text_str, text_str_len, "%lu", state->ctx->tick_count);
const char *font = "fonts/kenney-pixel.ttf"; const char *font = "/fonts/kenney-pixel.ttf";
int text_h = 32; int text_h = 32;
int text_w = text_get_width(text_str, text_h, font); int text_w = text_get_width(text_str, text_h, font);

View File

@ -38,6 +38,8 @@ TWN_API void push_circle(Vec2 position, float radius, Color color);
TWN_API void push_text(char *string, Vec2 position, int height_px, Color color, const char *font_path); TWN_API void push_text(char *string, Vec2 position, int height_px, Color color, const char *font_path);
TWN_API int text_get_width(char *string, int height_px, const char *font_path); TWN_API int text_get_width(char *string, int height_px, const char *font_path);
TWN_API void push_9slice(char *texture_path, int texture_w, int texture_h, int border_thickness, Rect rect, Color color);
/* pushes a textured 3d triangle onto the render queue */ /* pushes a textured 3d triangle onto the render queue */
/* vertices are in absolute coordinates, relative to world origin */ /* vertices are in absolute coordinates, relative to world origin */
/* texture coordinates are in pixels */ /* texture coordinates are in pixels */

View File

@ -50,6 +50,147 @@ void push_rectangle(Rect rect, Color color) {
} }
void push_9slice(char *texture_path, int texture_w, int texture_h, int border_thickness, Rect rect, Color color) {
const float bt = (float)border_thickness; /* i know! */
const float bt2 = bt * 2; /* combined size of the two borders in an axis */
Rect top_left = {
.x = rect.x,
.y = rect.y,
.w = bt,
.h = bt,
};
m_sprite(
m_set(path, texture_path),
m_set(rect, top_left),
m_opt(texture_region, ((Rect) { 0, 0, bt, bt })),
m_opt(color, color),
);
Rect top_center = {
.x = rect.x + bt,
.y = rect.y,
.w = rect.w - bt2, /* here bt2 represents the top left and right corners */
.h = bt,
};
m_sprite(
m_set(path, texture_path),
m_set(rect, top_center),
m_opt(texture_region, ((Rect) { bt, 0, (float)texture_w - bt2, bt })),
m_opt(color, color),
);
Rect top_right = {
.x = rect.x + (rect.w - bt),
.y = rect.y,
.w = bt,
.h = bt,
};
m_sprite(
m_set(path, texture_path),
m_set(rect, top_right),
m_opt(texture_region, ((Rect) { (float)texture_w - bt, 0, bt, bt })),
m_opt(color, color),
);
Rect center_left = {
.x = rect.x,
.y = rect.y + bt,
.w = bt,
.h = rect.h - bt2, /* here bt2 represents the top and bottom left corners */
};
m_sprite(
m_set(path, texture_path),
m_set(rect, center_left),
m_opt(texture_region, ((Rect) { 0, bt, bt, (float)texture_h - bt2 })),
m_opt(color, color),
);
Rect center_right = {
.x = rect.x + (rect.w - bt),
.y = rect.y + bt,
.w = bt,
.h = rect.h - bt2, /* here bt2 represents the top and bottom right corners */
};
m_sprite(
m_set(path, texture_path),
m_set(rect, center_right),
m_opt(texture_region, ((Rect) { (float)texture_w - bt, bt, bt, (float)texture_h - bt2 })),
m_opt(color, color),
);
Rect bottom_left = {
.x = rect.x,
.y = rect.y + (rect.h - bt),
.w = bt,
.h = bt,
};
m_sprite(
m_set(path, texture_path),
m_set(rect, bottom_left),
m_opt(texture_region, ((Rect) { 0, (float)texture_h - bt, bt, bt })),
m_opt(color, color),
);
Rect bottom_center = {
.x = rect.x + bt,
.y = rect.y + (rect.h - bt),
.w = rect.w - bt2, /* here bt2 represents the bottom left and right corners */
.h = bt,
};
m_sprite(
m_set(path, texture_path),
m_set(rect, bottom_center),
m_opt(texture_region, ((Rect) { bt, (float)texture_h - bt, (float)texture_w - bt2, bt })),
m_opt(color, color),
);
Rect bottom_right = {
.x = rect.x + (rect.w - bt),
.y = rect.y + (rect.h - bt),
.w = bt,
.h = bt,
};
m_sprite(
m_set(path, texture_path),
m_set(rect, bottom_right),
m_opt(texture_region, ((Rect) { (float)texture_w - bt, (float)texture_h - bt, bt, bt })),
m_opt(color, color),
);
Rect center = {
.x = rect.x + bt,
.y = rect.y + bt,
.w = rect.w - bt2,
.h = rect.h - bt2,
};
m_sprite(
m_set(path, texture_path),
m_set(rect, center),
m_opt(texture_region, ((Rect) { bt, bt, (float)texture_w - bt2, (float)texture_h - bt2 })),
m_opt(color, color),
);
}
static void render_2d(void) { static void render_2d(void) {
use_2d_pipeline(); use_2d_pipeline();