graph.inc 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118
  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. If xradius = 0 then inc(xradius);
  608. if yradius = 0 then inc(yradius);
  609. { check for an ellipse with negligable x and y radius }
  610. If (xradius <= 1) and (yradius <= 1) then
  611. begin
  612. putpixel(x,y,CurrentColor);
  613. ArcCall.X := X;
  614. ArcCall.Y := Y;
  615. ArcCall.XStart := X;
  616. ArcCall.YStart := Y;
  617. ArcCall.XEnd := X;
  618. ArcCall.YEnd := Y;
  619. exit;
  620. end;
  621. { check if valid angles }
  622. stangle := stAngle mod 361;
  623. EndAngle := EndAngle mod 361;
  624. { if impossible angles then swap them! }
  625. if Endangle < StAngle then
  626. Begin
  627. TmpAngle:=EndAngle;
  628. EndAngle:=StAngle;
  629. Stangle:=TmpAngle;
  630. end;
  631. { approximate the number of pixels required by using the circumference }
  632. { equation of an ellipse. }
  633. { Changed this formula a it (trial and error), but the net result is that }
  634. { less pixels have to be calculated now }
  635. NumOfPixels:=Round(Sqrt(3)*sqrt(sqr(XRadius)+sqr(YRadius)));
  636. { Calculate the angle precision required }
  637. Delta := 90.0 / NumOfPixels;
  638. { for restoring after PatternLine }
  639. BackupColor := CurrentColor;
  640. { removed from inner loop to make faster }
  641. { store some arccall info }
  642. ArcCall.X := X;
  643. ArcCall.Y := Y;
  644. TempTerm := (StAngle)*ConvFac;
  645. ArcCall.XStart := round(XRadius*Cos(TempTerm)) + X;
  646. ArcCall.YStart := round(YRadius*Sin(TempTerm+Pi)) + Y;
  647. TempTerm := (EndAngle)*ConvFac;
  648. ArcCall.XEnd := round(XRadius*Cos(TempTerm)) + X;
  649. ArcCall.YEnd := round(YRadius*Sin(TempTerm+Pi)) + Y;
  650. { Always just go over the first 90 degrees. Could be optimized a }
  651. { bit if StAngle and EndAngle lie in the same quadrant, left as an }
  652. { exercise for the reader :) (JM) }
  653. j := 0;
  654. { calculate stop position, go 1 further than 90 because otherwise }
  655. { 1 pixel is sometimes not drawn (JM) }
  656. DeltaEnd := 91;
  657. { Calculate points }
  658. xnext := XRadius;
  659. ynext := 0;
  660. Repeat
  661. xtemp := xnext;
  662. ytemp := ynext;
  663. { this is used by both sin and cos }
  664. TempTerm := (j+Delta)*ConvFac;
  665. { Calculate points }
  666. xnext := round(XRadius*Cos(TempTerm));
  667. ynext := round(YRadius*Sin(TempTerm+Pi));
  668. xp := x + xtemp;
  669. xm := x - xtemp;
  670. yp := y + ytemp;
  671. ym := y - ytemp;
  672. plxpyp := maxsmallint;
  673. plxmyp := -maxsmallint-1;
  674. plxpym := maxsmallint;
  675. plxmym := -maxsmallint-1;
  676. If (j >= StAngle) and (j <= EndAngle) then
  677. begin
  678. plxpyp := xp;
  679. PutPixel(xp,yp,CurrentColor);
  680. end;
  681. If ((180-j) >= StAngle) and ((180-j) <= EndAngle) then
  682. begin
  683. plxmyp := xm;
  684. PutPixel(xm,yp,CurrentColor);
  685. end;
  686. If ((j+180) >= StAngle) and ((j+180) <= EndAngle) then
  687. begin
  688. plxmym := xm;
  689. PutPixel(xm,ym,CurrentColor);
  690. end;
  691. If ((360-j) >= StAngle) and ((360-j) <= EndAngle) then
  692. begin
  693. plxpym := xp;
  694. PutPixel(xp,ym,CurrentColor);
  695. end;
  696. If (ynext <> ytemp) and
  697. (xp - xm >= 1) then
  698. begin
  699. CurrentColor := FillSettings.Color;
  700. pl(plxmyp+1,plxpyp-1,yp);
  701. pl(plxmym+1,plxpym-1,ym);
  702. CurrentColor := BackupColor;
  703. end;
  704. j:=j+Delta;
  705. Until j > (DeltaEnd);
  706. end;
  707. procedure PatternLineDefault(x1,x2,y: smallint); {$ifndef fpc}far;{$endif fpc}
  708. {********************************************************}
  709. { Draws a horizontal patterned line according to the }
  710. { current Fill Settings. }
  711. {********************************************************}
  712. { Important notes: }
  713. { - CurrentColor must be set correctly before entering }
  714. { this routine. }
  715. {********************************************************}
  716. var
  717. NrIterations: smallint;
  718. i : smallint;
  719. j : smallint;
  720. TmpFillPattern : byte;
  721. OldWriteMode : word;
  722. OldCurrentColor : word;
  723. begin
  724. { convert to global coordinates ... }
  725. x1 := x1 + StartXViewPort;
  726. x2 := x2 + StartXViewPort;
  727. y := y + StartYViewPort;
  728. { if line was fully clipped then exit...}
  729. if LineClipped(x1,y,x2,y,StartXViewPort,StartYViewPort,
  730. StartXViewPort+ViewWidth, StartYViewPort+ViewHeight) then
  731. exit;
  732. OldWriteMode := CurrentWriteMode;
  733. CurrentWriteMode := NormalPut;
  734. { Get the current pattern }
  735. TmpFillPattern := FillPatternTable
  736. [FillSettings.Pattern][(y and $7)+1];
  737. Case TmpFillPattern Of
  738. 0:
  739. begin
  740. OldCurrentColor := CurrentColor;
  741. CurrentColor := CurrentBkColor;
  742. { hline converts the coordinates to global ones, but that has been done }
  743. { already here!!! Convert them back to local ones... (JM) }
  744. HLine(x1-StartXViewPort,x2-StartXViewPort,y-StartYViewPort);
  745. CurrentColor := OldCurrentColor;
  746. end;
  747. $ff:
  748. begin
  749. HLine(x1-StartXViewPort,x2-StartXViewPort,y-StartYViewPort);
  750. end;
  751. else
  752. begin
  753. { number of times to go throuh the 8x8 pattern }
  754. NrIterations := abs(x2 - x1+8) div 8;
  755. For i:= 0 to NrIterations do
  756. Begin
  757. for j:=0 to 7 do
  758. Begin
  759. { x1 mod 8 }
  760. if RevBitArray[x1 and 7] and TmpFillPattern <> 0 then
  761. DirectPutpixel(x1,y)
  762. else
  763. begin
  764. { According to the TP graph manual, we overwrite everything }
  765. { which is filled up - checked against VGA and CGA drivers }
  766. { of TP. }
  767. OldCurrentColor := CurrentColor;
  768. CurrentColor := CurrentBkColor;
  769. DirectPutPixel(x1,y);
  770. CurrentColor := OldCurrentColor;
  771. end;
  772. Inc(x1);
  773. if x1 > x2 then
  774. begin
  775. CurrentWriteMode := OldWriteMode;
  776. exit;
  777. end;
  778. end;
  779. end;
  780. end;
  781. End;
  782. CurrentWriteMode := OldWriteMode;
  783. end;
  784. procedure LineRel(Dx, Dy: smallint);
  785. Begin
  786. Line(CurrentX, CurrentY, CurrentX + Dx, CurrentY + Dy);
  787. CurrentX := CurrentX + Dx;
  788. CurrentY := CurrentY + Dy;
  789. end;
  790. procedure LineTo(x,y : smallint);
  791. Begin
  792. Line(CurrentX, CurrentY, X, Y);
  793. CurrentX := X;
  794. CurrentY := Y;
  795. end;
  796. procedure Rectangle(x1,y1,x2,y2:smallint);
  797. begin
  798. { Do not draw the end points }
  799. Line(x1,y1,x2-1,y1);
  800. Line(x1,y1+1,x1,y2);
  801. Line(x2,y1,x2,y2-1);
  802. Line(x1+1,y2,x2,y2);
  803. end;
  804. procedure GetLineSettings(var ActiveLineInfo : LineSettingsType);
  805. begin
  806. Activelineinfo:=Lineinfo;
  807. end;
  808. procedure SetLineStyle(LineStyle: word; Pattern: word; Thickness: word);
  809. var
  810. i: byte;
  811. j: byte;
  812. Begin
  813. if (LineStyle > UserBitLn) or ((Thickness <> Normwidth) and (Thickness <> ThickWidth)) then
  814. _GraphResult := grError
  815. else
  816. begin
  817. LineInfo.Thickness := Thickness;
  818. LineInfo.LineStyle := LineStyle;
  819. case LineStyle of
  820. UserBitLn: Lineinfo.Pattern := pattern;
  821. SolidLn: Lineinfo.Pattern := $ffff; { ------- }
  822. DashedLn : Lineinfo.Pattern := $F8F8; { -- -- --}
  823. DottedLn: LineInfo.Pattern := $CCCC; { - - - - }
  824. CenterLn: LineInfo.Pattern := $FC78; { -- - -- }
  825. end; { end case }
  826. { setup pattern styles }
  827. j:=16;
  828. for i:=0 to 15 do
  829. Begin
  830. dec(j);
  831. { bitwise mask for each bit in the word }
  832. if (word($01 shl i) AND LineInfo.Pattern) <> 0 then
  833. LinePatterns[j]:=TRUE
  834. else
  835. LinePatterns[j]:=FALSE;
  836. end;
  837. end;
  838. end;
  839. {--------------------------------------------------------------------------}
  840. { }
  841. { VIEWPORT RELATED ROUTINES }
  842. { }
  843. {--------------------------------------------------------------------------}
  844. Procedure ClearViewPortDefault; {$ifndef fpc}far;{$endif fpc}
  845. var
  846. j: smallint;
  847. OldWriteMode, OldCurColor: word;
  848. LineSets : LineSettingsType;
  849. Begin
  850. { CP is always RELATIVE coordinates }
  851. CurrentX := 0;
  852. CurrentY := 0;
  853. { Save all old settings }
  854. OldCurColor := CurrentColor;
  855. CurrentColor:=CurrentBkColor;
  856. OldWriteMode:=CurrentWriteMode;
  857. CurrentWriteMode:=NormalPut;
  858. GetLineSettings(LineSets);
  859. { reset to normal line style...}
  860. SetLineStyle(SolidLn, 0, NormWidth);
  861. { routines are relative here...}
  862. { ViewHeight is Height-1 ! }
  863. for J:=0 to ViewHeight do
  864. HLine(0, ViewWidth , J);
  865. { restore old settings...}
  866. SetLineStyle(LineSets.LineStyle, LineSets.Pattern, LineSets.Thickness);
  867. CurrentColor := OldCurColor;
  868. CurrentWriteMode := OldWriteMode;
  869. end;
  870. Procedure SetViewPort(X1, Y1, X2, Y2: smallint; Clip: Boolean);
  871. Begin
  872. if (X1 > GetMaxX) or (X2 > GetMaxX) or (X1 > X2) or (X1 < 0) then
  873. Begin
  874. {$ifdef logging}
  875. logln('invalid setviewport parameters: ('
  876. +strf(x1)+','+strf(y1)+'), ('+strf(x2)+','+strf(y2)+')');
  877. logln('maxx = '+strf(getmaxx)+', maxy = '+strf(getmaxy));
  878. {$endif logging}
  879. _GraphResult := grError;
  880. exit;
  881. end;
  882. if (Y1 > GetMaxY) or (Y2 > GetMaxY) or (Y1 > Y2) or (Y1 < 0) then
  883. Begin
  884. {$ifdef logging}
  885. logln('invalid setviewport parameters: ('
  886. +strf(x1)+','+strf(y1)+'), ('+strf(x2)+','+strf(y2)+')');
  887. logln('maxx = '+strf(getmaxx)+', maxy = '+strf(getmaxy));
  888. {$endif logging}
  889. _GraphResult := grError;
  890. exit;
  891. end;
  892. { CP is always RELATIVE coordinates }
  893. CurrentX := 0;
  894. CurrentY := 0;
  895. StartXViewPort := X1;
  896. StartYViewPort := Y1;
  897. ViewWidth := X2-X1;
  898. ViewHeight:= Y2-Y1;
  899. ClipPixels := Clip;
  900. end;
  901. procedure GetViewSettings(var viewport : ViewPortType);
  902. begin
  903. ViewPort.X1 := StartXViewPort;
  904. ViewPort.Y1 := StartYViewPort;
  905. ViewPort.X2 := ViewWidth + StartXViewPort;
  906. ViewPort.Y2 := ViewHeight + StartYViewPort;
  907. ViewPort.Clip := ClipPixels;
  908. end;
  909. procedure ClearDevice;
  910. var
  911. ViewPort: ViewPortType;
  912. begin
  913. { Reset the CP }
  914. CurrentX := 0;
  915. CurrentY := 0;
  916. { save viewport }
  917. ViewPort.X1 := StartXviewPort;
  918. ViewPort.X2 := ViewWidth - StartXViewPort;
  919. ViewPort.Y1 := StartYViewPort;
  920. ViewPort.Y2 := ViewHeight - StartYViewPort;
  921. ViewPort.Clip := ClipPixels;
  922. { put viewport to full screen }
  923. StartXViewPort := 0;
  924. ViewHeight := MaxY;
  925. StartYViewPort := 0;
  926. ViewWidth := MaxX;
  927. ClipPixels := TRUE;
  928. ClearViewPort;
  929. { restore old viewport }
  930. StartXViewPort := ViewPort.X1;
  931. ViewWidth := ViewPort.X2-ViewPort.X1;
  932. StartYViewPort := ViewPort.Y1;
  933. ViewHeight := ViewPort.Y2-ViewPort.Y1;
  934. ClipPixels := ViewPort.Clip;
  935. end;
  936. {--------------------------------------------------------------------------}
  937. { }
  938. { BITMAP PUT/GET ROUTINES }
  939. { }
  940. {--------------------------------------------------------------------------}
  941. Procedure GetScanlineDefault (X1, X2, Y : smallint; Var Data); {$ifndef fpc}far;{$endif fpc}
  942. {**********************************************************}
  943. { Procedure GetScanLine() }
  944. {----------------------------------------------------------}
  945. { Returns the full scanline of the video line of the Y }
  946. { coordinate. The values are returned in a WORD array }
  947. { each WORD representing a pixel of the specified scanline }
  948. { note: we only need the pixels inside the ViewPort! (JM) }
  949. { note2: extended so you can specify start and end X coord }
  950. { so it is usable for GetImage too (JM) }
  951. {**********************************************************}
  952. Var
  953. x : smallint;
  954. Begin
  955. For x:=X1 to X2 Do
  956. WordArray(Data)[x-x1]:=GetPixel(x, y);
  957. End;
  958. Function DefaultImageSize(X1,Y1,X2,Y2: smallint): longint; {$ifndef fpc}far;{$endif fpc}
  959. Begin
  960. { each pixel uses two bytes, to enable modes with colors up to 64K }
  961. { to work. }
  962. DefaultImageSize := 12 + (((X2-X1+1)*(Y2-Y1+1))*2);
  963. end;
  964. Procedure DefaultPutImage(X,Y: smallint; var Bitmap; BitBlt: Word); {$ifndef fpc}far;{$endif fpc}
  965. type
  966. pt = array[0..$fffffff] of word;
  967. ptw = array[0..2] of longint;
  968. var
  969. k: longint;
  970. oldCurrentColor: word;
  971. oldCurrentWriteMode, i, j, y1, x1, deltaX, deltaX1, deltaY: smallint;
  972. Begin
  973. {$ifdef logging}
  974. LogLn('putImage at ('+strf(x)+','+strf(y)+') with width '+strf(ptw(Bitmap)[0])+
  975. ' and height '+strf(ptw(Bitmap)[1]));
  976. deltaY := 0;
  977. {$endif logging}
  978. inc(x,startXViewPort);
  979. inc(y,startYViewPort);
  980. { width/height are 1-based, coordinates are zero based }
  981. x1 := ptw(Bitmap)[0]+x-1; { get width and adjust end coordinate accordingly }
  982. y1 := ptw(Bitmap)[1]+y-1; { get height and adjust end coordinate accordingly }
  983. deltaX := 0;
  984. deltaX1 := 0;
  985. k := 3 * sizeOf(Longint) div sizeOf(Word); { Three reserved longs at start of bitmap }
  986. { check which part of the image is in the viewport }
  987. if clipPixels then
  988. begin
  989. if y < startYViewPort then
  990. begin
  991. deltaY := startYViewPort - y;
  992. inc(k,(x1-x+1)*deltaY);
  993. y := startYViewPort;
  994. end;
  995. if y1 > startYViewPort+viewHeight then
  996. y1 := startYViewPort+viewHeight;
  997. if x < startXViewPort then
  998. begin
  999. deltaX := startXViewPort-x;
  1000. x := startXViewPort;
  1001. end;
  1002. if x1 > startXViewPort + viewWidth then
  1003. begin
  1004. deltaX1 := x1 - (startXViewPort + viewWidth);
  1005. x1 := startXViewPort + viewWidth;
  1006. end;
  1007. end;
  1008. {$ifdef logging}
  1009. LogLn('deltax: '+strf(deltax)+', deltax1: '+strf(deltax1)+',deltay: '+strf(deltay));
  1010. {$endif logging}
  1011. oldCurrentColor := currentColor;
  1012. oldCurrentWriteMode := currentWriteMode;
  1013. currentWriteMode := bitBlt;
  1014. for j:=Y to Y1 do
  1015. Begin
  1016. inc(k,deltaX);
  1017. for i:=X to X1 do
  1018. begin
  1019. currentColor := pt(bitmap)[k];
  1020. directPutPixel(i,j);
  1021. inc(k);
  1022. end;
  1023. inc(k,deltaX1);
  1024. end;
  1025. currentWriteMode := oldCurrentWriteMode;
  1026. currentColor := oldCurrentColor;
  1027. end;
  1028. Procedure DefaultGetImage(X1,Y1,X2,Y2: smallint; Var Bitmap); {$ifndef fpc}far;{$endif fpc}
  1029. type
  1030. pt = array[0..$fffffff] of word;
  1031. ptw = array[0..2] of longint;
  1032. var
  1033. i,j: smallint;
  1034. k: longint;
  1035. Begin
  1036. k:= 3 * Sizeof(longint) div sizeof(word); { Three reserved longs at start of bitmap }
  1037. i := x2 - x1 + 1;
  1038. for j:=Y1 to Y2 do
  1039. Begin
  1040. GetScanLine(x1,x2,j,pt(Bitmap)[k]);
  1041. inc(k,i);
  1042. end;
  1043. ptw(Bitmap)[0] := X2-X1+1; { First longint is width }
  1044. ptw(Bitmap)[1] := Y2-Y1+1; { Second longint is height }
  1045. ptw(bitmap)[2] := 0; { Third longint is reserved}
  1046. end;
  1047. Procedure GetArcCoords(var ArcCoords: ArcCoordsType);
  1048. Begin
  1049. ArcCoords.X := ArcCall.X;
  1050. ArcCoords.Y := ArcCall.Y;
  1051. ArcCoords.XStart := ArcCall.XStart;
  1052. ArcCoords.YStart := ArcCall.YStart;
  1053. ArcCoords.XEnd := ArcCall.XEnd;
  1054. ArcCoords.YEnd := ArcCall.YEnd;
  1055. end;
  1056. procedure SetVisualPageDefault(page : word); {$ifndef fpc}far;{$endif fpc}
  1057. begin
  1058. end;
  1059. procedure SetActivePageDefault(page : word); {$ifndef fpc}far;{$endif fpc}
  1060. begin
  1061. end;
  1062. procedure DirectPutPixelDefault(X,Y: smallint);
  1063. begin
  1064. Writeln(stderr,'Error: Not in graphics mode (use InitGraph and test GraphResult afterwards)');
  1065. Halt(1);
  1066. end;
  1067. function GetPixelDefault(X,Y: smallint): word;
  1068. begin
  1069. Writeln(stderr,'Error: Not in graphics mode (use InitGraph and test GraphResult afterwards)');
  1070. Halt(1);
  1071. exit(0); { avoid warning }
  1072. end;
  1073. procedure PutPixelDefault(X,Y: smallint; Color: Word);
  1074. begin
  1075. Writeln(stderr,'Error: Not in graphics mode (use InitGraph and test GraphResult afterwards)');
  1076. Halt(1);
  1077. end;
  1078. procedure SetRGBPaletteDefault(ColorNum, RedValue, GreenValue, BlueValue: smallint);
  1079. begin
  1080. Writeln(stderr,'Error: Not in graphics mode (use InitGraph and test GraphResult afterwards)');
  1081. Halt(1);
  1082. end;
  1083. procedure GetRGBPaletteDefault(ColorNum: smallint; var
  1084. RedValue, GreenValue, BlueValue: smallint);
  1085. begin
  1086. Writeln(stderr,'Error: Not in graphics mode (use InitGraph and test GraphResult afterwards)');
  1087. Halt(1);
  1088. end;
  1089. procedure OutTextXYDefault(x,y : smallint;const TextString : string);forward;
  1090. procedure CircleDefault(X, Y: smallint; Radius:Word);forward;
  1091. {$i palette.inc}
  1092. Procedure DefaultHooks;
  1093. {********************************************************}
  1094. { Procedure DefaultHooks() }
  1095. {--------------------------------------------------------}
  1096. { Resets all hookable routine either to nil for those }
  1097. { which need overrides, and others to defaults. }
  1098. { This is called each time SetGraphMode() is called. }
  1099. {********************************************************}
  1100. Begin
  1101. { All default hooks procedures }
  1102. { required...}
  1103. DirectPutPixel := {$ifdef fpc}@{$endif}DirectPutPixelDefault;
  1104. PutPixel := {$ifdef fpc}@{$endif}PutPixelDefault;
  1105. GetPixel := {$ifdef fpc}@{$endif}GetPixelDefault;
  1106. SetRGBPalette := {$ifdef fpc}@{$endif}SetRGBPaletteDefault;
  1107. GetRGBPalette := {$ifdef fpc}@{$endif}GetRGBPaletteDefault;
  1108. { optional...}
  1109. SetAllPalette := {$ifdef fpc}@{$endif}SetAllPaletteDefault;
  1110. SetActivePage := {$ifdef fpc}@{$endif}SetActivePageDefault;
  1111. SetVisualPage := {$ifdef fpc}@{$endif}SetVisualPageDefault;
  1112. ClearViewPort := {$ifdef fpc}@{$endif}ClearViewportDefault;
  1113. PutImage := {$ifdef fpc}@{$endif}DefaultPutImage;
  1114. GetImage := {$ifdef fpc}@{$endif}DefaultGetImage;
  1115. ImageSize := {$ifdef fpc}@{$endif}DefaultImageSize;
  1116. GraphFreeMemPtr := nil;
  1117. GraphGetMemPtr := nil;
  1118. GetScanLine := {$ifdef fpc}@{$endif}GetScanLineDefault;
  1119. Line := {$ifdef fpc}@{$endif}LineDefault;
  1120. InternalEllipse := {$ifdef fpc}@{$endif}InternalEllipseDefault;
  1121. PatternLine := {$ifdef fpc}@{$endif}PatternLineDefault;
  1122. HLine := {$ifdef fpc}@{$endif}HLineDefault;
  1123. VLine := {$ifdef fpc}@{$endif}VLineDefault;
  1124. OuttextXY := {$ifdef fpc}@{$endif}OuttextXYDefault;
  1125. Circle := {$ifdef fpc}@{$endif}CircleDefault;
  1126. end;
  1127. Procedure InitVars;
  1128. {********************************************************}
  1129. { Procedure InitVars() }
  1130. {--------------------------------------------------------}
  1131. { Resets all internal variables, and resets all }
  1132. { overridable routines. }
  1133. {********************************************************}
  1134. Begin
  1135. DirectVideo := TRUE; { By default use fastest access possible }
  1136. ArcCall.X := 0;
  1137. ArcCall.Y := 0;
  1138. ArcCall.XStart := 0;
  1139. ArcCall.YStart := 0;
  1140. ArcCall.XEnd := 0;
  1141. ArcCall.YEnd := 0;
  1142. { Reset to default values }
  1143. IntCurrentMode := 0;
  1144. IntCurrentDriver := 0;
  1145. IntCurrentNewDriver := 0;
  1146. XAspect := 0;
  1147. YAspect := 0;
  1148. MaxX := 0;
  1149. MaxY := 0;
  1150. MaxColor := 0;
  1151. PaletteSize := 0;
  1152. DirectColor := FALSE;
  1153. HardwarePages := 0;
  1154. if hardwarepages=0 then; { remove note }
  1155. DefaultHooks;
  1156. end;
  1157. {$i modes.inc}
  1158. function InstallUserDriver(Name: string; AutoDetectPtr: Pointer): smallint;
  1159. begin
  1160. _graphResult := grError;
  1161. InstallUserDriver:=grError;
  1162. end;
  1163. function RegisterBGIDriver(driver: pointer): smallint;
  1164. begin
  1165. _graphResult := grError;
  1166. RegisterBGIDriver:=grError;
  1167. end;
  1168. { ----------------------------------------------------------------- }
  1169. Procedure Arc(X,Y : smallint; StAngle,EndAngle,Radius: word);
  1170. { var
  1171. OldWriteMode: word;}
  1172. Begin
  1173. { Only if we are using thickwidths lines do we accept }
  1174. { XORput write modes. }
  1175. { OldWriteMode := CurrentWriteMode;
  1176. if (LineInfo.Thickness = NormWidth) then
  1177. CurrentWriteMode := NormalPut;}
  1178. InternalEllipse(X,Y,Radius,Radius,StAngle,Endangle,{$ifdef fpc}@{$endif}DummyPatternLine);
  1179. { CurrentWriteMode := OldWriteMode;}
  1180. end;
  1181. procedure Ellipse(X,Y : smallint; stAngle, EndAngle: word; XRadius,YRadius: word);
  1182. Begin
  1183. InternalEllipse(X,Y,XRadius,YRadius,StAngle,Endangle,{$ifdef fpc}@{$endif}DummyPatternLine);
  1184. end;
  1185. procedure FillEllipse(X, Y: smallint; XRadius, YRadius: Word);
  1186. {********************************************************}
  1187. { Procedure FillEllipse() }
  1188. {--------------------------------------------------------}
  1189. { Draws a filled ellipse using (X,Y) as a center point }
  1190. { and XRadius and YRadius as the horizontal and vertical }
  1191. { axes. The ellipse is filled with the current fill color}
  1192. { and fill style, and is bordered with the current color.}
  1193. {********************************************************}
  1194. begin
  1195. InternalEllipse(X,Y,XRadius,YRadius,0,360,PatternLine)
  1196. end;
  1197. procedure CircleDefault(X, Y: smallint; Radius:Word);
  1198. {********************************************************}
  1199. { Draws a circle centered at X,Y with the given Radius. }
  1200. {********************************************************}
  1201. { Important notes: }
  1202. { - Thickwidth circles use the current write mode, while}
  1203. { normal width circles ALWAYS use CopyPut/NormalPut }
  1204. { mode. (Tested against VGA BGI driver -CEC 13/Aug/99 }
  1205. {********************************************************}
  1206. var OriginalArcInfo: ArcCoordsType;
  1207. OldWriteMode: word;
  1208. begin
  1209. if (Radius = 0) then
  1210. Exit;
  1211. if (Radius = 1) then
  1212. begin
  1213. { only normal put mode is supported by a call to PutPixel }
  1214. PutPixel(X, Y, CurrentColor);
  1215. Exit;
  1216. end;
  1217. { save state of arc information }
  1218. { because it is not needed for }
  1219. { a circle call. }
  1220. System.move(ArcCall,OriginalArcInfo, sizeof(ArcCall));
  1221. if LineInfo.Thickness = Normwidth then
  1222. begin
  1223. OldWriteMode := CurrentWriteMode;
  1224. CurrentWriteMode := CopyPut;
  1225. end;
  1226. { Adjust for screen aspect ratio }
  1227. InternalEllipse(X,Y,Radius,(longint(Radius)*XAspect) div YAspect,0,360,{$ifdef fpc}@{$endif}DummyPatternLine);
  1228. if LineInfo.Thickness = Normwidth then
  1229. CurrentWriteMode := OldWriteMode;
  1230. { restore arc information }
  1231. System.move(OriginalArcInfo, ArcCall,sizeof(ArcCall));
  1232. end;
  1233. procedure SectorPL(x1,x2,y: smallint); {$ifndef fpc}far;{$endif fpc}
  1234. var plx1, plx2: smallint;
  1235. begin
  1236. If (x1 = -maxsmallint) Then
  1237. If (x2 = maxsmallint-1) Then
  1238. { no ellipse points drawn on this line }
  1239. If (((Y < ArcCall.Y) and (Y > ArcCall.YStart)) or
  1240. ((Y > ArcCall.Y) and (Y < ArcCall.YStart))) Then
  1241. { there is a part of the sector at this y coordinate, but no }
  1242. { ellips points are plotted on this line, so draw a patternline }
  1243. { between the lines connecting (arccall.x,arccall.y) with }
  1244. { the start and the end of the arc (JM) }
  1245. { use: y-y1=(y2-y1)/(x2-x1)*(x-x1) => }
  1246. { x = (y-y1)/(y2-y1)*(x2-x1)+x1 }
  1247. Begin
  1248. plx1 := (y-ArcCall.Y)*(ArcCall.XStart-ArcCall.X)
  1249. div (ArcCall.YStart-ArcCall.Y)+ArcCall.X;
  1250. plx2 := (y-ArcCall.Y)*(ArcCall.XEnd-ArcCall.X)
  1251. div (ArcCall.YEnd-ArcCall.Y)+ArcCall.X;
  1252. If plx1 > plx2 then
  1253. begin
  1254. plx1 := plx1 xor plx2;
  1255. plx2 := plx1 xor plx2;
  1256. plx1 := plx1 xor plx2;
  1257. end;
  1258. End
  1259. { otherwise two points which have nothing to do with the sector }
  1260. Else exit
  1261. Else
  1262. { the arc is plotted at the right side, but not at the left side, }
  1263. { fill till the line between (ArcCall.X,ArcCall.Y) and }
  1264. { (ArcCall.XStart,ArcCall.YStart) }
  1265. Begin
  1266. If (y < ArcCall.Y) then
  1267. begin
  1268. plx1 := (y-ArcCall.Y)*(ArcCall.XEnd-ArcCall.X)
  1269. div (ArcCall.YEnd-ArcCall.Y)+ArcCall.X
  1270. end
  1271. else if (y > ArcCall.Y) then
  1272. begin
  1273. plx1 := (y-ArcCall.Y)*(ArcCall.XStart-ArcCall.X)
  1274. div (ArcCall.YStart-ArcCall.Y)+ArcCall.X
  1275. end
  1276. else plx1 := ArcCall.X;
  1277. plx2 := x2;
  1278. End
  1279. Else
  1280. If (x2 = maxsmallint-1) Then
  1281. { the arc is plotted at the left side, but not at the rigth side. }
  1282. { the right limit can be either the first or second line. Just take }
  1283. { the closest one, but watch out for division by zero! }
  1284. Begin
  1285. If (y < ArcCall.Y) then
  1286. begin
  1287. plx2 := (y-ArcCall.Y)*(ArcCall.XStart-ArcCall.X)
  1288. div (ArcCall.YStart-ArcCall.Y)+ArcCall.X
  1289. end
  1290. else if (y > ArcCall.Y) then
  1291. begin
  1292. plx2 := (y-ArcCall.Y)*(ArcCall.XEnd-ArcCall.X)
  1293. div (ArcCall.YEnd-ArcCall.Y)+ArcCall.X
  1294. end
  1295. else plx2 := ArcCall.X;
  1296. plx1 := x1;
  1297. End
  1298. Else
  1299. { the arc is plotted at both sides }
  1300. Begin
  1301. plx1 := x1;
  1302. plx2 := x2;
  1303. End;
  1304. If plx2 > plx1 then
  1305. Begin
  1306. PatternLine(plx1,plx2,y);
  1307. end;
  1308. end;
  1309. procedure Sector(x, y: smallint; StAngle,EndAngle, XRadius, YRadius: Word);
  1310. begin
  1311. internalellipse(x,y,XRadius, YRadius, StAngle, EndAngle, {$ifdef fpc}@{$endif}SectorPL);
  1312. Line(ArcCall.XStart, ArcCall.YStart, x,y);
  1313. Line(x,y,ArcCall.Xend,ArcCall.YEnd);
  1314. end;
  1315. procedure SetFillStyle(Pattern : word; Color: word);
  1316. begin
  1317. { on invalid input, the current fill setting will be }
  1318. { unchanged. }
  1319. if (Pattern > UserFill) or (Color > GetMaxColor) then
  1320. begin
  1321. {$ifdef logging}
  1322. logln('invalid fillstyle parameters');
  1323. {$endif logging}
  1324. _GraphResult := grError;
  1325. exit;
  1326. end;
  1327. FillSettings.Color := Color;
  1328. FillSettings.Pattern := Pattern;
  1329. end;
  1330. procedure SetFillPattern(Pattern: FillPatternType; Color: word);
  1331. {********************************************************}
  1332. { Changes the Current FillPattern to a user defined }
  1333. { pattern and changes also the current fill color. }
  1334. { The FillPattern is saved in the FillPattern array so }
  1335. { it can still be used with SetFillStyle(UserFill,Color) }
  1336. {********************************************************}
  1337. var
  1338. i: smallint;
  1339. begin
  1340. if Color > GetMaxColor then
  1341. begin
  1342. {$ifdef logging}
  1343. logln('invalid fillpattern parameters');
  1344. {$endif logging}
  1345. _GraphResult := grError;
  1346. exit;
  1347. end;
  1348. FillSettings.Color := Color;
  1349. FillSettings.Pattern := UserFill;
  1350. { Save the pattern in the buffer }
  1351. For i:=1 to 8 do
  1352. FillPatternTable[UserFill][i] := Pattern[i];
  1353. end;
  1354. procedure Bar(x1,y1,x2,y2:smallint);
  1355. {********************************************************}
  1356. { Important notes for compatibility with BP: }
  1357. { - WriteMode is always CopyPut }
  1358. { - No contour is drawn for the lines }
  1359. {********************************************************}
  1360. var y : smallint;
  1361. origcolor : longint;
  1362. origlinesettings: Linesettingstype;
  1363. origwritemode : smallint;
  1364. begin
  1365. origlinesettings:=lineinfo;
  1366. origcolor:=CurrentColor;
  1367. if y1>y2 then
  1368. begin
  1369. y:=y1;
  1370. y1:=y2;
  1371. y2:=y;
  1372. end;
  1373. { Always copy mode for Bars }
  1374. origwritemode := CurrentWriteMode;
  1375. CurrentWriteMode := CopyPut;
  1376. { All lines used are of this style }
  1377. Lineinfo.linestyle:=solidln;
  1378. Lineinfo.thickness:=normwidth;
  1379. case Fillsettings.pattern of
  1380. EmptyFill :
  1381. begin
  1382. Currentcolor:=CurrentBkColor;
  1383. for y:=y1 to y2 do
  1384. Hline(x1,x2,y);
  1385. end;
  1386. SolidFill :
  1387. begin
  1388. CurrentColor:=FillSettings.color;
  1389. for y:=y1 to y2 do
  1390. Hline(x1,x2,y);
  1391. end;
  1392. else
  1393. Begin
  1394. CurrentColor:=FillSettings.color;
  1395. for y:=y1 to y2 do
  1396. patternline(x1,x2,y);
  1397. end;
  1398. end;
  1399. CurrentColor:= Origcolor;
  1400. LineInfo := OrigLineSettings;
  1401. CurrentWriteMode := OrigWritemode;
  1402. end;
  1403. procedure bar3D(x1, y1, x2, y2 : smallint;depth : word;top : boolean);
  1404. var
  1405. origwritemode : smallint;
  1406. OldX, OldY : smallint;
  1407. begin
  1408. origwritemode := CurrentWriteMode;
  1409. CurrentWriteMode := CopyPut;
  1410. Bar(x1,y1,x2,y2);
  1411. Rectangle(x1,y1,x2,y2);
  1412. { Current CP should not be updated in Bar3D }
  1413. { therefore save it and then restore it on }
  1414. { exit. }
  1415. OldX := CurrentX;
  1416. OldY := CurrentY;
  1417. if top then begin
  1418. Moveto(x1,y1);
  1419. Lineto(x1+depth,y1-depth);
  1420. Lineto(x2+depth,y1-depth);
  1421. Lineto(x2,y1);
  1422. end;
  1423. if Depth <> 0 then
  1424. Begin
  1425. Moveto(x2+depth,y1-depth);
  1426. Lineto(x2+depth,y2-depth);
  1427. Lineto(x2,y2);
  1428. end;
  1429. { restore CP }
  1430. CurrentX := OldX;
  1431. CurrentY := OldY;
  1432. CurrentWriteMode := origwritemode;
  1433. end;
  1434. {--------------------------------------------------------------------------}
  1435. { }
  1436. { COLOR AND PALETTE ROUTINES }
  1437. { }
  1438. {--------------------------------------------------------------------------}
  1439. procedure SetColor(Color: Word);
  1440. Begin
  1441. CurrentColor := Color;
  1442. end;
  1443. function GetColor: Word;
  1444. Begin
  1445. GetColor := CurrentColor;
  1446. end;
  1447. function GetBkColor: Word;
  1448. Begin
  1449. GetBkColor := CurrentBkColor;
  1450. end;
  1451. procedure SetBkColor(ColorNum: Word);
  1452. { Background color means background screen color in this case, and it is }
  1453. { INDEPENDANT of the viewport settings, so we must clear the whole screen }
  1454. { with the color. }
  1455. var
  1456. ViewPort: ViewportType;
  1457. Begin
  1458. GetViewSettings(Viewport);
  1459. {$ifdef logging}
  1460. logln('calling setviewport from setbkcolor');
  1461. {$endif logging}
  1462. SetViewPort(0,0,MaxX,MaxY,FALSE);
  1463. {$ifdef logging}
  1464. logln('calling setviewport from setbkcolor done');
  1465. {$endif logging}
  1466. CurrentBkColor := ColorNum;
  1467. {ClearViewPort;}
  1468. if not DirectColor and (ColorNum<256) then
  1469. SetRGBPalette(0,
  1470. DefaultColors[ColorNum].Red,
  1471. DefaultColors[ColorNum].Green,
  1472. DefaultColors[ColorNum].Blue);
  1473. SetViewport(ViewPort.X1,Viewport.Y1,Viewport.X2,Viewport.Y2,Viewport.Clip);
  1474. end;
  1475. function GetMaxColor: word;
  1476. { Checked against TP VGA driver - CEC }
  1477. begin
  1478. GetMaxColor:=MaxColor-1; { based on an index of zero so subtract one }
  1479. end;
  1480. Procedure MoveRel(Dx, Dy: smallint);
  1481. Begin
  1482. CurrentX := CurrentX + Dx;
  1483. CurrentY := CurrentY + Dy;
  1484. end;
  1485. Procedure MoveTo(X,Y: smallint);
  1486. {********************************************************}
  1487. { Procedure MoveTo() }
  1488. {--------------------------------------------------------}
  1489. { Moves the current pointer in VIEWPORT relative }
  1490. { coordinates to the specified X,Y coordinate. }
  1491. {********************************************************}
  1492. Begin
  1493. CurrentX := X;
  1494. CurrentY := Y;
  1495. end;
  1496. function GraphErrorMsg(ErrorCode: smallint): string;
  1497. Begin
  1498. GraphErrorMsg:='';
  1499. case ErrorCode of
  1500. grOk,grFileNotFound,grInvalidDriver: exit;
  1501. grNoInitGraph: GraphErrorMsg:='Graphics driver not installed';
  1502. grNotDetected: GraphErrorMsg:='Graphics hardware not detected';
  1503. grNoLoadMem,grNoScanMem,grNoFloodMem: GraphErrorMsg := 'Not enough memory for graphics';
  1504. grNoFontMem: GraphErrorMsg := 'Not enough memory to load font';
  1505. grFontNotFound: GraphErrorMsg:= 'Font file not found';
  1506. grInvalidMode: GraphErrorMsg := 'Invalid graphics mode';
  1507. grError: GraphErrorMsg:='Graphics error';
  1508. grIoError: GraphErrorMsg:='Graphics I/O error';
  1509. grInvalidFont,grInvalidFontNum: GraphErrorMsg := 'Invalid font';
  1510. grInvalidVersion: GraphErrorMsg:='Invalid driver version';
  1511. end;
  1512. end;
  1513. Function GetMaxX: smallint;
  1514. { Routine checked against VGA driver - CEC }
  1515. Begin
  1516. GetMaxX := MaxX;
  1517. end;
  1518. Function GetMaxY: smallint;
  1519. { Routine checked against VGA driver - CEC }
  1520. Begin
  1521. GetMaxY := MaxY;
  1522. end;
  1523. Function GraphResult: smallint;
  1524. Begin
  1525. GraphResult := _GraphResult;
  1526. _GraphResult := grOk;
  1527. end;
  1528. Function GetX: smallint;
  1529. Begin
  1530. GetX := CurrentX;
  1531. end;
  1532. Function GetY: smallint;
  1533. Begin
  1534. GetY := CurrentY;
  1535. end;
  1536. Function GetDriverName: string;
  1537. begin
  1538. GetDriverName:=DriverName;
  1539. end;
  1540. procedure graphdefaults;
  1541. { PS: GraphDefaults does not ZERO the ArcCall structure }
  1542. { so a call to GetArcCoords will not change even the }
  1543. { returned values even if GraphDefaults is called in }
  1544. { between. }
  1545. var
  1546. i: smallint;
  1547. begin
  1548. lineinfo.linestyle:=solidln;
  1549. lineinfo.thickness:=normwidth;
  1550. { reset line style pattern }
  1551. for i:=0 to 15 do
  1552. LinePatterns[i] := TRUE;
  1553. { By default, according to the TP prog's reference }
  1554. { the default pattern is solid, and the default }
  1555. { color is the maximum color in the palette. }
  1556. fillsettings.color:=GetMaxColor;
  1557. fillsettings.pattern:=solidfill;
  1558. { GraphDefaults resets the User Fill pattern to $ff }
  1559. { checked with VGA BGI driver - CEC }
  1560. for i:=1 to 8 do
  1561. FillPatternTable[UserFill][i] := $ff;
  1562. CurrentColor:=white;
  1563. ClipPixels := TRUE;
  1564. { Reset the viewport }
  1565. StartXViewPort := 0;
  1566. StartYViewPort := 0;
  1567. ViewWidth := MaxX;
  1568. ViewHeight := MaxY;
  1569. { Reset CP }
  1570. CurrentX := 0;
  1571. CurrentY := 0;
  1572. SetBkColor(Black);
  1573. { normal write mode }
  1574. CurrentWriteMode := CopyPut;
  1575. { set font style }
  1576. CurrentTextInfo.font := DefaultFont;
  1577. CurrentTextInfo.direction:=HorizDir;
  1578. CurrentTextInfo.charsize:=1;
  1579. CurrentTextInfo.horiz:=LeftText;
  1580. CurrentTextInfo.vert:=TopText;
  1581. XAspect:=10000; YAspect:=10000;
  1582. end;
  1583. procedure GetAspectRatio(var Xasp,Yasp : word);
  1584. begin
  1585. XAsp:=XAspect;
  1586. YAsp:=YAspect;
  1587. end;
  1588. procedure SetAspectRatio(Xasp, Yasp : word);
  1589. begin
  1590. Xaspect:= XAsp;
  1591. YAspect:= YAsp;
  1592. end;
  1593. procedure SetWriteMode(WriteMode : smallint);
  1594. { TP sets the writemodes according to the following scheme (JM) }
  1595. begin
  1596. Case writemode of
  1597. xorput, andput: CurrentWriteMode := XorPut;
  1598. notput, orput, copyput: CurrentWriteMode := CopyPut;
  1599. End;
  1600. end;
  1601. procedure GetFillSettings(var Fillinfo:Fillsettingstype);
  1602. begin
  1603. Fillinfo:=Fillsettings;
  1604. end;
  1605. procedure GetFillPattern(var FillPattern:FillPatternType);
  1606. begin
  1607. FillPattern:=FillpatternTable[UserFill];
  1608. end;
  1609. procedure DrawPoly(numpoints : word;var polypoints);
  1610. type
  1611. ppointtype = ^pointtype;
  1612. pt = array[0..16000] of pointtype;
  1613. var
  1614. i : longint;
  1615. begin
  1616. if numpoints < 2 then
  1617. begin
  1618. _GraphResult := grError;
  1619. exit;
  1620. end;
  1621. for i:=0 to numpoints-2 do
  1622. line(pt(polypoints)[i].x,
  1623. pt(polypoints)[i].y,
  1624. pt(polypoints)[i+1].x,
  1625. pt(polypoints)[i+1].y);
  1626. end;
  1627. procedure PieSlice(X,Y,stangle,endAngle:smallint;Radius: Word);
  1628. begin
  1629. Sector(x,y,stangle,endangle,radius,radius);
  1630. end;
  1631. {$i fills.inc}
  1632. {$i gtext.inc}
  1633. procedure internDetectGraph(var GraphDriver, GraphMode:smallint;
  1634. calledFromInitGraph: boolean);
  1635. var LoMode, HiMode: smallint;
  1636. CpyMode: smallint;
  1637. CpyDriver: smallint;
  1638. begin
  1639. HiMode := -1;
  1640. LoMode := -1;
  1641. if not calledFromInitGraph or
  1642. (graphDriver < lowNewDriver) or
  1643. (graphDriver > highNewDriver) then
  1644. begin
  1645. { Search lowest supported bitDepth }
  1646. graphdriver := D1bit;
  1647. while (graphDriver <= highNewDriver) and
  1648. (hiMode = -1) do
  1649. begin
  1650. getModeRange(graphDriver,loMode,hiMode);
  1651. inc(graphDriver);
  1652. end;
  1653. dec(graphdriver);
  1654. if hiMode = -1 then
  1655. begin
  1656. _GraphResult := grNotDetected;
  1657. exit;
  1658. end;
  1659. CpyMode := 0;
  1660. repeat
  1661. GetModeRange(GraphDriver,LoMode,HiMode);
  1662. { save the highest mode possible...}
  1663. {$ifdef logging}
  1664. logln('Found driver '+strf(graphdriver)+' with modes '+
  1665. strf(lomode)+' - '+strf(himode));
  1666. {$endif logging}
  1667. if HiMode <> -1 then
  1668. begin
  1669. CpyMode:=HiMode;
  1670. CpyDriver:=GraphDriver;
  1671. end;
  1672. { go to next driver if it exists...}
  1673. Inc(graphDriver);
  1674. until (graphDriver > highNewDriver);
  1675. end
  1676. else
  1677. begin
  1678. cpyMode := 0;
  1679. getModeRange(graphDriver,loMode,hiMode);
  1680. if hiMode <> -1 then
  1681. begin
  1682. cpyDriver := graphDriver;
  1683. cpyMode := hiMode;
  1684. end;
  1685. end;
  1686. if cpyMode = 0 then
  1687. begin
  1688. _GraphResult := grNotDetected;
  1689. exit;
  1690. end;
  1691. _GraphResult := grOK;
  1692. GraphDriver := CpyDriver;
  1693. GraphMode := CpyMode;
  1694. end;
  1695. procedure detectGraph(var GraphDriver: smallint; var GraphMode:smallint);
  1696. begin
  1697. internDetectGraph(graphDriver,graphMode,false);
  1698. end;
  1699. procedure InitGraph(var GraphDriver:smallint;var GraphMode:smallint;
  1700. const PathToDriver:String);
  1701. const
  1702. dirchar = System.DirectorySeparator;
  1703. begin
  1704. InitVars;
  1705. { path to the fonts (where they will be searched)...}
  1706. bgipath:=PathToDriver;
  1707. if (Length(bgipath) > 0) and (bgipath[length(bgipath)]<>dirchar) then
  1708. bgipath:=bgipath+dirchar;
  1709. if not assigned(SaveVideoState) then
  1710. RunError(216);
  1711. DriverName:=InternalDriverName; { DOS Graphics driver }
  1712. if (Graphdriver=Detect)
  1713. or (GraphMode = detectMode) then
  1714. begin
  1715. internDetectGraph(GraphDriver,GraphMode,true);
  1716. If _GraphResult = grNotDetected then Exit;
  1717. { _GraphResult is now already set to grOK by DetectGraph }
  1718. IntCurrentDriver := GraphDriver;
  1719. if (graphDriver >= lowNewDriver) and
  1720. (graphDriver <= highNewDriver) then
  1721. IntCurrentNewDriver := GraphDriver
  1722. else IntCurrentNewDriver := -1;
  1723. { Actually set the graph mode...}
  1724. if firstCallOfInitgraph then
  1725. begin
  1726. SaveVideoState;
  1727. firstCallOfInitgraph := false;
  1728. end;
  1729. SetGraphMode(GraphMode);
  1730. end
  1731. else
  1732. begin
  1733. { Search if that graphics modec actually exists...}
  1734. if SearchMode(GraphDriver,GraphMode) = nil then
  1735. begin
  1736. _GraphResult := grInvalidMode;
  1737. exit;
  1738. end
  1739. else
  1740. begin
  1741. _GraphResult := grOK;
  1742. IntCurrentDriver := GraphDriver;
  1743. if (graphDriver >= lowNewDriver) and
  1744. (graphDriver <= highNewDriver) then
  1745. IntCurrentNewDriver := GraphDriver
  1746. else IntCurrentNewDriver := -1;
  1747. if firstCallOfInitgraph then
  1748. begin
  1749. SaveVideoState;
  1750. firstCallOfInitgraph := false;
  1751. end;
  1752. SetGraphMode(GraphMode);
  1753. end;
  1754. end;
  1755. end;
  1756. procedure SetDirectVideo(DirectAccess: boolean);
  1757. begin
  1758. DirectVideo := DirectAccess;
  1759. end;
  1760. function GetDirectVideo: boolean;
  1761. begin
  1762. GetDirectVideo := DirectVideo;
  1763. end;
  1764. procedure GraphExitProc; {$ifndef fpc} far; {$endif fpc}
  1765. { deallocates all memory allocated by the graph unit }
  1766. var
  1767. list: PModeInfo;
  1768. tmp : PModeInfo;
  1769. c: longint;
  1770. begin
  1771. { restore old exitproc! }
  1772. exitproc := exitsave;
  1773. if IsGraphMode and ((errorcode<>0) or (erroraddr<>nil)) then
  1774. CloseGraph;
  1775. { release memory allocated for fonts }
  1776. for c := 1 to installedfonts do
  1777. with fonts[c] Do
  1778. If assigned(instr) Then
  1779. System.Freemem(instr,instrlength);
  1780. { release memory allocated for modelist }
  1781. list := ModeList;
  1782. while assigned(list) do
  1783. begin
  1784. tmp := list;
  1785. list:=list^.next;
  1786. dispose(tmp);
  1787. end;
  1788. for c := lowNewDriver to highNewDriver do
  1789. begin
  1790. list := newModeList.modeinfo[c];
  1791. while assigned(list) do
  1792. begin
  1793. tmp := list;
  1794. list:=list^.next;
  1795. dispose(tmp);
  1796. end;
  1797. end;
  1798. {$IFDEF DPMI}
  1799. { We had copied the buffer of mode information }
  1800. { and allocated it dynamically... now free it }
  1801. { Warning: if GetVESAInfo returned false, this buffer is not allocated! (JM)}
  1802. If hasVesa then
  1803. Dispose(VESAInfo.ModeList);
  1804. {$ENDIF}
  1805. end;
  1806. procedure InitializeGraph;
  1807. begin
  1808. {$ifdef logging}
  1809. assign(debuglog,'grlog.txt');
  1810. rewrite(debuglog);
  1811. close(debuglog);
  1812. {$endif logging}
  1813. isgraphmode := false;
  1814. ModeList := nil;
  1815. fillChar(newModeList.modeinfo,sizeof(newModeList.modeinfo),#0);
  1816. { lo and hi modenumber are -1 currently (no modes supported) }
  1817. fillChar(newModeList.loHiModeNr,sizeof(newModeList.loHiModeNr),#255);
  1818. SaveVideoState := nil;
  1819. RestoreVideoState := nil;
  1820. { This must be called at startup... because GetGraphMode may }
  1821. { be called even when not in graph mode. }
  1822. {$ifdef logging}
  1823. LogLn('Calling QueryAdapterInfo...');
  1824. {$endif logging}
  1825. QueryAdapterInfo;
  1826. { Install standard fonts }
  1827. { This is done BEFORE startup... }
  1828. InstalledFonts := 0;
  1829. InstallUserFont('TRIP');
  1830. InstallUserFont('LITT');
  1831. InstallUserFont('SANS');
  1832. InstallUserFont('GOTH');
  1833. InstallUserFont('SCRI');
  1834. InstallUserFont('SIMP');
  1835. InstallUserFont('TSCR');
  1836. InstallUserFont('LCOM');
  1837. InstallUserFont('EURO');
  1838. InstallUserFont('BOLD');
  1839. { This installs an exit procedure which cleans up the mode list...}
  1840. ExitSave := ExitProc;
  1841. ExitProc := @GraphExitProc;
  1842. {$if defined(win32) and not defined(sdlgraph)}
  1843. charmessagehandler:=nil;
  1844. {$endif win32 and not sdlgraph}
  1845. end;