Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- import stuff
- comp = require "component"
- gpu = comp.gpu
- -- pattern {{"##00##", "#0#0#0"}, {{"#", 0xffffff}, {"0", 0x00ff00}}}
- pattern = {}
- --[[due to the fact, opencomputers doesnt support drawing individual pixel, this program uses a workaround to draw space characters instead of pixels
- on a certain position. The problem is that the space character's shape doesnt match a square pixel. to avoid this you can set this value to 2. 1 will work
- aswell, but it will look a little bit squeezed]]
- pattern.pixel_width = 2
- -- reads the given pattern table and replaces the chars with the given colors
- function pattern.draw(x, y, pat)
- local old_color = gpu.getBackground()
- for index,line in ipairs(pat[1]) do
- for i = 1, #line do
- local char = string.sub(line, i, i)
- pattern.setColor(char, pat)
- local filling_string = " "
- filling_string = string.rep(filling_string, pattern.pixel_width)
- gpu.set(x+((i-1)*pattern.pixel_width), y+index-1, filling_string)
- end
- end
- gpu.setBackground(old_color)
- end
- -- set the background color depending on the char's color definition in the table
- function pattern.setColor(char, pat)
- success = false
- for _,v in pairs(pat[2]) do
- if v[1] == char then
- if v[2] == nil then v[2] = 0xffffff end
- gpu.setBackground(v[2])
- success = true
- end
- end
- if not success then gpu.setBackground(0xffffff) end
- end
- return pattern
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement