2
0

graph.pp 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1993,99 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. Unit Graph;
  12. {-------------------------------------------------------}
  13. { Differences with TP Graph unit: }
  14. { - default putimage and getimage only support a max. }
  15. { of 64K colors on screen, because all pixels are }
  16. { saved as words. }
  17. { - Set RGB Palette is not used, SetPalette must be }
  18. { used instead. }
  19. { - In the TP graph unit, Clipping is always performed }
  20. { on strings written with OutText, and this clipping }
  21. { is done on a character per character basis (for }
  22. { example, if ONE part of a character is outside the }
  23. { viewport , then that character is not written at }
  24. { all to the screen. In FPC Pascal, clipping is done }
  25. { on a PIXEL basis, not a character basis, so part of }
  26. { characters which are not entirely in the viewport }
  27. { may appear on the screen. }
  28. { - SetTextStyle only conforms to the TP version when }
  29. { the correct (and expected) values are used for }
  30. { CharSize for stroked fonts (4 = stroked fonts) }
  31. { - InstallUserDriver is not supported, so always }
  32. { returns an error. }
  33. { - RegisterBGIDriver is not supported, so always }
  34. { returns an error. }
  35. { - DrawPoly XORPut mode is not exactly the same as in }
  36. { the TP graph unit. }
  37. { - Imagesize returns a longint instead of a word }
  38. { - ImageSize cannot return an error value }
  39. {-------------------------------------------------------}
  40. { AUTHORS: }
  41. { Gernot Tenchio - original version }
  42. { Florian Klaempfl - major updates }
  43. { Pierre Mueller - major bugfixes }
  44. { Carl Eric Codere - complete rewrite }
  45. { Thomas Schatzl - optimizations,routines and }
  46. { suggestions. }
  47. { Jonas Maebe - bugfixes and optimizations }
  48. { Credits (external): }
  49. { - Original FloodFill code by }
  50. { Menno Victor van der star }
  51. { (the code has been heavily modified) }
  52. {-------------------------------------------------------}
  53. {-------------------------------------------------------}
  54. { For significant speed improvements , is is recommended }
  55. { that these routines be hooked (otherwise the default, }
  56. { slower routines will be used) : }
  57. { HLine() }
  58. { VLine() }
  59. { PatternLine() }
  60. { ClearViewPort() }
  61. { PutImage() }
  62. { GetImage() - ImageSize() should also be changed }
  63. { InternalEllipse() }
  64. { Line() }
  65. { GetScanLine() }
  66. {--------------------------------------------------------}
  67. { FPC unit requirements: }
  68. { All modes should at least have 1 graphics page to }
  69. { make it possible to create animation on all supported }
  70. { systems , this can be done either by double-buffering }
  71. { yourself in the heap if no hardware is available to do}
  72. { it. }
  73. {--------------------------------------------------------}
  74. { COMPATIBILITY WARNING: Some of the compatibility tests }
  75. { were done using the CGA and other the VGA drivers. }
  76. { Within the BGI drivers themselves the BEHAVIOUR is not }
  77. { the same, so be warned!!! }
  78. {--------------------------------------------------------}
  79. { History log: }
  80. { 15th February 1999: }
  81. { + Added support for system font in vertical mode }
  82. { + system font is now available for all platforms }
  83. { * font support routines now compile }
  84. { * textHeight would not return correct size for system }
  85. { font }
  86. { * Alignment of fonts partly fixed }
  87. { 17th Feb. 1999: }
  88. { + First support for stroked fonts }
  89. { 18th Feb. 1999: }
  90. { * bugfix of line drawing which fixes stroked font }
  91. { displays. }
  92. { 23rd Feb. 1999: }
  93. { + Applied Pierre's patches to font }
  94. { + Added scaling of bitmapped fonts }
  95. { + Vertical stroked fonts }
  96. { 24th Feb. 1999: }
  97. { * Scaling of stroked fonts must be done using FPs }
  98. { to be 100% compatible with turbo pascal }
  99. { + Sped up by 40% stroked font scaling calculations }
  100. { + RegisterBGIFont }
  101. { 9th march 1999: }
  102. { + Starting implementing Fillpoly() }
  103. { 15th march 1999: }
  104. { + SetFillStyle() }
  105. { + patternLine() }
  106. { + Bar() }
  107. { * GraphDefaults would not make the Default color }
  108. { of the fill pattern to the Max. Palette entry. }
  109. { + SetFillPattern() }
  110. { 20th march 1999: }
  111. { * GraphDefaults would not reset to the text system }
  112. { * DefaultFont would write one character too much to }
  113. { the screen }
  114. { + Sloped thick lines in Line() }
  115. { + Sloped patterned lines in Line() }
  116. { * GraphDefaults would not reset the User Fill pattern}
  117. { to $ff }
  118. { + DirectPutPixel takes care of XOR mode writes }
  119. { improves speed by about 30% over old method of }
  120. { GetPixel XOR CurrentColor }
  121. { * Dashed LineStyle exactly like BP version now }
  122. { + Center LineStyle (checked against CGA driver) }
  123. { * GraphDefaults() now resets linepattern array }
  124. { 1st april 1999: }
  125. { + First implementation of FillPoly (incomplete) }
  126. { 2nd april 1999: }
  127. { * FillPoly did not Reset PatternLine index }
  128. { * FillPoly did not use correct color }
  129. { * PatternLine was writing modes in reverse direction }
  130. { * PatternLine would not work with non-rectangular }
  131. { shapes. }
  132. { * PatternLine must fill up the ENTIRE pattern, }
  133. { with either the foreground or background color. }
  134. { * GraphDefaults() would not call SetBkColor() }
  135. { * Fixed some memory leaks in FillPoly() }
  136. { 11th April 1999: }
  137. { * PatternLine() was drawing one pixel less then }
  138. { requested }
  139. { 12th April 1999: }
  140. { + FloodFill - first working implementation }
  141. { Horrbly slow even on very fast cpu's }
  142. { + Some suggestions of Thomas implemented }
  143. { 13th April 1999: }
  144. { * FloodFill() vertical index was off by one pixel }
  145. { * FloodFill() would never draw the last line in the }
  146. { list }
  147. { - Removed ClearViewPort320 which was wrong anyways, }
  148. { will need to be implemented later. }
  149. { * PatternLine() would not always restore write mode }
  150. { + Circle() uses NormalPut always with NormWidth lines}
  151. { + FillEllipse() initial version }
  152. { * InternalEllipse() - 0 to 360 now supported as }
  153. { angles. }
  154. { 14th April 1999: }
  155. { * mod x = and (x-1)(from Thomas Schatzl) gives a }
  156. { significant speed improvement. }
  157. { 15th april 1999: }
  158. { + Arc() ok except for Aspect Ratio, which does not }
  159. { give us the correct ratio on a 320x200 screen. }
  160. { + Added FillPoly() from Thomas Schatzl }
  161. { + More hookable routines }
  162. { 16th april 1999: }
  163. { + Line() checked ok. }
  164. { 17th april 1999: }
  165. { * GraphDefaults() would not reset CP }
  166. { + GetX(), GetY(), MoveTo() checked for viewports }
  167. { * OutTextXY() should not update the CP }
  168. { * ClearViewPort() would not update the CP }
  169. { * ClearDevice() would not update the CP }
  170. { * Sector() would update the CP by calling LineTo }
  171. { * Bar3D() would update the CP }
  172. { * PieSlice() would update the CP }
  173. { 18th april 1999: }
  174. { + Clipping algorithm }
  175. { 19th april 1999: }
  176. { + Adapterinfo structure }
  177. { 20th april 1999: }
  178. { + GetModeName }
  179. { + GetGraphMode }
  180. { + GetModeRange }
  181. {--------------------------------------------------------}
  182. { LEFT TO DO: }
  183. { - optimize scaling of stroked fonts }
  184. { - optimize InternalEllipse() }
  185. { using linear appx. of sine/cosine tables }
  186. { - justification for stroked fonts does not work }
  187. { - On Closegraph deallocate all font pointers }
  188. {--------------------------------------------------------}
  189. { text.inc will crash on aligned requirement machines. }
  190. { (packed record for fontrec) }
  191. {$ifndef fpc}
  192. {$G+}
  193. {$endif}
  194. Interface
  195. const
  196. { error codes }
  197. grOk = 0;
  198. grNoInitGraph = -1;
  199. grNotDetected = -2;
  200. grFileNotFound = -3;
  201. grInvalidDriver = -4;
  202. grNoLoadMem = -5;
  203. grNoScanMem = -6;
  204. grNoFloodMem = -7;
  205. grFontNotFound = -8;
  206. grNoFontMem = -9;
  207. grInvalidMode = -10;
  208. grError = -11;
  209. grIOerror = -12;
  210. grInvalidFont = -13;
  211. grInvalidFontNum = -14;
  212. grInvalidVersion = -18;
  213. { Color constants for setpalette }
  214. black = 0;
  215. blue = 1;
  216. green = 2;
  217. cyan = 3;
  218. red = 4;
  219. magenta = 5;
  220. brown = 6;
  221. lightgray = 7;
  222. darkgray = 8;
  223. lightblue = 9;
  224. lightgreen = 10;
  225. lightcyan = 11;
  226. lightred = 12;
  227. lightmagenta = 13;
  228. yellow = 14;
  229. white = 15;
  230. EGABlack = 0;
  231. EGABlue = 1;
  232. EGAGreen = 2;
  233. EGACyan = 3;
  234. EGARed = 4;
  235. EGAMagenta = 5;
  236. EGALightgray= 7;
  237. EGABrown = 20;
  238. EGADarkgray = 56;
  239. EGALightblue = 57;
  240. EGALightgreen = 58;
  241. EGALightcyan = 59;
  242. EGALightred = 60;
  243. EGALightmagenta=61;
  244. EGAYellow = 62;
  245. EGAWhite = 63;
  246. { Line styles for GetLineStyle/SetLineStyle }
  247. SolidLn = 0;
  248. DottedLn = 1;
  249. CenterLn = 2;
  250. DashedLn = 3;
  251. UserBitLn = 4;
  252. NormWidth = 1;
  253. ThickWidth = 3;
  254. { Set/GetTextStyle Konstanten: }
  255. DefaultFont = 0;
  256. TriplexFont = 1;
  257. SmallFont = 2;
  258. SansSerifFont = 3;
  259. GothicFont = 4;
  260. ScriptFont = 5;
  261. SimpleFont = 6;
  262. TSCRFont = 7;
  263. LCOMFont = 8;
  264. EuroFont = 9;
  265. BoldFont = 10;
  266. HorizDir = 0;
  267. VertDir = 1;
  268. UserCharSize = 0;
  269. ClipOn = true;
  270. ClipOff = false;
  271. { Bar3D constants }
  272. TopOn = true;
  273. TopOff = false;
  274. { fill pattern for Get/SetFillStyle: }
  275. EmptyFill = 0;
  276. SolidFill = 1;
  277. LineFill = 2;
  278. LtSlashFill = 3;
  279. SlashFill = 4;
  280. BkSlashFill = 5;
  281. LtBkSlashFill = 6;
  282. HatchFill = 7;
  283. XHatchFill = 8;
  284. InterleaveFill = 9;
  285. WideDotFill = 10;
  286. CloseDotFill = 11;
  287. UserFill = 12;
  288. { bitblt operators }
  289. NormalPut = 0;
  290. CopyPut = 0;
  291. XORPut = 1;
  292. OrPut = 2;
  293. AndPut = 3;
  294. NotPut = 4;
  295. { SetTextJustify constants }
  296. LeftText = 0;
  297. CenterText = 1;
  298. RightText = 2;
  299. BottomText = 0;
  300. TopText = 2;
  301. { graphic drivers }
  302. CurrentDriver = -128;
  303. Detect = 0;
  304. LowRes = 1;
  305. HercMono = 7;
  306. VGA = 9;
  307. VESA = 10;
  308. { graph modes }
  309. Default = 0;
  310. { VGA Driver modes }
  311. VGALo = 0;
  312. VGAMed = 1;
  313. VGAHi = 2;
  314. { Hercules mono card }
  315. HercMonoHi = 0;
  316. {$i graphmod.inc}
  317. MaxColors = 255; { Maximum possible colors using a palette }
  318. { otherwise, direct color encoding }
  319. type
  320. RGBRec = packed record
  321. Red: integer;
  322. Green: integer;
  323. Blue : integer;
  324. end;
  325. PaletteType = record
  326. Size : longint;
  327. Colors : array[0..MaxColors] of RGBRec;
  328. end;
  329. LineSettingsType = record
  330. linestyle : word;
  331. pattern : word;
  332. thickness : word;
  333. end;
  334. TextSettingsType = record
  335. font : word;
  336. direction : word;
  337. charsize : word;
  338. horiz : word;
  339. vert : word;
  340. end;
  341. FillSettingsType = record
  342. pattern : word;
  343. color : word;
  344. end;
  345. FillPatternType = array[1..8] of byte;
  346. PointType = record
  347. x,y : integer;
  348. end;
  349. ViewPortType = record
  350. x1,y1,x2,y2 : integer;
  351. Clip : boolean;
  352. end;
  353. ArcCoordsType = record
  354. x,y : integer;
  355. xstart,ystart : integer;
  356. xend,yend : integer;
  357. end;
  358. {$IFDEF FPC}
  359. graph_int = longint; { platform specific integer used for indexes;
  360. should be 16 bits on TP/BP and 32 bits on every-
  361. thing else for speed reasons }
  362. graph_float = single; { the platform's preferred floating point size }
  363. {$ELSE}
  364. graph_int = integer; { platform specific integer used for indexes;
  365. should be 16 bits on TP/BP and 32 bits on every-
  366. thing else for speed reasons }
  367. graph_float = real; { the platform's preferred floating point size }
  368. {$ENDIF}
  369. const
  370. fillpatternTable : array[0..12] of FillPatternType = (
  371. ($00,$00,$00,$00,$00,$00,$00,$00), { background color }
  372. ($ff,$ff,$ff,$ff,$ff,$ff,$ff,$ff), { foreground color }
  373. ($ff,$ff,$00,$00,$ff,$ff,$00,$00), { horizontal lines }
  374. ($01,$02,$04,$08,$10,$20,$40,$80), { slashes }
  375. ($07,$0e,$1c,$38,$70,$e0,$c1,$83), { thick slashes }
  376. ($07,$83,$c1,$e0,$70,$38,$1c,$0e), { thick backslashes }
  377. ($5a,$2d,$96,$4b,$a5,$d2,$69,$b4), { backslashes }
  378. ($ff,$88,$88,$88,$ff,$88,$88,$88), { small boxes }
  379. ($18,$24,$42,$81,$81,$42,$24,$18), { rhombus }
  380. ($cc,$33,$cc,$33,$cc,$33,$cc,$33), { wall pattern }
  381. ($80,$00,$08,$00,$80,$00,$08,$00), { wide points }
  382. ($88,$00,$22,$00,$88,$00,$22,$00), { dense points }
  383. (0,0,0,0,0,0,0,0) { user defined line style }
  384. );
  385. { ******************** PROCEDURAL VARIABLES ********************* }
  386. { * These are hooks which have device specific stuff in them, * }
  387. { * therefore to add new modes or to redirect these routines * }
  388. { * then declare variables of these types as shown below. * }
  389. {-----------------------------------------------------------------}
  390. TYPE
  391. { This is the standard putpixel routine used by all function }
  392. { drawing routines, it will use the viewport settings, as }
  393. { well as clip, and use the current foreground color to plot }
  394. { the desired pixel. }
  395. defpixelproc = procedure(X,Y: Integer);
  396. { standard plot and get pixel }
  397. getpixelproc = function(X,Y: Integer): word;
  398. putpixelproc = procedure(X,Y: Integer; Color: Word);
  399. { clears the viewport, also used to clear the device }
  400. clrviewproc = procedure;
  401. { putimage procedure, can be hooked to accomplish transparency }
  402. putimageproc = procedure (X,Y: Integer; var Bitmap; BitBlt: Word);
  403. getimageproc = procedure(X1,Y1,X2,Y2: Integer; Var Bitmap);
  404. imagesizeproc= function (X1,Y1,X2,Y2: Integer): longint;
  405. graphfreememprc = procedure (var P: Pointer; size: word);
  406. graphgetmemprc = procedure (var P: pointer; size: word);
  407. { internal routines -- can be hooked for much faster drawing }
  408. { draw filled horizontal lines using current color }
  409. { on entry coordinates are already clipped. }
  410. hlineproc = procedure (x, x2,y : integer);
  411. { on entry coordinates are already clipped. }
  412. { draw filled vertical line using current color }
  413. vlineproc = procedure (x,y,y2: integer);
  414. { this routine is used to draw filled patterns for all routines }
  415. { that require it. (FillPoly, FloodFill, Sector, etc... }
  416. { clipping is verified, uses current Fill settings for drawing }
  417. patternlineproc = procedure (x1,x2,y: integer);
  418. { this routine is used to draw all circles/ellipses/sectors }
  419. { more info... on this later... }
  420. ellipseproc = procedure (X,Y: Integer;XRadius: word;
  421. YRadius:word; stAngle,EndAngle: word; fp: PatternLineProc);
  422. { Line routine - draws lines thick/norm widths with current }
  423. { color and line style - LINE must be clipped here. }
  424. lineproc = procedure (X1, Y1, X2, Y2 : Integer);
  425. { this routine is used for FloodFill - it returns an entire }
  426. { screen scan line with a word for each pixel in the scanline }
  427. getscanlineproc = procedure (Y : integer; var data);
  428. { changes the active display screen where we draw to... }
  429. setactivepageproc = procedure (page: word);
  430. { changes the active display screen which we see ... }
  431. setvisualpageproc = procedure (page: word);
  432. { this routine actually switches to the desired video mode. }
  433. initmodeproc = procedure;
  434. { this routine is called to save the sate just before a mode set }
  435. savestateproc = procedure;
  436. { this routine is called in closegraph to cleanup... }
  437. restorestateproc = procedure;
  438. { This routine is a hook for SetRGBPalette }
  439. setrgbpaletteproc =
  440. procedure(ColorNum, RedValue, GreenValue, BlueValue: Integer);
  441. { This routine is a hook for GetRGBPalette }
  442. getrgbpaletteproc =
  443. procedure(ColorNum: integer; var
  444. RedValue, GreenValue, BlueValue: Integer);
  445. TYPE
  446. {-----------------------------------}
  447. { Linked list for mode information }
  448. { This list is set up by one of the }
  449. { following routines: }
  450. { It lists all available resolutions}
  451. { on this display adapter. }
  452. {-----------------------------------}
  453. { QueryAdapter() }
  454. { DetectGraph() }
  455. { InitGraph() }
  456. {-----------------------------------}
  457. PModeInfo = ^TModeInfo;
  458. TModeInfo = record
  459. DriverNumber: Integer;
  460. ModeNumber: Integer;
  461. MaxColor: Longint; { Maximum colors on screen }
  462. PaletteSize : Longint; { Maximum palette entry we can change }
  463. XAspect : Integer; { XAspect ratio correction factor }
  464. YAspect : Integer; { YAspect ratio correction factor }
  465. MaxX: Integer; { Max-X row }
  466. MaxY: Integer; { Max. column. }
  467. DirectColor: boolean; { Is this a direct color mode?? }
  468. Hardwarepages: byte; { total number of image pages - 1 }
  469. ModeName: String[18];
  470. { necessary hooks ... }
  471. DirectPutPixel : DefPixelProc;
  472. GetPixel : GetPixelProc;
  473. PutPixel : PutPixelProc;
  474. SetRGBPalette : SetRGBPaletteProc;
  475. GetRGBPalette : GetRGBPaletteProc;
  476. { defaults possible ... }
  477. SetVisualPage : SetVisualPageProc;
  478. SetActivePage : SetActivePageProc;
  479. ClearViewPort : ClrViewProc;
  480. PutImage : PutImageProc;
  481. GetImage : GetImageProc;
  482. ImageSize : ImageSizeProc;
  483. GetScanLine : GetScanLineProc;
  484. Line : LineProc;
  485. InternalEllipse: EllipseProc;
  486. PatternLine : PatternLineProc;
  487. HLine : HLineProc;
  488. VLine : VLineProc;
  489. InitMode : InitModeProc;
  490. next: PModeInfo;
  491. end;
  492. VAR
  493. DirectPutPixel : DefPixelProc;
  494. ClearViewPort : ClrViewProc;
  495. PutPixel : PutPixelProc;
  496. PutImage : PutImageProc;
  497. GetImage : GetImageProc;
  498. ImageSize : ImageSizeProc;
  499. GetPixel : GetPixelProc;
  500. SetVisualPage : SetVisualPageProc;
  501. SetActivePage : SetActivePageProc;
  502. SetRGBPalette : SetRGBPaletteProc;
  503. GetRGBPalette : GetRGBPaletteProc;
  504. GraphFreeMemPtr: graphfreememprc;
  505. GraphGetMemPtr : graphgetmemprc;
  506. GetScanLine : GetScanLineProc;
  507. Line : LineProc;
  508. InternalEllipse: EllipseProc;
  509. PatternLine : PatternLineProc;
  510. HLine : HLineProc;
  511. VLine : VLineProc;
  512. SaveVideoState : SaveStateProc;
  513. RestoreVideoState: RestoreStateProc;
  514. ExitSave: pointer;
  515. Procedure Closegraph;
  516. procedure SetLineStyle(LineStyle: word; Pattern: word; Thickness: word);
  517. function GraphErrorMsg(ErrorCode: Integer): string;
  518. Function GetMaxX: Integer;
  519. Function GetMaxY: Integer;
  520. Procedure SetViewPort(X1, Y1, X2, Y2: Integer; Clip: Boolean);
  521. Function GraphResult: Integer;
  522. function GetModeName(ModeNumber: integer): string;
  523. procedure SetGraphMode(Mode: Integer);
  524. function GetGraphMode: Integer;
  525. function GetMaxMode: word;
  526. procedure RestoreCrtMode;
  527. procedure GetModeRange(GraphDriver: Integer; var LoMode, HiMode: Integer);
  528. Function GetX: Integer;
  529. Function GetY: Integer;
  530. procedure GraphDefaults;
  531. procedure ClearDevice;
  532. procedure GetViewSettings(var viewport : ViewPortType);
  533. procedure SetWriteMode(WriteMode : integer);
  534. procedure GetFillSettings(var Fillinfo:Fillsettingstype);
  535. procedure GetFillPattern(var FillPattern:FillPatternType);
  536. procedure GetLineSettings(var ActiveLineInfo : LineSettingsType);
  537. procedure InitGraph(var GraphDriver:Integer;var GraphMode:Integer;const PathToDriver:String);
  538. function InstallUserDriver(Name: string; AutoDetectPtr: Pointer): integer;
  539. function RegisterBGIDriver(driver: pointer): integer;
  540. procedure SetFillStyle(Pattern : word; Color: word);
  541. procedure SetFillPattern(Pattern: FillPatternType; Color: word);
  542. Function GetDriverName: string;
  543. procedure MoveRel(Dx, Dy: Integer);
  544. procedure MoveTo(X,Y: Integer);
  545. procedure SetDirectVideo(DirectAccess: boolean);
  546. function GetDirectVideo: boolean;
  547. { -------------------- Color/Palette ------------------------------- }
  548. procedure SetBkColor(ColorNum: Word);
  549. function GetColor: Word;
  550. function GetBkColor: Word;
  551. procedure SetColor(Color: Word);
  552. function GetMaxColor: word;
  553. procedure SetAllPalette(var Palette:PaletteType);
  554. procedure SetPalette(ColorNum: word; Color: shortint);
  555. procedure GetPalette(var Palette: PaletteType);
  556. function GetPaletteSize: integer;
  557. procedure GetDefaultPalette(var Palette: PaletteType);
  558. { -------------------- Shapes/Lines -------------------------------- }
  559. procedure Rectangle(x1,y1,x2,y2:integer);
  560. procedure Bar(x1,y1,x2,y2:integer);
  561. procedure Bar3D(x1, y1, x2, y2 : integer;depth : word;top : boolean);
  562. procedure FillPoly(NumPoints: word; Var PolyPoints);
  563. procedure DrawPoly(NumPoints : word;var polypoints);
  564. procedure LineRel(Dx, Dy: Integer);
  565. procedure LineTo(X,Y : Integer);
  566. procedure FloodFill(x : integer; y : integer; Border: word);
  567. { -------------------- Circle related routines --------------------- }
  568. procedure GetAspectRatio(var Xasp,Yasp : word);
  569. procedure SetAspectRatio(Xasp, Yasp : word);
  570. procedure GetArcCoords(var ArcCoords: ArcCoordsType);
  571. procedure Arc(X,Y : Integer; StAngle,EndAngle,Radius: word);
  572. procedure PieSlice(X,Y,stangle,endAngle:integer;Radius: Word);
  573. procedure FillEllipse(X, Y: Integer; XRadius, YRadius: Word);
  574. procedure Circle(X, Y: Integer; Radius:Word);
  575. procedure Sector(x, y: Integer; StAngle,EndAngle, XRadius, YRadius: Word);
  576. procedure Ellipse(X,Y : Integer; stAngle, EndAngle: word; XRadius,
  577. YRadius: word);
  578. { --------------------- Text related routines --------------------- }
  579. function InstallUserFont(const FontFileName : string) : integer;
  580. function RegisterBGIfont(font : pointer) : integer;
  581. procedure GetTextSettings(var TextInfo : TextSettingsType);
  582. function TextHeight(const TextString : string) : word;
  583. function TextWidth(const TextString : string) : word;
  584. procedure SetTextJustify(horiz,vert : word);
  585. procedure SetTextStyle(font,direction : word;charsize : word);
  586. procedure SetUserCharSize(Multx,Divx,Multy,Divy : word);
  587. procedure OutTextXY(x,y : integer;const TextString : string);
  588. procedure OutText(const TextString : string);
  589. Implementation
  590. {$ifdef fpc}
  591. {$ifdef go32v2}
  592. {$define dpmi}
  593. uses go32,ports;
  594. Type TDPMIRegisters = go32.registers;
  595. {$endif go32v2}
  596. {$else fpc}
  597. {$IFDEF DPMI}
  598. uses WinAPI;
  599. {$ENDIF}
  600. {$endif fpc}
  601. {$ifdef logging}
  602. var debuglog: text;
  603. function strf(l: longint): string;
  604. begin
  605. str(l, strf)
  606. end;
  607. Procedure Log(Const s: String);
  608. Begin
  609. Append(debuglog);
  610. Write(debuglog, s);
  611. Close(debuglog);
  612. End;
  613. Procedure LogLn(Const s: string);
  614. Begin
  615. Append(debuglog);
  616. Writeln(debuglog,s);
  617. Close(debuglog);
  618. End;
  619. {$endif logging}
  620. const
  621. StdBufferSize = 4096; { Buffer size for FloodFill }
  622. type
  623. tinttable = array[0..16383] of integer;
  624. pinttable = ^tinttable;
  625. WordArray = Array [0..StdbufferSize] Of word;
  626. PWordArray = ^WordArray;
  627. const
  628. { Mask for each bit in byte used to determine pattern }
  629. BitArray: Array[0..7] of byte =
  630. ($01,$02,$04,$08,$10,$20,$40,$80);
  631. RevbitArray: Array[0..7] of byte =
  632. ($80,$40,$20,$10,$08,$04,$02,$01);
  633. { pre expanded line patterns }
  634. { 0 = LSB of byte pattern }
  635. { 15 = MSB of byte pattern }
  636. LinePatterns: Array[0..15] of BOOLEAN =
  637. (TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,
  638. TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE);
  639. const
  640. BGIPath : string = '.';
  641. { Default font 8x8 system from IBM PC }
  642. {$i fontdata.inc}
  643. var
  644. CurrentColor: Word;
  645. CurrentBkColor: Word;
  646. CurrentX : Integer; { viewport relative }
  647. CurrentY : Integer; { viewport relative }
  648. ClipPixels: Boolean; { Should cliiping be enabled }
  649. CurrentWriteMode: Integer;
  650. _GraphResult : Integer;
  651. LineInfo : LineSettingsType;
  652. FillSettings: FillSettingsType;
  653. { information for Text Output routines }
  654. CurrentTextInfo : TextSettingsType;
  655. CurrentXRatio, CurrentYRatio: graph_float;
  656. installedfonts: longint; { Number of installed fonts }
  657. StartXViewPort: Integer; { absolute }
  658. StartYViewPort: Integer; { absolute }
  659. ViewWidth : Integer;
  660. ViewHeight: Integer;
  661. IsGraphMode : Boolean; { Indicates if we are in graph mode or not }
  662. ArcCall: ArcCoordsType; { Information on the last call to Arc or Ellipse }
  663. var
  664. { ******************** HARDWARE INFORMATION ********************* }
  665. { Should be set in InitGraph once only. }
  666. IntCurrentMode : Integer;
  667. IntCurrentDriver : Integer; { Currently loaded driver }
  668. XAspect : Integer;
  669. YAspect : Integer;
  670. MaxX : Integer; { Maximum resolution - ABSOLUTE }
  671. MaxY : Integer; { Maximum resolution - ABSOLUTE }
  672. MaxColor : Longint;
  673. PaletteSize : longint; { Maximum palette entry we can set, usually equal}
  674. { maxcolor. }
  675. HardwarePages : byte; { maximum number of hardware visual pages }
  676. DriverName: String;
  677. DirectColor : Boolean ; { Is it a direct color mode? }
  678. ModeList : PModeInfo;
  679. DirectVideo : Boolean; { Direct access to video memory? }
  680. {--------------------------------------------------------------------------}
  681. { }
  682. { LINE AND LINE RELATED ROUTINES }
  683. { }
  684. {--------------------------------------------------------------------------}
  685. {$i clip.inc}
  686. procedure HLineDefault(x,x2,y: integer); {$ifndef fpc}far;{$endif fpc}
  687. var
  688. xtmp: integer;
  689. Begin
  690. { must we swap the values? }
  691. if x >= x2 then
  692. Begin
  693. xtmp := x2;
  694. x2 := x;
  695. x:= xtmp;
  696. end;
  697. { First convert to global coordinates }
  698. X := X + StartXViewPort;
  699. X2 := X2 + StartXViewPort;
  700. Y := Y + StartYViewPort;
  701. if ClipPixels then
  702. Begin
  703. if LineClipped(x,y,x2,y,StartXViewPort,StartYViewPort,
  704. StartXViewPort+ViewWidth, StartYViewPort+ViewHeight) then
  705. exit;
  706. end;
  707. for x:= x to x2 do
  708. DirectPutPixel(X,Y);
  709. end;
  710. procedure VLineDefault(x,y,y2: integer); {$ifndef fpc}far;{$endif fpc}
  711. var
  712. ytmp: integer;
  713. Begin
  714. { must we swap the values? }
  715. if y >= y2 then
  716. Begin
  717. ytmp := y2;
  718. y2 := y;
  719. y:= ytmp;
  720. end;
  721. { First convert to global coordinates }
  722. X := X + StartXViewPort;
  723. Y2 := Y2 + StartYViewPort;
  724. Y := Y + StartYViewPort;
  725. if ClipPixels then
  726. Begin
  727. if LineClipped(x,y,x,y2,StartXViewPort,StartYViewPort,
  728. StartXViewPort+ViewWidth, StartYViewPort+ViewHeight) then
  729. exit;
  730. end;
  731. for y := y to y2 do Directputpixel(x,y)
  732. End;
  733. procedure LineDefault(X1, Y1, X2, Y2: Integer); {$ifndef fpc}far;{$endif fpc}
  734. var X, Y : Integer;
  735. deltax, deltay : Integer;
  736. d, dinc1, dinc2: Integer;
  737. xinc1 : Integer;
  738. xinc2 : Integer;
  739. yinc1 : Integer;
  740. yinc2 : Integer;
  741. i : Integer;
  742. Flag : Boolean; { determines pixel direction in thick lines }
  743. NumPixels : Integer;
  744. PixelCount : Integer;
  745. OldCurrentColor: Word;
  746. swtmp : integer;
  747. TmpNumPixels : integer;
  748. begin
  749. {******************************************}
  750. { SOLID LINES }
  751. {******************************************}
  752. if lineinfo.LineStyle = SolidLn then
  753. Begin
  754. { we separate normal and thick width for speed }
  755. { and because it would not be 100% compatible }
  756. { with the TP graph unit otherwise }
  757. if y1 = y2 then
  758. Begin
  759. {******************************************}
  760. { SOLID LINES HORIZONTAL }
  761. {******************************************}
  762. if lineinfo.Thickness=NormWidth then
  763. hline(x1,x2,y2)
  764. else
  765. begin
  766. { thick width }
  767. hline(x1,x2,y2-1);
  768. hline(x1,x2,y2);
  769. hline(x2,x2,y2+1);
  770. end;
  771. end
  772. else
  773. if x1 = x2 then
  774. Begin
  775. {******************************************}
  776. { SOLID LINES VERTICAL }
  777. {******************************************}
  778. if lineinfo.Thickness=NormWidth then
  779. vline(x1,y1,y2)
  780. else
  781. begin
  782. { thick width }
  783. vline(x1-1,y1,y2);
  784. vline(x1,y1,y2);
  785. vline(x1+1,y1,y2);
  786. end;
  787. end
  788. else
  789. begin
  790. { Convert to global coordinates. }
  791. x1 := x1 + StartXViewPort;
  792. x2 := x2 + StartXViewPort;
  793. y1 := y1 + StartYViewPort;
  794. y2 := y2 + StartYViewPort;
  795. { if fully clipped then exit... }
  796. if ClipPixels then
  797. begin
  798. if LineClipped(x1,y1,x2,y2,StartXViewPort, StartYViewPort,
  799. StartXViewPort+ViewWidth, StartYViewPort+ViewHeight) then
  800. exit;
  801. end;
  802. {******************************************}
  803. { SLOPED SOLID LINES }
  804. {******************************************}
  805. oldCurrentColor :=
  806. CurrentColor;
  807. { Calculate deltax and deltay for initialisation }
  808. deltax := abs(x2 - x1);
  809. deltay := abs(y2 - y1);
  810. { Initialize all vars based on which is the independent variable }
  811. if deltax >= deltay then
  812. begin
  813. Flag := FALSE;
  814. { x is independent variable }
  815. numpixels := deltax + 1;
  816. d := (2 * deltay) - deltax;
  817. dinc1 := deltay Shl 1;
  818. dinc2 := (deltay - deltax) shl 1;
  819. xinc1 := 1;
  820. xinc2 := 1;
  821. yinc1 := 0;
  822. yinc2 := 1;
  823. end
  824. else
  825. begin
  826. Flag := TRUE;
  827. { y is independent variable }
  828. numpixels := deltay + 1;
  829. d := (2 * deltax) - deltay;
  830. dinc1 := deltax Shl 1;
  831. dinc2 := (deltax - deltay) shl 1;
  832. xinc1 := 0;
  833. xinc2 := 1;
  834. yinc1 := 1;
  835. yinc2 := 1;
  836. end;
  837. { Make sure x and y move in the right directions }
  838. if x1 > x2 then
  839. begin
  840. xinc1 := - xinc1;
  841. xinc2 := - xinc2;
  842. end;
  843. if y1 > y2 then
  844. begin
  845. yinc1 := - yinc1;
  846. yinc2 := - yinc2;
  847. end;
  848. { Start drawing at <x1, y1> }
  849. x := x1;
  850. y := y1;
  851. If LineInfo.Thickness=NormWidth then
  852. Begin
  853. { Draw the pixels }
  854. for i := 1 to numpixels do
  855. begin
  856. DirectPutPixel(x, y);
  857. if d < 0 then
  858. begin
  859. d := d + dinc1;
  860. x := x + xinc1;
  861. y := y + yinc1;
  862. end
  863. else
  864. begin
  865. d := d + dinc2;
  866. x := x + xinc2;
  867. y := y + yinc2;
  868. end;
  869. CurrentColor := OldCurrentColor;
  870. end;
  871. end
  872. else
  873. { Thick width lines }
  874. begin
  875. { Draw the pixels }
  876. for i := 1 to numpixels do
  877. begin
  878. { all depending on the slope, we can determine }
  879. { in what direction the extra width pixels will be put }
  880. If Flag then
  881. Begin
  882. DirectPutPixel(x-1,y);
  883. DirectPutPixel(x,y);
  884. DirectPutPixel(x+1,y);
  885. end
  886. else
  887. Begin
  888. DirectPutPixel(x, y-1);
  889. DirectPutPixel(x, y);
  890. DirectPutPixel(x, y+1);
  891. end;
  892. if d < 0 then
  893. begin
  894. d := d + dinc1;
  895. x := x + xinc1;
  896. y := y + yinc1;
  897. end
  898. else
  899. begin
  900. d := d + dinc2;
  901. x := x + xinc2;
  902. y := y + yinc2;
  903. end;
  904. CurrentColor := OldCurrentColor;
  905. end;
  906. end;
  907. end;
  908. end
  909. else
  910. {******************************************}
  911. { begin patterned lines }
  912. {******************************************}
  913. Begin
  914. { Convert to global coordinates. }
  915. x1 := x1 + StartXViewPort;
  916. x2 := x2 + StartXViewPort;
  917. y1 := y1 + StartYViewPort;
  918. y2 := y2 + StartYViewPort;
  919. { if fully clipped then exit... }
  920. if ClipPixels then
  921. begin
  922. if LineClipped(x1,y1,x2,y2,StartXViewPort, StartYViewPort,
  923. StartXViewPort+ViewWidth, StartYViewPort+ViewHeight) then
  924. exit;
  925. end;
  926. OldCurrentColor := CurrentColor;
  927. PixelCount:=0;
  928. if y1 = y2 then
  929. Begin
  930. { Check if we must swap }
  931. if x1 >= x2 then
  932. Begin
  933. swtmp := x1;
  934. x1 := x2;
  935. x2 := swtmp;
  936. end;
  937. if LineInfo.Thickness = NormWidth then
  938. Begin
  939. for PixelCount:=x1 to x2 do
  940. { optimization: PixelCount mod 16 }
  941. if LinePatterns[PixelCount and 15] = TRUE then
  942. begin
  943. DirectPutPixel(PixelCount,y2);
  944. end;
  945. end
  946. else
  947. Begin
  948. for i:=-1 to 1 do
  949. Begin
  950. for PixelCount:=x1 to x2 do
  951. { Optimization from Thomas - mod 16 = and 15 }
  952. {this optimization has been performed by the compiler
  953. for while as well (JM)}
  954. if LinePatterns[PixelCount and 15] = TRUE then
  955. begin
  956. DirectPutPixel(PixelCount,y2+i);
  957. end;
  958. end;
  959. end;
  960. end
  961. else
  962. if x1 = x2 then
  963. Begin
  964. { Check if we must swap }
  965. if y1 >= y2 then
  966. Begin
  967. swtmp := y1;
  968. y1 := y2;
  969. y2 := swtmp;
  970. end;
  971. if LineInfo.Thickness = NormWidth then
  972. Begin
  973. for PixelCount:=y1 to y2 do
  974. { compare if we should plot a pixel here , compare }
  975. { with predefined line patterns... }
  976. if LinePatterns[PixelCount and 15] = TRUE then
  977. begin
  978. DirectPutPixel(x1,PixelCount);
  979. end;
  980. end
  981. else
  982. Begin
  983. for i:=-1 to 1 do
  984. Begin
  985. for PixelCount:=y1 to y2 do
  986. { compare if we should plot a pixel here , compare }
  987. { with predefined line patterns... }
  988. if LinePatterns[PixelCount and 15] = TRUE then
  989. begin
  990. DirectPutPixel(x1+i,PixelCount);
  991. end;
  992. end;
  993. end;
  994. end
  995. else
  996. Begin
  997. oldCurrentColor := CurrentColor;
  998. { Calculate deltax and deltay for initialisation }
  999. deltax := abs(x2 - x1);
  1000. deltay := abs(y2 - y1);
  1001. { Initialize all vars based on which is the independent variable }
  1002. if deltax >= deltay then
  1003. begin
  1004. Flag := FALSE;
  1005. { x is independent variable }
  1006. numpixels := deltax + 1;
  1007. d := (2 * deltay) - deltax;
  1008. dinc1 := deltay Shl 1;
  1009. dinc2 := (deltay - deltax) shl 1;
  1010. xinc1 := 1;
  1011. xinc2 := 1;
  1012. yinc1 := 0;
  1013. yinc2 := 1;
  1014. end
  1015. else
  1016. begin
  1017. Flag := TRUE;
  1018. { y is independent variable }
  1019. numpixels := deltay + 1;
  1020. d := (2 * deltax) - deltay;
  1021. dinc1 := deltax Shl 1;
  1022. dinc2 := (deltax - deltay) shl 1;
  1023. xinc1 := 0;
  1024. xinc2 := 1;
  1025. yinc1 := 1;
  1026. yinc2 := 1;
  1027. end;
  1028. { Make sure x and y move in the right directions }
  1029. if x1 > x2 then
  1030. begin
  1031. xinc1 := - xinc1;
  1032. xinc2 := - xinc2;
  1033. end;
  1034. if y1 > y2 then
  1035. begin
  1036. yinc1 := - yinc1;
  1037. yinc2 := - yinc2;
  1038. end;
  1039. { Start drawing at <x1, y1> }
  1040. x := x1;
  1041. y := y1;
  1042. If LineInfo.Thickness=ThickWidth then
  1043. Begin
  1044. TmpNumPixels := NumPixels-1;
  1045. { Draw the pixels }
  1046. for i := 0 to TmpNumPixels do
  1047. begin
  1048. { all depending on the slope, we can determine }
  1049. { in what direction the extra width pixels will be put }
  1050. If Flag then
  1051. Begin
  1052. { compare if we should plot a pixel here , compare }
  1053. { with predefined line patterns... }
  1054. if LinePatterns[i and 15] = TRUE then
  1055. begin
  1056. DirectPutPixel(x-1,y);
  1057. DirectPutPixel(x,y);
  1058. DirectPutPixel(x+1,y);
  1059. end;
  1060. end
  1061. else
  1062. Begin
  1063. { compare if we should plot a pixel here , compare }
  1064. { with predefined line patterns... }
  1065. if LinePatterns[i and 15] = TRUE then
  1066. begin
  1067. DirectPutPixel(x,y-1);
  1068. DirectPutPixel(x,y);
  1069. DirectPutPixel(x,y+1);
  1070. end;
  1071. end;
  1072. if d < 0 then
  1073. begin
  1074. d := d + dinc1;
  1075. x := x + xinc1;
  1076. y := y + yinc1;
  1077. end
  1078. else
  1079. begin
  1080. d := d + dinc2;
  1081. x := x + xinc2;
  1082. y := y + yinc2;
  1083. end;
  1084. end;
  1085. end
  1086. else
  1087. Begin
  1088. { instead of putting in loop , substract by one now }
  1089. TmpNumPixels := NumPixels-1;
  1090. { NormWidth }
  1091. for i := 0 to TmpNumPixels do
  1092. begin
  1093. if LinePatterns[i and 15] = TRUE then
  1094. begin
  1095. DirectPutPixel(x,y);
  1096. end;
  1097. if d < 0 then
  1098. begin
  1099. d := d + dinc1;
  1100. x := x + xinc1;
  1101. y := y + yinc1;
  1102. end
  1103. else
  1104. begin
  1105. d := d + dinc2;
  1106. x := x + xinc2;
  1107. y := y + yinc2;
  1108. end;
  1109. end;
  1110. end
  1111. end;
  1112. {******************************************}
  1113. { end patterned lines }
  1114. {******************************************}
  1115. { restore color }
  1116. CurrentColor:=OldCurrentColor;
  1117. end;
  1118. end; { Line }
  1119. {********************************************************}
  1120. { Procedure DummyPatternLine() }
  1121. {--------------------------------------------------------}
  1122. { This is suimply an procedure that does nothing which }
  1123. { can be passed as a patternlineproc for non-filled }
  1124. { ellipses }
  1125. {********************************************************}
  1126. Procedure DummyPatternLine(x1, x2, y: integer); {$ifdef tp} far; {$endif tp}
  1127. begin
  1128. end;
  1129. {********************************************************}
  1130. { Procedure InternalEllipse() }
  1131. {--------------------------------------------------------}
  1132. { This routine first calculates all points required to }
  1133. { draw a circle to the screen, and stores the points }
  1134. { to display in a buffer before plotting them. The }
  1135. { aspect ratio of the screen is taken into account when }
  1136. { calculating the values. }
  1137. {--------------------------------------------------------}
  1138. { INPUTS: X,Y : Center coordinates of Ellipse. }
  1139. { XRadius - X-Axis radius of ellipse. }
  1140. { YRadius - Y-Axis radius of ellipse. }
  1141. { stAngle, EndAngle: Start angle and end angles of the }
  1142. { ellipse (used for partial ellipses and circles) }
  1143. { pl: procedure which either draws a patternline (for }
  1144. { FillEllipse) or does nothing (arc etc) }
  1145. {--------------------------------------------------------}
  1146. { NOTE: - }
  1147. { - }
  1148. {********************************************************}
  1149. Procedure InternalEllipseDefault(X,Y: Integer;XRadius: word;
  1150. YRadius:word; stAngle,EndAngle: word; pl: PatternLineProc); {$ifndef fpc}far;{$endif fpc}
  1151. var
  1152. j, Delta, DeltaEnd: graph_float;
  1153. NumOfPixels: longint;
  1154. ConvFac,TempTerm: graph_float;
  1155. xtemp, ytemp, xp, yp, xm, ym, xnext, ynext,
  1156. plxpyp, plxmyp, plxpym, plxmym: integer;
  1157. BackupColor, DeltaAngle, TmpAngle, OldLineWidth: word;
  1158. Begin
  1159. If LineInfo.ThickNess = ThickWidth Then
  1160. { first draw the two outer ellipses using normwidth and no filling (JM) }
  1161. Begin
  1162. OldLineWidth := LineInfo.Thickness;
  1163. LineInfo.Thickness := NormWidth;
  1164. InternalEllipseDefault(x,y,XRadius,YRadius,StAngle,EndAngle,
  1165. {$ifdef fpc}@{$endif fpc}DummyPatternLine);
  1166. InternalEllipseDefault(x,y,XRadius+1,YRadius+1,StAngle,EndAngle,
  1167. {$ifdef fpc}@{$endif fpc}DummyPatternLine);
  1168. If (XRadius <> 0) and (YRadius <> 0) Then
  1169. { draw the smallest ellipse last, since that one will use the }
  1170. { original pl, so it could possibly draw patternlines (JM) }
  1171. Begin
  1172. Dec(XRadius);
  1173. Dec(YRadius);
  1174. End
  1175. Else Exit;
  1176. { restore line thickness }
  1177. LineInfo.Thickness := OldLineWidth;
  1178. End;
  1179. { check for an ellipse with negligable x and y radius }
  1180. If (xradius <= 1) and (yradius <= 1) then
  1181. begin
  1182. putpixel(x,y,CurrentColor);
  1183. ArcCall.X := X;
  1184. ArcCall.Y := Y;
  1185. ArcCall.XStart := X;
  1186. ArcCall.YStart := Y;
  1187. ArcCall.XEnd := X;
  1188. ArcCall.YEnd := Y;
  1189. exit;
  1190. end;
  1191. { for restoring after PatternLine }
  1192. BackupColor := CurrentColor;
  1193. If xradius = 0 then inc(x);
  1194. if yradius = 0 then inc(y);
  1195. { check if valid angles }
  1196. stangle := stAngle mod 361;
  1197. EndAngle := EndAngle mod 361;
  1198. { if impossible angles then swap them! }
  1199. if Endangle < StAngle then
  1200. Begin
  1201. TmpAngle:=EndAngle;
  1202. EndAngle:=StAngle;
  1203. Stangle:=TmpAngle;
  1204. end;
  1205. { calculate difference of angle now so we don't always have to calculate it }
  1206. DeltaAngle:= EndAngle-StAngle;
  1207. { approximate the number of pixels required by using the circumference }
  1208. { equation of an ellipse. }
  1209. { In the worst case, we have to calculate everything from the }
  1210. { quadrant, so divide the circumference value by 4 (JM) }
  1211. NumOfPixels:=Round(Sqrt(3)*sqrt(sqr(XRadius)+sqr(YRadius)));
  1212. { Calculate the angle precision required }
  1213. Delta := 90.0 / (NumOfPixels);
  1214. { Adjust for screen aspect ratio }
  1215. XRadius:=(longint(XRadius)*10000) div XAspect;
  1216. YRadius:=(longint(YRadius)*10000) div YAspect;
  1217. { removed from inner loop to make faster }
  1218. ConvFac:=Pi/180.0;
  1219. { store some arccall info }
  1220. ArcCall.X := X;
  1221. ArcCall.Y := Y;
  1222. TempTerm := (StAngle)*ConvFac;
  1223. ArcCall.XStart := round(XRadius*Cos(TempTerm)) + X;
  1224. ArcCall.YStart := round(YRadius*Sin(TempTerm+Pi)) + Y;
  1225. TempTerm := (EndAngle)*ConvFac;
  1226. ArcCall.XEnd := round(XRadius*Cos(TempTerm)) + X;
  1227. ArcCall.YEnd := round(YRadius*Sin(TempTerm+Pi)) + Y;
  1228. { otherwise we get an endless loop }
  1229. { If DeltaAngle = 0 Then
  1230. Begin
  1231. Line(X,Y,ArcCall.XStart,ArcCall.YStart);
  1232. exit
  1233. End;}
  1234. { Always just go over the first 90 degrees. Could be optimized a }
  1235. { bit if StAngle and EndAngle lie in the same quadrant, left as an }
  1236. { exercise for the reader :) (JM) }
  1237. j := 0;
  1238. { calculate stop position, go 1 further than 90 because otherwise }
  1239. { 1 pixel is sometimes not drawn (JM) }
  1240. DeltaEnd := 91;
  1241. { Calculate points }
  1242. xnext := XRadius;
  1243. ynext := 0;
  1244. Repeat
  1245. xtemp := xnext;
  1246. ytemp := ynext;
  1247. { this is used by both sin and cos }
  1248. TempTerm := (j+Delta)*ConvFac;
  1249. { Calculate points }
  1250. xnext := round(XRadius*Cos(TempTerm));
  1251. ynext := round(YRadius*Sin(TempTerm+Pi));
  1252. xp := x + xtemp;
  1253. xm := x - xtemp;
  1254. yp := y + ytemp;
  1255. ym := y - ytemp;
  1256. plxpyp := maxint;
  1257. plxmyp := -maxint-1;
  1258. plxpym := maxint;
  1259. plxmym := -maxint-1;
  1260. If (j >= StAngle) and (j <= EndAngle) then
  1261. begin
  1262. plxpyp := xp;
  1263. PutPixel(xp,yp,CurrentColor);
  1264. end;
  1265. If ((180-j) >= StAngle) and ((180-j) <= EndAngle) then
  1266. begin
  1267. plxmyp := xm;
  1268. PutPixel(xm,yp,CurrentColor);
  1269. end;
  1270. If ((j+180) >= StAngle) and ((j+180) <= EndAngle) then
  1271. begin
  1272. plxmym := xm;
  1273. PutPixel(xm,ym,CurrentColor);
  1274. end;
  1275. If ((360-j) >= StAngle) and ((360-j) <= EndAngle) then
  1276. begin
  1277. plxpym := xp;
  1278. PutPixel(xp,ym,CurrentColor);
  1279. end;
  1280. If (ynext <> ytemp) and
  1281. (xp <> xm) then
  1282. begin
  1283. CurrentColor := FillSettings.Color;
  1284. pl(plxmyp+1,plxpyp-1,yp);
  1285. pl(plxmym+1,plxpym-1,ym);
  1286. CurrentColor := BackupColor;
  1287. end;
  1288. j:=j+Delta;
  1289. Until j > (DeltaEnd);
  1290. end;
  1291. {********************************************************}
  1292. { Procedure InternalEllipse() }
  1293. {--------------------------------------------------------}
  1294. { This routine first calculates all points required to }
  1295. { draw a circle to the screen, and stores the points }
  1296. { to display in a buffer before plotting them. The }
  1297. { aspect ratio of the screen is taken into account when }
  1298. { calculating the values. }
  1299. {--------------------------------------------------------}
  1300. { INPUTS: X,Y : Center coordinates of Ellipse. }
  1301. { XRadius - X-Axis radius of ellipse. }
  1302. { YRadius - Y-Axis radius of ellipse. }
  1303. { stAngle, EndAngle: Start angle and end angles of the }
  1304. { ellipse (used for partial ellipses and circles) }
  1305. {--------------------------------------------------------}
  1306. { NOTE: - uses the current write mode. }
  1307. { - Angles must both be between 0 and 360 }
  1308. {********************************************************}
  1309. (*
  1310. Procedure InternalEllipseDefault (x, y : integer;
  1311. xradius, yradius, stAngle, EndAngle : Word); far
  1312. { Draw an ellipse arc. Crude but it works (anyone have a better one?) }
  1313. Var
  1314. aSqr, bSqr, twoaSqr, twobSqr, xa, ya, twoXbSqr, twoYaSqr, error : LongInt;
  1315. Alpha : graph_float;
  1316. const
  1317. RadToDeg = 180/Pi;
  1318. Procedure PlotPoints;
  1319. var
  1320. i,j: integer;
  1321. xm, ym: integer;
  1322. xp, yp: integer;
  1323. Begin
  1324. ym := y-ya;
  1325. yp := y+ya;
  1326. xm := x-xa;
  1327. xp := x+xa;
  1328. if LineInfo.Thickness = Normwidth then
  1329. Begin
  1330. If (Alpha>=StAngle) And (Alpha<=EndAngle) then
  1331. PutPixel (xm,ym, CurrentColor);
  1332. If (180-Alpha>=StAngle) And (180-Alpha<=EndAngle) then
  1333. PutPixel (xm,yp, CurrentColor);
  1334. If (180+Alpha>=StAngle) And (180+Alpha<=EndAngle) then
  1335. PutPixel (xp,yp, CurrentColor);
  1336. If (360-Alpha>=StAngle) And (360-Alpha<=EndAngle) then
  1337. PutPixel (xp,ym, CurrentColor);
  1338. end
  1339. else
  1340. Begin
  1341. If (Alpha>=StAngle) And (Alpha<=EndAngle) then
  1342. for i:=-1 to 1 do
  1343. for j:=-1 to 1 do
  1344. PutPixel (xm+i,ym+j, CurrentColor);
  1345. If (180-Alpha>=StAngle) And (180-Alpha<=EndAngle) then
  1346. for i:=-1 to 1 do
  1347. for j:=-1 to 1 do
  1348. PutPixel (xm+i,yp+j, CurrentColor);
  1349. If (180+Alpha>=StAngle) And (180+Alpha<=EndAngle) then
  1350. for i:=-1 to 1 do
  1351. for j:=-1 to 1 do
  1352. PutPixel (xp+i,yp+j, CurrentColor);
  1353. If (360-Alpha>=StAngle) And (360-Alpha<=EndAngle) then
  1354. for i:=-1 to 1 do
  1355. for j:=-1 to 1 do
  1356. PutPixel (xp+i,ym+j, CurrentColor);
  1357. end;
  1358. End;
  1359. Begin
  1360. StAngle:=StAngle MOD 361;
  1361. EndAngle:=EndAngle MOD 361;
  1362. StAngle := StAngle + 270;
  1363. EndAngle := EndAngle + 270;
  1364. If StAngle>EndAngle then
  1365. Begin
  1366. StAngle:=StAngle Xor EndAngle; EndAngle:=EndAngle Xor StAngle; StAngle:=EndAngle Xor StAngle;
  1367. End;
  1368. { Adjust for screen aspect ratio }
  1369. XRadius:=(longint(XRadius)*10000) div XAspect;
  1370. YRadius:=(longint(YRadius)*10000) div YAspect;
  1371. aSqr:=LongInt (xradius)*LongInt (xradius);
  1372. bSqr:=LongInt (yradius)*LongInt (yradius);
  1373. twoaSqr:=2*aSqr;
  1374. twobSqr:=2*bSqr;
  1375. xa:=0;
  1376. ya:=yradius;
  1377. twoXbSqr:=0;
  1378. twoYaSqr:=ya*twoaSqr;
  1379. error:=-ya*aSqr;
  1380. While twoXbSqr<=twoYaSqr Do Begin
  1381. If ya=0 then Alpha:=90 Else Alpha:=RadToDeg*Arctan (xa/ya); { Crude but it works }
  1382. PlotPoints;
  1383. Inc (xa);
  1384. Inc (twoXbSqr,twobSqr);
  1385. Inc (error,twoXbSqr-bSqr);
  1386. If error>=0 then Begin
  1387. Dec (ya);
  1388. Dec (twoYaSqr,twoaSqr);
  1389. Dec (error,twoYaSqr);
  1390. End;
  1391. End;
  1392. xa:=xradius;
  1393. ya:=0;
  1394. twoXbSqr:=xa*twobSqr;
  1395. twoYaSqr:=0;
  1396. error:=-xa*bSqr;
  1397. While twoXbSqr>twoYaSqr Do Begin
  1398. If ya=0 then Alpha:=90 Else Alpha:=RadToDeg*Arctan (xa/ya);
  1399. PlotPoints;
  1400. Inc (ya);
  1401. Inc (twoYaSqr,twoaSqr);
  1402. Inc (error,twoYaSqr-aSqr);
  1403. If error>=0 then Begin
  1404. Dec (xa);
  1405. Dec (twoXbSqr,twobSqr);
  1406. Dec (error,twoXbSqr);
  1407. End;
  1408. End;
  1409. End;
  1410. *)
  1411. procedure PatternLineDefault(x1,x2,y: integer); {$ifndef fpc}far;{$endif fpc}
  1412. {********************************************************}
  1413. { Draws a horizontal patterned line according to the }
  1414. { current Fill Settings. }
  1415. {********************************************************}
  1416. { Important notes: }
  1417. { - CurrentColor must be set correctly before entering }
  1418. { this routine. }
  1419. {********************************************************}
  1420. var
  1421. NrIterations: Integer;
  1422. i : Integer;
  1423. j : Integer;
  1424. TmpFillPattern : byte;
  1425. OldWriteMode : word;
  1426. OldCurrentColor : word;
  1427. begin
  1428. { convert to global coordinates ... }
  1429. x1 := x1 + StartXViewPort;
  1430. x2 := x2 + StartXViewPort;
  1431. y := y + StartYViewPort;
  1432. { if line was fully clipped then exit...}
  1433. if LineClipped(x1,y,x2,y,StartXViewPort,StartYViewPort,
  1434. StartXViewPort+ViewWidth, StartYViewPort+ViewHeight) then
  1435. exit;
  1436. OldWriteMode := CurrentWriteMode;
  1437. CurrentWriteMode := NormalPut;
  1438. { Get the current pattern }
  1439. TmpFillPattern := FillPatternTable
  1440. [FillSettings.Pattern][(y and $7)+1];
  1441. Case TmpFillPattern Of
  1442. 0:
  1443. begin
  1444. OldCurrentColor := CurrentColor;
  1445. CurrentColor := CurrentBkColor;
  1446. { hline converts the coordinates to global ones, but that has been done }
  1447. { already here!!! Convert them back to local ones... (JM) }
  1448. HLine(x1-StartXViewPort,x2-StartXViewPort,y-StartYViewPort);
  1449. CurrentColor := OldCurrentColor;
  1450. end;
  1451. $ff:
  1452. begin
  1453. HLine(x1-StartXViewPort,x2-StartXViewPort,y-StartYViewPort);
  1454. end;
  1455. else
  1456. begin
  1457. { number of times to go throuh the 8x8 pattern }
  1458. NrIterations := abs(x2 - x1+1) div 8;
  1459. Inc(NrIterations);
  1460. For i:= 0 to NrIterations do
  1461. Begin
  1462. for j:=0 to 7 do
  1463. Begin
  1464. { x1 mod 8 }
  1465. if RevBitArray[x1 and 7] and TmpFillPattern <> 0 then
  1466. DirectPutpixel(x1,y)
  1467. else
  1468. begin
  1469. { According to the TP graph manual, we overwrite everything }
  1470. { which is filled up - checked against VGA and CGA drivers }
  1471. { of TP. }
  1472. OldCurrentColor := CurrentColor;
  1473. CurrentColor := CurrentBkColor;
  1474. DirectPutPixel(x1,y);
  1475. CurrentColor := OldCurrentColor;
  1476. end;
  1477. Inc(x1);
  1478. if x1 > x2 then
  1479. begin
  1480. CurrentWriteMode := OldWriteMode;
  1481. exit;
  1482. end;
  1483. end;
  1484. end;
  1485. end;
  1486. End;
  1487. CurrentWriteMode := OldWriteMode;
  1488. end;
  1489. procedure LineRel(Dx, Dy: Integer);
  1490. Begin
  1491. Line(CurrentX, CurrentY, CurrentX + Dx, CurrentY + Dy);
  1492. CurrentX := CurrentX + Dx;
  1493. CurrentY := CurrentY + Dy;
  1494. end;
  1495. procedure LineTo(x,y : Integer);
  1496. Begin
  1497. Line(CurrentX, CurrentY, X, Y);
  1498. CurrentX := X;
  1499. CurrentY := Y;
  1500. end;
  1501. procedure Rectangle(x1,y1,x2,y2:integer);
  1502. begin
  1503. { Do not draw the end points }
  1504. Line(x1,y1,x2-1,y1);
  1505. Line(x1,y1+1,x1,y2);
  1506. Line(x2,y1,x2,y2-1);
  1507. Line(x1+1,y2,x2,y2);
  1508. end;
  1509. procedure GetLineSettings(var ActiveLineInfo : LineSettingsType);
  1510. begin
  1511. Activelineinfo:=Lineinfo;
  1512. end;
  1513. procedure SetLineStyle(LineStyle: word; Pattern: word; Thickness: word);
  1514. var
  1515. i: byte;
  1516. j: byte;
  1517. Begin
  1518. if (LineStyle > UserBitLn) or ((Thickness <> Normwidth) and (Thickness <> ThickWidth)) then
  1519. _GraphResult := grError
  1520. else
  1521. begin
  1522. LineInfo.Thickness := Thickness;
  1523. LineInfo.LineStyle := LineStyle;
  1524. case LineStyle of
  1525. UserBitLn: Lineinfo.Pattern := pattern;
  1526. SolidLn: Lineinfo.Pattern := $ffff; { ------- }
  1527. DashedLn : Lineinfo.Pattern := $F8F8; { -- -- --}
  1528. DottedLn: LineInfo.Pattern := $CCCC; { - - - - }
  1529. CenterLn: LineInfo.Pattern := $FC78; { -- - -- }
  1530. end; { end case }
  1531. { setup pattern styles }
  1532. j:=16;
  1533. for i:=0 to 15 do
  1534. Begin
  1535. dec(j);
  1536. { bitwise mask for each bit in the word }
  1537. if (word($01 shl i) AND LineInfo.Pattern) <> 0 then
  1538. LinePatterns[j]:=TRUE
  1539. else
  1540. LinePatterns[j]:=FALSE;
  1541. end;
  1542. end;
  1543. end;
  1544. {--------------------------------------------------------------------------}
  1545. { }
  1546. { VIEWPORT RELATED ROUTINES }
  1547. { }
  1548. {--------------------------------------------------------------------------}
  1549. Procedure ClearViewPortDefault; {$ifndef fpc}far;{$endif fpc}
  1550. var
  1551. j: integer;
  1552. OldWriteMode, OldCurColor: word;
  1553. LineSets : LineSettingsType;
  1554. Begin
  1555. { CP is always RELATIVE coordinates }
  1556. CurrentX := 0;
  1557. CurrentY := 0;
  1558. { Save all old settings }
  1559. OldCurColor := CurrentColor;
  1560. CurrentColor:=CurrentBkColor;
  1561. OldWriteMode:=CurrentWriteMode;
  1562. GetLineSettings(LineSets);
  1563. { reset to normal line style...}
  1564. SetLineStyle(SolidLn, 0, NormWidth);
  1565. { routines are relative here...}
  1566. for J:=0 to ViewHeight do
  1567. HLine(0, ViewWidth, J);
  1568. { restore old settings...}
  1569. SetLineStyle(LineSets.LineStyle, LineSets.Pattern, LineSets.Thickness);
  1570. CurrentColor := OldCurColor;
  1571. CurrentWriteMode := OldWriteMode;
  1572. end;
  1573. Procedure SetViewPort(X1, Y1, X2, Y2: Integer; Clip: Boolean);
  1574. Begin
  1575. if (X1 > GetMaxX) or (X2 > GetMaxX) or (X1 > X2) or (X1 < 0) then
  1576. Begin
  1577. _GraphResult := grError;
  1578. exit;
  1579. end;
  1580. if (Y1 > GetMaxY) or (Y2 > GetMaxY) or (Y1 > Y2) or (Y1 < 0) then
  1581. Begin
  1582. _GraphResult := grError;
  1583. exit;
  1584. end;
  1585. { CP is always RELATIVE coordinates }
  1586. CurrentX := 0;
  1587. CurrentY := 0;
  1588. StartXViewPort := X1;
  1589. StartYViewPort := Y1;
  1590. ViewWidth := X2-X1;
  1591. ViewHeight:= Y2-Y1;
  1592. ClipPixels := Clip;
  1593. end;
  1594. procedure GetViewSettings(var viewport : ViewPortType);
  1595. begin
  1596. ViewPort.X1 := StartXViewPort;
  1597. ViewPort.Y1 := StartYViewPort;
  1598. ViewPort.X2 := ViewWidth + StartXViewPort;
  1599. ViewPort.Y2 := ViewHeight + StartYViewPort;
  1600. ViewPort.Clip := ClipPixels;
  1601. end;
  1602. procedure ClearDevice;
  1603. var
  1604. ViewPort: ViewPortType;
  1605. begin
  1606. { Reset the CP }
  1607. CurrentX := 0;
  1608. CurrentY := 0;
  1609. { save viewport }
  1610. ViewPort.X1 := StartXviewPort;
  1611. ViewPort.X2 := ViewWidth - StartXViewPort;
  1612. ViewPort.Y1 := StartYViewPort;
  1613. ViewPort.Y2 := ViewHeight - StartYViewPort;
  1614. ViewPort.Clip := ClipPixels;
  1615. { put viewport to full screen }
  1616. StartXViewPort := 0;
  1617. ViewHeight := MaxY;
  1618. StartYViewPort := 0;
  1619. ViewWidth := MaxX;
  1620. ClipPixels := TRUE;
  1621. ClearViewPort;
  1622. { restore old viewport }
  1623. StartXViewPort := ViewPort.X1;
  1624. ViewWidth := ViewPort.X2-ViewPort.X1;
  1625. StartYViewPort := ViewPort.Y1;
  1626. ViewHeight := ViewPort.Y2-ViewPort.Y1;
  1627. ClipPixels := ViewPort.Clip;
  1628. end;
  1629. {--------------------------------------------------------------------------}
  1630. { }
  1631. { BITMAP PUT/GET ROUTINES }
  1632. { }
  1633. {--------------------------------------------------------------------------}
  1634. Procedure GetScanlineDefault (Y : Integer; Var Data); {$ifndef fpc}far;{$endif fpc}
  1635. {**********************************************************}
  1636. { Procedure GetScanLine() }
  1637. {----------------------------------------------------------}
  1638. { Returns the full scanline of the video line of the Y }
  1639. { coordinate. The values are returned in a WORD array }
  1640. { each WORD representing a pixel of the specified scanline }
  1641. { note: we only need the pixels inside the ViewPort! (JM) }
  1642. {**********************************************************}
  1643. Var
  1644. x : Integer;
  1645. Begin
  1646. For x:=0 to ViewWidth Do
  1647. WordArray(Data)[x]:=GetPixel(x, y);
  1648. End;
  1649. Function DefaultImageSize(X1,Y1,X2,Y2: Integer): longint; {$ifndef fpc}far;{$endif fpc}
  1650. Begin
  1651. { each pixel uses two bytes, to enable modes with colors up to 64K }
  1652. { to work. }
  1653. DefaultImageSize := 12 + (((X2-X1)*(Y2-Y1))*2);
  1654. end;
  1655. Procedure DefaultPutImage(X,Y: Integer; var Bitmap; BitBlt: Word); {$ifndef fpc}far;{$endif fpc}
  1656. type
  1657. pt = array[0..32000] of word;
  1658. ptw = array[0..3] of longint;
  1659. var
  1660. color: word;
  1661. i,j: Integer;
  1662. Y1,X1: Integer;
  1663. k: integer;
  1664. Begin
  1665. X1:= ptw(Bitmap)[0]+X; { get width and adjust end coordinate accordingly }
  1666. Y1:= ptw(Bitmap)[1]+Y; { get height and adjust end coordinate accordingly }
  1667. k:= 12; { Three reserved longs at start of bitmap }
  1668. for j:=Y to Y1 do
  1669. Begin
  1670. for i:=X to X1 do
  1671. begin
  1672. case BitBlt of
  1673. {$R-}
  1674. CopyPut: color:= pt(Bitmap)[k]; { also = normalput }
  1675. XORPut: color:= pt(Bitmap)[k] XOR GetPixel(i,j);
  1676. OrPut: color:= pt(Bitmap)[k] OR GetPixel(i,j);
  1677. AndPut: color:= pt(Bitmap)[k] AND GetPixel(i,j);
  1678. NotPut: color:= not pt(Bitmap)[k];
  1679. {$ifdef debug}
  1680. {$R+}
  1681. {$endif debug}
  1682. end;
  1683. putpixel(i,j,color);
  1684. Inc(k);
  1685. end;
  1686. end;
  1687. end;
  1688. Procedure DefaultGetImage(X1,Y1,X2,Y2: Integer; Var Bitmap); {$ifndef fpc}far;{$endif fpc}
  1689. type
  1690. pt = array[0..32000] of word;
  1691. ptw = array[0..3] of longint;
  1692. var
  1693. i,j: integer;
  1694. k: longint;
  1695. Begin
  1696. k:= 12; { Three reserved longs at start of bitmap }
  1697. for j:=Y1 to Y2 do
  1698. Begin
  1699. for i:=X1 to X2 do
  1700. begin
  1701. {$R-}
  1702. pt(Bitmap)[k] :=getpixel(i,j);
  1703. {$ifdef debug}
  1704. {$R+}
  1705. {$endif debug}
  1706. Inc(k);
  1707. end;
  1708. end;
  1709. ptw(Bitmap)[0] := X2-X1; { First longint is width }
  1710. ptw(Bitmap)[1] := Y2-Y1; { Second longint is height }
  1711. ptw(bitmap)[2] := 0; { Third longint is reserved}
  1712. end;
  1713. Procedure GetArcCoords(var ArcCoords: ArcCoordsType);
  1714. Begin
  1715. ArcCoords.X := ArcCall.X;
  1716. ArcCoords.Y := ArcCall.Y;
  1717. ArcCoords.XStart := ArcCall.XStart;
  1718. ArcCoords.YStart := ArcCall.YStart;
  1719. ArcCoords.XEnd := ArcCall.XEnd;
  1720. ArcCoords.YEnd := ArcCall.YEnd;
  1721. end;
  1722. procedure SetVisualPageDefault(page : word); {$ifndef fpc}far;{$endif fpc}
  1723. begin
  1724. end;
  1725. procedure SetActivePageDefault(page : word); {$ifndef fpc}far;{$endif fpc}
  1726. begin
  1727. end;
  1728. Procedure DefaultHooks;
  1729. {********************************************************}
  1730. { Procedure DefaultHooks() }
  1731. {--------------------------------------------------------}
  1732. { Resets all hookable routine either to nil for those }
  1733. { which need overrides, and others to defaults. }
  1734. { This is called each time SetGraphMode() is called. }
  1735. {********************************************************}
  1736. Begin
  1737. { All default hooks procedures }
  1738. { required...}
  1739. DirectPutPixel := nil;
  1740. PutPixel := nil;
  1741. GetPixel := nil;
  1742. SetRGBPalette := nil;
  1743. GetRGBPalette := nil;
  1744. { optional...}
  1745. {$ifdef fpc}
  1746. SetActivePage := @SetActivePageDefault;
  1747. SetVisualPage := @SetVisualPageDefault;
  1748. ClearViewPort := @ClearViewportDefault;
  1749. PutImage := @DefaultPutImage;
  1750. GetImage := @DefaultGetImage;
  1751. ImageSize := @DefaultImageSize;
  1752. {$else fpc}
  1753. SetActivePage := SetActivePageDefault;
  1754. SetVisualPage := SetVisualPageDefault;
  1755. ClearViewPort := ClearViewportDefault;
  1756. PutImage := DefaultPutImage;
  1757. GetImage := DefaultGetImage;
  1758. ImageSize := DefaultImageSize;
  1759. {$endif fpc}
  1760. GraphFreeMemPtr := nil;
  1761. GraphGetMemPtr := nil;
  1762. {$ifdef fpc}
  1763. GetScanLine := @GetScanLineDefault;
  1764. Line := @LineDefault;
  1765. InternalEllipse := @InternalEllipseDefault;
  1766. PatternLine := @PatternLineDefault;
  1767. HLine := @HLineDefault;
  1768. VLine := @VLineDefault;
  1769. {$else fpc}
  1770. GetScanLine := GetScanLineDefault;
  1771. Line := LineDefault;
  1772. InternalEllipse := InternalEllipseDefault;
  1773. PatternLine := PatternLineDefault;
  1774. HLine := HLineDefault;
  1775. VLine := VLineDefault;
  1776. {$endif fpc}
  1777. end;
  1778. Procedure InitVars;
  1779. {********************************************************}
  1780. { Procedure InitVars() }
  1781. {--------------------------------------------------------}
  1782. { Resets all internal variables, and resets all }
  1783. { overridable routines. }
  1784. {********************************************************}
  1785. Begin
  1786. DirectVideo := TRUE; { By default use fastest access possible }
  1787. ArcCall.X := 0;
  1788. ArcCall.Y := 0;
  1789. ArcCall.XStart := 0;
  1790. ArcCall.YStart := 0;
  1791. ArcCall.XEnd := 0;
  1792. ArcCall.YEnd := 0;
  1793. { Reset to default values }
  1794. IntCurrentMode := 0;
  1795. IntCurrentDriver := 0;
  1796. XAspect := 0;
  1797. YAspect := 0;
  1798. MaxX := 0;
  1799. MaxY := 0;
  1800. MaxColor := 0;
  1801. PaletteSize := 0;
  1802. DirectColor := FALSE;
  1803. HardwarePages := 0;
  1804. DefaultHooks;
  1805. end;
  1806. {$ifdef DPMI}
  1807. {$i vesah.inc}
  1808. {$endif DPMI}
  1809. {$i modes.inc}
  1810. {$i graph.inc}
  1811. function InstallUserDriver(Name: string; AutoDetectPtr: Pointer): integer;
  1812. begin
  1813. _graphResult := grError;
  1814. InstallUserDriver:=grError;
  1815. end;
  1816. function RegisterBGIDriver(driver: pointer): integer;
  1817. begin
  1818. _graphResult := grError;
  1819. RegisterBGIDriver:=grError;
  1820. end;
  1821. { ----------------------------------------------------------------- }
  1822. Procedure Arc(X,Y : Integer; StAngle,EndAngle,Radius: word);
  1823. { var
  1824. OldWriteMode: word;}
  1825. Begin
  1826. { Only if we are using thickwidths lines do we accept }
  1827. { XORput write modes. }
  1828. { OldWriteMode := CurrentWriteMode;
  1829. if (LineInfo.Thickness = NormWidth) then
  1830. CurrentWriteMode := NormalPut;}
  1831. {$ifdef fpc}
  1832. InternalEllipse(X,Y,Radius,Radius,StAngle,Endangle,@DummyPatternLine);
  1833. {$else fpc}
  1834. InternalEllipse(X,Y,Radius,Radius,StAngle,Endangle,DummyPatternLine);
  1835. {$endif fpc}
  1836. { CurrentWriteMode := OldWriteMode;}
  1837. end;
  1838. procedure Ellipse(X,Y : Integer; stAngle, EndAngle: word; XRadius,YRadius: word);
  1839. Begin
  1840. {$ifdef fpc}
  1841. InternalEllipse(X,Y,XRadius,YRadius,StAngle,Endangle,@DummyPatternLine);
  1842. {$else fpc}
  1843. InternalEllipse(X,Y,XRadius,YRadius,StAngle,Endangle,DummyPatternLine);
  1844. {$endif fpc}
  1845. end;
  1846. procedure FillEllipse(X, Y: Integer; XRadius, YRadius: Word);
  1847. {********************************************************}
  1848. { Procedure FillEllipse() }
  1849. {--------------------------------------------------------}
  1850. { Draws a filled ellipse using (X,Y) as a center point }
  1851. { and XRadius and YRadius as the horizontal and vertical }
  1852. { axes. The ellipse is filled with the current fill color}
  1853. { and fill style, and is bordered with the current color.}
  1854. {********************************************************}
  1855. begin
  1856. InternalEllipse(X,Y,XRadius,YRadius,0,360,PatternLine)
  1857. end;
  1858. procedure Circle(X, Y: Integer; Radius:Word);
  1859. {********************************************************}
  1860. { Draws a circle centered at X,Y with the given Radius. }
  1861. {********************************************************}
  1862. { Important notes: }
  1863. { - Thickwidth circles use the current write mode, while}
  1864. { normal width circles ALWAYS use CopyPut/NormalPut }
  1865. { mode. (Tested against VGA BGI driver -CEC 13/Aug/99 }
  1866. {********************************************************}
  1867. var OriginalArcInfo: ArcCoordsType;
  1868. OldWriteMode: word;
  1869. begin
  1870. if (Radius = 0) then
  1871. Exit;
  1872. if (Radius = 1) then
  1873. begin
  1874. { only normal put mode is supported by a call to PutPixel }
  1875. PutPixel(X, Y, CurrentColor);
  1876. Exit;
  1877. end;
  1878. { save state of arc information }
  1879. { because it is not needed for }
  1880. { a circle call. }
  1881. move(ArcCall,OriginalArcInfo, sizeof(ArcCall));
  1882. if LineInfo.Thickness = Normwidth then
  1883. begin
  1884. OldWriteMode := CurrentWriteMode;
  1885. CurrentWriteMode := CopyPut;
  1886. end;
  1887. {$ifdef fpc}
  1888. InternalEllipse(X,Y,Radius,Radius,0,360,@DummyPatternLine);
  1889. {$else fpc}
  1890. InternalEllipse(X,Y,Radius,Radius,0,360,DummyPatternLine);
  1891. {$endif fpc}
  1892. if LineInfo.Thickness = Normwidth then
  1893. CurrentWriteMode := OldWriteMode;
  1894. { restore arc information }
  1895. move(OriginalArcInfo, ArcCall,sizeof(ArcCall));
  1896. end;
  1897. procedure SectorPL(x1,x2,y: Integer); {$ifndef fpc}far;{$endif fpc}
  1898. var plx1, plx2: integer;
  1899. {$ifdef sectorpldebug}
  1900. t : text;
  1901. {$endif sectorpldebug}
  1902. begin
  1903. {$ifdef sectorpldebug}
  1904. assign(t,'sector.log');
  1905. append(t);
  1906. writeln(t,'Got here for line ',y);
  1907. close(t);
  1908. {$endif sectorpldebug}
  1909. If (x1 = -maxint) Then
  1910. If (x2 = maxint-1) Then
  1911. { no ellipse points drawn on this line }
  1912. If (((Y < ArcCall.Y) and (Y > ArcCall.YStart)) or
  1913. ((Y > ArcCall.Y) and (Y < ArcCall.YStart))) Then
  1914. { there is a part of the sector at this y coordinate, but no }
  1915. { ellips points are plotted on this line, so draw a patternline }
  1916. { between the lines connecting (arccall.x,arccall.y) with }
  1917. { the start and the end of the arc (JM) }
  1918. { use: y-y1=(y2-y1)/(x2-x1)*(x-x1) => }
  1919. { x = (y-y1)/(y2-y1)*(x2-x1)+x1 }
  1920. Begin
  1921. {$ifdef sectorpldebug}
  1922. If (ArcCall.YStart-ArcCall.Y) = 0 then
  1923. begin
  1924. append(t);
  1925. writeln(t,'bug1');
  1926. close(t);
  1927. runerror(202);
  1928. end;
  1929. {$endif sectorpldebug}
  1930. plx1 := (y-ArcCall.Y)*(ArcCall.XStart-ArcCall.X)
  1931. div (ArcCall.YStart-ArcCall.Y)+ArcCall.X;
  1932. {$ifdef sectorpldebug}
  1933. If (ArcCall.YEnd-ArcCall.Y) = 0 then
  1934. begin
  1935. append(t);
  1936. writeln(t,'bug2');
  1937. close(t);
  1938. runerror(202);
  1939. end;
  1940. {$endif sectorpldebug}
  1941. plx2 := (y-ArcCall.Y)*(ArcCall.XEnd-ArcCall.X)
  1942. div (ArcCall.YEnd-ArcCall.Y)+ArcCall.X;
  1943. If plx1 > plx2 then
  1944. begin
  1945. plx1 := plx1 xor plx2;
  1946. plx2 := plx1 xor plx2;
  1947. plx1 := plx1 xor plx2;
  1948. end;
  1949. {$ifdef sectorpldebug}
  1950. append(t);
  1951. writeln(t,'lines: ',plx1,' - ',plx2);
  1952. close(t);
  1953. {$endif sectorpldebug}
  1954. End
  1955. { otherwise two points which have nothing to do with the sector }
  1956. Else exit
  1957. Else
  1958. { the arc is plotted at the right side, but not at the left side, }
  1959. { fill till the line between (ArcCall.X,ArcCall.Y) and }
  1960. { (ArcCall.XStart,ArcCall.YStart) }
  1961. Begin
  1962. If (y < ArcCall.Y) then
  1963. begin
  1964. {$ifdef sectorpldebug}
  1965. If (ArcCall.YEnd-ArcCall.Y) = 0 then
  1966. begin
  1967. append(t);
  1968. writeln(t,'bug3');
  1969. close(t);
  1970. runerror(202);
  1971. end;
  1972. {$endif sectorpldebug}
  1973. plx1 := (y-ArcCall.Y)*(ArcCall.XEnd-ArcCall.X)
  1974. div (ArcCall.YEnd-ArcCall.Y)+ArcCall.X
  1975. end
  1976. else if (y > ArcCall.Y) then
  1977. begin
  1978. {$ifdef sectorpldebug}
  1979. If (ArcCall.YStart-ArcCall.Y) = 0 then
  1980. begin
  1981. append(t);
  1982. writeln(t,'bug4');
  1983. close(t);
  1984. runerror(202);
  1985. end;
  1986. {$endif sectorpldebug}
  1987. plx1 := (y-ArcCall.Y)*(ArcCall.XStart-ArcCall.X)
  1988. div (ArcCall.YStart-ArcCall.Y)+ArcCall.X
  1989. end
  1990. else plx1 := ArcCall.X;
  1991. plx2 := x2;
  1992. {$ifdef sectorpldebug}
  1993. append(t);
  1994. writeln(t,'right: ',plx1,' - ',plx2);
  1995. close(t);
  1996. {$endif sectorpldebug}
  1997. End
  1998. Else
  1999. If (x2 = maxint-1) Then
  2000. { the arc is plotted at the left side, but not at the rigth side. }
  2001. { the right limit can be either the first or second line. Just take }
  2002. { the closest one, but watch out for division by zero! }
  2003. Begin
  2004. If (y < ArcCall.Y) then
  2005. begin
  2006. {$ifdef sectorpldebug}
  2007. If (ArcCall.YStart-ArcCall.Y) = 0 then
  2008. begin
  2009. append(t);
  2010. writeln(t,'bug5');
  2011. close(t);
  2012. runerror(202);
  2013. end;
  2014. {$endif sectorpldebug}
  2015. plx2 := (y-ArcCall.Y)*(ArcCall.XStart-ArcCall.X)
  2016. div (ArcCall.YStart-ArcCall.Y)+ArcCall.X
  2017. end
  2018. else if (y > ArcCall.Y) then
  2019. begin
  2020. {$ifdef sectorpldebug}
  2021. If (ArcCall.YEnd-ArcCall.Y) = 0 then
  2022. begin
  2023. append(t);
  2024. writeln(t,'bug6');
  2025. close(t);
  2026. runerror(202);
  2027. end;
  2028. {$endif sectorpldebug}
  2029. plx2 := (y-ArcCall.Y)*(ArcCall.XEnd-ArcCall.X)
  2030. div (ArcCall.YEnd-ArcCall.Y)+ArcCall.X
  2031. end
  2032. else plx2 := ArcCall.X;
  2033. plx1 := x1;
  2034. {$ifdef sectorpldebug}
  2035. append(t);
  2036. writeln(t,'left: ',plx1,' - ',plx2);
  2037. close(t);
  2038. {$endif sectorpldebug}
  2039. End
  2040. Else
  2041. { the arc is plotted at both sides }
  2042. Begin
  2043. plx1 := x1;
  2044. plx2 := x2;
  2045. {$ifdef sectorpldebug}
  2046. append(t);
  2047. writeln(t,'normal: ',plx1,' - ',plx2);
  2048. close(t);
  2049. {$endif sectorpldebug}
  2050. End;
  2051. If plx2 > plx1 then
  2052. Begin
  2053. {$ifdef sectorpldebug}
  2054. append(t);
  2055. Writeln(t,'drawing...');
  2056. close(t);
  2057. {$endif sectorpldebug}
  2058. PatternLine(plx1,plx2,y);
  2059. end;
  2060. end;
  2061. procedure Sector(x, y: Integer; StAngle,EndAngle, XRadius, YRadius: Word);
  2062. (* var angle : graph_float;
  2063. writemode : word; *)
  2064. begin
  2065. {$ifdef fpc}
  2066. internalellipse(x,y,XRadius, YRadius, StAngle, EndAngle, @SectorPL);
  2067. {$else fpc}
  2068. internalellipse(x,y,XRadius, YRadius, StAngle, EndAngle, SectorPL);
  2069. {$endif fpc}
  2070. Line(ArcCall.XStart, ArcCall.YStart, x,y);
  2071. Line(x,y,ArcCall.Xend,ArcCall.YEnd);
  2072. end;
  2073. procedure SetFillStyle(Pattern : word; Color: word);
  2074. begin
  2075. { on invalid input, the current fill setting will be }
  2076. { unchanged. }
  2077. if (Pattern > UserFill) or (Color > GetMaxColor) then
  2078. begin
  2079. _GraphResult := grError;
  2080. exit;
  2081. end;
  2082. FillSettings.Color := Color;
  2083. FillSettings.Pattern := Pattern;
  2084. end;
  2085. procedure SetFillPattern(Pattern: FillPatternType; Color: word);
  2086. {********************************************************}
  2087. { Changes the Current FillPattern to a user defined }
  2088. { pattern and changes also the current fill color. }
  2089. { The FillPattern is saved in the FillPattern array so }
  2090. { it can still be used with SetFillStyle(UserFill,Color) }
  2091. {********************************************************}
  2092. var
  2093. i: integer;
  2094. begin
  2095. if Color > GetMaxColor then
  2096. begin
  2097. _GraphResult := grError;
  2098. exit;
  2099. end;
  2100. FillSettings.Color := Color;
  2101. FillSettings.Pattern := UserFill;
  2102. { Save the pattern in the buffer }
  2103. For i:=1 to 8 do
  2104. FillPatternTable[UserFill][i] := Pattern[i];
  2105. end;
  2106. procedure Bar(x1,y1,x2,y2:integer);
  2107. {********************************************************}
  2108. { Important notes for compatibility with BP: }
  2109. { - WriteMode is always CopyPut }
  2110. { - No contour is drawn for the lines }
  2111. {********************************************************}
  2112. var y : Integer;
  2113. origcolor : longint;
  2114. origlinesettings: Linesettingstype;
  2115. origwritemode : Integer;
  2116. begin
  2117. origlinesettings:=lineinfo;
  2118. origcolor:=CurrentColor;
  2119. if y1>y2 then
  2120. begin
  2121. y:=y1;
  2122. y1:=y2;
  2123. y2:=y;
  2124. end;
  2125. { Always copy mode for Bars }
  2126. origwritemode := CurrentWriteMode;
  2127. CurrentWriteMode := CopyPut;
  2128. { All lines used are of this style }
  2129. Lineinfo.linestyle:=solidln;
  2130. Lineinfo.thickness:=normwidth;
  2131. case Fillsettings.pattern of
  2132. EmptyFill :
  2133. begin
  2134. Currentcolor:=CurrentBkColor;
  2135. for y:=y1 to y2 do
  2136. Hline(x1,x2,y);
  2137. end;
  2138. SolidFill :
  2139. begin
  2140. CurrentColor:=FillSettings.color;
  2141. for y:=y1 to y2 do
  2142. Hline(x1,x2,y);
  2143. end;
  2144. else
  2145. Begin
  2146. CurrentColor:=FillSettings.color;
  2147. for y:=y1 to y2 do
  2148. patternline(x1,x2,y);
  2149. end;
  2150. end;
  2151. CurrentColor:= Origcolor;
  2152. LineInfo := OrigLineSettings;
  2153. CurrentWriteMode := OrigWritemode;
  2154. end;
  2155. procedure bar3D(x1, y1, x2, y2 : integer;depth : word;top : boolean);
  2156. var
  2157. origwritemode : integer;
  2158. OldX, OldY : integer;
  2159. begin
  2160. origwritemode := CurrentWriteMode;
  2161. CurrentWriteMode := CopyPut;
  2162. Bar(x1,y1,x2,y2);
  2163. Rectangle(x1,y1,x2,y2);
  2164. { Current CP should not be updated in Bar3D }
  2165. { therefore save it and then restore it on }
  2166. { exit. }
  2167. OldX := CurrentX;
  2168. OldY := CurrentY;
  2169. if top then begin
  2170. Moveto(x1,y1);
  2171. Lineto(x1+depth,y1-depth);
  2172. Lineto(x2+depth,y1-depth);
  2173. Lineto(x2,y1);
  2174. end;
  2175. if Depth <> 0 then
  2176. Begin
  2177. Moveto(x2+depth,y1-depth);
  2178. Lineto(x2+depth,y2-depth);
  2179. Lineto(x2,y2);
  2180. end;
  2181. { restore CP }
  2182. CurrentX := OldX;
  2183. CurrentY := OldY;
  2184. CurrentWriteMode := origwritemode;
  2185. end;
  2186. {--------------------------------------------------------------------------}
  2187. { }
  2188. { COLOR AND PALETTE ROUTINES }
  2189. { }
  2190. {--------------------------------------------------------------------------}
  2191. {$i palette.inc}
  2192. procedure SetColor(Color: Word);
  2193. Begin
  2194. CurrentColor := Color;
  2195. end;
  2196. function GetColor: Word;
  2197. Begin
  2198. GetColor := CurrentColor;
  2199. end;
  2200. function GetBkColor: Word;
  2201. Begin
  2202. GetBkColor := CurrentBkColor;
  2203. end;
  2204. procedure SetBkColor(ColorNum: Word);
  2205. { Background color means background screen color in this case, and it is }
  2206. { INDEPENDANT of the viewport settings, so we must clear the whole screen }
  2207. { with the color. }
  2208. var
  2209. ViewPort: ViewportType;
  2210. Begin
  2211. GetViewSettings(Viewport);
  2212. SetViewPort(0,0,MaxX,MaxY,FALSE);
  2213. CurrentBkColor := ColorNum;
  2214. ClearViewPort;
  2215. SetViewport(ViewPort.X1,Viewport.Y1,Viewport.X2,Viewport.Y2,Viewport.Clip);
  2216. end;
  2217. function GetMaxColor: word;
  2218. { Checked against TP VGA driver - CEC }
  2219. begin
  2220. GetMaxColor:=MaxColor-1; { based on an index of zero so subtract one }
  2221. end;
  2222. Procedure MoveRel(Dx, Dy: Integer);
  2223. Begin
  2224. CurrentX := CurrentX + Dx;
  2225. CurrentY := CurrentY + Dy;
  2226. end;
  2227. Procedure MoveTo(X,Y: Integer);
  2228. {********************************************************}
  2229. { Procedure MoveTo() }
  2230. {--------------------------------------------------------}
  2231. { Moves the current pointer in VIEWPORT relative }
  2232. { coordinates to the specified X,Y coordinate. }
  2233. {********************************************************}
  2234. Begin
  2235. CurrentX := X;
  2236. CurrentY := Y;
  2237. end;
  2238. function GraphErrorMsg(ErrorCode: Integer): string;
  2239. Begin
  2240. GraphErrorMsg:='';
  2241. case ErrorCode of
  2242. grOk,grFileNotFound,grInvalidDriver: exit;
  2243. grNoInitGraph: GraphErrorMsg:='Graphics driver not installed';
  2244. grNotDetected: GraphErrorMsg:='Graphics hardware not detected';
  2245. grNoLoadMem,grNoScanMem,grNoFloodMem: GraphErrorMsg := 'Not enough memory for graphics';
  2246. grNoFontMem: GraphErrorMsg := 'Not enough memory to load font';
  2247. grFontNotFound: GraphErrorMsg:= 'Font file not found';
  2248. grInvalidMode: GraphErrorMsg := 'Invalid graphics mode';
  2249. grError: GraphErrorMsg:='Graphics error';
  2250. grIoError: GraphErrorMsg:='Graphics I/O error';
  2251. grInvalidFont,grInvalidFontNum: GraphErrorMsg := 'Invalid font';
  2252. grInvalidVersion: GraphErrorMsg:='Invalid driver version';
  2253. end;
  2254. end;
  2255. Function GetMaxX: Integer;
  2256. { Routine checked against VGA driver - CEC }
  2257. Begin
  2258. GetMaxX := MaxX;
  2259. end;
  2260. Function GetMaxY: Integer;
  2261. { Routine checked against VGA driver - CEC }
  2262. Begin
  2263. GetMaxY := MaxY;
  2264. end;
  2265. Function GraphResult: Integer;
  2266. Begin
  2267. GraphResult := _GraphResult;
  2268. _GraphResult := grOk;
  2269. end;
  2270. Function GetX: Integer;
  2271. Begin
  2272. GetX := CurrentX;
  2273. end;
  2274. Function GetY: Integer;
  2275. Begin
  2276. GetY := CurrentY;
  2277. end;
  2278. Function GetDriverName: string;
  2279. begin
  2280. GetDriverName:=DriverName;
  2281. end;
  2282. procedure graphdefaults;
  2283. { PS: GraphDefaults does not ZERO the ArcCall structure }
  2284. { so a call to GetArcCoords will not change even the }
  2285. { returned values even if GraphDefaults is called in }
  2286. { between. }
  2287. var
  2288. i: integer;
  2289. begin
  2290. lineinfo.linestyle:=solidln;
  2291. lineinfo.thickness:=normwidth;
  2292. { reset line style pattern }
  2293. for i:=0 to 15 do
  2294. LinePatterns[i] := TRUE;
  2295. { By default, according to the TP prog's reference }
  2296. { the default pattern is solid, and the default }
  2297. { color is the maximum color in the palette. }
  2298. fillsettings.color:=GetMaxColor;
  2299. fillsettings.pattern:=solidfill;
  2300. { GraphDefaults resets the User Fill pattern to $ff }
  2301. { checked with VGA BGI driver - CEC }
  2302. for i:=1 to 8 do
  2303. FillPatternTable[UserFill][i] := $ff;
  2304. CurrentColor:=white;
  2305. SetBkColor(Black);
  2306. ClipPixels := TRUE;
  2307. { Reset the viewport }
  2308. StartXViewPort := 0;
  2309. StartYViewPort := 0;
  2310. ViewWidth := MaxX;
  2311. ViewHeight := MaxY;
  2312. { Reset CP }
  2313. CurrentX := 0;
  2314. CurrentY := 0;
  2315. { normal write mode }
  2316. CurrentWriteMode := CopyPut;
  2317. { Schriftart einstellen }
  2318. CurrentTextInfo.font := DefaultFont;
  2319. CurrentTextInfo.direction:=HorizDir;
  2320. CurrentTextInfo.charsize:=1;
  2321. CurrentTextInfo.horiz:=LeftText;
  2322. CurrentTextInfo.vert:=TopText;
  2323. XAspect:=10000; YAspect:=10000;
  2324. end;
  2325. procedure GetAspectRatio(var Xasp,Yasp : word);
  2326. begin
  2327. XAsp:=XAspect;
  2328. YAsp:=YAspect;
  2329. end;
  2330. procedure SetAspectRatio(Xasp, Yasp : word);
  2331. begin
  2332. Xaspect:= XAsp;
  2333. YAspect:= YAsp;
  2334. end;
  2335. procedure SetWriteMode(WriteMode : integer);
  2336. { TP sets the writemodes according to the following scheme (JM) }
  2337. begin
  2338. Case writemode of
  2339. xorput, andput: CurrentWriteMode := XorPut;
  2340. notput, orput, copyput: CurrentWriteMode := CopyPut;
  2341. End;
  2342. end;
  2343. procedure GetFillSettings(var Fillinfo:Fillsettingstype);
  2344. begin
  2345. Fillinfo:=Fillsettings;
  2346. end;
  2347. procedure GetFillPattern(var FillPattern:FillPatternType);
  2348. begin
  2349. FillPattern:=FillpatternTable[UserFill];
  2350. end;
  2351. procedure DrawPoly(numpoints : word;var polypoints);
  2352. type
  2353. ppointtype = ^pointtype;
  2354. pt = array[0..16000] of pointtype;
  2355. var
  2356. i : longint;
  2357. begin
  2358. if numpoints < 2 then
  2359. begin
  2360. _GraphResult := grError;
  2361. exit;
  2362. end;
  2363. for i:=0 to numpoints-2 do
  2364. line(pt(polypoints)[i].x,
  2365. pt(polypoints)[i].y,
  2366. pt(polypoints)[i+1].x,
  2367. pt(polypoints)[i+1].y);
  2368. end;
  2369. procedure PieSlice(X,Y,stangle,endAngle:integer;Radius: Word);
  2370. begin
  2371. Sector(x,y,stangle,endangle,radius,radius);
  2372. end;
  2373. {$i fills.inc}
  2374. {$i gtext.inc}
  2375. procedure DetectGraph(var GraphDriver:Integer;var GraphMode:Integer);
  2376. var LoMode, HiMode: Integer;
  2377. CpyMode: Integer;
  2378. CpyDriver: Integer;
  2379. begin
  2380. HiMode := -1;
  2381. LoMode := -1;
  2382. { We start at VGA }
  2383. GraphDriver := VGA;
  2384. CpyMode := 0;
  2385. { search all possible graphic drivers in ascending order...}
  2386. { usually the new driver numbers indicate newest hardware...}
  2387. { Internal driver numbers start at VGA=9 }
  2388. repeat
  2389. GetModeRange(GraphDriver,LoMode,HiMode);
  2390. { save the highest mode possible...}
  2391. if HiMode = -1 then break;
  2392. CpyMode:=HiMode;
  2393. CpyDriver:=GraphDriver;
  2394. { go to next driver if it exists...}
  2395. Inc(GraphDriver);
  2396. until (CpyMode=-1);
  2397. { If this is equal to -1 then no graph mode possible...}
  2398. if CpyMode = -1 then
  2399. begin
  2400. _GraphResult := grNotDetected;
  2401. exit;
  2402. end;
  2403. GraphDriver := CpyDriver;
  2404. GraphMode := CpyMode;
  2405. end;
  2406. procedure InitGraph(var GraphDriver:Integer;var GraphMode:Integer;
  2407. const PathToDriver:String);
  2408. begin
  2409. { path to the fonts (where they will be searched)...}
  2410. bgipath:=PathToDriver;
  2411. if bgipath[length(bgipath)]<>'\' then
  2412. bgipath:=bgipath+'\';
  2413. if not assigned(SaveVideoState) then
  2414. RunError(216);
  2415. {$ifdef logging}
  2416. LogLn('Calling SaveVideoState at '+strf(longint(savevideostate)));
  2417. {$endif logging}
  2418. SaveVideoState;
  2419. InitVars;
  2420. DriverName:=InternalDriverName; { DOS Graphics driver }
  2421. if (Graphdriver=Detect) then
  2422. begin
  2423. DetectGraph(GraphDriver,GraphMode);
  2424. If _GraphResult = grNotDetected then Exit;
  2425. IntCurrentDriver := GraphDriver;
  2426. { Actually set the graph mode...}
  2427. SetGraphMode(GraphMode);
  2428. end
  2429. else
  2430. begin
  2431. { Search if that graphics modec actually exists...}
  2432. if SearchMode(GraphDriver,GraphMode) = nil then
  2433. begin
  2434. _GraphResult := grInvalidMode;
  2435. exit;
  2436. end
  2437. else
  2438. begin
  2439. IntCurrentDriver := GraphDriver;
  2440. SetGraphMode(GraphMode);
  2441. end;
  2442. end;
  2443. end;
  2444. procedure SetDirectVideo(DirectAccess: boolean);
  2445. begin
  2446. DirectVideo := DirectAccess;
  2447. end;
  2448. function GetDirectVideo: boolean;
  2449. begin
  2450. GetDirectVideo := DirectVideo;
  2451. end;
  2452. procedure GraphExitProc; {$ifndef fpc} far; {$endif fpc}
  2453. { deallocates all memory allocated by the graph unit }
  2454. var
  2455. list: PModeInfo;
  2456. tmp : PModeInfo;
  2457. c: graph_int;
  2458. begin
  2459. { restore old exitproc! }
  2460. exitproc := exitsave;
  2461. {$ifdef testsave}
  2462. restorevideostate;
  2463. {$endif testsave}
  2464. { release memory allocated for fonts }
  2465. for c := 1 to installedfonts do
  2466. with fonts[c] Do
  2467. If assigned(instr) Then
  2468. Freemem(instr,instrlength);
  2469. { release memory allocated for modelist }
  2470. list := ModeList;
  2471. while assigned(list) do
  2472. begin
  2473. tmp := list;
  2474. list:=list^.next;
  2475. dispose(tmp);
  2476. end;
  2477. {$IFDEF DPMI}
  2478. { We had copied the buffer of mode information }
  2479. { and allocated it dynamically... now free it }
  2480. { Warning: if GetVESAInfo returned false, this buffer is not allocated! (JM)}
  2481. If hasVesa then
  2482. Dispose(VESAInfo.ModeList);
  2483. {$ENDIF}
  2484. end;
  2485. begin
  2486. {$ifdef logging}
  2487. assign(debuglog,'grlog.txt');
  2488. rewrite(debuglog);
  2489. close(debuglog);
  2490. {$endif logging}
  2491. isgraphmode := false;
  2492. ModeList := nil;
  2493. SaveVideoState := nil;
  2494. RestoreVideoState := nil;
  2495. SavePtr := Nil;
  2496. { This must be called at startup... because GetGraphMode may }
  2497. { be called even when not in graph mode. }
  2498. {$ifdef logging}
  2499. LogLn('Calling QueryAdapterInfo...');
  2500. {$endif logging}
  2501. QueryAdapterInfo;
  2502. { Install standard fonts }
  2503. { This is done BEFORE startup... }
  2504. InstalledFonts := 0;
  2505. InstallUserFont('TRIP');
  2506. InstallUserFont('LITT');
  2507. InstallUserFont('SANS');
  2508. InstallUserFont('GOTH');
  2509. { This installs an exit procedure which cleans up the mode list...}
  2510. ExitSave := ExitProc;
  2511. ExitProc := @GraphExitProc;
  2512. {$ifdef testsave}
  2513. savevideostate;
  2514. {$endif testsave}
  2515. end.
  2516. SetGraphBufSize
  2517. {
  2518. $Log$
  2519. Revision 1.32 1999-09-28 15:07:46 jonas
  2520. * fix for disposing font data because it can contain #0 chars
  2521. Revision 1.31 1999/09/28 13:56:25 jonas
  2522. * reordered some local variables (first 4 byte vars, then 2 byte vars
  2523. etc)
  2524. * font data is now disposed in exitproc, exitproc is now called
  2525. GraphExitProc (was CleanModes) and resides in graph.pp instead of in
  2526. modes.inc
  2527. Revision 1.30 1999/09/27 23:34:41 peter
  2528. * new graph unit is default for go32v2
  2529. * removed warnings/notes
  2530. Revision 1.29 1999/09/26 13:31:06 jonas
  2531. * changed name of modeinfo variable to vesamodeinfo and fixed
  2532. associated errors (fillchar(modeinfo,sizeof(tmodeinfo),#0) instead
  2533. of sizeof(TVesamodeinfo) etc)
  2534. * changed several sizeof(type) to sizeof(varname) to avoid similar
  2535. errors in the future
  2536. Revision 1.28 1999/09/25 11:48:43 jonas
  2537. + detectgraph
  2538. * small change to internalellipsedefault so less pixels are
  2539. calculated twice
  2540. * some small corrections to graph.tex
  2541. Revision 1.27 1999/09/24 22:52:38 jonas
  2542. * optimized patternline a bit (always use hline when possible)
  2543. * isgraphmode stuff cleanup
  2544. * vesainfo.modelist now gets disposed in cleanmode instead of in
  2545. closegraph (required moving of some declarations from vesa.inc to
  2546. new vesah.inc)
  2547. * queryadapter gets no longer called from initgraph (is called from
  2548. initialization of graph unit)
  2549. * bugfix for notput in 32k and 64k vesa modes
  2550. * a div replaced by / in fillpoly
  2551. Revision 1.26 1999/09/22 13:13:35 jonas
  2552. * renamed text.inc -> gtext.inc to avoid conflict with system unit
  2553. * fixed textwidth
  2554. * isgraphmode now gets properly updated, so mode restoring works
  2555. again
  2556. Revision 1.25 1999/09/18 22:21:10 jonas
  2557. + hlinevesa256 and vlinevesa256
  2558. + support for not/xor/or/andput in vesamodes with 32k/64k colors
  2559. * lots of changes to avoid warnings under FPC
  2560. Revision 1.24 1999/09/18 16:03:37 jonas
  2561. * graph.pp: removed pieslice and sector from ToDo list
  2562. * closegraph: exits now immidiately if isgraphmode = false (caused
  2563. RTE 204 with VESA enabled if you set exitproc to call closegraph
  2564. and also called closegraph explicitely before exit, like bgidemo)
  2565. Revision 1.23 1999/09/17 13:58:31 jonas
  2566. * another fix for a case where internalellipsedefault went haywire
  2567. * sector() and pieslice() fully implemented!
  2568. * small change to prevent buffer overflow with floodfill
  2569. Revision 1.22 1999/09/15 13:37:50 jonas
  2570. * small change to internalellipsedef to be TP compatible
  2571. * fixed directputpixel for vga 320*200*256
  2572. Revision 1.21 1999/09/13 12:49:08 jonas
  2573. * fixed Arc: internallellipse went into an endless loop if StAngle =
  2574. EndAngle
  2575. * FillEllipse is now much faster: no more floodfill,
  2576. InternalEllipseDefault now draws the patternlines immediatety!
  2577. Revision 1.20 1999/09/12 17:29:00 jonas
  2578. * several changes to internalellipse to make it faster
  2579. and to make sure it updates the ArcCall correctly
  2580. (not yet done for width = 3)
  2581. * Arc mostly works now, only sometimes an endless loop, don't know
  2582. why
  2583. Revision 1.19 1999/09/11 19:43:01 jonas
  2584. * FloodFill: did not take into account current viewport settings
  2585. * GetScanLine: only get line inside viewport, data outside of it
  2586. is not used anyway
  2587. * InternalEllipseDefault: fix for when xradius or yradius = 0 and
  2588. increase xradius and yradius always by one (TP does this too)
  2589. * fixed conlict in vesa.inc from last update
  2590. * some conditionals to avoid range check and overflow errors in
  2591. places where it doesn't matter
  2592. Revision 1.18 1999/07/26 09:38:41 florian
  2593. * bar: y2 can be less y1, fixed
  2594. * settextstyle: charsize can be 0, must be changed into 1
  2595. Revision 1.17 1999/07/18 15:07:20 jonas
  2596. + xor-, and and- orput support for VESA256 modes
  2597. * compile with -dlogging if you wnt some info to be logged to grlog.txt
  2598. Revision 1.16 1999/07/14 18:18:04 florian
  2599. * cosmetic changes
  2600. }