video unit: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: video unit
| Old | New | Line | |
|---|---|---|---|
| 1 | + | The `video` unit in conjunction with the [[keyboard unit|keyboard]] 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]]. | |
| 2 | + | ||
| 3 | + | ## functionality | |
| 4 | + | ||
| 5 | + | 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. | |
| 6 | + | ||
| 7 | + | ## example | |
| 8 | + | ||
| 9 | + | Using the following enhancement unit: | |
| 10 | + | ||
| 11 | + | ``` pascal | |
| 12 | + | unit videoEnhanced; | |
| 13 | + | {$modeSwitch typeHelpers+} | |
| 14 | + | interface | |
| 15 | + | uses | |
| 16 | + | video; | |
| 17 | + | type | |
| 18 | + | {$push} | |
| 19 | + | {$scopedEnums on} | |
| 20 | + | color = (black := video.black, blue := video.blue, green := video.green, | |
| 21 | + | cyan := video.cyan, red := video.red, magenta := video.magenta, | |
| 22 | + | brown := video.brown, lightGray := video.lightGray, | |
| 23 | + | darkGray := video.darkGray, lightBlue := video.lightBlue, | |
| 24 | + | lightGreen := video.lightGreen, lightCyan := video.lightCyan, | |
| 25 | + | lightRed := video.lightRed, lightMagenta := video.lightMagenta, | |
| 26 | + | yellow := video.yellow, white := video.white); | |
| 27 | + | {$pop} | |
| 28 | + | attribute = bitpacked record | |
| 29 | + | foreground: color; | |
| 30 | + | background: color.black..color.lightGray; | |
| 31 | + | blinking: Boolean; | |
| 32 | + | end; | |
| 33 | + | cell = packed record | |
| 34 | + | {$ifDef endian_big} | |
| 35 | + | display: attribute; | |
| 36 | + | character: char; | |
| 37 | + | {$else} | |
| 38 | + | character: char; | |
| 39 | + | display: attribute; | |
| 40 | + | {$endIf} | |
| 41 | + | end; | |
| 42 | + | {$if sizeOf(cell) <> sizeOf(tVideoCell)} | |
| 43 | + | {$fatal wrong cell size} | |
| 44 | + | {$endIf} | |
| 45 | + | dimension = 0..16382{$ifNDef CPU16}+16377{$endIf}; | |
| 46 | + | ||
| 47 | + | bufferHelper = type helper for pVideoBuf | |
| 48 | + | function getCell(const x, y: dimension): cell; | |
| 49 | + | procedure setCell(const x, y: dimension; const content: cell); | |
| 50 | + | property cell[x, y: dimension]: cell | |
| 51 | + | read getCell write setCell; | |
| 52 | + | end; | |
| 53 | + | ||
| 54 | + | implementation | |
| 55 | + | ||
| 56 | + | function bufferHelper.getCell(const x, y: dimension): cell; | |
| 57 | + | type | |
| 58 | + | cast = cell; // otherwise `cell` means the _property_ | |
| 59 | + | begin | |
| 60 | + | getCell := cast(self^[x + y * screenWidth]); | |
| 61 | + | end; | |
| 62 | + | ||
| 63 | + | procedure bufferHelper.setCell(const x, y: dimension; const content: cell); | |
| 64 | + | begin | |
| 65 | + | self^[x + y * screenWidth] := tVideoCell(content); | |
| 66 | + | end; | |
| 67 | + | end. | |
| 68 | + | ``` | |
| 69 | + | ||
| 70 | + | The following program displays a yellow D in the fifth column of the fifth row: | |
| 71 | + | ||
| 72 | + | ``` pascal | |
| 73 | + | program videoDemo(input, output, stdErr); | |
| 74 | + | uses | |
| 75 | + | video, videoEnhanced; | |
| 76 | + | var | |
| 77 | + | c: cell; | |
| 78 | + | begin | |
| 79 | + | initVideo; | |
| 80 | + | if errorCode <> errOK then | |
| 81 | + | begin | |
| 82 | + | halt(errorCode); | |
| 83 | + | end; | |
| 84 | + | ||
| 85 | + | c := default(cell); | |
| 86 | + | c.character := 'D'; | |
| 87 | + | c.display.foreground := color.yellow; | |
| 88 | + | videoBuf.cell[4, 4] := c; | |
| 89 | + | ||
| 90 | + | updateScreen(false); | |
| 91 | + | ||
| 92 | + | readLn; | |
| 93 | + | doneVideo; | |
| 94 | + | ClearScreen; // Clean up console upon exit | |
| 95 | + | end. | |
| 96 | + | ``` | |
| 97 | + | ||
| 98 | + | This demo fails to make range checks. This demo assumes colors are available. | |
| 99 | + | ||
| 100 | + | ## see also | |
| 101 | + | ||
| 102 | + | - [`video` unit documentation](https://www.freepascal.org/docs-html/rtl/video/index.html) | |
| 103 | + | - [[ncurses|nCurses]] | |
| 104 | + | - [[Free Vision]] | |
| 105 | + | - | |
| 106 | + | - | |
| 107 | + | - | |
| 108 | + | - [`txtVideo` unit](http://web.tiscali.it/licciardi/prog/txtvideo.pas), a more extensive unit providing convenient routines | |
| 109 | + | ||
| 110 | + | --- | |
| 111 | + | *From the Free Pascal wiki, page "video unit", as it stood on 2021-10-20 after a change by Ojz0r, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 112 | + | Category: Code | |
| 113 | + | Category: Units |
