From 8ff3b8cced78ad85a3fa3c5a0d5648777af9f804 Mon Sep 17 00:00:00 2001 From: Scott Lambert Date: Wed, 7 Feb 2024 17:26:45 +0800 Subject: [PATCH] Add DrawTextureNPatch binding --- src/image.h | 16 ++++++++++++++++ src/types.h | 24 ++++++++++++++++++++++++ test/nine-patch.png | Bin 0 -> 787 bytes test/test2.janet | 4 ++++ 4 files changed, 44 insertions(+) create mode 100644 test/nine-patch.png diff --git a/src/image.h b/src/image.h index c5f7dd0..b4c904e 100644 --- a/src/image.h +++ b/src/image.h @@ -487,6 +487,18 @@ static Janet cfun_DrawTextureRec(int32_t argc, Janet *argv) { return janet_wrap_nil(); } +static Janet cfun_DrawTextureNPatch(int32_t argc, Janet *argv) { + janet_fixarity(argc, 6); + Texture2D texture = *jaylib_gettexture2d(argv, 0); + NPatchInfo nPatchInfo = jaylib_getnpatchinfo(argv, 1); + Rectangle rect = jaylib_getrect(argv, 2); + Vector2 origin = jaylib_getvec2(argv, 3); + float rotation = (float)janet_getnumber(argv, 4); + Color color = jaylib_getcolor(argv, 5); + DrawTextureNPatch(texture, nPatchInfo, rect, origin, rotation, color); + return janet_wrap_nil(); +} + static Janet cfun_DrawTexturePro(int32_t argc, Janet *argv) { janet_fixarity(argc, 6); Texture2D texture = *jaylib_gettexture2d(argv, 0); @@ -874,6 +886,10 @@ static const JanetReg image_cfuns[] = { "(draw-texture-rec texture source position tint)\n\n" "Draw a part of a texture defined by a rectangle" }, + {"draw-texture-n-patch", cfun_DrawTextureNPatch, + "(draw-texture-n-patch texture n-patch-info dest origin rotation tint)\n\n" + "Draw a texture (or part of it) that stretches or shrinks nicely" + }, {"gen-image-color", cfun_GenImageColor, "(gen-image-color width height color)\n\n" "Generate image: plain color" diff --git a/src/types.h b/src/types.h index 46bda33..b914c74 100644 --- a/src/types.h +++ b/src/types.h @@ -396,6 +396,30 @@ static Matrix jaylib_getmatrix(const Janet *argv, int32_t n) { }; } +static const KeyDef nine_patch_layouts[] = { + {"npatch-nine-patch", NPATCH_NINE_PATCH}, + {"npatch-three-patch-vertical", NPATCH_THREE_PATCH_VERTICAL}, + {"npatch-three-patch-horizontal", NPATCH_THREE_PATCH_HORIZONTAL} +}; + +static int jaylib_getnpatchlayout(const Janet *argv, int32_t n) { + return jaylib_castdef(argv, n, nine_patch_layouts, sizeof(nine_patch_layouts) / sizeof(KeyDef)); +} + +static NPatchInfo jaylib_getnpatchinfo(const Janet *argv, int32_t n) { + JanetView idx = janet_getindexed(argv, n); + Rectangle rect = jaylib_getrect(idx.items, 0); + + return (NPatchInfo){ + rect, + janet_unwrap_integer(idx.items[1]), + janet_unwrap_integer(idx.items[2]), + janet_unwrap_integer(idx.items[3]), + janet_unwrap_integer(idx.items[4]), + jaylib_getnpatchlayout(idx.items, 5), + }; +} + static const KeyDef pixel_format_defs[] = { {"astc-4x4-rgba", PIXELFORMAT_COMPRESSED_ASTC_4x4_RGBA}, {"astc-8x8-rgba", PIXELFORMAT_COMPRESSED_ASTC_8x8_RGBA}, diff --git a/test/nine-patch.png b/test/nine-patch.png new file mode 100644 index 0000000000000000000000000000000000000000..aaad7f69ea09d7da42bbd47b167e214e0b03772e GIT binary patch literal 787 zcmeAS@N?(olHy`uVBq!ia0vp^2_VeD1|%QND7OGojKx9jP7LeL$-HD>U^?sR;uumf z=j~nFE)hqWwvXv;JCEKwEIcD;+XC|mwoNl6q&Su-bTSaZyOr9c$(nW8^am=>xYSaaNet6K)^hK`THx`I&*I$3{0 zv|Da5bj#RZWm>3qV9{~^t8Ob;H*|drHWkcbSOb*SZOLc|?VG=S9RCMh3Zhr(l6&3>ANRjw!`3fB3=L>nOqW;Rj=^^A45=ADN|{UH}C}ceNDoJ@}}7 zt^ES)habFF?jArh6JuL!<>G^!EEsBPKYN!c-?zJSVjb^?S5K{9ehoc-`ED|sKsrMq zbqMx`-OKY+%|CqpT7GjYl36VW^m_RZ-)HU-xNzY|!Qbu~f?T`I?0SBy z<+Su1>*0Sa?<$e#lzhBB^L$f}!6MDahrhHJx=Ccb`Z3|V$rTRUr#<{X`xp0K&@8Xf zsFqJ)Id!c4lep1rHw{_&q9o4EkHCtAERQxvggPMTS@%A6nKdpA_kduEk zrz=9$srTc;dmE>ODqfM2e>IOgiq+}t$A$k6u5neqA}4=qPEWLoQ}@S(cP}1sRoo&a ze`_9ZEQ{0Wj|=}kyc4OsMNZyi`Qr4>1rqWl$1kV1E|7{ZyXg1u|6*I?n|%CgEs}@T Tf3z3?Q#pgDtDnm{r-UW|7