video.inc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. VideoInitialized:=True;
  134. if NextVideoModeSet then
  135. SetVideoMode(NextVideoMode)
  136. else
  137. AssignVideoBuf(0,0);
  138. ClearScreen;
  139. end;
  140. end;
  141. Procedure DoneVideo;
  142. begin
  143. If VideoInitialized then
  144. begin
  145. If Assigned(CurrentVideoDriver.DoneDriver) then
  146. CurrentVideoDriver.DoneDriver;
  147. FreeVideoBuf;
  148. VideoInitialized:=False;
  149. end;
  150. end;
  151. Procedure UpdateScreen (Force : Boolean);
  152. begin
  153. If (LockUpdateScreen<=0) and
  154. Assigned(CurrentVideoDriver.UpdateScreen) then
  155. CurrentVideoDriver.UpdateScreen(Force);
  156. end;
  157. Procedure ClearScreen;
  158. begin
  159. // Should this not be the current color ?
  160. FillWord(VideoBuf^,VideoBufSize shr 1,$0720);
  161. If Assigned(CurrentVideoDriver.ClearScreen) then
  162. CurrentVideoDriver.ClearScreen
  163. else
  164. UpdateScreen(True);
  165. FillWord(OldVideoBuf^,VideoBufSize shr 1,$0720);
  166. end;
  167. Procedure SetCursorType (NewType : Word);
  168. begin
  169. if Assigned(CurrentVideoDriver.SetCursorType) then
  170. CurrentVideoDriver.SetCursorType(NewType)
  171. end;
  172. Function GetCursorType : Word;
  173. begin
  174. if Assigned(CurrentVideoDriver.GetCursorType) then
  175. GetCursorType:=CurrentVideoDriver.GetCursorType()
  176. else
  177. GetCursorType:=0;
  178. end;
  179. procedure SetCursorPos(NewCursorX, NewCursorY: Word);
  180. begin
  181. If Assigned(CurrentVideoDriver.SetCursorPos) then
  182. CurrentVideoDriver.SetCursorPos(NewCursorX, NewCursorY)
  183. end;
  184. function GetCapabilities: Word;
  185. begin
  186. If Assigned(CurrentVideoDriver.GetCapabilities) then
  187. GetCapabilities:=CurrentVideoDriver.GetCapabilities()
  188. else
  189. GetCapabilities:=0;
  190. end;
  191. { ---------------------------------------------------------------------
  192. General functions
  193. ---------------------------------------------------------------------}
  194. procedure GetVideoMode(var Mode: TVideoMode);
  195. begin
  196. Mode.Col := ScreenWidth;
  197. Mode.Row := ScreenHeight;
  198. Mode.Color := ScreenColor;
  199. end;
  200. Function SetVideoMode(Const Mode: TVideoMode) : Boolean;
  201. Var
  202. OldR,OldC: Word;
  203. begin
  204. SetVideoMode:=DriverInitialized;
  205. if not DriverInitialized then
  206. exit;
  207. If VideoInitialized then
  208. begin
  209. OldC:=ScreenWidth;
  210. OldR:=ScreenHeight;
  211. If Assigned(CurrentVideoDriver.SetVideoMode) then
  212. SetVideoMode:=CurrentVideoDriver.SetVideoMode(Mode)
  213. else
  214. SetVideoMode:=False;
  215. // Assign buffer
  216. If SetVideoMode then
  217. AssignVideoBuf(OldC,Oldr);
  218. end
  219. else
  220. begin
  221. NextVideoMode:=Mode;
  222. NextVideoModeSet:=true;
  223. end;
  224. end;
  225. Function GetVideoModeCount : Word;
  226. begin
  227. If Assigned(CurrentVideoDriver.GetVideoModeCount) then
  228. GetVideoModeCount:=CurrentVideoDriver.GetVideoModeCount()
  229. else
  230. GetVideoModeCount:=1;
  231. end;
  232. Function GetVideoModeData(Index : Word; Var Data: TVideoMode) : Boolean;
  233. begin
  234. If Assigned(CurrentVideoDriver.GetVideoModeData) then
  235. GetVideoModeData:=CurrentVideoDriver.GetVideoModeData(Index,Data)
  236. else
  237. begin
  238. GetVideoModeData:=(Index=0);
  239. If GetVideoModeData then
  240. GetVideoMode(Data);
  241. end
  242. end;
  243. function DefaultErrorHandler(AErrorCode: Longint; AErrorInfo: Pointer): TErrorHandlerReturnValue;
  244. begin
  245. ErrorCode := AErrorCode;
  246. ErrorInfo := AErrorInfo;
  247. DefaultErrorHandler := errAbort; { return error code }
  248. end;