This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
darkroom's profile picture

Buffer Question

Started by darkroom, 14 January 2013 - 01:44 AM
darkroom #1
Posted 14 January 2013 - 02:44 AM
I have been thinking about ways to fix my flashing problem and my first idea which is very hard is to have some sort of stack that you push indexes of my objects and when you push or pop is the only time it updates the screen. The problem is you can't input easily. The other idea which I have no idea how to do is some sort of buffer thing is is there a way to do this:
buff = term.redirect(buffer)? I want to be able to do that so I can use the same pre built functions like this:
buff.write("herp")
buff.setCursorPos(1,1)
Then when i want to output I could make a function that only outputs changed things like this:
newBuff:changedUpdate(oldBuff)
theoriginalbit #2
Posted 14 January 2013 - 02:47 AM
I think the easiest way to do it would be override the term api with custom functions… unless you do particularly want to call buff.setCursorPos i think it would be easier to override it so term.setCursorPos actually goes to your api first…

EDIT: having the update function seems to me to be very OpenGL like ;)/>
Edited on 14 January 2013 - 01:50 AM
darkroom #3
Posted 14 January 2013 - 02:51 AM
Thing is how can you use the normal term functions on an array because the only way i can figure out to do it is to do an array with colors and then an array with text then print it out
theoriginalbit #4
Posted 14 January 2013 - 02:55 AM

local oldTerm = term -- stores the old term

local cursorX, cursorY = oldTerm.getCursorPos() -- init them where they currently are

term.setCursorPos = function( x, y )
  -- some validation on the pos making sure to use error( "Message", 2 ) so the error appears for the using program and not yours
  cursorX = x
  cursorY = y
  oldTerm.setCursorPos( x, y )
end

function restoreOldTerm() -- use this when the api is going to be no longer used
  term = oldTerm
end

EDIT: then when it comes to interacting with an array you have the cursorX and cursorY variabled
Edited on 14 January 2013 - 01:56 AM
darkroom #5
Posted 14 January 2013 - 03:23 AM
Epic is is what i am going to do! Thanks a lot
theoriginalbit #6
Posted 14 January 2013 - 03:30 AM
Epic is is what i am going to do! Thanks a lot

No problems… oh also the only update when required thing you could use a pull event loop with a custom event… example

when updating

function updateSomething()
  os.queueEvent( "some_name" )
end

then when drawing

function draw()
  os.pullEvent( "some_name" )
  -- draw stuff here
end

For a better working example of this look at my Loading Screen / Bar API…
Cloudy #7
Posted 14 January 2013 - 03:57 AM

local oldTerm = term -- stores the old term

local cursorX, cursorY = oldTerm.getCursorPos() -- init them where they currently are

term.setCursorPos = function( x, y )
  -- some validation on the pos making sure to use error( "Message", 2 ) so the error appears for the using program and not yours
  cursorX = x
  cursorY = y
  oldTerm.setCursorPos( x, y )
end

function restoreOldTerm() -- use this when the api is going to be no longer used
  term = oldTerm
end

EDIT: then when it comes to interacting with an array you have the cursorX and cursorY variabled

What the hell are you doing? You do realise this is what term.redirect and term.restore was invented for?

TL;DR - create a table with functions that mirror the behaviour of the actual term. Then use term.redirect with that object. Then use term.native.write etc to carry out the old actions.
theoriginalbit #8
Posted 14 January 2013 - 04:09 AM
TL;DR - create a table with functions that mirror the behaviour of the actual term. Then use term.redirect with that object. Then use term.native.write etc to carry out the old actions.
Ahh term.native to perform the old actions…. that was the part I could never figure out…
darkroom #9
Posted 14 January 2013 - 04:34 AM
Cloudy like so?

--Buffer Class--
local buffer = {
redirect = function(self)
  term.redirect(self.screen)
end
}
local buffer_mt = {
__index = buffer
}
function new(screen)
bufferConstructor = {
  screen = screen or { }
}
setmetatable(bufferConstructor, buffer_mt)
return bufferConstructor
end
Lyqyd #10
Posted 15 January 2013 - 04:48 AM
No. You need a table with matching functions for all the term calls, so you need myBufferTable.write, .clear, .clearLine, .setBackgroundColor, etc. All of those functions should act upon your buffer. Then, you use term.redirect and pass it your table of term replacement functions. You can use term.native functions inside those replacement functions to draw directly to screen, or outside of them to draw the whole buffer at once at pre-chosen points in your script.
GopherAtl #11
Posted 15 January 2013 - 07:44 AM
You could use this.http://pastebin.com/fU9Kj9zr it's my own redirect buffer api. Or use it as an example to figure out how to implement your own.

In your case you would use it something like this
Spoiler

if not buffer and not os.loadAPI("buffer") then
  error("buffer api required!")
end

local scrW,scrH=term.getSize()
local buff=buffer.createRedirectBuffer(scrW,scrH)

term.redirect(buff)

--main program loop
while true do
   --this function draws to term.native only the lines that have changed since last draw.
   --do this at the start of the loop, before pulling events or anything
   buff.drawDirty()

  --rest of yer code here.
  --draw stuff, update, whatever..

end
</shamelessselfpromotion>