video.pp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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_MOUSEBUTTONS Or IDCMP_RAWKEYS,
  58. WA_IDCMP,IDCMP_VANILLAKEY Or IDCMP_RAWKEY 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
  63. WFLG_SIZEGADGET Or WFLG_SIZEBBOTTOM 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. procedure SysClearScreen;
  107. begin
  108. UpdateScreen(true);
  109. end;
  110. procedure DrawChar(x,y: longint; crType: word);
  111. var tmpCharData: word;
  112. tmpChar : byte;
  113. tmpFGColor : byte;
  114. tmpBGColor : byte;
  115. var
  116. counterX, counterY:longint;
  117. sX,sY: longint;
  118. begin
  119. tmpCharData:=VideoBuf^[y*ScreenWidth+x];
  120. tmpChar :=tmpCharData and $0ff;
  121. tmpFGColor :=(tmpCharData shr 8) and %00001111;
  122. tmpBGColor :=(tmpCharData shr 12) and %00000111;
  123. sX:=x*8;
  124. sY:=y*16;
  125. if crType <> crBlock then begin
  126. SetABPenDrMd(videoWindow^.RPort,videoPens[tmpFGColor],videoPens[tmpBGColor],JAM2);
  127. end else begin
  128. { in case of block cursor, swap fg/bg colors
  129. and BltTemplate() below will take care of everything }
  130. SetABPenDrMd(videoWindow^.RPort,videoPens[tmpBGColor],videoPens[tmpFGColor],JAM2);
  131. end;
  132. BltTemplate(@vgafont[tmpChar,0],0,1,videoWindow^.RPort,sX,sY,8,16);
  133. if crType = crUnderLine then begin
  134. { draw two lines at the bottom of the char, in case of underline cursor }
  135. gfxMove(videoWindow^.RPort,sX,sY+14); Draw(videoWindow^.RPort,sX+7,sY+14);
  136. gfxMove(videoWindow^.RPort,sX,sY+15); Draw(videoWindow^.RPort,sX+7,sY+15);
  137. end;
  138. end;
  139. procedure SysUpdateScreen(force: boolean);
  140. var
  141. BufCounter : Longint;
  142. smallforce : boolean;
  143. cursormoved : boolean;
  144. counter, counterX, counterY: longint;
  145. begin
  146. smallforce:=false;
  147. cursormoved:=false;
  148. if force then
  149. smallforce:=true
  150. else begin
  151. counter:=0;
  152. while not smallforce and (counter<(VideoBufSize div 4)-1) do begin
  153. if PDWord(VideoBuf)[counter]<>PDWord(OldVideoBuf)[counter] then smallforce:=true;
  154. counter+=1;
  155. end;
  156. end;
  157. BufCounter:=0;
  158. if smallforce then begin
  159. for counterY:=0 to ScreenHeight-1 do begin
  160. for counterX:=0 to ScreenWidth-1 do begin
  161. if (VideoBuf^[BufCounter]<>OldVideoBuf^[BufCounter]) or force then
  162. DrawChar(counterX,counterY,crHidden);
  163. Inc(BufCounter);
  164. end;
  165. end;
  166. move(VideoBuf^,OldVideoBuf^,VideoBufSize);
  167. end;
  168. if (cursorType<>oldcursorType) or
  169. (CursorX<>oldCursorX) or (CursorY<>oldCursorY) or
  170. smallforce then begin
  171. DrawChar(oldCursorY,oldCursorX,crHidden);
  172. DrawChar(CursorY,CursorX,cursorType);
  173. oldCursorX:=CursorX;
  174. oldCursorY:=CursorY;
  175. oldcursorType:=cursorType;
  176. end;
  177. end;
  178. procedure SysSetCursorPos(NewCursorX, NewCursorY: Word);
  179. begin
  180. CursorX:=NewCursorY;
  181. CursorY:=NewCursorX;
  182. SysUpdateScreen(false);
  183. end;
  184. function SysGetCapabilities: Word;
  185. begin
  186. SysGetCapabilities:=cpColor or cpChangeCursor;
  187. end;
  188. function SysGetCursorType: Word;
  189. begin
  190. SysGetCursorType:=cursorType;
  191. end;
  192. procedure SysSetCursorType(NewType: Word);
  193. begin
  194. cursorType:=newType;
  195. { FIXME: halfBlock cursors are not supported for now
  196. by the rendering code }
  197. if cursorType = crHalfBlock then cursorType:=crBlock;
  198. SysUpdateScreen(false);
  199. end;
  200. // Amiga specific calls
  201. procedure GotCloseWindow;
  202. begin
  203. gotCloseWindowMsg:=true;
  204. end;
  205. function HasCloseWindow: boolean;
  206. begin
  207. HasCloseWindow:=gotCloseWindowMsg;
  208. gotCloseWindowMsg:=false;
  209. end;
  210. procedure GotResizeWindow;
  211. begin
  212. gotResizeWindowMsg:=true;
  213. end;
  214. function HasResizeWindow(var winw:longint; var winh: longint): boolean;
  215. begin
  216. HasResizeWindow:=gotResizeWindowMsg;
  217. winw:=videoWindow^.GZZWidth div 8;
  218. winh:=videoWindow^.GZZHeight div 16;
  219. gotResizeWindowMsg:=false;
  220. end;
  221. const
  222. SysVideoDriver : TVideoDriver = (
  223. InitDriver : @SysInitVideo;
  224. DoneDriver : @SysDoneVideo;
  225. UpdateScreen : @SysUpdateScreen;
  226. ClearScreen : @SysClearScreen;
  227. SetVideoMode : @SysSetVideoMode;
  228. GetVideoModeCount : nil;
  229. GetVideoModeData : nil;
  230. SetCursorPos : @SysSetCursorPos;
  231. GetCursorType : @SysGetCursorType;
  232. SetCursorType : @SysSetCursorType;
  233. GetCapabilities : @SysGetCapabilities
  234. );
  235. initialization
  236. SetVideoDriver(SysVideoDriver);
  237. end.