graph.pp 97 KB

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