Advertisement
MrNaru300

OpenComputers simple graphical API (on going)

Jul 9th, 2020
1,820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.71 KB | None | 0 0
  1. --[[
  2.   Author: MrNaru300
  3.   GitHub: https://github.com/MrNaru300
  4.   Description: Just a simple graphical program
  5. ]]--
  6.  
  7.  
  8.  
  9. local graphics = {}
  10.  
  11. graphics.gpu = component.proxy(component.list("gpu")())
  12. assert(graphics.gpu ~= nil, "Could not find a gpu in your system")
  13.  
  14. graphics.__screen_table = {}
  15. graphics.screen_size = {0,0}
  16. graphics.screen_pos = {0,0}
  17.  
  18. function graphics.resize (pos, size)
  19.   --[[
  20.     Resize the program borders
  21.     @pos:
  22.       *Where the program begins
  23.       *Default: {0,0}
  24.     @size:
  25.       *The size of the program
  26.       *Default: The screen size
  27.   ]]--
  28.  
  29.   if(pos == nil) then pos = {1,1} end
  30.   if(size == nil) then
  31.     size = {graphics.gpu.getResolution()}
  32.     size[1] = size[1] - pos[1]
  33.     size[2] = size[2] - pos[2]
  34.    end
  35.  
  36.   checkArg(1, pos, "table")
  37.   checkArg(2, size, "table")
  38.  
  39.   graphics.screen_pos = pos
  40.   graphics.screen_size = size
  41.   graphics.__screen_table = {}
  42.  
  43.   local bg = graphics.gpu.getBackground()
  44.   local fg = graphics.gpu.getForeground()
  45.  
  46.   for y = 0,size[2] do
  47.     graphics.__screen_table[y] = {}
  48.     for x = 0,size[1] do
  49.       graphics.__screen_table[y][x] = {bg=bg, fg=fg, chr=" ", chd=false}
  50.     end
  51.   end
  52.  
  53. end
  54.  
  55. function graphics.render()
  56.   --[[
  57.     Actually renders the program
  58.   ]]--
  59.  
  60.  
  61.   local gpu = graphics.gpu
  62.   local old_pixel = {}
  63.   local new_pixel = {}
  64.  
  65.   for y = 0,graphics.screen_size[2] do
  66.     for x = 0,graphics.screen_size[1] do
  67.       new_pixel = graphics.__screen_table[y][x]
  68.       if (old_pixel.bg ~= new_pixel.bg) then gpu.setBackground(new_pixel.bg) end
  69.       if (old_pixel.fg ~= new_pixel.fg) then gpu.setForeground(new_pixel.fg) end
  70.       if (new_pixel.chd) then
  71.         gpu.set(x+graphics.screen_pos[1]-1, y+graphics.screen_pos[2]-1, graphics.__screen_table[y][x].chr)
  72.         new_pixel.chd = false
  73.       end
  74.       old_pixel = new_pixel
  75.     end  
  76.   end
  77. end
  78.  
  79.  
  80.  
  81. function graphics.pixel(x, y, bg, fg, chr)
  82.  
  83.   if (chr == nil) then chr = " " end
  84.  
  85.   checkArg(1, x, "number")
  86.   checkArg(2, y, "number")
  87.   checkArg(3, bg, "number")
  88.   checkArg(4, fg, "number")
  89.   checkArg(5, chr, "string")
  90.  
  91.   chr = string.sub(chr, 1, 1)
  92.  
  93.   graphics.__screen_table[y-1][x-1] = {bg=bg,fg=fg,chr=chr,chd=true}
  94. end
  95.  
  96. function graphics.box (init, final, bg, fg, chr)
  97.  
  98.   checkArg(1, init, "table")
  99.   checkArg(2, final, "table")
  100.  
  101.   for x = init[1],final[1] do
  102.     for y = init[2],final[2] do
  103.       graphics.pixel(x,y,bg,fg,chr)
  104.     end
  105.   end
  106. end
  107.  
  108. function graphics.line(init, final, bg, fg, chr)
  109.   local variation = (init[2]-final[2])/(init[1]-final[1])
  110.  
  111.   for x = init[1],final[1]-1 do
  112.     graphics.box({x, math.floor(variation*x+init[2])}, {x, math.floor(variation*(x+1)+init[2])}, bg, fg, chr)
  113.   end
  114. end
  115.  
  116. return graphics
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement