From 3df788e69a109cc210051177f60b7681570bdfe5 Mon Sep 17 00:00:00 2001 From: Ratstail91 Date: Sat, 4 Mar 2023 08:19:18 +1100 Subject: [PATCH] Got character walking around, fixed leak --- Airport.vcxproj | 2 + assets/scripts/gameplay/lejana.toy | 154 +++++++++++++++++++++++++++++ assets/scripts/gameplay/scene.toy | 0 assets/scripts/init.toy | 4 +- assets/sprites/lejana.png | Bin 0 -> 1569 bytes box/box_engine.c | 8 +- box/lib_engine.c | 2 +- 7 files changed, 164 insertions(+), 6 deletions(-) create mode 100644 assets/scripts/gameplay/lejana.toy create mode 100644 assets/scripts/gameplay/scene.toy create mode 100644 assets/sprites/lejana.png diff --git a/Airport.vcxproj b/Airport.vcxproj index ee0f36c..999d88a 100644 --- a/Airport.vcxproj +++ b/Airport.vcxproj @@ -28,6 +28,8 @@ + + diff --git a/assets/scripts/gameplay/lejana.toy b/assets/scripts/gameplay/lejana.toy new file mode 100644 index 0000000..e51303a --- /dev/null +++ b/assets/scripts/gameplay/lejana.toy @@ -0,0 +1,154 @@ +import node; + +//constants +var SPEED: int const = 3; + +//variables +var parent: opaque = null; //cache the parent for quick access +var posX: int = 50; +var posY: int = 50; +var WIDTH: int const = 128; +var HEIGHT: int const = 128; + +var xspeed: int = 0; +var yspeed: int = 0; + + +//polyfills - animating different cycles on one image +var SPRITE_WIDTH: int const = 32; +var SPRITE_HEIGHT: int const = 32; +var stepCount: int = 0; +var stepDelay: int = 10; + +fn faceDown(node: opaque) { + node.setNodeRect(0, 0, SPRITE_WIDTH, SPRITE_HEIGHT); + node.setNodeFrames(4); +} + +fn faceUp(node: opaque) { + node.setNodeRect(32 * 4, 0, SPRITE_WIDTH, SPRITE_HEIGHT); + node.setNodeFrames(4); +} + +fn faceLeft(node: opaque) { + node.setNodeRect(32 * 8, 0, SPRITE_WIDTH, SPRITE_HEIGHT); + node.setNodeFrames(4); +} + +fn faceRight(node: opaque) { + node.setNodeRect(32 * 8, 0, SPRITE_WIDTH, SPRITE_HEIGHT); + node.setNodeFrames(4); +} + + +//lifecycle functions +fn onLoad(node: opaque) { + // +} + +fn onInit(node: opaque) { + parent = node.getParentNode(); + node.loadTexture("sprites:/lejana.png"); + node.faceDown(); +} + +fn onStep(node: opaque) { + posX += xspeed; + posY += yspeed; + + //animate after X steps + if (xspeed == 0 && yspeed == 0) { + stepCount = 0; + node.setCurrentNodeFrame(0); + } + else { + if (++stepCount >= stepDelay) { + node.incrementCurrentNodeFrame(); + stepCount = 0; + } + } +} + +fn onFree(node: opaque) { + node.freeTexture(); +} + +fn onDraw(node: opaque) { + var px = 0; + var py = 0; + + if (parent != null) { + px = parent.callNodeFn("getX"); + py = parent.callNodeFn("getY"); + } + + node.drawNode(posX + px, posY + py, WIDTH, HEIGHT); +} + +//event functions +fn onKeyDown(node: opaque, event: string) { + if (event == "character_up") { + yspeed -= SPEED; + faceUp(node); + return; + } + + if (event == "character_down") { + yspeed += SPEED; + faceDown(node); + return; + } + + if (event == "character_left") { + xspeed -= SPEED; + faceLeft(node); + return; + } + + if (event == "character_right") { + xspeed += SPEED; + faceRight(node); + return; + } +} + +fn onKeyUp(node: opaque, event: string) { + if (event == "character_up" && yspeed < 0) { + yspeed = 0; + return; + } + + if (event == "character_down" && yspeed > 0) { + yspeed = 0; + return; + } + + if (event == "character_left" && xspeed < 0) { + xspeed = 0; + return; + } + + if (event == "character_right" && xspeed > 0) { + xspeed = 0; + return; + } +} + +fn onMouseMotion(node: opaque, x: int, y: int, xrel: int, yrel: int) { + // +} + +fn onMouseButtonDown(node: opaque, x: int, y: int, button: string) { + //jump to pos + posX = x - WIDTH / 2; + posY = y - HEIGHT / 2; +} + +fn onMouseButtonUp(node: opaque, x: int, y: int, button: string) { + // +} + +fn onMouseWheel(node: opaque, xrel: int, yrel: int) { + // +} + diff --git a/assets/scripts/gameplay/scene.toy b/assets/scripts/gameplay/scene.toy new file mode 100644 index 0000000..e69de29 diff --git a/assets/scripts/init.toy b/assets/scripts/init.toy index fc969ec..614326f 100644 --- a/assets/scripts/init.toy +++ b/assets/scripts/init.toy @@ -28,10 +28,10 @@ //this function must always be called, or the engine won't run - initWindow("Airport Game", 1080, 720, false); + initWindow("Skyland", 1080, 720, false); //kick off the logic of the scene graph - loadRootNode("scripts:/demo/scene.toy"); + loadRootNode("scripts:/gameplay/lejana.toy"); } //Globals go here diff --git a/assets/sprites/lejana.png b/assets/sprites/lejana.png new file mode 100644 index 0000000000000000000000000000000000000000..e1b279f20a205470bef5b7950330d922d183566a GIT binary patch literal 1569 zcmV++2HyFJP)Px)+et)0RCt{2U9oQCI1qilY!e_=iv5EC`GIZo8SV|Z*fRBEv4vq=eWeQn+xs)r z-VYq$KS<%qfUAYBu*oHD{{x%*Pla=c~AU+Yy*_mubm$#{%ht3X5@crz;(4- zq562h_aA$2hY5<=1ZjR71BG1Nit{hcZ)2cP{GUC)je&v!|4~=FRgG%}Y;4ZHS@Ul< zYm!@we(n6lf8G3Z^8c?PQBW1|aykw<)F4Rnht(Ibl{X8d^=s!Z{_EzSlmE%LfWiR2 zEWZH&{yqHdCV}k|P?|ru)Uvpoj&2OI)vuku_^+FPZvGD{0AUb7BO$B_kmi@}0AUP( zuKY{$7yoth&&mIkKNggJL z3)1}39Y9*YcK+hOZvMIXpHcu8W|AQ!Grv2N5FC>r()^Z7r~yjr*UoRbl=_bZeilgc z8!o8=#GyawDIoKLy4OXJv(3Us@UwHjeg0*4v=}f;{gM2vF%-Q!#yZEc!itAbU zsP@Oi-{2PTpGE(m0uTw-MkvWIGyqOUzjl6w{zq{$u16ECox1=4vIVFQsr;XYM%42h z?5vUFQnO&XDE`xU`}&hyrXUw`aZlC%A^Hce1>_F+62@1%mAB>Qc4Khg%Nc`|{(=G* zVH>=?=3Eqwgw#psJ?l@J1O~bXI%gl}lBiMzsOabU?7cv{{>fr01F#hSM1S%XfEjoQ zLhcGYi2+!Ia%|@A{J_fiI^c9-m+A8`Qw(e1>fjtmg|EIJg0M{WgvW z{T%!a{S8MZ3T#dUs^xue+O8KpMjA7Ktusx@;{2(K{nl)2H?&Z2+U4@eK{TNhU>ye zHG*%X2|vThPi;)xB!6c8HinQh6ThT>@3=8J7?GQd0e9d(M;nOX;=J$PsXf@(`RAfP zW)8qR`+9tS!FIF8`|BR(!}Xb1vzh&XJrfxtA>P@SLD#}dBXWEELi)WJ%+J1sr$**) z;Qe)PS8&G*bqWh}irvGNd802blk_x`%aKmT=~fscF-a?v)Zg#%D< z20j|Mr2g;<#D%Nhe8$8*7C(X_44_j#hxhKWsRG2@0UP?e!2A$^hqeNoBL>4~PDr@X zH~8NA0PB@s$PeVxkDH%8c}UVfcqxeU;W`Ky;ZMsyI(AYFh8uvF_@`yQNwCcNz1vXX z0BCTFfwR+lIdB_(pr7>#7^DCDkG*^8&+We#1Aqp%1GY(M*MCYsV+@Tr0J06RJ^dC= zvQqfqaH{wab#!%^aPmm9ydHrDi-EAr1W=j=Fn~9n3DhJ1MCLANnnrP zXbri!dBaIyNxTny1aq(Zm{UUa<2h1xv> z%EamMJFg}5H)09$!JdETwV3}s>#=0J3Yr)Av zs{bDUV^V6coia4#M3dH7NaTbnM!AdMB4Pj7m>OU1cnS&%3JMAe3JMAe?hgM0-84fw Tz-sHZ00000NkvXXu0mjf9_AIz literal 0 HcmV?d00001 diff --git a/box/box_engine.c b/box/box_engine.c index 0c456bf..8d5cd30 100644 --- a/box/box_engine.c +++ b/box/box_engine.c @@ -146,8 +146,10 @@ static inline void execLoadRootNode() { //immediately call onLoad() after running the script - for loading other nodes Box_callEngineNode(engine.rootNode, &inner, "onLoad", NULL); + //cache the scope for later freeing + engine.rootNode->scope = inner.scope; + //manual cleanup - Toy_popScope(inner.scope); Toy_freeLiteralArray(&inner.stack); Toy_freeLiteralArray(&inner.literalCache); @@ -207,7 +209,7 @@ static inline void execEvents() { //call the function Toy_pushLiteralArray(&args, eventLiteral); Box_callRecursiveEngineNode(engine.rootNode, &engine.interpreter, "onKeyDown", &args); - Toy_popLiteralArray(&args); + Toy_freeLiteral(Toy_popLiteralArray(&args)); //push to the event list Toy_freeLiteral(eventLiteral); @@ -234,7 +236,7 @@ static inline void execEvents() { //call the function Toy_pushLiteralArray(&args, eventLiteral); Box_callRecursiveEngineNode(engine.rootNode, &engine.interpreter, "onKeyUp", &args); - Toy_popLiteralArray(&args); + Toy_freeLiteral(Toy_popLiteralArray(&args)); //push to the event list Toy_freeLiteral(eventLiteral); diff --git a/box/lib_engine.c b/box/lib_engine.c index b2903af..e1e4cb0 100644 --- a/box/lib_engine.c +++ b/box/lib_engine.c @@ -86,7 +86,7 @@ static int nativeInitWindow(Toy_Interpreter* interpreter, Toy_LiteralArray* argu printf("Renderer: %s (HW %s)\n", rendererInfo.name, rendererInfo.flags & SDL_RENDERER_ACCELERATED ? "yes" : "no"); - SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best"); + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0"); SDL_RenderSetLogicalSize(engine.renderer, engine.screenWidth, engine.screenHeight); //only run with a window