video.inc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. Const
  11. LockUpdateScreen : Integer = 0;
  12. Procedure LockScreenUpdate;
  13. begin
  14. Inc(LockUpdateScreen);
  15. end;
  16. Procedure UnLockScreenUpdate;
  17. begin
  18. If LockUpdateScreen>0 then
  19. Dec(LockUpdateScreen);
  20. end;
  21. Function GetLockScreenCount : integer;
  22. begin
  23. GetLockScreenCount:=LockUpdateScreen;
  24. end;
  25. Var
  26. CurrentVideoDriver : TVideoDriver;
  27. NextVideoMode : TVideoMode;
  28. Const
  29. VideoInitialized : Boolean = False;
  30. DriverInitialized : Boolean = False;
  31. NextVideoModeSet : Boolean = False;
  32. Function SetVideoDriver (Const Driver : TVideoDriver) : Boolean;
  33. { Sets the videodriver to be used }
  34. begin
  35. If Not VideoInitialized then
  36. Begin
  37. CurrentVideoDriver:=Driver;
  38. DriverInitialized:=true;
  39. NextVideoModeSet:=false;
  40. End;
  41. SetVideoDriver:=Not VideoInitialized;
  42. end;
  43. Procedure GetVideoDriver (Var Driver : TVideoDriver);
  44. { Retrieves the current videodriver }
  45. begin
  46. Driver:=CurrentVideoDriver;
  47. end;
  48. { ---------------------------------------------------------------------
  49. External functions that use the video driver.
  50. ---------------------------------------------------------------------}
  51. Procedure FreeVideoBuf;
  52. begin
  53. if (VideoBuf<>Nil) then
  54. begin
  55. FreeMem(VideoBuf);
  56. FreeMem(OldVideoBuf);
  57. VideoBuf:=Nil;
  58. OldVideoBuf:=Nil;
  59. VideoBufSize:=0;
  60. end;
  61. end;
  62. (*
  63. Procedure AssignVideoBuf (OldCols, OldRows : Word);
  64. Var NewVideoBuf,NewOldVideoBuf : PVideoBuf;
  65. I,C,R,NewVideoBufSize : longint;
  66. s:word;
  67. begin
  68. S:=sizeOf(TVideoCell);
  69. NewVideoBufSize:=ScreenWidth*ScreenHeight*s;
  70. GetMem(NewVideoBuf,NewVideoBufSize);
  71. GetMem(NewOldVideoBuf,NewVideoBufSize);
  72. // Move contents of old videobuffers to new if there are any.
  73. if (VideoBuf<>Nil) then
  74. begin
  75. If (ScreenWidth<OldCols) then
  76. C:=ScreenWidth
  77. else
  78. C:=OldCols;
  79. If (ScreenHeight<OldRows) then
  80. R:=ScreenHeight
  81. else
  82. R:=OldRows;
  83. For I:=0 to R-1 do
  84. begin
  85. Move(VideoBuf^[I*OldCols],NewVideoBuf^[I*ScreenWidth],S*C);
  86. Move(OldVideoBuf^[I*OldCols],NewOldVideoBuf^[I*ScreenWidth],S*C);
  87. end;
  88. end;
  89. FreeVideoBuf;
  90. VideoBufSize:=NewVideoBufSize;
  91. VideoBuf:=NewVideoBuf;
  92. OldVideoBuf:=NewOldVideoBuf;
  93. end;
  94. *)
  95. Procedure AssignVideoBuf (OldCols, OldRows : Word);
  96. var NewVideoBuf,NewOldVideoBuf:PVideoBuf;
  97. old_rowstart,new_rowstart:word;
  98. NewVideoBufSize : longint;
  99. begin
  100. NewVideoBufSize:=ScreenWidth*ScreenHeight*sizeof(TVideoCell);
  101. GetMem(NewVideoBuf,NewVideoBufSize);
  102. GetMem(NewOldVideoBuf,NewVideoBufSize);
  103. {Move contents of old videobuffers to new if there are any.}
  104. if VideoBuf<>nil then
  105. begin
  106. if ScreenWidth<OldCols then
  107. OldCols:=ScreenWidth;
  108. if ScreenHeight<OldRows then
  109. OldRows:=ScreenHeight;
  110. old_rowstart:=0;
  111. new_rowstart:=0;
  112. while oldrows>0 do
  113. begin
  114. move(VideoBuf^[old_rowstart],NewVideoBuf^[new_rowstart],OldCols*sizeof(TVideoCell));
  115. move(OldVideoBuf^[old_rowstart],NewOldVideoBuf^[new_rowstart],OldCols*sizeof(TVideoCell));
  116. inc(old_rowstart,OldCols);
  117. inc(new_rowstart,ScreenWidth);
  118. dec(OldRows);
  119. end;
  120. end;
  121. FreeVideoBuf;
  122. { FreeVideoBuf sets VideoBufSize to 0 }
  123. VideoBufSize:=NewVideoBufSize;
  124. VideoBuf:=NewVideoBuf;
  125. OldVideoBuf:=NewOldVideoBuf;
  126. end;
  127. Procedure InitVideo;
  128. begin
  129. if not VideoInitialized then
  130. begin
  131. if Assigned(CurrentVideoDriver.InitDriver) then
  132. CurrentVideoDriver.InitDriver;
  133. if errorcode=viook then
  134. begin
  135. VideoInitialized:=true;
  136. if NextVideoModeSet then
  137. SetVideoMode(NextVideoMode)
  138. else
  139. AssignVideoBuf(0,0);
  140. ClearScreen;
  141. end;
  142. end;
  143. end;
  144. Procedure DoneVideo;
  145. begin
  146. If VideoInitialized then
  147. begin
  148. If Assigned(CurrentVideoDriver.DoneDriver) then
  149. CurrentVideoDriver.DoneDriver;
  150. FreeVideoBuf;
  151. VideoInitialized:=False;
  152. end;
  153. end;
  154. Procedure UpdateScreen (Force : Boolean);
  155. begin
  156. If (LockUpdateScreen<=0) and
  157. Assigned(CurrentVideoDriver.UpdateScreen) then
  158. CurrentVideoDriver.UpdateScreen(Force);
  159. end;
  160. Procedure ClearScreen;
  161. begin
  162. // Should this not be the current color ?
  163. FillWord(VideoBuf^,VideoBufSize shr 1,$0720);
  164. If Assigned(CurrentVideoDriver.ClearScreen) then
  165. CurrentVideoDriver.ClearScreen
  166. else
  167. UpdateScreen(True);
  168. FillWord(OldVideoBuf^,VideoBufSize shr 1,$0720);
  169. end;
  170. Procedure SetCursorType (NewType : Word);
  171. begin
  172. if Assigned(CurrentVideoDriver.SetCursorType) then
  173. CurrentVideoDriver.SetCursorType(NewType)
  174. end;
  175. Function GetCursorType : Word;
  176. begin
  177. if Assigned(CurrentVideoDriver.GetCursorType) then
  178. GetCursorType:=CurrentVideoDriver.GetCursorType()
  179. else
  180. GetCursorType:=0;
  181. end;
  182. procedure SetCursorPos(NewCursorX, NewCursorY: Word);
  183. begin
  184. If Assigned(CurrentVideoDriver.SetCursorPos) then
  185. CurrentVideoDriver.SetCursorPos(NewCursorX, NewCursorY)
  186. end;
  187. function GetCapabilities: Word;
  188. begin
  189. If Assigned(CurrentVideoDriver.GetCapabilities) then
  190. GetCapabilities:=CurrentVideoDriver.GetCapabilities()
  191. else
  192. GetCapabilities:=0;
  193. end;
  194. { ---------------------------------------------------------------------
  195. General functions
  196. ---------------------------------------------------------------------}
  197. procedure GetVideoMode(var Mode: TVideoMode);
  198. begin
  199. Mode.Col := ScreenWidth;
  200. Mode.Row := ScreenHeight;
  201. Mode.Color := ScreenColor;
  202. end;
  203. Function SetVideoMode(Const Mode: TVideoMode) : Boolean;
  204. Var
  205. OldR,OldC: Word;
  206. begin
  207. SetVideoMode:=DriverInitialized;
  208. if not DriverInitialized then
  209. exit;
  210. If VideoInitialized then
  211. begin
  212. OldC:=ScreenWidth;
  213. OldR:=ScreenHeight;
  214. If Assigned(CurrentVideoDriver.SetVideoMode) then
  215. SetVideoMode:=CurrentVideoDriver.SetVideoMode(Mode)
  216. else
  217. SetVideoMode:=False;
  218. // Assign buffer
  219. If SetVideoMode then
  220. AssignVideoBuf(OldC,Oldr);
  221. end
  222. else
  223. begin
  224. NextVideoMode:=Mode;
  225. NextVideoModeSet:=true;
  226. end;
  227. end;
  228. Function GetVideoModeCount : Word;
  229. begin
  230. If Assigned(CurrentVideoDriver.GetVideoModeCount) then
  231. GetVideoModeCount:=CurrentVideoDriver.GetVideoModeCount()
  232. else
  233. GetVideoModeCount:=1;
  234. end;
  235. Function GetVideoModeData(Index : Word; Var Data: TVideoMode) : Boolean;
  236. begin
  237. If Assigned(CurrentVideoDriver.GetVideoModeData) then
  238. GetVideoModeData:=CurrentVideoDriver.GetVideoModeData(Index,Data)
  239. else
  240. begin
  241. GetVideoModeData:=(Index=0);
  242. If GetVideoModeData then
  243. GetVideoMode(Data);
  244. end
  245. end;
  246. function DefaultErrorHandler(AErrorCode: Longint; AErrorInfo: Pointer): TErrorHandlerReturnValue;
  247. begin
  248. ErrorCode := AErrorCode;
  249. ErrorInfo := AErrorInfo;
  250. DefaultErrorHandler := errAbort; { return error code }
  251. end;