Difference between revisions of "video unit"
From Free Pascal wiki
Jump to navigationJump to searchm (link, allegedly insert new external links [spam detection false positive]) |
m (→example) |
||
(2 intermediate revisions by one other user not shown) | |||
Line 77: | Line 77: | ||
begin | begin | ||
initVideo; | initVideo; | ||
+ | if errorCode <> errOK then | ||
+ | begin | ||
+ | halt(errorCode); | ||
+ | end; | ||
c := default(cell); | c := default(cell); | ||
Line 87: | Line 91: | ||
readLn; | readLn; | ||
doneVideo; | doneVideo; | ||
+ | ClearScreen; // Clean up console upon exit | ||
end. | end. | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 99: | Line 104: | ||
* {{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]] |
Latest revision as of 08:42, 20 October 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;
ClearScreen; // Clean up console upon exit
end.
This demo fails to make range checks. This demo assumes colors are available.
see also
video
unit documentationnCurses
- Free Vision
- Forum topic “How to change a character's text and background color in video unit?”
- Forum topic “Video unit and writeln color?”
- Forum topic “Video unit and updating the screen”
txtVideo
unit, a more extensive unit providing convenient routines