Skip to content

Commit

Permalink
Merge pull request #58 from s-lambert/add-texture-npatch-binding
Browse files Browse the repository at this point in the history
Add DrawTextureNPatch
  • Loading branch information
bakpakin authored May 18, 2024
2 parents 7e8ffbb + e0c81d3 commit 4875309
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,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);
Expand Down Expand Up @@ -926,6 +938,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"
Expand Down
24 changes: 24 additions & 0 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
Binary file added test/nine-patch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion test/test2.janet
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@
(def cells (gen-image-cellular 100 100 10))
(def cells-t (load-texture-from-image cells))

(def nine-patch (load-image-1 "test/nine-patch.png"))
(def nine-patch-t (load-texture-from-image nine-patch))

(while (not (window-should-close))
(begin-drawing)
(clear-background :green)

(draw-texture lenna-t 100 100 :white)
(draw-texture lenna-bt 200 200 :white)
(draw-texture cells-t 600 0 :white)
(draw-texture gen-t 10 10 :white)
(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)

(end-drawing))

Expand Down

0 comments on commit 4875309

Please sign in to comment.