Advertisement
nonogamer9

Mode 7 Graphics Test On OpenComputers

Jul 25th, 2024 (edited)
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.99 KB | Software | 0 0
  1. local component = require("component")
  2. local gpu = component.gpu
  3. local event = require("event")
  4. local math = require("math")
  5. local unicode = require("unicode")
  6.  
  7. local w, h = gpu.getResolution()
  8.  
  9. -- Color palettes
  10. local planeColors = {0x000000, 0x0000FF, 0x00FF00, 0xFF0000, 0xFFFF00}
  11. local plasmaColors = {0xFF0000, 0xFF7F00, 0xFFFF00, 0x00FF00, 0x0000FF, 0x8B00FF}
  12.  
  13. -- Custom buffer implementation
  14. local Buffer = {}
  15. Buffer.__index = Buffer
  16.  
  17. function Buffer.new(width, height)
  18.     local self = setmetatable({}, Buffer)
  19.     self.width = width
  20.     self.height = height
  21.     self.data = {}
  22.     for i = 1, width * height do
  23.         self.data[i] = {bg = 0x000000, fg = 0xFFFFFF, char = " "}
  24.     end
  25.     return self
  26. end
  27.  
  28. function Buffer:setPixel(x, y, bg, fg, char)
  29.     if x >= 1 and x <= self.width and y >= 1 and y <= self.height then
  30.         self.data[(y - 1) * self.width + x] = {bg = bg, fg = fg, char = char}
  31.     end
  32. end
  33.  
  34. function Buffer:render(gpuBuffer)
  35.     gpu.setActiveBuffer(gpuBuffer)
  36.     local lastBg, lastFg = -1, -1
  37.     for y = 1, self.height do
  38.         local rowStart = (y - 1) * self.width
  39.         for x = 1, self.width do
  40.             local pixel = self.data[rowStart + x]
  41.             if pixel.bg ~= lastBg then
  42.                 gpu.setBackground(pixel.bg)
  43.                 lastBg = pixel.bg
  44.             end
  45.             if pixel.fg ~= lastFg then
  46.                 gpu.setForeground(pixel.fg)
  47.                 lastFg = pixel.fg
  48.             end
  49.             gpu.set(x, y, pixel.char)
  50.         end
  51.     end
  52. end
  53.  
  54. -- Create buffers and GPU buffers
  55. local screenBuffer1 = Buffer.new(w, h)
  56. local screenBuffer2 = Buffer.new(w, h)
  57. local gpuBuffer1 = gpu.allocateBuffer(w, h)
  58. local gpuBuffer2 = gpu.allocateBuffer(w, h)
  59. local activeBuffer, inactiveBuffer = screenBuffer1, screenBuffer2
  60. local activeGPUBuffer, inactiveGPUBuffer = gpuBuffer1, gpuBuffer2
  61.  
  62. -- Precompute sine and cosine values
  63. local sineTable, cosineTable = {}, {}
  64. for i = 0, 360 do
  65.     sineTable[i] = math.sin(math.rad(i))
  66.     cosineTable[i] = math.cos(math.rad(i))
  67. end
  68.  
  69. -- Mode 7 parameters
  70. local scale = 1
  71. local angle = 0
  72. local offsetX, offsetY = 0, 0
  73.  
  74. -- Create a simple texture for Mode 7
  75. local textureSize = 64
  76. local texture = {}
  77. for y = 1, textureSize do
  78.     texture[y] = {}
  79.     for x = 1, textureSize do
  80.         texture[y][x] = planeColors[((x + y) % #planeColors) + 1]
  81.     end
  82. end
  83.  
  84. -- Mode 7 transformation function
  85. local function mode7(screenX, screenY, scale, angle, offsetX, offsetY)
  86.     local sin, cos = sineTable[angle], cosineTable[angle]
  87.     local x = (screenX - w/2) / scale
  88.     local y = (screenY - h) / scale
  89.     return x * cos - y * sin + offsetX, x * sin + y * cos + offsetY
  90. end
  91.  
  92. -- Optimized plasma generation
  93. local function generatePlasma(buffer, t)
  94.     for y = 1, buffer.height / 2 do
  95.         for x = 1, buffer.width do
  96.             local v = sineTable[(x * 12 + t * 15) % 360]
  97.             v = v + sineTable[(y * 9 + t * 12) % 360]
  98.             v = v + sineTable[((x + y) * 7 + t * 9) % 360]
  99.             v = v + sineTable[(math.floor(math.sqrt(x * x + y * y)) * 8 + t * 7) % 360]
  100.             v = v / 4
  101.             local colorIndex = math.floor((v + 1) * #plasmaColors / 2) + 1
  102.             buffer:setPixel(x, y, plasmaColors[colorIndex], 0x000000, " ")
  103.         end
  104.     end
  105. end
  106.  
  107. -- Main loop
  108. local t = 0
  109. local running = true
  110.  
  111. event.listen("key_down", function(_, _, _, code)
  112.     if code == 16 then  -- 'Q' key
  113.         running = false
  114.     end
  115. end)
  116.  
  117. while running do
  118.     generatePlasma(activeBuffer, t)
  119.    
  120.     -- Update Mode 7 parameters
  121.     angle = (angle + 1) % 360
  122.     offsetX = 32 + 16 * sineTable[t % 360]
  123.     offsetY = 32 + 16 * sineTable[(t + 90) % 360]
  124.     scale = 0.5 + 0.25 * sineTable[(t * 2) % 360]
  125.    
  126.     -- Render Mode 7 effect
  127.     for screenY = h/2, h do
  128.         for screenX = 1, w do
  129.             local tx, ty = mode7(screenX, screenY, scale, angle, offsetX, offsetY)
  130.             local textureX = math.floor(tx) % textureSize + 1
  131.             local textureY = math.floor(ty) % textureSize + 1
  132.             local color = texture[textureY][textureX]
  133.             activeBuffer:setPixel(screenX, screenY, color, color, "▄")
  134.         end
  135.     end
  136.  
  137.     -- Add a moving object
  138.     local objX = math.floor(w / 2 + sineTable[t % 360] * w / 4)
  139.     local objY = math.floor(h / 2 + sineTable[(t + 90) % 360] * h / 4)
  140.     for i = -2, 2 do
  141.         for j = -2, 2 do
  142.             activeBuffer:setPixel(objX + i, objY + j, 0xFFFFFF, 0x000000, "O")
  143.         end
  144.     end
  145.  
  146.     activeBuffer:render(activeGPUBuffer)
  147.    
  148.     -- Copy GPU buffer to screen
  149.     gpu.setActiveBuffer(0)
  150.     gpu.bitblt(0, 1, 1, w, h, activeGPUBuffer)
  151.  
  152.     -- Swap buffers
  153.     activeBuffer, inactiveBuffer = inactiveBuffer, activeBuffer
  154.     activeGPUBuffer, inactiveGPUBuffer = inactiveGPUBuffer, activeGPUBuffer
  155.  
  156.     t = t + 1
  157. end
  158.  
  159. -- Clean up
  160. gpu.setActiveBuffer(0)
  161. gpu.freeBuffer(gpuBuffer1)
  162. gpu.freeBuffer(gpuBuffer2)
  163. gpu.setBackground(0x000000)
  164. gpu.setForeground(0xFFFFFF)
  165. gpu.fill(1, 1, w, h, " ")
  166.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement