Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Author: MrNaru300
- GitHub: https://github.com/MrNaru300
- Description: Just a simple graphical program
- ]]--
- local graphics = {}
- graphics.gpu = component.proxy(component.list("gpu")())
- assert(graphics.gpu ~= nil, "Could not find a gpu in your system")
- graphics.__screen_table = {}
- graphics.screen_size = {0,0}
- graphics.screen_pos = {0,0}
- function graphics.resize (pos, size)
- --[[
- Resize the program borders
- @pos:
- *Where the program begins
- *Default: {0,0}
- @size:
- *The size of the program
- *Default: The screen size
- ]]--
- if(pos == nil) then pos = {1,1} end
- if(size == nil) then
- size = {graphics.gpu.getResolution()}
- size[1] = size[1] - pos[1]
- size[2] = size[2] - pos[2]
- end
- checkArg(1, pos, "table")
- checkArg(2, size, "table")
- graphics.screen_pos = pos
- graphics.screen_size = size
- graphics.__screen_table = {}
- local bg = graphics.gpu.getBackground()
- local fg = graphics.gpu.getForeground()
- for y = 0,size[2] do
- graphics.__screen_table[y] = {}
- for x = 0,size[1] do
- graphics.__screen_table[y][x] = {bg=bg, fg=fg, chr=" ", chd=false}
- end
- end
- end
- function graphics.render()
- --[[
- Actually renders the program
- ]]--
- local gpu = graphics.gpu
- local old_pixel = {}
- local new_pixel = {}
- for y = 0,graphics.screen_size[2] do
- for x = 0,graphics.screen_size[1] do
- new_pixel = graphics.__screen_table[y][x]
- if (old_pixel.bg ~= new_pixel.bg) then gpu.setBackground(new_pixel.bg) end
- if (old_pixel.fg ~= new_pixel.fg) then gpu.setForeground(new_pixel.fg) end
- if (new_pixel.chd) then
- gpu.set(x+graphics.screen_pos[1]-1, y+graphics.screen_pos[2]-1, graphics.__screen_table[y][x].chr)
- new_pixel.chd = false
- end
- old_pixel = new_pixel
- end
- end
- end
- function graphics.pixel(x, y, bg, fg, chr)
- if (chr == nil) then chr = " " end
- checkArg(1, x, "number")
- checkArg(2, y, "number")
- checkArg(3, bg, "number")
- checkArg(4, fg, "number")
- checkArg(5, chr, "string")
- chr = string.sub(chr, 1, 1)
- graphics.__screen_table[y-1][x-1] = {bg=bg,fg=fg,chr=chr,chd=true}
- end
- function graphics.box (init, final, bg, fg, chr)
- checkArg(1, init, "table")
- checkArg(2, final, "table")
- for x = init[1],final[1] do
- for y = init[2],final[2] do
- graphics.pixel(x,y,bg,fg,chr)
- end
- end
- end
- function graphics.line(init, final, bg, fg, chr)
- local variation = (init[2]-final[2])/(init[1]-final[1])
- for x = init[1],final[1]-1 do
- graphics.box({x, math.floor(variation*x+init[2])}, {x, math.floor(variation*(x+1)+init[2])}, bg, fg, chr)
- end
- end
- return graphics
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement