video.tex 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. %
  2. % $Id$
  3. % This file is part of the FPC documentation.
  4. % Copyright (C) 1997, by Michael Van Canneyt
  5. %
  6. % The FPC documentation is free text; you can redistribute it and/or
  7. % modify it under the terms of the GNU Library General Public License as
  8. % published by the Free Software Foundation; either version 2 of the
  9. % License, or (at your option) any later version.
  10. %
  11. % The FPC Documentation is distributed in the hope that it will be useful,
  12. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. % Library General Public License for more details.
  15. %
  16. % You should have received a copy of the GNU Library General Public
  17. % License along with the FPC documentation; see the file COPYING.LIB. If not,
  18. % write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. % Boston, MA 02111-1307, USA.
  20. %
  21. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  22. %
  23. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  24. % The Video unit
  25. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  26. \chapter{The VIDEO unit}
  27. \FPCexampledir{videoex}
  28. The \file{Video} unit implements a screen access layer which is system
  29. independent. It can be used to write on the screen in a system-independent
  30. way, which should be optimal on all platforms for which the unit is
  31. implemented.
  32. The working of the \file{Video} is simple: After calling \seep{InitVideo},
  33. the array \var{VideoBuf} contains a representation of the video screen of
  34. size \var{ScreenWidth*ScreenHeight}, going from left to right and top to
  35. bottom when walking the array elements: \var{VideoBuf[0]} contains the
  36. character and color code of the top-left character on the screen.
  37. \var{VideoBuf[ScreenWidth]} contains the data for the character in the
  38. first column of the second row on the screen, and so on.
  39. To write to the 'screen', the text to be written should be written to the
  40. \var{VideoBuf} array. Calling \seep{UpdateScreen} will then cp the text to
  41. the screen in the most optimal way. (an example can be found further on).
  42. The color attribute is a combination of the foreground and background color,
  43. plus the blink bit. The bits describe the various color combinations:
  44. \begin{description}
  45. \item[bits 0-3] The foreground color. Can be set using all color constants.
  46. \item[bits 4-6] The background color. Can be set using a subset of the
  47. color constants.
  48. \item[bit 7] The blinking bit. If this bit is set, the character will appear
  49. blinking.
  50. \end{description}
  51. Each possible color has a constant associated with it, see page
  52. \pageref{vidcolorconst} for a list of constants.
  53. The contents of the \var{VideoBuf} array may be modified: This is 'writing'
  54. to the screen. As soon as everything that needs to be written in the array
  55. is in the \var{VideoBuf} array, calling \var{UpdateScreen} will copy the
  56. contents of the array screen to the screen, in a manner that is as efficient
  57. as possible.
  58. The updating of the screen can be prohibited to optimize performance; To
  59. this end, the \seep{LockScreenUpdate} function can be used: This will
  60. increment an internal counter. As long as the counter differs from zero,
  61. calling \seep{UpdateScreen} will not do anything. The counter can be
  62. lowered with \seep{UnlockScreenUpdate}. When it reaches zero, the next call
  63. to \seep{UpdateScreen} will actually update the screen. This is useful when
  64. having nested procedures that do a lot of screen writing.
  65. The video unit also presents an interface for custom screen drivers, thus
  66. it is possible to override the default screen driver with a custom screen
  67. driver, see the \seef{SetVideoDriver} call. The current video driver can
  68. be retrieved using the \seep{GetVideoDriver} call.
  69. \begin{remark}
  70. The video unit should {\em not} be used together with the \file{crt} unit.
  71. Doing so will result in very strange behaviour, possibly program crashes.
  72. \end{remark}
  73. \section{Constants, Type and variables }
  74. \subsection{Constants}
  75. \label{vidcolorconst}
  76. The following constants describe colors that can be used as
  77. foreground and background colors.
  78. \begin{verbatim}
  79. Black = 0;
  80. Blue = 1;
  81. Green = 2;
  82. Cyan = 3;
  83. Red = 4;
  84. Magenta = 5;
  85. Brown = 6;
  86. LightGray = 7;
  87. \end{verbatim}
  88. The following color constants can be used as foreground colors only:
  89. \begin{verbatim}
  90. DarkGray = 8;
  91. LightBlue = 9;
  92. LightGreen = 10;
  93. LightCyan = 11;
  94. LightRed = 12;
  95. LightMagenta = 13;
  96. Yellow = 14;
  97. White = 15;
  98. \end{verbatim}
  99. The foreground and background color can be combined to a color attribute
  100. with the following code:
  101. \begin{verbatim}
  102. Attr:=ForeGroundColor + (BackGroundColor shl 4);
  103. \end{verbatim}
  104. The color attribute can be logically or-ed with the blink attribute to
  105. produce a blinking character:
  106. \begin{verbatim}
  107. Blink = 128;
  108. \end{verbatim}
  109. But not all drivers may support this.
  110. The following constants describe the capabilities of a certain video mode:
  111. \begin{verbatim}
  112. cpUnderLine = $0001;
  113. cpBlink = $0002;
  114. cpColor = $0004;
  115. cpChangeFont = $0008;
  116. cpChangeMode = $0010;
  117. cpChangeCursor = $0020;
  118. \end{verbatim}
  119. The following constants describe the various supported cursor modes:
  120. \begin{verbatim}
  121. crHidden = 0;
  122. crUnderLine = 1;
  123. crBlock = 2;
  124. crHalfBlock = 3;
  125. \end{verbatim}
  126. When a video function needs to report an error condition, the following
  127. constants are used:
  128. \begin{verbatim}
  129. vioOK = 0;
  130. errVioBase = 1000;
  131. errVioInit = errVioBase + 1; { Initialization error}
  132. errVioNotSupported = errVioBase + 2; { Unsupported function }
  133. errVioNoSuchMode = errVioBase + 3; { No such video mode }
  134. \end{verbatim}
  135. The following constants can be read to get some information about the
  136. current screen:
  137. \begin{verbatim}
  138. ScreenWidth : Word = 0;
  139. ScreenHeight : Word = 0;
  140. LowAscii : Boolean = true;
  141. NoExtendedFrame : Boolean = false;
  142. FVMaxWidth = 132;
  143. \end{verbatim}
  144. The error-handling code uses the following constants:
  145. \begin{verbatim}
  146. errOk = 0;
  147. ErrorCode: Longint = ErrOK;
  148. ErrorInfo: Pointer = nil;
  149. ErrorHandler: TErrorHandler = DefaultErrorHandler;
  150. \end{verbatim}
  151. The \var{ErrorHandler} variable can be set to a custom-error handling
  152. function. It is set by default to the \seep{DefaultErrorHandler} function.
  153. \subsection{Types}
  154. The \var{TVideoMode} record describes a videomode. Its fields are
  155. self-explaining: \var{Col,Row} describe the number of columns and
  156. rows on the screen for this mode. \var{Color} is \var{True} if this mode
  157. supports colors, or \var{False} if not.
  158. \begin{verbatim}
  159. PVideoMode = ^TVideoMode;
  160. TVideoMode = record
  161. Col,Row : Word;
  162. Color : Boolean;
  163. end;
  164. \end{verbatim}
  165. \var{TVideoCell} describes one character on the screen. The high byte
  166. contains the color attribute with which the character is drawn on the screen,
  167. and the low byte contains the ASCII code of the character to be drawn.
  168. \begin{verbatim}
  169. TVideoCell = Word;
  170. PVideoCell = ^TVideoCell;
  171. \end{verbatim}
  172. The \var{TVideoBuf} and \var{PVideoBuf} are two types used to represent the
  173. screen.
  174. \begin{verbatim}
  175. TVideoBuf = array[0..32759] of TVideoCell;
  176. PVideoBuf = ^TVideoBuf;
  177. \end{verbatim}
  178. The following type is used when reporting error conditions:
  179. \begin{verbatim}
  180. TErrorHandlerReturnValue = (errRetry, errAbort, errContinue);
  181. \end{verbatim}
  182. Here, \var{errRetry} means retry the operation, \var{errAbort}
  183. abort and return error code and \var{errContinue} means abort
  184. without returning an errorcode.
  185. The \var{TErrorHandler} function is used to register an own error
  186. handling function. It should be used when installing a custom error
  187. handling function, and must return one of the above values.
  188. \begin{verbatim}
  189. TErrorHandler =
  190. function (Code: Longint; Info: Pointer): TErrorHandlerReturnValue;
  191. \end{verbatim}
  192. \var{Code} should contain the error code for the error condition,
  193. and the \var{Info} parameter may contain any data type specific to
  194. the error code passed to the function.
  195. The \var{TVideoDriver} record can be used to install a custom video
  196. driver, with the \seef{SetVideoDriver} call:
  197. \begin{verbatim}
  198. TVideoDriver = Record
  199. InitDriver : Procedure;
  200. DoneDriver : Procedure;
  201. UpdateScreen : Procedure(Force : Boolean);
  202. ClearScreen : Procedure;
  203. SetVideoMode : Function (Const Mode : TVideoMode) : Boolean;
  204. GetVideoModeCount : Function : Word;
  205. GetVideoModeData : Function(Index : Word; Var Data : TVideoMode) : Boolean;
  206. SetCursorPos : procedure (NewCursorX, NewCursorY: Word);
  207. GetCursorType : function : Word;
  208. SetCursorType : procedure (NewType: Word);
  209. GetCapabilities : Function : Word;
  210. end;
  211. \end{verbatim}
  212. \subsection{Variables}
  213. The following variables contain information about the current screen
  214. status:
  215. \begin{verbatim}
  216. ScreenColor : Boolean;
  217. CursorX, CursorY : Word;
  218. \end{verbatim}
  219. \var{ScreenColor} indicates whether the current screen supports colors.
  220. \var{CursorX,CursorY} contain the current cursor position.
  221. The following variables form the heart of the \file{Video} unit: The
  222. \var{VideoBuf} array represents the physical screen. Writing to this
  223. array and calling \seep{UpdateScreen} will write the actual characters
  224. to the screen. \var{VideoBufSize} contains the actual screen size, and is
  225. equal to the product of the number of columns times the number of lines
  226. on the screen (\var{ScreenWidth*ScreenHeight}).
  227. \begin{verbatim}
  228. VideoBuf : PVideoBuf;
  229. OldVideoBuf : PVideoBuf;
  230. VideoBufSize : Longint;
  231. \end{verbatim}
  232. The \var{OldVideoBuf} contains the state of the video screen after the last
  233. screen update. The \seep{UpdateScreen} function uses this array to decide
  234. which characters on screen should be updated, and which not.
  235. Note that the \var{OldVideoBuf} array may be ignored by some drivers, so
  236. it should not be used. The Array is in the interface section of the video
  237. unit mainly so drivers that need it can make use of it.
  238. \section{Functions and Procedures}
  239. The examples in this section make use of the unit \file{vidutil}, which
  240. contains the \var{TextOut} function. This function writes a text to the
  241. screen at a given location. It looks as follows:
  242. \FPCexample{vidutil}
  243. \begin{procedure}{ClearScreen}
  244. \Declaration
  245. procedure ClearScreen;
  246. \Description
  247. \var{ClearScreen} clears the entire screen, and calls \seep{UpdateScreen}
  248. after that. This is done by writing spaces to all character cells of the
  249. video buffer in the default color (lightgray on black, color attribute \$07).
  250. \Errors
  251. None.
  252. \SeeAlso
  253. \seep{InitVideo}, \seep{UpdateScreen}
  254. \end{procedure}
  255. \FPCexample{ex3}
  256. \begin{procedure}{DefaultErrorHandler}
  257. \Declaration
  258. function DefaultErrorHandler(AErrorCode: Longint; AErrorInfo: Pointer): TErrorHandlerReturnValue;
  259. \Description
  260. \var{DefaultErrorHandler} is the default error handler used by the video
  261. driver. It simply sets the error code \var{AErrorCode} and \var{AErrorInfo}
  262. in the global variables \var{ErrorCode} and \var{ErrorInfo} and returns
  263. \var{errContinue}.
  264. \Errors
  265. None.
  266. \SeeAlso
  267. \end{procedure}
  268. \begin{procedure}{DoneVideo}
  269. \Declaration
  270. procedure DoneVideo;
  271. \Description
  272. \var{DoneVideo} disables the Video driver if the video driver is active. If
  273. the videodriver was already disabled or not yet initialized, it does
  274. nothing. Disabling the driver means it will clean up any allocated
  275. resources, possibly restore the screen in the state it was before
  276. \var{InitVideo} was called. Particularly, the \var{VideoBuf} and
  277. \var{OldVideoBuf} arrays are no longer valid after a call to
  278. \var{DoneVideo}.
  279. The \var{DoneVideo} should always be called if \var{InitVideo} was called.
  280. Failing to do so may leave the screen in an unusable state after the program
  281. exits.
  282. \Errors
  283. Normally none. If the driver reports an error, this is done through the
  284. \var{ErrorCode} variable.
  285. \SeeAlso
  286. \seep{InitVideo}
  287. \end{procedure}
  288. For an example, see most other functions.
  289. \begin{function}{GetCapabilities}
  290. \Declaration
  291. function GetCapabilities: Word;
  292. \Description
  293. \var{GetCapabilities} returns the capabilities of the current driver.
  294. It is an or-ed combination of the following constants:
  295. \begin{description}
  296. \item[cpUnderLine] The driver supports underlined characters.
  297. \item[cpBlink] The driver supports blinking characters.
  298. \item[cpColor] The driver supports colors.
  299. \item[cpChangeFont] The driver supports the setting of a screen font.
  300. Note, however, that a font setting API is not supported by the video unit.
  301. \item[cpChangeMode] The driver supports the setting of screen modes.
  302. \item[cpChangeCursor] The driver supports changing the cursor shape.
  303. \end{description}
  304. Note that the video driver should not yet be initialized to use this
  305. function. It is a property of the driver.
  306. \Errors
  307. None.
  308. \SeeAlso
  309. \seef{GetCursorType}, \seep{GetVideoDriver}
  310. \end{function}
  311. \FPCexample{ex4}
  312. \begin{function}{GetCursorType}
  313. \Declaration
  314. function GetCursorType: Word;
  315. \Description
  316. \var{GetCursorType} returns the current cursor type. It is one of the
  317. following values:
  318. \begin{description}
  319. \item[crHidden] The cursor is currently hidden.
  320. \item[crUnderLine] The cursor is currently the underline character.
  321. \item[crBlock] The cursor is currently the block character.
  322. \item[crHalfBlock] The cursur is currently a block with height of half the
  323. character cell height.
  324. \end{description}
  325. Note that not all drivers support all types of cursors.
  326. \Errors
  327. None.
  328. \SeeAlso
  329. \seep{SetCursorType}, \seef{GetCapabilities}
  330. \end{function}
  331. \FPCexample{ex5}
  332. \begin{function}{GetLockScreenCount}
  333. \Declaration
  334. Function GetLockScreenCount : integer;
  335. \Description
  336. \var{GetLockScreenCount} returns the current lock level. When the lock
  337. level is zero, a call to \seep{UpdateScreen} will actually update the
  338. screen.
  339. \Errors
  340. None.
  341. \SeeAlso
  342. \seep{LockScreenUpdate}, \seep{UnlockScreenUpdate}, \seep{UpdateScreen}
  343. \end{function}
  344. \FPCexample{ex6}
  345. \begin{procedure}{GetVideoDriver}
  346. \Declaration
  347. Procedure GetVideoDriver (Var Driver : TVideoDriver);
  348. \Declaration
  349. \var{GetVideoDriver} retrieves the current videodriver and returns it in
  350. \var{Driver}. This can be used to chain video drivers.
  351. \Errors
  352. None.
  353. \SeeAlso
  354. \seef{SetVideoDriver}
  355. \end{procedure}
  356. For an example, see the section on writing a custom video driver.
  357. \begin{procedure}{GetVideoMode}
  358. \Declaration
  359. procedure GetVideoMode(var Mode: TVideoMode);
  360. \Description
  361. \var{GetVideoMode} returns the settings of the currently active video mode.
  362. The \var{row,col} fields indicate the dimensions of the current video mode,
  363. and \var{Color} is true if the current video supports colors.
  364. \Errors
  365. None.
  366. \SeeAlso
  367. \seef{SetVideoMode}, \seef{GetVideoModeData}
  368. \end{procedure}
  369. \FPCexample{ex7}
  370. \begin{function}{GetVideoModeCount}
  371. \Declaration
  372. Function GetVideoModeCount : Word;
  373. \Description
  374. \var{GetVideoModeCount} returns the number of video modes that the current
  375. driver supports. If the driver does not support switching of modes, then 1
  376. is returned.
  377. This function can be used in conjunction with the \seef{GetVideoModeData}
  378. function to retrieve data for the supported video modes.
  379. \Errors
  380. None.
  381. \SeeAlso
  382. \seef{GetVideoModeData}, \seep{GetVideoMode}
  383. \end{function}
  384. \FPCexample{ex8}
  385. \begin{function}{GetVideoModeData}
  386. \Declaration
  387. Function GetVideoModeData(Index : Word; Var Data: TVideoMode) : Boolean;
  388. \Description
  389. \var{GetVideoModeData} returns the characteristics of the \var{Index}-th
  390. video mode in \var{Data}. \var{Index} is zero based, and has a maximum value of
  391. \var{GetVideoModeCount-1}. If the current driver does not support setting of
  392. modes (\var{GetVideoModeCount=1}) and \var{Index} is zero, the current mode
  393. is returned.
  394. The function returns \var{True} if the mode data was retrieved succesfully,
  395. \var{False} otherwise.
  396. \Errors
  397. In case \var{Index} has a wrong value, \var{False} is returned.
  398. \SeeAlso
  399. \seef{GetVideoModeCount}, \seef{SetVideoMode}, \seep{GetVideoMode}
  400. \end{function}
  401. For an example, see \seef{GetVideoModeCount}.
  402. \begin{procedure}{InitVideo}
  403. \Declaration
  404. procedure InitVideo;
  405. \Description
  406. \var{InitVideo} Initializes the video subsystem. If the video system was
  407. already initialized, it does nothing.
  408. After the driver has been initialized, the \var{VideoBuf} and \var{OldVideoBuf}
  409. pointers are initialized, based on the \var{ScreenWidth} and
  410. \var{ScreenHeight} variables. When this is done, the screen is cleared.
  411. \Errors
  412. if the driver fails to initialize, the \var{ErrorCode} variable is set.
  413. \SeeAlso
  414. \seep{DoneVideo}
  415. \end{procedure}
  416. For an example, see most other functions.
  417. \begin{procedure}{LockScreenUpdate}
  418. \Declaration
  419. Procedure LockScreenUpdate;
  420. \Description
  421. \var{LockScreenUpdate} increments the screen update lock count with one.
  422. As long as the screen update lock count is not zero, \seep{UpdateScreen}
  423. will not actually update the screen.
  424. This function can be used to optimize screen updating: If a lot of writing
  425. on the screen needs to be done (by possibly unknown functions), calling
  426. \var{LockScreenUpdate} before the drawing, and \seep{UnlockScreenUpdate}
  427. after the drawing, followed by a \seep{UpdateScreen} call, all writing will
  428. be shown on screen at once.
  429. \Errors
  430. None.
  431. \SeeAlso
  432. \seep{UpdateScreen}, \seep{UnlockScreenUpdate}, \seef{GetLockScreenCount}
  433. \end{procedure}
  434. For an example, see \seef{GetLockScreenCount}.
  435. \begin{procedure}{SetCursorPos}
  436. \Declaration
  437. procedure SetCursorPos(NewCursorX, NewCursorY: Word);
  438. \Description
  439. \var{SetCursorPos} positions the cursor on the given position: Column
  440. \var{NewCursorX} and row \var{NewCursorY}. The origin of the screen is the
  441. upper left corner, and has coordinates \var{(0,0)}.
  442. The current position is stored in the \var{CursorX} and \var{CursorY}
  443. variables.
  444. \Errors
  445. None.
  446. \SeeAlso
  447. \seep{SetCursorType}
  448. \end{procedure}
  449. \FPCexample{ex2}
  450. \begin{procedure}{SetCursorType}
  451. \Declaration
  452. procedure SetCursorType(NewType: Word);
  453. \Description
  454. \var{SetCursorType} sets the cursor to the type specified in \var{NewType}.
  455. \begin{description}
  456. \item[crHidden] the cursor is not visible.
  457. \item[crUnderLine] the cursor is a small underline character (usually
  458. denoting insert mode).
  459. \item[crBlock] the cursor is a block the size of a screen cell (usually
  460. denoting overwrite mode).
  461. \item[crHalfBlock] the cursor is a block half the size of a screen cell.
  462. \end{description}
  463. \Errors
  464. None.
  465. \SeeAlso
  466. \seep{SetCursorPos}
  467. \end{procedure}
  468. \begin{function}{SetVideoDriver}
  469. \Declaration
  470. Function SetVideoDriver (Const Driver : TVideoDriver) : Boolean;
  471. \Description
  472. \var{SetVideoDriver} sets the videodriver to be used to \var{Driver}. If
  473. the current videodriver is initialized (after a call to \var{InitVideo})
  474. then it does nothing and returns \var{False}.
  475. A new driver can only be installed if the previous driver was not yet
  476. activated (i.e. before a call to \seep{InitVideo}) or after it was
  477. deactivated (i.e after a call to \var{DoneVideo}).
  478. For more information about installing a videodriver, see \sees{viddriver}.
  479. \Errors
  480. If the current driver is initialized, then \var{False} is returned.
  481. \SeeAlso
  482. The example video driver in \sees{viddriver}
  483. \end{function}
  484. For an example, see the section on writing a custom video driver.
  485. \begin{function}{SetVideoMode}
  486. \Declaration
  487. Function SetVideoMode(Mode: TVideoMode) : Boolean;
  488. \Description
  489. \var{SetVideoMode} sets the video mode to the mode specified in \var{Mode}:
  490. \begin{verbatim}
  491. TVideoMode = record
  492. Col,Row : Word;
  493. Color : Boolean;
  494. end;
  495. \end{verbatim}
  496. If the call was succesful, then the screen will have \var{Col} columns and
  497. \var{Row} rows, and will be displaying in color if \var{Color} is
  498. \var{True}.
  499. The function returns \var{True} if the mode was set succesfully, \var{False}
  500. otherwise.
  501. Note that the video mode may not always be set. E.g. a console on Linux
  502. or a telnet session cannot always set the mode. It is important to check
  503. the error value returned by this function if it was not succesful.
  504. The mode can be set when the video driver has not yet been initialized
  505. (i.e. before \seep{InitVideo} was called) In that case, the video mode will
  506. be stored, and after the driver was initialized, an attempt will be made to
  507. set the requested mode. Changing the video driver before the call to
  508. \var{InitVideo} will clear the stored video mode.
  509. To know which modes are valid, use the \seef{GetVideoModeCount} and
  510. \seef{GetVideoModeData} functions. To retrieve the current video mode,
  511. use the \seep{GetVideoMode} procedure.
  512. \Errors
  513. If the specified mode cannot be set, then \var{errVioNoSuchMode} may be set
  514. in \var{ErrorCode}
  515. \SeeAlso
  516. \seef{GetVideoModeCount}
  517. \seef{GetVideoModeData}
  518. \seep{GetVideoMode}
  519. \end{function}
  520. \begin{procedure}{UnlockScreenUpdate}
  521. \Declaration
  522. Procedure UnlockScreenUpdate;
  523. \Description
  524. \var{UnlockScreenUpdate} decrements the screen update lock count with one if
  525. it is larger than zero. When the lock count reaches zero, the
  526. \seep{UpdateScreen} will actually update the screen. No screen update will
  527. be performed as long as the screen update lock count is nonzero. This
  528. mechanism can be used to increase screen performance in case a lot of
  529. writing is done.
  530. It is important to make sure that each call to \seep{LockScreenUpdate} is
  531. matched by exactly one call to \var{UnlockScreenUpdate}
  532. \Errors
  533. None.
  534. \SeeAlso
  535. \seep{LockScreenUpdate}, \seef{GetLockScreenCount}, \seep{UpdateScreen}
  536. \end{procedure}
  537. For an example, see \seef{GetLockScreenCount}.
  538. \begin{procedure}{UpdateScreen}
  539. \Declaration
  540. procedure UpdateScreen(Force: Boolean);
  541. \Description
  542. \var{UpdateScreen} synchronizes the actual screen with the contents
  543. of the \var{VideoBuf} internal buffer. The parameter \var{Force}
  544. specifies whether the whole screen has to be redrawn (\var{Force=True})
  545. or only parts that have changed since the last update of the screen.
  546. The \var{Video} unit keeps an internal copy of the screen as it last
  547. wrote it to the screen (in the \var{OldVideoBuf} array). The current
  548. contents of \var{VideoBuf} are examined to see what locations on the
  549. screen need to be updated. On slow terminals (e.g. a \linux telnet
  550. session) this mechanism can speed up the screen redraw considerably.
  551. \Errors
  552. None.
  553. \SeeAlso
  554. \seep{ClearScreen}
  555. \end{procedure}
  556. For an example, see most other functions.
  557. \section{Writing a custom video driver}
  558. \label{se:viddriver}
  559. Writing a custom video driver is not difficult, and generally means
  560. implementing a couple of functions, which whould be registered with
  561. the \seef{SetVideoDriver} function. The various functions that can be
  562. implemented are located in the \var{TVideoDriver} record:
  563. \begin{verbatim}
  564. TVideoDriver = Record
  565. InitDriver : Procedure;
  566. DoneDriver : Procedure;
  567. UpdateScreen : Procedure(Force : Boolean);
  568. ClearScreen : Procedure;
  569. SetVideoMode : Function (Const Mode : TVideoMode) : Boolean;
  570. GetVideoModeCount : Function : Word;
  571. GetVideoModeData : Function(Index : Word; Var Data : TVideoMode) : Boolean;
  572. SetCursorPos : procedure (NewCursorX, NewCursorY: Word);
  573. GetCursorType : function : Word;
  574. SetCursorType : procedure (NewType: Word);
  575. GetCapabilities : Function : Word;
  576. end;
  577. \end{verbatim}
  578. Not all of these functions must be implemented. In fact, the only absolutely
  579. necessary function to write a functioning driver is the \var{UpdateScreen}
  580. function. The general calls in the \file{Video} unit will check which
  581. functionality is implemented by the driver.
  582. The functionality of these calls is the same as the functionality of the
  583. calls in the video unit, so the expected behaviour can be found in the
  584. previous section. Some of the calls, however, need some additional remarks.
  585. \begin{description}
  586. \item[InitDriver] Called by \var{InitVideo}, this function should initialize
  587. any data structures needed for the functionality of the driver, maybe do some
  588. screen initializations. The function is guaranteed to be called only once; It
  589. can only be called again after a call to \var{DoneVideo}. The variables
  590. \var{ScreenWidth} and \var{ScreenHeight} should be initialized correctly
  591. after a call to this function, as the \var{InitVideo} call will initialize
  592. the \var{VideoBuf} and \var{OldVideoBuf} arrays based on their values.
  593. \item[DoneDriver] This should clean up any structures that have been
  594. initialized in the \var{InitDriver} function. It should possibly also
  595. restore the screen as it was before the driver was initialized. The VideoBuf
  596. and \var{OldVideoBuf} arrays will be disposed of by the general \var{DoneVideo}
  597. call.
  598. \item[UpdateScreen] This is the only required function of the driver. It
  599. should update the screen based on the \var{VideoBuf} array's contents. It
  600. can optimize this process by comparing the values with values in the
  601. \var{OldVideoBuf} array. After updating the screen, the \var{UpdateScreen}
  602. procedure should update the \var{OldVideoBuf} by itself. If the \var{Force}
  603. parameter is \var{True}, the whole screen should be updated, not just the
  604. changed values.
  605. \item[ClearScreen] If there is a faster way to clear the screen than to
  606. write spaces in all character cells, then it can be implemented here. If the
  607. driver does not implement this function, then the general routines will
  608. write spaces in all video cells, and will call \var{UpdateScreen(True)}.
  609. \item[SetVideoMode] Should set the desired video mode, if available. It
  610. should return \var{True} if the mode was set, \var{False} if not.
  611. \item[GetVideoModeCount] Should return the number of supported video modes.
  612. If no modes are supported, this function should not be implemented; the
  613. general routines will return 1. (for the current mode)
  614. \item[GetVideoModeData] Should return the data for the \var{Index}-th mode;
  615. \var{Index} is zero based. The function should return true if the data was
  616. returned correctly, false if \var{Index} contains an invalid index.
  617. If this is not implemented, then the general routine will return the current
  618. video mode when \var{Index} equals 0.
  619. \item[GetCapabilities] If this function is not implemented, zero (i.e.
  620. no capabilities) will be returned by the general function.
  621. \end{description}
  622. The following unit shows how to override a video driver, with a driver
  623. that writes debug information to a file.
  624. \FPCexample{viddbg}
  625. The unit can be used in any of the demonstration programs, by simply
  626. including it in the \var{uses} clause. Setting \var{DetailedVideoLogging} to
  627. \var{True} will create a more detailed log (but will also slow down
  628. functioning)