video.tex 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. \section{Constants, Type and variables }
  33. \subsection{Constants}
  34. The following constants describe colors that can be used as
  35. foreground and background colors.
  36. \begin{verbatim}
  37. Black = 0;
  38. Blue = 1;
  39. Green = 2;
  40. Cyan = 3;
  41. Red = 4;
  42. Magenta = 5;
  43. Brown = 6;
  44. LightGray = 7;
  45. \end{verbatim}
  46. The following color constants can be used only as foreground colors:
  47. \begin{verbatim}
  48. DarkGray = 8;
  49. LightBlue = 9;
  50. LightGreen = 10;
  51. LightCyan = 11;
  52. LightRed = 12;
  53. LightMagenta = 13;
  54. Yellow = 14;
  55. White = 15;
  56. \end{verbatim}
  57. The foreground color can be logically or-ed with the blink attribute:
  58. \begin{verbatim}
  59. Blink = 128;
  60. \end{verbatim}
  61. The following constants describe the capabilities of a certain video mode:
  62. \begin{verbatim}
  63. cpUnderLine = $0001;
  64. cpBlink = $0002;
  65. cpColor = $0004;
  66. cpChangeFont = $0008;
  67. cpChangeMode = $0010;
  68. cpChangeCursor = $0020;
  69. \end{verbatim}
  70. The following constants describe the various supported cursor modes:
  71. \begin{verbatim}
  72. crHidden = 0;
  73. crUnderLine = 1;
  74. crBlock = 2;
  75. crHalfBlock = 3;
  76. \end{verbatim}
  77. When a video function needs to report an error condition, the following
  78. constants are used:
  79. \begin{verbatim}
  80. vioOK = 0;
  81. errVioBase = 1000;
  82. errVioInit = errVioBase + 1; { Initialization error}
  83. errVioNotSupported = errVioBase + 2; { Unsupported function }
  84. errVioNoSuchMode = errVioBase + 3; { No such video mode }
  85. \end{verbatim}
  86. The following constants can be read to get some information about the
  87. current screen:
  88. \begin{verbatim}
  89. ScreenWidth : Word = 0;
  90. ScreenHeight : Word = 0;
  91. LowAscii : Boolean = true;
  92. NoExtendedFrame : Boolean = false;
  93. FVMaxWidth = 132;
  94. \end{verbatim}
  95. The error-handling code uses the following constants:
  96. \begin{verbatim}
  97. errOk = 0;
  98. ErrorCode: Longint = ErrOK;
  99. ErrorInfo: Pointer = nil;
  100. ErrorHandler: TErrorHandler = DefaultErrorHandler;
  101. \end{verbatim}
  102. The \var{ErrorHandler} variable can be set to a custom-error handling
  103. function. It is set by default to the \seef{DefaultErrorHandler} function.
  104. The \var{Modes} list contains the list of supported video modes:
  105. \begin{verbatim}
  106. Modes: PVideoModeList = nil;
  107. \end{verbatim}
  108. \subsection{Types}
  109. The \var{TVideoMode} record describes a videomode. Its fields are
  110. self-explaining. The \var{TVideoModeSelector} procedural variable
  111. is used to select a video mode.
  112. \begin{verbatim}
  113. PVideoMode = ^TVideoMode;
  114. TVideoMode = record
  115. Col,Row : Word;
  116. Color : Boolean;
  117. end;
  118. TVideoModeSelector = function (const VideoMode: TVideoMode; Params: Longint): Boolean;
  119. \end{verbatim}
  120. \var{TVideoCell} describes one character on the screen. The high byte
  121. contains the color attribute with which the character is drawn on the screen,
  122. and the low byte contains the ASCII code of the character to be drawn.
  123. \begin{verbatim}
  124. TVideoCell = Word;
  125. PVideoCell = ^TVideoCell;
  126. \end{verbatim}
  127. The \var{TVideoBuf} and \var{PVideoBuf} are two types used to represent the
  128. screen.
  129. \begin{verbatim}
  130. TVideoBuf = array[0..32759] of TVideoCell;
  131. PVideoBuf = ^TVideoBuf;
  132. \end{verbatim}
  133. When registering video modes, the following typed are used to store the
  134. registered modes:
  135. \begin{verbatim}
  136. PVideoModeList = ^TVideoModeList;
  137. TVideoModeList = record
  138. Col, Row: Word;
  139. Color: Boolean;
  140. VideoModeSelector: TVideoModeSelector;
  141. Params: Longint;
  142. Next: PVideoModeList;
  143. end;
  144. \end{verbatim}
  145. The following type is used when reporting error conditions:
  146. \begin{verbatim}
  147. TErrorHandlerReturnValue = (errRetry, errAbort, errContinue);
  148. \end{verbatim}
  149. Here, \var{errRetry} means retry the operation, \var{errAbort}
  150. abort and return error code and \var{errContinue} means abort
  151. without returning an errorcode.
  152. The \var{TErrorHandler} function is used to register an own error
  153. handling function. It should be used when installing a custom error
  154. handling function, and must return one of the above values.
  155. \begin{verbatim}
  156. TErrorHandler =
  157. function (Code: Longint; Info: Pointer): TErrorHandlerReturnValue;
  158. \end{verbatim}
  159. \var{Code} should contain the error code for the error condition,
  160. and the \var{Info} parameter may contain any data type specific to
  161. the error code passed to the function.
  162. \subsection{Variables}
  163. The following variables contain information about the current screen
  164. status:
  165. \begin{verbatim}
  166. ScreenColor : Boolean;
  167. CursorX, CursorY : Word;
  168. \end{verbatim}
  169. \var{ScreenColor} indicates whether the current screen supports colors.
  170. \var{CursorX,CursorY} contain the current cursor position.
  171. The \var{LockUpdateScreen} variable can be incremented to prevent the
  172. \seep{UpdateScreen} procedure from actually updating the screen. The
  173. \var{UpdateScreen} procedure will only update the screen when the
  174. \var{LockUpdateScreen} variable is zero. This mechanism can be used
  175. to optimize the screen updating routines when various updates are
  176. performed at once.
  177. \begin{verbatim}
  178. LockUpdateScreen : Word;
  179. CursorLines : Byte;
  180. \end{verbatim}
  181. The following variables form the heart of the \file{Video} unit: The
  182. \var{VideoBuf} array represents the physical screen. Writing to this
  183. array and calling \seep{UpdateScreen} will write the actual characters
  184. to the screen. \var{VideoBufSize} contains the actual screen size, and is
  185. equal to the product of the number of columns times the number of lines
  186. on the screen (\var{ScreenWidth*ScreenHeight}).
  187. \begin{verbatim}
  188. VideoBuf : PVideoBuf;
  189. VideoBufSize : Longint;
  190. \end
  191. \end{verbatim}
  192. \section{Functions and Procedures}
  193. \begin{procedure}{ClearScreen}
  194. \Declaration
  195. procedure ClearScreen;
  196. \Description
  197. \var{ClearScreen} clears the entire screen, and calls \seep{UpdateScreen}
  198. after that.
  199. \Errors
  200. None.
  201. \SeeAlso
  202. \seep{InitVideo}, \seep{UpdateScreen}
  203. \end{procedure}
  204. \begin{procedure}{DefaultErrorHandler}
  205. \Declaration
  206. function DefaultErrorHandler(AErrorCode: Longint; AErrorInfo: Pointer): TErrorHandlerReturnValue;
  207. \Description
  208. { Default error handler, simply sets error code, and returns errContinue }
  209. \Errors
  210. \SeeAlso
  211. \end{procedure}
  212. \begin{procedure}{DefaultVideoModeSelector}
  213. \Declaration
  214. function DefaultVideoModeSelector(const VideoMode: TVideoMode; Params: Longint): Boolean;
  215. \Description
  216. \Errors
  217. \SeeAlso
  218. \end{procedure}
  219. \begin{procedure}{DoneVideo}
  220. \Declaration
  221. procedure DoneVideo;
  222. \Description
  223. { Deinitializes the video subsystem }
  224. \Errors
  225. \SeeAlso
  226. \end{procedure}
  227. \begin{function}{GetCapabilities}
  228. \Declaration
  229. function GetCapabilities: Word;
  230. \Description
  231. { Return the capabilities of the current environment }
  232. \Errors
  233. \SeeAlso
  234. \end{function}
  235. \begin{function}{GetCursorType}
  236. \Declaration
  237. function GetCursorType: Word;
  238. \Description
  239. { Return the cursor type: Hidden, UnderLine or Block }
  240. \Errors
  241. \SeeAlso
  242. \end{function}
  243. \begin{procedure}{GetVideoMode}
  244. \Declaration
  245. procedure GetVideoMode(var Mode: TVideoMode);
  246. \Description
  247. { Return dimensions of the current video mode }
  248. \Errors
  249. \SeeAlso
  250. \end{procedure}
  251. \begin{procedure}{InitVideo}
  252. \Declaration
  253. procedure InitVideo;
  254. \Description
  255. { Initializes the video subsystem }
  256. \Errors
  257. \SeeAlso
  258. \end{procedure}
  259. \begin{procedure}{RegisterVideoMode}
  260. \Declaration
  261. procedure RegisterVideoMode(Col, Row: Word; Color: Boolean; VideoModeSelector: TVideoModeSelector; Params: Longint);
  262. \Description
  263. { Registers a video mode to be selectable by SetVideoMode }
  264. { moved to interface because we need a way to retrieve the modes }
  265. \Errors
  266. \SeeAlso
  267. \end{procedure}
  268. \begin{procedure}{SetCursorPos}
  269. \Declaration
  270. procedure SetCursorPos(NewCursorX, NewCursorY: Word);
  271. \Description
  272. { Position the cursor to the given position }
  273. \Errors
  274. \SeeAlso
  275. \end{procedure}
  276. \begin{procedure}{SetCursorType}
  277. \Declaration
  278. procedure SetCursorType(NewType: Word);
  279. \Description
  280. \var{SetCursorType} sets the cursor to the type specified in \var{NewType}.
  281. \begin{description}
  282. \item[crHidden] the cursor is not visible.
  283. \item[crUnderLine] the cursor is a small underline character (usually
  284. denoting insert mode).
  285. \item[crBlock] the cursor is a block the size of a screen cell (usually
  286. denoting overwrite mode).
  287. \item[crHalfBlock] the cursor is a block half the size of a screen cell.
  288. \end{description}
  289. \Errors
  290. None.
  291. \SeeAlso
  292. \seep{SetCursorPos}
  293. \end{procedure}
  294. \begin{procedure}{SetVideoMode}
  295. \Declaration
  296. procedure SetVideoMode(Mode: TVideoMode);
  297. \Description
  298. \var{SetVideoMode} sets the video mode to the mode specified in \var{Mode}:
  299. \begin{verbatim}
  300. TVideoMode = record
  301. Col,Row : Word;
  302. Color : Boolean;
  303. end;
  304. \end{verbatim}
  305. If the call was succesful, then the screen will have \var{Col} columns and
  306. \var{Row} rows, and will be displaying in color if \var{Color} is
  307. \var{True}.
  308. Note that the video mode may not always be set. E.g. a console on Linux
  309. or a telnet session cannot always set the mode. It is important to check
  310. the error value returned by this function if it was not succesful.
  311. \Errors
  312. If the specified mode cannot be set, then \var{errVioNoSuchMode} may be set
  313. in \var{ErrorCode}
  314. \SeeAlso
  315. \end{procedure}
  316. \begin{procedure}{UpdateScreen}
  317. \Declaration
  318. procedure UpdateScreen(Force: Boolean);
  319. \Description
  320. \var{UpdateScreen} synchronizes the actual screen with the contents
  321. of the \var{VideoBuf} internal buffer. The parameter \var{Force}
  322. specifies whether the whole screen has to be redrawn (\var{Force=True})
  323. or only parts that have changed since the last update of the screen.
  324. The \var{Video} unit keeps an internal copy of the screen as it last
  325. wrote it to the screen. The current contents of \var{VideoBuf} are examined
  326. to see what locations on the screen need to be updated. On slow terminals
  327. (e.g. a \linux telnet session) this mechanism can speed up the screen redraw
  328. considerably.
  329. \Errors
  330. None.
  331. \SeeAlso
  332. \seep{ClearScreen}
  333. \end{procedure}