Quick Buffer:
This buffer makes use of a method that I've had written on paper and imprinted in my mind for quite some time but was never able to translate it into code. This being the following stolen description from the file itself:
Spoiler
IMPORTANT NOTE 1:
Each buffer's contents is separated into three tables of
equal width and height:
- tText
- tTextColors
- tBackColors
Each of whom is setup that each character represents either a textual
character or a hex digit to represent a color as a single byte.
Colors are then converted at render time from their hex equivalent
into decimal format.
IMPORTANT NOTE 2:
What makes this buffer special is the way that
it handles rendering. While many similar apis simply
save a pixel as a character, a text color, and a background
color, then write said pixel out with the respective
term API calls, this buffer does something different:
Instead of changing colors all of the time, this
buffer makes rendering quicker by using a function
called 'getChunks.'
'getChunks' goes through a line in a buffer and
returns every 'chunk' of text that has the given
text and background colors. This way, the maximum
amount of text can be written before colors are
changed.
To prevent having to make 256 different iterations,
only the color pairs (set of text and background colors)
which are actually in the buffer are checked and rendered.
This is done by recording those used in 'write' and various
'clear' calls. Also, a function called 'updateColorPairs'
brute force checks the entire buffer for what color pairs
actually exist, then stores in them in the 'tColorPairs'
hash table which looks like this:
tColorPairs[sTextColor .. sBackColor] = true
(The value is true if it exists, nil or false if not.)
In essence, I think that this buffering method allows for some of the quickest rendering of common program visual output by making use of the aforementioned method. However, there is also a hangup to specializing for common output:
Basically, if you have a program whose visual output is very, very colorful and unique all over the place, then rendering will probably be slower than the other rendering methods.However, it is important to note that this maximizes
efficiency for common use, for most programs make use
of large portions of similarly colored text both in
the text color and background color.
- HOWEVER, situations in which the text and background
color pair is changing very often, this buffer may
actually be SLOWER than the classic change-every-iteration
approach!
Uses:
- Windowing
- Multiple programs running at the same time
- Taking screen shots of the screen
- Making animations from a frame or series of frames
- Accessing information that has been written to the screen
- Stuff that I haven't even thought of
Examples of Usage:
Spoiler
Animation:
Spoiler
Here's an example API script that will give the appearance of a frame growing from the center of the screen outwards to its original dimensions and position:
ocal function getTableSize (tTable)
local nSize = 0
for _, __ in pairs (tTable) do
nSize = nSize + 1
end
return nSize
end
local function getShrunkBuffer (tBuffer, nWidthScale, nHeightScale)
nHeightScale = math.floor (nHeightScale)
nWidthScale = math.floor (nWidthScale)
– Gotta set up new width and height dimensions so getChunks doesn't freak out!
local tShrunkBuffer = {
tText = {},
tTextColors = {},
tBackColors = {},
tTerm = tBuffer.tTerm
}
local nShrunkLineNumber = 1
local nLeftConstraint = tBuffer.nWidth / 2 - nWidthScale
local nRightConstraint = tBuffer.nWidth / 2 + nWidthScale
for nLineNumber = math.floor (tBuffer.nHeight / 2 - nHeightScale), math.floor (tBuffer.nHeight / 2 + nHeightScale) do
if nLineNumber >= 1 and nLineNumber <= tBuffer.nHeight then
tShrunkBuffer.tText[nShrunkLineNumber] = tBuffer.tText[nLineNumber]:sub (nLeftConstraint, nRightConstraint)
tShrunkBuffer.tTextColors[nShrunkLineNumber] = tBuffer.tTextColors[nLineNumber]:sub (nLeftConstraint, nRightConstraint)
tShrunkBuffer.tBackColors[nShrunkLineNumber] = tBuffer.tBackColors[nLineNumber]:sub (nLeftConstraint, nRightConstraint)
end
nShrunkLineNumber = nShrunkLineNumber + 1
end
tShrunkBuffer.nHeight = getTableSize (tShrunkBuffer.tText)
return tShrunkBuffer
end
function openCenter (tBuffer, nRunTime, nModifier)
local nModifier = nModifier or 1
local nRequiredIterations = math.min (tBuffer.nWidth, tBuffer.nHeight)
local nSleepTime = nRunTime / nRequiredIterations
– Step needs to be the amount of growth per sleep time to make it under the run time!
local nWidthStep = tBuffer.nWidth / nRequiredIterations
local nHeightStep = tBuffer.nHeight / nRequiredIterations
local nWidthScale = nWidthStep
local nHeightScale = nHeightStep
for nTime = 0, nRunTime, nSleepTime * nModifier do
local tShrunkBuffer = setmetatable (getShrunkBuffer (tBuffer, nWidthScale, nHeightScale), { __index = tBuffer })
if #tShrunkBuffer.tText > 0 then
tShrunkBuffer.x = tBuffer.nWidth / 2 - tShrunkBuffer.tText[1]:len() / 2
tShrunkBuffer.y = tBuffer.nHeight / 2 - tShrunkBuffer.nHeight / 2 + (tBuffer.nHeight % 2 == 0 and 1 or 0)
tBuffer.render (tShrunkBuffer)
else
break
end
sleep (nSleepTime)
nWidthScale = nWidthScale + nWidthStep * nModifier
nHeightScale = nHeightScale + nHeightStep * nModifier
end
return tBuffer:render()
end
You would then call:
Animate.openCenter (tBuffer, 0.5, 1)
for a fairly slow animation, but by adding a modifier and reducing the run time:
Animate.openCenter (tBuffer, 0.1, 3.6)
we can create an animation that is very quick, yet still visually appealing. (This will look similar to the animations seen in oeed's OneOS as things open and close, but there is much, much more potential in the ability of you, the user, to create something cool from this template or from scratch! :D/>)
Buffer Pastebin
Animation Pastebin
As always, you're welcome to use this code and modify it as long as credit is given where credit is due.
Thanks for reading!