summaryrefslogtreecommitdiff
path: root/src/shapes.c
diff options
context:
space:
mode:
authorjussi2025-04-09 00:34:19 +0300
committerjussi2025-04-09 00:34:19 +0300
commit3471e4514ffae28cab3fb926c86e5bf461723ac9 (patch)
treea1fda2f7c82eb360cc897a60e52519b168791b78 /src/shapes.c
parentd0bfd1f71d852eb55ec0e94792a02c7291080633 (diff)
downloadreilua-enhanced-3471e4514ffae28cab3fb926c86e5bf461723ac9.tar.gz
reilua-enhanced-3471e4514ffae28cab3fb926c86e5bf461723ac9.tar.bz2
reilua-enhanced-3471e4514ffae28cab3fb926c86e5bf461723ac9.zip
RectPack.
Diffstat (limited to 'src/shapes.c')
-rw-r--r--src/shapes.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/shapes.c b/src/shapes.c
index 07f40c3..527632d 100644
--- a/src/shapes.c
+++ b/src/shapes.c
@@ -1040,3 +1040,50 @@ int lshapesGetCollisionRec( lua_State* L ) {
return 1;
}
+
+/*
+> rects{} = RL.RectPack( Rectangle{} rects, Vector2 size, int padding )
+
+Useful for e.g. packing rectangular textures into an atlas. stbrp_pack_rects
+
+- Success return Rectangle{}
+*/
+int lshapesRectPack( lua_State* L ) {
+ int rectCount = uluaGetTableLen( L, 1 );
+ Vector2 size = uluaGetVector2( L, 2 );
+ int padding = luaL_checknumber( L, 3 );
+
+ stbrp_context *context = (stbrp_context*)RL_MALLOC( sizeof( *context ) );
+ stbrp_node *nodes = (stbrp_node*)RL_MALLOC( rectCount * sizeof( *nodes ) );
+
+ stbrp_init_target( context, size.x, size.y, nodes, rectCount ) ;
+ stbrp_rect *rects = (stbrp_rect*)RL_MALLOC( rectCount * sizeof( stbrp_rect ) );
+
+ int t = 1, i = 0;
+ lua_pushnil( L );
+
+ while ( lua_next( L, t ) != 0 ) {
+ Rectangle rect = uluaGetRectangle( L, lua_gettop( L ) );
+ rects[i] = (stbrp_rect){ .id = 0, .x = rect.x, .y = rect.y,
+ .w = rect.width + padding, .h = rect.height + padding, .was_packed = 0
+ };
+ i++;
+ lua_pop( L, 1 );
+ }
+ stbrp_pack_rects( context, rects, rectCount );
+
+ lua_createtable( L, rectCount, 0 );
+
+ for ( int i = 0; i < rectCount; i++ ) {
+ uluaPushRectangle( L, (Rectangle){ .x = rects[i].x, .y = rects[i].y,
+ .width = rects[i].w - padding, .height = rects[i].h - padding }
+ );
+ lua_rawseti( L, -2, i+1 );
+ }
+
+ RL_FREE( rects );
+ RL_FREE( nodes );
+ RL_FREE( context );
+
+ return 1;
+}