ptcgraph.pp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2007 by Daniel Mantione
  4. member of the Free Pascal development team
  5. This file implements the PTC support for the graph unit
  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 ptcgraph;
  13. {$define logging}
  14. {******************************************************************************}
  15. interface
  16. {******************************************************************************}
  17. {$i graphh.inc}
  18. {Driver number for PTC.}
  19. const PTC=22;
  20. {******************************************************************************}
  21. implementation
  22. {******************************************************************************}
  23. uses
  24. termio,x86,ptc;
  25. const
  26. InternalDriverName = 'PTCPas';
  27. {$i graph.inc}
  28. type
  29. PByte = ^Byte;
  30. PLongInt = ^LongInt;
  31. PByteArray = ^TByteArray;
  32. TByteArray = array [0..MAXINT - 1] of Byte;
  33. { ---------------------------------------------------------------------
  34. SVGA bindings.
  35. ---------------------------------------------------------------------}
  36. Const
  37. { Text }
  38. WRITEMODE_OVERWRITE = 0;
  39. WRITEMODE_MASKED = 1;
  40. FONT_EXPANDED = 0;
  41. FONT_COMPRESSED = 2;
  42. { Types }
  43. type
  44. pvga_modeinfo = ^vga_modeinfo;
  45. vga_modeinfo = record
  46. width,
  47. height,
  48. bytesperpixel,
  49. colors,
  50. linewidth, { scanline width in bytes }
  51. maxlogicalwidth, { maximum logical scanline width }
  52. startaddressrange, { changeable bits set }
  53. maxpixels, { video memory / bytesperpixel }
  54. haveblit, { mask of blit functions available }
  55. flags: Longint; { other flags }
  56. { Extended fields: }
  57. chiptype, { Chiptype detected }
  58. memory, { videomemory in KB }
  59. linewidth_unit: Longint; { Use only a multiple of this as parameter for set_displaystart }
  60. linear_aperture: PChar; { points to mmap secondary mem aperture of card }
  61. aperture_size: Longint; { size of aperture in KB if size>=videomemory.}
  62. set_aperture_page: procedure (page: Longint);
  63. { if aperture_size<videomemory select a memory page }
  64. extensions: Pointer; { points to copy of eeprom for mach32 }
  65. { depends from actual driver/chiptype.. etc. }
  66. end;
  67. PGraphicsContext = ^TGraphicsContext;
  68. TGraphicsContext = record
  69. ModeType: Byte;
  70. ModeFlags: Byte;
  71. Dummy: Byte;
  72. FlipPage: Byte;
  73. Width: LongInt;
  74. Height: LongInt;
  75. BytesPerPixel: LongInt;
  76. Colors: LongInt;
  77. BitsPerPixel: LongInt;
  78. ByteWidth: LongInt;
  79. VBuf: pointer;
  80. Clip: LongInt;
  81. ClipX1: LongInt;
  82. ClipY1: LongInt;
  83. ClipX2: LongInt;
  84. ClipY2: LongInt;
  85. ff: pointer;
  86. end;
  87. var
  88. OldIO : TermIos;
  89. ptcconsole:TPTCconsole;
  90. ptcsurface:TPTCSurface;
  91. ptcformat:TPTCFormat;
  92. Procedure SetRawMode(b:boolean);
  93. Var
  94. Tio : Termios;
  95. Begin
  96. if b then
  97. begin
  98. TCGetAttr(1,Tio);
  99. OldIO:=Tio;
  100. CFMakeRaw(Tio);
  101. end
  102. else
  103. Tio:=OldIO;
  104. TCSetAttr(1,TCSANOW,Tio);
  105. End;
  106. { ---------------------------------------------------------------------
  107. Required procedures
  108. ---------------------------------------------------------------------}
  109. var
  110. LastColor: smallint; {Cache the last set color to improve speed}
  111. procedure ptc_savevideostate;
  112. begin
  113. end;
  114. procedure ptc_restorevideostate;
  115. begin
  116. { vga_setmode(0);}
  117. end;
  118. {
  119. const
  120. BgiColors: array[0..15] of LongInt
  121. = ($000000, $000020, $002000, $002020,
  122. $200000, $200020, $202000, $303030,
  123. $202020, $00003F, $003F00, $003F3F,
  124. $3F0000, $3F003F, $3F3F00, $3F3F3F);
  125. }
  126. procedure InitColors(nrColors: longint);
  127. var
  128. i: smallint;
  129. begin
  130. { for i:=0 to nrColors do
  131. vga_setpalette(I,DefaultColors[i].red shr 2,
  132. DefaultColors[i].green shr 2,DefaultColors[i].blue shr 2)}
  133. end;
  134. procedure ptc_initmodeproc;
  135. begin
  136. writeln('Initializing mode');
  137. { create format }
  138. ptcformat:=TPTCFormat.Create(16,$f800,$07e0,$001f);
  139. { open the console }
  140. ptcconsole.open(paramstr(0),ptcformat);
  141. { create surface matching console dimensions }
  142. ptcsurface:=TPTCSurface.Create(ptcconsole.width,ptcconsole.height,ptcformat);
  143. end;
  144. Function ClipCoords (Var X,Y : smallint) : Boolean;
  145. { Adapt to viewport, return TRUE if still in viewport,
  146. false if outside viewport}
  147. begin
  148. X:= X + StartXViewPort;
  149. Y:= Y + StartYViewPort;
  150. ClipCoords:=Not ClipPixels;
  151. if ClipPixels then
  152. Begin
  153. ClipCoords:=(X < StartXViewPort) or (X > (StartXViewPort + ViewWidth));
  154. ClipCoords:=ClipCoords or
  155. ((Y < StartYViewPort) or (Y > (StartYViewPort + ViewHeight)));
  156. ClipCoords:=Not ClipCoords;
  157. end;
  158. end;
  159. procedure ptc_directpixelproc_16bpp(X,Y: smallint);
  160. var color:word;
  161. pixels:Pword;
  162. begin
  163. case CurrentWriteMode of
  164. XORPut:
  165. begin
  166. { getpixel wants local/relative coordinates }
  167. Color := GetPixel(x-StartXViewPort,y-StartYViewPort);
  168. Color := CurrentColor Xor Color;
  169. end;
  170. OrPut:
  171. begin
  172. { getpixel wants local/relative coordinates }
  173. Color := GetPixel(x-StartXViewPort,y-StartYViewPort);
  174. Color := CurrentColor Or Color;
  175. end;
  176. AndPut:
  177. begin
  178. { getpixel wants local/relative coordinates }
  179. Color := GetPixel(x-StartXViewPort,y-StartYViewPort);
  180. Color := CurrentColor And Color;
  181. end;
  182. NotPut:
  183. begin
  184. Color := Not Color;
  185. end
  186. else
  187. Color:=CurrentColor;
  188. end;
  189. pixels:=ptcsurface.lock;
  190. {Plot the pixel on the surface.}
  191. pixels[x+y*ptcsurface.width]:=color;
  192. ptcsurface.unlock;
  193. { copy to console }
  194. ptcsurface.copy(ptcconsole);
  195. { update console }
  196. ptcconsole.update;
  197. end;
  198. procedure ptc_putpixelproc_16bpp(X,Y:smallint;Color:Word);
  199. var pixels:Pword;
  200. begin
  201. if clipcoords(X,Y) then
  202. begin
  203. pixels:=ptcsurface.lock;
  204. { pixels:=ptcconsole.lock;}
  205. {Plot the pixel on the surface.}
  206. pixels[x+y*ptcsurface.width]:=color;
  207. ptcsurface.unlock;
  208. { copy to console }
  209. ptcsurface.copy(ptcconsole);
  210. { update console }
  211. ptcconsole.update;
  212. end;
  213. end;
  214. function ptc_getpixelproc_16bpp(X,Y: smallint):word;
  215. var pixels:Pword;
  216. begin
  217. if clipcoords(X,Y) then
  218. begin
  219. pixels:=ptcsurface.lock;
  220. {Get the pixel from the surface.}
  221. ptc_getpixelproc_16bpp:=pixels[x+y*ptcsurface.width];
  222. ptcsurface.unlock;
  223. end;
  224. end;
  225. { Bitmap utilities }
  226. {type
  227. PBitmap = ^TBitmap;
  228. TBitmap = record
  229. Width, Height: smallint;
  230. Data: record end;
  231. end;
  232. }
  233. procedure ptc_putimageproc (X,Y: smallint; var Bitmap; BitBlt: Word);
  234. begin
  235. end;
  236. procedure ptc_getimageproc (X1,Y1,X2,Y2: smallint; Var Bitmap);
  237. begin
  238. end;
  239. function ptc_imagesizeproc (X1,Y1,X2,Y2: smallint): longint;
  240. begin
  241. end;
  242. procedure ptc_hlineproc_16bpp(x, x2,y : smallint);
  243. var pixels:Pword;
  244. i:word;
  245. begin
  246. {Clip.}
  247. if (y<0) or (y>viewheight) then
  248. exit;
  249. if x<0 then
  250. x:=0;
  251. if x>viewwidth then
  252. x:=viewwidth;
  253. if x2<0 then
  254. x2:=0;
  255. if x>viewwidth then
  256. x2:=viewwidth;
  257. pixels:=ptcsurface.lock;
  258. inc(x,StartXViewPort);
  259. inc(x2,StartXViewPort);
  260. inc(y,StartXViewPort);
  261. {Plot the pixel on the surface.}
  262. for i:=x to x2 do
  263. pixels[i+y*ptcsurface.width]:=$ffff;
  264. ptcsurface.unlock;
  265. { copy to console }
  266. ptcsurface.copy(ptcconsole);
  267. { update console }
  268. ptcconsole.update;
  269. end;
  270. procedure ptc_vlineproc (x,y,y2: smallint);
  271. begin
  272. end;
  273. procedure ptc_clrviewproc_16bpp;
  274. Var I,Xmax : longint;
  275. begin
  276. Xmax:=StartXViewPort+ViewWidth-1;
  277. For i:=StartYViewPort to StartYViewPort+ViewHeight-1 do
  278. ptc_hlineproc_16bpp(0,viewwidth,i);
  279. { reset coordinates }
  280. CurrentX := 0;
  281. CurrentY := 0;
  282. end;
  283. procedure ptc_patternlineproc (x1,x2,y: smallint);
  284. begin
  285. end;
  286. procedure ptc_ellipseproc (X,Y: smallint;XRadius: word;
  287. YRadius:word; stAngle,EndAngle: word; fp: PatternLineProc);
  288. begin
  289. end;
  290. procedure ptc_lineproc (X1, Y1, X2, Y2 : smallint);
  291. begin
  292. end;
  293. procedure ptc_getscanlineproc (X1,X2,Y : smallint; var data);
  294. begin
  295. end;
  296. procedure ptc_setactivepageproc (page: word);
  297. begin
  298. end;
  299. procedure ptc_setvisualpageproc (page: word);
  300. begin
  301. end;
  302. procedure ptc_savestateproc;
  303. begin
  304. end;
  305. procedure ptc_restorestateproc;
  306. begin
  307. end;
  308. procedure ptc_setrgbpaletteproc(ColorNum, RedValue, GreenValue, BlueValue: smallint);
  309. begin
  310. { vga_setpalette(ColorNum,RedValue shr 2,GreenValue shr 2,BlueValue shr 2);}
  311. end;
  312. procedure ptc_getrgbpaletteproc (ColorNum: smallint;
  313. var RedValue, GreenValue, BlueValue: smallint);
  314. Var R,G,B : longint;
  315. begin
  316. { vga_getpalette(ColorNum,R,G,B);}
  317. RedValue:=R * 255 div 63;
  318. GreenValue:=G * 255 div 63;
  319. BlueValue:=B * 255 div 63;
  320. end;
  321. {************************************************************************}
  322. {* General routines *}
  323. {************************************************************************}
  324. procedure CloseGraph;
  325. Begin
  326. If not isgraphmode then
  327. begin
  328. _graphresult := grnoinitgraph;
  329. exit
  330. end;
  331. SetRawMode(False);
  332. RestoreVideoState;
  333. isgraphmode := false;
  334. end;
  335. function QueryAdapterInfo:PModeInfo;
  336. { This routine returns the head pointer to the list }
  337. { of supported graphics modes. }
  338. { Returns nil if no graphics mode supported. }
  339. { This list is READ ONLY! }
  340. var
  341. graphmode:Tmodeinfo;
  342. ptcmode: PPTCmode;
  343. d,i : longint;
  344. ws,hs:string[5];
  345. const depths:array[0..3] of byte=(8,16,24,32);
  346. colours:array[0..3] of longint=(256,65536,16777216,16777216);
  347. depth_names:array[0..3] of string[5]=('256','64K','16M','16M32');
  348. begin
  349. QueryAdapterInfo := ModeList;
  350. { If the mode listing already exists... }
  351. { simply return it, without changing }
  352. { anything... }
  353. if assigned(ModeList) then
  354. exit;
  355. SaveVideoState:=@ptc_savevideostate;
  356. RestoreVideoState:=@ptc_restorevideostate;
  357. ptcconsole:=TPTCconsole.create;
  358. ptcmode:=ptcconsole.modes;
  359. i:=0;
  360. initmode(graphmode);
  361. with graphmode do
  362. begin
  363. modenumber:=0;
  364. drivernumber:=ptcgraph.ptc;
  365. maxx:=639;
  366. maxy:=479;
  367. modename:='PTC_640x480x64K';
  368. maxcolor:=65536;
  369. palettesize:=65536;
  370. hardwarepages:=0;
  371. InitMode := @ptc_InitModeProc;
  372. DirectPutPixel := @ptc_DirectPixelProc_16bpp;
  373. GetPixel := @ptc_GetPixelProc_16bpp;
  374. PutPixel := @ptc_PutPixelProc_16bpp;
  375. SetRGBPalette := @ptc_SetRGBPaletteProc;
  376. GetRGBPalette := @ptc_GetRGBPaletteProc;
  377. end;
  378. addmode(graphmode);
  379. (*
  380. writeln('processing modes');
  381. while ptcmode^.valid do
  382. begin
  383. for d:=low(depths) to high(depths) do
  384. begin
  385. InitMode(graphmode);
  386. with graphmode do
  387. begin
  388. ModeNumber:=I;
  389. DriverNumber:=ptcgraph.PTC;
  390. { MaxX is number of pixels in X direction - 1}
  391. MaxX:=ptcmode^.width-1;
  392. { same for MaxY}
  393. MaxY:=ptcmode^.height-1;
  394. str(ptcmode^.width,ws);
  395. str(ptcmode^.height,hs);
  396. modename:='PTC_'+ws+'x'+hs+'x'+depth_names[d];
  397. MaxColor := 1 shl ptcmode^.format.r * 1 shl ptcmode^.format.g *1 shl ptcmode^.format.b;
  398. writeln('mode ',modename,' ',maxcolor,'kleuren');
  399. PaletteSize := MaxColor;
  400. HardwarePages := 0;
  401. *)
  402. { necessary hooks ...}
  403. (*
  404. if (MaxColor = 16) and
  405. (LongInt(ModeInfo.Width) * LongInt(ModeInfo.Height) < 65536*4*2) then
  406. begin
  407. {Use optimized graphics routines for 4 bit EGA/VGA modes.}
  408. ScrWidth := ModeInfo.Width div 8;
  409. DirectPutPixel := @DirectPutPixel16;
  410. PutPixel := @PutPixel16;
  411. GetPixel := @GetPixel16;
  412. HLine := @HLine16;
  413. VLine := @VLine16;
  414. GetScanLine := @GetScanLine16;
  415. end
  416. else
  417. *)
  418. (*
  419. begin
  420. DirectPutPixel := @ptc_DirectPixelProc;
  421. GetPixel := @ptc_GetPixelProc;
  422. PutPixel := @ptc_PutPixelProc;
  423. { May be implemented later:
  424. HLine := @libvga_HLineProc;
  425. VLine := @libvga_VLineProc;
  426. GetScanLine := @libvga_GetScanLineProc;}
  427. ClearViewPort := @ptc_ClrViewProc;
  428. end;
  429. SetRGBPalette := @ptc_SetRGBPaletteProc;
  430. GetRGBPalette := @ptc_GetRGBPaletteProc;
  431. { These are not really implemented yet:
  432. PutImage := @libvga_PutImageProc;
  433. GetImage := @libvga_GetImageProc;}
  434. { If you use the default getimage/putimage, you also need the default
  435. imagesize! (JM)
  436. ImageSize := @libvga_ImageSizeProc; }
  437. { Add later maybe ?
  438. SetVisualPage := SetVisualPageProc;
  439. SetActivePage := SetActivePageProc;
  440. Line := @libvga_LineProc;
  441. InternalEllipse:= @libvga_EllipseProc;
  442. PatternLine := @libvga_PatternLineProc;
  443. }
  444. InitMode := @ptc_InitModeProc;
  445. end;
  446. AddMode(graphmode);
  447. inc(i);
  448. end;
  449. end;
  450. *)
  451. end;
  452. initialization
  453. ptcconsole:=TPTCconsole.create;
  454. InitializeGraph;
  455. finalization
  456. ptcconsole.destroy;
  457. end.