From b67bc9285798086b9db46e38cdd90e7ac6c999e9 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Thu, 20 Feb 2025 16:19:03 +0300 Subject: [PATCH] remove optional by pointer texture_region parameters --- apps/demos/scenery/scenes/ingame.c | 2 +- include/twn_draw.h | 6 +++--- share/twn_api.json | 4 ++-- src/rendering/twn_billboards.c | 11 +++++++---- src/rendering/twn_sprites.c | 15 ++++++++++----- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/apps/demos/scenery/scenes/ingame.c b/apps/demos/scenery/scenes/ingame.c index 6a0f063..a6b5b70 100644 --- a/apps/demos/scenery/scenes/ingame.c +++ b/apps/demos/scenery/scenes/ingame.c @@ -179,7 +179,7 @@ static void draw_terrain(SceneIngame *scn) { draw_billboard("/assets/grasses/10.png", (Vec3){ (float)x, d0 + 0.15f, (float)y }, (Vec2){0.3f, 0.3f}, - NULL, + (Rect){0}, (Color){255, 255, 255, 255}, true); } } diff --git a/include/twn_draw.h b/include/twn_draw.h index b963ba1..0e7d863 100644 --- a/include/twn_draw.h +++ b/include/twn_draw.h @@ -10,7 +10,7 @@ /* TODO: combine flip_x and flip_y into a flip_mask with enum */ TWN_API void draw_sprite(char const *texture, Rect rect, - Rect const *texture_region, /* optional, default: NULL */ + Rect texture_region, /* optional, default: all 0 */ Color color, /* optional, default: all 255 */ float rotation, /* optional, default: 0 */ bool flip_x, /* optional, default: false */ @@ -76,8 +76,8 @@ TWN_API void draw_quad(char const *texture, TWN_API void draw_billboard(char const *texture, Vec3 position, Vec2 size, - Rect const *texture_region, /* optional, default: NULL */ - Color color, /* optional, default: all 255 */ + Rect texture_region, /* optional, default: NULL */ + Color color, /* optional, default: all 0 */ bool cylindrical); /* optional, default: false */ TWN_API void draw_camera_2d(Vec2 position, /* optional, default: (0, 0) */ diff --git a/share/twn_api.json b/share/twn_api.json index 186cca4..5811e2e 100644 --- a/share/twn_api.json +++ b/share/twn_api.json @@ -59,7 +59,7 @@ "params": [ { "name": "texture", "type": "char *" }, { "name": "rect", "type": "Rect" }, - { "name": "texture_region", "type": "Rect *", "default": {} }, + { "name": "texture_region", "type": "Rect", "default": { "x": 0, "y": 0, "w": 0, "h": 0 } }, { "name": "color", "type": "Color", "default": { "r": 255, "g": 255, "b": 255, "a": 255 } }, { "name": "rotation", "type": "float", "default": 0.0 }, { "name": "flip_x", "type": "bool", "default": false }, @@ -180,7 +180,7 @@ { "name": "texture", "type": "char *" }, { "name": "position", "type": "Vec3" }, { "name": "size", "type": "Vec2" }, - { "name": "texture_region", "type": "Rect *", "default": {} }, + { "name": "texture_region", "type": "Rect", "default": { "x": 0, "y": 0, "w": 0, "h": 0 } }, { "name": "color", "type": "Color", "default": { "r": 255, "g": 255, "b": 255, "a": 255 } }, { "name": "cylindrical", "type": "bool", "default": false } ] diff --git a/src/rendering/twn_billboards.c b/src/rendering/twn_billboards.c index ec4ec4d..6ead8e7 100644 --- a/src/rendering/twn_billboards.c +++ b/src/rendering/twn_billboards.c @@ -12,7 +12,7 @@ void draw_billboard(char const *texture, Vec3 position, Vec2 size, - Rect const *texture_region, + Rect texture_region, Color color, bool cylindrical) { @@ -26,16 +26,19 @@ void draw_billboard(char const *texture, batch_p = &ctx.billboard_batches[hmlenu(ctx.billboard_batches) - 1]; /* TODO: can last index be used? */ } + bool const texture_region_valid = fabsf(texture_region.w - texture_region.h) > 0.00001f + && fabsf(0.0f - texture_region.w) > 0.00001f; + struct SpaceBillboard billboard = { .color = color, .cylindrical = cylindrical, .position = position, .size = size, - .texture_region_opt_set = texture_region != NULL, + .texture_region_opt_set = texture_region_valid, }; - if (texture_region) - billboard.texture_region_opt = *texture_region; + if (texture_region_valid) + billboard.texture_region_opt = texture_region; struct SpaceBillboard *billboards = (struct SpaceBillboard *)(void *)batch_p->value.primitives; diff --git a/src/rendering/twn_sprites.c b/src/rendering/twn_sprites.c index c5dc0a5..ecec32f 100644 --- a/src/rendering/twn_sprites.c +++ b/src/rendering/twn_sprites.c @@ -19,13 +19,18 @@ */ void draw_sprite(char const *path, Rect rect, - Rect const *texture_region, /* optional, default: NULL */ + Rect texture_region, /* optional, default: 0 */ 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) { + /* if .w and .h are zeroed then assume whole region */ + /* TODO: don't check here, just move to redner code ? */ + bool const texture_region_valid = fabsf(texture_region.w - texture_region.h) > 0.00001f + && fabsf(0.0f - texture_region.w) > 0.00001f; + SpritePrimitive sprite = { .rect = rect, .color = color, @@ -34,11 +39,11 @@ void draw_sprite(char const *path, .flip_x = flip_x, .flip_y = flip_y, .repeat = !stretch, - .texture_region_opt_set = texture_region != NULL, + .texture_region_opt_set = texture_region_valid, }; - if (texture_region) - sprite.texture_region_opt = *texture_region; + if (texture_region_valid) + sprite.texture_region_opt = texture_region; Primitive2D primitive = { .type = PRIMITIVE_2D_SPRITE, @@ -54,7 +59,7 @@ void draw_sprite_args(const DrawSpriteArgs args) { bool const flip_x = m_or(args, flip_x, false); bool const flip_y = m_or(args, flip_y, false); bool const stretch = m_or(args, stretch, false); - Rect const *texture_region = m_is_set(args, texture_region) ? &args.texture_region_opt : NULL; + Rect const texture_region = m_or(args, texture_region, ((Rect){0})); draw_sprite(args.texture, args.rect, texture_region, color, rotation, flip_x, flip_y, stretch); }