graph.inc 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131
  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. Graph unit implementation part
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. var
  12. ExitSave: pointer;
  13. const
  14. firstCallOfInitGraph: boolean = true;
  15. {$ifdef logging}
  16. var debuglog: text;
  17. function strf(l: longint): string;
  18. begin
  19. str(l, strf)
  20. end;
  21. Procedure Log(Const s: String);
  22. Begin
  23. Append(debuglog);
  24. Write(debuglog, s);
  25. Close(debuglog);
  26. End;
  27. Procedure LogLn(Const s: string);
  28. Begin
  29. Append(debuglog);
  30. Writeln(debuglog,s);
  31. Close(debuglog);
  32. End;
  33. {$endif logging}
  34. const
  35. StdBufferSize = 4096; { Buffer size for FloodFill }
  36. type
  37. tinttable = array[0..16383] of smallint;
  38. pinttable = ^tinttable;
  39. WordArray = Array [0..StdbufferSize] Of word;
  40. PWordArray = ^WordArray;
  41. const
  42. { Mask for each bit in byte used to determine pattern }
  43. BitArray: Array[0..7] of byte =
  44. ($01,$02,$04,$08,$10,$20,$40,$80);
  45. RevbitArray: Array[0..7] of byte =
  46. ($80,$40,$20,$10,$08,$04,$02,$01);
  47. { pre expanded line patterns }
  48. { 0 = LSB of byte pattern }
  49. { 15 = MSB of byte pattern }
  50. LinePatterns: Array[0..15] of BOOLEAN =
  51. (TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,
  52. TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE);
  53. const
  54. BGIPath : string = '.';
  55. { Default font 8x8 system from IBM PC }
  56. {$i fontdata.inc}
  57. var
  58. CurrentColor: Word;
  59. CurrentBkColor: Word;
  60. CurrentX : smallint; { viewport relative }
  61. CurrentY : smallint; { viewport relative }
  62. ClipPixels: Boolean; { Should cliiping be enabled }
  63. CurrentWriteMode: smallint;
  64. _GraphResult : smallint;
  65. LineInfo : LineSettingsType;
  66. FillSettings: FillSettingsType;
  67. { information for Text Output routines }
  68. CurrentTextInfo : TextSettingsType;
  69. CurrentXRatio, CurrentYRatio: graph_float;
  70. installedfonts: longint; { Number of installed fonts }
  71. StartXViewPort: smallint; { absolute }
  72. StartYViewPort: smallint; { absolute }
  73. ViewWidth : smallint;
  74. ViewHeight: smallint;
  75. IsGraphMode : Boolean; { Indicates if we are in graph mode or not }
  76. ArcCall: ArcCoordsType; { Information on the last call to Arc or Ellipse }
  77. var
  78. { ******************** HARDWARE INFORMATION ********************* }
  79. { Should be set in InitGraph once only. }
  80. IntCurrentMode : smallint;
  81. IntCurrentDriver : smallint; { Currently loaded driver }
  82. IntCurrentNewDriver: smallint;
  83. XAspect : word;
  84. YAspect : word;
  85. MaxX : smallint; { Maximum resolution - ABSOLUTE }
  86. MaxY : smallint; { Maximum resolution - ABSOLUTE }
  87. MaxColor : Longint;
  88. PaletteSize : longint; { Maximum palette entry we can set, usually equal}
  89. { maxcolor. }
  90. HardwarePages : byte; { maximum number of hardware visual pages }
  91. DriverName: String;
  92. DirectColor : Boolean ; { Is it a direct color mode? }
  93. ModeList : PModeInfo;
  94. newModeList: TNewModeInfo;
  95. DirectVideo : Boolean; { Direct access to video memory? }
  96. {--------------------------------------------------------------------------}
  97. { }
  98. { LINE AND LINE RELATED ROUTINES }
  99. { }
  100. {--------------------------------------------------------------------------}
  101. {$i clip.inc}
  102. procedure HLineDefault(x,x2,y: smallint); {$ifndef fpc}far;{$endif fpc}
  103. var
  104. xtmp: smallint;
  105. Begin
  106. { must we swap the values? }
  107. if x >= x2 then
  108. Begin
  109. xtmp := x2;
  110. x2 := x;
  111. x:= xtmp;
  112. end;
  113. { First convert to global coordinates }
  114. X := X + StartXViewPort;
  115. X2 := X2 + StartXViewPort;
  116. Y := Y + StartYViewPort;
  117. if ClipPixels then
  118. Begin
  119. if LineClipped(x,y,x2,y,StartXViewPort,StartYViewPort,
  120. StartXViewPort+ViewWidth, StartYViewPort+ViewHeight) then
  121. exit;
  122. end;
  123. for x:= x to x2 do
  124. DirectPutPixel(X,Y);
  125. end;
  126. procedure VLineDefault(x,y,y2: smallint); {$ifndef fpc}far;{$endif fpc}
  127. var
  128. ytmp: smallint;
  129. Begin
  130. { must we swap the values? }
  131. if y >= y2 then
  132. Begin
  133. ytmp := y2;
  134. y2 := y;
  135. y:= ytmp;
  136. end;
  137. { First convert to global coordinates }
  138. X := X + StartXViewPort;
  139. Y2 := Y2 + StartYViewPort;
  140. Y := Y + StartYViewPort;
  141. if ClipPixels then
  142. Begin
  143. if LineClipped(x,y,x,y2,StartXViewPort,StartYViewPort,
  144. StartXViewPort+ViewWidth, StartYViewPort+ViewHeight) then
  145. exit;
  146. end;
  147. for y := y to y2 do Directputpixel(x,y)
  148. End;
  149. Procedure DirectPutPixelClip(x,y: smallint);
  150. { for thickwidth lines, because they may call DirectPutPixel for coords }
  151. { outside the current viewport (bug found by CEC) }
  152. Begin
  153. If (Not ClipPixels) Or
  154. ((X >= StartXViewPort) And (X <= (StartXViewPort + ViewWidth)) And
  155. (Y >= StartYViewPort) And (Y <= (StartYViewPort + ViewHeight))) then
  156. Begin
  157. DirectPutPixel(x,y)
  158. End
  159. End;
  160. procedure LineDefault(X1, Y1, X2, Y2: smallint); {$ifndef fpc}far;{$endif fpc}
  161. var X, Y : smallint;
  162. deltax, deltay : smallint;
  163. d, dinc1, dinc2: smallint;
  164. xinc1 : smallint;
  165. xinc2 : smallint;
  166. yinc1 : smallint;
  167. yinc2 : smallint;
  168. i : smallint;
  169. Flag : Boolean; { determines pixel direction in thick lines }
  170. NumPixels : smallint;
  171. PixelCount : smallint;
  172. OldCurrentColor: Word;
  173. swtmp : smallint;
  174. TmpNumPixels : smallint;
  175. begin
  176. {******************************************}
  177. { SOLID LINES }
  178. {******************************************}
  179. if lineinfo.LineStyle = SolidLn then
  180. Begin
  181. { we separate normal and thick width for speed }
  182. { and because it would not be 100% compatible }
  183. { with the TP graph unit otherwise }
  184. if y1 = y2 then
  185. Begin
  186. {******************************************}
  187. { SOLID LINES HORIZONTAL }
  188. {******************************************}
  189. if lineinfo.Thickness=NormWidth then
  190. hline(x1,x2,y2)
  191. else
  192. begin
  193. { thick width }
  194. hline(x1,x2,y2-1);
  195. hline(x1,x2,y2);
  196. hline(x2,x2,y2+1);
  197. end;
  198. end
  199. else
  200. if x1 = x2 then
  201. Begin
  202. {******************************************}
  203. { SOLID LINES VERTICAL }
  204. {******************************************}
  205. if lineinfo.Thickness=NormWidth then
  206. vline(x1,y1,y2)
  207. else
  208. begin
  209. { thick width }
  210. vline(x1-1,y1,y2);
  211. vline(x1,y1,y2);
  212. vline(x1+1,y1,y2);
  213. end;
  214. end
  215. else
  216. begin
  217. { Convert to global coordinates. }
  218. x1 := x1 + StartXViewPort;
  219. x2 := x2 + StartXViewPort;
  220. y1 := y1 + StartYViewPort;
  221. y2 := y2 + StartYViewPort;
  222. { if fully clipped then exit... }
  223. if ClipPixels then
  224. begin
  225. if LineClipped(x1,y1,x2,y2,StartXViewPort, StartYViewPort,
  226. StartXViewPort+ViewWidth, StartYViewPort+ViewHeight) then
  227. exit;
  228. end;
  229. {******************************************}
  230. { SLOPED SOLID LINES }
  231. {******************************************}
  232. oldCurrentColor :=
  233. CurrentColor;
  234. { Calculate deltax and deltay for initialisation }
  235. deltax := abs(x2 - x1);
  236. deltay := abs(y2 - y1);
  237. { Initialize all vars based on which is the independent variable }
  238. if deltax >= deltay then
  239. begin
  240. Flag := FALSE;
  241. { x is independent variable }
  242. numpixels := deltax + 1;
  243. d := (2 * deltay) - deltax;
  244. dinc1 := deltay Shl 1;
  245. dinc2 := (deltay - deltax) shl 1;
  246. xinc1 := 1;
  247. xinc2 := 1;
  248. yinc1 := 0;
  249. yinc2 := 1;
  250. end
  251. else
  252. begin
  253. Flag := TRUE;
  254. { y is independent variable }
  255. numpixels := deltay + 1;
  256. d := (2 * deltax) - deltay;
  257. dinc1 := deltax Shl 1;
  258. dinc2 := (deltax - deltay) shl 1;
  259. xinc1 := 0;
  260. xinc2 := 1;
  261. yinc1 := 1;
  262. yinc2 := 1;
  263. end;
  264. { Make sure x and y move in the right directions }
  265. if x1 > x2 then
  266. begin
  267. xinc1 := - xinc1;
  268. xinc2 := - xinc2;
  269. end;
  270. if y1 > y2 then
  271. begin
  272. yinc1 := - yinc1;
  273. yinc2 := - yinc2;
  274. end;
  275. { Start drawing at <x1, y1> }
  276. x := x1;
  277. y := y1;
  278. If LineInfo.Thickness=NormWidth then
  279. Begin
  280. { Draw the pixels }
  281. for i := 1 to numpixels do
  282. begin
  283. DirectPutPixel(x, y);
  284. if d < 0 then
  285. begin
  286. d := d + dinc1;
  287. x := x + xinc1;
  288. y := y + yinc1;
  289. end
  290. else
  291. begin
  292. d := d + dinc2;
  293. x := x + xinc2;
  294. y := y + yinc2;
  295. end;
  296. CurrentColor := OldCurrentColor;
  297. end;
  298. end
  299. else
  300. { Thick width lines }
  301. begin
  302. { Draw the pixels }
  303. for i := 1 to numpixels do
  304. begin
  305. { all depending on the slope, we can determine }
  306. { in what direction the extra width pixels will be put }
  307. If Flag then
  308. Begin
  309. DirectPutPixelClip(x-1,y);
  310. DirectPutPixelClip(x,y);
  311. DirectPutPixelClip(x+1,y);
  312. end
  313. else
  314. Begin
  315. DirectPutPixelClip(x, y-1);
  316. DirectPutPixelClip(x, y);
  317. DirectPutPixelClip(x, y+1);
  318. end;
  319. if d < 0 then
  320. begin
  321. d := d + dinc1;
  322. x := x + xinc1;
  323. y := y + yinc1;
  324. end
  325. else
  326. begin
  327. d := d + dinc2;
  328. x := x + xinc2;
  329. y := y + yinc2;
  330. end;
  331. CurrentColor := OldCurrentColor;
  332. end;
  333. end;
  334. end;
  335. end
  336. else
  337. {******************************************}
  338. { begin patterned lines }
  339. {******************************************}
  340. Begin
  341. { Convert to global coordinates. }
  342. x1 := x1 + StartXViewPort;
  343. x2 := x2 + StartXViewPort;
  344. y1 := y1 + StartYViewPort;
  345. y2 := y2 + StartYViewPort;
  346. { if fully clipped then exit... }
  347. if ClipPixels then
  348. begin
  349. if LineClipped(x1,y1,x2,y2,StartXViewPort, StartYViewPort,
  350. StartXViewPort+ViewWidth, StartYViewPort+ViewHeight) then
  351. exit;
  352. end;
  353. OldCurrentColor := CurrentColor;
  354. PixelCount:=0;
  355. if y1 = y2 then
  356. Begin
  357. { Check if we must swap }
  358. if x1 >= x2 then
  359. Begin
  360. swtmp := x1;
  361. x1 := x2;
  362. x2 := swtmp;
  363. end;
  364. if LineInfo.Thickness = NormWidth then
  365. Begin
  366. for PixelCount:=x1 to x2 do
  367. { optimization: PixelCount mod 16 }
  368. if LinePatterns[PixelCount and 15] = TRUE then
  369. begin
  370. DirectPutPixel(PixelCount,y2);
  371. end;
  372. end
  373. else
  374. Begin
  375. for i:=-1 to 1 do
  376. Begin
  377. for PixelCount:=x1 to x2 do
  378. { Optimization from Thomas - mod 16 = and 15 }
  379. {this optimization has been performed by the compiler
  380. for while as well (JM)}
  381. if LinePatterns[PixelCount and 15] = TRUE then
  382. begin
  383. DirectPutPixelClip(PixelCount,y2+i);
  384. end;
  385. end;
  386. end;
  387. end
  388. else
  389. if x1 = x2 then
  390. Begin
  391. { Check if we must swap }
  392. if y1 >= y2 then
  393. Begin
  394. swtmp := y1;
  395. y1 := y2;
  396. y2 := swtmp;
  397. end;
  398. if LineInfo.Thickness = NormWidth then
  399. Begin
  400. for PixelCount:=y1 to y2 do
  401. { compare if we should plot a pixel here , compare }
  402. { with predefined line patterns... }
  403. if LinePatterns[PixelCount and 15] = TRUE then
  404. begin
  405. DirectPutPixel(x1,PixelCount);
  406. end;
  407. end
  408. else
  409. Begin
  410. for i:=-1 to 1 do
  411. Begin
  412. for PixelCount:=y1 to y2 do
  413. { compare if we should plot a pixel here , compare }
  414. { with predefined line patterns... }
  415. if LinePatterns[PixelCount and 15] = TRUE then
  416. begin
  417. DirectPutPixelClip(x1+i,PixelCount);
  418. end;
  419. end;
  420. end;
  421. end
  422. else
  423. Begin
  424. oldCurrentColor := CurrentColor;
  425. { Calculate deltax and deltay for initialisation }
  426. deltax := abs(x2 - x1);
  427. deltay := abs(y2 - y1);
  428. { Initialize all vars based on which is the independent variable }
  429. if deltax >= deltay then
  430. begin
  431. Flag := FALSE;
  432. { x is independent variable }
  433. numpixels := deltax + 1;
  434. d := (2 * deltay) - deltax;
  435. dinc1 := deltay Shl 1;
  436. dinc2 := (deltay - deltax) shl 1;
  437. xinc1 := 1;
  438. xinc2 := 1;
  439. yinc1 := 0;
  440. yinc2 := 1;
  441. end
  442. else
  443. begin
  444. Flag := TRUE;
  445. { y is independent variable }
  446. numpixels := deltay + 1;
  447. d := (2 * deltax) - deltay;
  448. dinc1 := deltax Shl 1;
  449. dinc2 := (deltax - deltay) shl 1;
  450. xinc1 := 0;
  451. xinc2 := 1;
  452. yinc1 := 1;
  453. yinc2 := 1;
  454. end;
  455. { Make sure x and y move in the right directions }
  456. if x1 > x2 then
  457. begin
  458. xinc1 := - xinc1;
  459. xinc2 := - xinc2;
  460. end;
  461. if y1 > y2 then
  462. begin
  463. yinc1 := - yinc1;
  464. yinc2 := - yinc2;
  465. end;
  466. { Start drawing at <x1, y1> }
  467. x := x1;
  468. y := y1;
  469. If LineInfo.Thickness=ThickWidth then
  470. Begin
  471. TmpNumPixels := NumPixels-1;
  472. { Draw the pixels }
  473. for i := 0 to TmpNumPixels do
  474. begin
  475. { all depending on the slope, we can determine }
  476. { in what direction the extra width pixels will be put }
  477. If Flag then
  478. Begin
  479. { compare if we should plot a pixel here , compare }
  480. { with predefined line patterns... }
  481. if LinePatterns[i and 15] = TRUE then
  482. begin
  483. DirectPutPixelClip(x-1,y);
  484. DirectPutPixelClip(x,y);
  485. DirectPutPixelClip(x+1,y);
  486. end;
  487. end
  488. else
  489. Begin
  490. { compare if we should plot a pixel here , compare }
  491. { with predefined line patterns... }
  492. if LinePatterns[i and 15] = TRUE then
  493. begin
  494. DirectPutPixelClip(x,y-1);
  495. DirectPutPixelClip(x,y);
  496. DirectPutPixelClip(x,y+1);
  497. end;
  498. end;
  499. if d < 0 then
  500. begin
  501. d := d + dinc1;
  502. x := x + xinc1;
  503. y := y + yinc1;
  504. end
  505. else
  506. begin
  507. d := d + dinc2;
  508. x := x + xinc2;
  509. y := y + yinc2;
  510. end;
  511. end;
  512. end
  513. else
  514. Begin
  515. { instead of putting in loop , substract by one now }
  516. TmpNumPixels := NumPixels-1;
  517. { NormWidth }
  518. for i := 0 to TmpNumPixels do
  519. begin
  520. if LinePatterns[i and 15] = TRUE then
  521. begin
  522. DirectPutPixel(x,y);
  523. end;
  524. if d < 0 then
  525. begin
  526. d := d + dinc1;
  527. x := x + xinc1;
  528. y := y + yinc1;
  529. end
  530. else
  531. begin
  532. d := d + dinc2;
  533. x := x + xinc2;
  534. y := y + yinc2;
  535. end;
  536. end;
  537. end
  538. end;
  539. {******************************************}
  540. { end patterned lines }
  541. {******************************************}
  542. { restore color }
  543. CurrentColor:=OldCurrentColor;
  544. end;
  545. end; { Line }
  546. {********************************************************}
  547. { Procedure DummyPatternLine() }
  548. {--------------------------------------------------------}
  549. { This is suimply an procedure that does nothing which }
  550. { can be passed as a patternlineproc for non-filled }
  551. { ellipses }
  552. {********************************************************}
  553. Procedure DummyPatternLine(x1, x2, y: smallint); {$ifdef tp} far; {$endif tp}
  554. begin
  555. end;
  556. {********************************************************}
  557. { Procedure InternalEllipse() }
  558. {--------------------------------------------------------}
  559. { This routine first calculates all points required to }
  560. { draw a circle to the screen, and stores the points }
  561. { to display in a buffer before plotting them. The }
  562. { aspect ratio of the screen is taken into account when }
  563. { calculating the values. }
  564. {--------------------------------------------------------}
  565. { INPUTS: X,Y : Center coordinates of Ellipse. }
  566. { XRadius - X-Axis radius of ellipse. }
  567. { YRadius - Y-Axis radius of ellipse. }
  568. { stAngle, EndAngle: Start angle and end angles of the }
  569. { ellipse (used for partial ellipses and circles) }
  570. { pl: procedure which either draws a patternline (for }
  571. { FillEllipse) or does nothing (arc etc) }
  572. {--------------------------------------------------------}
  573. { NOTE: - }
  574. { - }
  575. {********************************************************}
  576. Procedure InternalEllipseDefault(X,Y: smallint;XRadius: word;
  577. YRadius:word; stAngle,EndAngle: word; pl: PatternLineProc); {$ifndef fpc}far;{$endif fpc}
  578. Const ConvFac = Pi/180.0;
  579. var
  580. j, Delta, DeltaEnd: graph_float;
  581. NumOfPixels: longint;
  582. TempTerm: graph_float;
  583. xtemp, ytemp, xp, yp, xm, ym, xnext, ynext,
  584. plxpyp, plxmyp, plxpym, plxmym: smallint;
  585. BackupColor, TmpAngle, OldLineWidth: word;
  586. Begin
  587. If LineInfo.ThickNess = ThickWidth Then
  588. { first draw the two outer ellipses using normwidth and no filling (JM) }
  589. Begin
  590. OldLineWidth := LineInfo.Thickness;
  591. LineInfo.Thickness := NormWidth;
  592. InternalEllipseDefault(x,y,XRadius,YRadius,StAngle,EndAngle,
  593. {$ifdef fpc}@{$endif fpc}DummyPatternLine);
  594. InternalEllipseDefault(x,y,XRadius+1,YRadius+1,StAngle,EndAngle,
  595. {$ifdef fpc}@{$endif fpc}DummyPatternLine);
  596. If (XRadius > 0) and (YRadius > 0) Then
  597. { draw the smallest ellipse last, since that one will use the }
  598. { original pl, so it could possibly draw patternlines (JM) }
  599. Begin
  600. Dec(XRadius);
  601. Dec(YRadius);
  602. End
  603. Else Exit;
  604. { restore line thickness }
  605. LineInfo.Thickness := OldLineWidth;
  606. End;
  607. { Adjust for screen aspect ratio }
  608. XRadius:=(longint(XRadius)*10000) div XAspect;
  609. YRadius:=(longint(YRadius)*10000) div YAspect;
  610. If xradius = 0 then inc(xradius);
  611. if yradius = 0 then inc(yradius);
  612. { check for an ellipse with negligable x and y radius }
  613. If (xradius <= 1) and (yradius <= 1) then
  614. begin
  615. putpixel(x,y,CurrentColor);
  616. ArcCall.X := X;
  617. ArcCall.Y := Y;
  618. ArcCall.XStart := X;
  619. ArcCall.YStart := Y;
  620. ArcCall.XEnd := X;
  621. ArcCall.YEnd := Y;
  622. exit;
  623. end;
  624. { check if valid angles }
  625. stangle := stAngle mod 361;
  626. EndAngle := EndAngle mod 361;
  627. { if impossible angles then swap them! }
  628. if Endangle < StAngle then
  629. Begin
  630. TmpAngle:=EndAngle;
  631. EndAngle:=StAngle;
  632. Stangle:=TmpAngle;
  633. end;
  634. { approximate the number of pixels required by using the circumference }
  635. { equation of an ellipse. }
  636. { Changed this formula a it (trial and error), but the net result is that }
  637. { less pixels have to be calculated now }
  638. NumOfPixels:=Round(Sqrt(3)*sqrt(sqr(XRadius)+sqr(YRadius)));
  639. { Calculate the angle precision required }
  640. Delta := 90.0 / NumOfPixels;
  641. { for restoring after PatternLine }
  642. BackupColor := CurrentColor;
  643. { removed from inner loop to make faster }
  644. { store some arccall info }
  645. ArcCall.X := X;
  646. ArcCall.Y := Y;
  647. TempTerm := (StAngle)*ConvFac;
  648. ArcCall.XStart := round(XRadius*Cos(TempTerm)) + X;
  649. ArcCall.YStart := round(YRadius*Sin(TempTerm+Pi)) + Y;
  650. TempTerm := (EndAngle)*ConvFac;
  651. ArcCall.XEnd := round(XRadius*Cos(TempTerm)) + X;
  652. ArcCall.YEnd := round(YRadius*Sin(TempTerm+Pi)) + Y;
  653. { Always just go over the first 90 degrees. Could be optimized a }
  654. { bit if StAngle and EndAngle lie in the same quadrant, left as an }
  655. { exercise for the reader :) (JM) }
  656. j := 0;
  657. { calculate stop position, go 1 further than 90 because otherwise }
  658. { 1 pixel is sometimes not drawn (JM) }
  659. DeltaEnd := 91;
  660. { Calculate points }
  661. xnext := XRadius;
  662. ynext := 0;
  663. Repeat
  664. xtemp := xnext;
  665. ytemp := ynext;
  666. { this is used by both sin and cos }
  667. TempTerm := (j+Delta)*ConvFac;
  668. { Calculate points }
  669. xnext := round(XRadius*Cos(TempTerm));
  670. ynext := round(YRadius*Sin(TempTerm+Pi));
  671. xp := x + xtemp;
  672. xm := x - xtemp;
  673. yp := y + ytemp;
  674. ym := y - ytemp;
  675. plxpyp := maxsmallint;
  676. plxmyp := -maxsmallint-1;
  677. plxpym := maxsmallint;
  678. plxmym := -maxsmallint-1;
  679. If (j >= StAngle) and (j <= EndAngle) then
  680. begin
  681. plxpyp := xp;
  682. PutPixel(xp,yp,CurrentColor);
  683. end;
  684. If ((180-j) >= StAngle) and ((180-j) <= EndAngle) then
  685. begin
  686. plxmyp := xm;
  687. PutPixel(xm,yp,CurrentColor);
  688. end;
  689. If ((j+180) >= StAngle) and ((j+180) <= EndAngle) then
  690. begin
  691. plxmym := xm;
  692. PutPixel(xm,ym,CurrentColor);
  693. end;
  694. If ((360-j) >= StAngle) and ((360-j) <= EndAngle) then
  695. begin
  696. plxpym := xp;
  697. PutPixel(xp,ym,CurrentColor);
  698. end;
  699. If (ynext <> ytemp) and
  700. (xp - xm >= 1) then
  701. begin
  702. CurrentColor := FillSettings.Color;
  703. pl(plxmyp+1,plxpyp-1,yp);
  704. pl(plxmym+1,plxpym-1,ym);
  705. CurrentColor := BackupColor;
  706. end;
  707. j:=j+Delta;
  708. Until j > (DeltaEnd);
  709. end;
  710. procedure PatternLineDefault(x1,x2,y: smallint); {$ifndef fpc}far;{$endif fpc}
  711. {********************************************************}
  712. { Draws a horizontal patterned line according to the }
  713. { current Fill Settings. }
  714. {********************************************************}
  715. { Important notes: }
  716. { - CurrentColor must be set correctly before entering }
  717. { this routine. }
  718. {********************************************************}
  719. var
  720. NrIterations: smallint;
  721. i : smallint;
  722. j : smallint;
  723. TmpFillPattern : byte;
  724. OldWriteMode : word;
  725. OldCurrentColor : word;
  726. begin
  727. { convert to global coordinates ... }
  728. x1 := x1 + StartXViewPort;
  729. x2 := x2 + StartXViewPort;
  730. y := y + StartYViewPort;
  731. { if line was fully clipped then exit...}
  732. if LineClipped(x1,y,x2,y,StartXViewPort,StartYViewPort,
  733. StartXViewPort+ViewWidth, StartYViewPort+ViewHeight) then
  734. exit;
  735. OldWriteMode := CurrentWriteMode;
  736. CurrentWriteMode := NormalPut;
  737. { Get the current pattern }
  738. TmpFillPattern := FillPatternTable
  739. [FillSettings.Pattern][(y and $7)+1];
  740. Case TmpFillPattern Of
  741. 0:
  742. begin
  743. OldCurrentColor := CurrentColor;
  744. CurrentColor := CurrentBkColor;
  745. { hline converts the coordinates to global ones, but that has been done }
  746. { already here!!! Convert them back to local ones... (JM) }
  747. HLine(x1-StartXViewPort,x2-StartXViewPort,y-StartYViewPort);
  748. CurrentColor := OldCurrentColor;
  749. end;
  750. $ff:
  751. begin
  752. HLine(x1-StartXViewPort,x2-StartXViewPort,y-StartYViewPort);
  753. end;
  754. else
  755. begin
  756. { number of times to go throuh the 8x8 pattern }
  757. NrIterations := abs(x2 - x1+8) div 8;
  758. For i:= 0 to NrIterations do
  759. Begin
  760. for j:=0 to 7 do
  761. Begin
  762. { x1 mod 8 }
  763. if RevBitArray[x1 and 7] and TmpFillPattern <> 0 then
  764. DirectPutpixel(x1,y)
  765. else
  766. begin
  767. { According to the TP graph manual, we overwrite everything }
  768. { which is filled up - checked against VGA and CGA drivers }
  769. { of TP. }
  770. OldCurrentColor := CurrentColor;
  771. CurrentColor := CurrentBkColor;
  772. DirectPutPixel(x1,y);
  773. CurrentColor := OldCurrentColor;
  774. end;
  775. Inc(x1);
  776. if x1 > x2 then
  777. begin
  778. CurrentWriteMode := OldWriteMode;
  779. exit;
  780. end;
  781. end;
  782. end;
  783. end;
  784. End;
  785. CurrentWriteMode := OldWriteMode;
  786. end;
  787. procedure LineRel(Dx, Dy: smallint);
  788. Begin
  789. Line(CurrentX, CurrentY, CurrentX + Dx, CurrentY + Dy);
  790. CurrentX := CurrentX + Dx;
  791. CurrentY := CurrentY + Dy;
  792. end;
  793. procedure LineTo(x,y : smallint);
  794. Begin
  795. Line(CurrentX, CurrentY, X, Y);
  796. CurrentX := X;
  797. CurrentY := Y;
  798. end;
  799. procedure Rectangle(x1,y1,x2,y2:smallint);
  800. begin
  801. { Do not draw the end points }
  802. Line(x1,y1,x2-1,y1);
  803. Line(x1,y1+1,x1,y2);
  804. Line(x2,y1,x2,y2-1);
  805. Line(x1+1,y2,x2,y2);
  806. end;
  807. procedure GetLineSettings(var ActiveLineInfo : LineSettingsType);
  808. begin
  809. Activelineinfo:=Lineinfo;
  810. end;
  811. procedure SetLineStyle(LineStyle: word; Pattern: word; Thickness: word);
  812. var
  813. i: byte;
  814. j: byte;
  815. Begin
  816. if (LineStyle > UserBitLn) or ((Thickness <> Normwidth) and (Thickness <> ThickWidth)) then
  817. _GraphResult := grError
  818. else
  819. begin
  820. LineInfo.Thickness := Thickness;
  821. LineInfo.LineStyle := LineStyle;
  822. case LineStyle of
  823. UserBitLn: Lineinfo.Pattern := pattern;
  824. SolidLn: Lineinfo.Pattern := $ffff; { ------- }
  825. DashedLn : Lineinfo.Pattern := $F8F8; { -- -- --}
  826. DottedLn: LineInfo.Pattern := $CCCC; { - - - - }
  827. CenterLn: LineInfo.Pattern := $FC78; { -- - -- }
  828. end; { end case }
  829. { setup pattern styles }
  830. j:=16;
  831. for i:=0 to 15 do
  832. Begin
  833. dec(j);
  834. { bitwise mask for each bit in the word }
  835. if (word($01 shl i) AND LineInfo.Pattern) <> 0 then
  836. LinePatterns[j]:=TRUE
  837. else
  838. LinePatterns[j]:=FALSE;
  839. end;
  840. end;
  841. end;
  842. {--------------------------------------------------------------------------}
  843. { }
  844. { VIEWPORT RELATED ROUTINES }
  845. { }
  846. {--------------------------------------------------------------------------}
  847. Procedure ClearViewPortDefault; {$ifndef fpc}far;{$endif fpc}
  848. var
  849. j: smallint;
  850. OldWriteMode, OldCurColor: word;
  851. LineSets : LineSettingsType;
  852. Begin
  853. { CP is always RELATIVE coordinates }
  854. CurrentX := 0;
  855. CurrentY := 0;
  856. { Save all old settings }
  857. OldCurColor := CurrentColor;
  858. CurrentColor:=CurrentBkColor;
  859. OldWriteMode:=CurrentWriteMode;
  860. CurrentWriteMode:=NormalPut;
  861. GetLineSettings(LineSets);
  862. { reset to normal line style...}
  863. SetLineStyle(SolidLn, 0, NormWidth);
  864. { routines are relative here...}
  865. { ViewHeight is Height-1 ! }
  866. for J:=0 to ViewHeight do
  867. HLine(0, ViewWidth , J);
  868. { restore old settings...}
  869. SetLineStyle(LineSets.LineStyle, LineSets.Pattern, LineSets.Thickness);
  870. CurrentColor := OldCurColor;
  871. CurrentWriteMode := OldWriteMode;
  872. end;
  873. Procedure SetViewPort(X1, Y1, X2, Y2: smallint; Clip: Boolean);
  874. Begin
  875. if (X1 > GetMaxX) or (X2 > GetMaxX) or (X1 > X2) or (X1 < 0) then
  876. Begin
  877. {$ifdef logging}
  878. logln('invalid setviewport parameters: ('
  879. +strf(x1)+','+strf(y1)+'), ('+strf(x2)+','+strf(y2)+')');
  880. logln('maxx = '+strf(getmaxx)+', maxy = '+strf(getmaxy));
  881. {$endif logging}
  882. _GraphResult := grError;
  883. exit;
  884. end;
  885. if (Y1 > GetMaxY) or (Y2 > GetMaxY) or (Y1 > Y2) or (Y1 < 0) then
  886. Begin
  887. {$ifdef logging}
  888. logln('invalid setviewport parameters: ('
  889. +strf(x1)+','+strf(y1)+'), ('+strf(x2)+','+strf(y2)+')');
  890. logln('maxx = '+strf(getmaxx)+', maxy = '+strf(getmaxy));
  891. {$endif logging}
  892. _GraphResult := grError;
  893. exit;
  894. end;
  895. { CP is always RELATIVE coordinates }
  896. CurrentX := 0;
  897. CurrentY := 0;
  898. StartXViewPort := X1;
  899. StartYViewPort := Y1;
  900. ViewWidth := X2-X1;
  901. ViewHeight:= Y2-Y1;
  902. ClipPixels := Clip;
  903. end;
  904. procedure GetViewSettings(var viewport : ViewPortType);
  905. begin
  906. ViewPort.X1 := StartXViewPort;
  907. ViewPort.Y1 := StartYViewPort;
  908. ViewPort.X2 := ViewWidth + StartXViewPort;
  909. ViewPort.Y2 := ViewHeight + StartYViewPort;
  910. ViewPort.Clip := ClipPixels;
  911. end;
  912. procedure ClearDevice;
  913. var
  914. ViewPort: ViewPortType;
  915. begin
  916. { Reset the CP }
  917. CurrentX := 0;
  918. CurrentY := 0;
  919. { save viewport }
  920. ViewPort.X1 := StartXviewPort;
  921. ViewPort.X2 := ViewWidth - StartXViewPort;
  922. ViewPort.Y1 := StartYViewPort;
  923. ViewPort.Y2 := ViewHeight - StartYViewPort;
  924. ViewPort.Clip := ClipPixels;
  925. { put viewport to full screen }
  926. StartXViewPort := 0;
  927. ViewHeight := MaxY;
  928. StartYViewPort := 0;
  929. ViewWidth := MaxX;
  930. ClipPixels := TRUE;
  931. ClearViewPort;
  932. { restore old viewport }
  933. StartXViewPort := ViewPort.X1;
  934. ViewWidth := ViewPort.X2-ViewPort.X1;
  935. StartYViewPort := ViewPort.Y1;
  936. ViewHeight := ViewPort.Y2-ViewPort.Y1;
  937. ClipPixels := ViewPort.Clip;
  938. end;
  939. {--------------------------------------------------------------------------}
  940. { }
  941. { BITMAP PUT/GET ROUTINES }
  942. { }
  943. {--------------------------------------------------------------------------}
  944. Procedure GetScanlineDefault (X1, X2, Y : smallint; Var Data); {$ifndef fpc}far;{$endif fpc}
  945. {**********************************************************}
  946. { Procedure GetScanLine() }
  947. {----------------------------------------------------------}
  948. { Returns the full scanline of the video line of the Y }
  949. { coordinate. The values are returned in a WORD array }
  950. { each WORD representing a pixel of the specified scanline }
  951. { note: we only need the pixels inside the ViewPort! (JM) }
  952. { note2: extended so you can specify start and end X coord }
  953. { so it is usable for GetImage too (JM) }
  954. {**********************************************************}
  955. Var
  956. x : smallint;
  957. Begin
  958. For x:=X1 to X2 Do
  959. WordArray(Data)[x-x1]:=GetPixel(x, y);
  960. End;
  961. Function DefaultImageSize(X1,Y1,X2,Y2: smallint): longint; {$ifndef fpc}far;{$endif fpc}
  962. Begin
  963. { each pixel uses two bytes, to enable modes with colors up to 64K }
  964. { to work. }
  965. DefaultImageSize := 12 + (((X2-X1+1)*(Y2-Y1+1))*2);
  966. end;
  967. Procedure DefaultPutImage(X,Y: smallint; var Bitmap; BitBlt: Word); {$ifndef fpc}far;{$endif fpc}
  968. type
  969. pt = array[0..$fffffff] of word;
  970. ptw = array[0..2] of longint;
  971. var
  972. k: longint;
  973. oldCurrentColor: word;
  974. oldCurrentWriteMode, i, j, y1, x1, deltaX, deltaX1, deltaY: smallint;
  975. Begin
  976. {$ifdef logging}
  977. LogLn('putImage at ('+strf(x)+','+strf(y)+') with width '+strf(ptw(Bitmap)[0])+
  978. ' and height '+strf(ptw(Bitmap)[1]));
  979. deltaY := 0;
  980. {$endif logging}
  981. inc(x,startXViewPort);
  982. inc(y,startYViewPort);
  983. { width/height are 1-based, coordinates are zero based }
  984. x1 := ptw(Bitmap)[0]+x-1; { get width and adjust end coordinate accordingly }
  985. y1 := ptw(Bitmap)[1]+y-1; { get height and adjust end coordinate accordingly }
  986. deltaX := 0;
  987. deltaX1 := 0;
  988. k := 3 * sizeOf(Longint) div sizeOf(Word); { Three reserved longs at start of bitmap }
  989. { check which part of the image is in the viewport }
  990. if clipPixels then
  991. begin
  992. if y < startYViewPort then
  993. begin
  994. deltaY := startYViewPort - y;
  995. inc(k,(x1-x+1)*deltaY);
  996. y := startYViewPort;
  997. end;
  998. if y1 > startYViewPort+viewHeight then
  999. y1 := startYViewPort+viewHeight;
  1000. if x < startXViewPort then
  1001. begin
  1002. deltaX := startXViewPort-x;
  1003. x := startXViewPort;
  1004. end;
  1005. if x1 > startXViewPort + viewWidth then
  1006. begin
  1007. deltaX1 := x1 - (startXViewPort + viewWidth);
  1008. x1 := startXViewPort + viewWidth;
  1009. end;
  1010. end;
  1011. {$ifdef logging}
  1012. LogLn('deltax: '+strf(deltax)+', deltax1: '+strf(deltax1)+',deltay: '+strf(deltay));
  1013. {$endif logging}
  1014. oldCurrentColor := currentColor;
  1015. oldCurrentWriteMode := currentWriteMode;
  1016. currentWriteMode := bitBlt;
  1017. for j:=Y to Y1 do
  1018. Begin
  1019. inc(k,deltaX);
  1020. for i:=X to X1 do
  1021. begin
  1022. currentColor := pt(bitmap)[k];
  1023. directPutPixel(i,j);
  1024. inc(k);
  1025. end;
  1026. inc(k,deltaX1);
  1027. end;
  1028. currentWriteMode := oldCurrentWriteMode;
  1029. currentColor := oldCurrentColor;
  1030. end;
  1031. Procedure DefaultGetImage(X1,Y1,X2,Y2: smallint; Var Bitmap); {$ifndef fpc}far;{$endif fpc}
  1032. type
  1033. pt = array[0..$fffffff] of word;
  1034. ptw = array[0..2] of longint;
  1035. var
  1036. i,j: smallint;
  1037. k: longint;
  1038. Begin
  1039. k:= 3 * Sizeof(longint) div sizeof(word); { Three reserved longs at start of bitmap }
  1040. i := x2 - x1 + 1;
  1041. for j:=Y1 to Y2 do
  1042. Begin
  1043. GetScanLine(x1,x2,j,pt(Bitmap)[k]);
  1044. inc(k,i);
  1045. end;
  1046. ptw(Bitmap)[0] := X2-X1+1; { First longint is width }
  1047. ptw(Bitmap)[1] := Y2-Y1+1; { Second longint is height }
  1048. ptw(bitmap)[2] := 0; { Third longint is reserved}
  1049. end;
  1050. Procedure GetArcCoords(var ArcCoords: ArcCoordsType);
  1051. Begin
  1052. ArcCoords.X := ArcCall.X;
  1053. ArcCoords.Y := ArcCall.Y;
  1054. ArcCoords.XStart := ArcCall.XStart;
  1055. ArcCoords.YStart := ArcCall.YStart;
  1056. ArcCoords.XEnd := ArcCall.XEnd;
  1057. ArcCoords.YEnd := ArcCall.YEnd;
  1058. end;
  1059. procedure SetVisualPageDefault(page : word); {$ifndef fpc}far;{$endif fpc}
  1060. begin
  1061. end;
  1062. procedure SetActivePageDefault(page : word); {$ifndef fpc}far;{$endif fpc}
  1063. begin
  1064. end;
  1065. procedure DirectPutPixelDefault(X,Y: smallint);
  1066. begin
  1067. Writeln(stderr,'Error: Not in graphics mode (use InitGraph and test GraphResult afterwards)');
  1068. Halt(1);
  1069. end;
  1070. function GetPixelDefault(X,Y: smallint): word;
  1071. begin
  1072. Writeln(stderr,'Error: Not in graphics mode (use InitGraph and test GraphResult afterwards)');
  1073. Halt(1);
  1074. exit(0); { avoid warning }
  1075. end;
  1076. procedure PutPixelDefault(X,Y: smallint; Color: Word);
  1077. begin
  1078. Writeln(stderr,'Error: Not in graphics mode (use InitGraph and test GraphResult afterwards)');
  1079. Halt(1);
  1080. end;
  1081. procedure SetRGBPaletteDefault(ColorNum, RedValue, GreenValue, BlueValue: smallint);
  1082. begin
  1083. Writeln(stderr,'Error: Not in graphics mode (use InitGraph and test GraphResult afterwards)');
  1084. Halt(1);
  1085. end;
  1086. procedure GetRGBPaletteDefault(ColorNum: smallint; var
  1087. RedValue, GreenValue, BlueValue: smallint);
  1088. begin
  1089. Writeln(stderr,'Error: Not in graphics mode (use InitGraph and test GraphResult afterwards)');
  1090. Halt(1);
  1091. end;
  1092. procedure OutTextXYDefault(x,y : smallint;const TextString : string);forward;
  1093. procedure CircleDefault(X, Y: smallint; Radius:Word);forward;
  1094. {$i palette.inc}
  1095. Procedure DefaultHooks;
  1096. {********************************************************}
  1097. { Procedure DefaultHooks() }
  1098. {--------------------------------------------------------}
  1099. { Resets all hookable routine either to nil for those }
  1100. { which need overrides, and others to defaults. }
  1101. { This is called each time SetGraphMode() is called. }
  1102. {********************************************************}
  1103. Begin
  1104. { All default hooks procedures }
  1105. { required...}
  1106. DirectPutPixel := {$ifdef fpc}@{$endif}DirectPutPixelDefault;
  1107. PutPixel := {$ifdef fpc}@{$endif}PutPixelDefault;
  1108. GetPixel := {$ifdef fpc}@{$endif}GetPixelDefault;
  1109. SetRGBPalette := {$ifdef fpc}@{$endif}SetRGBPaletteDefault;
  1110. GetRGBPalette := {$ifdef fpc}@{$endif}GetRGBPaletteDefault;
  1111. { optional...}
  1112. SetAllPalette := {$ifdef fpc}@{$endif}SetAllPaletteDefault;
  1113. SetActivePage := {$ifdef fpc}@{$endif}SetActivePageDefault;
  1114. SetVisualPage := {$ifdef fpc}@{$endif}SetVisualPageDefault;
  1115. ClearViewPort := {$ifdef fpc}@{$endif}ClearViewportDefault;
  1116. PutImage := {$ifdef fpc}@{$endif}DefaultPutImage;
  1117. GetImage := {$ifdef fpc}@{$endif}DefaultGetImage;
  1118. ImageSize := {$ifdef fpc}@{$endif}DefaultImageSize;
  1119. GraphFreeMemPtr := nil;
  1120. GraphGetMemPtr := nil;
  1121. GetScanLine := {$ifdef fpc}@{$endif}GetScanLineDefault;
  1122. Line := {$ifdef fpc}@{$endif}LineDefault;
  1123. InternalEllipse := {$ifdef fpc}@{$endif}InternalEllipseDefault;
  1124. PatternLine := {$ifdef fpc}@{$endif}PatternLineDefault;
  1125. HLine := {$ifdef fpc}@{$endif}HLineDefault;
  1126. VLine := {$ifdef fpc}@{$endif}VLineDefault;
  1127. OuttextXY := {$ifdef fpc}@{$endif}OuttextXYDefault;
  1128. Circle := {$ifdef fpc}@{$endif}CircleDefault;
  1129. end;
  1130. Procedure InitVars;
  1131. {********************************************************}
  1132. { Procedure InitVars() }
  1133. {--------------------------------------------------------}
  1134. { Resets all internal variables, and resets all }
  1135. { overridable routines. }
  1136. {********************************************************}
  1137. Begin
  1138. DirectVideo := TRUE; { By default use fastest access possible }
  1139. ArcCall.X := 0;
  1140. ArcCall.Y := 0;
  1141. ArcCall.XStart := 0;
  1142. ArcCall.YStart := 0;
  1143. ArcCall.XEnd := 0;
  1144. ArcCall.YEnd := 0;
  1145. { Reset to default values }
  1146. IntCurrentMode := 0;
  1147. IntCurrentDriver := 0;
  1148. IntCurrentNewDriver := 0;
  1149. XAspect := 0;
  1150. YAspect := 0;
  1151. MaxX := 0;
  1152. MaxY := 0;
  1153. MaxColor := 0;
  1154. PaletteSize := 0;
  1155. DirectColor := FALSE;
  1156. HardwarePages := 0;
  1157. if hardwarepages=0 then; { remove note }
  1158. DefaultHooks;
  1159. end;
  1160. {$i modes.inc}
  1161. function InstallUserDriver(Name: string; AutoDetectPtr: Pointer): smallint;
  1162. begin
  1163. _graphResult := grError;
  1164. InstallUserDriver:=grError;
  1165. end;
  1166. function RegisterBGIDriver(driver: pointer): smallint;
  1167. begin
  1168. _graphResult := grError;
  1169. RegisterBGIDriver:=grError;
  1170. end;
  1171. { ----------------------------------------------------------------- }
  1172. Procedure Arc(X,Y : smallint; StAngle,EndAngle,Radius: word);
  1173. { var
  1174. OldWriteMode: word;}
  1175. Begin
  1176. { Only if we are using thickwidths lines do we accept }
  1177. { XORput write modes. }
  1178. { OldWriteMode := CurrentWriteMode;
  1179. if (LineInfo.Thickness = NormWidth) then
  1180. CurrentWriteMode := NormalPut;}
  1181. InternalEllipse(X,Y,Radius,Radius,StAngle,Endangle,{$ifdef fpc}@{$endif}DummyPatternLine);
  1182. { CurrentWriteMode := OldWriteMode;}
  1183. end;
  1184. procedure Ellipse(X,Y : smallint; stAngle, EndAngle: word; XRadius,YRadius: word);
  1185. Begin
  1186. InternalEllipse(X,Y,XRadius,YRadius,StAngle,Endangle,{$ifdef fpc}@{$endif}DummyPatternLine);
  1187. end;
  1188. procedure FillEllipse(X, Y: smallint; XRadius, YRadius: Word);
  1189. {********************************************************}
  1190. { Procedure FillEllipse() }
  1191. {--------------------------------------------------------}
  1192. { Draws a filled ellipse using (X,Y) as a center point }
  1193. { and XRadius and YRadius as the horizontal and vertical }
  1194. { axes. The ellipse is filled with the current fill color}
  1195. { and fill style, and is bordered with the current color.}
  1196. {********************************************************}
  1197. begin
  1198. InternalEllipse(X,Y,XRadius,YRadius,0,360,PatternLine)
  1199. end;
  1200. procedure CircleDefault(X, Y: smallint; Radius:Word);
  1201. {********************************************************}
  1202. { Draws a circle centered at X,Y with the given Radius. }
  1203. {********************************************************}
  1204. { Important notes: }
  1205. { - Thickwidth circles use the current write mode, while}
  1206. { normal width circles ALWAYS use CopyPut/NormalPut }
  1207. { mode. (Tested against VGA BGI driver -CEC 13/Aug/99 }
  1208. {********************************************************}
  1209. var OriginalArcInfo: ArcCoordsType;
  1210. OldWriteMode: word;
  1211. begin
  1212. if (Radius = 0) then
  1213. Exit;
  1214. if (Radius = 1) then
  1215. begin
  1216. { only normal put mode is supported by a call to PutPixel }
  1217. PutPixel(X, Y, CurrentColor);
  1218. Exit;
  1219. end;
  1220. { save state of arc information }
  1221. { because it is not needed for }
  1222. { a circle call. }
  1223. move(ArcCall,OriginalArcInfo, sizeof(ArcCall));
  1224. if LineInfo.Thickness = Normwidth then
  1225. begin
  1226. OldWriteMode := CurrentWriteMode;
  1227. CurrentWriteMode := CopyPut;
  1228. end;
  1229. InternalEllipse(X,Y,Radius,Radius,0,360,{$ifdef fpc}@{$endif}DummyPatternLine);
  1230. if LineInfo.Thickness = Normwidth then
  1231. CurrentWriteMode := OldWriteMode;
  1232. { restore arc information }
  1233. move(OriginalArcInfo, ArcCall,sizeof(ArcCall));
  1234. end;
  1235. procedure SectorPL(x1,x2,y: smallint); {$ifndef fpc}far;{$endif fpc}
  1236. var plx1, plx2: smallint;
  1237. begin
  1238. If (x1 = -maxsmallint) Then
  1239. If (x2 = maxsmallint-1) Then
  1240. { no ellipse points drawn on this line }
  1241. If (((Y < ArcCall.Y) and (Y > ArcCall.YStart)) or
  1242. ((Y > ArcCall.Y) and (Y < ArcCall.YStart))) Then
  1243. { there is a part of the sector at this y coordinate, but no }
  1244. { ellips points are plotted on this line, so draw a patternline }
  1245. { between the lines connecting (arccall.x,arccall.y) with }
  1246. { the start and the end of the arc (JM) }
  1247. { use: y-y1=(y2-y1)/(x2-x1)*(x-x1) => }
  1248. { x = (y-y1)/(y2-y1)*(x2-x1)+x1 }
  1249. Begin
  1250. plx1 := (y-ArcCall.Y)*(ArcCall.XStart-ArcCall.X)
  1251. div (ArcCall.YStart-ArcCall.Y)+ArcCall.X;
  1252. plx2 := (y-ArcCall.Y)*(ArcCall.XEnd-ArcCall.X)
  1253. div (ArcCall.YEnd-ArcCall.Y)+ArcCall.X;
  1254. If plx1 > plx2 then
  1255. begin
  1256. plx1 := plx1 xor plx2;
  1257. plx2 := plx1 xor plx2;
  1258. plx1 := plx1 xor plx2;
  1259. end;
  1260. End
  1261. { otherwise two points which have nothing to do with the sector }
  1262. Else exit
  1263. Else
  1264. { the arc is plotted at the right side, but not at the left side, }
  1265. { fill till the line between (ArcCall.X,ArcCall.Y) and }
  1266. { (ArcCall.XStart,ArcCall.YStart) }
  1267. Begin
  1268. If (y < ArcCall.Y) then
  1269. begin
  1270. plx1 := (y-ArcCall.Y)*(ArcCall.XEnd-ArcCall.X)
  1271. div (ArcCall.YEnd-ArcCall.Y)+ArcCall.X
  1272. end
  1273. else if (y > ArcCall.Y) then
  1274. begin
  1275. plx1 := (y-ArcCall.Y)*(ArcCall.XStart-ArcCall.X)
  1276. div (ArcCall.YStart-ArcCall.Y)+ArcCall.X
  1277. end
  1278. else plx1 := ArcCall.X;
  1279. plx2 := x2;
  1280. End
  1281. Else
  1282. If (x2 = maxsmallint-1) Then
  1283. { the arc is plotted at the left side, but not at the rigth side. }
  1284. { the right limit can be either the first or second line. Just take }
  1285. { the closest one, but watch out for division by zero! }
  1286. Begin
  1287. If (y < ArcCall.Y) then
  1288. begin
  1289. plx2 := (y-ArcCall.Y)*(ArcCall.XStart-ArcCall.X)
  1290. div (ArcCall.YStart-ArcCall.Y)+ArcCall.X
  1291. end
  1292. else if (y > ArcCall.Y) then
  1293. begin
  1294. plx2 := (y-ArcCall.Y)*(ArcCall.XEnd-ArcCall.X)
  1295. div (ArcCall.YEnd-ArcCall.Y)+ArcCall.X
  1296. end
  1297. else plx2 := ArcCall.X;
  1298. plx1 := x1;
  1299. End
  1300. Else
  1301. { the arc is plotted at both sides }
  1302. Begin
  1303. plx1 := x1;
  1304. plx2 := x2;
  1305. End;
  1306. If plx2 > plx1 then
  1307. Begin
  1308. PatternLine(plx1,plx2,y);
  1309. end;
  1310. end;
  1311. procedure Sector(x, y: smallint; StAngle,EndAngle, XRadius, YRadius: Word);
  1312. begin
  1313. internalellipse(x,y,XRadius, YRadius, StAngle, EndAngle, {$ifdef fpc}@{$endif}SectorPL);
  1314. Line(ArcCall.XStart, ArcCall.YStart, x,y);
  1315. Line(x,y,ArcCall.Xend,ArcCall.YEnd);
  1316. end;
  1317. procedure SetFillStyle(Pattern : word; Color: word);
  1318. begin
  1319. { on invalid input, the current fill setting will be }
  1320. { unchanged. }
  1321. if (Pattern > UserFill) or (Color > GetMaxColor) then
  1322. begin
  1323. {$ifdef logging}
  1324. logln('invalid fillstyle parameters');
  1325. {$endif logging}
  1326. _GraphResult := grError;
  1327. exit;
  1328. end;
  1329. FillSettings.Color := Color;
  1330. FillSettings.Pattern := Pattern;
  1331. end;
  1332. procedure SetFillPattern(Pattern: FillPatternType; Color: word);
  1333. {********************************************************}
  1334. { Changes the Current FillPattern to a user defined }
  1335. { pattern and changes also the current fill color. }
  1336. { The FillPattern is saved in the FillPattern array so }
  1337. { it can still be used with SetFillStyle(UserFill,Color) }
  1338. {********************************************************}
  1339. var
  1340. i: smallint;
  1341. begin
  1342. if Color > GetMaxColor then
  1343. begin
  1344. {$ifdef logging}
  1345. logln('invalid fillpattern parameters');
  1346. {$endif logging}
  1347. _GraphResult := grError;
  1348. exit;
  1349. end;
  1350. FillSettings.Color := Color;
  1351. FillSettings.Pattern := UserFill;
  1352. { Save the pattern in the buffer }
  1353. For i:=1 to 8 do
  1354. FillPatternTable[UserFill][i] := Pattern[i];
  1355. end;
  1356. procedure Bar(x1,y1,x2,y2:smallint);
  1357. {********************************************************}
  1358. { Important notes for compatibility with BP: }
  1359. { - WriteMode is always CopyPut }
  1360. { - No contour is drawn for the lines }
  1361. {********************************************************}
  1362. var y : smallint;
  1363. origcolor : longint;
  1364. origlinesettings: Linesettingstype;
  1365. origwritemode : smallint;
  1366. begin
  1367. origlinesettings:=lineinfo;
  1368. origcolor:=CurrentColor;
  1369. if y1>y2 then
  1370. begin
  1371. y:=y1;
  1372. y1:=y2;
  1373. y2:=y;
  1374. end;
  1375. { Always copy mode for Bars }
  1376. origwritemode := CurrentWriteMode;
  1377. CurrentWriteMode := CopyPut;
  1378. { All lines used are of this style }
  1379. Lineinfo.linestyle:=solidln;
  1380. Lineinfo.thickness:=normwidth;
  1381. case Fillsettings.pattern of
  1382. EmptyFill :
  1383. begin
  1384. Currentcolor:=CurrentBkColor;
  1385. for y:=y1 to y2 do
  1386. Hline(x1,x2,y);
  1387. end;
  1388. SolidFill :
  1389. begin
  1390. CurrentColor:=FillSettings.color;
  1391. for y:=y1 to y2 do
  1392. Hline(x1,x2,y);
  1393. end;
  1394. else
  1395. Begin
  1396. CurrentColor:=FillSettings.color;
  1397. for y:=y1 to y2 do
  1398. patternline(x1,x2,y);
  1399. end;
  1400. end;
  1401. CurrentColor:= Origcolor;
  1402. LineInfo := OrigLineSettings;
  1403. CurrentWriteMode := OrigWritemode;
  1404. end;
  1405. procedure bar3D(x1, y1, x2, y2 : smallint;depth : word;top : boolean);
  1406. var
  1407. origwritemode : smallint;
  1408. OldX, OldY : smallint;
  1409. begin
  1410. origwritemode := CurrentWriteMode;
  1411. CurrentWriteMode := CopyPut;
  1412. Bar(x1,y1,x2,y2);
  1413. Rectangle(x1,y1,x2,y2);
  1414. { Current CP should not be updated in Bar3D }
  1415. { therefore save it and then restore it on }
  1416. { exit. }
  1417. OldX := CurrentX;
  1418. OldY := CurrentY;
  1419. if top then begin
  1420. Moveto(x1,y1);
  1421. Lineto(x1+depth,y1-depth);
  1422. Lineto(x2+depth,y1-depth);
  1423. Lineto(x2,y1);
  1424. end;
  1425. if Depth <> 0 then
  1426. Begin
  1427. Moveto(x2+depth,y1-depth);
  1428. Lineto(x2+depth,y2-depth);
  1429. Lineto(x2,y2);
  1430. end;
  1431. { restore CP }
  1432. CurrentX := OldX;
  1433. CurrentY := OldY;
  1434. CurrentWriteMode := origwritemode;
  1435. end;
  1436. {--------------------------------------------------------------------------}
  1437. { }
  1438. { COLOR AND PALETTE ROUTINES }
  1439. { }
  1440. {--------------------------------------------------------------------------}
  1441. procedure SetColor(Color: Word);
  1442. Begin
  1443. CurrentColor := Color;
  1444. end;
  1445. function GetColor: Word;
  1446. Begin
  1447. GetColor := CurrentColor;
  1448. end;
  1449. function GetBkColor: Word;
  1450. Begin
  1451. GetBkColor := CurrentBkColor;
  1452. end;
  1453. procedure SetBkColor(ColorNum: Word);
  1454. { Background color means background screen color in this case, and it is }
  1455. { INDEPENDANT of the viewport settings, so we must clear the whole screen }
  1456. { with the color. }
  1457. var
  1458. ViewPort: ViewportType;
  1459. Begin
  1460. GetViewSettings(Viewport);
  1461. {$ifdef logging}
  1462. logln('calling setviewport from setbkcolor');
  1463. {$endif logging}
  1464. SetViewPort(0,0,MaxX,MaxY,FALSE);
  1465. {$ifdef logging}
  1466. logln('calling setviewport from setbkcolor done');
  1467. {$endif logging}
  1468. CurrentBkColor := ColorNum;
  1469. {ClearViewPort;}
  1470. if not DirectColor and (ColorNum<256) then
  1471. SetRGBPalette(0,
  1472. DefaultColors[ColorNum].Red,
  1473. DefaultColors[ColorNum].Green,
  1474. DefaultColors[ColorNum].Blue);
  1475. SetViewport(ViewPort.X1,Viewport.Y1,Viewport.X2,Viewport.Y2,Viewport.Clip);
  1476. end;
  1477. function GetMaxColor: word;
  1478. { Checked against TP VGA driver - CEC }
  1479. begin
  1480. GetMaxColor:=MaxColor-1; { based on an index of zero so subtract one }
  1481. end;
  1482. Procedure MoveRel(Dx, Dy: smallint);
  1483. Begin
  1484. CurrentX := CurrentX + Dx;
  1485. CurrentY := CurrentY + Dy;
  1486. end;
  1487. Procedure MoveTo(X,Y: smallint);
  1488. {********************************************************}
  1489. { Procedure MoveTo() }
  1490. {--------------------------------------------------------}
  1491. { Moves the current pointer in VIEWPORT relative }
  1492. { coordinates to the specified X,Y coordinate. }
  1493. {********************************************************}
  1494. Begin
  1495. CurrentX := X;
  1496. CurrentY := Y;
  1497. end;
  1498. function GraphErrorMsg(ErrorCode: smallint): string;
  1499. Begin
  1500. GraphErrorMsg:='';
  1501. case ErrorCode of
  1502. grOk,grFileNotFound,grInvalidDriver: exit;
  1503. grNoInitGraph: GraphErrorMsg:='Graphics driver not installed';
  1504. grNotDetected: GraphErrorMsg:='Graphics hardware not detected';
  1505. grNoLoadMem,grNoScanMem,grNoFloodMem: GraphErrorMsg := 'Not enough memory for graphics';
  1506. grNoFontMem: GraphErrorMsg := 'Not enough memory to load font';
  1507. grFontNotFound: GraphErrorMsg:= 'Font file not found';
  1508. grInvalidMode: GraphErrorMsg := 'Invalid graphics mode';
  1509. grError: GraphErrorMsg:='Graphics error';
  1510. grIoError: GraphErrorMsg:='Graphics I/O error';
  1511. grInvalidFont,grInvalidFontNum: GraphErrorMsg := 'Invalid font';
  1512. grInvalidVersion: GraphErrorMsg:='Invalid driver version';
  1513. end;
  1514. end;
  1515. Function GetMaxX: smallint;
  1516. { Routine checked against VGA driver - CEC }
  1517. Begin
  1518. GetMaxX := MaxX;
  1519. end;
  1520. Function GetMaxY: smallint;
  1521. { Routine checked against VGA driver - CEC }
  1522. Begin
  1523. GetMaxY := MaxY;
  1524. end;
  1525. Function GraphResult: smallint;
  1526. Begin
  1527. GraphResult := _GraphResult;
  1528. _GraphResult := grOk;
  1529. end;
  1530. Function GetX: smallint;
  1531. Begin
  1532. GetX := CurrentX;
  1533. end;
  1534. Function GetY: smallint;
  1535. Begin
  1536. GetY := CurrentY;
  1537. end;
  1538. Function GetDriverName: string;
  1539. begin
  1540. GetDriverName:=DriverName;
  1541. end;
  1542. procedure graphdefaults;
  1543. { PS: GraphDefaults does not ZERO the ArcCall structure }
  1544. { so a call to GetArcCoords will not change even the }
  1545. { returned values even if GraphDefaults is called in }
  1546. { between. }
  1547. var
  1548. i: smallint;
  1549. begin
  1550. lineinfo.linestyle:=solidln;
  1551. lineinfo.thickness:=normwidth;
  1552. { reset line style pattern }
  1553. for i:=0 to 15 do
  1554. LinePatterns[i] := TRUE;
  1555. { By default, according to the TP prog's reference }
  1556. { the default pattern is solid, and the default }
  1557. { color is the maximum color in the palette. }
  1558. fillsettings.color:=GetMaxColor;
  1559. fillsettings.pattern:=solidfill;
  1560. { GraphDefaults resets the User Fill pattern to $ff }
  1561. { checked with VGA BGI driver - CEC }
  1562. for i:=1 to 8 do
  1563. FillPatternTable[UserFill][i] := $ff;
  1564. CurrentColor:=white;
  1565. ClipPixels := TRUE;
  1566. { Reset the viewport }
  1567. StartXViewPort := 0;
  1568. StartYViewPort := 0;
  1569. ViewWidth := MaxX;
  1570. ViewHeight := MaxY;
  1571. { Reset CP }
  1572. CurrentX := 0;
  1573. CurrentY := 0;
  1574. SetBkColor(Black);
  1575. { normal write mode }
  1576. CurrentWriteMode := CopyPut;
  1577. { Schriftart einstellen }
  1578. CurrentTextInfo.font := DefaultFont;
  1579. CurrentTextInfo.direction:=HorizDir;
  1580. CurrentTextInfo.charsize:=1;
  1581. CurrentTextInfo.horiz:=LeftText;
  1582. CurrentTextInfo.vert:=TopText;
  1583. XAspect:=10000; YAspect:=10000;
  1584. end;
  1585. procedure GetAspectRatio(var Xasp,Yasp : word);
  1586. begin
  1587. XAsp:=XAspect;
  1588. YAsp:=YAspect;
  1589. end;
  1590. procedure SetAspectRatio(Xasp, Yasp : word);
  1591. begin
  1592. Xaspect:= XAsp;
  1593. YAspect:= YAsp;
  1594. end;
  1595. procedure SetWriteMode(WriteMode : smallint);
  1596. { TP sets the writemodes according to the following scheme (JM) }
  1597. begin
  1598. Case writemode of
  1599. xorput, andput: CurrentWriteMode := XorPut;
  1600. notput, orput, copyput: CurrentWriteMode := CopyPut;
  1601. End;
  1602. end;
  1603. procedure GetFillSettings(var Fillinfo:Fillsettingstype);
  1604. begin
  1605. Fillinfo:=Fillsettings;
  1606. end;
  1607. procedure GetFillPattern(var FillPattern:FillPatternType);
  1608. begin
  1609. FillPattern:=FillpatternTable[UserFill];
  1610. end;
  1611. procedure DrawPoly(numpoints : word;var polypoints);
  1612. type
  1613. ppointtype = ^pointtype;
  1614. pt = array[0..16000] of pointtype;
  1615. var
  1616. i : longint;
  1617. begin
  1618. if numpoints < 2 then
  1619. begin
  1620. _GraphResult := grError;
  1621. exit;
  1622. end;
  1623. for i:=0 to numpoints-2 do
  1624. line(pt(polypoints)[i].x,
  1625. pt(polypoints)[i].y,
  1626. pt(polypoints)[i+1].x,
  1627. pt(polypoints)[i+1].y);
  1628. end;
  1629. procedure PieSlice(X,Y,stangle,endAngle:smallint;Radius: Word);
  1630. begin
  1631. Sector(x,y,stangle,endangle,radius,radius);
  1632. end;
  1633. {$i fills.inc}
  1634. {$i gtext.inc}
  1635. procedure internDetectGraph(var GraphDriver, GraphMode:smallint;
  1636. calledFromInitGraph: boolean);
  1637. var LoMode, HiMode: smallint;
  1638. CpyMode: smallint;
  1639. CpyDriver: smallint;
  1640. begin
  1641. HiMode := -1;
  1642. LoMode := -1;
  1643. if not calledFromInitGraph or
  1644. (graphDriver < lowNewDriver) or
  1645. (graphDriver > highNewDriver) then
  1646. begin
  1647. { Search lowest supported bitDepth }
  1648. graphdriver := D1bit;
  1649. while (graphDriver <= highNewDriver) and
  1650. (hiMode = -1) do
  1651. begin
  1652. getModeRange(graphDriver,loMode,hiMode);
  1653. inc(graphDriver);
  1654. end;
  1655. dec(graphdriver);
  1656. if hiMode = -1 then
  1657. begin
  1658. _GraphResult := grNotDetected;
  1659. exit;
  1660. end;
  1661. CpyMode := 0;
  1662. repeat
  1663. GetModeRange(GraphDriver,LoMode,HiMode);
  1664. { save the highest mode possible...}
  1665. {$ifdef logging}
  1666. logln('Found driver '+strf(graphdriver)+' with modes '+
  1667. strf(lomode)+' - '+strf(himode));
  1668. {$endif logging}
  1669. if HiMode <> -1 then
  1670. begin
  1671. CpyMode:=HiMode;
  1672. CpyDriver:=GraphDriver;
  1673. end;
  1674. { go to next driver if it exists...}
  1675. Inc(graphDriver);
  1676. until (graphDriver > highNewDriver);
  1677. end
  1678. else
  1679. begin
  1680. cpyMode := 0;
  1681. getModeRange(graphDriver,loMode,hiMode);
  1682. if hiMode <> -1 then
  1683. begin
  1684. cpyDriver := graphDriver;
  1685. cpyMode := hiMode;
  1686. end;
  1687. end;
  1688. if cpyMode = 0 then
  1689. begin
  1690. _GraphResult := grNotDetected;
  1691. exit;
  1692. end;
  1693. _GraphResult := grOK;
  1694. GraphDriver := CpyDriver;
  1695. GraphMode := CpyMode;
  1696. end;
  1697. procedure detectGraph(var GraphDriver: smallint; var GraphMode:smallint);
  1698. begin
  1699. internDetectGraph(graphDriver,graphMode,false);
  1700. end;
  1701. procedure InitGraph(var GraphDriver:smallint;var GraphMode:smallint;
  1702. const PathToDriver:String);
  1703. const
  1704. dirchar = System.DirectorySeparator;
  1705. begin
  1706. InitVars;
  1707. { path to the fonts (where they will be searched)...}
  1708. bgipath:=PathToDriver;
  1709. if (Length(bgipath) > 0) and (bgipath[length(bgipath)]<>dirchar) then
  1710. bgipath:=bgipath+dirchar;
  1711. if not assigned(SaveVideoState) then
  1712. RunError(216);
  1713. DriverName:=InternalDriverName; { DOS Graphics driver }
  1714. if (Graphdriver=Detect)
  1715. or (GraphMode = detectMode)
  1716. then
  1717. begin
  1718. internDetectGraph(GraphDriver,GraphMode,true);
  1719. If _GraphResult = grNotDetected then Exit;
  1720. { _GraphResult is now already set to grOK by DetectGraph }
  1721. IntCurrentDriver := GraphDriver;
  1722. if (graphDriver >= lowNewDriver) and
  1723. (graphDriver <= highNewDriver) then
  1724. IntCurrentNewDriver := GraphDriver
  1725. else IntCurrentNewDriver := -1;
  1726. { Actually set the graph mode...}
  1727. if firstCallOfInitgraph then
  1728. begin
  1729. SaveVideoState;
  1730. firstCallOfInitgraph := false;
  1731. end;
  1732. SetGraphMode(GraphMode);
  1733. end
  1734. else
  1735. begin
  1736. { Search if that graphics modec actually exists...}
  1737. if SearchMode(GraphDriver,GraphMode) = nil then
  1738. begin
  1739. _GraphResult := grInvalidMode;
  1740. exit;
  1741. end
  1742. else
  1743. begin
  1744. _GraphResult := grOK;
  1745. IntCurrentDriver := GraphDriver;
  1746. if (graphDriver >= lowNewDriver) and
  1747. (graphDriver <= highNewDriver) then
  1748. IntCurrentNewDriver := GraphDriver
  1749. else IntCurrentNewDriver := -1;
  1750. if firstCallOfInitgraph then
  1751. begin
  1752. SaveVideoState;
  1753. firstCallOfInitgraph := false;
  1754. end;
  1755. SetGraphMode(GraphMode);
  1756. end;
  1757. end;
  1758. end;
  1759. procedure SetDirectVideo(DirectAccess: boolean);
  1760. begin
  1761. DirectVideo := DirectAccess;
  1762. end;
  1763. function GetDirectVideo: boolean;
  1764. begin
  1765. GetDirectVideo := DirectVideo;
  1766. end;
  1767. procedure GraphExitProc; {$ifndef fpc} far; {$endif fpc}
  1768. { deallocates all memory allocated by the graph unit }
  1769. var
  1770. list: PModeInfo;
  1771. tmp : PModeInfo;
  1772. c: longint;
  1773. begin
  1774. { restore old exitproc! }
  1775. exitproc := exitsave;
  1776. if IsGraphMode and ((errorcode<>0) or (erroraddr<>nil)) then
  1777. CloseGraph;
  1778. { release memory allocated for fonts }
  1779. for c := 1 to installedfonts do
  1780. with fonts[c] Do
  1781. If assigned(instr) Then
  1782. Freemem(instr,instrlength);
  1783. { release memory allocated for modelist }
  1784. list := ModeList;
  1785. while assigned(list) do
  1786. begin
  1787. tmp := list;
  1788. list:=list^.next;
  1789. dispose(tmp);
  1790. end;
  1791. for c := lowNewDriver to highNewDriver do
  1792. begin
  1793. list := newModeList.modeinfo[c];
  1794. while assigned(list) do
  1795. begin
  1796. tmp := list;
  1797. list:=list^.next;
  1798. dispose(tmp);
  1799. end;
  1800. end;
  1801. {$IFDEF DPMI}
  1802. { We had copied the buffer of mode information }
  1803. { and allocated it dynamically... now free it }
  1804. { Warning: if GetVESAInfo returned false, this buffer is not allocated! (JM)}
  1805. If hasVesa then
  1806. Dispose(VESAInfo.ModeList);
  1807. {$ENDIF}
  1808. end;
  1809. procedure InitializeGraph;
  1810. begin
  1811. {$ifdef logging}
  1812. assign(debuglog,'grlog.txt');
  1813. rewrite(debuglog);
  1814. close(debuglog);
  1815. {$endif logging}
  1816. isgraphmode := false;
  1817. ModeList := nil;
  1818. fillChar(newModeList.modeinfo,sizeof(newModeList.modeinfo),#0);
  1819. { lo and hi modenumber are -1 currently (no modes supported) }
  1820. fillChar(newModeList.loHiModeNr,sizeof(newModeList.loHiModeNr),#255);
  1821. SaveVideoState := nil;
  1822. RestoreVideoState := nil;
  1823. { This must be called at startup... because GetGraphMode may }
  1824. { be called even when not in graph mode. }
  1825. {$ifdef logging}
  1826. LogLn('Calling QueryAdapterInfo...');
  1827. {$endif logging}
  1828. QueryAdapterInfo;
  1829. { Install standard fonts }
  1830. { This is done BEFORE startup... }
  1831. InstalledFonts := 0;
  1832. InstallUserFont('TRIP');
  1833. InstallUserFont('LITT');
  1834. InstallUserFont('SANS');
  1835. InstallUserFont('GOTH');
  1836. InstallUserFont('SCRI');
  1837. InstallUserFont('SIMP');
  1838. InstallUserFont('TSCR');
  1839. InstallUserFont('LCOM');
  1840. InstallUserFont('EURO');
  1841. InstallUserFont('BOLD');
  1842. { This installs an exit procedure which cleans up the mode list...}
  1843. ExitSave := ExitProc;
  1844. ExitProc := @GraphExitProc;
  1845. {$ifdef win32}
  1846. charmessagehandler:=nil;
  1847. {$endif win32}
  1848. end;