Jump to content




Buffer Question


10 replies to this topic

#1 darkroom

  • Members
  • 70 posts

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)

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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 by TheOriginalBIT, 14 January 2013 - 02:50 AM.


#3 darkroom

  • Members
  • 70 posts

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

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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 by TheOriginalBIT, 14 January 2013 - 02:56 AM.


#5 darkroom

  • Members
  • 70 posts

Posted 14 January 2013 - 03:23 AM

Epic is is what i am going to do! Thanks a lot

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 14 January 2013 - 03:30 AM

View Postdarkroom, on 14 January 2013 - 03:23 AM, said:

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...

#7 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 14 January 2013 - 03:57 AM

View PostTheOriginalBIT, on 14 January 2013 - 02:55 AM, said:

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.

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 14 January 2013 - 04:09 AM

View PostCloudy, on 14 January 2013 - 03:57 AM, said:

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...

#9 darkroom

  • Members
  • 70 posts

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


#10 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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.

#11 GopherAtl

  • Members
  • 888 posts

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
</shamelessselfpromotion>





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users