graph.inc 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479
  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. { width/height are 1-based, coordinates are zero based }
  1169. x1 := ptw(Bitmap)[0]+x-1; { get width and adjust end coordinate accordingly }
  1170. y1 := ptw(Bitmap)[1]+y-1; { get height and adjust end coordinate accordingly }
  1171. deltaX := 0;
  1172. deltaX1 := 0;
  1173. k := 3 * sizeOf(Longint) div sizeOf(Word); { Three reserved longs at start of bitmap }
  1174. { check which part of the image is in the viewport }
  1175. if clipPixels then
  1176. begin
  1177. if y < startYViewPort then
  1178. begin
  1179. deltaY := startYViewPort - y;
  1180. inc(k,(x1-x+1)*deltaY);
  1181. y := startYViewPort;
  1182. end;
  1183. if y1 > startYViewPort+viewHeight then
  1184. y1 := startYViewPort+viewHeight;
  1185. if x < startXViewPort then
  1186. begin
  1187. deltaX := startXViewPort-x;
  1188. x := startXViewPort;
  1189. end;
  1190. if x1 > startXViewPort + viewWidth then
  1191. begin
  1192. deltaX1 := x1 - (startXViewPort + viewWidth);
  1193. x1 := startXViewPort + viewWidth;
  1194. end;
  1195. end;
  1196. {$ifdef logging}
  1197. LogLn('deltax: '+strf(deltax)+', deltax1: '+strf(deltax1)+',deltay: '+strf(deltay));
  1198. {$endif logging}
  1199. oldCurrentColor := currentColor;
  1200. oldCurrentWriteMode := currentWriteMode;
  1201. currentWriteMode := bitBlt;
  1202. for j:=Y to Y1 do
  1203. Begin
  1204. inc(k,deltaX);
  1205. for i:=X to X1 do
  1206. begin
  1207. currentColor := pt(bitmap)[k];
  1208. directPutPixel(i,j);
  1209. inc(k);
  1210. end;
  1211. inc(k,deltaX1);
  1212. end;
  1213. currentWriteMode := oldCurrentWriteMode;
  1214. currentColor := oldCurrentColor;
  1215. end;
  1216. Procedure DefaultGetImage(X1,Y1,X2,Y2: smallint; Var Bitmap); {$ifndef fpc}far;{$endif fpc}
  1217. type
  1218. pt = array[0..$fffffff] of word;
  1219. ptw = array[0..2] of longint;
  1220. var
  1221. i,j: smallint;
  1222. k: longint;
  1223. Begin
  1224. k:= 3 * Sizeof(longint) div sizeof(word); { Three reserved longs at start of bitmap }
  1225. i := x2 - x1 + 1;
  1226. for j:=Y1 to Y2 do
  1227. Begin
  1228. GetScanLine(x1,x2,j,pt(Bitmap)[k]);
  1229. inc(k,i);
  1230. end;
  1231. ptw(Bitmap)[0] := X2-X1+1; { First longint is width }
  1232. ptw(Bitmap)[1] := Y2-Y1+1; { Second longint is height }
  1233. ptw(bitmap)[2] := 0; { Third longint is reserved}
  1234. end;
  1235. Procedure GetArcCoords(var ArcCoords: ArcCoordsType);
  1236. Begin
  1237. ArcCoords.X := ArcCall.X;
  1238. ArcCoords.Y := ArcCall.Y;
  1239. ArcCoords.XStart := ArcCall.XStart;
  1240. ArcCoords.YStart := ArcCall.YStart;
  1241. ArcCoords.XEnd := ArcCall.XEnd;
  1242. ArcCoords.YEnd := ArcCall.YEnd;
  1243. end;
  1244. procedure SetVisualPageDefault(page : word); {$ifndef fpc}far;{$endif fpc}
  1245. begin
  1246. end;
  1247. procedure SetActivePageDefault(page : word); {$ifndef fpc}far;{$endif fpc}
  1248. begin
  1249. end;
  1250. procedure DirectPutPixelDefault(X,Y: smallint);
  1251. begin
  1252. Writeln(stderr,'Error: Not in graphics mode (use InitGraph and test GraphResult afterwards)');
  1253. Halt(1);
  1254. end;
  1255. function GetPixelDefault(X,Y: smallint): word;
  1256. begin
  1257. Writeln(stderr,'Error: Not in graphics mode (use InitGraph and test GraphResult afterwards)');
  1258. Halt(1);
  1259. exit(0); { avoid warning }
  1260. end;
  1261. procedure PutPixelDefault(X,Y: smallint; Color: Word);
  1262. begin
  1263. Writeln(stderr,'Error: Not in graphics mode (use InitGraph and test GraphResult afterwards)');
  1264. Halt(1);
  1265. end;
  1266. procedure SetRGBPaletteDefault(ColorNum, RedValue, GreenValue, BlueValue: smallint);
  1267. begin
  1268. Writeln(stderr,'Error: Not in graphics mode (use InitGraph and test GraphResult afterwards)');
  1269. Halt(1);
  1270. end;
  1271. procedure GetRGBPaletteDefault(ColorNum: smallint; var
  1272. RedValue, GreenValue, BlueValue: smallint);
  1273. begin
  1274. Writeln(stderr,'Error: Not in graphics mode (use InitGraph and test GraphResult afterwards)');
  1275. Halt(1);
  1276. end;
  1277. procedure OutTextXYDefault(x,y : smallint;const TextString : string);forward;
  1278. procedure CircleDefault(X, Y: smallint; Radius:Word);forward;
  1279. {$i palette.inc}
  1280. Procedure DefaultHooks;
  1281. {********************************************************}
  1282. { Procedure DefaultHooks() }
  1283. {--------------------------------------------------------}
  1284. { Resets all hookable routine either to nil for those }
  1285. { which need overrides, and others to defaults. }
  1286. { This is called each time SetGraphMode() is called. }
  1287. {********************************************************}
  1288. Begin
  1289. { All default hooks procedures }
  1290. { required...}
  1291. DirectPutPixel := {$ifdef fpc}@{$endif}DirectPutPixelDefault;
  1292. PutPixel := {$ifdef fpc}@{$endif}PutPixelDefault;
  1293. GetPixel := {$ifdef fpc}@{$endif}GetPixelDefault;
  1294. SetRGBPalette := {$ifdef fpc}@{$endif}SetRGBPaletteDefault;
  1295. GetRGBPalette := {$ifdef fpc}@{$endif}GetRGBPaletteDefault;
  1296. { optional...}
  1297. SetAllPalette := {$ifdef fpc}@{$endif}SetAllPaletteDefault;
  1298. SetActivePage := {$ifdef fpc}@{$endif}SetActivePageDefault;
  1299. SetVisualPage := {$ifdef fpc}@{$endif}SetVisualPageDefault;
  1300. ClearViewPort := {$ifdef fpc}@{$endif}ClearViewportDefault;
  1301. PutImage := {$ifdef fpc}@{$endif}DefaultPutImage;
  1302. GetImage := {$ifdef fpc}@{$endif}DefaultGetImage;
  1303. ImageSize := {$ifdef fpc}@{$endif}DefaultImageSize;
  1304. GraphFreeMemPtr := nil;
  1305. GraphGetMemPtr := nil;
  1306. GetScanLine := {$ifdef fpc}@{$endif}GetScanLineDefault;
  1307. Line := {$ifdef fpc}@{$endif}LineDefault;
  1308. InternalEllipse := {$ifdef fpc}@{$endif}InternalEllipseDefault;
  1309. PatternLine := {$ifdef fpc}@{$endif}PatternLineDefault;
  1310. HLine := {$ifdef fpc}@{$endif}HLineDefault;
  1311. VLine := {$ifdef fpc}@{$endif}VLineDefault;
  1312. OuttextXY := {$ifdef fpc}@{$endif}OuttextXYDefault;
  1313. Circle := {$ifdef fpc}@{$endif}CircleDefault;
  1314. end;
  1315. Procedure InitVars;
  1316. {********************************************************}
  1317. { Procedure InitVars() }
  1318. {--------------------------------------------------------}
  1319. { Resets all internal variables, and resets all }
  1320. { overridable routines. }
  1321. {********************************************************}
  1322. Begin
  1323. DirectVideo := TRUE; { By default use fastest access possible }
  1324. ArcCall.X := 0;
  1325. ArcCall.Y := 0;
  1326. ArcCall.XStart := 0;
  1327. ArcCall.YStart := 0;
  1328. ArcCall.XEnd := 0;
  1329. ArcCall.YEnd := 0;
  1330. { Reset to default values }
  1331. IntCurrentMode := 0;
  1332. IntCurrentDriver := 0;
  1333. IntCurrentNewDriver := 0;
  1334. XAspect := 0;
  1335. YAspect := 0;
  1336. MaxX := 0;
  1337. MaxY := 0;
  1338. MaxColor := 0;
  1339. PaletteSize := 0;
  1340. DirectColor := FALSE;
  1341. HardwarePages := 0;
  1342. if hardwarepages=0 then; { remove note }
  1343. DefaultHooks;
  1344. end;
  1345. {$i modes.inc}
  1346. function InstallUserDriver(Name: string; AutoDetectPtr: Pointer): smallint;
  1347. begin
  1348. _graphResult := grError;
  1349. InstallUserDriver:=grError;
  1350. end;
  1351. function RegisterBGIDriver(driver: pointer): smallint;
  1352. begin
  1353. _graphResult := grError;
  1354. RegisterBGIDriver:=grError;
  1355. end;
  1356. { ----------------------------------------------------------------- }
  1357. Procedure Arc(X,Y : smallint; StAngle,EndAngle,Radius: word);
  1358. { var
  1359. OldWriteMode: word;}
  1360. Begin
  1361. { Only if we are using thickwidths lines do we accept }
  1362. { XORput write modes. }
  1363. { OldWriteMode := CurrentWriteMode;
  1364. if (LineInfo.Thickness = NormWidth) then
  1365. CurrentWriteMode := NormalPut;}
  1366. InternalEllipse(X,Y,Radius,Radius,StAngle,Endangle,{$ifdef fpc}@{$endif}DummyPatternLine);
  1367. { CurrentWriteMode := OldWriteMode;}
  1368. end;
  1369. procedure Ellipse(X,Y : smallint; stAngle, EndAngle: word; XRadius,YRadius: word);
  1370. Begin
  1371. InternalEllipse(X,Y,XRadius,YRadius,StAngle,Endangle,{$ifdef fpc}@{$endif}DummyPatternLine);
  1372. end;
  1373. procedure FillEllipse(X, Y: smallint; XRadius, YRadius: Word);
  1374. {********************************************************}
  1375. { Procedure FillEllipse() }
  1376. {--------------------------------------------------------}
  1377. { Draws a filled ellipse using (X,Y) as a center point }
  1378. { and XRadius and YRadius as the horizontal and vertical }
  1379. { axes. The ellipse is filled with the current fill color}
  1380. { and fill style, and is bordered with the current color.}
  1381. {********************************************************}
  1382. begin
  1383. InternalEllipse(X,Y,XRadius,YRadius,0,360,PatternLine)
  1384. end;
  1385. procedure CircleDefault(X, Y: smallint; Radius:Word);
  1386. {********************************************************}
  1387. { Draws a circle centered at X,Y with the given Radius. }
  1388. {********************************************************}
  1389. { Important notes: }
  1390. { - Thickwidth circles use the current write mode, while}
  1391. { normal width circles ALWAYS use CopyPut/NormalPut }
  1392. { mode. (Tested against VGA BGI driver -CEC 13/Aug/99 }
  1393. {********************************************************}
  1394. var OriginalArcInfo: ArcCoordsType;
  1395. OldWriteMode: word;
  1396. begin
  1397. if (Radius = 0) then
  1398. Exit;
  1399. if (Radius = 1) then
  1400. begin
  1401. { only normal put mode is supported by a call to PutPixel }
  1402. PutPixel(X, Y, CurrentColor);
  1403. Exit;
  1404. end;
  1405. { save state of arc information }
  1406. { because it is not needed for }
  1407. { a circle call. }
  1408. move(ArcCall,OriginalArcInfo, sizeof(ArcCall));
  1409. if LineInfo.Thickness = Normwidth then
  1410. begin
  1411. OldWriteMode := CurrentWriteMode;
  1412. CurrentWriteMode := CopyPut;
  1413. end;
  1414. InternalEllipse(X,Y,Radius,Radius,0,360,{$ifdef fpc}@{$endif}DummyPatternLine);
  1415. if LineInfo.Thickness = Normwidth then
  1416. CurrentWriteMode := OldWriteMode;
  1417. { restore arc information }
  1418. move(OriginalArcInfo, ArcCall,sizeof(ArcCall));
  1419. end;
  1420. procedure SectorPL(x1,x2,y: smallint); {$ifndef fpc}far;{$endif fpc}
  1421. var plx1, plx2: smallint;
  1422. {$ifdef sectorpldebug}
  1423. t : text;
  1424. {$endif sectorpldebug}
  1425. begin
  1426. {$ifdef sectorpldebug}
  1427. assign(t,'sector.log');
  1428. append(t);
  1429. writeln(t,'Got here for line ',y);
  1430. close(t);
  1431. {$endif sectorpldebug}
  1432. If (x1 = -maxsmallint) Then
  1433. If (x2 = maxsmallint-1) Then
  1434. { no ellipse points drawn on this line }
  1435. If (((Y < ArcCall.Y) and (Y > ArcCall.YStart)) or
  1436. ((Y > ArcCall.Y) and (Y < ArcCall.YStart))) Then
  1437. { there is a part of the sector at this y coordinate, but no }
  1438. { ellips points are plotted on this line, so draw a patternline }
  1439. { between the lines connecting (arccall.x,arccall.y) with }
  1440. { the start and the end of the arc (JM) }
  1441. { use: y-y1=(y2-y1)/(x2-x1)*(x-x1) => }
  1442. { x = (y-y1)/(y2-y1)*(x2-x1)+x1 }
  1443. Begin
  1444. {$ifdef sectorpldebug}
  1445. If (ArcCall.YStart-ArcCall.Y) = 0 then
  1446. begin
  1447. append(t);
  1448. writeln(t,'bug1');
  1449. close(t);
  1450. runerror(202);
  1451. end;
  1452. {$endif sectorpldebug}
  1453. plx1 := (y-ArcCall.Y)*(ArcCall.XStart-ArcCall.X)
  1454. div (ArcCall.YStart-ArcCall.Y)+ArcCall.X;
  1455. {$ifdef sectorpldebug}
  1456. If (ArcCall.YEnd-ArcCall.Y) = 0 then
  1457. begin
  1458. append(t);
  1459. writeln(t,'bug2');
  1460. close(t);
  1461. runerror(202);
  1462. end;
  1463. {$endif sectorpldebug}
  1464. plx2 := (y-ArcCall.Y)*(ArcCall.XEnd-ArcCall.X)
  1465. div (ArcCall.YEnd-ArcCall.Y)+ArcCall.X;
  1466. If plx1 > plx2 then
  1467. begin
  1468. plx1 := plx1 xor plx2;
  1469. plx2 := plx1 xor plx2;
  1470. plx1 := plx1 xor plx2;
  1471. end;
  1472. {$ifdef sectorpldebug}
  1473. append(t);
  1474. writeln(t,'lines: ',plx1,' - ',plx2);
  1475. close(t);
  1476. {$endif sectorpldebug}
  1477. End
  1478. { otherwise two points which have nothing to do with the sector }
  1479. Else exit
  1480. Else
  1481. { the arc is plotted at the right side, but not at the left side, }
  1482. { fill till the line between (ArcCall.X,ArcCall.Y) and }
  1483. { (ArcCall.XStart,ArcCall.YStart) }
  1484. Begin
  1485. If (y < ArcCall.Y) then
  1486. begin
  1487. {$ifdef sectorpldebug}
  1488. If (ArcCall.YEnd-ArcCall.Y) = 0 then
  1489. begin
  1490. append(t);
  1491. writeln(t,'bug3');
  1492. close(t);
  1493. runerror(202);
  1494. end;
  1495. {$endif sectorpldebug}
  1496. plx1 := (y-ArcCall.Y)*(ArcCall.XEnd-ArcCall.X)
  1497. div (ArcCall.YEnd-ArcCall.Y)+ArcCall.X
  1498. end
  1499. else if (y > ArcCall.Y) then
  1500. begin
  1501. {$ifdef sectorpldebug}
  1502. If (ArcCall.YStart-ArcCall.Y) = 0 then
  1503. begin
  1504. append(t);
  1505. writeln(t,'bug4');
  1506. close(t);
  1507. runerror(202);
  1508. end;
  1509. {$endif sectorpldebug}
  1510. plx1 := (y-ArcCall.Y)*(ArcCall.XStart-ArcCall.X)
  1511. div (ArcCall.YStart-ArcCall.Y)+ArcCall.X
  1512. end
  1513. else plx1 := ArcCall.X;
  1514. plx2 := x2;
  1515. {$ifdef sectorpldebug}
  1516. append(t);
  1517. writeln(t,'right: ',plx1,' - ',plx2);
  1518. close(t);
  1519. {$endif sectorpldebug}
  1520. End
  1521. Else
  1522. If (x2 = maxsmallint-1) Then
  1523. { the arc is plotted at the left side, but not at the rigth side. }
  1524. { the right limit can be either the first or second line. Just take }
  1525. { the closest one, but watch out for division by zero! }
  1526. Begin
  1527. If (y < ArcCall.Y) then
  1528. begin
  1529. {$ifdef sectorpldebug}
  1530. If (ArcCall.YStart-ArcCall.Y) = 0 then
  1531. begin
  1532. append(t);
  1533. writeln(t,'bug5');
  1534. close(t);
  1535. runerror(202);
  1536. end;
  1537. {$endif sectorpldebug}
  1538. plx2 := (y-ArcCall.Y)*(ArcCall.XStart-ArcCall.X)
  1539. div (ArcCall.YStart-ArcCall.Y)+ArcCall.X
  1540. end
  1541. else if (y > ArcCall.Y) then
  1542. begin
  1543. {$ifdef sectorpldebug}
  1544. If (ArcCall.YEnd-ArcCall.Y) = 0 then
  1545. begin
  1546. append(t);
  1547. writeln(t,'bug6');
  1548. close(t);
  1549. runerror(202);
  1550. end;
  1551. {$endif sectorpldebug}
  1552. plx2 := (y-ArcCall.Y)*(ArcCall.XEnd-ArcCall.X)
  1553. div (ArcCall.YEnd-ArcCall.Y)+ArcCall.X
  1554. end
  1555. else plx2 := ArcCall.X;
  1556. plx1 := x1;
  1557. {$ifdef sectorpldebug}
  1558. append(t);
  1559. writeln(t,'left: ',plx1,' - ',plx2);
  1560. close(t);
  1561. {$endif sectorpldebug}
  1562. End
  1563. Else
  1564. { the arc is plotted at both sides }
  1565. Begin
  1566. plx1 := x1;
  1567. plx2 := x2;
  1568. {$ifdef sectorpldebug}
  1569. append(t);
  1570. writeln(t,'normal: ',plx1,' - ',plx2);
  1571. close(t);
  1572. {$endif sectorpldebug}
  1573. End;
  1574. If plx2 > plx1 then
  1575. Begin
  1576. {$ifdef sectorpldebug}
  1577. append(t);
  1578. Writeln(t,'drawing...');
  1579. close(t);
  1580. {$endif sectorpldebug}
  1581. PatternLine(plx1,plx2,y);
  1582. end;
  1583. end;
  1584. procedure Sector(x, y: smallint; StAngle,EndAngle, XRadius, YRadius: Word);
  1585. begin
  1586. internalellipse(x,y,XRadius, YRadius, StAngle, EndAngle, {$ifdef fpc}@{$endif}SectorPL);
  1587. Line(ArcCall.XStart, ArcCall.YStart, x,y);
  1588. Line(x,y,ArcCall.Xend,ArcCall.YEnd);
  1589. end;
  1590. procedure SetFillStyle(Pattern : word; Color: word);
  1591. begin
  1592. { on invalid input, the current fill setting will be }
  1593. { unchanged. }
  1594. if (Pattern > UserFill) or (Color > GetMaxColor) then
  1595. begin
  1596. {$ifdef logging}
  1597. logln('invalid fillstyle parameters');
  1598. {$endif logging}
  1599. _GraphResult := grError;
  1600. exit;
  1601. end;
  1602. FillSettings.Color := Color;
  1603. FillSettings.Pattern := Pattern;
  1604. end;
  1605. procedure SetFillPattern(Pattern: FillPatternType; Color: word);
  1606. {********************************************************}
  1607. { Changes the Current FillPattern to a user defined }
  1608. { pattern and changes also the current fill color. }
  1609. { The FillPattern is saved in the FillPattern array so }
  1610. { it can still be used with SetFillStyle(UserFill,Color) }
  1611. {********************************************************}
  1612. var
  1613. i: smallint;
  1614. begin
  1615. if Color > GetMaxColor then
  1616. begin
  1617. {$ifdef logging}
  1618. logln('invalid fillpattern parameters');
  1619. {$endif logging}
  1620. _GraphResult := grError;
  1621. exit;
  1622. end;
  1623. FillSettings.Color := Color;
  1624. FillSettings.Pattern := UserFill;
  1625. { Save the pattern in the buffer }
  1626. For i:=1 to 8 do
  1627. FillPatternTable[UserFill][i] := Pattern[i];
  1628. end;
  1629. procedure Bar(x1,y1,x2,y2:smallint);
  1630. {********************************************************}
  1631. { Important notes for compatibility with BP: }
  1632. { - WriteMode is always CopyPut }
  1633. { - No contour is drawn for the lines }
  1634. {********************************************************}
  1635. var y : smallint;
  1636. origcolor : longint;
  1637. origlinesettings: Linesettingstype;
  1638. origwritemode : smallint;
  1639. begin
  1640. origlinesettings:=lineinfo;
  1641. origcolor:=CurrentColor;
  1642. if y1>y2 then
  1643. begin
  1644. y:=y1;
  1645. y1:=y2;
  1646. y2:=y;
  1647. end;
  1648. { Always copy mode for Bars }
  1649. origwritemode := CurrentWriteMode;
  1650. CurrentWriteMode := CopyPut;
  1651. { All lines used are of this style }
  1652. Lineinfo.linestyle:=solidln;
  1653. Lineinfo.thickness:=normwidth;
  1654. case Fillsettings.pattern of
  1655. EmptyFill :
  1656. begin
  1657. Currentcolor:=CurrentBkColor;
  1658. for y:=y1 to y2 do
  1659. Hline(x1,x2,y);
  1660. end;
  1661. SolidFill :
  1662. begin
  1663. CurrentColor:=FillSettings.color;
  1664. for y:=y1 to y2 do
  1665. Hline(x1,x2,y);
  1666. end;
  1667. else
  1668. Begin
  1669. CurrentColor:=FillSettings.color;
  1670. for y:=y1 to y2 do
  1671. patternline(x1,x2,y);
  1672. end;
  1673. end;
  1674. CurrentColor:= Origcolor;
  1675. LineInfo := OrigLineSettings;
  1676. CurrentWriteMode := OrigWritemode;
  1677. end;
  1678. procedure bar3D(x1, y1, x2, y2 : smallint;depth : word;top : boolean);
  1679. var
  1680. origwritemode : smallint;
  1681. OldX, OldY : smallint;
  1682. begin
  1683. origwritemode := CurrentWriteMode;
  1684. CurrentWriteMode := CopyPut;
  1685. Bar(x1,y1,x2,y2);
  1686. Rectangle(x1,y1,x2,y2);
  1687. { Current CP should not be updated in Bar3D }
  1688. { therefore save it and then restore it on }
  1689. { exit. }
  1690. OldX := CurrentX;
  1691. OldY := CurrentY;
  1692. if top then begin
  1693. Moveto(x1,y1);
  1694. Lineto(x1+depth,y1-depth);
  1695. Lineto(x2+depth,y1-depth);
  1696. Lineto(x2,y1);
  1697. end;
  1698. if Depth <> 0 then
  1699. Begin
  1700. Moveto(x2+depth,y1-depth);
  1701. Lineto(x2+depth,y2-depth);
  1702. Lineto(x2,y2);
  1703. end;
  1704. { restore CP }
  1705. CurrentX := OldX;
  1706. CurrentY := OldY;
  1707. CurrentWriteMode := origwritemode;
  1708. end;
  1709. {--------------------------------------------------------------------------}
  1710. { }
  1711. { COLOR AND PALETTE ROUTINES }
  1712. { }
  1713. {--------------------------------------------------------------------------}
  1714. procedure SetColor(Color: Word);
  1715. Begin
  1716. CurrentColor := Color;
  1717. end;
  1718. function GetColor: Word;
  1719. Begin
  1720. GetColor := CurrentColor;
  1721. end;
  1722. function GetBkColor: Word;
  1723. Begin
  1724. GetBkColor := CurrentBkColor;
  1725. end;
  1726. procedure SetBkColor(ColorNum: Word);
  1727. { Background color means background screen color in this case, and it is }
  1728. { INDEPENDANT of the viewport settings, so we must clear the whole screen }
  1729. { with the color. }
  1730. var
  1731. ViewPort: ViewportType;
  1732. Begin
  1733. GetViewSettings(Viewport);
  1734. {$ifdef logging}
  1735. logln('calling setviewport from setbkcolor');
  1736. {$endif logging}
  1737. SetViewPort(0,0,MaxX,MaxY,FALSE);
  1738. {$ifdef logging}
  1739. logln('calling setviewport from setbkcolor done');
  1740. {$endif logging}
  1741. CurrentBkColor := ColorNum;
  1742. {ClearViewPort;}
  1743. if not DirectColor and (ColorNum<256) then
  1744. SetRGBPalette(0,
  1745. DefaultColors[ColorNum].Red,
  1746. DefaultColors[ColorNum].Green,
  1747. DefaultColors[ColorNum].Blue);
  1748. SetViewport(ViewPort.X1,Viewport.Y1,Viewport.X2,Viewport.Y2,Viewport.Clip);
  1749. end;
  1750. function GetMaxColor: word;
  1751. { Checked against TP VGA driver - CEC }
  1752. begin
  1753. GetMaxColor:=MaxColor-1; { based on an index of zero so subtract one }
  1754. end;
  1755. Procedure MoveRel(Dx, Dy: smallint);
  1756. Begin
  1757. CurrentX := CurrentX + Dx;
  1758. CurrentY := CurrentY + Dy;
  1759. end;
  1760. Procedure MoveTo(X,Y: smallint);
  1761. {********************************************************}
  1762. { Procedure MoveTo() }
  1763. {--------------------------------------------------------}
  1764. { Moves the current pointer in VIEWPORT relative }
  1765. { coordinates to the specified X,Y coordinate. }
  1766. {********************************************************}
  1767. Begin
  1768. CurrentX := X;
  1769. CurrentY := Y;
  1770. end;
  1771. function GraphErrorMsg(ErrorCode: smallint): string;
  1772. Begin
  1773. GraphErrorMsg:='';
  1774. case ErrorCode of
  1775. grOk,grFileNotFound,grInvalidDriver: exit;
  1776. grNoInitGraph: GraphErrorMsg:='Graphics driver not installed';
  1777. grNotDetected: GraphErrorMsg:='Graphics hardware not detected';
  1778. grNoLoadMem,grNoScanMem,grNoFloodMem: GraphErrorMsg := 'Not enough memory for graphics';
  1779. grNoFontMem: GraphErrorMsg := 'Not enough memory to load font';
  1780. grFontNotFound: GraphErrorMsg:= 'Font file not found';
  1781. grInvalidMode: GraphErrorMsg := 'Invalid graphics mode';
  1782. grError: GraphErrorMsg:='Graphics error';
  1783. grIoError: GraphErrorMsg:='Graphics I/O error';
  1784. grInvalidFont,grInvalidFontNum: GraphErrorMsg := 'Invalid font';
  1785. grInvalidVersion: GraphErrorMsg:='Invalid driver version';
  1786. end;
  1787. end;
  1788. Function GetMaxX: smallint;
  1789. { Routine checked against VGA driver - CEC }
  1790. Begin
  1791. GetMaxX := MaxX;
  1792. end;
  1793. Function GetMaxY: smallint;
  1794. { Routine checked against VGA driver - CEC }
  1795. Begin
  1796. GetMaxY := MaxY;
  1797. end;
  1798. Function GraphResult: smallint;
  1799. Begin
  1800. GraphResult := _GraphResult;
  1801. _GraphResult := grOk;
  1802. end;
  1803. Function GetX: smallint;
  1804. Begin
  1805. GetX := CurrentX;
  1806. end;
  1807. Function GetY: smallint;
  1808. Begin
  1809. GetY := CurrentY;
  1810. end;
  1811. Function GetDriverName: string;
  1812. begin
  1813. GetDriverName:=DriverName;
  1814. end;
  1815. procedure graphdefaults;
  1816. { PS: GraphDefaults does not ZERO the ArcCall structure }
  1817. { so a call to GetArcCoords will not change even the }
  1818. { returned values even if GraphDefaults is called in }
  1819. { between. }
  1820. var
  1821. i: smallint;
  1822. begin
  1823. lineinfo.linestyle:=solidln;
  1824. lineinfo.thickness:=normwidth;
  1825. { reset line style pattern }
  1826. for i:=0 to 15 do
  1827. LinePatterns[i] := TRUE;
  1828. { By default, according to the TP prog's reference }
  1829. { the default pattern is solid, and the default }
  1830. { color is the maximum color in the palette. }
  1831. fillsettings.color:=GetMaxColor;
  1832. fillsettings.pattern:=solidfill;
  1833. { GraphDefaults resets the User Fill pattern to $ff }
  1834. { checked with VGA BGI driver - CEC }
  1835. for i:=1 to 8 do
  1836. FillPatternTable[UserFill][i] := $ff;
  1837. CurrentColor:=white;
  1838. ClipPixels := TRUE;
  1839. { Reset the viewport }
  1840. StartXViewPort := 0;
  1841. StartYViewPort := 0;
  1842. ViewWidth := MaxX;
  1843. ViewHeight := MaxY;
  1844. { Reset CP }
  1845. CurrentX := 0;
  1846. CurrentY := 0;
  1847. SetBkColor(Black);
  1848. { normal write mode }
  1849. CurrentWriteMode := CopyPut;
  1850. { Schriftart einstellen }
  1851. CurrentTextInfo.font := DefaultFont;
  1852. CurrentTextInfo.direction:=HorizDir;
  1853. CurrentTextInfo.charsize:=1;
  1854. CurrentTextInfo.horiz:=LeftText;
  1855. CurrentTextInfo.vert:=TopText;
  1856. XAspect:=10000; YAspect:=10000;
  1857. end;
  1858. procedure GetAspectRatio(var Xasp,Yasp : word);
  1859. begin
  1860. XAsp:=XAspect;
  1861. YAsp:=YAspect;
  1862. end;
  1863. procedure SetAspectRatio(Xasp, Yasp : word);
  1864. begin
  1865. Xaspect:= XAsp;
  1866. YAspect:= YAsp;
  1867. end;
  1868. procedure SetWriteMode(WriteMode : smallint);
  1869. { TP sets the writemodes according to the following scheme (JM) }
  1870. begin
  1871. Case writemode of
  1872. xorput, andput: CurrentWriteMode := XorPut;
  1873. notput, orput, copyput: CurrentWriteMode := CopyPut;
  1874. End;
  1875. end;
  1876. procedure GetFillSettings(var Fillinfo:Fillsettingstype);
  1877. begin
  1878. Fillinfo:=Fillsettings;
  1879. end;
  1880. procedure GetFillPattern(var FillPattern:FillPatternType);
  1881. begin
  1882. FillPattern:=FillpatternTable[UserFill];
  1883. end;
  1884. procedure DrawPoly(numpoints : word;var polypoints);
  1885. type
  1886. ppointtype = ^pointtype;
  1887. pt = array[0..16000] of pointtype;
  1888. var
  1889. i : longint;
  1890. begin
  1891. if numpoints < 2 then
  1892. begin
  1893. _GraphResult := grError;
  1894. exit;
  1895. end;
  1896. for i:=0 to numpoints-2 do
  1897. line(pt(polypoints)[i].x,
  1898. pt(polypoints)[i].y,
  1899. pt(polypoints)[i+1].x,
  1900. pt(polypoints)[i+1].y);
  1901. end;
  1902. procedure PieSlice(X,Y,stangle,endAngle:smallint;Radius: Word);
  1903. begin
  1904. Sector(x,y,stangle,endangle,radius,radius);
  1905. end;
  1906. {$i fills.inc}
  1907. {$i gtext.inc}
  1908. procedure internDetectGraph(var GraphDriver, GraphMode:smallint;
  1909. calledFromInitGraph: boolean);
  1910. var LoMode, HiMode: smallint;
  1911. CpyMode: smallint;
  1912. CpyDriver: smallint;
  1913. begin
  1914. HiMode := -1;
  1915. LoMode := -1;
  1916. {$ifndef nonewmodes}
  1917. if not calledFromInitGraph or
  1918. (graphDriver < lowNewDriver) or
  1919. (graphDriver > highNewDriver) then
  1920. begin
  1921. { Search lowest supported bitDepth }
  1922. graphdriver := D1bit;
  1923. while (graphDriver <= highNewDriver) and
  1924. (hiMode = -1) do
  1925. begin
  1926. getModeRange(graphDriver,loMode,hiMode);
  1927. inc(graphDriver);
  1928. end;
  1929. dec(graphdriver);
  1930. if hiMode = -1 then
  1931. begin
  1932. _GraphResult := grNotDetected;
  1933. exit;
  1934. end;
  1935. CpyMode := 0;
  1936. repeat
  1937. GetModeRange(GraphDriver,LoMode,HiMode);
  1938. { save the highest mode possible...}
  1939. {$ifdef logging}
  1940. logln('Found driver '+strf(graphdriver)+' with modes '+
  1941. strf(lomode)+' - '+strf(himode));
  1942. {$endif logging}
  1943. if HiMode <> -1 then
  1944. begin
  1945. CpyMode:=HiMode;
  1946. CpyDriver:=GraphDriver;
  1947. end;
  1948. { go to next driver if it exists...}
  1949. Inc(graphDriver);
  1950. until (graphDriver > highNewDriver);
  1951. end
  1952. else
  1953. begin
  1954. cpyMode := 0;
  1955. getModeRange(graphDriver,loMode,hiMode);
  1956. if hiMode <> -1 then
  1957. begin
  1958. cpyDriver := graphDriver;
  1959. cpyMode := hiMode;
  1960. end;
  1961. end;
  1962. if cpyMode = 0 then
  1963. begin
  1964. _GraphResult := grNotDetected;
  1965. exit;
  1966. end;
  1967. {$else nonewmodes}
  1968. { We start at VGA }
  1969. GraphDriver := VGA;
  1970. CpyMode := 0;
  1971. { search all possible graphic drivers in ascending order...}
  1972. { usually the new driver numbers indicate newest hardware...}
  1973. { Internal driver numbers start at VGA=9 }
  1974. repeat
  1975. GetModeRange(GraphDriver,LoMode,HiMode);
  1976. { save the highest mode possible...}
  1977. {$ifdef logging}
  1978. logln('Found driver '+strf(graphdriver)+' with modes '+
  1979. strf(lomode)+' - '+strf(himode));
  1980. {$endif logging}
  1981. if HiMode = -1 then break;
  1982. CpyMode:=HiMode;
  1983. CpyDriver:=GraphDriver;
  1984. { go to next driver if it exists...}
  1985. Inc(GraphDriver);
  1986. until (CpyMode=-1);
  1987. { If this is equal to -1 then no graph mode possible...}
  1988. if CpyMode = -1 then
  1989. begin
  1990. _GraphResult := grNotDetected;
  1991. exit;
  1992. end;
  1993. {$endif nonewmodes}
  1994. _GraphResult := grOK;
  1995. GraphDriver := CpyDriver;
  1996. GraphMode := CpyMode;
  1997. end;
  1998. procedure detectGraph(var GraphDriver: smallint; var GraphMode:smallint);
  1999. begin
  2000. internDetectGraph(graphDriver,graphMode,false);
  2001. end;
  2002. procedure InitGraph(var GraphDriver:smallint;var GraphMode:smallint;
  2003. const PathToDriver:String);
  2004. const
  2005. {$IFDEF Linux}
  2006. dirchar = '/';
  2007. {$ELSE}
  2008. dirchar = '\';
  2009. {$ENDIF}
  2010. begin
  2011. InitVars;
  2012. { path to the fonts (where they will be searched)...}
  2013. bgipath:=PathToDriver;
  2014. if (Length(bgipath) > 0) and (bgipath[length(bgipath)]<>dirchar) then
  2015. bgipath:=bgipath+dirchar;
  2016. if not assigned(SaveVideoState) then
  2017. RunError(216);
  2018. DriverName:=InternalDriverName; { DOS Graphics driver }
  2019. if (Graphdriver=Detect)
  2020. {$ifndef nonewmodes}
  2021. or (GraphMode = detectMode)
  2022. {$endif}
  2023. then
  2024. begin
  2025. internDetectGraph(GraphDriver,GraphMode,true);
  2026. If _GraphResult = grNotDetected then Exit;
  2027. { _GraphResult is now already set to grOK by DetectGraph }
  2028. IntCurrentDriver := GraphDriver;
  2029. if (graphDriver >= lowNewDriver) and
  2030. (graphDriver <= highNewDriver) then
  2031. IntCurrentNewDriver := GraphDriver
  2032. else IntCurrentNewDriver := -1;
  2033. { Actually set the graph mode...}
  2034. if firstCallOfInitgraph then
  2035. begin
  2036. SaveVideoState;
  2037. firstCallOfInitgraph := false;
  2038. end;
  2039. SetGraphMode(GraphMode);
  2040. end
  2041. else
  2042. begin
  2043. { Search if that graphics modec actually exists...}
  2044. if SearchMode(GraphDriver,GraphMode) = nil then
  2045. begin
  2046. _GraphResult := grInvalidMode;
  2047. exit;
  2048. end
  2049. else
  2050. begin
  2051. _GraphResult := grOK;
  2052. IntCurrentDriver := GraphDriver;
  2053. if (graphDriver >= lowNewDriver) and
  2054. (graphDriver <= highNewDriver) then
  2055. IntCurrentNewDriver := GraphDriver
  2056. else IntCurrentNewDriver := -1;
  2057. if firstCallOfInitgraph then
  2058. begin
  2059. SaveVideoState;
  2060. firstCallOfInitgraph := false;
  2061. end;
  2062. SetGraphMode(GraphMode);
  2063. end;
  2064. end;
  2065. end;
  2066. procedure SetDirectVideo(DirectAccess: boolean);
  2067. begin
  2068. DirectVideo := DirectAccess;
  2069. end;
  2070. function GetDirectVideo: boolean;
  2071. begin
  2072. GetDirectVideo := DirectVideo;
  2073. end;
  2074. procedure GraphExitProc; {$ifndef fpc} far; {$endif fpc}
  2075. { deallocates all memory allocated by the graph unit }
  2076. var
  2077. list: PModeInfo;
  2078. tmp : PModeInfo;
  2079. c: graph_int;
  2080. begin
  2081. { restore old exitproc! }
  2082. exitproc := exitsave;
  2083. if IsGraphMode and ((errorcode<>0) or (erroraddr<>nil)) then
  2084. CloseGraph;
  2085. { release memory allocated for fonts }
  2086. for c := 1 to installedfonts do
  2087. with fonts[c] Do
  2088. If assigned(instr) Then
  2089. Freemem(instr,instrlength);
  2090. { release memory allocated for modelist }
  2091. list := ModeList;
  2092. while assigned(list) do
  2093. begin
  2094. tmp := list;
  2095. list:=list^.next;
  2096. dispose(tmp);
  2097. end;
  2098. {$ifndef nonewmodes}
  2099. for c := lowNewDriver to highNewDriver do
  2100. begin
  2101. list := newModeList.modeinfo[c];
  2102. while assigned(list) do
  2103. begin
  2104. tmp := list;
  2105. list:=list^.next;
  2106. dispose(tmp);
  2107. end;
  2108. end;
  2109. {$endif nonewmodes}
  2110. {$IFDEF DPMI}
  2111. { We had copied the buffer of mode information }
  2112. { and allocated it dynamically... now free it }
  2113. { Warning: if GetVESAInfo returned false, this buffer is not allocated! (JM)}
  2114. If hasVesa then
  2115. Dispose(VESAInfo.ModeList);
  2116. {$ENDIF}
  2117. end;
  2118. procedure InitializeGraph;
  2119. begin
  2120. {$ifdef logging}
  2121. assign(debuglog,'grlog.txt');
  2122. rewrite(debuglog);
  2123. close(debuglog);
  2124. {$endif logging}
  2125. isgraphmode := false;
  2126. ModeList := nil;
  2127. {$ifndef nonewmodes}
  2128. fillChar(newModeList.modeinfo,sizeof(newModeList.modeinfo),#0);
  2129. { lo and hi modenumber are -1 currently (no modes supported) }
  2130. fillChar(newModeList.loHiModeNr,sizeof(newModeList.loHiModeNr),#255);
  2131. {$endif nonewmodes}
  2132. SaveVideoState := nil;
  2133. RestoreVideoState := nil;
  2134. {$ifdef oldfont}
  2135. {$ifdef go32v2}
  2136. LoadFont8x8;
  2137. {$endif go32v2}
  2138. {$endif oldfont}
  2139. { This must be called at startup... because GetGraphMode may }
  2140. { be called even when not in graph mode. }
  2141. {$ifdef logging}
  2142. LogLn('Calling QueryAdapterInfo...');
  2143. {$endif logging}
  2144. QueryAdapterInfo;
  2145. { Install standard fonts }
  2146. { This is done BEFORE startup... }
  2147. InstalledFonts := 0;
  2148. InstallUserFont('TRIP');
  2149. InstallUserFont('LITT');
  2150. InstallUserFont('SANS');
  2151. InstallUserFont('GOTH');
  2152. InstallUserFont('SCRI');
  2153. InstallUserFont('SIMP');
  2154. InstallUserFont('TSCR');
  2155. InstallUserFont('LCOM');
  2156. InstallUserFont('EURO');
  2157. InstallUserFont('BOLD');
  2158. { This installs an exit procedure which cleans up the mode list...}
  2159. ExitSave := ExitProc;
  2160. ExitProc := @GraphExitProc;
  2161. {$ifdef win32}
  2162. charmessagehandler:=nil;
  2163. {$endif win32}
  2164. end;
  2165. {
  2166. $Log$
  2167. Revision 1.6 2001-04-14 16:06:03 jonas
  2168. * fixed problem with new modes system (reported by Vladimir Ravodin) (merged)
  2169. Revision 1.5 2000/12/07 17:19:47 jonas
  2170. * new constant handling: from now on, hex constants >$7fffffff are
  2171. parsed as unsigned constants (otherwise, $80000000 got sign extended
  2172. and became $ffffffff80000000), all constants in the longint range
  2173. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  2174. are cardinals and the rest are int64's.
  2175. * added lots of longint typecast to prevent range check errors in the
  2176. compiler and rtl
  2177. * type casts of symbolic ordinal constants are now preserved
  2178. * fixed bug where the original resulttype wasn't restored correctly
  2179. after doing a 64bit rangecheck
  2180. Revision 1.4 2000/08/12 12:27:13 jonas
  2181. + setallpalette hook
  2182. + setallpalette implemented for standard vga and VESA 2.0+
  2183. Revision 1.3 2000/08/05 18:34:47 peter
  2184. * merged setvideostate patch
  2185. Revision 1.2 2000/07/13 11:33:46 michael
  2186. + removed logs
  2187. }