From deda5fb9170295133444809618835e8b9aa61512 Mon Sep 17 00:00:00 2001 From: jussi Date: Sun, 27 Nov 2022 14:08:58 +0200 Subject: [PATCH] Exit function and N-Patch fix. --- API.md | 6 ++ changelog | 2 + examples/n-patches/main.lua | 25 +++++++ examples/resources/images/n-patch.png | Bin 0 -> 6021 bytes include/lua_core.h | 1 + src/lua_core.c | 96 +++++++++++++++----------- src/main.c | 1 + src/textures.c | 1 + 8 files changed, 92 insertions(+), 40 deletions(-) create mode 100644 examples/n-patches/main.lua create mode 100644 examples/resources/images/n-patch.png diff --git a/API.md b/API.md index c30e84a..94b9d65 100644 --- a/API.md +++ b/API.md @@ -31,6 +31,12 @@ This function can be used for custom log message handling. --- +> function exit() + +This function will be called on program close. Cleanup could be done here. + +--- + ## Globals - ConfigFlags FLAG_VSYNC_HINT diff --git a/changelog b/changelog index 92caf06..db08e35 100644 --- a/changelog +++ b/changelog @@ -5,6 +5,8 @@ KEY CHANGES: - ADDED: This changelog. - ADDED: Lua interpreter mode. - ADDED: Easings extra module. + - ADDED: exit function. + - FIX: uluaGetNPatchInfo fix for RL_DrawTextureNPatch. Guess this was never tested and did not work at all >:E. Detailed changes: ADDED: Help argument. diff --git a/examples/n-patches/main.lua b/examples/n-patches/main.lua new file mode 100644 index 0000000..045bb84 --- /dev/null +++ b/examples/n-patches/main.lua @@ -0,0 +1,25 @@ +local dstRec = { 160.0, 160.0, 8.0, 8.0 }; +local origin = { 0.0, 0.0 } + +-- local ninePatchInfo = { { 0.0, 0.0, 24.0, 24.0 }, 8, 8, 8, 8, NPATCH_NINE_PATCH } +local ninePatchInfo = { source = { 0, 0, 24.0, 24.0 }, left = 8, top = 8, right = 8, bottom = 8, layout = NPATCH_NINE_PATCH } + +local nPatchTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/n-patch.png" ) + +function init() + RL_SetWindowTitle( "N-Patches" ) + RL_SetWindowState( FLAG_VSYNC_HINT ) +end + +function process( delta ) + local mousePosition = RL_GetMousePosition(); + + -- Resize the n-patch based on mouse position + dstRec[3] = mousePosition[1] - dstRec[1]; + dstRec[4] = mousePosition[2] - dstRec[2]; +end + +function draw() + RL_ClearBackground( RAYWHITE ) + RL_DrawTextureNPatch( nPatchTexture, ninePatchInfo, dstRec, origin, 0.0, WHITE ) +end diff --git a/examples/resources/images/n-patch.png b/examples/resources/images/n-patch.png new file mode 100644 index 0000000000000000000000000000000000000000..c6e738bfca9356b047df2bc57fd9d33d3b2f638b GIT binary patch literal 6021 zcmeHKX;2f{77jv?O$XTqSu_Mi7!27+NFs~u;1Je`BcjkrIx#{>Ou{;Ys30mR1Vq_Y z1P75t5kUnM6qO)|;wTOR;-G9U;0S^s4zCmUdavr0Rqy>XRo&@(mhYVV-E&Xh-s|n@ zxa{Mr`1M7G+d+@Bg$6waE-bq&9ib9bn^YI-Jc?IGtocju=1?B_1G z?$&Lox+-?adiknlMef!`#RCV23bSW_3w+o!nteZ_w(s<20jX|)t5*81ytl`2W$`}q zZ33-lF@^OH-L}16YL(KNlIOzQ+TriY34iij&{VeY6|T&z$n=(@X)O8Fk)Ys4&zKXj zlQ!Aa=U)7ZUVRFymtWmmu-J9zxyJTCded9a1N^g|!JC7{hIQK?KM8Y$BgXi(4H`&2 zy_0bv6IX7$yHU?8Ms0XlVG<@UIDe$C=D=T5Wo@_ZUO#^v0|s@kV07*w4nMPXNoFj; zyQIa!?gEB)xu)aV&sAz~R~Q?e#VAK4s%D={Ksnsg_TB84uTrxmz4B;?E-$Brm1TRR zv_z?++fyfSeMo7zkf9LS;$J))-P?A$KRv`^=Y7f_qxlz`>4RZ^&^V*0V?(UqEerA% z-@A5if5o=oeH3HsYpeYCvE-O?Pxix7EnB3<7n4!Y?;wj$sTOHlBPNIXy*?G4&tNk*knzw zD;iGA)}$M3oJeEr(O`IBSL7O`n-q(eUCZ!oAI3KcuNULh1Qs>t`&Ipvo!6`^d7?5+ z3%Zc_6QPqPthz~flB$z)_D|o|UXfeL+jbk#NM-Ti`9(y71$I#hM#d?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AN7}&#+M?LY*!oP z=V@&$x+*;2kgSU~7;TOln-h*+*xs;-b+T)bJnbgVckg*|ONGkibrZo^TeRdg@UO`} z9#@31y-m$ib*8soU78wtx>jH!|J%H+`$iI->+P+pI3syedz;Ek&8`kz)l?+7%}T<% z2CUlQyWd&kLyP>zuLAUf+t&s9RmTc6yfj_4))xL2cOBd{uEVYl1aI0-t2&aoVD>Yb zv0Lz>pVf_4yJ#=Z0F2huxg_n?%5S!rbxa9bmg!NUp-a~B-l5X+cl_Mtv96ry5_c;z zt@)Io(69sotMFq!h3@T)xZ; zBKs>#5r_4etgmvDYGl*-G7-rA6Yf{mALlMJhODSmiW83!E=|wf$p$HnPhs*H941Bf z2mmZRi-jYh=vH(qlqCa)M*%n-fWpzqSb&JP1Tkdn7f|kDLJ<(g0Hsh6xCIBoAzCrX zRt#t%@k|1MvLvto6hMFwEdc@sV?}3KTH;w>KzIo_P*nnmP`VHj-{hO zEDj3+fD9Cgj-jJ4Oa{veU;=c&iY$X-GAJ%Q0T+OllfwnrAetY>mMNr!Q|!InZICz% z%x8&rC?H}%4mL;+PFQ%9SCu$w0&c(mI6zWO8UO6h{FFhoamPON1HUo;uW@4XCaUQjPYe`aK@%QTbPS-av@J+_wtLuM8m&)hQDKHE=1w}$1OA;q|DfFSG zNdMW@2}Xx4fx*<27nwp5Wxm^bAq=*#NqWgO^G+}!;e3%h)p`D)s=g`-o>`!^91;oL zo$P6D%`XmBE@(ukDctgj2*wZ#07a|NE67m$5wmoay+y?QhL{WI&EL)QUK33ccjicT z7H9Op8ET`3n?kgIIDOKVrPx@VvgxJWvwpsV`cCx(go4oh|S jCQMF3UYd=%7#S@`eQum3zlJygg@?I2dpcD(1n>M8aA|ur literal 0 HcmV?d00001 diff --git a/include/lua_core.h b/include/lua_core.h index 58b84d2..228c4c0 100644 --- a/include/lua_core.h +++ b/include/lua_core.h @@ -5,6 +5,7 @@ int luaTraceback( lua_State *L ); bool luaCallMain(); void luaCallProcess(); void luaCallDraw(); +void luaCallExit(); void luaRegister(); /* Lua Util functions */ Color uluaGetColor( lua_State *L ); diff --git a/src/lua_core.c b/src/lua_core.c index ac0ba32..1d46440 100644 --- a/src/lua_core.c +++ b/src/lua_core.c @@ -645,6 +645,23 @@ void luaCallDraw() { lua_pop( L, -1 ); } +void luaCallExit() { + lua_State *L = state->luaState; + lua_pushcfunction( L, luaTraceback ); + int tracebackidx = lua_gettop(L); + + lua_getglobal( L, "exit" ); + + if ( lua_isfunction( L, -1 ) ) { + if ( lua_pcall( L, 0, 0, tracebackidx ) != 0 ) { + TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); + state->run = false; + return; + } + } + lua_pop( L, -1 ); +} + void luaRegister() { lua_State *L = state->luaState; @@ -1730,54 +1747,53 @@ NPatchInfo uluaGetNPatchInfo( lua_State *L ) { lua_pushnil( L ); while ( lua_next( L, t ) != 0 ) { - if ( lua_isnumber( L, -1 ) ) { - if ( lua_isnumber( L, -2 ) ) { - switch ( i ) { - case 0: - npatch.source = uluaGetRectangle( L ); - break; - case 1: - npatch.left = lua_tointeger( L, -1 ); - break; - case 2: - npatch.top = lua_tointeger( L, -1 ); - break; - case 3: - npatch.right = lua_tointeger( L, -1 ); - break; - case 4: - npatch.bottom = lua_tointeger( L, -1 ); - break; - case 5: - npatch.layout = lua_tointeger( L, -1 ); - break; - default: - break; - } - } - else if ( lua_isstring( L, -2 ) ) { - if ( strcmp( "source", (char*)lua_tostring( L, -2 ) ) == 0 ) { + /* Do not check type since there should be table and ints. */ + if ( lua_isnumber( L, -2 ) ) { + switch ( i ) { + case 0: npatch.source = uluaGetRectangle( L ); - } - else if ( strcmp( "left", (char*)lua_tostring( L, -2 ) ) == 0 ) { + break; + case 1: npatch.left = lua_tointeger( L, -1 ); - } - else if ( strcmp( "top", (char*)lua_tostring( L, -2 ) ) == 0 ) { + break; + case 2: npatch.top = lua_tointeger( L, -1 ); - } - else if ( strcmp( "right", (char*)lua_tostring( L, -2 ) ) == 0 ) { + break; + case 3: npatch.right = lua_tointeger( L, -1 ); - } - else if ( strcmp( "bottom", (char*)lua_tostring( L, -2 ) ) == 0 ) { + break; + case 4: npatch.bottom = lua_tointeger( L, -1 ); - } - else if ( strcmp( "layout", (char*)lua_tostring( L, -2 ) ) == 0 ) { + break; + case 5: npatch.layout = lua_tointeger( L, -1 ); - } + break; + default: + break; } - i++; - lua_pop( L, 1 ); } + else if ( lua_isstring( L, -2 ) ) { + if ( strcmp( "source", (char*)lua_tostring( L, -2 ) ) == 0 ) { + npatch.source = uluaGetRectangle( L ); + } + else if ( strcmp( "left", (char*)lua_tostring( L, -2 ) ) == 0 ) { + npatch.left = lua_tointeger( L, -1 ); + } + else if ( strcmp( "top", (char*)lua_tostring( L, -2 ) ) == 0 ) { + npatch.top = lua_tointeger( L, -1 ); + } + else if ( strcmp( "right", (char*)lua_tostring( L, -2 ) ) == 0 ) { + npatch.right = lua_tointeger( L, -1 ); + } + else if ( strcmp( "bottom", (char*)lua_tostring( L, -2 ) ) == 0 ) { + npatch.bottom = lua_tointeger( L, -1 ); + } + else if ( strcmp( "layout", (char*)lua_tostring( L, -2 ) ) == 0 ) { + npatch.layout = lua_tointeger( L, -1 ); + } + } + i++; + lua_pop( L, 1 ); } return npatch; } diff --git a/src/main.c b/src/main.c index 0f935a1..d1be4a1 100644 --- a/src/main.c +++ b/src/main.c @@ -71,6 +71,7 @@ int main( int argn, const char **argc ) { luaCallProcess(); luaCallDraw(); } + luaCallExit(); } stateFree(); diff --git a/src/textures.c b/src/textures.c index 7cc423a..0b1a198 100644 --- a/src/textures.c +++ b/src/textures.c @@ -2099,6 +2099,7 @@ int ltexturesDrawTextureNPatch( lua_State *L ) { Rectangle dest = uluaGetRectangle( L ); lua_pop( L, 1 ); NPatchInfo nPatchInfo = uluaGetNPatchInfo( L ); + lua_pop( L, 1 ); size_t texId = lua_tointeger( L, -1 );