graph.inc 72 KB

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