From 2351d4114cb2ee15231f75f9cfa38d8d3878a717 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Tue, 14 Jan 2025 02:53:18 +0300 Subject: [PATCH] twn_draw.c: add draw_quad() --- include/twn_draw.h | 8 ++++++++ share/twn_api.json | 15 +++++++++++++++ src/rendering/twn_draw.c | 25 +++++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/include/twn_draw.h b/include/twn_draw.h index 757e55a..e81a045 100644 --- a/include/twn_draw.h +++ b/include/twn_draw.h @@ -55,6 +55,14 @@ TWN_API void draw_triangle(char const *texture, Color c1, /* optional, default: all 255 */ Color c2); /* optional, default: all 255 */ +TWN_API void draw_quad(char const *texture, + Vec3 v0, /* upper-left */ + Vec3 v1, /* bottom-left */ + Vec3 v2, /* bottom-right */ + Vec3 v3, /* upper-right */ + Rect texture_region, + Color color); /* optional, default: all 255 */ + TWN_API void draw_billboard(const char *texture, Vec3 position, Vec2 size, diff --git a/share/twn_api.json b/share/twn_api.json index 2e3f7c9..964deae 100644 --- a/share/twn_api.json +++ b/share/twn_api.json @@ -145,6 +145,21 @@ ] }, + "draw_quad": { + "module": "draw", + "symbol": "quad", + "header": "twn_draw.h", + "params": [ + { "name": "texture", "type": "char *" }, + { "name": "v0", "type": "Vec3" }, + { "name": "v1", "type": "Vec3" }, + { "name": "v2", "type": "Vec3" }, + { "name": "v3", "type": "Vec3" }, + { "name": "texture_region", "type": "Rect" }, + { "name": "color", "type": "Color", "default": { "r": 255, "g": 255, "b": 255, "a": 255 } } + ] + }, + "draw_billboard": { "module": "draw", "symbol": "billboard", diff --git a/src/rendering/twn_draw.c b/src/rendering/twn_draw.c index 0a2dd73..35ff13d 100644 --- a/src/rendering/twn_draw.c +++ b/src/rendering/twn_draw.c @@ -179,6 +179,31 @@ void draw_nine_slice(const char *texture, Vec2 corners, Rect rect, float border_ } +TWN_API void draw_quad(char const *texture, + Vec3 v0, /* upper-left */ + Vec3 v1, /* bottom-left */ + Vec3 v2, /* bottom-right */ + Vec3 v3, /* upper-right */ + Rect texture_region, + Color color) +{ + Vec2 const uv0 = { texture_region.x, texture_region.y }; + Vec2 const uv1 = { texture_region.x, texture_region.y + texture_region.h }; + Vec2 const uv2 = { texture_region.x + texture_region.w, texture_region.y + texture_region.h }; + Vec2 const uv3 = { texture_region.x + texture_region.w, texture_region.y }; + + draw_triangle(texture, + v0, v1, v3, + uv0, uv1, uv3, + color, color, color); + + draw_triangle(texture, + v3, v1, v2, + uv3, uv1, uv2, + color, color, color); +} + + static void render_2d(void) { use_2d_pipeline();