From 8ab6181928d9369dd8f20f9500a7298b8c273a7c Mon Sep 17 00:00:00 2001 From: Firew0lf Date: Thu, 31 Mar 2016 21:53:19 +0200 Subject: [PATCH] Fixed many things, Added overclock on new3DS (can be disabled), Rewrote the README --- README.md | 31 +++++++++++++++++- uCompat/Canvas.lua | 6 ++-- uCompat/Image.lua | 8 ++++- uCompat/Map.lua | 13 +++++--- uCompat/ScrollMap.lua | 6 ++-- uCompat/System.lua | 76 +++++++++++++++++++++---------------------- uCompat/init.lua | 6 ++++ uCompat/screen.lua | 5 +-- 8 files changed, 98 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index d129834..9408e26 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,30 @@ # uCompat µLua compatibility layer for ctrµLua. -Actually done: +### What is that ? + +_uCompat_ is a ctrµLua "library" that allows you to launch and use µLua homebrews +on your 3DS. + +### How does it work ? + +This library recreate the µLua execution environment in ctrµLua using only pure +Lua. All the functions were recreated using the [official µLua documentation](https://sourceforge.net/p/microlua/wiki/API471/), +and the library is designed to run any µLua program from 3.0 to 4.7.2. + +### How do I use it ? + +Just download the latest version by clicking the "Download ZIP" button (somewhere +on the page) or with a `git clone`, and place the `uCompat` directory in your +ctrµLua `libs` folder (the __folder__, not the files in the folder). +Once it's done, put your µLua homebrews in a directory somewhere, it doesn't +mater where, just remember it. +Finally, open your homebrews' main files (`index.lua` on a lot of them) and add +the line `require("uCompat")` at the beggining of the file, before any other +line of code. Now, it should work. + +### What's done ? + * Canvas * Color * Controls @@ -22,3 +45,9 @@ Actually done: * System * Timer * Wifi + +### What's not ? + + * Soundbanks decoding + * `screen.drawGradientRect()`: very ugly render ... + * 3DS <-> DS Nifi diff --git a/uCompat/Canvas.lua b/uCompat/Canvas.lua index e1d29d6..5ddbd63 100644 --- a/uCompat/Canvas.lua +++ b/uCompat/Canvas.lua @@ -41,7 +41,7 @@ local function attrTable() [ATTR_COLOR3] = false, [ATTR_COLOR4] = false, [ATTR_TEXT] = false, - [ATTR_VISIBLE] = false, + [ATTR_VISIBLE] = true, [ATTR_FONT] = false, [ATTR_IMAGE] = false } @@ -186,7 +186,9 @@ end function Canvas.draw(scr, canvas, x, y) for i=1, #canvas do local o = canvas[i] -- gotta go fast - if o.type == TYPE_LINE then + if not o[ATTR_VISIBLE] then + -- Invisible + elseif o.type == TYPE_LINE then screen.drawLine(scr, o[ATTR_X1]+x, o[ATTR_Y1]+y, o[ATTR_X2]+x, o[ATTR_Y2]+y, o[ATTR_COLOR]) elseif o.type == TYPE_POINT then screen.drawPoint(scr, o[ATTR_X1]+x, o[ATTR_Y1]+y, o[ATTR_COLOR]) diff --git a/uCompat/Image.lua b/uCompat/Image.lua index 1c47227..ff3294e 100644 --- a/uCompat/Image.lua +++ b/uCompat/Image.lua @@ -6,6 +6,7 @@ local texture = require("ctr.gfx.texture") local color = require("ctr.gfx.color") +require("uCompat.System") -- Constants @@ -17,9 +18,14 @@ VRAM = texture.PLACE_VRAM -- has to be "PLACE_RAM" on hardware Image = {} function Image.load(path, dest) - local t = texture.load(path, dest) + local t = texture.load(System.fixPath(path), dest) if not t then return nil end local w,h = t:getSize() + for x=0, w do + for y=0, h do + if t:getPixel(x,y) == 0xff00ffff then t:setPixel(x, y, 0) end + end + end return { -- Image object texture = t, rotation = 0.0, diff --git a/uCompat/Map.lua b/uCompat/Map.lua index 99c01ab..98d9cbd 100644 --- a/uCompat/Map.lua +++ b/uCompat/Map.lua @@ -4,13 +4,15 @@ -- Local -map = require("ctr.gfx.map") +local map = require("ctr.gfx.map") --- Interface +require("uCompat.System") + +-- Module Map = {} function Map.mapToTable(filename, w, h) - local f = io.open(filename, "r") + local f = assert(io.open(filename, "r")) local data = f:read("a") f:close() @@ -38,7 +40,7 @@ end require("uCompat.screen") function Map.new(image, mapFile, width, height, tileWidth, tileHeight) - local tiles = mapToTable(mapFile, width, height) + local tiles = mapToTable(System.fixPath(mapFile), width, height) local m = map.load(tiles, image.texture, tileWidth, tileHeight) return { @@ -51,7 +53,8 @@ function Map.new(image, mapFile, width, height, tileWidth, tileHeight) end function Map.destroy(m) - m.map:unload() + -- This `if` should not exist. + if m then m.map:unload() end -- got some crashes here ... We should ban global values in Lua. end function Map.draw(scr, m, x, y, w, h) diff --git a/uCompat/ScrollMap.lua b/uCompat/ScrollMap.lua index 5f91a99..f48dc84 100644 --- a/uCompat/ScrollMap.lua +++ b/uCompat/ScrollMap.lua @@ -4,7 +4,7 @@ -- Local -map = require("ctr.gfx.map") +local map = require("ctr.gfx.map") -- Module @@ -14,7 +14,7 @@ require("uCompat.Map") ScrollMap = {} function ScrollMap.new(image, mapFile, width, height, tileWidth, tileHeight) - local tiles = Map.mapToTable(mapFile, width, height) + local tiles = Map.mapToTable(System.fixPath(mapFile), width, height) local m = map.load(tiles, image.texture, tileWidth, tileHeight) return { map = m, @@ -26,7 +26,7 @@ function ScrollMap.new(image, mapFile, width, height, tileWidth, tileHeight) end function ScrollMap.destroy(m) - m.map:destroy() + m.map:unload() end function ScrollMap.draw(scr, m) diff --git a/uCompat/System.lua b/uCompat/System.lua index 32d7604..b5f99f0 100644 --- a/uCompat/System.lua +++ b/uCompat/System.lua @@ -9,42 +9,6 @@ local fs = require("ctr.fs") local gfx = require("ctr.gfx") --- µ -> ctrµ -local function fixPath(DSpath) - local path - if DSpath:sub(1, 5) == "fat:/" then -- fix root - path = ("sdmc:/"..DSpath:sub(6, -1)) - elseif DSpath:sub(1, 1) == "/" then - path = ("sdmc:"..DSpath) - elseif DSpath:sub(1, 5) == "efs:/" then - path = ("romfs:/"..DSpath:sub(6,-1)) - elseif DSpath:sub(1, 2) ~= "./" then - path = (fs.getDirectory()..DSpath) - else - path = DSpath - end - - if path:sub(-1,-1) ~= "/" then - path = (path.."/") - end - - return path -end - --- ctrµ -> µ -local function unfixPath(path) - local DSpath - if path:sub(1, 6) == "sdmc:/" then - DSpath = ("fat:/"..path:sub(7, -1)) - elseif path.sub(1, 1) == "/" then - DSpath = ("fat:"..path) - elseif path:sub(1, 7) == "romfs:/" then - DSpath = ("efs:/"..path:sub(8, -1)) - end - - return DSpath -end - -- Constants LED_ON = 0 @@ -58,11 +22,11 @@ System = {} System.EFS = false function System.currentDirectory() - return unfixPath(fs.getDirectory()) + return System.unfixPath(fs.getDirectory()) end function System.changeDirectory(dir) - fs.setDirectory(fixPath(dir)) + fs.setDirectory(System.fixPath(dir)) end function System.remove(path) @@ -79,7 +43,7 @@ end function System.listDirectory(path) - local list = fs.list(fixPath(path)) + local list = fs.list(System.fixPath(path)) local flist = {} for i=1, #list do flist[i] = { @@ -119,3 +83,37 @@ end function System.sleep() end + +-- Small API + +-- µ -> ctrµ +function System.fixPath(DSpath) + local path + if DSpath:sub(1, 5) == "fat:/" then -- fix root + path = ("sdmc:/"..DSpath:sub(6, -1)) + elseif DSpath:sub(1, 1) == "/" then + path = ("sdmc:"..DSpath) + elseif DSpath:sub(1, 5) == "efs:/" then + path = ("romfs:/"..DSpath:sub(6,-1)) + elseif DSpath:sub(1, 2) ~= "./" then + path = (fs.getDirectory()..DSpath) + else + path = DSpath + end + + return path +end + +-- ctrµ -> µ +function System.unfixPath(path) + local DSpath + if path:sub(1, 6) == "sdmc:/" then + DSpath = ("fat:/"..path:sub(7, -1)) + elseif path.sub(1, 1) == "/" then + DSpath = ("fat:"..path) + elseif path:sub(1, 7) == "romfs:/" then + DSpath = ("efs:/"..path:sub(8, -1)) + end + + return DSpath +end diff --git a/uCompat/init.lua b/uCompat/init.lua index 194bafd..299f30e 100644 --- a/uCompat/init.lua +++ b/uCompat/init.lua @@ -7,8 +7,14 @@ -- Just something needed local ctr = require("ctr") +local ptm = require("ctr.ptm") + +-- Setup the new3DS overclock, uncomment if you need more speed +ptm.init() +ptm.configureNew3DSCPU(true) -- Constants +MICROLUA_VERSION = "Microlua 4.1" -- such old, very version ULUA_VERSION = "Microlua 4.7.2" ULUA_DIR = ctr.root -- you can change it to anything ULUA_SCRIPTS = (ULUA_DIR.."scripts/") -- Warning: you may need to create the folder diff --git a/uCompat/screen.lua b/uCompat/screen.lua index 464b2ec..233c9e9 100644 --- a/uCompat/screen.lua +++ b/uCompat/screen.lua @@ -72,7 +72,7 @@ function stopDrawing() if drawScreen == 0 then fpscount = fpscount + 1 end - if (ctr.time() - fpstime) > 1000 then + if (ctr.time() - fpstime) >= 1000 then NB_FPS = fpscount fpstime = ctr.time() fpscount = 0 @@ -205,7 +205,8 @@ function screen.init() -- unused end function screen.startDrawing2D() -- unused - --reset the video stacks + ctr.run() + --reset the video stacks videoStack[drawScreen] = {} -- As you can change the screen size, we have to re-calculate this every time.