Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ]] First a little documentation about how this format actually works.
- --[[
- I find visual illistrations the easiest way to learn so...
- Lets say we want to render this
- XXXX
- X XX X
- XXXXXX
- X XX X
- XXXX
- Now to decide the colors (We only have 4: White, Light Grey, Dark Grey, Black)
- We also need to make sure we are using 1 tile of space, or 8x8
- 00000000
- 00000000
- 00111100
- 01233210
- 01333310
- 01233210
- 00111100
- 00000000
- Now if you have synesthesia you can already see the image, most people however cannot. So good for you or whatever.
- Now for the harder part, how in the world do we convert this data to 2bpp?
- Well, as the name implies we only need 2 bits per pixel. 1 tile takes up 16 bytes, it takes 2 bits to make 4 colors.
- Meaning, 2 bytes per row.
- Lets use a very simple example: 0xFF 0x00 <- These bois are our first row of pixelies.
- Now easily convert to bin for all the juicy bits
- 1111 1111
- 0000 0000
- ^^^^ ^^^^
- 1234 5678
- Pixel # ^
- Ok, so this is a row of 10, or dark grey (2) colored pixels, yes?
- Woah, woah now, this is the gameboy days where indirect headaches were a thing, so don't forget to
- interpret the data as little indian first.
- 01 or light grey pixelies.
- ]] --
- local gbColors =
- {
- {
- name = "gray",
- [0] = colors.white,
- [1] = colors.lightGray,
- [2] = colors.gray,
- [3] = colors.black,
- },
- {
- name = "blue",
- [0] = colors.white,
- [1] = colors.lightBlue,
- [2] = colors.blue,
- [3] = colors.black,
- },
- {
- name = "red",
- [0] = colors.white,
- [1] = colors.pink,
- [2] = colors.red,
- [3] = colors.black,
- },
- {
- name = "fire",
- [0] = colors.white,
- [1] = colors.yellow,
- [2] = colors.orange,
- [3] = colors.red,
- },
- {
- name = "green",
- [0] = colors.white,
- [1] = colors.lime,
- [2] = colors.green,
- [3] = colors.black,
- },
- {
- name = "brown",
- [0] = colors.white,
- [1] = colors.orange,
- [2] = colors.brown,
- [3] = colors.black,
- },
- }
- function create_tile(bytes)
- -- A string list of bytes seperated by spaces
- local hexbytes = ""
- -- Remove spaces
- bytes = bytes:gsub("%s+","")
- if(#bytes%2 ~= 0)then error("create_tile (Converting to bytecode): Malformed bytes.") end
- if(#bytes/2 ~= 16)then error("create_tile (Converting to bytecode): Must be exactly 16 bytes!") end
- for i = 1, #bytes, 2 do
- hexbytes = hexbytes .. string.char(tonumber(bytes:sub(i,i+1),16))
- end
- return hexbytes
- end
- function load_tile(file)
- if(not fs.exists(file))then
- error("load_tile: No such file.")
- end
- local tile = {}
- local f = fs.open(file,"rb")
- local byteInd = 0
- local bytes = ""
- for byte in f.read do
- bytes = bytes .. string.char(byte)
- byteInd = byteInd + 1
- if(byteInd == 16)then
- table.insert(tile,bytes)
- byteInd = 0
- bytes = ""
- end
- end
- f.close()
- return tile
- end
- function pixel_getcolor(one,two,ind)
- local checkbit = bit32.lshift(1,ind-1)
- one = bit32.band(one, checkbit) ~= 0
- two = bit32.band(two, checkbit) ~= 0
- if one then one = 1 else one = 0 end
- if two then two = 1 else two = 0 end
- return tonumber(two .. one, 2)
- end
- function pixel_setcolor(one,two,ind,col)
- local checkbit = bit32.lshift(1,ind-1)
- if(col == 3)then
- one = bit32.bor(one, checkbit)
- two = bit32.bor(two, checkbit)
- elseif(col == 2)then
- one = bit32.band(one, bit32.bnot(checkbit))
- two = bit32.bor(two, checkbit)
- elseif(col == 1)then
- one = bit32.bor(one, checkbit)
- two = bit32.band(two, bit32.bnot(checkbit))
- elseif(col == 0)then
- one = bit32.band(one, bit32.bnot(checkbit))
- two = bit32.band(two, bit32.bnot(checkbit))
- end
- return one,two
- end
- function draw_tile(sx,sy,tile,scalex,scaley,pal)
- if(pal == nil)then
- pal = "Gray"
- end
- for i = 1, #gbColors do
- if(pal == gbColors[i].name)then pal = i end
- end
- if(type(pal) == "string")then pal = 1 end
- -- I really want this program to be able to draw ACTUAL 2bpp files so, im encoding these tiles
- -- in a string of real bytes.
- if(not scalex)then scalex = 1 end
- if(not scaley)then scaley = 1 end
- if( scalex < 0.5 or scaley < 0.5 or
- ( scalex < 1 and scalex ~= 0.5) or (scaley < 1 and scaley ~= 0.5)
- )then
- error("draw_tile: Unsupported image scalar, supported formats are as follows: \"0.5,1.0,2.0, ...\"")
- end
- bytes = {}
- local twobit = gbColors[pal]
- -- Uncompress tile so we can blit it all out quicker
- for i = 1, 16 do
- bytes[i-1] = string.byte(tile:sub(i,i))
- end
- local px, py = sx, sy, skip, rowskip
- if(scalex == 0 or scaley == 0)then return end
- for row = 0, #bytes, 2 do
- if(scaley < 1)then
- if(row%(8*scaley) ~= 0)then rowskip = true end
- end
- if(not rowskip)then
- for col = 8, 1, -1 do
- if(scalex < 1)then
- if(col%(4*scalex) ~= 0)then skip = true end
- end
- if(not skip)then
- local bit = pixel_getcolor(bytes[row], bytes[row + 1],col)
- term.setBackgroundColor(twobit[bit])
- term.setCursorPos(px,py)
- local bsize = scalex
- if(scalex < 1)then bsize = 1 end
- write(string.rep(" ",bsize))
- if(scaley > 1)then
- for i = 1, scaley-1 do
- term.setCursorPos(px, py+i)
- write(string.rep(" ",scalex))
- end
- end
- if(scalex >= 1)then
- px = px + scalex
- else
- px = px + 1
- end
- end
- skip = false
- end
- px = sx
- if(scaley >= 1)then
- py = py + scaley
- else
- py = py + 1
- end
- end
- rowskip = false
- end
- end
- function draw_tiles(sx,sy,tilearray,width,mx,my,pal)
- if(not mx)then mx = 1 end
- if(not my)then my = 1 end
- local px, py = sx, sy
- if(width == nil)then width = 3 end
- local tile_line = 0
- for i = 1, #tilearray do
- draw_tile(px,py,tilearray[i],mx,my, pal)
- px = px + (8*mx)
- tile_line = tile_line + 1
- if(tile_line == width)then
- py = py + (8*my)
- px = sx
- tile_line = 0
- end
- end
- end
- function little_draw_tiles(sx,sy,tilearray,width,mx,my,pal)
- if(not fs.exists("blittle"))then
- error ("little_draw_tiles: blittle is required to use this function. ")
- end
- os.loadAPI("blittle")
- local little_win = blittle.createWindow()
- local normal_win = term.current()
- term.redirect(little_win)
- draw_tiles(sx,sy,tilearray,width,mx,my, pal)
- term.redirect(normal_win)
- end
- function canvas_toBin(canv)
- local bytes = ""
- local msb = 0
- local lsb = 0
- local bit = 1
- write("\n")
- for t = 1, #canv.surface do
- local tile = canv.surface[t]
- -- Loop through entire tile, and build colors into bytes.
- for p = 1, 64 do
- lsb, msb = pixel_setcolor(lsb, msb, 8-(bit-1), tonumber(tile:sub(p,p)))
- --write(tile:sub(p,p) )
- --sleep(0.2)
- bit = bit + 1
- if(bit > 8)then
- --write(": " .. toHex(lsb) .. " " .. toHex(msb) .. "\n")
- bytes = bytes .. string.char(lsb) .. string.char(msb)
- lsb = 0
- msb = 0
- bit = 1
- --os.pullEvent("key")
- end
- end
- end
- return bytes
- end
- function bin_toCanvas(file)
- if(not fs.exists(file))then
- error("bin_toCanvas: No such file.")
- end
- local img = fs.open(file,"rb")
- local tiles = {}
- local msb = 0
- local lsb = 0
- local bytes = ""
- write("\n")
- for byte in img.read do
- bytes = bytes .. string.char(byte)
- if(#bytes == 16)then
- -- Convert tile bytes to 64 char string
- local tile = ""
- for b = 1, #bytes, 2 do
- lsb = string.byte(bytes:sub(b, b ))
- msb = string.byte(bytes:sub(b+1,b+1))
- for x = 8, 1, -1 do
- local c = pixel_getcolor(lsb,msb,x)
- tile = tile .. c
- end
- end
- table.insert(tiles,tile)
- bytes = ""
- end
- end
- return tiles
- end
- function save_setBackgroundColor(bg)
- local isvalid = false
- for i = 0, 15 do
- if(bg == 2^i)then isvalid = true end
- end
- if(bg == nil)then bg = "NULL" end
- if(not isvalid)then error("Background color not valid: " .. bg) end
- term.setBackgroundColor(bg)
- end
- function paint_tiles(file)
- local TILE = 8
- local exit_flag = false
- local _W, _H = term.getSize()
- local inpaint = true
- local col = 1
- local workingfile = file
- local pal = 1
- local palnames = {}
- for i = 1, #gbColors do
- table.insert(palnames,gbColors[i].name)
- end
- _H = _H - 1 -- Account for options bar.
- canvas =
- {
- ratio = 4,
- tiles = 16,
- offsetx = 0,
- offsety = 0,
- invert = 1,
- rendergrid = true,
- twobit = gbColors[pal],
- surface = {},
- tile_coords = {},
- init = function(self)
- -- surface[tile] = 64 chars.
- -- Store white pixels, 8 width * 8 height.
- for i = 1, self.tiles do
- self.surface[i] = string.rep("0",64)
- end
- end,
- draw = function(self,x,y,color)
- x = x - self.offsetx*self.invert
- y = y + self.offsety*self.invert
- local tx = math.floor(x%TILE)
- local ty = math.floor(y%TILE)
- --local tyi = math.ceil( y/TILE )
- --local txi = math.ceil( x/TILE )
- local ti = 0
- if(tx == 0)then tx = 8 end
- if(ty == 0)then ty = 8 end
- local sc = tx + (ty-1) * 8
- for i = 1, self.tiles do
- if(x >= self.tile_coords[i].x and x < self.tile_coords[i].x+TILE and
- y >= self.tile_coords[i].y and y < self.tile_coords[i].y+TILE)then
- ti = i
- end
- end
- if(ti == 0)then return false end
- --term.setCursorPos(1,15)
- --term.setBackgroundColor(colors.lightBlue)
- --term.setTextColor(colors.black)
- --print("TC: " .. txi .. ", " .. tyi .. " ")
- --print("TI: " .. ti .. " \nSC: " .. sc .. " ")
- self.surface[ti] = self.surface[ti]:sub(1,sc-1) .. color .. self.surface[ti]:sub(sc+1,-1)
- term.setCursorPos(x+self.offsetx*self.invert,y-self.offsety*self.invert)
- term.setBackgroundColor(self.twobit[color])
- term.setTextColor(colors.lightGray)
- if(color == 0 and self.rendergrid)then
- write("\127")
- else
- write(" ")
- end
- end,
- render_center = function(self,cap)
- if(cap==nil)then self.ratio = math.floor(math.sqrt(self.tiles)) end
- self.offsetx = math.floor(_W/2) - math.floor(((self.tiles*TILE)/self.ratio)/2)
- self.offsety = -(math.ceil(_H/2) - math.floor((self.tiles/self.ratio)*TILE)/2)
- end,
- load = function(self,surf)
- self.surface = surf
- self.tiles = #surf
- self:render_center()
- end,
- render_tile = function(self,tile,sx,sy)
- local indx = sx
- local indy = sy
- local pix = 1
- term.setCursorPos(sx, sy)
- if(#tile ~= 64)then
- error("Tile is corrupt! (" .. #tile .. ") ")
- end
- for y = 1, 8 do
- for x = 1, 8 do
- term.setCursorPos(indx, indy)
- local c = self.twobit[tonumber(tile:sub(pix,pix))]
- term.setBackgroundColor(c)
- term.setTextColor(colors.lightGray)
- if(indx <= _W and indy <= _H and indx > 0 and indy > 0)then
- if(tonumber(tile:sub(pix,pix)) == 0 and self.rendergrid)then
- write("\127")
- else
- write(" ")
- end
- end
- indx = indx + 1
- pix = pix + 1
- end
- indy = indy + 1
- indx = sx
- end
- end,
- render = function(self)
- local sx = 1
- local sy = 1
- for t = 1, self.tiles do
- self.tile_coords[t] = {x = sx, y = sy}
- if(self.tile_coords.my == nil)then self.tile_coords.my = sy end
- if(self.tile_coords.mx == nil)then self.tile_coords.mx = sx end
- if(self.tile_coords.my < sy)then self.tile_coords.my = sy end
- if(self.tile_coords.mx < sx)then self.tile_coords.mx = sx end
- self:render_tile(self.surface[t], sx+self.offsetx*self.invert, sy-self.offsety*self.invert)
- --term.setCursorPos(sx, sy)
- --term.setBackgroundColor(colors.black)
- --write(self.tile_coords[t].x .. ", " .. self.tile_coords[t].y)
- if( (t%self.ratio) == 0)then
- sy = sy + 8
- sx = 1
- else
- sx = sx + 8
- end
- end
- end,
- drawCharBox = function(sx,sy,ex,ey,tc,bc,ch)
- for y = sy, ey do
- term.setCursorPos(sx, y)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.gray)
- write(string.rep("\127",(ex-sx)+1))
- end
- end,
- move_render = function(self,xo,yo,bg,tc,cc)
- local aox = self.offsetx + xo
- local aoy = self.offsety + yo
- --if(aox < 0)then aox = 0 end
- --if(aoy < 0)then aoy = 0 end
- --if(aox > self.tile_coords.mx)then aox = self.tile_coords.mx end
- --if(aoy > self.tile_coords.my)then aoy = self.tile_coords.my end
- local sx = 1+self.offsetx*self.invert
- local sy = 1-self.offsety*self.invert
- local ex = math.min((self.ratio*8)+(1+self.offsetx*self.invert),_W)
- local ey = math.min((((self.tiles/self.ratio)*8)+(1-self.offsety*self.invert)),_H)
- self.drawCharBox(sx,sy,ex,ey,tc,bg,cc)
- self.offsetx = aox
- self.offsety = aoy
- end,
- resize = function(self,tiles)
- tiles = tonumber(tiles)
- if(tiles > self.tiles)then
- for i = 1, tiles do
- if(self.surface[i] == nil or self.surface[i] == "")then
- self.surface[i] = string.rep("0",64)
- end
- end
- else
- for i = self.tiles, tiles+1, -1 do
- table.remove(self.surface, i)
- end
- end
- self.tiles = tiles
- self:render_center()
- end,
- setRatio = function(self,ratio)
- ratio = tonumber(ratio)
- if(ratio > self.tiles)then ratio = self.tiles end
- self.ratio = ratio
- self:render_center(1)
- self:render()
- end,
- }
- local function textbar(c,txt)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(c)
- term.setCursorPos(1,19)
- term.clearLine()
- write(txt)
- end
- local function openSelectionMenu(items,x,y,tc,bc,w,sind)
- if(sind == nil)then sind = 1 end
- local selected = sind
- local selecting = true
- local function drawitems(clr)
- if(clr)then
- paintutils.drawFilledBox(x,y-1,x+w,y+#items,bc)
- end
- term.setBackgroundColor(bc)
- for i = 1, #items do
- term.setCursorPos(x,y+i-1)
- term.setTextColor(gbColors[i][1])
- if(i == selected)then
- write("> " .. items[i] .. " <")
- else
- write(" " .. items[i] .. " ")
- end
- end
- end
- drawitems(true)
- while (selecting) do
- local e = {os.pullEvent()}
- if(e[1] == "key")then
- local key = e[2]
- if(key == keys.up)then selected = selected - 1 end
- if(key == keys.down)then selected = selected + 1 end
- if(selected > #items)then selected = 1 end
- if(selected <= 0)then selected = #items end
- drawitems(false)
- if(key == keys.enter)then selecting = false end
- elseif(e[1] == "mouse_click")then
- local mx = e[3]
- local my = e[4]
- if(mx >= x and mx <= x+w and my > y-1 and my < y+#items)then
- selected = my-(y-1)
- drawitems(false)
- sleep(0.2)
- selecting = false
- else
- selected = 0
- selecting = false
- end
- end
- end
- return selected
- end
- local contexts =
- {
- file = {
- {c=colors.red,text=" Quit",button=1,func = function()
- exit_flag = true
- end},
- {c=colors.white,text=" Load",button=1,func = function()
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.setCursorPos(1,19)
- term.clearLine()
- write("Load file: ")
- local nn = read()
- if(nn == nil or nn == "")then return end
- if(not fs.exists(nn))then
- textbar(colors.red,"File doesn't exist!")
- sleep(0.5)
- return
- end
- local nsurf = bin_toCanvas(nn)
- canvas:load(nsurf)
- textbar(colors.white,"Loaded " .. nn .. ". ")
- workingfile = nn
- canvas:render()
- sleep(0.5)
- return
- end},
- {c=colors.white,text=" Save As",button=1,func = function()
- local nn = ""
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.setCursorPos(1,19)
- term.clearLine()
- write("Save as: ")
- nn = read()
- if(nn == nil or nn == "")then return end
- if(fs.exists(nn))then
- textbar(colors.red,"File already exists!")
- sleep(0.5)
- return
- else
- local sav = canvas_toBin(canvas)
- f = fs.open(workingfile,"w")
- f.write(sav)
- f.close()
- textbar(colors.white,"Saved to: " .. workingfile)
- sleep(0.5)
- return
- end
- end},
- {c=colors.white,text=" Save",button=1,func = function()
- local nn = ""
- if(workingfile == "" and workingfile ~= nil)then
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.setCursorPos(1,19)
- term.clearLine()
- write("Save as: ")
- nn = read()
- if(nn == nil or nn == "")then return end
- if(fs.exists(nn))then
- textbar(colors.red,"File already exists!")
- sleep(0.5)
- return
- end
- else
- local sav = canvas_toBin(canvas)
- f = fs.open(workingfile,"w")
- f.write(sav)
- f.close()
- textbar(colors.white,"Saved to: " .. workingfile)
- sleep(0.5)
- return
- end
- if(workingfile == "" and workingfile ~= nil)then
- workingfile = nn
- local sav = canvas_toBin(canvas)
- f = fs.open(nn,"w")
- f.write(sav)
- f.close()
- textbar(colors.white,"Saved to: " .. workingfile)
- sleep(0.5)
- return
- end
- end},
- {c=colors.black,text="",button=0},
- },
- image = {
- {c=colors.white,button=1,text=" Toggle [G]rid",func = function()
- canvas.rendergrid = not canvas.rendergrid
- canvas:render()
- end},
- {c=colors.blue,text=" Palette",button=1,func = function()
- local npal = openSelectionMenu(palnames,7,_H-#palnames,colors.white,colors.gray,8,pal)
- if(npal ~= 0)then
- pal = npal
- canvas.twobit = gbColors[pal]
- end
- end},
- {c=colors.white,text=" Set Ratio",button=1,func = function()
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.setCursorPos(1,19)
- term.clearLine()
- write("Tile Aspect Ratio: ")
- local yy = read()
- if(yy == "" or yy == nil)then return end
- term.setBackgroundColor(colors.black)
- canvas:setRatio(yy)
- canvas:render()
- end},
- {c=colors.white,text=" Resize",button=1,func = function()
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.setCursorPos(1,19)
- term.clearLine()
- write("Tiles: ")
- local xx = read()
- if(xx == "" or xx == nil)then return end
- term.setBackgroundColor(colors.black)
- canvas:resize(xx)
- canvas:render()
- end},
- {c=colors.black,text="",button=0},
- },
- }
- local options =
- {
- {x=1,c=colors.gray,w=8 ,text="File" ,menu = contexts.file },
- {x=7,c=colors.gray,w=14 ,text="Image" ,menu = contexts.image},
- }
- local inmenu = 0
- local function drawGUI()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.gray)
- for i = 1, _H do
- print(string.rep("\127",_W))
- end
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(5,19)
- term.clearLine()
- term.setTextColor(colors.white)
- for i = 1, #options do
- term.setCursorPos(options[i].x,19)
- write(options[i].text)
- end
- canvas:render()
- end
- local function drawMenuOpt(opt)
- local sx = options[opt].x
- local sy = _H-#options[opt].menu
- paintutils.drawFilledBox(sx,sy,sx+options[opt].w,sy+#options[opt].menu,options[opt].c)
- for i = 1, #options[opt].menu do
- term.setBackgroundColor(options[inmenu].c)
- term.setTextColor(options[inmenu].menu[i].c)
- term.setCursorPos(options[inmenu].x,18-i)
- write(options[inmenu].menu[i].text)
- end
- end
- local function optionBlink(i,t)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.gray)
- term.setCursorPos(options[i].x,19)
- write(options[i].text)
- sleep(t)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- --term.setCursorPos(options[i].x,19)
- --write(options[i].text)
- end
- local function menu_optionBlink(i,t)
- term.setBackgroundColor(options[inmenu].menu[i].c)
- term.setTextColor(options[inmenu].c)
- term.setCursorPos(options[inmenu].x,_H-i)
- write(options[inmenu].menu[i].text .. string.rep(" ",options[inmenu].w-#options[inmenu].menu[i].text))
- sleep(t)
- term.setBackgroundColor(options[inmenu].c)
- term.setTextColor(options[inmenu].menu[i].c)
- term.setCursorPos(options[inmenu].x,_H-i)
- write(options[inmenu].menu[i].text .. string.rep(" ",options[inmenu].w-#options[inmenu].menu[i].text))
- end
- local function updateGUI()
- term.setTextColor(canvas.twobit[3-col])
- term.setCursorPos(45,19)
- term.setBackgroundColor(canvas.twobit[col])
- write(" Color ")
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(33,19)
- term.setTextColor(colors.black)
- write("X:" .. canvas.offsetx .. " ")
- term.setCursorPos(39,19)
- write("Y:" .. canvas.offsety .. " ")
- end
- canvas:init()
- if(workingfile ~= "" and workingfile ~= nil)then
- if(fs.exists(workingfile))then
- local nsurf = bin_toCanvas(workingfile)
- canvas:load(nsurf)
- end
- end
- drawGUI()
- updateGUI()
- while inpaint do
- if(exit_flag)then
- inpaint = false
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- paintutils.drawFilledBox(1,1,_W,7,colors.blue)
- term.setCursorPos(15,2)
- print("Thank you for using 2BCC!")
- term.setCursorPos(3,4)
- print("If you find any bugs when using 2BCC please be")
- term.setCursorPos(3,5)
- print("sure to post about them on the forums.")
- term.setCursorPos(_W-5,6)
- print("\1672018")
- term.setCursorPos(1, 9)
- return
- end
- local e = {os.pullEvent()}
- if(e[1] == "mouse_click" or e[1] == "mouse_drag")then
- local x = e[3]
- local y = e[4]
- if(y ~= _H+1 and inmenu == 0)then
- canvas:draw(x,y,col)
- end
- if(y == _H+1 and inmenu == 0)then
- for i = 1, #options do
- if(x >= options[i].x and x < options[i].x+#options[i].text)then
- optionBlink(i,0.2)
- inmenu = i
- drawMenuOpt(i)
- --drawGUI()
- --updateGUI()
- end
- end
- elseif(inmenu ~= 0)then
- for i = 1, #options[inmenu].menu do
- if(x >= options[inmenu].x and x < options[inmenu].x+options[inmenu].w and y == _H-i)then
- if(options[inmenu].menu[i].button == 1)then
- menu_optionBlink(i,0.2)
- drawGUI()
- updateGUI()
- options[inmenu].menu[i].func()
- inmenu = 0
- drawGUI()
- updateGUI()
- break
- end
- end
- end
- if(inmenu ~= 0)then
- if(not (
- x >= options[inmenu].x and x <= options[inmenu].x+options[inmenu].w and
- y >= _H-#options[inmenu].menu and y <= _H
- ) )then
- inmenu = 0
- drawGUI()
- updateGUI()
- end
- end
- end
- elseif(e[1] == "mouse_scroll")then
- local sdir = e[2]
- if(sdir == 1)then
- col = col + 1
- if(col > 3)then col = 3 end
- sleep(0.01)
- updateGUI()
- end
- if(sdir == -1)then
- col = col - 1
- if(col < 0)then col = 0 end
- sleep(0.01)
- updateGUI()
- end
- elseif(e[1] == "key")then
- local k = e[2]
- if(k == keys.one )then col = 0 updateGUI() end
- if(k == keys.two )then col = 1 updateGUI() end
- if(k == keys.three )then col = 2 updateGUI() end
- if(k == keys.four )then col = 3 updateGUI() end
- if(k == keys.g)then
- canvas.rendergrid = not canvas.rendergrid
- canvas:render()
- end
- local vis = term.current().setVisible
- if(vis == nil)then vis = function(tf) return false end end
- if(k == keys.up)then
- vis(false)
- canvas:move_render(0,-1,colors.black,colors.gray,"\127")
- canvas:render()
- updateGUI()
- vis(true)
- end
- if(k == keys.down)then
- vis(false)
- canvas:move_render(0,1,colors.black,colors.gray,"\127")
- canvas:render()
- updateGUI()
- vis(true)
- end
- if(k == keys.left)then
- vis(false)
- canvas:move_render(-1,0,colors.black,colors.gray,"\127")
- canvas:render()
- updateGUI()
- vis(true)
- end
- if(k == keys.right)then
- vis(false)
- canvas:move_render(1,0,colors.black,colors.gray,"\127")
- canvas:render()
- updateGUI()
- vis(true)
- end
- end
- end
- end
- --draw_tile(8,5,create_tile("FF 00 7E FF 85 81 89 83 93 85 A5 8B C9 97 7E FF"))
- -- Here's an example 2bpp from pokemon red; The back sprite of bellsprout.
- local bellsprout = {
- create_tile("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"),
- create_tile("00 00 00 00 00 00 03 07 08 08 10 10 00 20 20 70"),
- create_tile("00 00 00 00 00 00 c0 e0 70 10 38 08 1c 04 18 04"),
- create_tile("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"),
- create_tile("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"),
- create_tile("90 88 58 e8 f0 88 20 70 20 c0 80 a0 b0 a0 08 90"),
- create_tile("0c 02 06 02 06 02 16 0e 0a 1e 10 0a 00 04 0c 04"),
- create_tile("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"),
- create_tile("00 00 00 00 1c 3e 4b 61 87 e0 ed f3 0a 0c 15 18"),
- create_tile("50 58 28 28 14 10 07 0c 8b 8a 8d c5 20 24 14 12"),
- create_tile("0c 04 1c 04 34 0c e8 1c ca 32 bf 61 bf ef 6e 72"),
- create_tile("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"),
- create_tile("2e 30 55 68 8e f0 87 ff 00 00 00 00 00 00 00 00"),
- create_tile("2a 0a 16 0e 2a 06 19 87 00 00 00 00 00 00 00 00"),
- create_tile("08 1c 00 00 00 00 00 00 00 00 00 00 00 00 00 00"),
- create_tile("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"),
- }
- local targs = { ... }
- if(#targs < 1)then
- term.setTextColor(colors.red)
- print("Usage: " .. shell.getRunningProgram() .. " <function> <args>")
- term.setTextColor(colors.white)
- print(shell.getRunningProgram() .. " functions:\n")
- print("[edit <file>] Opens/Creates a 2BPP file for editing.\n")
- print("[render <file> <ratio> (xscale) (yscale) (palette)] Renders a 2BPP file at the specified tile-ratio.\n")
- print("[renderlittle <file> <ratio> (xscale) (yscale) (palette)] Renders a 2BPP file using blittle.\n")
- print("[demo] Renders a demo 2BPP sprite")
- print("[littledemo] Uses blittle to render a demo 2BPP sprite")
- else
- -- I've made so many lists at this point that i'm just going to if/elif this :P
- if(targs[1]:lower() == "edit")then
- if(#targs < 2)then
- error("[2BPP] Usage: edit <file>")
- end
- paint_tiles(targs[2])
- elseif(targs[1]:lower() == "render")then
- if(#targs < 3)then
- error("[2BPP] Usage: render <file> <ratio> (scalex) (scaley) (palette)")
- end
- local scx, scy = tonumber(targs[4]), tonumber(targs[5])
- if(scx == nil)then scx = 2 end
- if(scy == nil)then scy = 2 end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- draw_tiles(2,1,load_tile(targs[2]),tonumber(targs[3]),scx,scy,targs[6])
- term.setCursorPos(1,1)
- elseif(targs[1]:lower() == "renderlittle")then
- if(#targs < 3)then
- error("[2BPP] Usage: renderlittle <file> <ratio> (scalex) (scaley) (palette)")
- end
- local scx, scy = tonumber(targs[4]), tonumber(targs[5])
- if(scx == nil)then scx = 2 end
- if(scy == nil)then scy = 2 end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- little_draw_tiles(3,2,load_tile(targs[2]),tonumber(targs[3]),scx,scy,targs[6])
- term.setCursorPos(1,1)
- elseif(targs[1]:lower() == "demo")then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- draw_tiles(1,-5,bellsprout,4,2,1)
- elseif(targs[1]:lower() == "littledemo")then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- little_draw_tiles(5,1,bellsprout,4,2,2)
- term.setCursorPos(1,1)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement