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

Redirecting Term to a window?

Started by TheOddByte, 19 March 2014 - 05:46 PM
TheOddByte #1
Posted 19 March 2014 - 06:46 PM
Hello, I currently creating a window API and it feels that much of it got a little over my head :P/>
As the topic says I'm wondering how to redirect the term to write only in the window.
Here's an example of what a window looks like

local window = {
	title = "Testing Window";
	color = colors.white;
	top_background=colors.cyan;
	background = colors.white;
	sX=1; fX=20; sY=1; fY=10;
	resizable=true;
}
The function that initializes the window will calculate and get the window height & width. Then those numbers are inserted into the window so you can access them like this

-- window.w < Width of the window
-- window.h < Height of the window
Now I'm currently doing this when overriding term functions and are keeping this in a function called redirect in the API
I also have a function that restores the native functions so I can write normally.

term.setCursorPos = function( x, y )
	term.native.setCursorPos( (window.sX-1)+x, window.sY + x )
end
term.getSize = function()
	return window.w, window.h
end
But's that's all I'm currently doing since I'm unsure which functions I should override and how I would do this efficiently.
Help is much appreciated :)/>
Lyqyd #2
Posted 19 March 2014 - 06:52 PM
Essentially, you should create a full redirect object that draws into a buffer. Your buffer will keep track of what is on the screen; text, text/background color, size, cursor position, cursor blink, current text/background colors. Each window essentially is its own little screen, and then you can draw the contents of the buffer onto the actual screen whenever you want, usually when the program yields.
TheOddByte #3
Posted 21 March 2014 - 06:40 PM
Essentially, you should create a full redirect object that draws into a buffer. Your buffer will keep track of what is on the screen; text, text/background color, size, cursor position, cursor blink, current text/background colors. Each window essentially is its own little screen, and then you can draw the contents of the buffer onto the actual screen whenever you want, usually when the program yields.
To be honest I'm not sure how I would do this, Is there any example I can check out and see if I learn from it?
Lyqyd #4
Posted 21 March 2014 - 06:47 PM
My Framebuffer API is an example of creating a redirect that draws into a buffer, and includes code that will draw out the contents of a buffer to the screen.
TheOddByte #5
Posted 21 March 2014 - 07:13 PM
My Framebuffer API is an example of creating a redirect that draws into a buffer, and includes code that will draw out the contents of a buffer to the screen.
Thanks, Your was much easier to understand than Symmetrycs who I checked out.
But I'm just wondering what's the parameter current in the draw function for? And do you have a usage example?
Edited on 21 March 2014 - 06:27 PM
Lyqyd #6
Posted 21 March 2014 - 09:11 PM
That's for a feature I use in LyqydOS. The parameter is for a buffer containing what is already on the screen. If that parameter is provided, the drawing function will only write the lines that contain differences between the current screen contents and the buffer being written. I use that in the final step of my window compositing process, where I have a buffer that contains all of the windows stacked properly. The `current` parameter in that case is the composited screen buffer that I previously wrote to the screen. This helps reduce network latency and improves draw times.

A usage example can be found in the compositor API in the LyqydOS repository on my github, but it will likely be harder to follow than framebuffer.