diff options
| author | jussi | 2025-04-09 00:34:19 +0300 |
|---|---|---|
| committer | jussi | 2025-04-09 00:34:19 +0300 |
| commit | 3471e4514ffae28cab3fb926c86e5bf461723ac9 (patch) | |
| tree | a1fda2f7c82eb360cc897a60e52519b168791b78 /src | |
| parent | d0bfd1f71d852eb55ec0e94792a02c7291080633 (diff) | |
| download | reilua-enhanced-3471e4514ffae28cab3fb926c86e5bf461723ac9.tar.gz reilua-enhanced-3471e4514ffae28cab3fb926c86e5bf461723ac9.tar.bz2 reilua-enhanced-3471e4514ffae28cab3fb926c86e5bf461723ac9.zip | |
RectPack.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lua_core.c | 1 | ||||
| -rw-r--r-- | src/shapes.c | 47 |
2 files changed, 48 insertions, 0 deletions
diff --git a/src/lua_core.c b/src/lua_core.c index 389da1a..29a6bf8 100644 --- a/src/lua_core.c +++ b/src/lua_core.c @@ -1853,6 +1853,7 @@ void luaRegister() { assingGlobalFunction( "CheckCollisionLines", lshapesCheckCollisionLines ); assingGlobalFunction( "CheckCollisionPointLine", lshapesCheckCollisionPointLine ); assingGlobalFunction( "GetCollisionRec", lshapesGetCollisionRec ); + assingGlobalFunction( "RectPack", lshapesRectPack ); /* Textures. */ /* Image loading functions. */ 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; +} |
