ID_RF.C 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846
  1. /* Catacomb Armageddon Source Code
  2. * Copyright (C) 1993-2014 Flat Rock Software
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. // ID_RF.C
  19. /*
  20. =============================================================================
  21. notes
  22. -----
  23. scrolling more than one tile / refresh forces a total redraw
  24. two overlapping sprites of equal priority can change drawing order when
  25. updated
  26. =============================================================================
  27. */
  28. #include "ID_HEADS.H"
  29. #pragma hdrstop
  30. /*
  31. =============================================================================
  32. LOCAL CONSTANTS
  33. =============================================================================
  34. */
  35. #define SCREENTILESWIDE 20
  36. #define SCREENTILESHIGH 13
  37. #define SCREENSPACE (SCREENWIDTH*240)
  38. #define FREEEGAMEM (0x10000l-3l*SCREENSPACE)
  39. //
  40. // the update array must have enough space for two screens that can float
  41. // up two two tiles each way
  42. //
  43. // (PORTTILESWIDE+1)*PORTTILESHIGH must be even so the arrays can be cleared
  44. // by word width instructions
  45. #define UPDATESCREENSIZE (UPDATEWIDE*PORTTILESHIGH+2)
  46. #define UPDATESPARESIZE (UPDATEWIDE*2+4)
  47. #define UPDATESIZE (UPDATESCREENSIZE+2*UPDATESPARESIZE)
  48. #define G_EGASX_SHIFT 7 // global >> ?? = screen x
  49. #define G_CGASX_SHIFT 6 // global >> ?? = screen x
  50. #define G_SY_SHIFT 4 // global >> ?? = screen y
  51. unsigned SX_T_SHIFT; // screen x >> ?? = tile EGA = 1, CGA = 2;
  52. #define SY_T_SHIFT 4 // screen y >> ?? = tile
  53. #define EGAPORTSCREENWIDE 42
  54. #define CGAPORTSCREENWIDE 84
  55. #define PORTSCREENHIGH 224
  56. #define UPDATESCREENSIZE (UPDATEWIDE*PORTTILESHIGH+2)
  57. #define UPDATESPARESIZE (UPDATEWIDE*2+4)
  58. #define UPDATESIZE (UPDATESCREENSIZE+2*UPDATESPARESIZE)
  59. #define MAXSCROLLEDGES 6
  60. /*
  61. =============================================================================
  62. LOCAL TYPES
  63. =============================================================================
  64. */
  65. typedef struct spriteliststruct
  66. {
  67. int screenx,screeny;
  68. int width,height;
  69. unsigned grseg,sourceofs,planesize;
  70. drawtype draw;
  71. unsigned tilex,tiley,tilewide,tilehigh;
  72. int priority,updatecount;
  73. struct spriteliststruct **prevptr,*nextsprite;
  74. } spritelisttype;
  75. typedef struct
  76. {
  77. int screenx,screeny;
  78. int width,height;
  79. } eraseblocktype;
  80. typedef struct
  81. {
  82. unsigned current; // foreground tiles have high bit set
  83. int count;
  84. } tiletype;
  85. typedef struct animtilestruct
  86. {
  87. unsigned x,y,tile;
  88. tiletype *chain;
  89. unsigned far *mapplane;
  90. struct animtilestruct **prevptr,*nexttile;
  91. } animtiletype;
  92. /*
  93. =============================================================================
  94. GLOBAL VARIABLES
  95. =============================================================================
  96. */
  97. unsigned tics;
  98. long lasttimecount;
  99. boolean compatability; // crippled refresh for wierdo SVGAs
  100. unsigned mapwidth,mapheight,mapbyteswide,mapwordswide
  101. ,mapbytesextra,mapwordsextra;
  102. unsigned mapbwidthtable[MAXMAPHEIGHT];
  103. //
  104. // Global : Actor coordinates are in this, at 1/16 th of a pixel, to allow
  105. // for fractional movement and acceleration.
  106. //
  107. // Tiles : Tile offsets from the upper left corner of the current map.
  108. //
  109. // Screen : Graphics level offsets from map origin, x in bytes, y in pixels.
  110. // originxscreen is the same spot as originxtile, just with extra precision
  111. // so graphics don't need to be done in tile boundaries.
  112. //
  113. unsigned originxglobal,originyglobal;
  114. unsigned originxtile,originytile;
  115. unsigned originxscreen,originyscreen;
  116. unsigned originmap;
  117. unsigned originxmin,originxmax,originymin,originymax;
  118. unsigned masterofs;
  119. //
  120. // Table of the offsets from bufferofs of each tile spot in the
  121. // view port. The extra wide tile should never be drawn, but the space
  122. // is needed to account for the extra 0 in the update arrays. Built by
  123. // RF_Startup
  124. //
  125. unsigned blockstarts[UPDATEWIDE*UPDATEHIGH];
  126. unsigned updatemapofs[UPDATEWIDE*UPDATEHIGH];
  127. unsigned uwidthtable[PORTTILESHIGH]; // lookup instead of multiply
  128. byte update[2][UPDATESIZE];
  129. byte *updateptr,*baseupdateptr, // current start of update window
  130. *updatestart[2],
  131. *baseupdatestart[2];
  132. /*
  133. =============================================================================
  134. LOCAL VARIABLES
  135. =============================================================================
  136. */
  137. static char scratch[20],str[80];
  138. tiletype allanims[MAXANIMTYPES];
  139. unsigned numanimchains;
  140. void (*refreshvector) (void);
  141. unsigned screenstart[3] =
  142. {0,SCREENSPACE,SCREENSPACE*2};
  143. unsigned xpanmask; // prevent panning to odd pixels
  144. unsigned screenpage; // screen currently being displayed
  145. unsigned otherpage;
  146. spritelisttype spritearray[MAXSPRITES],*prioritystart[PRIORITIES],
  147. *spritefreeptr;
  148. animtiletype animarray[MAXANIMTILES],*animhead,*animfreeptr;
  149. int animfreespot;
  150. eraseblocktype eraselist[2][MAXSPRITES],*eraselistptr[2];
  151. int hscrollblocks,vscrollblocks;
  152. int hscrolledge[MAXSCROLLEDGES],vscrolledge[MAXSCROLLEDGES];
  153. /*
  154. =============================================================================
  155. LOCAL PROTOTYPES
  156. =============================================================================
  157. */
  158. void RFL_NewTile (unsigned updateoffset);
  159. void RFL_MaskForegroundTiles (void);
  160. void RFL_UpdateTiles (void);
  161. void RFL_BoundScroll (int x, int y);
  162. void RFL_CalcOriginStuff (long x, long y);
  163. void RFL_ClearScrollBlocks (void);
  164. void RFL_InitSpriteList (void);
  165. void RFL_InitAnimList (void);
  166. void RFL_CheckForAnimTile (unsigned x, unsigned y);
  167. void RFL_AnimateTiles (void);
  168. void RFL_RemoveAnimsOnX (unsigned x);
  169. void RFL_RemoveAnimsOnY (unsigned y);
  170. void RFL_EraseBlocks (void);
  171. void RFL_UpdateSprites (void);
  172. /*
  173. =============================================================================
  174. GRMODE INDEPENDANT ROUTINES
  175. =============================================================================
  176. */
  177. /*
  178. =====================
  179. =
  180. = RF_Startup
  181. =
  182. =====================
  183. */
  184. static char *ParmStrings[] = {"comp",""};
  185. void RF_Startup (void)
  186. {
  187. int i,x,y;
  188. unsigned *blockstart;
  189. if (grmode == EGAGR)
  190. for (i = 1;i < _argc;i++)
  191. if (US_CheckParm(_argv[i],ParmStrings) == 0)
  192. {
  193. compatability = true;
  194. break;
  195. }
  196. for (i=0;i<PORTTILESHIGH;i++)
  197. uwidthtable[i] = UPDATEWIDE*i;
  198. originxmin = originymin = MAPBORDER*TILEGLOBAL;
  199. eraselistptr[0] = &eraselist[0][0];
  200. eraselistptr[1] = &eraselist[1][0];
  201. if (grmode == EGAGR)
  202. {
  203. SX_T_SHIFT = 1;
  204. baseupdatestart[0] = &update[0][UPDATESPARESIZE];
  205. baseupdatestart[1] = &update[1][UPDATESPARESIZE];
  206. screenpage = 0;
  207. otherpage = 1;
  208. displayofs = screenstart[screenpage];
  209. bufferofs = screenstart[otherpage];
  210. masterofs = screenstart[2];
  211. updateptr = baseupdatestart[otherpage];
  212. blockstart = &blockstarts[0];
  213. for (y=0;y<UPDATEHIGH;y++)
  214. for (x=0;x<UPDATEWIDE;x++)
  215. *blockstart++ = SCREENWIDTH*16*y+x*TILEWIDTH;
  216. xpanmask = 6; // dont pan to odd pixels
  217. }
  218. else if (grmode == CGAGR)
  219. {
  220. SX_T_SHIFT = 2;
  221. updateptr = baseupdateptr = &update[0][UPDATESPARESIZE];
  222. bufferofs = 0;
  223. masterofs = 0x8000;
  224. blockstart = &blockstarts[0];
  225. for (y=0;y<UPDATEHIGH;y++)
  226. for (x=0;x<UPDATEWIDE;x++)
  227. *blockstart++ = SCREENWIDTH*16*y+x*TILEWIDTH;
  228. }
  229. }
  230. /*
  231. =====================
  232. =
  233. = RF_Shutdown
  234. =
  235. =====================
  236. */
  237. void RF_Shutdown (void)
  238. {
  239. }
  240. //===========================================================================
  241. /*
  242. =====================
  243. =
  244. = RF_FixOfs
  245. =
  246. = Sets bufferofs,displayofs, and masterofs to regular values, for the
  247. = occasions when you have moved them around manually
  248. =
  249. =====================
  250. */
  251. void RF_FixOfs (void)
  252. {
  253. if (grmode == EGAGR)
  254. {
  255. screenpage = 0;
  256. otherpage = 1;
  257. panx = pany = pansx = pansy = panadjust = 0;
  258. displayofs = screenstart[screenpage];
  259. bufferofs = screenstart[otherpage];
  260. masterofs = screenstart[2];
  261. VW_SetScreen (displayofs,0);
  262. }
  263. else
  264. {
  265. bufferofs = 0;
  266. masterofs = 0x8000;
  267. }
  268. }
  269. //===========================================================================
  270. /*
  271. =====================
  272. =
  273. = RF_NewMap
  274. =
  275. = Makes some convienient calculations based on maphead->
  276. =
  277. =====================
  278. */
  279. void RF_NewMap (void)
  280. {
  281. int i,x,y;
  282. unsigned spot,*table;
  283. mapwidth = mapheaderseg[mapon]->width;
  284. mapbyteswide = 2*mapwidth;
  285. mapheight = mapheaderseg[mapon]->height;
  286. mapwordsextra = mapwidth-PORTTILESWIDE;
  287. mapbytesextra = 2*mapwordsextra;
  288. //
  289. // make a lookup table for the maps left edge
  290. //
  291. if (mapheight > MAXMAPHEIGHT)
  292. Quit ("RF_NewMap: Map too tall!");
  293. spot = 0;
  294. for (i=0;i<mapheight;i++)
  295. {
  296. mapbwidthtable[i] = spot;
  297. spot += mapbyteswide;
  298. }
  299. //
  300. // fill in updatemapofs with the new width info
  301. //
  302. table = &updatemapofs[0];
  303. for (y=0;y<PORTTILESHIGH;y++)
  304. for (x=0;x<UPDATEWIDE;x++)
  305. *table++ = mapbwidthtable[y]+x*2;
  306. //
  307. // the y max value clips off the bottom half of a tile so a map that is
  308. // 13 + MAPBORDER*2 tile high will not scroll at all vertically
  309. //
  310. originxmax = (mapwidth-MAPBORDER-SCREENTILESWIDE)*TILEGLOBAL;
  311. originymax = (mapheight-MAPBORDER-SCREENTILESHIGH)*TILEGLOBAL;
  312. if (originxmax<originxmin) // for very small maps
  313. originxmax=originxmin;
  314. if (originymax<originymin)
  315. originymax=originymin;
  316. //
  317. // clear out the lists
  318. //
  319. RFL_InitSpriteList ();
  320. RFL_InitAnimList ();
  321. RFL_ClearScrollBlocks ();
  322. RF_SetScrollBlock (0,MAPBORDER-1,true);
  323. RF_SetScrollBlock (0,mapheight-MAPBORDER,true);
  324. RF_SetScrollBlock (MAPBORDER-1,0,false);
  325. RF_SetScrollBlock (mapwidth-MAPBORDER,0,false);
  326. lasttimecount = TimeCount; // setup for adaptive timing
  327. tics = 1;
  328. }
  329. //===========================================================================
  330. /*
  331. ==========================
  332. =
  333. = RF_MarkTileGraphics
  334. =
  335. = Goes through mapplane[0/1] and marks all background/foreground tiles
  336. = needed, then follows all animation sequences to make sure animated
  337. = tiles get all the stages. Every unique animating tile is given an
  338. = entry in allanims[], so every instance of that tile will animate at the
  339. = same rate. The info plane for each animating tile will hold a pointer
  340. = into allanims[], therefore you can't have both an animating foreground
  341. = and background tile in the same spot!
  342. =
  343. ==========================
  344. */
  345. void RF_MarkTileGraphics (void)
  346. {
  347. unsigned size;
  348. int tile,next,anims,change;
  349. unsigned far *start,far *end,far *info;
  350. unsigned i,tilehigh;
  351. char str[80],str2[10];
  352. memset (allanims,0,sizeof(allanims));
  353. numanimchains = 0;
  354. size = mapwidth*mapheight;
  355. //
  356. // background plane
  357. //
  358. start = mapsegs[0];
  359. info = mapsegs[2];
  360. end = start+size;
  361. do
  362. {
  363. tile = *start++;
  364. if (tile>=0) // <0 is a tile that is never drawn
  365. {
  366. CA_MarkGrChunk(STARTTILE16+tile);
  367. if (tinf[ANIM+tile])
  368. {
  369. // this tile will animated
  370. if (tinf[SPEED+tile])
  371. {
  372. if (!tinf[ANIM+tile])
  373. {
  374. strcpy (str,"RF_MarkTileGraphics: Background anim of 0:");
  375. itoa (tile,str2,10);
  376. strcat (str,str2);
  377. Quit (str);
  378. }
  379. for (i=0;i<numanimchains;i++)
  380. if (allanims[i].current == tile)
  381. {
  382. *info = (unsigned)&allanims[i];
  383. goto nextback;
  384. }
  385. // new chain of animating tiles
  386. if (i>=MAXANIMTYPES)
  387. Quit ("RF_MarkTileGraphics: Too many unique animated tiles!");
  388. allanims[i].current = tile;
  389. allanims[i].count = tinf[SPEED+tile];
  390. *info = (unsigned)&allanims[i];
  391. numanimchains++;
  392. }
  393. anims = 0;
  394. change = (signed char)(tinf[ANIM+tile]);
  395. next = tile+change;
  396. while (change && next != tile)
  397. {
  398. CA_MarkGrChunk(STARTTILE16+next);
  399. change = (signed char)(tinf[ANIM+next]);
  400. next += change;
  401. if (++anims > 20)
  402. {
  403. strcpy (str,"RF_MarkTileGraphics: Unending background animation:");
  404. itoa (next,str2,10);
  405. strcat (str,str2);
  406. Quit (str);
  407. }
  408. }
  409. }
  410. }
  411. nextback:
  412. info++;
  413. } while (start<end);
  414. //
  415. // foreground plane
  416. //
  417. start = mapsegs[1];
  418. info = mapsegs[2];
  419. end = start+size;
  420. do
  421. {
  422. tile = *start++;
  423. if (tile>=0) // <0 is a tile that is never drawn
  424. {
  425. CA_MarkGrChunk(STARTTILE16M+tile);
  426. if (tinf[MANIM+tile])
  427. {
  428. // this tile will animated
  429. if (tinf[MSPEED+tile])
  430. {
  431. if (!tinf[MANIM+tile])
  432. {
  433. strcpy (str,"RF_MarkTileGraphics: Foreground anim of 0:");
  434. itoa (tile,str2,10);
  435. strcat (str,str2);
  436. Quit (str);
  437. }
  438. tilehigh = tile | 0x8000; // foreground tiles have high bit
  439. for (i=0;i<numanimchains;i++)
  440. if (allanims[i].current == tilehigh)
  441. {
  442. *info = (unsigned)&allanims[i];
  443. goto nextfront;
  444. }
  445. // new chain of animating tiles
  446. if (i>=MAXANIMTYPES)
  447. Quit ("RF_MarkTileGraphics: Too many unique animated tiles!");
  448. allanims[i].current = tilehigh;
  449. allanims[i].count = tinf[MSPEED+tile];
  450. *info = (unsigned)&allanims[i];
  451. numanimchains++;
  452. }
  453. anims = 0;
  454. change = (signed char)(tinf[MANIM+tile]);
  455. next = tile+change;
  456. while (change && next != tile)
  457. {
  458. CA_MarkGrChunk(STARTTILE16M+next);
  459. change = (signed char)(tinf[MANIM+next]);
  460. next += change;
  461. if (++anims > 20)
  462. {
  463. strcpy (str,"RF_MarkTileGraphics: Unending foreground animation:");
  464. itoa (next,str2,10);
  465. strcat (str,str2);
  466. Quit (str);
  467. }
  468. }
  469. }
  470. }
  471. nextfront:
  472. info++;
  473. } while (start<end);
  474. }
  475. //===========================================================================
  476. /*
  477. =========================
  478. =
  479. = RFL_InitAnimList
  480. =
  481. = Call to clear out the entire animating tile list and return all of them to
  482. = the free list.
  483. =
  484. =========================
  485. */
  486. void RFL_InitAnimList (void)
  487. {
  488. int i;
  489. animfreeptr = &animarray[0];
  490. for (i=0;i<MAXANIMTILES-1;i++)
  491. animarray[i].nexttile = &animarray[i+1];
  492. animarray[i].nexttile = NULL;
  493. animhead = NULL; // nothing in list
  494. }
  495. /*
  496. ====================
  497. =
  498. = RFL_CheckForAnimTile
  499. =
  500. ====================
  501. */
  502. void RFL_CheckForAnimTile (unsigned x, unsigned y)
  503. {
  504. unsigned tile,offset,speed,lasttime,thistime,timemissed;
  505. unsigned far *map;
  506. animtiletype *anim,*next;
  507. // the info plane of each animating tile has a near pointer into allanims[]
  508. // which gives the current state of all concurrently animating tiles
  509. offset = mapbwidthtable[y]/2+x;
  510. //
  511. // background
  512. //
  513. map = mapsegs[0]+offset;
  514. tile = *map;
  515. if (tinf[ANIM+tile] && tinf[SPEED+tile])
  516. {
  517. if (!animfreeptr)
  518. Quit ("RF_CheckForAnimTile: No free spots in tilearray!");
  519. anim = animfreeptr;
  520. animfreeptr = animfreeptr->nexttile;
  521. next = animhead; // stick it at the start of the list
  522. animhead = anim;
  523. if (next)
  524. next->prevptr = &anim->nexttile;
  525. anim->nexttile = next;
  526. anim->prevptr = &animhead;
  527. anim->x = x;
  528. anim->y = y;
  529. anim->tile = tile;
  530. anim->mapplane = map;
  531. anim->chain = (tiletype *)*(mapsegs[2]+offset);
  532. }
  533. //
  534. // foreground
  535. //
  536. map = mapsegs[1]+offset;
  537. tile = *map;
  538. if (tinf[MANIM+tile] && tinf[MSPEED+tile])
  539. {
  540. if (!animfreeptr)
  541. Quit ("RF_CheckForAnimTile: No free spots in tilearray!");
  542. anim = animfreeptr;
  543. animfreeptr = animfreeptr->nexttile;
  544. next = animhead; // stick it at the start of the list
  545. animhead = anim;
  546. if (next)
  547. next->prevptr = &anim->nexttile;
  548. anim->nexttile = next;
  549. anim->prevptr = &animhead;
  550. anim->x = x;
  551. anim->y = y;
  552. anim->tile = tile;
  553. anim->mapplane = map;
  554. anim->chain = (tiletype *)*(mapsegs[2]+offset);
  555. }
  556. }
  557. /*
  558. ====================
  559. =
  560. = RFL_RemoveAnimsOnX
  561. =
  562. ====================
  563. */
  564. void RFL_RemoveAnimsOnX (unsigned x)
  565. {
  566. animtiletype *current,*next;
  567. current = animhead;
  568. while (current)
  569. {
  570. if (current->x == x)
  571. {
  572. *(void **)current->prevptr = current->nexttile;
  573. if (current->nexttile)
  574. current->nexttile->prevptr = current->prevptr;
  575. next = current->nexttile;
  576. current->nexttile = animfreeptr;
  577. animfreeptr = current;
  578. current = next;
  579. }
  580. else
  581. current = current->nexttile;
  582. }
  583. }
  584. /*
  585. ====================
  586. =
  587. = RFL_RemoveAnimsOnY
  588. =
  589. ====================
  590. */
  591. void RFL_RemoveAnimsOnY (unsigned y)
  592. {
  593. animtiletype *current,*next;
  594. current = animhead;
  595. while (current)
  596. {
  597. if (current->y == y)
  598. {
  599. *(void **)current->prevptr = current->nexttile;
  600. if (current->nexttile)
  601. current->nexttile->prevptr = current->prevptr;
  602. next = current->nexttile;
  603. current->nexttile = animfreeptr;
  604. animfreeptr = current;
  605. current = next;
  606. }
  607. else
  608. current = current->nexttile;
  609. }
  610. }
  611. /*
  612. ====================
  613. =
  614. = RFL_RemoveAnimsInBlock
  615. =
  616. ====================
  617. */
  618. void RFL_RemoveAnimsInBlock (unsigned x, unsigned y, unsigned width, unsigned height)
  619. {
  620. animtiletype *current,*next;
  621. current = animhead;
  622. while (current)
  623. {
  624. if (current->x - x < width && current->y - y < height)
  625. {
  626. *(void **)current->prevptr = current->nexttile;
  627. if (current->nexttile)
  628. current->nexttile->prevptr = current->prevptr;
  629. next = current->nexttile;
  630. current->nexttile = animfreeptr;
  631. animfreeptr = current;
  632. current = next;
  633. }
  634. else
  635. current = current->nexttile;
  636. }
  637. }
  638. /*
  639. ====================
  640. =
  641. = RFL_AnimateTiles
  642. =
  643. ====================
  644. */
  645. void RFL_AnimateTiles (void)
  646. {
  647. animtiletype *current;
  648. unsigned updateofs,tile,x,y;
  649. tiletype *anim;
  650. //
  651. // animate the lists of tiles
  652. //
  653. anim = &allanims[0];
  654. while (anim->current)
  655. {
  656. anim->count-=tics;
  657. while ( anim->count < 1)
  658. {
  659. if (anim->current & 0x8000)
  660. {
  661. tile = anim->current & 0x7fff;
  662. tile += (signed char)tinf[MANIM+tile];
  663. anim->count += tinf[MSPEED+tile];
  664. tile |= 0x8000;
  665. }
  666. else
  667. {
  668. tile = anim->current;
  669. tile += (signed char)tinf[ANIM+tile];
  670. anim->count += tinf[SPEED+tile];
  671. }
  672. anim->current = tile;
  673. }
  674. anim++;
  675. }
  676. //
  677. // traverse the list of animating tiles
  678. //
  679. current = animhead;
  680. while (current)
  681. {
  682. tile =current->chain->current;
  683. if ( tile != current->tile)
  684. {
  685. // tile has animated
  686. //
  687. // remove tile from master screen cache,
  688. // change a tile to its next state, set the structure up for
  689. // next animation, and post an update region to both update pages
  690. //
  691. current->tile = tile;
  692. *(current->mapplane) = tile & 0x7fff; // change in map
  693. x = current->x-originxtile;
  694. y = current->y-originytile;
  695. if (x>=PORTTILESWIDE || y>=PORTTILESHIGH)
  696. Quit ("RFL_AnimateTiles: Out of bounds!");
  697. updateofs = uwidthtable[y] + x;
  698. RFL_NewTile(updateofs); // puts "1"s in both pages
  699. }
  700. current = current->nexttile;
  701. }
  702. }
  703. //===========================================================================
  704. /*
  705. =========================
  706. =
  707. = RFL_InitSpriteList
  708. =
  709. = Call to clear out the entire sprite list and return all of them to
  710. = the free list.
  711. =
  712. =========================
  713. */
  714. void RFL_InitSpriteList (void)
  715. {
  716. int i;
  717. spritefreeptr = &spritearray[0];
  718. for (i=0;i<MAXSPRITES-1;i++)
  719. spritearray[i].nextsprite = &spritearray[i+1];
  720. spritearray[i].nextsprite = NULL;
  721. // NULL in all priority levels
  722. memset (prioritystart,0,sizeof(prioritystart));
  723. }
  724. //===========================================================================
  725. /*
  726. =================
  727. =
  728. = RFL_CalcOriginStuff
  729. =
  730. = Calculate all the global variables for a new position
  731. = Long parms so position can be clipped to a maximum near 64k
  732. =
  733. =================
  734. */
  735. void RFL_CalcOriginStuff (long x, long y)
  736. {
  737. originxglobal = x;
  738. originyglobal = y;
  739. originxtile = originxglobal>>G_T_SHIFT;
  740. originytile = originyglobal>>G_T_SHIFT;
  741. originxscreen = originxtile<<SX_T_SHIFT;
  742. originyscreen = originytile<<SY_T_SHIFT;
  743. originmap = mapbwidthtable[originytile] + originxtile*2;
  744. #if GRMODE == EGAGR
  745. panx = (originxglobal>>G_P_SHIFT) & 15;
  746. pansx = panx & 8;
  747. pany = pansy = (originyglobal>>G_P_SHIFT) & 15;
  748. panadjust = panx/8 + ylookup[pany];
  749. #endif
  750. #if GRMODE == CGAGR
  751. panx = (originxglobal>>G_P_SHIFT) & 15;
  752. pansx = panx & 12;
  753. pany = pansy = (originyglobal>>G_P_SHIFT) & 15;
  754. panadjust = pansx/4 + ylookup[pansy];
  755. #endif
  756. }
  757. /*
  758. =================
  759. =
  760. = RFL_ClearScrollBlocks
  761. =
  762. =================
  763. */
  764. void RFL_ClearScrollBlocks (void)
  765. {
  766. hscrollblocks = vscrollblocks = 0;
  767. }
  768. /*
  769. =================
  770. =
  771. = RF_SetScrollBlock
  772. =
  773. = Sets a horizontal or vertical scroll block
  774. = a horizontal block is ----, meaning it blocks up/down movement
  775. =
  776. =================
  777. */
  778. void RF_SetScrollBlock (int x, int y, boolean horizontal)
  779. {
  780. if (horizontal)
  781. {
  782. hscrolledge[hscrollblocks] = y;
  783. if (hscrollblocks++ == MAXSCROLLEDGES)
  784. Quit ("RF_SetScrollBlock: Too many horizontal scroll blocks");
  785. }
  786. else
  787. {
  788. vscrolledge[vscrollblocks] = x;
  789. if (vscrollblocks++ == MAXSCROLLEDGES)
  790. Quit ("RF_SetScrollBlock: Too many vertical scroll blocks");
  791. }
  792. }
  793. /*
  794. =================
  795. =
  796. = RFL_BoundScroll
  797. =
  798. = Bound a given x/y movement to scroll blocks
  799. =
  800. =================
  801. */
  802. void RFL_BoundScroll (int x, int y)
  803. {
  804. int check,newxtile,newytile;
  805. originxglobal += x;
  806. originyglobal += y;
  807. newxtile= originxglobal >> G_T_SHIFT;
  808. newytile = originyglobal >> G_T_SHIFT;
  809. if (x>0)
  810. {
  811. newxtile+=SCREENTILESWIDE;
  812. for (check=0;check<vscrollblocks;check++)
  813. if (vscrolledge[check] == newxtile)
  814. {
  815. originxglobal = originxglobal&0xff00;
  816. break;
  817. }
  818. }
  819. else if (x<0)
  820. {
  821. for (check=0;check<vscrollblocks;check++)
  822. if (vscrolledge[check] == newxtile)
  823. {
  824. originxglobal = (originxglobal&0xff00)+0x100;
  825. break;
  826. }
  827. }
  828. if (y>0)
  829. {
  830. newytile+=SCREENTILESHIGH;
  831. for (check=0;check<hscrollblocks;check++)
  832. if (hscrolledge[check] == newytile)
  833. {
  834. originyglobal = originyglobal&0xff00;
  835. break;
  836. }
  837. }
  838. else if (y<0)
  839. {
  840. for (check=0;check<hscrollblocks;check++)
  841. if (hscrolledge[check] == newytile)
  842. {
  843. originyglobal = (originyglobal&0xff00)+0x100;
  844. break;
  845. }
  846. }
  847. RFL_CalcOriginStuff (originxglobal, originyglobal);
  848. }
  849. //===========================================================================
  850. /*
  851. =====================
  852. =
  853. = RF_SetRefreshHook
  854. =
  855. =====================
  856. */
  857. void RF_SetRefreshHook (void (*func) (void) )
  858. {
  859. refreshvector = func;
  860. }
  861. //===========================================================================
  862. /*
  863. =================
  864. =
  865. = RFL_NewRow
  866. =
  867. = Bring a new row of tiles onto the port, spawning animating tiles
  868. =
  869. =================
  870. */
  871. void RFL_NewRow (int dir)
  872. {
  873. unsigned count,updatespot,updatestep;
  874. int x,y,xstep,ystep;
  875. switch (dir)
  876. {
  877. case 0: // top row
  878. updatespot = 0;
  879. updatestep = 1;
  880. x = originxtile;
  881. y = originytile;
  882. xstep = 1;
  883. ystep = 0;
  884. count = PORTTILESWIDE;
  885. break;
  886. case 1: // right row
  887. updatespot = PORTTILESWIDE-1;
  888. updatestep = UPDATEWIDE;
  889. x = originxtile + PORTTILESWIDE-1;
  890. y = originytile;
  891. xstep = 0;
  892. ystep = 1;
  893. count = PORTTILESHIGH;
  894. break;
  895. case 2: // bottom row
  896. updatespot = UPDATEWIDE*(PORTTILESHIGH-1);
  897. updatestep = 1;
  898. x = originxtile;
  899. y = originytile + PORTTILESHIGH-1;
  900. xstep = 1;
  901. ystep = 0;
  902. count = PORTTILESWIDE;
  903. break;
  904. case 3: // left row
  905. updatespot = 0;
  906. updatestep = UPDATEWIDE;
  907. x = originxtile;
  908. y = originytile;
  909. xstep = 0;
  910. ystep = 1;
  911. count = PORTTILESHIGH;
  912. break;
  913. default:
  914. Quit ("RFL_NewRow: Bad dir!");
  915. }
  916. while (count--)
  917. {
  918. RFL_NewTile(updatespot);
  919. RFL_CheckForAnimTile (x,y);
  920. updatespot+=updatestep;
  921. x+=xstep;
  922. y+=ystep;
  923. }
  924. }
  925. //===========================================================================
  926. /*
  927. =====================
  928. =
  929. = RF_ForceRefresh
  930. =
  931. =====================
  932. */
  933. void RF_ForceRefresh (void)
  934. {
  935. RF_NewPosition (originxglobal,originyglobal);
  936. RF_Refresh ();
  937. RF_Refresh ();
  938. }
  939. //===========================================================================
  940. /*
  941. =====================
  942. =
  943. = RF_MapToMap
  944. =
  945. = Copies a block of tiles (all three planes) from one point
  946. = in the map to another, accounting for animating tiles
  947. =
  948. =====================
  949. */
  950. void RF_MapToMap (unsigned srcx, unsigned srcy,
  951. unsigned destx, unsigned desty,
  952. unsigned width, unsigned height)
  953. {
  954. int x,y;
  955. unsigned source,destofs,xspot,yspot;
  956. unsigned linedelta,p0,p1,p2,updatespot;
  957. unsigned far *source0, far *source1, far *source2;
  958. unsigned far *dest0, far *dest1, far *dest2;
  959. boolean changed;
  960. RFL_RemoveAnimsInBlock (destx,desty,width,height);
  961. source = mapbwidthtable[srcy]/2 + srcx;
  962. source0 = mapsegs[0]+source;
  963. source1 = mapsegs[1]+source;
  964. source2 = mapsegs[2]+source;
  965. destofs = mapbwidthtable[desty]/2 + destx;
  966. destofs -= source;
  967. linedelta = mapwidth - width;
  968. for (y=0;y<height;y++,source0+=linedelta,source1+=linedelta,source2+=linedelta)
  969. for (x=0;x<width;x++,source0++,source1++,source2++)
  970. {
  971. p0 = *source0;
  972. p1 = *source1;
  973. p2 = *source2;
  974. dest0 = source0 + destofs;
  975. dest1 = source1 + destofs;
  976. dest2 = source2 + destofs;
  977. //
  978. // only make a new tile if it is different
  979. //
  980. if (p0 != *dest0 || p1 != *dest1 || p2 != *dest2)
  981. {
  982. *dest0 = p0;
  983. *dest1 = p1;
  984. *dest2 = p2;
  985. changed = true;
  986. }
  987. else
  988. changed = false;
  989. //
  990. // if tile is on the view port
  991. //
  992. xspot = destx+x-originxtile;
  993. yspot = desty+y-originytile;
  994. if (yspot < PORTTILESHIGH && xspot < PORTTILESWIDE)
  995. {
  996. if (changed)
  997. {
  998. updatespot = uwidthtable[yspot]+xspot;
  999. RFL_NewTile(updatespot);
  1000. }
  1001. RFL_CheckForAnimTile (destx+x,desty+y);
  1002. }
  1003. }
  1004. }
  1005. //===========================================================================
  1006. /*
  1007. =====================
  1008. =
  1009. = RF_MemToMap
  1010. =
  1011. = Copies a string of tiles from main memory to the map,
  1012. = accounting for animating tiles
  1013. =
  1014. =====================
  1015. */
  1016. void RF_MemToMap (unsigned far *source, unsigned plane,
  1017. unsigned destx, unsigned desty,
  1018. unsigned width, unsigned height)
  1019. {
  1020. int x,y;
  1021. unsigned xspot,yspot;
  1022. unsigned linedelta,updatespot;
  1023. unsigned far *dest,old,new;
  1024. boolean changed;
  1025. RFL_RemoveAnimsInBlock (destx,desty,width,height);
  1026. dest = mapsegs[plane] + mapbwidthtable[desty]/2 + destx;
  1027. linedelta = mapwidth - width;
  1028. for (y=0;y<height;y++,dest+=linedelta)
  1029. for (x=0;x<width;x++)
  1030. {
  1031. old = *dest;
  1032. new = *source++;
  1033. if (old != new)
  1034. {
  1035. *dest = new;
  1036. changed = true;
  1037. }
  1038. else
  1039. changed = false;
  1040. dest++;
  1041. xspot = destx+x-originxtile;
  1042. yspot = desty+y-originytile;
  1043. if (yspot < PORTTILESHIGH && xspot < PORTTILESWIDE)
  1044. {
  1045. if (changed)
  1046. {
  1047. updatespot = uwidthtable[yspot]+xspot;
  1048. RFL_NewTile(updatespot);
  1049. }
  1050. RFL_CheckForAnimTile (destx+x,desty+y);
  1051. }
  1052. }
  1053. }
  1054. //===========================================================================
  1055. /*
  1056. =====================
  1057. =
  1058. = RFL_BoundNewOrigin
  1059. =
  1060. = Copies a string of tiles from main memory to the map,
  1061. = accounting for animating tiles
  1062. =
  1063. =====================
  1064. */
  1065. void RFL_BoundNewOrigin (unsigned orgx,unsigned orgy)
  1066. {
  1067. int check,edge;
  1068. //
  1069. // calculate new origin related globals
  1070. //
  1071. if (orgx<originxmin)
  1072. orgx=originxmin;
  1073. else if (orgx>originxmax)
  1074. orgx=originxmax;
  1075. if (orgy<originymin)
  1076. orgy=originymin;
  1077. else if (orgy>originymax)
  1078. orgy=originymax;
  1079. originxtile = orgx>>G_T_SHIFT;
  1080. originytile = orgy>>G_T_SHIFT;
  1081. for (check=0;check<vscrollblocks;check++)
  1082. {
  1083. edge = vscrolledge[check];
  1084. if (edge>=originxtile && edge <=originxtile+10)
  1085. {
  1086. orgx = (edge+1)*TILEGLOBAL;
  1087. break;
  1088. }
  1089. if (edge>=originxtile+11 && edge <=originxtile+20)
  1090. {
  1091. orgx = (edge-20)*TILEGLOBAL;
  1092. break;
  1093. }
  1094. }
  1095. for (check=0;check<hscrollblocks;check++)
  1096. {
  1097. edge = hscrolledge[check];
  1098. if (edge>=originytile && edge <=originytile+6)
  1099. {
  1100. orgy = (edge+1)*TILEGLOBAL;
  1101. break;
  1102. }
  1103. if (edge>=originytile+7 && edge <=originytile+13)
  1104. {
  1105. orgy = (edge-13)*TILEGLOBAL;
  1106. break;
  1107. }
  1108. }
  1109. RFL_CalcOriginStuff (orgx,orgy);
  1110. }
  1111. //===========================================================================
  1112. /*
  1113. =====================
  1114. =
  1115. = RF_ClearBlock
  1116. =
  1117. = Posts erase blocks to clear a certain area of the screen to the master
  1118. = screen, to erase text or something draw directly to the screen
  1119. =
  1120. = Parameters in pixels, but erasure is byte bounded
  1121. =
  1122. =====================
  1123. */
  1124. void RF_ClearBlock (int x, int y, int width, int height)
  1125. {
  1126. eraseblocktype block;
  1127. #if GRMODE == EGAGR
  1128. block.screenx = x/8+originxscreen;
  1129. block.screeny = y+originyscreen;
  1130. block.width = (width+(x&7)+7)/8;
  1131. block.height = height;
  1132. memcpy (eraselistptr[0]++,&block,sizeof(block));
  1133. memcpy (eraselistptr[1]++,&block,sizeof(block));
  1134. #endif
  1135. #if GRMODE == CGAGR
  1136. block.screenx = x/4+originxscreen;
  1137. block.screeny = y+originyscreen;
  1138. block.width = (width+(x&3)+3)/4;
  1139. block.height = height;
  1140. memcpy (eraselistptr[0]++,&block,sizeof(block));
  1141. #endif
  1142. }
  1143. //===========================================================================
  1144. /*
  1145. =====================
  1146. =
  1147. = RF_RedrawBlock
  1148. =
  1149. = Causes a number of tiles to be redrawn to the master screen and updated
  1150. =
  1151. = Parameters in pixels, but erasure is tile bounded
  1152. =
  1153. =====================
  1154. */
  1155. void RF_RedrawBlock (int x, int y, int width, int height)
  1156. {
  1157. int xx,yy,xl,xh,yl,yh;
  1158. xl=(x+panx)/16;
  1159. xh=(x+panx+width+15)/16;
  1160. yl=(y+pany)/16;
  1161. yh=(y+pany+height+15)/16;
  1162. for (yy=yl;yy<=yh;yy++)
  1163. for (xx=xl;xx<=xh;xx++)
  1164. RFL_NewTile (yy*UPDATEWIDE+xx);
  1165. }
  1166. //===========================================================================
  1167. /*
  1168. =====================
  1169. =
  1170. = RF_CalcTics
  1171. =
  1172. =====================
  1173. */
  1174. void RF_CalcTics (void)
  1175. {
  1176. long newtime,oldtimecount;
  1177. //
  1178. // calculate tics since last refresh for adaptive timing
  1179. //
  1180. if (lasttimecount > TimeCount)
  1181. TimeCount = lasttimecount; // if the game was paused a LONG time
  1182. if (DemoMode) // demo recording and playback needs
  1183. { // to be constant
  1184. //
  1185. // take DEMOTICS or more tics, and modify Timecount to reflect time taken
  1186. //
  1187. oldtimecount = lasttimecount;
  1188. while (TimeCount<oldtimecount+DEMOTICS*2)
  1189. ;
  1190. lasttimecount = oldtimecount + DEMOTICS;
  1191. TimeCount = lasttimecount + DEMOTICS;
  1192. tics = DEMOTICS;
  1193. }
  1194. else
  1195. {
  1196. //
  1197. // non demo, so report actual time
  1198. //
  1199. do
  1200. {
  1201. newtime = TimeCount;
  1202. tics = newtime-lasttimecount;
  1203. } while (tics<MINTICS);
  1204. lasttimecount = newtime;
  1205. #ifdef PROFILE
  1206. strcpy (scratch,"\tTics:");
  1207. itoa (tics,str,10);
  1208. strcat (scratch,str);
  1209. strcat (scratch,"\n");
  1210. write (profilehandle,scratch,strlen(scratch));
  1211. #endif
  1212. if (tics>MAXTICS)
  1213. {
  1214. TimeCount -= (tics-MAXTICS);
  1215. tics = MAXTICS;
  1216. }
  1217. }
  1218. }
  1219. /*
  1220. =============================================================================
  1221. EGA specific routines
  1222. =============================================================================
  1223. */
  1224. #if GRMODE == EGAGR
  1225. /*
  1226. =====================
  1227. =
  1228. = RF_FindFreeBuffer
  1229. =
  1230. = Finds the start of unused, non visable buffer space
  1231. =
  1232. =====================
  1233. */
  1234. unsigned RF_FindFreeBuffer (void)
  1235. {
  1236. unsigned spot,i,j;
  1237. boolean ok;
  1238. for (i=0;i<3;i++)
  1239. {
  1240. spot = screenstart[i]+SCREENSPACE;
  1241. ok = true;
  1242. for (j=0;j<3;j++)
  1243. if (spot == screenstart[j])
  1244. {
  1245. ok = false;
  1246. break;
  1247. }
  1248. if (ok)
  1249. return spot;
  1250. }
  1251. return 0; // never get here...
  1252. }
  1253. //===========================================================================
  1254. /*
  1255. =====================
  1256. =
  1257. = RF_NewPosition EGA
  1258. =
  1259. =====================
  1260. */
  1261. void RF_NewPosition (unsigned x, unsigned y)
  1262. {
  1263. int mx,my;
  1264. byte *page0ptr,*page1ptr;
  1265. unsigned updatenum;
  1266. RFL_BoundNewOrigin (x,y);
  1267. //
  1268. // clear out all animating tiles
  1269. //
  1270. RFL_InitAnimList ();
  1271. //
  1272. // set up the new update arrays at base position
  1273. //
  1274. updatestart[0] = baseupdatestart[0];
  1275. updatestart[1] = baseupdatestart[1];
  1276. updateptr = updatestart[otherpage];
  1277. page0ptr = updatestart[0]+PORTTILESWIDE; // used to stick "0"s after rows
  1278. page1ptr = updatestart[1]+PORTTILESWIDE;
  1279. updatenum = 0; // start at first visable tile
  1280. for (my=0;my<PORTTILESHIGH;my++)
  1281. {
  1282. for (mx=0;mx<PORTTILESWIDE;mx++)
  1283. {
  1284. RFL_NewTile(updatenum); // puts "1"s in both pages
  1285. RFL_CheckForAnimTile(mx+originxtile,my+originytile);
  1286. updatenum++;
  1287. }
  1288. updatenum++;
  1289. *page0ptr = *page1ptr = 0; // set a 0 at end of a line of tiles
  1290. page0ptr+=(PORTTILESWIDE+1);
  1291. page1ptr+=(PORTTILESWIDE+1);
  1292. }
  1293. *(word *)(page0ptr-PORTTILESWIDE)
  1294. = *(word *)(page1ptr-PORTTILESWIDE) = UPDATETERMINATE;
  1295. }
  1296. //===========================================================================
  1297. /*
  1298. =====================
  1299. =
  1300. = RF_Scroll EGA
  1301. =
  1302. = Move the origin x/y global coordinates, readjust the screen panning, and
  1303. = scroll if needed. If the scroll distance is greater than one tile, the
  1304. = entire screen will be redrawn (this could be generalized, but scrolling
  1305. = more than one tile per refresh is a bad idea!).
  1306. =
  1307. =====================
  1308. */
  1309. void RF_Scroll (int x, int y)
  1310. {
  1311. long neworgx,neworgy;
  1312. int i,deltax,deltay,absdx,absdy;
  1313. int oldxt,oldyt,move,yy;
  1314. unsigned updatespot;
  1315. byte *update0,*update1;
  1316. unsigned oldpanx,oldpanadjust,oldscreen,newscreen,screencopy;
  1317. int screenmove;
  1318. oldxt = originxtile;
  1319. oldyt = originytile;
  1320. oldpanadjust = panadjust;
  1321. oldpanx = panx;
  1322. RFL_BoundScroll (x,y);
  1323. deltax = originxtile - oldxt;
  1324. absdx = abs(deltax);
  1325. deltay = originytile - oldyt;
  1326. absdy = abs(deltay);
  1327. if (absdx>1 || absdy>1)
  1328. {
  1329. //
  1330. // scrolled more than one tile, so start from scratch
  1331. //
  1332. RF_NewPosition(originxglobal,originyglobal);
  1333. return;
  1334. }
  1335. if (!absdx && !absdy)
  1336. return; // the screen has not scrolled an entire tile
  1337. //
  1338. // adjust screens and handle SVGA crippled compatability mode
  1339. //
  1340. screenmove = deltay*16*SCREENWIDTH + deltax*TILEWIDTH;
  1341. for (i=0;i<3;i++)
  1342. {
  1343. screenstart[i]+= screenmove;
  1344. if (compatability && screenstart[i] > (0x10000l-SCREENSPACE) )
  1345. {
  1346. //
  1347. // move the screen to the opposite end of the buffer
  1348. //
  1349. screencopy = screenmove>0 ? FREEEGAMEM : -FREEEGAMEM;
  1350. oldscreen = screenstart[i] - screenmove;
  1351. newscreen = oldscreen + screencopy;
  1352. screenstart[i] = newscreen + screenmove;
  1353. VW_ScreenToScreen (oldscreen,newscreen,
  1354. PORTTILESWIDE*2,PORTTILESHIGH*16);
  1355. if (i==screenpage)
  1356. VW_SetScreen(newscreen+oldpanadjust,oldpanx & xpanmask);
  1357. }
  1358. }
  1359. bufferofs = screenstart[otherpage];
  1360. displayofs = screenstart[screenpage];
  1361. masterofs = screenstart[2];
  1362. //
  1363. // float the update regions
  1364. //
  1365. move = deltax;
  1366. if (deltay==1)
  1367. move += UPDATEWIDE;
  1368. else if (deltay==-1)
  1369. move -= UPDATEWIDE;
  1370. updatestart[0]+=move;
  1371. updatestart[1]+=move;
  1372. //
  1373. // draw the new tiles just scrolled on to the master screen, and
  1374. // mark them as needing to be copied to each screen next refreshes
  1375. // Make sure a zero is at the end of each row in update
  1376. //
  1377. if (deltax)
  1378. {
  1379. if (deltax==1)
  1380. {
  1381. RFL_NewRow (1); // new right row
  1382. RFL_RemoveAnimsOnX (originxtile-1);
  1383. }
  1384. else
  1385. {
  1386. RFL_NewRow (3); // new left row
  1387. RFL_RemoveAnimsOnX (originxtile+PORTTILESWIDE);
  1388. }
  1389. update0 = updatestart[0]+PORTTILESWIDE;
  1390. update1 = updatestart[1]+PORTTILESWIDE;
  1391. for (yy=0;yy<PORTTILESHIGH;yy++)
  1392. {
  1393. *update0 = *update1 = 0; // drop a 0 at end of each row
  1394. update0+=UPDATEWIDE;
  1395. update1+=UPDATEWIDE;
  1396. }
  1397. }
  1398. //----------------
  1399. if (deltay)
  1400. {
  1401. if (deltay==1)
  1402. {
  1403. updatespot = UPDATEWIDE*(PORTTILESHIGH-1);
  1404. RFL_NewRow (2); // new bottom row
  1405. RFL_RemoveAnimsOnY (originytile-1);
  1406. }
  1407. else
  1408. {
  1409. updatespot = 0;
  1410. RFL_NewRow (0); // new top row
  1411. RFL_RemoveAnimsOnY (originytile+PORTTILESHIGH);
  1412. }
  1413. *(updatestart[0]+updatespot+PORTTILESWIDE) =
  1414. *(updatestart[1]+updatespot+PORTTILESWIDE) = 0;
  1415. }
  1416. //----------------
  1417. //
  1418. // place a new terminator
  1419. //
  1420. update0 = updatestart[0]+UPDATEWIDE*PORTTILESHIGH-1;
  1421. update1 = updatestart[1]+UPDATEWIDE*PORTTILESHIGH-1;
  1422. *update0++ = *update1++ = 0;
  1423. *(unsigned *)update0 = *(unsigned *)update1 = UPDATETERMINATE;
  1424. }
  1425. //===========================================================================
  1426. /*
  1427. =====================
  1428. =
  1429. = RF_PlaceSprite EGA
  1430. =
  1431. =====================
  1432. */
  1433. void RF_PlaceSprite (void **user,unsigned globalx,unsigned globaly,
  1434. unsigned spritenumber, drawtype draw, int priority)
  1435. {
  1436. spritelisttype register *sprite,*next;
  1437. spritetabletype far *spr;
  1438. spritetype _seg *block;
  1439. unsigned shift,pixx;
  1440. char str[80],str2[10];
  1441. if (!spritenumber || spritenumber == (unsigned)-1)
  1442. {
  1443. RF_RemoveSprite (user);
  1444. return;
  1445. }
  1446. sprite = (spritelisttype *)*user;
  1447. if (sprite)
  1448. {
  1449. // sprite allready exists in the list, so we can use it's block
  1450. //
  1451. // post an erase block to both pages by copying screenx,screeny,width,height
  1452. // both pages may not need to be erased if the sprite just changed last frame
  1453. //
  1454. if (sprite->updatecount<2)
  1455. {
  1456. if (!sprite->updatecount)
  1457. memcpy (eraselistptr[otherpage]++,sprite,sizeof(eraseblocktype));
  1458. memcpy (eraselistptr[screenpage]++,sprite,sizeof(eraseblocktype));
  1459. }
  1460. if (priority != sprite->priority)
  1461. {
  1462. // sprite mvoed to another priority, so unlink the old one and
  1463. // relink it in the new priority
  1464. next = sprite->nextsprite; // cut old links
  1465. if (next)
  1466. next->prevptr = sprite->prevptr;
  1467. *sprite->prevptr = next;
  1468. goto linknewspot;
  1469. }
  1470. }
  1471. else
  1472. {
  1473. // this is a brand new sprite, so allocate a block from the array
  1474. if (!spritefreeptr)
  1475. Quit ("RF_PlaceSprite: No free spots in spritearray!");
  1476. sprite = spritefreeptr;
  1477. spritefreeptr = spritefreeptr->nextsprite;
  1478. linknewspot:
  1479. next = prioritystart[priority]; // stick it in new spot
  1480. if (next)
  1481. next->prevptr = &sprite->nextsprite;
  1482. sprite->nextsprite = next;
  1483. prioritystart[priority] = sprite;
  1484. sprite->prevptr = &prioritystart[priority];
  1485. }
  1486. //
  1487. // write the new info to the sprite
  1488. //
  1489. spr = &spritetable[spritenumber-STARTSPRITES];
  1490. block = (spritetype _seg *)grsegs[spritenumber];
  1491. if (!block)
  1492. {
  1493. strcpy (str,"RF_PlaceSprite: Placed an uncached sprite:");
  1494. itoa (spritenumber,str2,10);
  1495. strcat (str,str2);
  1496. Quit (str);
  1497. }
  1498. globaly+=spr->orgy;
  1499. globalx+=spr->orgx;
  1500. pixx = globalx >> G_SY_SHIFT;
  1501. shift = (pixx&7)/2;
  1502. sprite->screenx = pixx >> (G_EGASX_SHIFT-G_SY_SHIFT);
  1503. sprite->screeny = globaly >> G_SY_SHIFT;
  1504. sprite->width = block->width[shift];
  1505. sprite->height = spr->height;
  1506. sprite->grseg = spritenumber;
  1507. sprite->sourceofs = block->sourceoffset[shift];
  1508. sprite->planesize = block->planesize[shift];
  1509. sprite->draw = draw;
  1510. sprite->priority = priority;
  1511. sprite->tilex = sprite->screenx >> SX_T_SHIFT;
  1512. sprite->tiley = sprite->screeny >> SY_T_SHIFT;
  1513. sprite->tilewide = ( (sprite->screenx + sprite->width -1) >> SX_T_SHIFT )
  1514. - sprite->tilex + 1;
  1515. sprite->tilehigh = ( (sprite->screeny + sprite->height -1) >> SY_T_SHIFT )
  1516. - sprite->tiley + 1;
  1517. sprite->updatecount = 2; // draw on next two refreshes
  1518. // save the sprite pointer off in the user's pointer so it can be moved
  1519. // again later
  1520. *user = sprite;
  1521. }
  1522. //===========================================================================
  1523. /*
  1524. =====================
  1525. =
  1526. = RF_RemoveSprite EGA
  1527. =
  1528. =====================
  1529. */
  1530. void RF_RemoveSprite (void **user)
  1531. {
  1532. spritelisttype *sprite,*next;
  1533. sprite = (spritelisttype *)*user;
  1534. if (!sprite)
  1535. return;
  1536. //
  1537. // post an erase block to both pages by copying screenx,screeny,width,height
  1538. // both pages may not need to be erased if the sprite just changed last frame
  1539. //
  1540. if (sprite->updatecount<2)
  1541. {
  1542. if (!sprite->updatecount)
  1543. memcpy (eraselistptr[otherpage]++,sprite,sizeof(eraseblocktype));
  1544. memcpy (eraselistptr[screenpage]++,sprite,sizeof(eraseblocktype));
  1545. }
  1546. //
  1547. // unlink the sprite node
  1548. //
  1549. next = sprite->nextsprite;
  1550. if (next) // if (!next), sprite is last in chain
  1551. next->prevptr = sprite->prevptr;
  1552. *sprite->prevptr = next;
  1553. //
  1554. // add it back to the free list
  1555. //
  1556. sprite->nextsprite = spritefreeptr;
  1557. spritefreeptr = sprite;
  1558. //
  1559. // null the users pointer, so next time that actor gets placed, it will
  1560. // allocate a new block
  1561. //
  1562. *user = 0;
  1563. }
  1564. //===========================================================================
  1565. /*
  1566. ====================
  1567. =
  1568. = RFL_EraseBlocks EGA
  1569. =
  1570. = Write mode 1 should be set
  1571. =
  1572. ====================
  1573. */
  1574. void RFL_EraseBlocks (void)
  1575. {
  1576. eraseblocktype *block,*done;
  1577. int screenxh,screenyh;
  1578. unsigned pos,xtl,ytl,xth,yth,x,y;
  1579. byte *updatespot;
  1580. unsigned updatedelta;
  1581. unsigned erasecount;
  1582. #ifdef PROFILE
  1583. erasecount = 0;
  1584. #endif
  1585. block = otherpage ? &eraselist[1][0] : &eraselist[0][0];
  1586. done = eraselistptr[otherpage];
  1587. while (block != done)
  1588. {
  1589. //
  1590. // clip the block to the current screen view
  1591. //
  1592. block->screenx -= originxscreen;
  1593. block->screeny -= originyscreen;
  1594. if (block->screenx < 0)
  1595. {
  1596. block->width += block->screenx;
  1597. if (block->width<1)
  1598. goto next;
  1599. block->screenx = 0;
  1600. }
  1601. if (block->screeny < 0)
  1602. {
  1603. block->height += block->screeny;
  1604. if (block->height<1)
  1605. goto next;
  1606. block->screeny = 0;
  1607. }
  1608. screenxh = block->screenx + block->width;
  1609. screenyh = block->screeny + block->height;
  1610. if (screenxh > EGAPORTSCREENWIDE)
  1611. {
  1612. block->width = EGAPORTSCREENWIDE-block->screenx;
  1613. screenxh = block->screenx + block->width;
  1614. }
  1615. if (screenyh > PORTSCREENHIGH)
  1616. {
  1617. block->height = PORTSCREENHIGH-block->screeny;
  1618. screenyh = block->screeny + block->height;
  1619. }
  1620. if (block->width<1 || block->height<1)
  1621. goto next;
  1622. //
  1623. // erase the block by copying from the master screen
  1624. //
  1625. pos = ylookup[block->screeny]+block->screenx;
  1626. VW_ScreenToScreen (masterofs+pos,bufferofs+pos,
  1627. block->width,block->height);
  1628. //
  1629. // put 2s in update where the block was, to force sprites to update
  1630. //
  1631. xtl = block->screenx >> SX_T_SHIFT;
  1632. xth = (block->screenx+block->width-1) >> SX_T_SHIFT;
  1633. ytl = block->screeny >> SY_T_SHIFT;
  1634. yth = (block->screeny+block->height-1) >> SY_T_SHIFT;
  1635. updatespot = updateptr + uwidthtable[ytl] + xtl;
  1636. updatedelta = UPDATEWIDE - (xth-xtl+1);
  1637. for (y=ytl;y<=yth;y++)
  1638. {
  1639. for (x=xtl;x<=xth;x++)
  1640. *updatespot++ = 2;
  1641. updatespot += updatedelta; // down to next line
  1642. }
  1643. #ifdef PROFILE
  1644. erasecount++;
  1645. #endif
  1646. next:
  1647. block++;
  1648. }
  1649. eraselistptr[otherpage] = otherpage ? &eraselist[1][0] : &eraselist[0][0];
  1650. #ifdef PROFILE
  1651. strcpy (scratch,"\tErase:");
  1652. itoa (erasecount,str,10);
  1653. strcat (scratch,str);
  1654. write (profilehandle,scratch,strlen(scratch));
  1655. #endif
  1656. }
  1657. /*
  1658. ====================
  1659. =
  1660. = RFL_UpdateSprites EGA
  1661. =
  1662. = NOTE: Implement vertical clipping!
  1663. =
  1664. ====================
  1665. */
  1666. void RFL_UpdateSprites (void)
  1667. {
  1668. spritelisttype *sprite;
  1669. int portx,porty,x,y,xtl,xth,ytl,yth;
  1670. int priority;
  1671. unsigned dest;
  1672. byte *updatespot,*baseupdatespot;
  1673. unsigned updatedelta;
  1674. unsigned updatecount;
  1675. unsigned height,sourceofs;
  1676. #ifdef PROFILE
  1677. updatecount = 0;
  1678. #endif
  1679. for (priority=0;priority<PRIORITIES;priority++)
  1680. {
  1681. if (priority==MASKEDTILEPRIORITY)
  1682. RFL_MaskForegroundTiles ();
  1683. for (sprite = prioritystart[priority]; sprite ;
  1684. sprite = (spritelisttype *)sprite->nextsprite)
  1685. {
  1686. //
  1687. // see if the sprite has any visable area in the port
  1688. //
  1689. portx = sprite->screenx - originxscreen;
  1690. porty = sprite->screeny - originyscreen;
  1691. xtl = portx >> SX_T_SHIFT;
  1692. xth = (portx + sprite->width-1) >> SX_T_SHIFT;
  1693. ytl = porty >> SY_T_SHIFT;
  1694. yth = (porty + sprite->height-1) >> SY_T_SHIFT;
  1695. if (xtl<0)
  1696. xtl = 0;
  1697. if (xth>=PORTTILESWIDE)
  1698. xth = PORTTILESWIDE-1;
  1699. if (ytl<0)
  1700. ytl = 0;
  1701. if (yth>=PORTTILESHIGH)
  1702. yth = PORTTILESHIGH-1;
  1703. if (xtl>xth || ytl>yth)
  1704. continue;
  1705. //
  1706. // see if it's visable area covers any non 0 update tiles
  1707. //
  1708. updatespot = baseupdatespot = updateptr + uwidthtable[ytl] + xtl;
  1709. updatedelta = UPDATEWIDE - (xth-xtl+1);
  1710. if (sprite->updatecount)
  1711. {
  1712. sprite->updatecount--; // the sprite was just placed,
  1713. goto redraw; // so draw it for sure
  1714. }
  1715. for (y=ytl;y<=yth;y++)
  1716. {
  1717. for (x=xtl;x<=xth;x++)
  1718. if (*updatespot++)
  1719. goto redraw;
  1720. updatespot += updatedelta; // down to next line
  1721. }
  1722. continue; // no need to update
  1723. redraw:
  1724. //
  1725. // set the tiles it covers to 3, because those tiles are being
  1726. // updated
  1727. //
  1728. updatespot = baseupdatespot;
  1729. for (y=ytl;y<=yth;y++)
  1730. {
  1731. for (x=xtl;x<=xth;x++)
  1732. *updatespot++ = 3;
  1733. updatespot += updatedelta; // down to next line
  1734. }
  1735. //
  1736. // draw it!
  1737. //
  1738. height = sprite->height;
  1739. sourceofs = sprite->sourceofs;
  1740. if (porty<0)
  1741. {
  1742. height += porty; // clip top off
  1743. sourceofs -= porty*sprite->width;
  1744. porty = 0;
  1745. }
  1746. else if (porty+height>PORTSCREENHIGH)
  1747. {
  1748. height = PORTSCREENHIGH - porty; // clip bottom off
  1749. }
  1750. dest = bufferofs + ylookup[porty] + portx;
  1751. switch (sprite->draw)
  1752. {
  1753. case spritedraw:
  1754. VW_MaskBlock(grsegs[sprite->grseg], sourceofs,
  1755. dest,sprite->width,height,sprite->planesize);
  1756. break;
  1757. case maskdraw:
  1758. break;
  1759. }
  1760. #ifdef PROFILE
  1761. updatecount++;
  1762. #endif
  1763. }
  1764. }
  1765. #ifdef PROFILE
  1766. strcpy (scratch,"\tSprites:");
  1767. itoa (updatecount,str,10);
  1768. strcat (scratch,str);
  1769. write (profilehandle,scratch,strlen(scratch));
  1770. #endif
  1771. }
  1772. /*
  1773. =====================
  1774. =
  1775. = RF_Refresh EGA
  1776. =
  1777. = All routines will draw at the port at bufferofs, possibly copying from
  1778. = the port at masterofs. The EGA version then page flips, while the
  1779. = CGA version updates the screen from the buffer port.
  1780. =
  1781. = Screenpage is the currently displayed page, not the one being drawn
  1782. = Otherpage is the page to be worked with now
  1783. =
  1784. =====================
  1785. */
  1786. void RF_Refresh (void)
  1787. {
  1788. byte *newupdate;
  1789. updateptr = updatestart[otherpage];
  1790. RFL_AnimateTiles (); // DEBUG
  1791. //
  1792. // update newly scrolled on tiles and animated tiles from the master screen
  1793. //
  1794. EGAWRITEMODE(1);
  1795. EGAMAPMASK(15);
  1796. RFL_UpdateTiles ();
  1797. RFL_EraseBlocks ();
  1798. //
  1799. // Update is all 0 except where sprites have changed or new area has
  1800. // been scrolled on. Go through all sprites and update the ones that cover
  1801. // a non 0 update tile
  1802. //
  1803. EGAWRITEMODE(0);
  1804. RFL_UpdateSprites ();
  1805. //
  1806. // if the main program has a refresh hook set, call their function before
  1807. // displaying the new page
  1808. //
  1809. if (refreshvector)
  1810. refreshvector();
  1811. //
  1812. // display the changed screen
  1813. //
  1814. VW_SetScreen(bufferofs+panadjust,panx & xpanmask);
  1815. //
  1816. // prepare for next refresh
  1817. //
  1818. // Set the update array to the middle position and clear it out to all "0"s
  1819. // with an UPDATETERMINATE at the end
  1820. //
  1821. updatestart[otherpage] = newupdate = baseupdatestart[otherpage];
  1822. asm mov ax,ds
  1823. asm mov es,ax
  1824. asm xor ax,ax
  1825. asm mov cx,(UPDATESCREENSIZE-2)/2
  1826. asm mov di,[newupdate]
  1827. asm rep stosw
  1828. asm mov [WORD PTR es:di],UPDATETERMINATE
  1829. screenpage ^= 1;
  1830. otherpage ^= 1;
  1831. bufferofs = screenstart[otherpage];
  1832. displayofs = screenstart[screenpage];
  1833. //
  1834. // calculate tics since last refresh for adaptive timing
  1835. //
  1836. RF_CalcTics ();
  1837. }
  1838. #endif // GRMODE == EGAGR
  1839. /*
  1840. =============================================================================
  1841. CGA specific routines
  1842. =============================================================================
  1843. */
  1844. #if GRMODE == CGAGR
  1845. /*
  1846. =====================
  1847. =
  1848. = RF_NewPosition CGA
  1849. =
  1850. =====================
  1851. */
  1852. void RF_NewPosition (unsigned x, unsigned y)
  1853. {
  1854. int mx,my;
  1855. byte *spotptr;
  1856. unsigned updatenum;
  1857. RFL_BoundNewOrigin (x,y);
  1858. //
  1859. // clear out all animating tiles
  1860. //
  1861. RFL_InitAnimList ();
  1862. //
  1863. // set up the new update arrays at base position
  1864. //
  1865. updateptr = baseupdateptr;
  1866. spotptr = updateptr + PORTTILESWIDE; // used to stick "0"s after rows
  1867. updatenum = 0; // start at first visable tile
  1868. for (my=0;my<PORTTILESHIGH;my++)
  1869. {
  1870. for (mx=0;mx<PORTTILESWIDE;mx++)
  1871. {
  1872. RFL_NewTile(updatenum); // puts "1"s in both pages
  1873. RFL_CheckForAnimTile(mx+originxtile,my+originytile);
  1874. updatenum++;
  1875. }
  1876. updatenum++;
  1877. *spotptr = 0; // set a 0 at end of a line of tiles
  1878. spotptr +=(PORTTILESWIDE+1);
  1879. }
  1880. *(word *)(spotptr-PORTTILESWIDE) = UPDATETERMINATE;
  1881. }
  1882. /*
  1883. =====================
  1884. =
  1885. = RF_Scroll CGA
  1886. =
  1887. = Move the origin x/y global coordinates, readjust the screen panning, and
  1888. = scroll if needed. If the scroll distance is greater than one tile, the
  1889. = entire screen will be redrawn (this could be generalized, but scrolling
  1890. = more than one tile per refresh is a bad idea!).
  1891. =
  1892. =====================
  1893. */
  1894. void RF_Scroll (int x, int y)
  1895. {
  1896. long neworgx,neworgy;
  1897. int i,deltax,deltay,absdx,absdy;
  1898. int oldxt,oldyt,move,yy;
  1899. unsigned updatespot;
  1900. byte *spotptr;
  1901. unsigned oldoriginmap,oldscreen,newscreen,screencopy;
  1902. int screenmove;
  1903. oldxt = originxtile;
  1904. oldyt = originytile;
  1905. RFL_BoundScroll (x,y);
  1906. deltax = originxtile - oldxt;
  1907. absdx = abs(deltax);
  1908. deltay = originytile - oldyt;
  1909. absdy = abs(deltay);
  1910. if (absdx>1 || absdy>1)
  1911. {
  1912. //
  1913. // scrolled more than one tile, so start from scratch
  1914. //
  1915. RF_NewPosition(originxglobal,originyglobal);
  1916. return;
  1917. }
  1918. if (!absdx && !absdy)
  1919. return; // the screen has not scrolled an entire tile
  1920. //
  1921. // float screens
  1922. //
  1923. screenmove = deltay*16*SCREENWIDTH + deltax*TILEWIDTH;
  1924. bufferofs += screenmove;
  1925. masterofs += screenmove;
  1926. //
  1927. // float the update regions
  1928. //
  1929. move = deltax;
  1930. if (deltay==1)
  1931. move += UPDATEWIDE;
  1932. else if (deltay==-1)
  1933. move -= UPDATEWIDE;
  1934. updateptr+=move;
  1935. //
  1936. // draw the new tiles just scrolled on to the master screen, and
  1937. // mark them as needing to be copied to each screen next refreshes
  1938. // Make sure a zero is at the end of each row in update
  1939. //
  1940. if (deltax)
  1941. {
  1942. if (deltax==1)
  1943. {
  1944. RFL_NewRow (1); // new right row
  1945. RFL_RemoveAnimsOnX (originxtile-1);
  1946. }
  1947. else
  1948. {
  1949. RFL_NewRow (3); // new left row
  1950. RFL_RemoveAnimsOnX (originxtile+PORTTILESWIDE);
  1951. }
  1952. spotptr = updateptr+PORTTILESWIDE;
  1953. for (yy=0;yy<PORTTILESHIGH;yy++)
  1954. {
  1955. *spotptr = 0; // drop a 0 at end of each row
  1956. spotptr+=UPDATEWIDE;
  1957. }
  1958. }
  1959. //----------------
  1960. if (deltay)
  1961. {
  1962. if (deltay==1)
  1963. {
  1964. RFL_NewRow (2); // new bottom row
  1965. *(updateptr+UPDATEWIDE*(PORTTILESHIGH-1)+PORTTILESWIDE) = 0;
  1966. RFL_RemoveAnimsOnY (originytile-1);
  1967. }
  1968. else
  1969. {
  1970. RFL_NewRow (0); // new top row
  1971. *(updateptr+PORTTILESWIDE) = 0;
  1972. RFL_RemoveAnimsOnY (originytile+PORTTILESHIGH);
  1973. }
  1974. }
  1975. //----------------
  1976. //
  1977. // place a new terminator
  1978. //
  1979. spotptr = updateptr+UPDATEWIDE*PORTTILESHIGH-1;
  1980. *spotptr++ = 0;
  1981. *(unsigned *)spotptr = UPDATETERMINATE;
  1982. }
  1983. /*
  1984. =====================
  1985. =
  1986. = RF_PlaceSprite CGA
  1987. =
  1988. =====================
  1989. */
  1990. void RF_PlaceSprite (void **user,unsigned globalx,unsigned globaly,
  1991. unsigned spritenumber, drawtype draw, int priority)
  1992. {
  1993. spritelisttype register *sprite,*next;
  1994. spritetabletype far *spr;
  1995. spritetype _seg *block;
  1996. unsigned shift,pixx;
  1997. char str[80],str2[10];
  1998. if (!spritenumber || spritenumber == (unsigned)-1)
  1999. {
  2000. RF_RemoveSprite (user);
  2001. return;
  2002. }
  2003. sprite = (spritelisttype *)*user;
  2004. if (sprite)
  2005. {
  2006. // sprite allready exists in the list, so we can use it's block
  2007. //
  2008. // post an erase block to erase the old position by copying
  2009. // screenx,screeny,width,height
  2010. //
  2011. if (!sprite->updatecount) // may not have been drawn at all yet
  2012. memcpy (eraselistptr[0]++,sprite,sizeof(eraseblocktype));
  2013. if (priority != sprite->priority)
  2014. {
  2015. // sprite moved to another priority, so unlink the old one and
  2016. // relink it in the new priority
  2017. next = sprite->nextsprite; // cut old links
  2018. if (next)
  2019. next->prevptr = sprite->prevptr;
  2020. *sprite->prevptr = next;
  2021. goto linknewspot;
  2022. }
  2023. }
  2024. else
  2025. {
  2026. // this is a brand new sprite, so allocate a block from the array
  2027. if (!spritefreeptr)
  2028. Quit ("RF_PlaceSprite: No free spots in spritearray!");
  2029. sprite = spritefreeptr;
  2030. spritefreeptr = spritefreeptr->nextsprite;
  2031. linknewspot:
  2032. next = prioritystart[priority]; // stick it in new spot
  2033. if (next)
  2034. next->prevptr = &sprite->nextsprite;
  2035. sprite->nextsprite = next;
  2036. prioritystart[priority] = sprite;
  2037. sprite->prevptr = &prioritystart[priority];
  2038. }
  2039. //
  2040. // write the new info to the sprite
  2041. //
  2042. spr = &spritetable[spritenumber-STARTSPRITES];
  2043. block = (spritetype _seg *)grsegs[spritenumber];
  2044. if (!block)
  2045. {
  2046. strcpy (str,"RF_PlaceSprite: Placed an uncached sprite!");
  2047. itoa (spritenumber,str2,10);
  2048. strcat (str,str2);
  2049. Quit (str);
  2050. }
  2051. globaly+=spr->orgy;
  2052. globalx+=spr->orgx;
  2053. sprite->screenx = globalx >> G_CGASX_SHIFT;
  2054. sprite->screeny = globaly >> G_SY_SHIFT;
  2055. sprite->width = block->width[0];
  2056. sprite->height = spr->height;
  2057. sprite->grseg = spritenumber;
  2058. sprite->sourceofs = block->sourceoffset[0];
  2059. sprite->planesize = block->planesize[0];
  2060. sprite->draw = draw;
  2061. sprite->priority = priority;
  2062. sprite->tilex = sprite->screenx >> SX_T_SHIFT;
  2063. sprite->tiley = sprite->screeny >> SY_T_SHIFT;
  2064. sprite->tilewide = ( (sprite->screenx + sprite->width -1) >> SX_T_SHIFT )
  2065. - sprite->tilex + 1;
  2066. sprite->tilehigh = ( (sprite->screeny + sprite->height -1) >> SY_T_SHIFT )
  2067. - sprite->tiley + 1;
  2068. sprite->updatecount = 1; // draw on next refresh
  2069. // save the sprite pointer off in the user's pointer so it can be moved
  2070. // again later
  2071. *user = sprite;
  2072. }
  2073. //===========================================================================
  2074. /*
  2075. =====================
  2076. =
  2077. = RF_RemoveSprite CGA
  2078. =
  2079. =====================
  2080. */
  2081. void RF_RemoveSprite (void **user)
  2082. {
  2083. spritelisttype *sprite,*next;
  2084. sprite = (spritelisttype *)*user;
  2085. if (!sprite)
  2086. return;
  2087. //
  2088. // post an erase block to erase the old position by copying
  2089. // screenx,screeny,width,height
  2090. //
  2091. if (!sprite->updatecount)
  2092. {
  2093. memcpy (eraselistptr[0]++,sprite,sizeof(eraseblocktype));
  2094. }
  2095. //
  2096. // unlink the sprite node
  2097. //
  2098. next = sprite->nextsprite;
  2099. if (next) // if (!next), sprite is last in chain
  2100. next->prevptr = sprite->prevptr;
  2101. *sprite->prevptr = next;
  2102. //
  2103. // add it back to the free list
  2104. //
  2105. sprite->nextsprite = spritefreeptr;
  2106. spritefreeptr = sprite;
  2107. //
  2108. // null the users pointer, so next time that actor gets placed, it will
  2109. // allocate a new block
  2110. //
  2111. *user = 0;
  2112. }
  2113. /*
  2114. ====================
  2115. =
  2116. = RFL_EraseBlocks CGA
  2117. =
  2118. = Write mode 1 should be set
  2119. =
  2120. ====================
  2121. */
  2122. void RFL_EraseBlocks (void)
  2123. {
  2124. eraseblocktype *block,*done;
  2125. int screenxh,screenyh;
  2126. unsigned pos,xtl,ytl,xth,yth,x,y;
  2127. byte *updatespot;
  2128. unsigned updatedelta;
  2129. block = &eraselist[0][0];
  2130. done = eraselistptr[0];
  2131. while (block != done)
  2132. {
  2133. //
  2134. // clip the block to the current screen view
  2135. //
  2136. block->screenx -= originxscreen;
  2137. block->screeny -= originyscreen;
  2138. if (block->screenx < 0)
  2139. {
  2140. block->width += block->screenx;
  2141. if (block->width<1)
  2142. goto next;
  2143. block->screenx = 0;
  2144. }
  2145. if (block->screeny < 0)
  2146. {
  2147. block->height += block->screeny;
  2148. if (block->height<1)
  2149. goto next;
  2150. block->screeny = 0;
  2151. }
  2152. screenxh = block->screenx + block->width;
  2153. screenyh = block->screeny + block->height;
  2154. if (screenxh > CGAPORTSCREENWIDE)
  2155. {
  2156. block->width = CGAPORTSCREENWIDE-block->screenx;
  2157. screenxh = block->screenx + block->width;
  2158. }
  2159. if (screenyh > PORTSCREENHIGH)
  2160. {
  2161. block->height = PORTSCREENHIGH-block->screeny;
  2162. screenyh = block->screeny + block->height;
  2163. }
  2164. if (block->width<1 || block->height<1)
  2165. goto next;
  2166. //
  2167. // erase the block by copying from the master screen
  2168. //
  2169. pos = ylookup[block->screeny]+block->screenx;
  2170. block->width = (block->width + (pos&1) + 1)& ~1;
  2171. pos &= ~1; // make sure a word copy gets used
  2172. VW_ScreenToScreen (masterofs+pos,bufferofs+pos,
  2173. block->width,block->height);
  2174. //
  2175. // put 2s in update where the block was, to force sprites to update
  2176. //
  2177. xtl = block->screenx >> SX_T_SHIFT;
  2178. xth = (block->screenx+block->width-1) >> SX_T_SHIFT;
  2179. ytl = block->screeny >> SY_T_SHIFT;
  2180. yth = (block->screeny+block->height-1) >> SY_T_SHIFT;
  2181. updatespot = updateptr + uwidthtable[ytl] + xtl;
  2182. updatedelta = UPDATEWIDE - (xth-xtl+1);
  2183. for (y=ytl;y<=yth;y++)
  2184. {
  2185. for (x=xtl;x<=xth;x++)
  2186. *updatespot++ = 2;
  2187. updatespot += updatedelta; // down to next line
  2188. }
  2189. next:
  2190. block++;
  2191. }
  2192. eraselistptr[0] = &eraselist[0][0];
  2193. }
  2194. /*
  2195. ====================
  2196. =
  2197. = RFL_UpdateSprites CGA
  2198. =
  2199. = NOTE: Implement vertical clipping!
  2200. =
  2201. ====================
  2202. */
  2203. void RFL_UpdateSprites (void)
  2204. {
  2205. spritelisttype *sprite;
  2206. int portx,porty,x,y,xtl,xth,ytl,yth;
  2207. int priority;
  2208. unsigned dest;
  2209. byte *updatespot,*baseupdatespot;
  2210. unsigned updatedelta;
  2211. unsigned updatecount;
  2212. unsigned height,sourceofs;
  2213. #ifdef PROFILE
  2214. updatecount = 0;
  2215. #endif
  2216. for (priority=0;priority<PRIORITIES;priority++)
  2217. {
  2218. if (priority==MASKEDTILEPRIORITY)
  2219. RFL_MaskForegroundTiles ();
  2220. for (sprite = prioritystart[priority]; sprite ;
  2221. sprite = (spritelisttype *)sprite->nextsprite)
  2222. {
  2223. //
  2224. // see if the sprite has any visable area in the port
  2225. //
  2226. portx = sprite->screenx - originxscreen;
  2227. porty = sprite->screeny - originyscreen;
  2228. xtl = portx >> SX_T_SHIFT;
  2229. xth = (portx + sprite->width-1) >> SX_T_SHIFT;
  2230. ytl = porty >> SY_T_SHIFT;
  2231. yth = (porty + sprite->height-1) >> SY_T_SHIFT;
  2232. if (xtl<0)
  2233. xtl = 0;
  2234. if (xth>=PORTTILESWIDE)
  2235. xth = PORTTILESWIDE-1;
  2236. if (ytl<0)
  2237. ytl = 0;
  2238. if (yth>=PORTTILESHIGH)
  2239. yth = PORTTILESHIGH-1;
  2240. if (xtl>xth || ytl>yth)
  2241. continue;
  2242. //
  2243. // see if it's visable area covers any non 0 update tiles
  2244. //
  2245. updatespot = baseupdatespot = updateptr + uwidthtable[ytl] + xtl;
  2246. updatedelta = UPDATEWIDE - (xth-xtl+1);
  2247. if (sprite->updatecount)
  2248. {
  2249. sprite->updatecount--; // the sprite was just placed,
  2250. goto redraw; // so draw it for sure
  2251. }
  2252. for (y=ytl;y<=yth;y++)
  2253. {
  2254. for (x=xtl;x<=xth;x++)
  2255. if (*updatespot++)
  2256. goto redraw;
  2257. updatespot += updatedelta; // down to next line
  2258. }
  2259. continue; // no need to update
  2260. redraw:
  2261. //
  2262. // set the tiles it covers to 3, because those tiles are being
  2263. // updated
  2264. //
  2265. updatespot = baseupdatespot;
  2266. for (y=ytl;y<=yth;y++)
  2267. {
  2268. for (x=xtl;x<=xth;x++)
  2269. *updatespot++ = 3;
  2270. updatespot += updatedelta; // down to next line
  2271. }
  2272. //
  2273. // draw it!
  2274. //
  2275. height = sprite->height;
  2276. sourceofs = sprite->sourceofs;
  2277. if (porty<0)
  2278. {
  2279. height += porty; // clip top off
  2280. sourceofs -= porty*sprite->width;
  2281. porty = 0;
  2282. }
  2283. else if (porty+height>PORTSCREENHIGH)
  2284. {
  2285. height = PORTSCREENHIGH - porty; // clip bottom off
  2286. }
  2287. dest = bufferofs + ylookup[porty] + portx;
  2288. switch (sprite->draw)
  2289. {
  2290. case spritedraw:
  2291. VW_MaskBlock(grsegs[sprite->grseg], sourceofs,
  2292. dest,sprite->width,height,sprite->planesize);
  2293. break;
  2294. case maskdraw:
  2295. break;
  2296. }
  2297. #ifdef PROFILE
  2298. updatecount++;
  2299. #endif
  2300. }
  2301. }
  2302. }
  2303. /*
  2304. =====================
  2305. =
  2306. = RF_Refresh CGA
  2307. =
  2308. = All routines will draw at the port at bufferofs, possibly copying from
  2309. = the port at masterofs. The EGA version then page flips, while the
  2310. = CGA version updates the screen from the buffer port.
  2311. =
  2312. = Screenpage is the currently displayed page, not the one being drawn
  2313. = Otherpage is the page to be worked with now
  2314. =
  2315. =====================
  2316. */
  2317. void RF_Refresh (void)
  2318. {
  2319. long newtime,oldtimecount;
  2320. RFL_AnimateTiles ();
  2321. //
  2322. // update newly scrolled on tiles and animated tiles from the master screen
  2323. //
  2324. RFL_UpdateTiles ();
  2325. RFL_EraseBlocks ();
  2326. //
  2327. // Update is all 0 except where sprites have changed or new area has
  2328. // been scrolled on. Go through all sprites and update the ones that cover
  2329. // a non 0 update tile
  2330. //
  2331. RFL_UpdateSprites ();
  2332. //
  2333. // if the main program has a refresh hook set, call their function before
  2334. // displaying the new page
  2335. //
  2336. if (refreshvector)
  2337. refreshvector();
  2338. //
  2339. // update everything to the screen
  2340. //
  2341. VW_CGAFullUpdate ();
  2342. //
  2343. // calculate tics since last refresh for adaptive timing
  2344. //
  2345. RFL_CalcTics ();
  2346. }
  2347. #endif // GRMODE == CGAGR