graph.inc 63 KB

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