Skip to content

Commit 4875309

Browse files
authored
Merge pull request #58 from s-lambert/add-texture-npatch-binding
Add DrawTextureNPatch
2 parents 7e8ffbb + e0c81d3 commit 4875309

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

src/image.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,18 @@ static Janet cfun_DrawTextureRec(int32_t argc, Janet *argv) {
520520
return janet_wrap_nil();
521521
}
522522

523+
static Janet cfun_DrawTextureNPatch(int32_t argc, Janet *argv) {
524+
janet_fixarity(argc, 6);
525+
Texture2D texture = *jaylib_gettexture2d(argv, 0);
526+
NPatchInfo nPatchInfo = jaylib_getnpatchinfo(argv, 1);
527+
Rectangle rect = jaylib_getrect(argv, 2);
528+
Vector2 origin = jaylib_getvec2(argv, 3);
529+
float rotation = (float)janet_getnumber(argv, 4);
530+
Color color = jaylib_getcolor(argv, 5);
531+
DrawTextureNPatch(texture, nPatchInfo, rect, origin, rotation, color);
532+
return janet_wrap_nil();
533+
}
534+
523535
static Janet cfun_DrawTexturePro(int32_t argc, Janet *argv) {
524536
janet_fixarity(argc, 6);
525537
Texture2D texture = *jaylib_gettexture2d(argv, 0);
@@ -926,6 +938,10 @@ static const JanetReg image_cfuns[] = {
926938
"(draw-texture-rec texture source position tint)\n\n"
927939
"Draw a part of a texture defined by a rectangle"
928940
},
941+
{"draw-texture-n-patch", cfun_DrawTextureNPatch,
942+
"(draw-texture-n-patch texture n-patch-info dest origin rotation tint)\n\n"
943+
"Draw a texture (or part of it) that stretches or shrinks nicely"
944+
},
929945
{"gen-image-color", cfun_GenImageColor,
930946
"(gen-image-color width height color)\n\n"
931947
"Generate image: plain color"

src/types.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,30 @@ static Matrix jaylib_getmatrix(const Janet *argv, int32_t n) {
396396
};
397397
}
398398

399+
static const KeyDef nine_patch_layouts[] = {
400+
{"npatch-nine-patch", NPATCH_NINE_PATCH},
401+
{"npatch-three-patch-vertical", NPATCH_THREE_PATCH_VERTICAL},
402+
{"npatch-three-patch-horizontal", NPATCH_THREE_PATCH_HORIZONTAL}
403+
};
404+
405+
static int jaylib_getnpatchlayout(const Janet *argv, int32_t n) {
406+
return jaylib_castdef(argv, n, nine_patch_layouts, sizeof(nine_patch_layouts) / sizeof(KeyDef));
407+
}
408+
409+
static NPatchInfo jaylib_getnpatchinfo(const Janet *argv, int32_t n) {
410+
JanetView idx = janet_getindexed(argv, n);
411+
Rectangle rect = jaylib_getrect(idx.items, 0);
412+
413+
return (NPatchInfo){
414+
rect,
415+
janet_unwrap_integer(idx.items[1]),
416+
janet_unwrap_integer(idx.items[2]),
417+
janet_unwrap_integer(idx.items[3]),
418+
janet_unwrap_integer(idx.items[4]),
419+
jaylib_getnpatchlayout(idx.items, 5),
420+
};
421+
}
422+
399423
static const KeyDef pixel_format_defs[] = {
400424
{"astc-4x4-rgba", PIXELFORMAT_COMPRESSED_ASTC_4x4_RGBA},
401425
{"astc-8x8-rgba", PIXELFORMAT_COMPRESSED_ASTC_8x8_RGBA},

test/nine-patch.png

787 Bytes
Loading

test/test2.janet

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@
2626
(def cells (gen-image-cellular 100 100 10))
2727
(def cells-t (load-texture-from-image cells))
2828

29+
(def nine-patch (load-image-1 "test/nine-patch.png"))
30+
(def nine-patch-t (load-texture-from-image nine-patch))
31+
2932
(while (not (window-should-close))
3033
(begin-drawing)
3134
(clear-background :green)
3235

3336
(draw-texture lenna-t 100 100 :white)
3437
(draw-texture lenna-bt 200 200 :white)
3538
(draw-texture cells-t 600 0 :white)
36-
(draw-texture gen-t 10 10 :white)
39+
(draw-texture-n-patch nine-patch-t [[0 0 96 96] 32 32 32 32 :npatch-nine-patch] [0 0 96 138] [0 0] 0 :white)
3740

3841
(end-drawing))
3942

0 commit comments

Comments
 (0)