1
0
Fork 0
mirror of https://github.com/ctruLua/uCompat.git synced 2025-10-27 16:49:31 +00:00

Added Image, Motion and Sprite libraries

This commit is contained in:
Firew0lf 2015-10-04 19:06:45 +02:00
parent 6d3ec78c8c
commit 0166ba6bce
6 changed files with 307 additions and 2 deletions

68
Image.lua Normal file
View file

@ -0,0 +1,68 @@
--[[
Images related µLua compatibility layer/lib for ctrµLua
Actually doesn't support GIFs, because sfillib doesn't. Use PNGs, these are
better.
]]
-- Local
local texture = require("ctr.gfx.texture")
-- Constants
RAM = texture.PLACE_RAM
VRAM = texture.PLACE_VRAM
-- Module
Image = {}
function Image.load(path, dest)
local t = texture.load(path, dest)
if not t then return nil end
return { -- Image object
texture = t,
rotation = 0
}
end
function Image.destroy(img)
img.texture:unload()
img = nil
end
function Image.width(img)
local x,y = img.texture:getSize()
return x
end
function Image.height(img)
local x,y = img.texture:getSize()
return y
end
function Image.scale(img, w, h)
end
function Image.rotate(img, angle, cx, cy)
img.rotation = angle*((math.pi*2)/512)
end
function Image.rotateDegree(img, angle, cx, cy)
img.rotation = angle*((math.pi*2)/360)
end
function Image.mirrorH(img, activate)
end
function Image.mirrorV(img. activate)
end
function Image.setTint(img, color)
img.texture:setBlendColor(color*256)
end

63
Motion.lua Normal file
View file

@ -0,0 +1,63 @@
--[[
Motion related µLua compatibility layer/lib for ctrµLua
Nobody actually used this module. So please use directly the ctrµLua functions.
]]
-- Local
local hid = require("hid")
calX, calY, calZ = 0,0,0 -- for Motion.calibrate
-- Module
Motion = {}
function Motion.init()
return true
end
function Motion.calibrate()
hid.read()
calX, calY, calZ = hid.gyro()
end
function Motion.readX()
local x,y,z = hid.gyro()
return (calX-x)
end
function Motion.readY()
local x,y,z = hid.gyro()
return (calY-y)
end
function Motion.readZ()
local x,y,z = hid.gyro()
return (calZ-z)
end
function Motion.accelerationX()
local x,y,z = hid.accel()
return x
end
function Motion.accelerationY()
local x,y,z = hid.accel()
return y
end
function Motion.accelerationZ()
local x,y,z = hid.accel()
return z
end
function Motion.readGyro()
return (Motion.readX()*Motion.readY()*Motion.readZ())
end
function Motion.rotation()
return (Motion.accelerationX()*Motion.accelerationY()*Motion.accelerationZ())
end

View file

@ -7,6 +7,9 @@ Actually done:
* Color
* Controls
* dsUser
* Image
* Motion
* Rumble
* screen
* Sprite
* Timer

165
Sprite.lua Normal file
View file

@ -0,0 +1,165 @@
--[[
Sprites related µLua compatibility layer/lib for ctrµLua
The code comes directly from the "libs.lua" file of the original µLua.
]]
-- Module
require("uCompat.Image")
require("uCompat.screen")
Sprite = {
-- graph: path of the image which contains the sprite, or a image object
-- height: height of the frames
-- width: width of the frames
-- dest: destination (RAM or VRAM)
new = function(graph, width, height, dest)
assert(graph ~= nil, "Graph can't be null")
assert(width > 0, "Width must be positive")
assert(height > 0, "Height must be positive")
local img
local animations = {}
if type(graph) == "string" then
assert(dest == RAM or dest == VRAM, "Destination must be RAM or VRAM")
img = Image.load(graph, dest)
assert(img, "Image not found: "..graph)
elseif type(graph) == "userdata" then
img = graph
else
error("Bad graph type")
end
-- ### Public methods ###
-- Draw a frame
-- scr: screen (SCREEN_UP or SCREEN_DOWN)
-- x: X-coordinate where to draw the frame
-- y: Y-coordinate where to draw the frame
-- noFrame: number of the frame to draw
local drawFrame = function(self, scr, x, y, noFrame)
assert(scr == SCREEN_UP or scr == SCREEN_DOWN, "Bad screen number")
assert(x ~= nil, "X can't be null")
assert(y ~= nil, "Y can't be null")
assert(noFrame ~= nil, "Frame number can't be null")
local boardWidth = Image.width(img) / width
local yy = math.floor(noFrame / boardWidth)
local xx = noFrame - (yy * boardWidth)
screen.blit(scr, x, y, img, xx*width, yy*height, width, height)
end
-- Create an animation
-- tabAnim: the table of the animation frames
-- delay: delay between each frame
local addAnimation = function(self, tabAnim, delay)
assert(tabAnim ~= nil, "Table can't be null")
assert(delay >= 0, "Delay must be positive")
table.insert(animations, SpriteAnimation.new(tabAnim, delay))
end
-- Reset an animation
-- noAnim: number of the animation
local resetAnimation = function(self, noAnim)
assert(noAnim > 0, "Animation number must be 1 or more")
animations[noAnim].tmr:reset()
end
-- Start an animation
-- noAnim: number of the animation
local startAnimation = function(self, noAnim)
assert(noAnim > 0, "Animation number must be 1 or more")
animations[noAnim].tmr:start()
end
-- Stop an animation
-- noAnim: number of the animation
local stopAnimation = function(self, noAnim)
assert(noAnim > 0, "Animation number must be 1 or more")
animations[noAnim].tmr:stop()
end
-- Return true if the animation is at the end of a loop
-- noAnim: number of the animation
local isAnimationAtEnd = function(self, noAnim)
assert(noAnim > 0, "Animation number must be 1 or more")
return math.floor(animations[noAnim].tmr:getTime()/animations[noAnim].delay+1) >= #(animations[noAnim].tabAnim)
end
-- Play an animation
-- scr: screen (SCREEN_UP or SCREEN_DOWN)
-- x: X-coordinate where to draw the animation
-- y: Y-coordinate where to draw the animation
-- noAnim: number of the animation to draw
local playAnimation = function(self, scr, x, y, noAnim)
assert(scr == SCREEN_UP or scr == SCREEN_DOWN, "Bad screen number")
assert(x ~= nil, "X can't be null")
assert(y ~= nil, "Y can't be null")
assert(noAnim > 0, "Animation number must be 1 or more")
if not animations[noAnim].isPlayed then
animations[noAnim].tmr:reset()
animations[noAnim].tmr:start()
animations[noAnim].isPlayed = true
end
if math.floor(animations[noAnim].tmr:getTime()/animations[noAnim].delay) >= #(animations[noAnim].tabAnim) then
resetAnimation(self, noAnim)
startAnimation(self, noAnim)
end
local animToDraw = animations[noAnim].tabAnim[math.floor(animations[noAnim].tmr:getTime()/animations[noAnim].delay)+1]
if animToDraw ~= nil then
drawFrame(self, scr, x, y, animToDraw)
end
end
local getWidth = function(self)
return width
end
local getHeight = function(self)
return height
end
local destroy = function(self)
for _, value in pairs(animations) do
value = nil
end
Image.destroy(img)
img = nil
end
-- ### Returns ###
return {
getWidth = getWidth,
getHeight = getHeight,
drawFrame = drawFrame,
addAnimation = addAnimation,
playAnimation = playAnimation,
resetAnimation = resetAnimation,
stopAnimation = stopAnimation,
startAnimation = startAnimation,
isAnimationAtEnd = isAnimationAtEnd,
destroy = destroy
}
end
}
-- Declaration of the SpriteAnimation class
SpriteAnimation = {
-- tabAnim: the table of the animation frames
-- delay: delay between each frame
new = function(tabAnim, delay)
return {
tabAnim = tabAnim,
delay = delay,
tmr = Timer.new(),
isPlayed = false
}
end
}

View file

@ -102,7 +102,7 @@ end
function screen.blit(scr, x, y, img, sx, sy, w, h)
local sizex, sizey = img:getSize()
videoStack[scr][#videoStack[scr]] = {"img", {offsetX+x, offsetY+y, (sx or 0), (sy or 0), (w or sizex), (h or sizey)}}
videoStack[scr][#videoStack[scr]] = {"img", img.texture, {offsetX+x, offsetY+y, (sx or 0), (sy or 0), (w or sizex), (h or sizey), img.rotation}}
end
function screen.drawPoint(scr, x, y, color)
@ -171,7 +171,7 @@ function screen.endDrawing()
for i=1, #videoStack[drawScreen] do
local e = videoStack[drawScreen][i]
if e[1] == "img" then
e[2]:drawPart(unpack(e[3]))
else
gfx[e[1]](unpack(e[2])
end

View file

@ -15,4 +15,10 @@ ULUA_BOOT_FULLPATH = (ULUA_DIR..ULUA_BOOT_FILE)
-- Other libs
require("uCompat.screen")
require("uCompat.Color")
require("uCompat.Image")
require("uCompat.Timer")
require("uCompat.Sprite")
require("uCompat.Controls")
require("uCompat.Motion")
require("uCompat.Rumble")
require("uCompat.dsUser")