Fixed buffer positioning

This commit is contained in:
zenith391 2020-06-19 13:41:29 +02:00
parent f091f8b08b
commit bbae8c26b6
2 changed files with 17 additions and 11 deletions

View File

@ -7,7 +7,6 @@ local utf8 = require("lua-utf8")
local bindaddress local bindaddress
local depthTbl = {1,4,8} local depthTbl = {1,4,8}
local vramTbl = {1,2,3}
local rdepthTbl = {1,[4]=2,[8]=3} local rdepthTbl = {1,[4]=2,[8]=3}
local depthNames = {"OneBit","FourBit","EightBit"} local depthNames = {"OneBit","FourBit","EightBit"}
@ -24,19 +23,23 @@ local obj = {}
local activeBufferIdx = 0 -- 0 = screen local activeBufferIdx = 0 -- 0 = screen
local buffers = {} local buffers = {}
local totalMemory = maxwidth*maxheight*vramTbl[maxtier] local totalMemory = maxwidth*maxheight*maxtier
local usedMemory = 0 local usedMemory = 0
local function bufferSet(buf, x, y, char, fg, bg) local function bufferSet(buf, x, y, char, fg, bg)
if x > buf.width or y > buf.height or x < 1 or y < 1 then
return false
end
local pos = (y-1) * buf.width + x local pos = (y-1) * buf.width + x
fg = fg or buf.fg fg = fg or buf.fg
bg = bg or buf.bg bg = bg or buf.bg
buf.foreground[pos] = fg buf.foreground[pos] = fg
buf.background[pos] = bg buf.background[pos] = bg
local before = buf.text:sub(1, pos-1) local before = utf8.sub(buf.text, 1, pos-1)
local after = buf.text:sub(pos+1) local after = utf8.sub(buf.text, pos+1)
buf.text = before .. char .. after buf.text = before .. char .. after
buf.dirty = true buf.dirty = true
return true
end end
local function bufferGet(buf, x, y) local function bufferGet(buf, x, y)
@ -88,6 +91,7 @@ end
mai.freeBuffer = {direct = true, doc = "function(index: number): boolean -- Closes buffer at `index`. Returns true if a buffer closed. If the current buffer is closed, index moves to 0"} mai.freeBuffer = {direct = true, doc = "function(index: number): boolean -- Closes buffer at `index`. Returns true if a buffer closed. If the current buffer is closed, index moves to 0"}
function obj.freeBuffer(idx) function obj.freeBuffer(idx)
cprint("gpu.freeBuffer", idx)
if not buffers[idx] then if not buffers[idx] then
return false, "no buffer at index" return false, "no buffer at index"
else else
@ -238,7 +242,7 @@ end
mai.setForeground = {direct = true, doc = "function(value:number[, palette:boolean]):number, number or nil -- 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."} mai.setForeground = {direct = true, doc = "function(value:number[, palette:boolean]):number, number or nil -- 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."}
function obj.setForeground(value, palette) function obj.setForeground(value, palette)
cprint("gpu.setForeground", value, palette) cprint("gpu.setForeground", value, palette)
if consumeGraphicCallBudget(setForegroundCosts[maxtier]) then return end if not consumeGraphicCallBudget(setForegroundCosts[maxtier]) then return end
compCheckArg(1,value,"number") compCheckArg(1,value,"number")
compCheckArg(2,palette,"boolean","nil") compCheckArg(2,palette,"boolean","nil")
if bindaddress == nil then if bindaddress == nil then
@ -325,7 +329,7 @@ end
mai.fill = {direct = true, doc = "function(x:number, y:number, width:number, height:number, char:string):boolean -- Fills a portion of the screen at the specified position with the specified size with the specified character."} mai.fill = {direct = true, doc = "function(x:number, y:number, width:number, height:number, char:string):boolean -- Fills a portion of the screen at the specified position with the specified size with the specified character."}
function obj.fill(x, y, width, height, char) function obj.fill(x, y, width, height, char)
cprint("gpu.fill", x, y, width, height, char) cprint("gpu.fill", x, y, width, height, char)
if activeBufferIdx == 0 and not machine.consumeCallBudget(fillCosts[maxtier]) then return end if not consumeGraphicCallBudget(fillCosts[maxtier]) then return end
compCheckArg(1,x,"number") compCheckArg(1,x,"number")
compCheckArg(2,y,"number") compCheckArg(2,y,"number")
compCheckArg(3,width,"number") compCheckArg(3,width,"number")
@ -476,7 +480,7 @@ end
mai.set = {direct = true, doc = "function(x:number, y:number, value:string[, vertical:boolean]):boolean -- Plots a string value to the screen at the specified position. Optionally writes the string vertically."} mai.set = {direct = true, doc = "function(x:number, y:number, value:string[, vertical:boolean]):boolean -- Plots a string value to the screen at the specified position. Optionally writes the string vertically."}
function obj.set(x, y, value, vertical) function obj.set(x, y, value, vertical)
cprint("gpu.set", x, y, value, vertical) cprint("gpu.set", x, y, value, vertical)
if consumeGraphicCallBudget(setCosts[maxtier]) then return end if not consumeGraphicCallBudget(setCosts[maxtier]) then return end
compCheckArg(1,x,"number") compCheckArg(1,x,"number")
compCheckArg(2,y,"number") compCheckArg(2,y,"number")
compCheckArg(3,value,"string") compCheckArg(3,value,"string")

View File

@ -504,21 +504,23 @@ function cec.fill(x1, y1, w, h, char) -- Fills a portion of the screen at the sp
end end
return true return true
end end
function cec.bitblt(buf, col, row, width, height, fromCol, fromRow) function cec.bitblt(buf, col, row, w, h, fromCol, fromRow)
cprint("(cec) screen.bitblt", tostring(buf), col, row, width, height, fromCol, fromRow) cprint("(cec) screen.bitblt", tostring(buf), col, row, w, h, fromCol, fromRow)
local oldFg = srcfgc local oldFg = srcfgc
local oldBg = srcbgc local oldBg = srcbgc
for x=0, width-1 do for y=0, h-1 do
for y=0, height-1 do for x=0, w-1 do
local char, fg, bg = buf:bufferGet(x+fromCol, y+fromRow) local char, fg, bg = buf:bufferGet(x+fromCol, y+fromRow)
local dx = x+col local dx = x+col
local dy = y+row local dy = y+row
if dx >= 1 and dx <= width and dy >= 1 and dy <= height then if dx >= 1 and dx <= width and dy >= 1 and dy <= height then
srcfgc = fg srcfgc = fg
srcbgc = bg srcbgc = bg
io.stdout:write(char)
setPos(dx, dy, utf8.byte(char), fg, bg) setPos(dx, dy, utf8.byte(char), fg, bg)
end end
end end
print("")
end end
srcfgc = oldFg srcfgc = oldFg
srcbgc = oldBg srcbgc = oldBg