From c4475849ffbce33f5b17cc1d70b5ef4a8eea63ce Mon Sep 17 00:00:00 2001 From: gamax92 Date: Fri, 26 Jun 2015 01:54:56 -0600 Subject: [PATCH] Higher success Check for and clamp to NUL in the unicode api math.floor depth when changing screen depth Fix getForeground and getBackground to return the palette index when using a palette, which is still not really correct but will mostly work for depth 8 Have palette changes recolor the screen --- src/apis/unicode.lua | 37 ++++++++++++++++++++++++++++++----- src/component/gpu.lua | 1 + src/component/screen_sdl2.lua | 21 +++++++++++++++++++- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/apis/unicode.lua b/src/apis/unicode.lua index 20eb79d..c4a70e4 100644 --- a/src/apis/unicode.lua +++ b/src/apis/unicode.lua @@ -2,11 +2,11 @@ local env = ... local utf8 = require("utf8") -env.unicode = { - len = utf8.len, - reverse = utf8.reverse, - sub = utf8.sub, -} +local function cln(str) + return str:gsub("%z.*","") .. "" +end + +env.unicode = {} function env.unicode.char(...) cprint("unicode.char", ...) @@ -21,17 +21,41 @@ function env.unicode.lower(str) cprint("unicode.lower", str) if type(str) == "number" then str = tostring(str) end checkArg(1,str,"string") + str=cln(str) return utf8.lower(str) end function env.unicode.upper(str) cprint("unicode.upper", str) if type(str) == "number" then str = tostring(str) end checkArg(1,str,"string") + str=cln(str) return utf8.upper(str) end +function env.unicode.len(str) + cprint("unicode.len", str) + checkArg(1,str,"string") + str=cln(str) + return utf8.len(str) +end +function env.unicode.reverse(str) + cprint("unicode.reverse", str) + checkArg(1,str,"string") + str=cln(str) + return utf8.reverse(str) +end +function env.unicode.sub(str, i, j) + cprint("unicode.isWide", str) + checkArg(1,str,"string") + checkArg(2,i,"number") + if j == nil then j = -1 end + checkArg(3,j,"number") + str=cln(str) + return utf8.sub(str,i,j) +end function env.unicode.isWide(str) cprint("unicode.isWide", str) checkArg(1,str,"string") + str=cln(str) if #str == 0 then error("String index out of range: 0",3) end @@ -41,6 +65,7 @@ end function env.unicode.charWidth(str) cprint("unicode.charWidth", str) checkArg(1,str,"string") + str=cln(str) if #str == 0 then error("String index out of range: 0",3) end @@ -50,6 +75,7 @@ end function env.unicode.wlen(str) cprint("unicode.wlen", str) checkArg(1,str,"string") + str=cln(str) local length = 0 for _,c in utf8.next, str do length = length + getCharWidth(c) @@ -60,6 +86,7 @@ function env.unicode.wtrunc(str, count) cprint("unicode.wtrunc", str, count) checkArg(1,str,"string") checkArg(2,count,"number") + str=cln(str) if count == math.huge then count = 0 end diff --git a/src/component/gpu.lua b/src/component/gpu.lua index c70400d..90edbbd 100644 --- a/src/component/gpu.lua +++ b/src/component/gpu.lua @@ -79,6 +79,7 @@ function obj.setDepth(depth) -- Set the color depth. Returns the previous value. if bindaddress == nil then return nil, "no screen" end + depth = math.floor(depth) local scrmax = component.cecinvoke(bindaddress, "maxDepth") if rdepthTbl[depth] == nil or rdepthTbl[depth] > math.max(scrmax, maxtier) then error("unsupported depth",3) diff --git a/src/component/screen_sdl2.lua b/src/component/screen_sdl2.lua index 3397cd4..cabdfb0 100644 --- a/src/component/screen_sdl2.lua +++ b/src/component/screen_sdl2.lua @@ -313,8 +313,12 @@ end local cec = {} +-- TODO: For (get/set)(Fore/Back)ground, they return what was passed in, rather than the current screen state function cec.getForeground() -- Get the current foreground color and whether it's from the palette or not. cprint("(cec) screen.getForeground") + if scrfgp then + return scrfgp, true + end return scrrfc, scrfgp end function cec.setForeground(value, palette) -- Sets the foreground color to the specified value. Optionally takes an explicit palette index. Returns the old value and if it was from the palette its palette index. @@ -327,6 +331,9 @@ function cec.setForeground(value, palette) -- Sets the foreground color to the s end function cec.getBackground() -- Get the current background color and whether it's from the palette or not. cprint("(cec) screen.getBackground") + if scrbgp then + return scrbgp, true + end return scrrbc, scrbgp end function cec.setBackground(value, palette) -- Sets the background color to the specified value. Optionally takes an explicit palette index. Returns the old value and if it was from the palette its palette index. @@ -404,7 +411,19 @@ function cec.setPaletteColor(index, color) -- Set the palette color at the speci if scrbgp == index then scrrbc, scrbgc = color, color end - -- TODO: Palette changes recolor the screen for respective characters + for y = 1,height do + for x = 1,width do + if screen.fgp[y][x] == index or screen.bgp[y][x] == index then + if screen.fgp[y][x] == index then + screen.fg[y][x] = color + end + if screen.bgp[y][x] == index then + screen.bg[y][x] = color + end + renderChar(utf8.byte(screen.txt[y][x]),(x-1)*8,(y-1)*16,screen.fg[y][x],screen.bg[y][x]) + end + end + end return old end function cec.get(x, y) -- Get the value displayed on the screen at the specified index, as well as the foreground and background color. If the foreground or background is from the palette, returns the palette indices as fourth and fifth results, else nil, respectively.