video.pp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2006 by Karoly Balogh
  4. member of the Free Pascal development team
  5. Video unit for Amiga and MorphOS
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit Video;
  13. interface
  14. uses
  15. intuition;
  16. {$i videoh.inc}
  17. { Amiga specific calls, to help interaction between Keyboard, Mouse and
  18. Video units, and Free Vision }
  19. procedure GotCloseWindow;
  20. function HasCloseWindow: boolean;
  21. procedure GotResizeWindow;
  22. function HasResizeWindow(var winw:longint; var winh: longint): boolean;
  23. var
  24. videoWindow : pWindow;
  25. implementation
  26. uses
  27. exec, graphics;
  28. {$i video.inc}
  29. {$i videodata.inc}
  30. const
  31. LastCursorType: word = crUnderline;
  32. OrigScreen: PVideoBuf = nil;
  33. OrigScreenSize: cardinal = 0;
  34. var
  35. videoColorMap : pColorMap;
  36. videoPens : array[0..15] of longint;
  37. oldCursorX, oldCursorY: longint;
  38. cursorType: word;
  39. oldcursorType: word;
  40. gotCloseWindowMsg: boolean;
  41. gotResizeWindowMsg: boolean;
  42. procedure SysInitVideo;
  43. var counter: longint;
  44. begin
  45. InitGraphicsLibrary;
  46. InitIntuitionLibrary;
  47. // fill videobuf and oldvideobuf with different bytes, to allow proper first draw
  48. FillDword(VideoBuf^,VideoBufSize Div 4,$1234D3AD);
  49. FillDword(OldVideoBuf^,VideoBufSize Div 4,$4321BEEF);
  50. videoWindow:=OpenWindowTags(Nil, [
  51. WA_Left,50,
  52. WA_Top,50,
  53. WA_InnerWidth,80*8,
  54. WA_InnerHeight,25*16,
  55. WA_MaxWidth,32768,
  56. WA_MaxHeight,32768,
  57. WA_IDCMP,IDCMP_VANILLAKEY Or IDCMP_RAWKEY Or
  58. IDCMP_MOUSEMOVE Or IDCMP_MOUSEBUTTONS Or
  59. IDCMP_CLOSEWINDOW Or IDCMP_CHANGEWINDOW,
  60. WA_Title,DWord(PChar('Free Pascal Video Output')),
  61. WA_Flags,(WFLG_GIMMEZEROZERO Or WFLG_SMART_REFRESH Or WFLG_NOCAREREFRESH Or
  62. WFLG_ACTIVATE Or WFLG_DRAGBAR Or WFLG_DEPTHGADGET Or WFLG_REPORTMOUSE Or
  63. WFLG_SIZEGADGET Or WFLG_SIZEBBOTTOM Or WFLG_RMBTRAP Or
  64. WFLG_CLOSEGADGET)
  65. ]);
  66. ScreenWidth := 80;
  67. ScreenHeight := 25;
  68. ScreenColor := true;
  69. videoColorMap := pScreen(videoWindow^.WScreen)^.ViewPort.ColorMap;
  70. for counter:=0 to 15 do begin
  71. videoPens[counter]:=ObtainPen(videoColorMap,-1,
  72. vgacolors[counter,0] shl 24,vgacolors[counter,1] shl 24,vgacolors[counter,2] shl 24,
  73. PEN_EXCLUSIVE);
  74. // writeln(videoPens[counter]);
  75. // XXX: do checks for -1 colors (KB)
  76. end;
  77. CursorX:=0;
  78. CursorY:=0;
  79. oldCursorX:=0;
  80. oldCursorY:=0;
  81. cursorType:=crHidden;
  82. oldcursorType:=crHidden;
  83. gotCloseWindowMsg:=false;
  84. gotResizeWindowMsg:=false;
  85. end;
  86. procedure SysDoneVideo;
  87. var counter: longint;
  88. begin
  89. if videoWindow<>nil then CloseWindow(videoWindow);
  90. for counter:=0 to 15 do ReleasePen(videoColorMap,videoPens[counter]);
  91. end;
  92. function SysSetVideoMode (Const Mode : TVideoMode) : Boolean;
  93. var
  94. I : Integer;
  95. dx : integer;
  96. dy : integer;
  97. begin
  98. dx := (Mode.col * 8) - videoWindow^.GZZWidth;
  99. dy := (Mode.row * 16) - videoWindow^.GZZHeight;
  100. SizeWindow(videoWindow,dx,dy);
  101. ScreenWidth:=Mode.col;
  102. ScreenHeight:=Mode.row;
  103. ScreenColor:=Mode.color;
  104. SysSetVideoMode:=true;
  105. end;
  106. var
  107. oldSH, oldSW : longint;
  108. procedure SysClearScreen;
  109. begin
  110. oldSH := -1;
  111. oldSW := -1;
  112. UpdateScreen(true);
  113. end;
  114. procedure DrawChar(x,y: longint; crType: word);
  115. var tmpCharData: word;
  116. tmpChar : byte;
  117. tmpFGColor : byte;
  118. tmpBGColor : byte;
  119. var
  120. counterX, counterY:longint;
  121. sX,sY: longint;
  122. begin
  123. tmpCharData:=VideoBuf^[y*ScreenWidth+x];
  124. tmpChar :=tmpCharData and $0ff;
  125. tmpFGColor :=(tmpCharData shr 8) and %00001111;
  126. tmpBGColor :=(tmpCharData shr 12) and %00000111;
  127. sX:=x*8;
  128. sY:=y*16;
  129. if crType <> crBlock then begin
  130. SetABPenDrMd(videoWindow^.RPort,videoPens[tmpFGColor],videoPens[tmpBGColor],JAM2);
  131. end else begin
  132. { in case of block cursor, swap fg/bg colors
  133. and BltTemplate() below will take care of everything }
  134. SetABPenDrMd(videoWindow^.RPort,videoPens[tmpBGColor],videoPens[tmpFGColor],JAM2);
  135. end;
  136. BltTemplate(@vgafont[tmpChar,0],0,1,videoWindow^.RPort,sX,sY,8,16);
  137. if crType = crUnderLine then begin
  138. { draw two lines at the bottom of the char, in case of underline cursor }
  139. gfxMove(videoWindow^.RPort,sX,sY+14); Draw(videoWindow^.RPort,sX+7,sY+14);
  140. gfxMove(videoWindow^.RPort,sX,sY+15); Draw(videoWindow^.RPort,sX+7,sY+15);
  141. end;
  142. end;
  143. procedure SysUpdateScreen(force: boolean);
  144. var
  145. BufCounter : Longint;
  146. smallforce : boolean;
  147. cursormoved : boolean;
  148. counter, counterX, counterY: longint;
  149. begin
  150. smallforce:=false;
  151. cursormoved:=false;
  152. // override forced update when screen dimensions haven't changed
  153. if force then begin
  154. if (oldSH = ScreenHeight) and
  155. (oldSW = ScreenWidth) then
  156. force:=false
  157. else begin
  158. oldSH := ScreenHeight;
  159. oldSW := ScreenWidth;
  160. end;
  161. end;
  162. if force then begin
  163. smallforce:=true;
  164. end else begin
  165. counter:=0;
  166. while not smallforce and (counter<(VideoBufSize div 4)-1) do begin
  167. smallforce:=(PDWord(VideoBuf)[counter] <> PDWord(OldVideoBuf)[counter]);
  168. inc(counter);
  169. end;
  170. end;
  171. BufCounter:=0;
  172. if smallforce then begin
  173. for counterY:=0 to ScreenHeight-1 do begin
  174. for counterX:=0 to ScreenWidth-1 do begin
  175. if (VideoBuf^[BufCounter]<>OldVideoBuf^[BufCounter]) or force then
  176. DrawChar(counterX,counterY,crHidden);
  177. Inc(BufCounter);
  178. end;
  179. end;
  180. move(VideoBuf^,OldVideoBuf^,VideoBufSize);
  181. end;
  182. if (cursorType<>oldcursorType) or
  183. (CursorX<>oldCursorX) or (CursorY<>oldCursorY) or
  184. smallforce then begin
  185. DrawChar(oldCursorY,oldCursorX,crHidden);
  186. DrawChar(CursorY,CursorX,cursorType);
  187. oldCursorX:=CursorX;
  188. oldCursorY:=CursorY;
  189. oldcursorType:=cursorType;
  190. end;
  191. end;
  192. procedure SysSetCursorPos(NewCursorX, NewCursorY: Word);
  193. begin
  194. CursorX:=NewCursorY;
  195. CursorY:=NewCursorX;
  196. SysUpdateScreen(false);
  197. end;
  198. function SysGetCapabilities: Word;
  199. begin
  200. SysGetCapabilities:=cpColor or cpChangeCursor;
  201. end;
  202. function SysGetCursorType: Word;
  203. begin
  204. SysGetCursorType:=cursorType;
  205. end;
  206. procedure SysSetCursorType(NewType: Word);
  207. begin
  208. cursorType:=newType;
  209. { FIXME: halfBlock cursors are not supported for now
  210. by the rendering code }
  211. if cursorType = crHalfBlock then cursorType:=crBlock;
  212. SysUpdateScreen(false);
  213. end;
  214. // Amiga specific calls
  215. procedure GotCloseWindow;
  216. begin
  217. gotCloseWindowMsg:=true;
  218. end;
  219. function HasCloseWindow: boolean;
  220. begin
  221. HasCloseWindow:=gotCloseWindowMsg;
  222. gotCloseWindowMsg:=false;
  223. end;
  224. procedure GotResizeWindow;
  225. begin
  226. gotResizeWindowMsg:=true;
  227. end;
  228. function HasResizeWindow(var winw:longint; var winh: longint): boolean;
  229. begin
  230. HasResizeWindow:=gotResizeWindowMsg;
  231. winw:=videoWindow^.GZZWidth div 8;
  232. winh:=videoWindow^.GZZHeight div 16;
  233. gotResizeWindowMsg:=false;
  234. end;
  235. const
  236. SysVideoDriver : TVideoDriver = (
  237. InitDriver : @SysInitVideo;
  238. DoneDriver : @SysDoneVideo;
  239. UpdateScreen : @SysUpdateScreen;
  240. ClearScreen : @SysClearScreen;
  241. SetVideoMode : @SysSetVideoMode;
  242. GetVideoModeCount : nil;
  243. GetVideoModeData : nil;
  244. SetCursorPos : @SysSetCursorPos;
  245. GetCursorType : @SysGetCursorType;
  246. SetCursorType : @SysSetCursorType;
  247. GetCapabilities : @SysGetCapabilities
  248. );
  249. initialization
  250. SetVideoDriver(SysVideoDriver);
  251. end.