video.inc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. Procedure AssignVideoBuf (OldCols, OldRows : Word);
  63. Var NewVideoBuf,NewOldVideoBuf : PVideoBuf;
  64. S,I,C,R,NewVideoBufSize : longint;
  65. begin
  66. S:=SizeOf(TVideoCell);
  67. NewVideoBufSize:=ScreenWidth*ScreenHeight*S;
  68. GetMem(NewVideoBuf,NewVideoBufSize);
  69. GetMem(NewOldVideoBuf,NewVideoBufSize);
  70. // Move contents of old videobuffers to new if there are any.
  71. if (VideoBuf<>Nil) then
  72. begin
  73. If (ScreenWidth<OldCols) then
  74. C:=ScreenWidth
  75. else
  76. C:=OldCols;
  77. If (ScreenHeight<OldRows) then
  78. R:=ScreenHeight
  79. else
  80. R:=OldRows;
  81. For I:=0 to R-1 do
  82. begin
  83. Move(VideoBuf^[I*OldCols],NewVideoBuf^[I*ScreenWidth],S*C);
  84. Move(OldVideoBuf^[I*OldCols],NewOldVideoBuf^[I*ScreenWidth],S*C);
  85. end;
  86. end;
  87. FreeVideoBuf;
  88. VideoBufSize:=NewVideoBufSize;
  89. VideoBuf:=NewVideoBuf;
  90. OldVideoBuf:=NewOldVideoBuf;
  91. end;
  92. Procedure InitVideo;
  93. begin
  94. If Not VideoInitialized then
  95. begin
  96. If Assigned(CurrentVideoDriver.InitDriver) then
  97. CurrentVideoDriver.InitDriver;
  98. VideoInitialized:=True;
  99. if NextVideoModeSet then
  100. SetVideoMode(NextVideoMode)
  101. else
  102. AssignVideoBuf(0,0);
  103. ClearScreen;
  104. end;
  105. end;
  106. Procedure DoneVideo;
  107. begin
  108. If VideoInitialized then
  109. begin
  110. If Assigned(CurrentVideoDriver.DoneDriver) then
  111. CurrentVideoDriver.DoneDriver;
  112. FreeVideoBuf;
  113. VideoInitialized:=False;
  114. end;
  115. end;
  116. Procedure UpdateScreen (Force : Boolean);
  117. begin
  118. If (LockUpdateScreen<=0) and
  119. Assigned(CurrentVideoDriver.UpdateScreen) then
  120. CurrentVideoDriver.UpdateScreen(Force);
  121. end;
  122. Procedure ClearScreen;
  123. begin
  124. // Should this not be the current color ?
  125. FillWord(VideoBuf^,VideoBufSize shr 1,$0720);
  126. If Assigned(CurrentVideoDriver.ClearScreen) then
  127. CurrentVideoDriver.ClearScreen
  128. else
  129. UpdateScreen(True);
  130. FillWord(OldVideoBuf^,VideoBufSize shr 1,$0720);
  131. end;
  132. Procedure SetCursorType (NewType : Word);
  133. begin
  134. if Assigned(CurrentVideoDriver.SetCursorType) then
  135. CurrentVideoDriver.SetCursorType(NewType)
  136. end;
  137. Function GetCursorType : Word;
  138. begin
  139. if Assigned(CurrentVideoDriver.GetCursorType) then
  140. GetCursorType:=CurrentVideoDriver.GetCursorType()
  141. else
  142. GetCursorType:=0;
  143. end;
  144. procedure SetCursorPos(NewCursorX, NewCursorY: Word);
  145. begin
  146. If Assigned(CurrentVideoDriver.SetCursorPos) then
  147. CurrentVideoDriver.SetCursorPos(NewCursorX, NewCursorY)
  148. end;
  149. function GetCapabilities: Word;
  150. begin
  151. If Assigned(CurrentVideoDriver.GetCapabilities) then
  152. GetCapabilities:=CurrentVideoDriver.GetCapabilities()
  153. else
  154. GetCapabilities:=0;
  155. end;
  156. { ---------------------------------------------------------------------
  157. General functions
  158. ---------------------------------------------------------------------}
  159. procedure GetVideoMode(var Mode: TVideoMode);
  160. begin
  161. Mode.Col := ScreenWidth;
  162. Mode.Row := ScreenHeight;
  163. Mode.Color := ScreenColor;
  164. end;
  165. Function SetVideoMode(Const Mode: TVideoMode) : Boolean;
  166. Var
  167. OldR,OldC: Word;
  168. begin
  169. SetVideoMode:=DriverInitialized;
  170. if not DriverInitialized then
  171. exit;
  172. If VideoInitialized then
  173. begin
  174. OldC:=ScreenWidth;
  175. OldR:=ScreenHeight;
  176. If Assigned(CurrentVideoDriver.SetVideoMode) then
  177. SetVideoMode:=CurrentVideoDriver.SetVideoMode(Mode)
  178. else
  179. SetVideoMode:=False;
  180. // Assign buffer
  181. If SetVideoMode then
  182. AssignVideoBuf(OldC,Oldr);
  183. end
  184. else
  185. begin
  186. NextVideoMode:=Mode;
  187. NextVideoModeSet:=true;
  188. end;
  189. end;
  190. Function GetVideoModeCount : Word;
  191. begin
  192. If Assigned(CurrentVideoDriver.GetVideoModeCount) then
  193. GetVideoModeCount:=CurrentVideoDriver.GetVideoModeCount()
  194. else
  195. GetVideoModeCount:=1;
  196. end;
  197. Function GetVideoModeData(Index : Word; Var Data: TVideoMode) : Boolean;
  198. begin
  199. If Assigned(CurrentVideoDriver.GetVideoModeData) then
  200. GetVideoModeData:=CurrentVideoDriver.GetVideoModeData(Index,Data)
  201. else
  202. begin
  203. GetVideoModeData:=(Index=0);
  204. If GetVideoModeData then
  205. GetVideoMode(Data);
  206. end
  207. end;
  208. function DefaultErrorHandler(AErrorCode: Longint; AErrorInfo: Pointer): TErrorHandlerReturnValue;
  209. begin
  210. ErrorCode := AErrorCode;
  211. ErrorInfo := AErrorInfo;
  212. DefaultErrorHandler := errAbort; { return error code }
  213. end;