Difference between revisions of "video unit"

From Lazarus wiki
Jump to navigationJump to search
(→‎example: improvement of code, allegedly insert new external links [spam detection false positive])
(→‎see also: add link to txtVideo.pas from the “contributed units” section on freepascal.org)
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
The <syntaxhighlight lang="pascal" inline>video</syntaxhighlight> unit in conjunction with the [[keyboard|<syntaxhighlight lang="pascal" inline>keyboard</syntaxhighlight>]] unit provide a system-independent way to program interactions with the console, a text terminal.
+
The <syntaxhighlight lang="pascal" inline>video</syntaxhighlight> unit in conjunction with the [[keyboard unit|<syntaxhighlight lang="pascal" inline>keyboard</syntaxhighlight>]] unit provide a system-independent way to program interactions with the console, a text terminal.
 
It is part of the [[FPC]]’s standard [[RTL|run-time library]].
 
It is part of the [[FPC]]’s standard [[RTL|run-time library]].
  
Line 77: Line 77:
 
begin
 
begin
 
initVideo;
 
initVideo;
 +
if errorCode <> errOK then
 +
begin
 +
halt(errorCode);
 +
end;
 
 
 
c := default(cell);
 
c := default(cell);
Line 99: Line 103:
 
* {{Forum topic|24187|Forum topic “Video unit and writeln color?”}}
 
* {{Forum topic|24187|Forum topic “Video unit and writeln color?”}}
 
* {{Forum topic|53072|Forum topic “Video unit and updating the screen”|392128}}
 
* {{Forum topic|53072|Forum topic “Video unit and updating the screen”|392128}}
 +
* [http://web.tiscali.it/licciardi/prog/txtvideo.pas <syntaxhighlight lang="pascal" inline>txtVideo</syntaxhighlight> unit], a more extensive unit providing convenient routines
  
 
[[Category: Code]]
 
[[Category: Code]]
 
[[Category: Units]]
 
[[Category: Units]]

Revision as of 15:32, 7 February 2021

The video unit in conjunction with the keyboard unit provide a system-independent way to program interactions with the console, a text terminal. It is part of the FPC’s standard run-time library.

functionality

The provided routines are rather low-level. There is no facility to just print a string at a certain position. Instead, the video unit maintains a buffer of the entire screen. It records every cell’s contents and possibly attributes like colors. The buffer then needs to be written with the updateScreen procedure.

example

Using the following enhancement unit:

unit videoEnhanced;
{$modeSwitch typeHelpers+}
interface
uses
	video;
type
	{$push}
	{$scopedEnums on}
	color = (black := video.black, blue := video.blue, green := video.green,
		cyan := video.cyan, red := video.red, magenta := video.magenta,
		brown := video.brown, lightGray := video.lightGray,
		darkGray := video.darkGray, lightBlue := video.lightBlue,
		lightGreen := video.lightGreen, lightCyan := video.lightCyan,
		lightRed := video.lightRed, lightMagenta := video.lightMagenta,
		yellow := video.yellow, white := video.white);
	{$pop}
	attribute = bitpacked record
			foreground: color;
			background: color.black..color.lightGray;
			blinking: Boolean;
		end;
	cell = packed record
			{$ifDef endian_big}
				display: attribute;
				character: char;
			{$else}
				character: char;
				display: attribute;
			{$endIf}
		end;
	{$if sizeOf(cell) <> sizeOf(tVideoCell)}
		{$fatal wrong cell size}
	{$endIf}
	dimension = 0..16382{$ifNDef CPU16}+16377{$endIf};
	
	bufferHelper = type helper for pVideoBuf
			function getCell(const x, y: dimension): cell;
			procedure setCell(const x, y: dimension; const content: cell);
			property cell[x, y: dimension]: cell
				read getCell write setCell;
		end;

implementation

function bufferHelper.getCell(const x, y: dimension): cell;
type
	cast = cell; // otherwise `cell` means the _property_
begin
	getCell := cast(self^[x + y * screenWidth]);
end;

procedure bufferHelper.setCell(const x, y: dimension; const content: cell);
begin
	self^[x + y * screenWidth] := tVideoCell(content);
end;
end.

The following program displays a yellow D in the fifth column of the fifth row:

program videoDemo(input, output, stdErr);
uses
	video, videoEnhanced;
var
	c: cell;
begin
	initVideo;
	if errorCode <> errOK then
	begin
		halt(errorCode);
	end;
	
	c := default(cell);
	c.character := 'D';
	c.display.foreground := color.yellow;
	videoBuf.cell[4, 4] := c;
	
	updateScreen(false);
	
	readLn;
	doneVideo;
end.

This demo fails to make range checks. This demo assumes colors are available.

see also