diff --git a/sdcard/3ds/ctruLua/libs/sprite.lua b/sdcard/3ds/ctruLua/libs/sprite.lua new file mode 100644 index 0000000..a731e41 --- /dev/null +++ b/sdcard/3ds/ctruLua/libs/sprite.lua @@ -0,0 +1,72 @@ +local gfx = require("ctr.gfx") +local ctr = require("ctr") +local mod = {} + +-- Module functions +local function getBox(tx, ty, i, sx, sy) + --i = i - 1 + x = (i%tx) + y = math.floor(i/tx) + + return (x*sx), (y*sy) +end + +-- Sprite object methods +local function draw(self, x, y, rad) + if not self.animations[self.currentAnimation] then return 0 end + local frame = self.animations[self.currentAnimation].animation[self.currentFrame] + local tsx, tsy = self.texture:getSize() + + local sx, sy = getBox(tsx, tsy, frame, self.frameSizeX, self.frameSizeY) + gfx.text(200, 200, sx.."/"..sy) + self.texture:drawPart(x, y, sx, sy, self.frameSizeX, self.frameSizeY, rad) + + if (ctr.time()-self.frameTimer) >= self.animations[self.currentAnimation].delay then + self.currentFrame = (self.currentFrame+1) + self.frameTimer = ctr.time() + if self.currentFrame > #self.animations[self.currentAnimation].animation then + self.currentFrame = 1 + end + end + + return frame +end + +local function addAnimation(self, anim, delay) + self.animations[#self.animations+1] = {animation=anim, delay=delay} + return #self.animations +end + +-- Set to 0 to hide the sprite +local function setAnimation(self, anim) + self.currentAnimation = anim + self.currentFrame = 1 + if not self.animations[anim] then + return false + end + return true +end + +local function resetTimer(self) + self.frameTimer = ctr.time() +end + +-- Sprite object constructor +function mod.new(texture, fsx, fsy) + return { + texture = texture, + frameSizeX = fsx, + frameSizeY = fsy, + animations = {}, + currentAnimation = 0, + currentFrame = 1, + frameTimer = 0, + + draw = draw, + addAnimation = addAnimation, + setAnimation = setAnimation, + resetTimer = resetTimer, + } +end + +return mod diff --git a/source/texture.c b/source/texture.c index dfadb5e..2581445 100644 --- a/source/texture.c +++ b/source/texture.c @@ -81,6 +81,15 @@ static int texture_drawPart(lua_State *L) { return 0; } +static int texture_getSize(lua_State *L) { + texture_userdata *texture = luaL_checkudata(L, 1, "LTexture"); + + lua_pushinteger(L, texture->texture->width); + lua_pushinteger(L, texture->texture->height); + + return 2; +} + static int texture_unload(lua_State *L) { texture_userdata *texture = luaL_checkudata(L, 1, "LTexture"); @@ -138,6 +147,7 @@ static const struct luaL_Reg texture_methods[] = { { "draw", texture_draw }, { "drawPart", texture_drawPart }, { "scale", texture_scale }, + { "getSize", texture_getSize }, { "unload", texture_unload }, { "getPixel", texture_getPixel }, { "setPixel", texture_setPixel },