From 7844e5a117d23ededfff98487c82cb596bbbf7b4 Mon Sep 17 00:00:00 2001 From: Firew0lf Date: Sun, 4 Oct 2015 00:36:08 +0200 Subject: [PATCH] Initial untested code. --- Color.lua | 16 +++++ screen.lua | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++++ uCompat.lua | 16 +++++ 3 files changed, 215 insertions(+) create mode 100644 Color.lua create mode 100644 screen.lua create mode 100644 uCompat.lua diff --git a/Color.lua b/Color.lua new file mode 100644 index 0000000..f98e2bd --- /dev/null +++ b/Color.lua @@ -0,0 +1,16 @@ +--[[ + Colors related µLua compatibility layer/lib for ctrµLua +]] + +Color = {} + +function Color.new(r, g, b) + r = r*8 + g = g*8 + b = b*8 + return (r+g*256+b*65536) +end + +function Color.new256(r, g, b) + return (r+g*256+b*65536) +end diff --git a/screen.lua b/screen.lua new file mode 100644 index 0000000..3618fd0 --- /dev/null +++ b/screen.lua @@ -0,0 +1,183 @@ +--[[ + Screen related µLua compatibility layer/lib for ctrµLua + + Warning: as µLua API was created with the ass, all functions are declared as +globals. Keep this in mind. + Also, not all the functions are implemented, it won't trigger errors but just +won't draw some things. + The screen size is a real problem when you write something like this, so by +default the library uses NDS screen size, but you can change it to the 3DS size. + Finally, µLua draw 1 screen each frame at 60 FPS, so to avoid graphical bugs +I had to reproduce this. That's bad, but I want something as close to the +original µLua as possible. + + µLua have ~4MB of RAM available to the user, so using some more bytes for this +library shouldn't be a problem :). Same thing for the CPU. +]] + +-- Constants + +SCREEN_WIDTH = 256 +SCREEN_HEIGHT = 192 +--SCREEN_WIDTH = 320 -- uncomment for a larger screen +--SCREEN_HEIGHT = 240 -- idem +SCREEN_UP = 0 +SCREEN_DOWN = 1 +ALPHA_RESET = 100 + +NB_FPS = 0 + +-- Local + +local ctr = require("ctr") +local gfx = require("ctr.gfx") + +-- 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. +local videoStack = {} + +local alpha = ALPHA_RESET + +local drawScreen = SCREEN_UP + +local fpscount = 0 +local fpstime = ctr.time() + +local function RGB2RGBA(c) + return (c*256)+alpha) +end + +-- Module +function startDrawing() + screen.startDrawing2D() +end + +function stopDrawing() + -- As you can change the screen size, we have to re-calculate this every time. + local offsetX = (gfx.BOTTOM_WIDTH-SCREEN_WIDTH)/2 + local offsetY = (gfx.BOTTOM_HEIGHT-SCREEN_HEIGHT)/2 + if drawScreen == gfx.SCREEN_UP then + offsetX = offsetX + 40 + end + + -- FPS counter + fpscount = fpscount + 1 + if (ctr.time() - fpstime) > 1000 then + NB_FPS = fpscount + fpscount = 0 + end + + -- render + screen.endDrawing() + + -- set the screen + drawScreen = ((drawScreen == 0 and 1) or 0) +end + +function render() + startDrawing() + stopDrawing() +end + +screen = {} + +function screen.switch() + SCREEN_UP, SCREEN_DOWN = SCREEN_DOWN, SCREEN_UP +end + +function screen.getLayer() + return #videoStack[drawScreen] +end + +function screen.getAlphaLevel() + return ALPHA_RESET +end + +function screen.setAlpha(level, layer) + alpha = level +end + +function screen.print(scr, x, y, text, color) + videoStack[scr][#videoStack[scr]] = {"text", {x, y, text, 8, RGB2RGBA(color), nil}} +end + +function screen.printFont(scr, x, y, text, color, font) + videoStack[scr][#videoStack[scr]] = {"text", {x, y, text, 8, RGB2RGBA(color), font}} +end + +function screen.blit(scr, x, y, img, sx, sy, w, h) + local sizex, sizey = img:getSize() + videoStack[scr][#videoStack[scr]] = {"img", {x, y, (sx or 0), (sy or 0), (w or sizex), (h or sizey)}} +end + +function screen.drawPoint(scr, x, y, color) + videoStack[scr][#videoStack[scr]] = {"point", {x, y, RGB2RGBA(color)}} +end + +function screen.drawLine(scr, x0, y0, x1, y1, color) + videoStack[scr][#videoStack[scr]] = {"line", {x0, y0, x1, y1, RGB2RGBA(color)}} +end + +function screen.drawRect(scr, x0, y0, x1, y1, color) + videoStack[scr][#videoStack[scr]] = {"line", {x0, y0, x0, y1, RGB2RGBA(color)}} + videoStack[scr][#videoStack[scr]] = {"line", {x0, y0, x1, y0, RGB2RGBA(color)}} + videoStack[scr][#videoStack[scr]] = {"line", {x0, y1, x1, y1, RGB2RGBA(color)}} + videoStack[scr][#videoStack[scr]] = {"line", {x1, y0, x1, y1, RGB2RGBA(color)}} +end + +function screen.drawFillRect(scr, x0, y0, x1, y1, color) + videoStack[scr][#videoStack[scr]] = {"rectangle", {x0, y0, (x1-x0), (y1-y0), 0, RGB2RGBA(color)}} +end + +function screen.drawGradientRect(scr, x0, y0, x1, y1, color, color, color, color) + +end + +function screen.drawTextBox(scr, x0, y0, x1, y1, text, color) + +end + +function screen.drawTexturedQuad(scr, x0, y0, x1, y1, x2, y2, x3, y3, img, sx, sy, w, h) + +end + +function screen.drawTexturedTriangle(scr, x0, y0, x1, y1, x2, y2, img, sx, sy, w, h) + +end + +function screen.getMainLcd() + return (drawScreen == SCREEN_UP) +end + +function screen.setSpaceBetweenScreens(space) -- deprecated in µLua 4.7.1 + +end + +function screen.init() -- unused + +end + +function screen.startDrawing2D() -- unused + --reset the video stacks + videoStack = {} + videoStack[0] = {} + videoStack[1] = {} +end + +function screen.endDrawing() + gfx.startFrame(drawScreen) + for i=1, #videoStack[drawScreen] do + local e = videoStack[drawScreen][i] + if e[1] == "img" then + + else + gfx[e[1]](unpack(e[2]) + end + end + gfx.endFrame() + gfx.render() +end + +function screen.waitForVBL() -- unused + +end diff --git a/uCompat.lua b/uCompat.lua new file mode 100644 index 0000000..1f71efb --- /dev/null +++ b/uCompat.lua @@ -0,0 +1,16 @@ +--[[ + Main µLua compatibility layer/lib for ctrµLua + + The goal is not to provide a full compatibility, but something close. +]] + +-- Constants +ULUA_VERSION = "4.7.2" +ULUA_DIR = "sdmc:/3ds/ctruLua/" +ULUA_SCRIPTS = (ULUA_DIR.."scripts/") -- Warning: you need to create the folder +ULUA_LIBS = (ULUA_DIR.."libs/") +ULUA_BOOT_FILE = "main.lua" +ULUA_BOOT_FULLPATH = (ULUA_DIR..ULUA_BOOT_FILE) + +-- Other libs +screen = require("uCompat.screen")