vgagraph.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by the Free Pascal development team
  5. svgalib implementation of 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. const
  13. InternalDriverName = 'LinuxVGA';
  14. var SavePtr : Pointer;
  15. { ---------------------------------------------------------------------
  16. SVGA bindings.
  17. ---------------------------------------------------------------------}
  18. { Link with VGA, gl and c libraries }
  19. {$linklib vga}
  20. {$linklib c}
  21. Const
  22. { Text }
  23. WRITEMODE_OVERWRITE = 0;
  24. WRITEMODE_MASKED = 1;
  25. FONT_EXPANDED = 0;
  26. FONT_COMPRESSED = 2;
  27. { Types }
  28. type
  29. pvga_modeinfo = ^vga_modeinfo;
  30. vga_modeinfo = record
  31. width,
  32. height,
  33. bytesperpixel,
  34. colors,
  35. linewidth, { scanline width in bytes }
  36. maxlogicalwidth, { maximum logical scanline width }
  37. startaddressrange, { changeable bits set }
  38. maxpixels, { video memory / bytesperpixel }
  39. haveblit, { mask of blit functions available }
  40. flags: Longint; { other flags }
  41. { Extended fields: }
  42. chiptype, { Chiptype detected }
  43. memory, { videomemory in KB }
  44. linewidth_unit: Longint; { Use only a multiple of this as parameter for set_displaystart }
  45. linear_aperture: PChar; { points to mmap secondary mem aperture of card }
  46. aperture_size: Longint; { size of aperture in KB if size>=videomemory.}
  47. set_aperture_page: procedure (page: Longint);
  48. { if aperture_size<videomemory select a memory page }
  49. extensions: Pointer; { points to copy of eeprom for mach32 }
  50. { depends from actual driver/chiptype.. etc. }
  51. end;
  52. PGraphicsContext = ^TGraphicsContext;
  53. TGraphicsContext = record
  54. ModeType: Byte;
  55. ModeFlags: Byte;
  56. Dummy: Byte;
  57. FlipPage: Byte;
  58. Width: LongInt;
  59. Height: LongInt;
  60. BytesPerPixel: LongInt;
  61. Colors: LongInt;
  62. BitsPerPixel: LongInt;
  63. ByteWidth: LongInt;
  64. VBuf: pointer;
  65. Clip: LongInt;
  66. ClipX1: LongInt;
  67. ClipY1: LongInt;
  68. ClipX2: LongInt;
  69. ClipY2: LongInt;
  70. ff: pointer;
  71. end;
  72. Const
  73. GLASTMODE = 49;
  74. ModeNames : Array[0..GLastMode] of string [18] =
  75. ('Text',
  76. 'G320x200x16',
  77. 'G640x200x16',
  78. 'G640x350x16',
  79. 'G640x480x16',
  80. 'G320x200x256',
  81. 'G320x240x256',
  82. 'G320x400x256',
  83. 'G360x480x256',
  84. 'G640x480x2',
  85. 'G640x480x256',
  86. 'G800x600x256',
  87. 'G1024x768x256',
  88. 'G1280x1024x256',
  89. 'G320x200x32K',
  90. 'G320x200x64K',
  91. 'G320x200x16M',
  92. 'G640x480x32K',
  93. 'G640x480x64K',
  94. 'G640x480x16M',
  95. 'G800x600x32K',
  96. 'G800x600x64K',
  97. 'G800x600x16M',
  98. 'G1024x768x32K',
  99. 'G1024x768x64K',
  100. 'G1024x768x16M',
  101. 'G1280x1024x32K',
  102. 'G1280x1024x64K',
  103. 'G1280x1024x16M',
  104. 'G800x600x16',
  105. '1024x768x16',
  106. '1280x1024x16',
  107. 'G720x348x2',
  108. 'G320x200x16M32',
  109. 'G640x480x16M32',
  110. 'G800x600x16M32',
  111. 'G1024x768x16M32',
  112. 'G1280x1024x16M32',
  113. 'G1152x864x16',
  114. 'G1152x864x256',
  115. 'G1152x864x32K',
  116. 'G1152x864x64K',
  117. 'G1152x864x16M',
  118. 'G1152x864x16M32',
  119. 'G1600x1200x16',
  120. 'G1600x1200x256',
  121. 'G1600x1200x32K',
  122. 'G1600x1200x64K',
  123. 'G1600x1200x16M',
  124. 'G1600x1200x16M32');
  125. var
  126. PhysicalScreen: PGraphicsContext;
  127. { vga functions }
  128. Function vga_init: Longint; Cdecl; External;
  129. Function vga_hasmode(mode: Longint): Boolean; Cdecl; External;
  130. Function vga_getmodeinfo(mode: Longint): pvga_modeinfo; Cdecl; External;
  131. Function vga_setmode(mode: Longint): Longint; Cdecl; External;
  132. Function vga_getcolors: Longint; cdecl;external;
  133. Function vga_setpalette(index: Longint; red: Longint; green: Longint; blue: Longint) : longint; cdecl;external;
  134. Function vga_getpalette(index: Longint; var red: Longint; var green: Longint; var blue: Longint): Longint; cdecl;external;
  135. Function vga_setegacolor(Color: Longint) : longint; cdecl;external;
  136. Function vga_setcolor(color: Longint): Longint; cdecl;external;
  137. Function vga_drawpixel(x, y: Longint): Longint; cdecl;external;
  138. Function vga_getpixel(x, y: Longint): Longint; cdecl;external;
  139. Function vga_drawline(x1, y1, x2, y2: Longint): Longint; cdecl;external;
  140. function vga_screenoff: Longint; Cdecl; External;
  141. function vga_screenon: Longint; Cdecl; External;
  142. { ---------------------------------------------------------------------
  143. Required procedures
  144. ---------------------------------------------------------------------}
  145. procedure libvga_savevideostate;
  146. begin
  147. end;
  148. procedure libvga_restorevideostate;
  149. begin
  150. vga_setmode(0);
  151. end;
  152. const
  153. BgiColors: array[0..15] of LongInt
  154. = ($000000, $000020, $002000, $002020,
  155. $200000, $200020, $202000, $303030,
  156. $202020, $00003F, $003F00, $003F3F,
  157. $3F0000, $3F003F, $3F3F00, $3F3F3F);
  158. procedure InitColors;
  159. var
  160. i: Integer;
  161. begin
  162. for i:=0 to 15 do
  163. vga_setpalette(I,BgiColors[i] shr 16,
  164. (BgiColors[i] shr 8) and 255,
  165. BgiColors[i] and 255)
  166. end;
  167. procedure libvga_initmodeproc;
  168. Var Nrcolors : Longint;
  169. begin
  170. vga_setmode(IntCurrentMode);
  171. vga_screenon;
  172. nrColors:=vga_getcolors;
  173. if (nrColors=16) or (nrcolors=256) then
  174. InitColors;
  175. end;
  176. Function ClipCoords (Var X,Y : Integer) : Boolean;
  177. { Adapt to viewport, return TRUE if still in viewport,
  178. false if outside viewport}
  179. begin
  180. X:= X + StartXViewPort;
  181. Y:= Y + StartYViewPort;
  182. ClipCoords:=Not ClipPixels;
  183. if ClipPixels then
  184. Begin
  185. ClipCoords:=(X < StartXViewPort) or (X > (StartXViewPort + ViewWidth));
  186. ClipCoords:=ClipCoords or
  187. ((Y < StartYViewPort) or (Y > (StartYViewPort + ViewHeight)));
  188. ClipCoords:=Not ClipCoords;
  189. end;
  190. end;
  191. procedure libvga_directpixelproc(X,Y: Integer);
  192. Var Color : Word;
  193. begin
  194. case CurrentWriteMode of
  195. XORPut:
  196. begin
  197. { getpixel wants local/relative coordinates }
  198. Color := GetPixel(x-StartXViewPort,y-StartYViewPort);
  199. Color := CurrentColor Xor Color;
  200. end;
  201. OrPut:
  202. begin
  203. { getpixel wants local/relative coordinates }
  204. Color := GetPixel(x-StartXViewPort,y-StartYViewPort);
  205. Color := CurrentColor Or Color;
  206. end;
  207. AndPut:
  208. begin
  209. { getpixel wants local/relative coordinates }
  210. Color := GetPixel(x-StartXViewPort,y-StartYViewPort);
  211. Color := CurrentColor And Color;
  212. end;
  213. NotPut:
  214. begin
  215. Color := Not Color;
  216. end
  217. else
  218. Color:=CurrentColor;
  219. end;
  220. vga_setegaColor(Color);
  221. vga_drawpixel(x, y);
  222. end;
  223. procedure libvga_putpixelproc(X,Y: Integer; Color: Word);
  224. begin
  225. If Not ClipCoords(X,Y) Then exit;
  226. vga_setegaColor(Color);
  227. vga_drawpixel(x, y);
  228. end;
  229. function libvga_getpixelproc (X,Y: Integer): word;
  230. begin
  231. ClipCoords(X,Y);
  232. libvga_getpixelproc:=vga_getpixel(x, y);
  233. end;
  234. procedure libvga_clrviewproc;
  235. Var I,Xmax : longint;
  236. begin
  237. vga_SetegaColor(CurrentBkColor);
  238. Xmax:=StartXViewPort+ViewWidth-1;
  239. For i:=StartYViewPort to StartYViewPort+ViewHeight-1 do
  240. vga_drawline(StartXViewPort,I,Xmax,I);
  241. end;
  242. { Bitmap utilities }
  243. type
  244. PBitmap = ^TBitmap;
  245. TBitmap = record
  246. Width, Height: Integer;
  247. Data: record end;
  248. end;
  249. procedure libvga_putimageproc (X,Y: Integer; var Bitmap; BitBlt: Word);
  250. begin
  251. {
  252. With TBitMap(BitMap) do
  253. gl_putbox(x, y, width, height, @Data);
  254. }
  255. end;
  256. procedure libvga_getimageproc (X1,Y1,X2,Y2: Integer; Var Bitmap);
  257. begin
  258. { with TBitmap(Bitmap) do
  259. begin
  260. Width := x2 - x1 + 1;
  261. Height := y2 - y1 + 1;
  262. gl_getbox(x1,y1, x2 - x1 + 1, y2 - y1 + 1, @Data);
  263. end;
  264. }
  265. end;
  266. function libvga_imagesizeproc (X1,Y1,X2,Y2: Integer): longint;
  267. begin
  268. libvga_imagesizeproc := SizeOf(TBitmap) + (x2 - x1 + 1) * (y2 - y1 + 1) * PhysicalScreen^.BytesPerPixel;
  269. end;
  270. procedure libvga_hlineproc (x, x2,y : integer);
  271. begin
  272. end;
  273. procedure libvga_vlineproc (x,y,y2: integer);
  274. begin
  275. end;
  276. procedure libvga_patternlineproc (x1,x2,y: integer);
  277. begin
  278. end;
  279. procedure libvga_ellipseproc (X,Y: Integer;XRadius: word;
  280. YRadius:word; stAngle,EndAngle: word; fp: PatternLineProc);
  281. begin
  282. end;
  283. procedure libvga_lineproc (X1, Y1, X2, Y2 : Integer);
  284. begin
  285. end;
  286. procedure libvga_getscanlineproc (X1,X2,Y : integer; var data);
  287. begin
  288. end;
  289. procedure libvga_setactivepageproc (page: word);
  290. begin
  291. end;
  292. procedure libvga_setvisualpageproc (page: word);
  293. begin
  294. end;
  295. procedure libvga_savestateproc;
  296. begin
  297. end;
  298. procedure libvga_restorestateproc;
  299. begin
  300. end;
  301. procedure libvga_setrgbpaletteproc(ColorNum, RedValue, GreenValue, BlueValue: Integer);
  302. begin
  303. vga_setpalette(ColorNum,RedValue shr 2,GreenValue shr 2,BlueValue shr 2);
  304. end;
  305. procedure libvga_getrgbpaletteproc (ColorNum: integer;
  306. var RedValue, GreenValue, BlueValue: Integer);
  307. Var R,G,B : longint;
  308. begin
  309. vga_getpalette(ColorNum,R,G,B);
  310. RedValue:=R * 255 div 63;
  311. GreenValue:=G * 255 div 63;
  312. BlueValue:=B * 255 div 63;
  313. end;
  314. {************************************************************************}
  315. {* General routines *}
  316. {************************************************************************}
  317. procedure CloseGraph;
  318. Begin
  319. If not isgraphmode then
  320. begin
  321. _graphresult := grnoinitgraph;
  322. exit
  323. end;
  324. RestoreVideoState;
  325. isgraphmode := false;
  326. end;
  327. function QueryAdapterInfo:PModeInfo;
  328. { This routine returns the head pointer to the list }
  329. { of supported graphics modes. }
  330. { Returns nil if no graphics mode supported. }
  331. { This list is READ ONLY! }
  332. var
  333. mode: TModeInfo;
  334. modeinfo : vga_modeinfo;
  335. i : longint;
  336. begin
  337. QueryAdapterInfo := ModeList;
  338. { If the mode listing already exists... }
  339. { simply return it, without changing }
  340. { anything... }
  341. if assigned(ModeList) then
  342. exit;
  343. SaveVideoState:=libvga_savevideostate;
  344. RestoreVideoState:=libvga_restorevideostate;
  345. vga_init;
  346. For I:=0 to GLastMode do
  347. begin
  348. If vga_hasmode(I) then
  349. begin
  350. ModeInfo:=vga_getmodeinfo(i)^;
  351. InitMode(Mode);
  352. With Mode do
  353. begin
  354. ModeNumber:=I;
  355. ModeName:=ModeNames[i];
  356. // Pretend we're VGA always.
  357. DriverNumber := VGA;
  358. MaxX:=ModeInfo.Width;
  359. MaxY:=ModeInfo.height;
  360. MaxColor := ModeInfo.colors;
  361. PaletteSize := MaxColor;
  362. HardwarePages := 0;
  363. // necessary hooks ...
  364. DirectPutPixel := @libvga_DirectPixelProc;
  365. GetPixel := @Libvga_GetPixelProc;
  366. PutPixel := @libvga_PutPixelProc;
  367. SetRGBPalette := @libvga_SetRGBPaletteProc;
  368. GetRGBPalette := @libvga_GetRGBPaletteProc;
  369. ClearViewPort := libvga_ClrViewProc;
  370. PutImage := @Libvga_PutImageProc;
  371. GetImage := @libvga_GetImageProc;
  372. ImageSize := @libvga_ImageSizeProc;
  373. { Add later maybe ?
  374. SetVisualPage := SetVisualPageProc;
  375. SetActivePage := SetActivePageProc;
  376. GetScanLine := @libvga_GetScanLineProc;
  377. Line := @libvga_LineProc;
  378. InternalEllipse:= @libvga_EllipseProc;
  379. PatternLine := @libvga_PatternLineProc;
  380. HLine := @libvga_HLineProc;
  381. VLine := @libvga_VLineProc;
  382. }
  383. InitMode := @libvga_InitModeProc;
  384. end;
  385. AddMode(Mode);
  386. end;
  387. end;
  388. end;
  389. {
  390. $Log$
  391. Revision 1.8 2000-02-06 11:26:45 sg
  392. * Fixed SetRGBPalette and GetRGBPalette (hopefully; not tested)
  393. Revision 1.7 2000/02/06 01:48:55 sg
  394. * Fixed the default palette. libsvga works with a RGB range from 0-63, not
  395. 0-255!
  396. * PutPixel fixed (pixels didn't get drawn before)
  397. Revision 1.6 2000/02/03 20:39:58 michael
  398. + Version using only vgalib
  399. Revision 1.5 2000/01/07 16:41:42 daniel
  400. * copyright 2000
  401. Revision 1.4 1999/12/20 11:22:38 peter
  402. * modes moved to interface
  403. * integer -> smallint
  404. Revision 1.3 1999/12/11 23:41:39 jonas
  405. * changed definition of getscanlineproc to "getscanline(x1,x2,y:
  406. integer; var data);" so it can be used by getimage too
  407. * changed getimage so it uses getscanline
  408. * changed floodfill, getscanline16 and definitions in Linux
  409. include files so they use this new format
  410. + getscanlineVESA256 for 256 color VESA modes (banked)
  411. Revision 1.2 1999/11/08 00:08:43 michael
  412. * Fist working version of svgalib new graph unit
  413. * Initial implementation of ggi new graph unit
  414. Revision 1.1 1999/11/07 16:57:26 michael
  415. + Start of common graph implementation
  416. }