mirror of
https://github.com/ctruLua/ctruLua.git
synced 2025-10-27 16:39:29 +00:00
Added a sprite library (unfinished, but working)
This commit is contained in:
parent
6a26be4b43
commit
a4a6e09d74
2 changed files with 82 additions and 0 deletions
72
sdcard/3ds/ctruLua/libs/sprite.lua
Normal file
72
sdcard/3ds/ctruLua/libs/sprite.lua
Normal file
|
|
@ -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
|
||||
|
|
@ -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 },
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue