diff --git a/README.md b/README.md index 4fc65cb..31932cc 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ Actually done: * Font * Image * ini + * Map * Motion * Rumble * screen diff --git a/uCompat/Color.lua b/uCompat/Color.lua index f98e2bd..379c48a 100644 --- a/uCompat/Color.lua +++ b/uCompat/Color.lua @@ -2,15 +2,17 @@ Colors related µLua compatibility layer/lib for ctrµLua ]] +-- Module + Color = {} function Color.new(r, g, b) - r = r*8 - g = g*8 - b = b*8 - return (r+g*256+b*65536) + return (r+g*32+b*1024) end function Color.new256(r, g, b) - return (r+g*256+b*65536) + r = math.floor(r*31/255) + g = math.floor(g*31/255) + b = math.floor(b*31/255) + return (r+g*32+b*1024) end diff --git a/uCompat/Controls.lua b/uCompat/Controls.lua index baf129f..17d7ea9 100644 --- a/uCompat/Controls.lua +++ b/uCompat/Controls.lua @@ -44,6 +44,7 @@ function Controls.read() Stylus.X, Stylus.Y = hid.touch() local offsetX, offsetY = screen.offset() + if screen.getMainLcd() then offsetX = (offsetX-40) end Stylus.X = (Stylus.X - offsetX) if Stylus.X < 0 or Stylus.X > 255 then Stylus.X = stylusX end Stylus.Y = (Stylus.Y - offsetY) diff --git a/uCompat/Map.lua b/uCompat/Map.lua new file mode 100644 index 0000000..0f7976d --- /dev/null +++ b/uCompat/Map.lua @@ -0,0 +1,53 @@ +--[[ + Maps related µLua compatibility layer/lib for ctrµLua +]] + +-- Local + +map = require("ctr.gfx.map") + +-- Module + +require("uCompat.screen") + +Map = {} + +function Map.new(image, mapFile, width, height, tileWidth, tileHeight) + local m = map.load(mapFile, image.texture, tileWidth, tileHeight) + + return { + map = m, + scrollX = 0, + scrollY = 0, + tileWidth = tileWidth, + tileHeight = tileHeight, + } +end + +function Map.destroy(m) + m.map:unload() +end + +function Map.draw(scr, m, x, y, w, h) + local stack = screen.getStack(scr) + local offsetX, offsetY = screen.offset() + + stack[#stack+1] = {"map", m, {offsetX+(x*math.floor(m.scrollX*m.tileWidth)), offsetY+(y*math.floor(m.scrollY*m.tileHeight))}} +end + +function Map.scroll(m, x, y) + m.scrollX = x + m.scrollY = y +end + +function Map.space(m, x, y) + m.map:setSpace(x, y) +end + +function Map.setTile(m, x, y, t) + m.map:setTile(x, y, t) +end + +function Map.getTile(m, x, y) + return m.map:getTile(x, y) +end diff --git a/uCompat/init.lua b/uCompat/init.lua index d4bdce0..efe0c9f 100644 --- a/uCompat/init.lua +++ b/uCompat/init.lua @@ -17,6 +17,7 @@ require("uCompat.screen") require("uCompat.Color") require("uCompat.Font") require("uCompat.Image") +require("uCompat.Map") require("uCompat.Timer") require("uCompat.Sprite") require("uCompat.Controls") diff --git a/uCompat/screen.lua b/uCompat/screen.lua index 45825ef..02a1654 100644 --- a/uCompat/screen.lua +++ b/uCompat/screen.lua @@ -31,6 +31,7 @@ NB_FPS = 0 local ctr = require("ctr") local gfx = require("ctr.gfx") +local color = require("ctr.gfx.color") -- As the µLua and ctrµLua drawing systems are very differents, we have to use a -- stack. That's bad, but it's the only solution. @@ -45,8 +46,11 @@ local fpscount = 0 local fpstime = ctr.time() local function RGB2RGBA(c) - if not c then return nil end - return (c*256)+math.floor(alpha*2.55) + if not c then return color.getDefault() end + local r = (c%32)*8 + local g = math.floor(c%1024/32)*8 + local b = math.floor(c/1024)*8 + return color.RGBA8(r, g, b, math.floor(alpha*2.55)) end local function checkBuffer(scr) @@ -131,7 +135,7 @@ function screen.drawRect(scr, x0, y0, x1, y1, color) end function screen.drawFillRect(scr, x0, y0, x1, y1, color) - checkBuffer(scr)[#videoStack[scr]+1] = {"rectangle", {offsetX+x0, offsetY+y0, offsetX+(x1-x0), offsetY+(y1-y0), 0, RGB2RGBA(color)}} + checkBuffer(scr)[#videoStack[scr]+1] = {"rectangle", {offsetX+x0, offsetY+y0, (x1-x0), (y1-y0), 0, RGB2RGBA(color)}} end function screen.drawGradientRect(scr, x0, y0, x1, y1, color, color, color, color) @@ -169,7 +173,7 @@ function screen.startDrawing2D() -- unused -- As you can change the screen size, we have to re-calculate this every time. offsetX = (gfx.BOTTOM_WIDTH-SCREEN_WIDTH)/2 offsetY = (gfx.BOTTOM_HEIGHT-SCREEN_HEIGHT)/2 - if drawScreen == 0 then + if drawScreen == gfx.GFX_TOP then offsetX = offsetX + 40 end end @@ -182,6 +186,8 @@ function screen.endDrawing() local e = videoStack[drawScreen][i] if e[1] == "img" then e[2]:drawPart(table.unpack(e[3])) + elseif e[1] == "map" then + e[2]:draw(table.unpack(e[3])) else gfx[e[1]](table.unpack(e[2])) end @@ -202,6 +208,10 @@ function screen.offset() return offsetX, offsetY end +function screen.getStack(scr) + return checkBuffer(scr) +end + -- Initialize the thing startDrawing()