Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TombRaider.cpp 191KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281
  1. /*!
  2. * \file src/TombRaider.cpp
  3. * \brief Loads maps, meshes, textures...
  4. *
  5. * \author Mongoose
  6. */
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <math.h>
  10. #include <string.h>
  11. #include <stdarg.h>
  12. #include <zlib.h>
  13. #include "TombRaider.h"
  14. #ifdef __TEST_TR5_DUMP_TGA
  15. #include "utils/tga.h"
  16. #endif
  17. TombRaider::TombRaider()
  18. {
  19. _textile8 = NULL;
  20. _textile16 = NULL;
  21. _textile32 = NULL;
  22. _tex_special = NULL;
  23. _rooms = NULL;
  24. _floor_data = NULL;
  25. _animations = NULL;
  26. _state_changes = NULL;
  27. _anim_dispatches = NULL;
  28. _anim_commands = NULL;
  29. _mesh_trees = NULL;
  30. _frames = NULL;
  31. _moveables = NULL;
  32. _static_meshes = NULL;
  33. _object_textures = NULL;
  34. _sprite_textures = NULL;
  35. _sprite_sequences = NULL;
  36. _cameras = NULL;
  37. _sound_sources = NULL;
  38. _boxes = NULL;
  39. _overlaps = NULL;
  40. _zones = NULL;
  41. _animated_textures = NULL;
  42. _items = NULL;
  43. _light_map = NULL;
  44. _cinematic_frames = NULL;
  45. _demo_data = NULL;
  46. mRoomsTR5 = 0x0;
  47. mMeshes = 0x0;
  48. mSoundMap = 0x0;
  49. mSoundDetails = 0x0;
  50. mSampleIndices = 0x0;
  51. mSampleIndicesTR5 = 0x0;
  52. mRiffData = 0x0;
  53. mTR4Samples = 0x0;
  54. mTR4SamplesSz = 0x0;
  55. mRiffAlternateOffsets = 0x0;
  56. mCompressedLevelData = 0x0;
  57. moveablesTR5 = 0x0;
  58. animationsTR5 = 0x0;
  59. objectTexturesTR5 = 0x0;
  60. cinematicFramesTR5 = 0x0;
  61. flyByCamerasTR5 = 0x0;
  62. mNumTR4Samples = 0;
  63. mReset = false;
  64. mDebug = false;
  65. mRiffAlternateLoaded = false;
  66. mRoomVertexLightingFactor = 50.0f;
  67. mTexelScale = 256.0f;
  68. mRiffDataSz = 0;
  69. reset();
  70. }
  71. TombRaider::~TombRaider()
  72. {
  73. reset();
  74. }
  75. int TombRaider::NumMoveables()
  76. {
  77. return _num_moveables;
  78. }
  79. int TombRaider::NumRooms()
  80. {
  81. return _num_rooms;
  82. }
  83. int TombRaider::NumAnimations()
  84. {
  85. return _num_animations;
  86. }
  87. unsigned int TombRaider::NumFrames()
  88. {
  89. return _num_frames;
  90. }
  91. int TombRaider::NumStaticMeshes()
  92. {
  93. return _num_static_meshes;
  94. }
  95. int TombRaider::NumSprites()
  96. {
  97. return _num_sprite_textures;
  98. }
  99. int TombRaider::NumSpriteSequences()
  100. {
  101. return _num_sprite_sequences;
  102. }
  103. int TombRaider::NumItems()
  104. {
  105. return _num_items;
  106. }
  107. int TombRaider::NumTextures()
  108. {
  109. //return _num_room_textures + _num_misc_textures + _num_bump_map_textures / 2;
  110. return _num_textiles - _num_bump_map_textures / 2;
  111. }
  112. tr2_room_t *TombRaider::Room()
  113. {
  114. return _rooms;
  115. }
  116. tr2_item_t *TombRaider::Item()
  117. {
  118. return _items;
  119. }
  120. tr2_object_texture_t *TombRaider::ObjectTextures()
  121. {
  122. return _object_textures;
  123. }
  124. unsigned int TombRaider::getNumBoxes()
  125. {
  126. return _num_boxes;
  127. }
  128. tr2_box_t *TombRaider::Box()
  129. {
  130. return _boxes;
  131. }
  132. tr2_mesh_t *TombRaider::Mesh()
  133. {
  134. /*
  135. if (n > 0 || n > mMeshCount)
  136. return NULL;
  137. return _meshes+n;
  138. */
  139. return mMeshes;
  140. }
  141. int TombRaider::getNumAnimsForMoveable(int moveable_index)
  142. {
  143. /***************************************************************************
  144. * It seems the number of animations isn't available in the moveable,
  145. * so we have to calculate it:
  146. * - Get the "# starting anim" for the next moveable (->N)
  147. * - Substract the "# starting anim" for moveable to N
  148. *
  149. * Doing this, we assume that the next moveable has its animations following
  150. * the animations of the current moveable (seems right for all tested
  151. * levels, but...)
  152. *
  153. * We also have to deal with the fact that the next moveable
  154. * could have "# starting anim" == -1
  155. * (ie. anim handled by the engine, like the ponytail anim).
  156. * If it's the case, we skip the moveable
  157. * and use the next moveable for our computation
  158. *
  159. * - Mongoose, Notes I edited from TRViewer
  160. **************************************************************************/
  161. int start_anim;
  162. int next_start_anim = 0xFFFF;
  163. tr2_moveable_t *last_moveable = 0x0;
  164. tr2_moveable_t *moveable = 0x0;
  165. tr2_moveable_t *next_moveable = 0x0;
  166. if ((moveable_index >= 0 &&
  167. moveable_index <= (int)_num_moveables) || _num_moveables < 1)
  168. {
  169. moveable = &_moveables[moveable_index];
  170. }
  171. if (!moveable)
  172. {
  173. return -1; // Was 0
  174. }
  175. last_moveable = &_moveables[moveable_index-1];
  176. start_anim = moveable->animation;
  177. while (moveable != last_moveable)
  178. {
  179. next_moveable = moveable + 1;
  180. next_start_anim = next_moveable->animation;
  181. if (next_start_anim != 0xFFFF)
  182. break;
  183. moveable = next_moveable++;
  184. }
  185. if (moveable == last_moveable)
  186. {
  187. next_start_anim = _num_animations;
  188. }
  189. return ((start_anim != 0xFFFF) ? next_start_anim - start_anim : 0);
  190. }
  191. tr2_staticmesh_t *TombRaider::StaticMesh()
  192. {
  193. return _static_meshes;
  194. }
  195. tr2_version_type TombRaider::Engine()
  196. {
  197. return mEngineVersion;
  198. }
  199. tr2_animation_t *TombRaider::Animation()
  200. {
  201. return _animations;
  202. }
  203. unsigned short *TombRaider::Frame()
  204. {
  205. return _frames;
  206. }
  207. tr2_moveable_t *TombRaider::Moveable()
  208. {
  209. /*
  210. if (n > 0 || n > (int)_num_moveables)
  211. return NULL;
  212. */
  213. return _moveables;
  214. }
  215. tr2_meshtree_t *TombRaider::MeshTree()
  216. {
  217. /*
  218. if (n > 0 || n > (int)_num_mesh_trees)
  219. return NULL;
  220. */
  221. return _mesh_trees;
  222. }
  223. tr2_sprite_texture_t *TombRaider::Sprite()
  224. {
  225. return _sprite_textures;
  226. }
  227. tr2_sprite_sequence_t *TombRaider::SpriteSequence()
  228. {
  229. return _sprite_sequences;
  230. }
  231. unsigned char *TombRaider::SpecialTexTile(int texture)
  232. {
  233. unsigned char *image;
  234. unsigned char *ptr;
  235. image = NULL;
  236. if (texture >=0 && texture < NumSpecialTextures())
  237. {
  238. // Get base and offset into 32bit special textures/bump maps
  239. ptr = _tex_special;
  240. ptr += 256*256*4*texture;
  241. // Clone it as a single 256x256 @ 32bpp image
  242. image = new unsigned char[256*256*4];
  243. memcpy(image, ptr, 256*256*4);
  244. }
  245. return image;
  246. }
  247. int TombRaider::NumSpecialTextures()
  248. {
  249. return _num_tex_special;
  250. }
  251. void TombRaider::Texture(int texture, unsigned char **image,
  252. unsigned char **bumpmap)
  253. {
  254. int bumpmap_base = _num_room_textures + _num_misc_textures;
  255. *image = getTexTile(texture);
  256. *bumpmap = NULL;
  257. if (_num_bump_map_textures && texture >= bumpmap_base)
  258. {
  259. *bumpmap = getTexTile(texture + _num_bump_map_textures/2);
  260. }
  261. }
  262. unsigned int *TombRaider::Palette16()
  263. {
  264. return _palette16;
  265. }
  266. unsigned char *TombRaider::Palette8()
  267. {
  268. return (unsigned char *)_palette8;
  269. }
  270. int TombRaider::checkMime(char *filename)
  271. {
  272. FILE *f;
  273. unsigned int version;
  274. if (!filename || !filename[0])
  275. {
  276. print("checkFile", "Given filename was empty string or NULL");
  277. return -1;
  278. }
  279. f = fopen(filename, "rb");
  280. if (!f)
  281. {
  282. perror(filename);
  283. return -1;
  284. }
  285. //! \fixme Endianess
  286. fread(&version, sizeof(version), 1, f);
  287. fclose(f);
  288. switch (version)
  289. {
  290. case 0x00000020:
  291. case 0x0000002d:
  292. case 0xff080038:
  293. case 0xff180038:
  294. case 0xfffffff0: // bogus
  295. case 0x00345254: // "TR4\0"
  296. return 0;
  297. default:
  298. ;
  299. }
  300. return 1;
  301. }
  302. int TombRaider::Load(char *filename, void (*percent)(int))
  303. {
  304. FILE *f;
  305. int i, j, l;
  306. unsigned int num_mesh_data_words, num_mesh_pointers, data_size, data_offset;
  307. unsigned int *mesh_pointer_list;
  308. unsigned char *raw_mesh_data;
  309. bool tr5;
  310. long debugf;
  311. f = fopen(filename, "rb");
  312. if (!f)
  313. {
  314. perror(filename);
  315. return -1;
  316. }
  317. if (percent)
  318. (*percent)(1);
  319. mReset = false;
  320. Fread(&mPakVersion, sizeof(mPakVersion), 1, f);
  321. //! \fixme endian
  322. printDebug("Load", "mPakVersion = %u", mPakVersion);
  323. tr5 = false;
  324. switch (mPakVersion)
  325. {
  326. case 0x00000020:
  327. mEngineVersion = TR_VERSION_1;
  328. break;
  329. case 0x0000002d:
  330. mEngineVersion = TR_VERSION_2;
  331. break;
  332. case 0xff080038:
  333. case 0xff180038:
  334. mEngineVersion = TR_VERSION_3;
  335. break;
  336. case 0xfffffff0: // bogus
  337. case 0x00345254: // "TR4\0"
  338. mEngineVersion = TR_VERSION_4;
  339. // Check to see if this is really a TR5 demo
  340. l = strlen(filename);
  341. // Looking for pattern "filename.trc"
  342. if ((filename[l-1] == 'c' || filename[l-1] == 'C') &&
  343. (filename[l-2] == 'r' || filename[l-2] == 'R') &&
  344. (filename[l-3] == 't' || filename[l-3] == 'T'))
  345. {
  346. printDebug("Load", "This is really a TR5 pak");
  347. mEngineVersion = TR_VERSION_5;
  348. return loadTR5(f, percent);
  349. }
  350. break;
  351. default:
  352. mEngineVersion = TR_VERSION_UNKNOWN;
  353. }
  354. if (percent)
  355. (*percent)(5);
  356. printDebug("Load", "mEngineVersion = 0x%x", mPakVersion);
  357. if (mEngineVersion == TR_VERSION_UNKNOWN)
  358. return -1;
  359. if (mEngineVersion == TR_VERSION_4)
  360. {
  361. unsigned int sz, usz; // compressed and uncompressed size
  362. unsigned char *compressed_data = NULL;
  363. int zerr;
  364. uLongf foo;
  365. // Read texture type offsets
  366. Fread(&_num_room_textures, 2, 1, f);
  367. printDebug("LoadTR4", "_num_room_textures = %u", _num_room_textures);
  368. Fread(&_num_misc_textures, 2, 1, f);
  369. printDebug("LoadTR4", "_num_misc_textures = %u", _num_misc_textures);
  370. Fread(&_num_bump_map_textures, 2, 1, f);
  371. printDebug("LoadTR4", "_num_bump_map_textures = %u", _num_bump_map_textures);
  372. // Read the sizes of the 32-bit textures
  373. Fread(&usz, sizeof(usz), 1, f);
  374. Fread(&sz, sizeof(sz), 1, f);
  375. printDebug("Load", "TR4 32-bit textures compressed size = %u bytes", sz);
  376. printDebug("Load", "TR4 32-bit textures uncompressed size = %u bytes", usz);
  377. _num_textiles = usz / sizeof(tr2_textile32_t);
  378. printDebug("LoadTR4", "_num_textiles = %i/%lu = %i",
  379. usz, sizeof(tr2_textile32_t), _num_textiles);
  380. _textile32 = new tr2_textile32_t[_num_textiles];
  381. // Allocate a temporary buffer for decompression
  382. compressed_data = new unsigned char[sz];
  383. Fread(compressed_data, sz, 1, f);
  384. // Decompress the textures
  385. foo = usz;
  386. zerr = uncompress((unsigned char *)_textile32,
  387. &foo,
  388. compressed_data,
  389. sz);
  390. usz = foo;
  391. if (percent)
  392. (*percent)(6);
  393. printDebug("LoadTR4", "textile decompress [%s]",
  394. (zerr == Z_OK) ? "OK" : "ERROR");
  395. switch (zerr)
  396. {
  397. case Z_MEM_ERROR:
  398. printDebug("LoadTR4", "There was not enough memory");
  399. break;
  400. case Z_BUF_ERROR:
  401. printDebug("LoadTR4", "There was not enough room in the output buffer");
  402. break;
  403. case Z_DATA_ERROR:
  404. printDebug("LoadTR4", "The input data was corrupted");
  405. break;
  406. default:
  407. printDebug("LoadTR4", "textile decompress %i", zerr);
  408. }
  409. // Free the temporary buffer
  410. delete [] compressed_data;
  411. // Read in the 16-bit textures, set NumTextiles
  412. Fread(&usz, sizeof(usz), 1, f);
  413. Fread(&sz, sizeof(sz), 1, f);
  414. printDebug("Load", "TR4 16-bit textures compressed size = %u bytes", sz);
  415. printDebug("Load", "TR4 16-bit textures uncompressed size = %u bytes", usz);
  416. _num_textiles = usz / sizeof(tr2_textile16_t);
  417. printDebug("Load", "TR4 _num_textiles = %i/%lu = %i",
  418. usz, sizeof(tr2_textile16_t), _num_textiles);
  419. _textile16 = new tr2_textile16_t[_num_textiles];
  420. // Allocate a temporary buffer for decompression
  421. compressed_data = new unsigned char[sz];
  422. Fread(compressed_data, sz, 1, f);
  423. // Decompress the textures
  424. foo = usz;
  425. zerr = uncompress((unsigned char *)_textile16,
  426. &foo,
  427. compressed_data,
  428. sz);
  429. usz = foo;
  430. if (percent)
  431. (*percent)(7);
  432. // printDebug("Load", "TR4 textile decompress [%s]",
  433. // (zerr == Z_OK) ? "OK" : "ERROR");
  434. switch (zerr)
  435. {
  436. case Z_MEM_ERROR:
  437. printDebug("Load", "TR4 textile decompress [ERROR]");
  438. printDebug("Load", "TR4 There was not enough memory");
  439. break;
  440. case Z_BUF_ERROR:
  441. printDebug("Load", "TR4 textile decompress [ERROR]");
  442. printDebug("Load", "TR4 There was not enough room in the output buffer");
  443. break;
  444. case Z_DATA_ERROR:
  445. printDebug("Load", "TR4 textile decompress [ERROR]");
  446. printDebug("Load", "TR4 The input data was corrupted");
  447. break;
  448. case Z_OK:
  449. printDebug("Load", "TR4 textile decompress [OK]");
  450. break;
  451. default:
  452. printDebug("Load", "TR4 textile decompress %i", zerr);
  453. }
  454. // Free the temporary buffer
  455. delete [] compressed_data;
  456. // Read the sizes of the sprite textures
  457. Fread(&usz, sizeof(usz), 1, f);
  458. Fread(&sz, sizeof(sz), 1, f);
  459. printDebug("Load", "TR4 sprite textures compressed size = %u bytes", sz);
  460. printDebug("Load", "TR4 sprite textures uncompressed size = %u bytes", usz);
  461. // Load sprite/bump map/gui/etc textures also
  462. _num_tex_special = usz/(256*256*4);
  463. printDebug("LoadTR5", "_num_tex_special = %i/%i = %i",
  464. usz, 256*256*4, _num_tex_special);
  465. printDebug("LoadTR5", "Reading %ibytes of sprite textures", usz);
  466. if (usz)
  467. {
  468. _tex_special = new unsigned char[usz];
  469. // Allocate a temporary buffer for decompression
  470. compressed_data = new unsigned char[sz];
  471. Fread(compressed_data, sz, 1, f);
  472. // Decompress the textures
  473. foo = usz;
  474. zerr = uncompress(_tex_special,
  475. &foo,
  476. compressed_data,
  477. sz);
  478. usz = foo;
  479. printDebug("LoadTR5", "special texture decompress [%s]",
  480. (zerr == Z_OK) ? "OK" : "ERROR");
  481. switch (zerr)
  482. {
  483. case Z_MEM_ERROR:
  484. printDebug("LoadTR5", "There was not enough memory");
  485. break;
  486. case Z_BUF_ERROR:
  487. printDebug("LoadTR5", "There was not enough room in the output buffer");
  488. break;
  489. case Z_DATA_ERROR:
  490. printDebug("LoadTR5", "The input data was corrupted");
  491. break;
  492. default:
  493. printDebug("LoadTR5", "textile decompress %i", zerr);
  494. }
  495. // Free the temporary buffer
  496. delete [] compressed_data;
  497. }
  498. if (percent)
  499. (*percent)(9);
  500. // Read the sizes of the level data
  501. Fread(&usz, sizeof(usz), 1, f);
  502. Fread(&sz, sizeof(sz), 1, f);
  503. printDebug("Load", "TR4 level data compressed size = %u bytes", sz);
  504. printDebug("Load", "TR4 level data uncompressed size = %u bytes", usz);
  505. // Allocate a temporary buffer for decompression
  506. compressed_data = new unsigned char[sz];
  507. Fread(compressed_data, sz, 1, f);
  508. mCompressedLevelData = new unsigned char[usz];
  509. // Decompress the level data
  510. foo = usz;
  511. zerr = uncompress(mCompressedLevelData, &foo, compressed_data, sz);
  512. usz = foo;
  513. printDebug("Load", "TR4 level data decompress [%s]",
  514. (zerr == Z_OK) ? "OK" : "ERROR");
  515. switch (zerr)
  516. {
  517. case Z_MEM_ERROR:
  518. printDebug("Load", "TR4 There was not enough memory");
  519. break;
  520. case Z_BUF_ERROR:
  521. printDebug("Load", "TR4 There was not enough room in the output buffer");
  522. break;
  523. case Z_DATA_ERROR:
  524. printDebug("Load", "TR4 The input data was corrupted");
  525. break;
  526. }
  527. delete [] compressed_data;
  528. mCompressedLevelDataOffset = 0;
  529. mCompressedLevelSize = usz;
  530. // Toggle Fread mode to read from decompressed data in memory, not diskfile
  531. mFreadMode = TR_FREAD_COMPRESSED;
  532. }
  533. if (mEngineVersion == TR_VERSION_2 || mEngineVersion == TR_VERSION_3)
  534. {
  535. /* Read the 8-bit palette */
  536. Fread(_palette8, sizeof(tr2_colour_t), 256, f);
  537. /* Read 16-bit palette */
  538. Fread(_palette16, sizeof(_palette16), 1, f);
  539. printDebug("Load", "Read TR 2|3 8bit and 16bit palettes");
  540. }
  541. if (mEngineVersion != TR_VERSION_4)
  542. {
  543. /* Read the textiles */
  544. Fread(&_num_textiles, sizeof(_num_textiles), 1, f);
  545. printDebug("Load", "_num_textiles = %i", _num_textiles);
  546. /* 8-bit textiles come first */
  547. _textile8 = new tr2_textile8_t[_num_textiles];
  548. Fread(_textile8, sizeof(tr2_textile8_t), _num_textiles, f);
  549. /* 16-bit textiles come second */
  550. _textile16 = new tr2_textile16_t[_num_textiles];
  551. if (mEngineVersion != TR_VERSION_1)
  552. {
  553. //! \fixme need endian checking here
  554. Fread(_textile16, sizeof(tr2_textile16_t), _num_textiles, f);
  555. printDebug("Load", "Read in 16bit texture tiles");
  556. }
  557. }
  558. if (percent)
  559. (*percent)(10);
  560. /* 32-bit unknown - seems to always be 0 */
  561. Fread(&_unknown_t, sizeof(_unknown_t), 1, f);
  562. printDebug("Load", "_unknown_t = 0x%x", _unknown_t);
  563. /* Read raw room data */
  564. //! \fixme needs endian checking
  565. Fread(&_num_rooms, sizeof(_num_rooms), 1, f);
  566. printDebug("Load", "_num_rooms = %i", _num_rooms);
  567. data_size = _num_rooms * sizeof(tr2_room_t);
  568. _rooms = new tr2_room_t[_num_rooms];
  569. /* Extract room details */
  570. for (i = 0; i < _num_rooms; ++i)
  571. {
  572. if (percent)
  573. {
  574. (*percent)(11 + (int)(((float)i/(float)_num_rooms)*70.0));
  575. }
  576. /* Read RoomInfo */
  577. //! \fixme endian check needed
  578. Fread(&_rooms[i].info, sizeof(tr2_room_info_t), 1, f);
  579. printDebug("Load", "_rooms[%i].info =\n { x=%i, z=%i, yt=%i, yb=%i}",
  580. i,
  581. _rooms[i].info.x, _rooms[i].info.z,
  582. _rooms[i].info.y_top, _rooms[i].info.y_bottom);
  583. /* Read raw data for rest of room */
  584. Fread(&_rooms[i].num_data_words, sizeof(_rooms[i].num_data_words), 1, f);
  585. printDebug("Load", "_rooms[%i].num_data_words = %u",
  586. i, _rooms[i].num_data_words);
  587. _rooms[i].data = new unsigned char[_rooms[i].num_data_words*2];
  588. Fread(_rooms[i].data, 2, _rooms[i].num_data_words, f);
  589. /* Identify vertices */
  590. data_offset = 0;
  591. //! \fixme endian
  592. _rooms[i].room_data.num_vertices = *(short *)(void *)(_rooms[i].data);
  593. data_offset += sizeof(_rooms[0].room_data.num_vertices);
  594. data_size = _rooms[i].room_data.num_vertices * sizeof(tr2_vertex_room_t);
  595. printDebug("Load", "_rooms[%i].room_data.num_vertices = %u",
  596. i, _rooms[i].room_data.num_vertices);
  597. _rooms[i].room_data.vertices = 0x0;
  598. if (_rooms[i].room_data.num_vertices > 0)
  599. {
  600. _rooms[i].room_data.vertices =
  601. new tr2_vertex_room_t[_rooms[i].room_data.num_vertices];
  602. if (mEngineVersion == TR_VERSION_1)
  603. {
  604. data_size = _rooms[i].room_data.num_vertices *
  605. (sizeof(tr2_vertex_room_t) - 4);
  606. for (j = 0; j < _rooms[i].room_data.num_vertices; ++j)
  607. {
  608. memcpy(&_rooms[i].room_data.vertices[j],
  609. _rooms[i].data + data_offset +
  610. (j * (sizeof(tr2_vertex_room_t) - 4)),
  611. sizeof(tr2_vertex_room_t) - 4);
  612. // ??? Adjust for what's missing?
  613. _rooms[i].room_data.vertices[j].lighting2 =
  614. _rooms[i].room_data.vertices[j].lighting1;
  615. _rooms[i].room_data.vertices[j].attributes = 0;
  616. }
  617. }
  618. else
  619. {
  620. memcpy(_rooms[i].room_data.vertices,
  621. _rooms[i].data + data_offset, data_size);
  622. }
  623. //! \fixme endian conversions for verts needed
  624. }
  625. data_offset += data_size;
  626. /* identify rectangles */
  627. //! \fixme endian conversion
  628. _rooms[i].room_data.num_rectangles =
  629. *(short *)(void *)(_rooms[i].data + data_offset);
  630. data_offset += sizeof(_rooms[0].room_data.num_rectangles);
  631. data_size = _rooms[i].room_data.num_rectangles * sizeof(tr2_quad_t);
  632. printDebug("Load", "_rooms[%i].room_data.num_rectangles = %i",
  633. i, _rooms[i].room_data.num_rectangles);
  634. _rooms[i].room_data.rectangles = 0x0;
  635. if (_rooms[i].room_data.num_rectangles > 0)
  636. {
  637. _rooms[i].room_data.rectangles =
  638. new tr2_quad_t[_rooms[i].room_data.num_rectangles];
  639. memcpy(_rooms[i].room_data.rectangles,
  640. _rooms[i].data + data_offset, data_size);
  641. if (mEngineVersion >= TR_VERSION_3)
  642. {
  643. for (j = 0; j < _rooms[i].room_data.num_rectangles; ++j)
  644. {
  645. _rooms[i].room_data.rectangles[j].texture &= 0x7fff;
  646. }
  647. }
  648. //! \fixme endian conversion
  649. }
  650. data_offset += data_size;
  651. /* Identify triangles */
  652. _rooms[i].room_data.num_triangles =
  653. *(short *)(void *)(_rooms[i].data + data_offset);
  654. //! \fixme endian
  655. data_offset += sizeof(_rooms[0].room_data.num_triangles);
  656. data_size = _rooms[i].room_data.num_triangles * sizeof(tr2_tri_t);
  657. printDebug("Load", "_rooms[%i].room_data.num_triangles = %i",
  658. i, _rooms[i].room_data.num_triangles);
  659. _rooms[i].room_data.triangles = 0x0;
  660. if (_rooms[i].room_data.num_triangles > 0)
  661. {
  662. _rooms[i].room_data.triangles =
  663. new tr2_tri_t[_rooms[i].room_data.num_triangles];
  664. memcpy(_rooms[i].room_data.triangles,
  665. _rooms[i].data + data_offset, data_size);
  666. if (mEngineVersion >= TR_VERSION_3)
  667. {
  668. for (j = 0; j < _rooms[i].room_data.num_triangles; ++j)
  669. {
  670. _rooms[i].room_data.triangles[j].texture &= 0x7fff;
  671. }
  672. //! \fixme endian
  673. }
  674. }
  675. data_offset += data_size;
  676. /* Identify sprites */
  677. _rooms[i].room_data.num_sprites =
  678. *(short *)(void *)(_rooms[i].data + data_offset);
  679. //! \fixme endian
  680. data_offset += sizeof(_rooms[0].room_data.num_sprites);
  681. data_size = _rooms[i].room_data.num_sprites * sizeof(tr2_room_sprite_t);
  682. printDebug("Load", "_rooms[%i].room_data.num_sprites = %i",
  683. i, _rooms[i].room_data.num_sprites);
  684. _rooms[i].room_data.sprites = 0x0;
  685. if (_rooms[i].room_data.num_sprites > 0)
  686. {
  687. _rooms[i].room_data.sprites =
  688. new tr2_room_sprite_t[_rooms[i].room_data.num_sprites];
  689. memcpy(_rooms[i].room_data.sprites,
  690. _rooms[i].data + data_offset, data_size);
  691. if (mEngineVersion >= TR_VERSION_3)
  692. {
  693. for (j = 0; j < _rooms[i].room_data.num_sprites; j++)
  694. {
  695. _rooms[i].room_data.sprites[j].texture &= 0x7fff;
  696. }
  697. }
  698. //! \fixme endian
  699. }
  700. /* Free the raw room data */
  701. delete [] _rooms[i].data;
  702. _rooms[i].data = NULL;
  703. /* Read door info */
  704. //! \fixme endian
  705. Fread(&_rooms[i].num_portals, sizeof(_rooms[0].num_portals), 1, f);
  706. printDebug("Load", "_rooms[%i].num_portals = %i",
  707. i, _rooms[i].num_portals);
  708. if (_rooms[i].num_portals > 0)
  709. _rooms[i].portals = new tr2_room_portal_t[_rooms[i].num_portals];
  710. else
  711. _rooms[i].portals = 0;
  712. Fread(_rooms[i].portals, sizeof(tr2_room_portal_t),
  713. _rooms[i].num_portals, f);
  714. //! \fixme endian
  715. /* Read sector info */
  716. //! \fixme endian
  717. Fread(&_rooms[i].num_zsectors, sizeof(_rooms[0].num_zsectors), 1, f);
  718. Fread(&_rooms[i].num_xsectors, sizeof(_rooms[0].num_xsectors), 1, f);
  719. printDebug("Load", "_rooms[%i].num_zsectors = %i",
  720. i, _rooms[i].num_zsectors);
  721. printDebug("Load", "_rooms[%i].num_xsectors = %i",
  722. i, _rooms[i].num_xsectors);
  723. if (_rooms[i].num_zsectors > 0 && _rooms[i].num_xsectors > 0)
  724. {
  725. _rooms[i].sector_list =
  726. new tr2_room_sector_t[_rooms[i].num_zsectors * _rooms[i].num_xsectors];
  727. }
  728. else
  729. {
  730. _rooms[i].sector_list = 0x0;
  731. }
  732. Fread(_rooms[i].sector_list, sizeof(tr2_room_sector_t),
  733. _rooms[i].num_zsectors * _rooms[i].num_xsectors, f);
  734. //! \fixme endian
  735. printDebug("Load", "Read %u room sectors",
  736. _rooms[i].num_zsectors * _rooms[i].num_xsectors);
  737. /* Read room lighting & mode */
  738. if (mEngineVersion >= TR_VERSION_3)
  739. {
  740. Fread(&_rooms[i].intensity1, 4, 1, f);
  741. // Fake TR2 record:
  742. _rooms[i].light_mode = 0;
  743. }
  744. else if (mEngineVersion == TR_VERSION_1)
  745. {
  746. Fread(&_rooms[i].intensity1, 2, 1, f);
  747. // Is this intensity or LightMode?
  748. printDebug("Load", "_rooms[%i].intensity1 = %u",
  749. i, _rooms[i].intensity1);
  750. _rooms[i].intensity2 = _rooms[i].intensity1;
  751. _rooms[i].light_mode = 0;
  752. }
  753. else
  754. { // TR2
  755. Fread(&_rooms[i].intensity1, 6, 1, f);
  756. printDebug("Load", "TR2 _rooms[%i].intensity1 = %u",
  757. i, _rooms[i].intensity1);
  758. }
  759. /* Read room lighting info */
  760. //! \fixme endian
  761. Fread(&_rooms[i].num_lights, sizeof(_rooms[i].num_lights), 1, f);
  762. printDebug("Load", "_rooms[%i].num_lights = %u",
  763. i, _rooms[i].num_lights);
  764. _rooms[i].lights = 0x0;
  765. _rooms[i].tr4Lights = 0x0;
  766. // Mongoose 2002.04.03, New TR4 light struct, removed old
  767. // double size for others
  768. if (_rooms[i].num_lights > 0)
  769. {
  770. if (mEngineVersion == TR_VERSION_1)
  771. {
  772. _rooms[i].lights = new tr2_room_light_t[_rooms[i].num_lights];
  773. for (j = 0; j < _rooms[i].num_lights; ++j)
  774. {
  775. Fread(&_rooms[i].lights[j].x, sizeof(_rooms[0].lights[0].x), 3,f);
  776. // x, y, z
  777. printDebug("Load", "_rooms[%i].lights[%i] = <%i %i %i>",
  778. i, j,
  779. _rooms[i].lights[j].x,
  780. _rooms[i].lights[j].y,
  781. _rooms[i].lights[j].z);
  782. Fread(&_rooms[i].lights[j].intensity1, sizeof(short), 1, f);
  783. // Intensity1
  784. printDebug("Load", "_rooms[%i].lights[%i].intensity1 = %u",
  785. i, j,
  786. _rooms[i].lights[j].intensity1);
  787. _rooms[i].lights[j].intensity2 = _rooms[i].lights[j].intensity1;
  788. Fread(&_rooms[i].lights[j].fade1, sizeof(unsigned int), 1, f);
  789. // Fade1
  790. printDebug("Load", "_rooms[%i].lights[%i].fade1 = %u",
  791. i, j,
  792. _rooms[i].lights[j].fade1);
  793. _rooms[i].lights[j].fade2 = _rooms[i].lights[j].fade1;
  794. }
  795. }
  796. else if (mEngineVersion == TR_VERSION_4)
  797. {
  798. _rooms[i].tr4Lights = new tr4_room_light_t[_rooms[i].num_lights];
  799. Fread(_rooms[i].tr4Lights, sizeof(tr4_room_light_t),
  800. _rooms[i].num_lights, f);
  801. }
  802. else
  803. {
  804. _rooms[i].lights = new tr2_room_light_t[_rooms[i].num_lights];
  805. Fread(_rooms[i].lights, sizeof(tr2_room_light_t),
  806. _rooms[i].num_lights, f);
  807. }
  808. }
  809. //! \fixme endian
  810. /* Read Static Mesh Data */
  811. Fread(&_rooms[i].num_static_meshes, sizeof(unsigned short), 1, f);
  812. //! \fixme endian
  813. printDebug("Load", "_rooms[%i].num_static_meshes = %u",
  814. i, _rooms[i].num_static_meshes);
  815. _rooms[i].static_meshes = 0x0;
  816. if (_rooms[i].num_static_meshes > 0)
  817. {
  818. _rooms[i].static_meshes =
  819. new tr2_room_staticmesh_t[_rooms[i].num_static_meshes];
  820. if (mEngineVersion == TR_VERSION_1)
  821. {
  822. for (j = 0; j < _rooms[i].num_static_meshes; j++)
  823. {
  824. Fread(&_rooms[i].static_meshes[j], 18, 1, f);
  825. // Account for the missing .intensity2
  826. _rooms[i].static_meshes[j].object_id =
  827. _rooms[i].static_meshes[j].intensity2;
  828. _rooms[i].static_meshes[j].intensity2 =
  829. _rooms[i].static_meshes[j].intensity1;
  830. }
  831. }
  832. else
  833. {
  834. Fread(_rooms[i].static_meshes, sizeof(tr2_room_staticmesh_t),
  835. _rooms[i].num_static_meshes, f);
  836. }
  837. }
  838. //! \fixme endian
  839. Fread(&_rooms[i].alternate_room, sizeof(short), 1, f);
  840. //! \fixme endian
  841. printDebug("Load", "_rooms[%i].alternate_room = %i",
  842. i, _rooms[i].alternate_room);
  843. Fread(&_rooms[i].flags, sizeof(short), 1, f);
  844. //! \fixme endian
  845. printDebug("Load", "_rooms[%i].flags = 0x%x",
  846. i, _rooms[i].flags);
  847. /* Read TR3 room light colour */
  848. if (mEngineVersion >= TR_VERSION_3)
  849. {
  850. /* we force this to be 3 bytes
  851. (instead of just sizeof(room_light_colour))
  852. for Macs and others that can't handle odd-length structures...
  853. */
  854. Fread(&_rooms[i].room_light_colour, 3, 1, f);
  855. printDebug("Load", "TR3 _rooms[%i].room_light_colour {%i %i %i}",
  856. i,
  857. _rooms[i].room_light_colour.r,
  858. _rooms[i].room_light_colour.g,
  859. _rooms[i].room_light_colour.b);
  860. }
  861. }
  862. /* Read floor data */
  863. /*
  864. * Really, FloorData should be a per-sector dynamic allocation; however,
  865. * that requires a parser that can accurately determine where one sector's
  866. * FloorData ends and another's begins. Until we have that, we'll stick to
  867. * this crude (but effective) method...
  868. */
  869. Fread(&_num_floor_data, sizeof(_num_floor_data), 1, f);
  870. printDebug("Load", "_num_floor_data = %u", _num_floor_data);
  871. _floor_data = 0x0;
  872. if (_num_floor_data > 0)
  873. {
  874. _floor_data = new unsigned short[_num_floor_data];
  875. Fread(_floor_data, sizeof(short), _num_floor_data, f);
  876. //! \fixme endian
  877. }
  878. /* Read mesh data */
  879. Fread(&num_mesh_data_words, sizeof(num_mesh_data_words), 1, f);
  880. //! \fixme endian
  881. printDebug("Load", "num_mesh_data_words = %u", num_mesh_data_words);
  882. raw_mesh_data = new unsigned char[num_mesh_data_words*2];
  883. Fread(raw_mesh_data, 2, num_mesh_data_words, f);
  884. // Endian-conversion of this data occurs in ExtractMeshes()
  885. printDebug("Load", "Read raw_mesh_data");
  886. /* Read mesh pointers */
  887. Fread(&num_mesh_pointers, sizeof(num_mesh_pointers), 1, f);
  888. //! \fixme endian
  889. printDebug("Load", "num_mesh_pointers = %u", num_mesh_pointers);
  890. mesh_pointer_list = new unsigned int[num_mesh_pointers];
  891. Fread(mesh_pointer_list, sizeof(unsigned int), num_mesh_pointers, f);
  892. //! \fixme endian
  893. printDebug("Load", "Read mesh_pointer_list");
  894. /* Extract meshes */
  895. extractMeshes(raw_mesh_data, num_mesh_pointers, mesh_pointer_list);
  896. delete [] raw_mesh_data;
  897. delete [] mesh_pointer_list;
  898. /* Read animations */
  899. Fread(&_num_animations, sizeof(_num_animations), 1, f);
  900. //! \fixme endian
  901. printDebug("Load", "_num_animations = %u", _num_animations);
  902. _animations = 0x0;
  903. if (_num_animations > 0)
  904. {
  905. _animations = new tr2_animation_t[_num_animations];
  906. if (mEngineVersion == TR_VERSION_4)
  907. {
  908. tr4_animation_t tr4_anim;
  909. for (i = 0; i < (int)_num_animations; ++i)
  910. {
  911. Fread(&tr4_anim, 40, 1, f);
  912. _animations[i].frame_offset = tr4_anim.frame_offset;
  913. _animations[i].frame_rate = tr4_anim.frame_rate;
  914. _animations[i].frame_size = tr4_anim.frame_size;
  915. _animations[i].state_id = tr4_anim.state_id;
  916. _animations[i].unknown1 = tr4_anim.unknown;
  917. _animations[i].unknown2 = tr4_anim.speed;
  918. _animations[i].unknown3 = tr4_anim.accel_lo;
  919. _animations[i].unknown4 = tr4_anim.accel_hi;
  920. _animations[i].frame_start = tr4_anim.frame_start;
  921. _animations[i].frame_end = tr4_anim.frame_end;
  922. _animations[i].next_animation = tr4_anim.next_animation;
  923. _animations[i].next_frame = tr4_anim.next_frame;
  924. _animations[i].num_state_changes = tr4_anim.num_state_changes;
  925. _animations[i].state_change_offset = tr4_anim.state_change_offset;
  926. _animations[i].num_anim_commands = (tr4_anim.num_anim_commands > 256)
  927. ? 0 : tr4_anim.num_anim_commands;
  928. _animations[i].anim_command = tr4_anim.anim_command;
  929. }
  930. }
  931. else
  932. {
  933. Fread(_animations, sizeof(tr2_animation_t), _num_animations, f);
  934. }
  935. }
  936. //! \fixme endian
  937. /* Read state changes */
  938. Fread(&_num_state_changes, sizeof(_num_state_changes), 1, f);
  939. //! \fixme endian
  940. if (percent)
  941. (*percent)(80);
  942. printDebug("Load", "_num_state_changes = %u", _num_state_changes);
  943. if (_num_state_changes > 0)
  944. {
  945. _state_changes = new tr2_state_change_t[_num_state_changes];
  946. Fread(_state_changes, sizeof(tr2_state_change_t), _num_state_changes, f);
  947. }
  948. //! \fixme endian
  949. /* Read AnimDispatches */
  950. Fread(&_num_anim_dispatches, sizeof(_num_anim_dispatches), 1, f);
  951. //! \fixme endian
  952. printDebug("Load", "_num_anim_dispatches = %u", _num_anim_dispatches);
  953. _anim_dispatches = 0x0;
  954. if (_num_anim_dispatches > 0)
  955. {
  956. _anim_dispatches = new tr2_anim_dispatch_t[_num_anim_dispatches];
  957. Fread(_anim_dispatches, sizeof(tr2_anim_dispatch_t),
  958. _num_anim_dispatches, f);
  959. }
  960. //! \fixme endian
  961. /* Read anim commands */
  962. Fread(&_num_anim_commands, sizeof(_num_anim_commands), 1, f);
  963. //! \fixme endian
  964. printDebug("Load", "_num_anim_commands = %u", _num_anim_commands);
  965. _anim_commands = 0x0;
  966. if (_num_anim_commands > 0)
  967. {
  968. _anim_commands = new tr2_anim_command_t[_num_anim_commands];
  969. Fread(_anim_commands, sizeof(tr2_anim_command_t), _num_anim_commands, f);
  970. }
  971. //! \fixme endian
  972. /* Read MeshTrees */
  973. Fread(&_num_mesh_trees, sizeof(_num_mesh_trees), 1, f);
  974. //! \fixme endian
  975. printDebug("Load", "_num_mesh_trees = %u", _num_mesh_trees);
  976. _mesh_trees = 0x0;
  977. if (_num_mesh_trees > 0)
  978. {
  979. _mesh_trees = new tr2_meshtree_t[_num_mesh_trees];
  980. Fread(_mesh_trees, sizeof(int), _num_mesh_trees, f);
  981. }
  982. //! \fixme endian
  983. /* Read frames */
  984. Fread(&_num_frames, sizeof(_num_frames), 1, f);
  985. //! \fixme endian
  986. printDebug("Load", "_num_frames = %u", _num_frames);
  987. _frames = 0x0;
  988. if (_num_frames > 0)
  989. {
  990. _frames = new unsigned short[_num_frames];
  991. Fread(_frames, 2, _num_frames, f);
  992. //! \fixme endian
  993. if (mEngineVersion == TR_VERSION_1)
  994. {
  995. // re-format the frames[] to look like TR2 frames
  996. int num_frames;
  997. for (j = 0; j < (int)_num_animations; ++j)
  998. {
  999. int fo = _animations[j].frame_offset / 2;
  1000. _animations[j].frame_size = (unsigned char)(_frames[fo + 9] * 2) + 10;
  1001. }
  1002. for (i = 0; i < (int)_num_frames; )
  1003. {
  1004. i += 9; // point to num_frames;
  1005. j = i; // get rid of (overwrite) num_frames
  1006. num_frames = _frames[i++];
  1007. while (num_frames--)
  1008. {
  1009. _frames[j++] = _frames[i + 1]; // reverse the words as we go
  1010. _frames[j++] = _frames[i];
  1011. i += 2;
  1012. }
  1013. }
  1014. }
  1015. }
  1016. /* Read moveables */
  1017. Fread(&_num_moveables, sizeof(_num_moveables), 1, f);
  1018. //! \fixme endian
  1019. printDebug("Load", "_num_moveables = %u", _num_moveables);
  1020. _moveables = 0x0;
  1021. if (_num_moveables > 0)
  1022. {
  1023. debugf = ftell(f);
  1024. _moveables = new tr2_moveable_t[_num_moveables];
  1025. Fread(_moveables, 18, _num_moveables, f);
  1026. }
  1027. //! \fixme endian
  1028. Fread(&_num_static_meshes, sizeof(int), 1, f);
  1029. //! \fixme endian
  1030. printDebug("Load", "_num_static_meshes = %u", _num_static_meshes);
  1031. // SAFE EXIT //////////////////////////
  1032. _static_meshes = 0x0;
  1033. if (_num_static_meshes > 0)
  1034. {
  1035. _static_meshes = new tr2_staticmesh_t[_num_static_meshes];
  1036. Fread(_static_meshes, sizeof(tr2_staticmesh_t),
  1037. _num_static_meshes, f);
  1038. //! \fixme endian
  1039. }
  1040. _object_textures = 0x0;
  1041. if (mEngineVersion < TR_VERSION_3)
  1042. {
  1043. /* Read object textures */
  1044. Fread(&_num_object_textures, sizeof(int), 1, f);
  1045. printDebug("Load", "_num_object_textures = %u", _num_object_textures);
  1046. //! \fixme endian
  1047. if (_num_object_textures > 0)
  1048. {
  1049. _object_textures = new tr2_object_texture_t[_num_object_textures];
  1050. Fread(_object_textures, sizeof(tr2_object_texture_t),
  1051. _num_object_textures, f);
  1052. }
  1053. //! \fixme endian
  1054. }
  1055. if (percent)
  1056. (*percent)(90);
  1057. if (mEngineVersion == TR_VERSION_4)
  1058. {
  1059. unsigned char zzbuf[4];
  1060. Fread(zzbuf, 1, 3, f); // skip "SPR"
  1061. zzbuf[3] = 0;
  1062. printDebug("Load", "TR4 checking if %s == SPR", zzbuf);
  1063. }
  1064. /* Read sprite textures */
  1065. Fread(&_num_sprite_textures, sizeof(int), 1, f);
  1066. //! \fixme endian
  1067. printDebug("Load", "_num_sprite_textures = %u", _num_sprite_textures);
  1068. _sprite_textures = 0x0;
  1069. if (_num_sprite_textures > 0)
  1070. {
  1071. _sprite_textures = new tr2_sprite_texture_t[_num_sprite_textures];
  1072. Fread(_sprite_textures, sizeof(tr2_sprite_texture_t),
  1073. _num_sprite_textures, f);
  1074. }
  1075. //! \fixme endian
  1076. /* Read sprite texture data (?) */
  1077. Fread(&_num_sprite_sequences, sizeof(int), 1, f);
  1078. //! \fixme endian
  1079. printDebug("Load", "_num_sprite_sequences = %u", _num_sprite_sequences);
  1080. _sprite_sequences = 0x0;
  1081. if (_num_sprite_sequences > 0)
  1082. {
  1083. _sprite_sequences = new tr2_sprite_sequence_t[_num_sprite_sequences];
  1084. Fread(_sprite_sequences, sizeof(tr2_sprite_sequence_t),
  1085. _num_sprite_sequences, f);
  1086. }
  1087. //! \fixme endian
  1088. /* Read cameras */
  1089. Fread(&_num_cameras, sizeof(_num_cameras), 1, f);
  1090. //! \fixme endian
  1091. printDebug("Load", "_num_cameras = %i", _num_cameras);
  1092. _cameras = 0x0;
  1093. if (_num_cameras > 0)
  1094. {
  1095. _cameras = new tr2_camera_t[_num_cameras];
  1096. Fread(_cameras, sizeof(tr2_camera_t), _num_cameras, f);
  1097. //! \fixme endian
  1098. }
  1099. if (mEngineVersion == TR_VERSION_4)
  1100. {
  1101. int num_ex_cam;
  1102. tr4_extra_camera_t *ex_cam;
  1103. Fread(&num_ex_cam, 4, 1, f);
  1104. printDebug("Load", "num_extra_cam = %i", num_ex_cam);
  1105. if (num_ex_cam > 0)
  1106. {
  1107. ex_cam = new tr4_extra_camera_t[num_ex_cam];
  1108. Fread(ex_cam, sizeof(tr4_extra_camera_t), num_ex_cam, f);
  1109. delete [] ex_cam;
  1110. }
  1111. }
  1112. /* Read sound effects (?) */
  1113. Fread(&_num_sound_sources, sizeof(_num_sound_sources), 1, f);
  1114. //! \fixme endian
  1115. printDebug("Load", "_num_sound_sources = %i", _num_sound_sources);
  1116. _sound_sources = 0x0;
  1117. if (_num_sound_sources > 0)
  1118. {
  1119. _sound_sources =
  1120. (tr2_sound_source_t*) new unsigned char[_num_sound_sources*40];
  1121. Fread(_sound_sources, sizeof(tr2_sound_source_t),
  1122. _num_sound_sources, f);
  1123. //! \fixme endian
  1124. }
  1125. #ifdef OBSOLETE
  1126. if (mEngineVersion == TR_VERSION_4)
  1127. {
  1128. unsigned int num_ZZ;
  1129. unsigned char zzbuf[17];
  1130. Fread(&num_ZZ, 1, sizeof(num_ZZ), f);
  1131. while (num_ZZ--)
  1132. {
  1133. Fread(zzbuf, 1, 16, f);
  1134. zzbuf[16] = 0;
  1135. printDebug("Load", "TR4 zz dump '%s'", zzbuf);
  1136. }
  1137. }
  1138. #endif
  1139. /* Read boxes */
  1140. Fread(&_num_boxes, sizeof(_num_boxes), 1, f);
  1141. //! \fixme endian
  1142. printDebug("Load", "_num_boxes = %i", _num_boxes);
  1143. _boxes = 0x0;
  1144. if (_num_boxes > 0)
  1145. {
  1146. _boxes = new tr2_box_t[_num_boxes];
  1147. if (mEngineVersion == TR_VERSION_1)
  1148. {
  1149. struct tr1_box
  1150. {
  1151. int zmin, zmax, xmin, xmax;
  1152. short true_floor, overlap_index;
  1153. } __attribute__ ((packed)) *tr1box;
  1154. tr1box = new tr1_box[_num_boxes];
  1155. Fread(tr1box, sizeof(struct tr1_box), _num_boxes, f);
  1156. //! \fixme endian
  1157. for (j = 0; j < _num_boxes; ++j)
  1158. {
  1159. _boxes[j].zmin = (unsigned char)(tr1box[j].zmin / 1024);
  1160. _boxes[j].zmax = (unsigned char)(tr1box[j].zmax / 1024);
  1161. _boxes[j].xmin = (unsigned char)(tr1box[j].xmin / 1024);
  1162. _boxes[j].xmax = (unsigned char)(tr1box[j].xmax / 1024);
  1163. _boxes[j].true_floor = tr1box[j].true_floor;
  1164. _boxes[j].overlap_index = tr1box[j].overlap_index;
  1165. }
  1166. delete [] tr1box;
  1167. }
  1168. else
  1169. {
  1170. Fread(_boxes, sizeof(tr2_box_t), _num_boxes, f);
  1171. }
  1172. //! \fixme endian
  1173. }
  1174. /* Read overlaps (?) */
  1175. Fread(&_num_overlaps, sizeof(_num_overlaps), 1, f);
  1176. //! \fixme endian
  1177. printDebug("Load", "_num_overlaps = %i", _num_overlaps);
  1178. _overlaps = 0x0;
  1179. if (_num_overlaps > 0)
  1180. {
  1181. _overlaps = new short[_num_overlaps];
  1182. Fread(_overlaps, 2, _num_overlaps, f);
  1183. //! \fixme endian
  1184. }
  1185. _zones = 0x0;
  1186. /* Read Zones */
  1187. if (_num_boxes > 0)
  1188. {
  1189. _zones = new short[_num_boxes*10];
  1190. if (mEngineVersion == TR_VERSION_1)
  1191. {
  1192. Fread(_zones, 12, _num_boxes, f);
  1193. }
  1194. else
  1195. {
  1196. Fread(_zones, 20, _num_boxes, f);
  1197. }
  1198. //! \fixme endian
  1199. }
  1200. /* Read animation textures (?) */
  1201. Fread(&_num_animated_textures, sizeof(_num_animated_textures), 1, f);
  1202. //! \fixme endian
  1203. printDebug("Load", "_num_animated_textures = %i", _num_animated_textures);
  1204. _animated_textures = 0x0;
  1205. if (_num_animated_textures > 0)
  1206. {
  1207. _animated_textures = new short[_num_animated_textures];
  1208. Fread(_animated_textures, 2, _num_animated_textures, f);
  1209. //! \fixme endian
  1210. }
  1211. if (mEngineVersion >= TR_VERSION_3)
  1212. {
  1213. /* Read object textures */
  1214. if (mEngineVersion == TR_VERSION_4)
  1215. {
  1216. unsigned char zzbuf[5];
  1217. Fread(zzbuf, 1, 4, f); // skip "TEX"
  1218. //!! this should be 3, but we have a bug...
  1219. zzbuf[4] = 0;
  1220. printDebug("Load", "TR4 checking %s == TEX", zzbuf);
  1221. }
  1222. Fread(&_num_object_textures, sizeof(_num_object_textures), 1, f);
  1223. //! \fixme endian
  1224. printDebug("Load", "_num_object_textures = %i", _num_object_textures);
  1225. _object_textures = 0x0;
  1226. if (_num_object_textures > 0)
  1227. {
  1228. // Used to be 2 * num, and I forgot why...
  1229. _object_textures = new tr2_object_texture_t[_num_object_textures];
  1230. //! \fixme This is fu fu fu fu fu fu
  1231. if (mEngineVersion == TR_VERSION_4)
  1232. {
  1233. int jjj, kkk;
  1234. tr4_object_texture_t *tr4_tex;
  1235. tr4_tex = new tr4_object_texture_t[_num_object_textures];
  1236. Fread(tr4_tex, 38, _num_object_textures, f);
  1237. for (jjj = 0; jjj < (int)_num_object_textures; ++jjj)
  1238. {
  1239. _object_textures[jjj].transparency_flags =
  1240. tr4_tex[jjj].attribute;
  1241. _object_textures[jjj].tile =
  1242. (unsigned short)tr4_tex[jjj].tile & 0x7fff;
  1243. for (kkk = 0; kkk < 4; ++kkk)
  1244. {
  1245. _object_textures[jjj].vertices[kkk].xcoordinate =
  1246. tr4_tex[jjj].vertices[kkk].xcoordinate;
  1247. _object_textures[jjj].vertices[kkk].xpixel =
  1248. tr4_tex[jjj].vertices[kkk].xpixel;
  1249. _object_textures[jjj].vertices[kkk].ycoordinate =
  1250. tr4_tex[jjj].vertices[kkk].ycoordinate;
  1251. _object_textures[jjj].vertices[kkk].ypixel =
  1252. tr4_tex[jjj].vertices[kkk].ypixel;
  1253. }
  1254. }
  1255. delete [] tr4_tex;
  1256. }
  1257. else
  1258. {
  1259. Fread(_object_textures, sizeof(tr2_object_texture_t),
  1260. _num_object_textures, f);
  1261. }
  1262. }
  1263. //! \fixme endian
  1264. }
  1265. /* Read items */
  1266. Fread(&_num_items, sizeof(_num_items), 1, f);
  1267. //! \fixme endian
  1268. printDebug("Load", "_num_items = %i", _num_items);
  1269. _items = 0x0;
  1270. if (_num_items > 0)
  1271. {
  1272. _items = new tr2_item_t[_num_items];
  1273. if (mEngineVersion == TR_VERSION_1)
  1274. {
  1275. for (i = 0; i < _num_items; ++i)
  1276. {
  1277. Fread(&_items[i], sizeof(tr2_item_t) - 2, 1, f);
  1278. _items[i].flags = _items[i].intensity2;
  1279. _items[i].intensity2 = _items[i].intensity1;
  1280. }
  1281. }
  1282. else
  1283. {
  1284. Fread(_items, sizeof(tr2_item_t), _num_items, f);
  1285. }
  1286. }
  1287. //! \fixme endian
  1288. /* Read LightMaps */
  1289. _light_map = new unsigned char[32 * 256];
  1290. if (mEngineVersion != TR_VERSION_4)
  1291. {
  1292. Fread(_light_map, 32, 256, f);
  1293. }
  1294. if (mEngineVersion == TR_VERSION_1)
  1295. {
  1296. /* read the 8-bit palette */
  1297. Fread(_palette8, sizeof(tr2_colour_t), 256, f);
  1298. printDebug("Load", "Read TR 1 palette");
  1299. // build 16-bit textiles from 8-bit
  1300. // (no extra colours, but creates consistent .TR2 file)
  1301. for (i = 0; i < (int)_num_textiles; ++i)
  1302. {
  1303. unsigned short argb;
  1304. double colour_tmp;
  1305. for (j = 0; j < (256 * 256); ++j)
  1306. {
  1307. colour_tmp = _palette8[_textile8[i].tile[j]].r & 0x3f;
  1308. colour_tmp = colour_tmp * 31.0 / 63.0;
  1309. argb = (unsigned short)(((int)colour_tmp) << 10);
  1310. colour_tmp = _palette8[_textile8[i].tile[j]].g & 0x3f;
  1311. colour_tmp = colour_tmp * 31.0 / 63.0;
  1312. argb |= (unsigned short)(((int)colour_tmp) << 5);
  1313. colour_tmp = _palette8[_textile8[i].tile[j]].b & 0x3f;
  1314. colour_tmp = colour_tmp * 31.0 / 63.0;
  1315. argb |= (unsigned short)((int)colour_tmp);
  1316. argb &= 0x7fff; // ???
  1317. if (_textile8[i].tile[j] != 0)
  1318. argb |= 0x8000;
  1319. _textile16[i].tile[j] = argb;
  1320. }
  1321. }
  1322. }
  1323. /* Read cinematic frames */
  1324. if (mEngineVersion == TR_VERSION_4)
  1325. {
  1326. unsigned int num_ai_data;
  1327. Fread(&num_ai_data, 4, 1, f);
  1328. printDebug("Load", "num_ai_data = %i", num_ai_data);
  1329. tr4_ai_object_t *ai_obj = 0x0;
  1330. if (num_ai_data > 0)
  1331. {
  1332. ai_obj = new tr4_ai_object_t[num_ai_data];
  1333. Fread(ai_obj, sizeof(tr4_ai_object_t), num_ai_data, f);
  1334. delete [] ai_obj;
  1335. }
  1336. }
  1337. else
  1338. {
  1339. unsigned short num_cinematic_frames;
  1340. Fread(&num_cinematic_frames, sizeof(num_cinematic_frames), 1, f);
  1341. //! \fixme endian
  1342. _num_cinematic_frames = num_cinematic_frames;
  1343. printDebug("Load", "_num_cinematic_frames = %i", _num_cinematic_frames);
  1344. _cinematic_frames = 0x0;
  1345. if (_num_cinematic_frames > 0)
  1346. {
  1347. _cinematic_frames = new tr2_cinematic_frame_t[_num_cinematic_frames];
  1348. Fread(_cinematic_frames, sizeof(tr2_cinematic_frame_t),
  1349. _num_cinematic_frames, f);
  1350. // There may or may not be endian conversion required here - I have
  1351. // no idea what this data is.
  1352. }
  1353. }
  1354. /* Read demodata (?) */
  1355. Fread(&_num_demo_data, sizeof(_num_demo_data), 1, f);
  1356. //! \fixme endian
  1357. printDebug("Load", "_num_demo_data = %i", _num_demo_data);
  1358. _demo_data = 0x0;
  1359. if (_num_demo_data > 0)
  1360. {
  1361. _demo_data = new unsigned char[_num_demo_data];
  1362. Fread(_demo_data, 1, _num_demo_data, f);
  1363. // There may or may not be endian conversion required here - I have
  1364. // no idea what this data is.
  1365. }
  1366. /* Read SoundMap */
  1367. mSoundMap = new short[370];
  1368. if (mEngineVersion == TR_VERSION_1)
  1369. {
  1370. Fread(mSoundMap, sizeof(short), 256, f);
  1371. //memset(_sound_map, 0, 370 * sizeof(short)); //! \fixme KLUDGE!!!
  1372. }
  1373. else
  1374. {
  1375. Fread(mSoundMap, sizeof(short), 370, f);
  1376. }
  1377. //! \fixme endian
  1378. /* Read SoundDetails */
  1379. Fread(&mNumSoundDetails, sizeof(mNumSoundDetails), 1, f);
  1380. printDebug("Load", "mNumSoundDetails = %i", mNumSoundDetails);
  1381. //! \fixme endian
  1382. mSoundDetails = 0x0;
  1383. if (mNumSoundDetails > 0)
  1384. {
  1385. mSoundDetails = new tr2_sound_details_t[mNumSoundDetails];
  1386. Fread(mSoundDetails, sizeof(tr2_sound_details_t), mNumSoundDetails, f);
  1387. }
  1388. //! \fixme endian
  1389. // Read sound sample indices
  1390. mSampleIndices = 0x0;
  1391. mNumSampleIndices = 0;
  1392. mRiffDataSz = 0;
  1393. mRiffData = 0x0;
  1394. mNumTR4Samples = 0;
  1395. mTR4Samples = 0x0;
  1396. switch (mEngineVersion)
  1397. {
  1398. case TR_VERSION_1:
  1399. Fread(&mRiffDataSz, 4, 1, f);
  1400. printDebug("Load", "mRiffDataSz = %ibytes", mRiffDataSz);
  1401. if (mRiffDataSz > 0)
  1402. {
  1403. mRiffData = new unsigned char[mRiffDataSz];
  1404. Fread(mRiffData, 1, mRiffDataSz, f);
  1405. }
  1406. Fread(&mNumSampleIndices, 4, 1, f);
  1407. printDebug("Load", "mNumSampleIndices = %i", mNumSampleIndices);
  1408. if (mNumSampleIndices > 0)
  1409. {
  1410. mSampleIndices = new int[mNumSampleIndices];
  1411. //! \fixme (Endian)
  1412. Fread(mSampleIndices, 4, mNumSampleIndices, f);
  1413. }
  1414. break;
  1415. case TR_VERSION_4:
  1416. mFreadMode = TR_FREAD_NORMAL;
  1417. // 0x46464952
  1418. //! \fixme (Endian) Read bitu32 / u_int32_t
  1419. Fread(&mNumTR4Samples, 4, 1, f);
  1420. printDebug("Load", "mNumTR4Samples = %i", mNumTR4Samples);
  1421. mRiffDataSz = 0;
  1422. mTR4Samples = new unsigned char *[mNumTR4Samples];
  1423. mTR4SamplesSz = new unsigned int[mNumTR4Samples];
  1424. memset(mTR4SamplesSz, 0, mNumTR4Samples*4);
  1425. for (i = 0; i < (int)mNumTR4Samples; ++i)
  1426. {
  1427. unsigned int sizeCompressed;
  1428. unsigned int sizeUncompressed;
  1429. unsigned char *compressedSoundSample;
  1430. unsigned char *unCompressedSoundSample;
  1431. int zErr;
  1432. uLongf libzUncompressedSize;
  1433. Fread(&sizeUncompressed, 4, 1, f);
  1434. printDebug("Load", " sizeUncompressed = %i", sizeUncompressed);
  1435. Fread(&sizeCompressed, 4, 1, f);
  1436. printDebug("Load", " sizeCompressed = %i", sizeCompressed);
  1437. compressedSoundSample = new unsigned char[sizeCompressed];
  1438. unCompressedSoundSample = new unsigned char[sizeUncompressed];
  1439. //printDebug("Load", " %lubytes read from file", ftell(f));
  1440. Fread(compressedSoundSample, sizeCompressed, 1, f);
  1441. printDebug("Load", " %c%c%c%c should be RIFF",
  1442. compressedSoundSample[0],
  1443. compressedSoundSample[1],
  1444. compressedSoundSample[2],
  1445. compressedSoundSample[3]);
  1446. //#define NEVER_DECOMPRESS
  1447. #ifdef NEVER_DECOMPRESS
  1448. mTR4Samples[i] = compressedSoundSample;
  1449. mTR4SamplesSz[i] = sizeCompressed;
  1450. delete [] unCompressedSoundSample;
  1451. #else
  1452. // Decompress the sample
  1453. libzUncompressedSize = sizeUncompressed;
  1454. zErr = uncompress(unCompressedSoundSample,
  1455. &libzUncompressedSize,
  1456. compressedSoundSample,
  1457. sizeCompressed);
  1458. sizeUncompressed = libzUncompressedSize;
  1459. switch (zErr)
  1460. {
  1461. case Z_MEM_ERROR:
  1462. printDebug("Load", " Decompress Error: not enough memory");
  1463. break;
  1464. case Z_BUF_ERROR:
  1465. printDebug("Load", " Decompress Error: output buffer too small");
  1466. break;
  1467. case Z_DATA_ERROR:
  1468. printDebug("Load", " Decompress Error: input data was corrupted");
  1469. break;
  1470. case Z_OK:
  1471. printDebug("Load", " Decompress OK");
  1472. break;
  1473. default:
  1474. printDebug("Load", " Decompress Error: decompress error #%i", zErr);
  1475. }
  1476. // Hhhmm... handle uncompressed RIFFs too?
  1477. if (zErr == Z_OK)
  1478. {
  1479. mTR4Samples[i] = unCompressedSoundSample;
  1480. mTR4SamplesSz[i] = sizeUncompressed;
  1481. delete [] compressedSoundSample;
  1482. }
  1483. else
  1484. {
  1485. printDebug("Load", " %lubytes read from file", ftell(f));
  1486. mTR4Samples[i] = compressedSoundSample;
  1487. mTR4SamplesSz[i] = sizeCompressed;
  1488. delete [] unCompressedSoundSample;
  1489. }
  1490. #endif
  1491. }
  1492. break;
  1493. case TR_VERSION_2:
  1494. case TR_VERSION_3:
  1495. case TR_VERSION_5:
  1496. case TR_VERSION_UNKNOWN:
  1497. //! \fixme (Endian) Read bit32 / int32_t
  1498. Fread(&mNumSampleIndices, 4, 1, f);
  1499. printDebug("Load", "mNumSampleIndices = %i", mNumSampleIndices);
  1500. if (mNumSampleIndices > 0)
  1501. {
  1502. mSampleIndices = new int[mNumSampleIndices];
  1503. //! \fixme (Endian)
  1504. Fread(mSampleIndices, 4, mNumSampleIndices, f);
  1505. }
  1506. break;
  1507. }
  1508. if (mCompressedLevelData)
  1509. {
  1510. printDebug("Load", "Freeing uncompressed TR4 data");
  1511. delete [] mCompressedLevelData;
  1512. }
  1513. //! \fixme memory damage?
  1514. mCompressedLevelData = NULL;
  1515. fclose(f);
  1516. if (percent)
  1517. (*percent)(100);
  1518. return 0;
  1519. }
  1520. ////////////////////////////////////////////////////////////
  1521. // Public Accessors
  1522. ////////////////////////////////////////////////////////////
  1523. float TombRaider::adjustTexel(unsigned char texel, char offset)
  1524. {
  1525. if (offset >= 0)
  1526. texel++;
  1527. else
  1528. texel--;
  1529. return ((float)texel / 255.0f);
  1530. }
  1531. void TombRaider::computeRotationAngles(unsigned short **frame,
  1532. unsigned int *frame_offset,
  1533. unsigned int *angle_offset,
  1534. float *x, float *y, float *z)
  1535. {
  1536. unsigned short itmp, itmp2;
  1537. float angle;
  1538. itmp = (*frame)[(*frame_offset) + (*angle_offset)++];
  1539. if (Engine() == TR_VERSION_1)
  1540. {
  1541. // All angles are three-axis
  1542. angle = (itmp >> 4) & 0x03ff;
  1543. angle *= 360.0 / 1024.0;
  1544. *x = angle;
  1545. itmp2 = (itmp << 6) & 0x03c0;
  1546. // Get Z rotation
  1547. itmp = (*frame)[(*frame_offset) + (*angle_offset)];
  1548. ++(*angle_offset);
  1549. itmp2 |= (itmp >> 10) & 0x003f;
  1550. angle = itmp2;
  1551. angle *= 360.0 / 1024.0;
  1552. *y = angle;
  1553. angle = itmp & 0x3ff;
  1554. angle *= 360.0 / 1024.0;
  1555. *z = angle;
  1556. }
  1557. else if (itmp & 0xc000)
  1558. {
  1559. // TR2, TR3, TR4 - single axis of rotation
  1560. if (Engine() == TR_VERSION_4)
  1561. {
  1562. angle = itmp & 0x0fff;
  1563. angle /= 4096.0;
  1564. angle *= 360.0;
  1565. }
  1566. else
  1567. {
  1568. angle = itmp & 0x3ff;
  1569. angle /= 1024.0;
  1570. angle *= 360.0;
  1571. }
  1572. switch (itmp & 0xc000)
  1573. {
  1574. case 0x4000:
  1575. *x = angle;
  1576. break;
  1577. case 0x8000:
  1578. *y = angle;
  1579. break;
  1580. case 0xc000:
  1581. *z = angle;
  1582. break;
  1583. }
  1584. }
  1585. else // TR2, TR3, TR4 - three axes
  1586. {
  1587. angle = (itmp >> 4) & 0x03ff;
  1588. angle *= 360.0 / 1024.0;
  1589. *x = angle;
  1590. itmp2 = (itmp << 6) & 0x03c0;
  1591. itmp = (*frame)[(*frame_offset) + (*angle_offset)++]; // get Z rotation
  1592. itmp2 |= (itmp >> 10) & 0x003f;
  1593. angle = itmp2;
  1594. angle *= 360.0 / 1024.0;
  1595. *y = angle;
  1596. angle = itmp & 0x3ff;
  1597. angle *= 360.0 / 1024.0;
  1598. *z = angle;
  1599. }
  1600. }
  1601. void TombRaider::computeUV(tr2_object_texture_vert_t *st, float *u, float *v)
  1602. {
  1603. unsigned char x, y;
  1604. if (!st || !u || !v)
  1605. return;
  1606. x = st->xpixel;
  1607. y = st->ypixel;
  1608. x += (char)st->xcoordinate;
  1609. y += (char)st->ycoordinate;
  1610. //x += ((char)st->xcoordinate >= 0) ? 1 : -1;
  1611. //y += ((char)st->ycoordinate >= 0) ? 1 : -1;
  1612. *u = (float)x / 255.0f;
  1613. *v = (float)y / 255.0f;
  1614. }
  1615. int TombRaider::getBumpMapCount()
  1616. {
  1617. return _num_bump_map_textures / 2;
  1618. }
  1619. void TombRaider::getColor(int index, float color[4])
  1620. {
  1621. switch (getEngine())
  1622. {
  1623. case TR_VERSION_1:
  1624. color[0] = _palette8[index].r / 64.0f;
  1625. color[1] = _palette8[index].g / 64.0f;
  1626. color[2] = _palette8[index].b / 64.0f;
  1627. //color[0] = (_palette8[index].r & 0xfd) / 64.0f;
  1628. //color[1] = (_palette8[index].g & 0xfd) / 64.0f;
  1629. //color[2] = (_palette8[index].b & 0xfd) / 64.0f;
  1630. color[3] = 1.0;
  1631. break;
  1632. case TR_VERSION_2:
  1633. case TR_VERSION_3:
  1634. case TR_VERSION_4:
  1635. case TR_VERSION_5:
  1636. case TR_VERSION_UNKNOWN:
  1637. color[0] = (float)(_palette16[index] & 0xff) / 256.0f;
  1638. color[1] = (float)((_palette16[index] >> 8) & 0xff) / 256.0f;
  1639. color[2] = (float)((_palette16[index] >> 16) & 0xff) / 256.0f;
  1640. color[3] = 1.0;
  1641. break;
  1642. }
  1643. }
  1644. tr2_version_type TombRaider::getEngine()
  1645. {
  1646. return mEngineVersion;
  1647. }
  1648. void TombRaider::getMeshCollisionInfo(unsigned int meshIndex,
  1649. float center[3], float *radius)
  1650. {
  1651. if ((int)meshIndex > mMeshCount)
  1652. {
  1653. print("getMeshCollisionInfo", "Assertion error: invalid mesh index\n");
  1654. return;
  1655. }
  1656. center[0] = mMeshes[meshIndex].centre.x;
  1657. center[1] = mMeshes[meshIndex].centre.y;
  1658. center[2] = mMeshes[meshIndex].centre.z;
  1659. *radius = mMeshes[meshIndex].collision_size;
  1660. }
  1661. int TombRaider::getMeshCount()
  1662. {
  1663. return mMeshCount;
  1664. }
  1665. /*! \fixme Needs refinement once the ideal format it's feeding is refined
  1666. * I should stick a HACK postfix on the method name - it's temporary
  1667. * until an array format can be crafted from a pinned down design and
  1668. * RE notes review session ( eg what about TR5? )
  1669. */
  1670. void TombRaider::getMeshColoredRectangle(unsigned int meshIndex,
  1671. unsigned int faceIndex,
  1672. int *index, float *color)
  1673. {
  1674. unsigned int t1_1, t1_2, t1_3, t2_1, t2_2, t2_3;
  1675. if ((int)meshIndex > mMeshCount)
  1676. {
  1677. print("getMeshColoredRectangle", "Assertion error: invalid mesh index\n");
  1678. return;
  1679. }
  1680. // Make 2 triangles from one quad!
  1681. t1_1 = 0;
  1682. t1_2 = 1;
  1683. t1_3 = 2;
  1684. t2_1 = 3;
  1685. t2_2 = 0;
  1686. t2_3 = 2;
  1687. index[0] = mMeshes[meshIndex].coloured_rectangles[faceIndex].vertices[t1_1];
  1688. index[1] = mMeshes[meshIndex].coloured_rectangles[faceIndex].vertices[t1_2];
  1689. index[2] = mMeshes[meshIndex].coloured_rectangles[faceIndex].vertices[t1_3];
  1690. index[3] = mMeshes[meshIndex].coloured_rectangles[faceIndex].vertices[t2_1];
  1691. index[4] = mMeshes[meshIndex].coloured_rectangles[faceIndex].vertices[t2_2];
  1692. index[5] = mMeshes[meshIndex].coloured_rectangles[faceIndex].vertices[t2_3];
  1693. switch (Engine())
  1694. {
  1695. case TR_VERSION_1:
  1696. getColor(mMeshes[meshIndex].coloured_rectangles[faceIndex].texture & 0xff, color);
  1697. break;
  1698. case TR_VERSION_2:
  1699. case TR_VERSION_3:
  1700. case TR_VERSION_4:
  1701. case TR_VERSION_5:
  1702. case TR_VERSION_UNKNOWN:
  1703. getColor((mMeshes[meshIndex].coloured_rectangles[faceIndex].texture>>8) & 0xff, color);
  1704. break;
  1705. }
  1706. }
  1707. void TombRaider::getMeshColoredTriangle(unsigned int meshIndex,
  1708. unsigned int faceIndex,
  1709. int *index, float *color)
  1710. {
  1711. if ((int)meshIndex > mMeshCount)
  1712. {
  1713. print("getMeshColoredTriangle", "Assertion error: invalid mesh index\n");
  1714. return;
  1715. }
  1716. index[0] = mMeshes[meshIndex].coloured_triangles[faceIndex].vertices[0];
  1717. index[1] = mMeshes[meshIndex].coloured_triangles[faceIndex].vertices[1];
  1718. index[2] = mMeshes[meshIndex].coloured_triangles[faceIndex].vertices[2];
  1719. switch (Engine())
  1720. {
  1721. case TR_VERSION_1:
  1722. getColor(mMeshes[meshIndex].coloured_triangles[faceIndex].texture & 0xff,
  1723. color);
  1724. break;
  1725. case TR_VERSION_2:
  1726. case TR_VERSION_3:
  1727. case TR_VERSION_4:
  1728. case TR_VERSION_5:
  1729. case TR_VERSION_UNKNOWN:
  1730. getColor((mMeshes[meshIndex].coloured_triangles[faceIndex].texture>>8) & 0xff,
  1731. color);
  1732. break;
  1733. }
  1734. }
  1735. void TombRaider::getMeshTexturedRectangle(unsigned int meshIndex,
  1736. unsigned int faceIndex,
  1737. int *index, float *st, int *texture,
  1738. unsigned short *transparency)
  1739. {
  1740. unsigned int t1_1, t1_2, t1_3, t2_1, t2_2, t2_3;
  1741. static bool givenWarning = false; // hahaha... okay less spewing
  1742. tr2_mesh_t *meshes;
  1743. tr2_object_texture_t *object_texture;
  1744. int t_index;
  1745. unsigned int i, m;
  1746. if ((int)meshIndex > mMeshCount)
  1747. {
  1748. print("getMeshTexturedRectangle", "Assertion error: invalid mesh index\n");
  1749. return;
  1750. }
  1751. m = meshIndex; // lazy
  1752. meshes = mMeshes; // lazy
  1753. object_texture = _object_textures; // lazy
  1754. i = faceIndex; // lazy
  1755. // Make 2 triangles from one quad!
  1756. t1_1 = 0;
  1757. t1_2 = 1;
  1758. t1_3 = 2;
  1759. t2_1 = 3;
  1760. t2_2 = 0;
  1761. t2_3 = 2;
  1762. t_index = meshes[m].textured_rectangles[i].texture;
  1763. index[0] = meshes[m].textured_rectangles[i].vertices[t1_1];
  1764. index[1] = meshes[m].textured_rectangles[i].vertices[t1_2];
  1765. index[2] = meshes[m].textured_rectangles[i].vertices[t1_3];
  1766. index[3] = meshes[m].textured_rectangles[i].vertices[t2_1];
  1767. index[4] = meshes[m].textured_rectangles[i].vertices[t2_2];
  1768. index[5] = meshes[m].textured_rectangles[i].vertices[t2_3];
  1769. computeUV(object_texture[t_index].vertices+t1_1, (st), (st)+1);
  1770. computeUV(object_texture[t_index].vertices+t1_2, (st)+2, (st)+3);
  1771. computeUV(object_texture[t_index].vertices+t1_3, (st)+4, (st)+5);
  1772. computeUV(object_texture[t_index].vertices+t2_1, (st)+6, (st)+7);
  1773. computeUV(object_texture[t_index].vertices+t2_2, (st)+8, (st)+9);
  1774. computeUV(object_texture[t_index].vertices+t2_3, (st)+10, (st)+11);
  1775. *texture = object_texture[t_index].tile;
  1776. *transparency = object_texture[t_index].transparency_flags;
  1777. // TR3+ alpha Textured polygons
  1778. if (!givenWarning && *transparency == 2)
  1779. {
  1780. givenWarning = true;
  1781. //! \fixme Use Material class to handle greyscale alpha intensity
  1782. // (partial alpha)
  1783. print("getMeshTexturedRectangle",
  1784. "TR3+ greyscale alpha intensity not implmented, %s:%i",
  1785. __FILE__, __LINE__);
  1786. }
  1787. }
  1788. void TombRaider::getMeshTexturedTriangle(unsigned int meshIndex,
  1789. unsigned int faceIndex,
  1790. int *index, float *st, int *texture,
  1791. unsigned short *transparency)
  1792. {
  1793. static bool givenWarning = false; // hahaha... okay less spewing
  1794. tr2_mesh_t *meshes;
  1795. tr2_object_texture_t *object_texture;
  1796. int t_index;
  1797. unsigned int i;
  1798. if ((int)meshIndex > mMeshCount)
  1799. {
  1800. print("getMeshTexturedTriangle", "Assertion error: invalid mesh index\n");
  1801. return;
  1802. }
  1803. meshes = mMeshes; // lazy
  1804. object_texture = _object_textures; // lazy
  1805. i = faceIndex; // lazy
  1806. t_index = meshes[meshIndex].textured_triangles[i].texture;
  1807. index[0] = meshes[meshIndex].textured_triangles[i].vertices[0];
  1808. index[1] = meshes[meshIndex].textured_triangles[i].vertices[1];
  1809. index[2] = meshes[meshIndex].textured_triangles[i].vertices[2];
  1810. computeUV(object_texture[t_index].vertices, (st), (st)+1);
  1811. computeUV(object_texture[t_index].vertices+1, (st)+2, (st)+3);
  1812. computeUV(object_texture[t_index].vertices+2, (st)+4, (st)+5);
  1813. *texture = object_texture[t_index].tile;
  1814. *transparency = object_texture[t_index].transparency_flags;
  1815. // TR3+ alpha Textured polygons
  1816. if (!givenWarning && *transparency == 2)
  1817. {
  1818. givenWarning = true;
  1819. //! \fixme Use Material class to handle greyscale alpha intensity
  1820. // (partial alpha)
  1821. print("getMeshTexturedTriangle",
  1822. "TR3+ greyscale alpha intensity not implmented, %s:%i",
  1823. __FILE__, __LINE__);
  1824. }
  1825. }
  1826. int TombRaider::getMeshTexturedTriangleCount(unsigned int meshIndex)
  1827. {
  1828. if ((int)meshIndex > mMeshCount)
  1829. {
  1830. print("getMeshTexturedTriangleCount", "Assertion error: invalid mesh index\n");
  1831. return 0;
  1832. }
  1833. return ((mMeshes[meshIndex].num_textured_triangles <= 0) ? 0 :
  1834. mMeshes[meshIndex].num_textured_triangles);
  1835. }
  1836. int TombRaider::getMeshColoredTriangleCount(unsigned int meshIndex)
  1837. {
  1838. if ((int)meshIndex > mMeshCount)
  1839. {
  1840. print("getMeshColoredTriangleCount", "Assertion error: invalid mesh index\n");
  1841. return 0;
  1842. }
  1843. return ((mMeshes[meshIndex].num_coloured_triangles <= 0) ? 0 :
  1844. mMeshes[meshIndex].num_coloured_triangles);
  1845. }
  1846. int TombRaider::getMeshTexturedRectangleCount(unsigned int meshIndex)
  1847. {
  1848. if ((int)meshIndex > mMeshCount)
  1849. {
  1850. print("getMeshTexturedRectangleCount", "Assertion error: invalid mesh index\n");
  1851. return 0;
  1852. }
  1853. return ((mMeshes[meshIndex].num_textured_rectangles <= 0) ? 0 :
  1854. mMeshes[meshIndex].num_textured_rectangles);
  1855. }
  1856. int TombRaider::getMeshColoredRectangleCount(unsigned int meshIndex)
  1857. {
  1858. if ((int)meshIndex > mMeshCount)
  1859. {
  1860. print("getMeshColoredRectangleCount", "Assertion error: invalid mesh index\n");
  1861. return 0;
  1862. }
  1863. return ((mMeshes[meshIndex].num_coloured_rectangles <= 0) ? 0:
  1864. mMeshes[meshIndex].num_coloured_rectangles);
  1865. }
  1866. //! \fixme Perhaps making color an 8bit intensity would be a better idea
  1867. void TombRaider::getMeshVertexArrays(unsigned int meshIndex,
  1868. unsigned int *vertexCount, float **verts,
  1869. unsigned int *normalCount, float **norms,
  1870. unsigned int *colorCount, float **colors)
  1871. {
  1872. unsigned int i;
  1873. float colorValue;
  1874. *vertexCount = 0;
  1875. *verts = 0x0;
  1876. *normalCount = 0;
  1877. *norms = 0x0;
  1878. *colorCount = 0;
  1879. *colors = 0x0;
  1880. if ((int)meshIndex > mMeshCount || mMeshes[meshIndex].num_vertices < 0)
  1881. return;
  1882. // Vertices
  1883. *vertexCount = mMeshes[meshIndex].num_vertices;
  1884. *verts = new float[*vertexCount * 3];
  1885. for (i = 0; i < *vertexCount; ++i)
  1886. {
  1887. (*verts)[i*3] = mMeshes[meshIndex].vertices[i].x;
  1888. (*verts)[i*3+1] = mMeshes[meshIndex].vertices[i].y;
  1889. (*verts)[i*3+2] = mMeshes[meshIndex].vertices[i].z;
  1890. }
  1891. // Normals, if any
  1892. if (mMeshes[meshIndex].num_normals > 0 &&
  1893. mMeshes[meshIndex].normals &&
  1894. mMeshes[meshIndex].num_normals == mMeshes[meshIndex].num_vertices)
  1895. {
  1896. *normalCount = mMeshes[meshIndex].num_vertices;
  1897. *norms = new float[*normalCount * 3];
  1898. for (i = 0; i < *normalCount; ++i)
  1899. {
  1900. (*norms)[i*3] = mMeshes[meshIndex].normals[i].x;
  1901. (*norms)[i*3+1] = mMeshes[meshIndex].normals[i].y;
  1902. (*norms)[i*3+2] = mMeshes[meshIndex].normals[i].z;
  1903. }
  1904. }
  1905. // Vertex lighting/colors, if any
  1906. else if (mMeshes[meshIndex].num_vertices > 0 &&
  1907. mMeshes[meshIndex].mesh_lights)
  1908. {
  1909. *colorCount = mMeshes[meshIndex].num_vertices;
  1910. #ifdef MESH_COLORS_SHOULD_BR_RGBA
  1911. *colors = new float[*colorCount * 4];
  1912. #else
  1913. *colors = new float[*colorCount];
  1914. #endif
  1915. for (i = 0; i < *colorCount; ++i)
  1916. {
  1917. colorValue = mMeshes[meshIndex].mesh_lights[i];
  1918. switch (Engine())
  1919. {
  1920. case TR_VERSION_4:
  1921. case TR_VERSION_3:
  1922. colorValue /= 16384.0;
  1923. //! \fixme Should we really fall through here?!?
  1924. break; // just testing -- xythobuz, 20140119
  1925. case TR_VERSION_1:
  1926. case TR_VERSION_2:
  1927. case TR_VERSION_5:
  1928. case TR_VERSION_UNKNOWN:
  1929. colorValue = (1.0f - (colorValue / 8192.0f));
  1930. break;
  1931. }
  1932. #ifdef MESH_COLORS_SHOULD_BR_RGBA
  1933. (*colors)[i*4+0] = colorValue;
  1934. (*colors)[i*4+1] = colorValue;
  1935. (*colors)[i*4+2] = colorValue;
  1936. (*colors)[i*4+3] = 1.0;
  1937. #else
  1938. (*colors)[i] = colorValue;
  1939. #endif
  1940. }
  1941. }
  1942. }
  1943. int TombRaider::getRoomBox(unsigned int roomIndex, unsigned int index,
  1944. float *xyzA, float *xyzB,
  1945. float *xyzC, float *xyzD)
  1946. {
  1947. if (!getRoomBoxCount(roomIndex) || index > getRoomBoxCount(roomIndex))
  1948. return -1;
  1949. switch (getEngine())
  1950. {
  1951. case TR_VERSION_UNKNOWN:
  1952. break;
  1953. case TR_VERSION_1:
  1954. case TR_VERSION_2:
  1955. case TR_VERSION_3:
  1956. case TR_VERSION_4:
  1957. case TR_VERSION_5:
  1958. xyzA[0] = (unsigned short)_boxes[index].xmin * 1024.0f;
  1959. xyzA[1] = (short)_boxes[index].true_floor;
  1960. xyzA[2] = (unsigned short)_boxes[index].zmin * 1024.0f;
  1961. xyzB[0] = (unsigned short)_boxes[index].xmax * 1024.0f;
  1962. xyzB[1] = (short)_boxes[index].true_floor;
  1963. xyzB[2] = (unsigned short)_boxes[index].zmin * 1024.0f;
  1964. xyzC[0] = (unsigned short)_boxes[index].xmax * 1024.0f;
  1965. xyzC[1] = (short)_boxes[index].true_floor;
  1966. xyzC[2] = (unsigned short)_boxes[index].zmax * 1024.0f;
  1967. xyzD[0] = (unsigned short)_boxes[index].xmin * 1024.0f;
  1968. xyzD[1] = (short)_boxes[index].true_floor;
  1969. xyzD[2] = (unsigned short)_boxes[index].zmax * 1024.0f;
  1970. }
  1971. return 0;
  1972. }
  1973. unsigned int TombRaider::getRoomBoxCount(unsigned int roomIndex)
  1974. {
  1975. if (!isRoomValid(roomIndex))
  1976. return 0;
  1977. switch (getEngine())
  1978. {
  1979. case TR_VERSION_UNKNOWN:
  1980. break;
  1981. case TR_VERSION_1:
  1982. case TR_VERSION_2:
  1983. case TR_VERSION_3:
  1984. case TR_VERSION_4:
  1985. case TR_VERSION_5:
  1986. return _num_boxes;
  1987. }
  1988. return 0;
  1989. }
  1990. void TombRaider::getRoomInfo(unsigned int index,
  1991. unsigned int *flags, float pos[3],
  1992. float bboxMin[3], float bboxMax[3])
  1993. {
  1994. unsigned int i, n;
  1995. float f;
  1996. if (!isRoomValid(index))
  1997. return;
  1998. switch (getEngine())
  1999. {
  2000. case TR_VERSION_UNKNOWN:
  2001. break;
  2002. case TR_VERSION_5:
  2003. // Flags
  2004. *flags = mRoomsTR5[index].roomFlag; // Needs to be generic flags in class
  2005. // Positioning
  2006. pos[0] = mRoomsTR5[index].roomX;
  2007. pos[1] = 0.0f;
  2008. pos[2] = mRoomsTR5[index].roomZ;
  2009. // Bounding box setup
  2010. bboxMin[0] = 0.0;
  2011. bboxMin[1] = 0.0;
  2012. bboxMin[2] = 0.0;
  2013. bboxMax[0] = 0.0;
  2014. bboxMax[1] = 0.0;
  2015. bboxMax[2] = 0.0;
  2016. // Bounding Box setup
  2017. for (i = 0; i < mRoomsTR5[index].numLayers; ++i)
  2018. {
  2019. //! \fixme check the boxes are in min, max order in TRC
  2020. if (i == 0)
  2021. {
  2022. bboxMin[0] = mRoomsTR5[index].layers[i].layerBoundingBoxX1;
  2023. bboxMin[1] = mRoomsTR5[index].layers[i].layerBoundingBoxY1;
  2024. bboxMin[2] = mRoomsTR5[index].layers[i].layerBoundingBoxZ1;
  2025. bboxMax[0] = mRoomsTR5[index].layers[i].layerBoundingBoxX1;
  2026. bboxMax[1] = mRoomsTR5[index].layers[i].layerBoundingBoxY2;
  2027. bboxMax[2] = mRoomsTR5[index].layers[i].layerBoundingBoxZ2;
  2028. continue;
  2029. }
  2030. if (mRoomsTR5[index].layers[i].layerBoundingBoxX1 < bboxMin[0])
  2031. bboxMin[0] = mRoomsTR5[index].layers[i].layerBoundingBoxX1;
  2032. if (mRoomsTR5[index].layers[i].layerBoundingBoxY1 < bboxMin[1])
  2033. bboxMin[1] = mRoomsTR5[index].layers[i].layerBoundingBoxY1;
  2034. if (mRoomsTR5[index].layers[i].layerBoundingBoxZ1 < bboxMin[2])
  2035. bboxMin[2] = mRoomsTR5[index].layers[i].layerBoundingBoxZ1;
  2036. if (mRoomsTR5[index].layers[i].layerBoundingBoxX2 < bboxMax[0])
  2037. bboxMax[0] = mRoomsTR5[index].layers[i].layerBoundingBoxX2;
  2038. if (mRoomsTR5[index].layers[i].layerBoundingBoxY2 < bboxMax[1])
  2039. bboxMax[1] = mRoomsTR5[index].layers[i].layerBoundingBoxY2;
  2040. if (mRoomsTR5[index].layers[i].layerBoundingBoxZ2 < bboxMax[2])
  2041. bboxMax[2] = mRoomsTR5[index].layers[i].layerBoundingBoxZ2;
  2042. }
  2043. break;
  2044. case TR_VERSION_1:
  2045. case TR_VERSION_2:
  2046. case TR_VERSION_3:
  2047. case TR_VERSION_4:
  2048. // Flags
  2049. *flags = _rooms[index].flags; // Needs to be generic flags in class
  2050. // Positioning
  2051. pos[0] = _rooms[index].info.x;
  2052. pos[1] = 0.0f;
  2053. pos[2] = _rooms[index].info.z;
  2054. bboxMin[0] = 0.0;
  2055. bboxMin[1] = 0.0;
  2056. bboxMin[2] = 0.0;
  2057. bboxMax[0] = 0.0;
  2058. bboxMax[1] = 0.0;
  2059. bboxMax[2] = 0.0;
  2060. // Bounding Box setup
  2061. n = ((_rooms[index].room_data.num_vertices < 0) ? 0 :
  2062. _rooms[index].room_data.num_vertices);
  2063. for (i = 0; i < n; ++i)
  2064. {
  2065. if (i == 0)
  2066. {
  2067. bboxMin[0] = _rooms[index].room_data.vertices[i].vertex.x;
  2068. bboxMin[1] = _rooms[index].room_data.vertices[i].vertex.y;
  2069. bboxMin[2] = _rooms[index].room_data.vertices[i].vertex.z;
  2070. bboxMax[0] = _rooms[index].room_data.vertices[i].vertex.x;
  2071. bboxMax[1] = _rooms[index].room_data.vertices[i].vertex.y;
  2072. bboxMax[2] = _rooms[index].room_data.vertices[i].vertex.z;
  2073. continue;
  2074. }
  2075. f = _rooms[index].room_data.vertices[i].vertex.x;
  2076. if (f < bboxMin[0])
  2077. bboxMin[0] = f;
  2078. if (f > bboxMax[0])
  2079. bboxMax[0] = f;
  2080. f = _rooms[index].room_data.vertices[i].vertex.y;
  2081. if (f < bboxMin[1])
  2082. bboxMin[1] = f;
  2083. if (f > bboxMax[1])
  2084. bboxMax[1] = f;
  2085. f = _rooms[index].room_data.vertices[i].vertex.z;
  2086. if (f < bboxMin[2])
  2087. bboxMin[2] = f;
  2088. if (f > bboxMax[2])
  2089. bboxMax[2] = f;
  2090. }
  2091. break;
  2092. }
  2093. }
  2094. int TombRaider::getRoomLight(unsigned int roomIndex, unsigned int index,
  2095. float pos[4], float color[4], float dir[3],
  2096. float *attenuation, float *cutoffAngle,
  2097. unsigned int *type, unsigned int *flags)
  2098. {
  2099. const float dd = 10000.0; // 4095.0;
  2100. float f;
  2101. *flags = 0; // reset
  2102. switch (getEngine())
  2103. {
  2104. case TR_VERSION_UNKNOWN:
  2105. return -1;
  2106. case TR_VERSION_1:
  2107. case TR_VERSION_2:
  2108. case TR_VERSION_3:
  2109. if (_rooms[roomIndex].num_lights <= 0 ||
  2110. (int)index > _rooms[roomIndex].num_lights)
  2111. {
  2112. return -1;
  2113. }
  2114. if (_rooms[roomIndex].lights[index].fade1 == 100730731)
  2115. {
  2116. f = 1.0;
  2117. }
  2118. else
  2119. {
  2120. f = _rooms[roomIndex].lights[index].fade1;
  2121. f /= dd;
  2122. }
  2123. *attenuation = f;
  2124. *flags |= tombraiderLight_useAttenuation;
  2125. pos[0] = _rooms[roomIndex].lights[index].x;
  2126. pos[1] = _rooms[roomIndex].lights[index].y;
  2127. pos[2] = _rooms[roomIndex].lights[index].z;
  2128. pos[3] = 1.0f;
  2129. color[0] = _rooms[roomIndex].lights[index].intensity1 / 409.60f;
  2130. color[1] = color[0];
  2131. color[2] = color[0];
  2132. color[3] = 1.0f;
  2133. if (Engine() == TR_VERSION_3)
  2134. {
  2135. color[0] = _rooms[roomIndex].room_light_colour.r;
  2136. color[1] = _rooms[roomIndex].room_light_colour.g;
  2137. color[2] = _rooms[roomIndex].room_light_colour.b;
  2138. }
  2139. *type = tombraiderLight_typeDirectional;
  2140. break;
  2141. case TR_VERSION_4:
  2142. if (_rooms[roomIndex].num_lights <= 0 ||
  2143. (int)index > _rooms[roomIndex].num_lights)
  2144. {
  2145. return -1;
  2146. }
  2147. pos[0] = _rooms[roomIndex].tr4Lights[index].xPosition;
  2148. pos[1] = _rooms[roomIndex].tr4Lights[index].yPosition;
  2149. pos[2] = _rooms[roomIndex].tr4Lights[index].zPosition;
  2150. pos[3] = 0.0f;
  2151. color[0] = _rooms[roomIndex].tr4Lights[index].color.r;
  2152. color[1] = _rooms[roomIndex].tr4Lights[index].color.g;
  2153. color[2] = _rooms[roomIndex].tr4Lights[index].color.b;
  2154. dir[0] = _rooms[roomIndex].tr4Lights[index].xDir;
  2155. dir[1] = _rooms[roomIndex].tr4Lights[index].yDir;
  2156. dir[2] = _rooms[roomIndex].tr4Lights[index].zDir;
  2157. *cutoffAngle = _rooms[roomIndex].tr4Lights[index].cutoff;
  2158. *flags |= tombraiderLight_useCutoff;
  2159. switch (_rooms[roomIndex].tr4Lights[index].lightType)
  2160. {
  2161. case 1:
  2162. *type = tombraiderLight_typeDirectional;
  2163. break;
  2164. case 2:
  2165. *type = tombraiderLight_typeSpot;
  2166. break;
  2167. default:
  2168. *type = tombraiderLight_typePoint;
  2169. }
  2170. break;
  2171. case TR_VERSION_5:
  2172. if (mRoomsTR5[roomIndex].numRoomLights <= 0 ||
  2173. (int)index > mRoomsTR5[roomIndex].numRoomLights)
  2174. {
  2175. return -1;
  2176. }
  2177. pos[0] = mRoomsTR5[roomIndex].lights[index].x;
  2178. pos[1] = mRoomsTR5[roomIndex].lights[index].y;
  2179. pos[2] = mRoomsTR5[roomIndex].lights[index].z;
  2180. pos[3] = 0.0f;
  2181. color[0] = mRoomsTR5[roomIndex].lights[index].red;
  2182. color[1] = mRoomsTR5[roomIndex].lights[index].green;
  2183. color[2] = mRoomsTR5[roomIndex].lights[index].blue;
  2184. dir[0] = mRoomsTR5[roomIndex].lights[index].directionVectorX;
  2185. dir[1] = mRoomsTR5[roomIndex].lights[index].directionVectorY;
  2186. dir[2] = mRoomsTR5[roomIndex].lights[index].directionVectorZ;
  2187. switch (mRoomsTR5[roomIndex].lights[index].lightType)
  2188. {
  2189. case 2:
  2190. *type = tombraiderLight_typeSpot;
  2191. break;
  2192. case 3:
  2193. *type = tombraiderLight_typeDirectional;
  2194. break;
  2195. default:
  2196. *type = tombraiderLight_typePoint;
  2197. }
  2198. break;
  2199. }
  2200. return 0;
  2201. }
  2202. unsigned int TombRaider::getRoomLightCount(unsigned int roomIndex)
  2203. {
  2204. if (!isRoomValid(roomIndex))
  2205. return 0;
  2206. switch (getEngine())
  2207. {
  2208. case TR_VERSION_UNKNOWN:
  2209. break;
  2210. case TR_VERSION_5:
  2211. return mRoomsTR5[roomIndex].numRoomLights;
  2212. case TR_VERSION_1:
  2213. case TR_VERSION_2:
  2214. case TR_VERSION_3:
  2215. case TR_VERSION_4:
  2216. return _rooms[roomIndex].num_lights;
  2217. }
  2218. return 0;
  2219. }
  2220. int TombRaider::getRoomModel(unsigned int roomIndex, unsigned int index,
  2221. int *modelIndex, float pos[3], float *yaw)
  2222. {
  2223. unsigned int i, count;
  2224. if (!getRoomModelCount(roomIndex) || index > getRoomModelCount(roomIndex))
  2225. return -1;
  2226. switch (getEngine())
  2227. {
  2228. case TR_VERSION_UNKNOWN:
  2229. return -1;
  2230. case TR_VERSION_5:
  2231. count = NumStaticMeshes();
  2232. for (i = 0; i < count; ++i)
  2233. {
  2234. if (mRoomsTR5[roomIndex].meshes[index].object_id ==
  2235. _static_meshes[i].object_id)
  2236. {
  2237. *modelIndex = _static_meshes[i].starting_mesh;
  2238. pos[0] = mRoomsTR5[roomIndex].meshes[index].x;
  2239. pos[1] = mRoomsTR5[roomIndex].meshes[index].y;
  2240. pos[2] = mRoomsTR5[roomIndex].meshes[index].z;
  2241. *yaw = ((mRoomsTR5[roomIndex].meshes[index].rotation >> 14)
  2242. & 0x03) * 90;
  2243. }
  2244. }
  2245. break;
  2246. case TR_VERSION_1:
  2247. case TR_VERSION_2:
  2248. case TR_VERSION_3:
  2249. case TR_VERSION_4:
  2250. count = NumStaticMeshes();
  2251. for (i = 0; i < count; ++i)
  2252. {
  2253. if (_rooms[roomIndex].static_meshes[index].object_id ==
  2254. _static_meshes[i].object_id)
  2255. {
  2256. *modelIndex = _static_meshes[i].starting_mesh;
  2257. pos[0] = _rooms[roomIndex].static_meshes[index].x;
  2258. pos[1] = _rooms[roomIndex].static_meshes[index].y;
  2259. pos[2] = _rooms[roomIndex].static_meshes[index].z;
  2260. *yaw = ((_rooms[roomIndex].static_meshes[index].rotation >> 14)
  2261. & 0x03) * 90;
  2262. }
  2263. }
  2264. }
  2265. return 0;
  2266. }
  2267. unsigned int TombRaider::getRoomModelCount(unsigned int roomIndex)
  2268. {
  2269. if (!isRoomValid(roomIndex))
  2270. return 0;
  2271. switch (getEngine())
  2272. {
  2273. case TR_VERSION_UNKNOWN:
  2274. break;
  2275. case TR_VERSION_5:
  2276. return mRoomsTR5[roomIndex].numStaticMeshes;
  2277. case TR_VERSION_1:
  2278. case TR_VERSION_2:
  2279. case TR_VERSION_3:
  2280. case TR_VERSION_4:
  2281. return _rooms[roomIndex].num_static_meshes;
  2282. }
  2283. return 0;
  2284. }
  2285. int TombRaider::getRoomPortal(unsigned int roomIndex, unsigned int index,
  2286. int *adjoiningRoom,
  2287. float normal[3], float vertices[12])
  2288. {
  2289. if (!getRoomPortalCount(roomIndex) || index > getRoomPortalCount(roomIndex))
  2290. return 0;
  2291. switch (getEngine())
  2292. {
  2293. case TR_VERSION_UNKNOWN:
  2294. break;
  2295. case TR_VERSION_5:
  2296. *adjoiningRoom = mRoomsTR5[roomIndex].doors[index].adjoining_room;
  2297. normal[0] = mRoomsTR5[roomIndex].doors[index].normal.x;// / NORMAL_SCALE;
  2298. normal[1] = mRoomsTR5[roomIndex].doors[index].normal.y;// / NORMAL_SCALE;
  2299. normal[2] = mRoomsTR5[roomIndex].doors[index].normal.z;// / NORMAL_SCALE;
  2300. vertices[0] = mRoomsTR5[roomIndex].doors[index].vertices[0].x;
  2301. vertices[1] = mRoomsTR5[roomIndex].doors[index].vertices[0].y;
  2302. vertices[2] = mRoomsTR5[roomIndex].doors[index].vertices[0].z;
  2303. vertices[3] = mRoomsTR5[roomIndex].doors[index].vertices[1].x;
  2304. vertices[4] = mRoomsTR5[roomIndex].doors[index].vertices[1].y;
  2305. vertices[5] = mRoomsTR5[roomIndex].doors[index].vertices[1].z;
  2306. vertices[6] = mRoomsTR5[roomIndex].doors[index].vertices[2].x;
  2307. vertices[7] = mRoomsTR5[roomIndex].doors[index].vertices[2].y;
  2308. vertices[8] = mRoomsTR5[roomIndex].doors[index].vertices[2].z;
  2309. vertices[9] = mRoomsTR5[roomIndex].doors[index].vertices[3].x;
  2310. vertices[10] = mRoomsTR5[roomIndex].doors[index].vertices[3].y;
  2311. vertices[11] = mRoomsTR5[roomIndex].doors[index].vertices[3].z;
  2312. break;
  2313. case TR_VERSION_1:
  2314. case TR_VERSION_2:
  2315. case TR_VERSION_3:
  2316. case TR_VERSION_4:
  2317. *adjoiningRoom = _rooms[roomIndex].portals[index].adjoining_room;
  2318. normal[0] = _rooms[roomIndex].portals[index].normal.x;// / NORMAL_SCALE;
  2319. normal[1] = _rooms[roomIndex].portals[index].normal.y;// / NORMAL_SCALE;
  2320. normal[2] = _rooms[roomIndex].portals[index].normal.z;// / NORMAL_SCALE;
  2321. vertices[0] = _rooms[roomIndex].portals[index].vertices[0].x;
  2322. vertices[1] = _rooms[roomIndex].portals[index].vertices[0].y;
  2323. vertices[2] = _rooms[roomIndex].portals[index].vertices[0].z;
  2324. vertices[3] = _rooms[roomIndex].portals[index].vertices[1].x;
  2325. vertices[4] = _rooms[roomIndex].portals[index].vertices[1].y;
  2326. vertices[5] = _rooms[roomIndex].portals[index].vertices[1].z;
  2327. vertices[6] = _rooms[roomIndex].portals[index].vertices[2].x;
  2328. vertices[7] = _rooms[roomIndex].portals[index].vertices[2].y;
  2329. vertices[8] = _rooms[roomIndex].portals[index].vertices[2].z;
  2330. vertices[9] = _rooms[roomIndex].portals[index].vertices[3].x;
  2331. vertices[10] = _rooms[roomIndex].portals[index].vertices[3].y;
  2332. vertices[11] = _rooms[roomIndex].portals[index].vertices[3].z;
  2333. }
  2334. return 0;
  2335. }
  2336. unsigned int TombRaider::getRoomPortalCount(unsigned int roomIndex)
  2337. {
  2338. if (!isRoomValid(roomIndex))
  2339. return 0;
  2340. switch (getEngine())
  2341. {
  2342. case TR_VERSION_UNKNOWN:
  2343. break;
  2344. case TR_VERSION_5:
  2345. return mRoomsTR5[roomIndex].numDoors;
  2346. case TR_VERSION_1:
  2347. case TR_VERSION_2:
  2348. case TR_VERSION_3:
  2349. case TR_VERSION_4:
  2350. return _rooms[roomIndex].num_portals;
  2351. }
  2352. return 0;
  2353. }
  2354. //! \fixme No TRC support
  2355. void TombRaider::getRoomRectangle(unsigned int roomIndex,
  2356. unsigned int rectangleIndex,
  2357. unsigned int *indices, float *texCoords,
  2358. int *texture, unsigned int *flags)
  2359. {
  2360. int tIndex;
  2361. unsigned int count, i, j, k;
  2362. switch (getEngine())
  2363. {
  2364. case TR_VERSION_UNKNOWN:
  2365. break;
  2366. case TR_VERSION_5:
  2367. for (i = 0, count = 0; i < mRoomsTR5[roomIndex].numLayers; ++i)
  2368. {
  2369. if (count + mRoomsTR5[roomIndex].layers[i].numLayerRectangles >
  2370. rectangleIndex)
  2371. {
  2372. k = rectangleIndex - count;
  2373. *texture = *flags = 0; // FIXME
  2374. // Setup per vertex
  2375. for (j = 0; j < 3; ++j)
  2376. {
  2377. // Get vertex index { (0, a), (1, b), (2, c), (3, d) }
  2378. indices[j] = mRoomsTR5[roomIndex].faces[i].quads[k].vertices[j];
  2379. // FIXME
  2380. //tIndex = mRoomsTR5[roomIndex].faces[i].vertices[j].texture & 0xff;
  2381. //computeUV(objectTexturesTR5[tIndex].vertices + i,
  2382. // texCoords+i*2, texCoords+i*2+1);
  2383. }
  2384. break;
  2385. }
  2386. count += mRoomsTR5[roomIndex].layers[i].numLayerRectangles;
  2387. }
  2388. break;
  2389. case TR_VERSION_1:
  2390. case TR_VERSION_2:
  2391. case TR_VERSION_3:
  2392. case TR_VERSION_4:
  2393. tIndex = _rooms[roomIndex].room_data.rectangles[rectangleIndex].texture;
  2394. *texture = _object_textures[tIndex].tile;
  2395. *flags = 0;
  2396. switch (_object_textures[tIndex].transparency_flags)
  2397. {
  2398. case 0:
  2399. break;
  2400. case 2:
  2401. *flags |= tombraiderFace_PartialAlpha;
  2402. break;
  2403. default:
  2404. *flags |= tombraiderFace_Alpha;
  2405. break;
  2406. }
  2407. // Setup per vertex
  2408. for (i = 0; i < 4; ++i)
  2409. {
  2410. // Get vertex index {(0, a), (1, b), (2, c), (3, d)}
  2411. indices[i] = _rooms[roomIndex].room_data.rectangles[rectangleIndex].vertices[i];
  2412. computeUV(_object_textures[tIndex].vertices + i,
  2413. texCoords+i*2, texCoords+i*2+1);
  2414. }
  2415. }
  2416. }
  2417. unsigned int TombRaider::getRoomRectangleCount(unsigned int roomIndex)
  2418. {
  2419. unsigned int i, count;
  2420. if (!isRoomValid(roomIndex))
  2421. return 0;
  2422. switch (getEngine())
  2423. {
  2424. case TR_VERSION_UNKNOWN:
  2425. break;
  2426. case TR_VERSION_5:
  2427. for (i = 0, count = 0; i < mRoomsTR5[roomIndex].numLayers; ++i)
  2428. {
  2429. count += mRoomsTR5[roomIndex].layers[i].numLayerRectangles;
  2430. }
  2431. return count;
  2432. case TR_VERSION_1:
  2433. case TR_VERSION_2:
  2434. case TR_VERSION_3:
  2435. case TR_VERSION_4:
  2436. return ((_rooms[roomIndex].room_data.num_rectangles < 0) ? 0 :
  2437. _rooms[roomIndex].room_data.num_rectangles);
  2438. }
  2439. return 0;
  2440. }
  2441. int TombRaider::getRoomSector(unsigned int roomIndex, unsigned int index,
  2442. unsigned int *flags,
  2443. float *ceiling, float *floor,
  2444. int *floorDataIndex, int *boxIndex,
  2445. int *roomBelow, int *roomAbove)
  2446. {
  2447. unsigned int count;
  2448. unsigned int zSectorsCount;
  2449. unsigned int xSectorsCount;
  2450. count = getRoomSectorCount(roomIndex, &zSectorsCount, &xSectorsCount);
  2451. if (!count || index > count)
  2452. return -1;
  2453. *flags = 0;
  2454. switch (getEngine())
  2455. {
  2456. case TR_VERSION_UNKNOWN:
  2457. break;
  2458. case TR_VERSION_5:
  2459. *floorDataIndex = mRoomsTR5[roomIndex].sectors[index].fd_index;
  2460. *boxIndex = mRoomsTR5[roomIndex].sectors[index].box_index;
  2461. *roomBelow = mRoomsTR5[roomIndex].sectors[index].room_below;
  2462. *roomAbove = mRoomsTR5[roomIndex].sectors[index].room_above;
  2463. if ((unsigned char)mRoomsTR5[roomIndex].sectors[index].floor == 0x81 ||
  2464. (unsigned char)mRoomsTR5[roomIndex].sectors[index].ceiling == 0x81)
  2465. {
  2466. *flags |= tombraiderSector_wall;
  2467. }
  2468. *floor = mRoomsTR5[roomIndex].sectors[index].floor * 256.0f;
  2469. *ceiling = mRoomsTR5[roomIndex].sectors[index].ceiling * 256.0f;
  2470. break;
  2471. case TR_VERSION_1:
  2472. case TR_VERSION_2:
  2473. case TR_VERSION_3:
  2474. case TR_VERSION_4:
  2475. *floorDataIndex = _rooms[roomIndex].sector_list[index].fd_index;
  2476. *boxIndex = _rooms[roomIndex].sector_list[index].box_index;
  2477. *roomBelow = _rooms[roomIndex].sector_list[index].room_below;
  2478. *roomAbove = _rooms[roomIndex].sector_list[index].room_above;
  2479. if ((unsigned char)_rooms[roomIndex].sector_list[index].floor == 0x81 ||
  2480. (unsigned char)_rooms[roomIndex].sector_list[index].ceiling == 0x81)
  2481. {
  2482. *flags |= tombraiderSector_wall;
  2483. }
  2484. *floor = _rooms[roomIndex].sector_list[index].floor * 256.0f;
  2485. *ceiling = _rooms[roomIndex].sector_list[index].ceiling * 256.0f;
  2486. }
  2487. if (*boxIndex == 65536)
  2488. {
  2489. *boxIndex = -1;
  2490. }
  2491. if (*roomBelow == 255)
  2492. {
  2493. *roomBelow = -1;
  2494. }
  2495. if (*roomAbove == 255)
  2496. {
  2497. *roomAbove = -1;
  2498. }
  2499. return 0;
  2500. }
  2501. unsigned int TombRaider::getRoomSectorCount(unsigned int roomIndex,
  2502. unsigned int *zSectorsCount,
  2503. unsigned int *xSectorsCount)
  2504. {
  2505. unsigned int count;
  2506. if (!isRoomValid(roomIndex))
  2507. return 0;
  2508. switch (getEngine())
  2509. {
  2510. case TR_VERSION_UNKNOWN:
  2511. break;
  2512. case TR_VERSION_5:
  2513. // width of sector list
  2514. *zSectorsCount = mRoomsTR5[roomIndex].numZSectors;
  2515. // height of sector list
  2516. *xSectorsCount = mRoomsTR5[roomIndex].numXSectors;
  2517. return (mRoomsTR5[roomIndex].numZSectors *
  2518. mRoomsTR5[roomIndex].numXSectors);
  2519. case TR_VERSION_1:
  2520. case TR_VERSION_2:
  2521. case TR_VERSION_3:
  2522. case TR_VERSION_4:
  2523. // width of sector list
  2524. *zSectorsCount = _rooms[roomIndex].num_zsectors;
  2525. // height of sector list
  2526. *xSectorsCount = _rooms[roomIndex].num_xsectors;
  2527. count = _rooms[roomIndex].num_zsectors * _rooms[roomIndex].num_xsectors;
  2528. return count;
  2529. }
  2530. return 0;
  2531. }
  2532. void TombRaider::getRoomSprite(unsigned int roomIndex, unsigned int index,
  2533. float scale, int *texture,
  2534. float *pos, float *vertices, float *texcoords)
  2535. {
  2536. tr2_sprite_texture_t *sprite;
  2537. tr2_vertex_t *vertex;
  2538. int t_index, width, height, x, y;
  2539. float width2, height2;
  2540. unsigned int spriteCount;
  2541. spriteCount = getRoomSpriteCount(roomIndex);
  2542. if (spriteCount == 0 || index > spriteCount)
  2543. return;
  2544. if (scale == 0.0)
  2545. scale = 10.0;
  2546. t_index = _rooms[roomIndex].room_data.sprites[index].texture;
  2547. vertex =
  2548. &_rooms[roomIndex].room_data.vertices[_rooms[roomIndex].room_data.sprites[index].vertex].vertex;
  2549. sprite = &_sprite_textures[t_index];
  2550. // Info, offset in room added to position
  2551. pos[0] = _rooms[roomIndex].info.x + vertex->x;
  2552. pos[1] = vertex->y;
  2553. pos[2] = _rooms[roomIndex].info.z + vertex->z;
  2554. *texture = sprite->tile;
  2555. width = sprite->width >> 8;
  2556. height = sprite->height >> 8;
  2557. x = sprite->x;
  2558. y = sprite->y;
  2559. width2 = width * scale; // scale it up a bit (sprites are rather tiny)
  2560. height2 = height * scale;
  2561. // Quad
  2562. vertices[0] = -width2 / 2.0f;
  2563. vertices[1] = 0;
  2564. vertices[2] = 0;
  2565. vertices[3] = -width2 / 2.0f;
  2566. vertices[4] = -height2;
  2567. vertices[5] = 0;
  2568. vertices[6] = width2 / 2.0f;
  2569. vertices[7] = -height2;
  2570. vertices[8] = 0;
  2571. vertices[9] = width2 / 2.0f;
  2572. vertices[10] = 0;
  2573. vertices[11] = 0;
  2574. texcoords[0] = ((float)y + height) / mTexelScale;
  2575. texcoords[1] = (float)(x) / mTexelScale;
  2576. texcoords[2] = (float)(y) / mTexelScale;
  2577. texcoords[3] = (float)(x) / mTexelScale;
  2578. texcoords[4] = (float)(y) / mTexelScale;
  2579. texcoords[5] = ((float)x + width) / mTexelScale;
  2580. texcoords[6] = ((float)y + height) / mTexelScale;
  2581. texcoords[7] = ((float)x + width) / mTexelScale;
  2582. }
  2583. #ifdef OPTIONAL_ARRAY_INTERFACE
  2584. void TombRaider::getRoomSpriteArray(unsigned int index, float scale,
  2585. unsigned int *spriteCount, int *textures,
  2586. float *pos, float *vertices, float *texcoords)
  2587. {
  2588. int i, t_index;
  2589. tr2_vertex_t *vertex;
  2590. int width, height, x, y;
  2591. float width2, height2;
  2592. *spriteCount = ((room[index].room_data.num_sprites < 0) ?
  2593. 0 : room[index].room_data.num_sprites);
  2594. if (*spriteCount == 0)
  2595. return;
  2596. textures = new int[*spriteCount];
  2597. pos = new float[*spriteCount * 3];
  2598. vertices = new float[*spriteCount * 12];
  2599. texcoords = new float[*spriteCount * 8];
  2600. if (scale == 0.0)
  2601. scale = 10.0;
  2602. for (i = 0; i < *spriteCount; ++i)
  2603. {
  2604. t_index = room[index].room_data.sprites[i].texture;
  2605. vertex =
  2606. &room[index].room_data.vertices[room[index].room_data.sprites[i].vertex].vertex;
  2607. sprite = &sprite_textures[t_index];
  2608. // Info, offset in room added to position
  2609. pos[0+i*3] = room[index].info.x + vertex->x;
  2610. pos[1+i*3] = vertex->y;
  2611. pos[2+i*3] = room[index].info.z + vertex->z;
  2612. textures[i] = sprite->tile;
  2613. width = sprite->width >> 8;
  2614. height = sprite->height >> 8;
  2615. x = sprite->x;
  2616. y = sprite->y;
  2617. width2 = width * scale; // scale it up a bit (sprites are rather tiny)
  2618. height2 = height * scale;
  2619. // Quad
  2620. vertices[0+i*12] = -width2 / 2.0;
  2621. vertices[1+i*12] = 0;
  2622. vertices[2+i*12] = 0;
  2623. vertices[3+i*12] = -width2 / 2.0;
  2624. vertices[4+i*12] = -height2;
  2625. vertices[5+i*12] = 0;
  2626. vertices[6+i*12] = width2 / 2.0;
  2627. vertices[7+i*12] = -height2;
  2628. vertices[8+i*12] = 0;
  2629. vertices[9+i*12] = width2 / 2.0;
  2630. vertices[10+i*12] = 0;
  2631. vertices[11+i*12] = 0;
  2632. texcoords[0+i*8] = ((float)y + height) / mTexelScale;
  2633. texcoords[1+i*8] = (float)(x) / mTexelScale;
  2634. texcoords[2+i*8] = (float)(y) / mTexelScale;
  2635. texcoords[3+i*8] = (float)(x) / mTexelScale;
  2636. texcoords[4+i*8] = (float)(y) / mTexelScale;
  2637. texcoords[5+i*8] = ((float)x + width) / mTexelScale;
  2638. texcoords[6+i*8] = ((float)y + height) / mTexelScale;
  2639. texcoords[7+i*8] = ((float)x + width) / mTexelScale;
  2640. }
  2641. }
  2642. #endif
  2643. unsigned int TombRaider::getRoomSpriteCount(unsigned int roomIndex)
  2644. {
  2645. if (!isRoomValid(roomIndex))
  2646. return 0;
  2647. switch (getEngine())
  2648. {
  2649. case TR_VERSION_UNKNOWN:
  2650. break;
  2651. case TR_VERSION_5:
  2652. return 0; // No room sprites in TRC
  2653. case TR_VERSION_1:
  2654. case TR_VERSION_2:
  2655. case TR_VERSION_3:
  2656. case TR_VERSION_4:
  2657. return ((_rooms[roomIndex].room_data.num_sprites < 0) ?
  2658. 0 : _rooms[roomIndex].room_data.num_sprites);
  2659. }
  2660. return 0;
  2661. }
  2662. void TombRaider::getRoomTriangle(unsigned int roomIndex,
  2663. unsigned int triangleIndex,
  2664. unsigned int *indices, float *texCoords,
  2665. int *texture, unsigned int *flags)
  2666. {
  2667. int tIndex;
  2668. unsigned int count, i, j, k;
  2669. switch (getEngine())
  2670. {
  2671. case TR_VERSION_UNKNOWN:
  2672. break;
  2673. case TR_VERSION_5:
  2674. for (i = 0, count = 0; i < mRoomsTR5[roomIndex].numLayers; ++i)
  2675. {
  2676. if (count + mRoomsTR5[roomIndex].layers[i].numLayerTriangles >
  2677. triangleIndex)
  2678. {
  2679. k = triangleIndex - count;
  2680. *texture = *flags = 0; // FIXME
  2681. // Setup per vertex
  2682. for (j = 0; j < 3; ++j)
  2683. {
  2684. // Get vertex index {(0, a), (1, b), (2, c) }
  2685. indices[j] = mRoomsTR5[roomIndex].faces[i].tris[k].vertices[j];
  2686. // FIXME
  2687. //tIndex = mRoomsTR5[roomIndex].faces[i].vertices[j].texture & 0xff;
  2688. //computeUV(objectTexturesTR5[tIndex].vertices + i,
  2689. // texCoords+i*2, texCoords+i*2+1);
  2690. }
  2691. break;
  2692. }
  2693. count += mRoomsTR5[roomIndex].layers[i].numLayerTriangles;
  2694. }
  2695. break;
  2696. case TR_VERSION_1:
  2697. case TR_VERSION_2:
  2698. case TR_VERSION_3:
  2699. case TR_VERSION_4:
  2700. tIndex = _rooms[roomIndex].room_data.triangles[triangleIndex].texture;
  2701. *texture = _object_textures[tIndex].tile;
  2702. *flags = 0;
  2703. switch (_object_textures[tIndex].transparency_flags)
  2704. {
  2705. case 0:
  2706. break;
  2707. case 2:
  2708. *flags |= tombraiderFace_PartialAlpha;
  2709. break;
  2710. default:
  2711. *flags |= tombraiderFace_Alpha;
  2712. break;
  2713. }
  2714. // Setup per vertex
  2715. for (i = 0; i < 3; ++i)
  2716. {
  2717. // Get vertex index {(0, a), (1, b), (2, c) }
  2718. indices[i] = _rooms[roomIndex].room_data.triangles[triangleIndex].vertices[i];
  2719. computeUV(_object_textures[tIndex].vertices + i,
  2720. texCoords+i*2, texCoords+i*2+1);
  2721. }
  2722. }
  2723. }
  2724. void TombRaider::getRoomTriangles(unsigned int index, int textureOffset,
  2725. unsigned int *triangleCount,
  2726. unsigned int **indices,
  2727. float **texCoords, int **textures,
  2728. unsigned int **flags)
  2729. {
  2730. unsigned int count, i, j;
  2731. int texture;
  2732. if (!isRoomValid(index))
  2733. return;
  2734. switch (getEngine())
  2735. {
  2736. case TR_VERSION_UNKNOWN:
  2737. break;
  2738. case TR_VERSION_5:
  2739. for (i = 0, count = 0; i < mRoomsTR5[index].numLayers; ++i)
  2740. {
  2741. count += mRoomsTR5[index].layers[i].numLayerTriangles;
  2742. }
  2743. //! \fixme !!!
  2744. break;
  2745. case TR_VERSION_1:
  2746. case TR_VERSION_2:
  2747. case TR_VERSION_3:
  2748. case TR_VERSION_4:
  2749. // Generate textured triangles
  2750. count = ((_rooms[index].room_data.num_triangles < 0) ? 0 :
  2751. _rooms[index].room_data.num_triangles);
  2752. *triangleCount = count;
  2753. *indices = new unsigned int[count * 3];
  2754. *texCoords = new float[count * 6];
  2755. *textures = new int[count];
  2756. *flags = new unsigned int[count];
  2757. for (i = 0; i < count; ++i)
  2758. {
  2759. texture = _rooms[index].room_data.triangles[i].texture;
  2760. // Adjust texture id using m_texOffset to map into
  2761. // correct textures
  2762. (*textures)[i] = _object_textures[texture].tile + textureOffset;
  2763. (*flags)[i] = 0;
  2764. switch (_object_textures[texture].transparency_flags)
  2765. {
  2766. case 0:
  2767. break;
  2768. case 2:
  2769. (*flags)[i] |= tombraiderFace_PartialAlpha;
  2770. break;
  2771. default:
  2772. (*flags)[i] |= tombraiderFace_Alpha;
  2773. break;
  2774. }
  2775. // Setup per vertex
  2776. for (j = 0; j < 3; ++j)
  2777. {
  2778. // Get vertex index {(0, a), (1, b), (2, c)}
  2779. (*indices)[i*3+j] = _rooms[index].room_data.triangles[i].vertices[j];
  2780. computeUV(_object_textures[texture].vertices+j,
  2781. (*texCoords)+i*3+j*2, (*texCoords)+i*3+j*2+1);
  2782. }
  2783. }
  2784. }
  2785. }
  2786. unsigned int TombRaider::getRoomTriangleCount(unsigned int roomIndex)
  2787. {
  2788. unsigned int i, count;
  2789. if (!isRoomValid(roomIndex))
  2790. return 0;
  2791. switch (getEngine())
  2792. {
  2793. case TR_VERSION_UNKNOWN:
  2794. break;
  2795. case TR_VERSION_5:
  2796. for (i = 0, count = 0; i < mRoomsTR5[roomIndex].numLayers; ++i)
  2797. {
  2798. count += mRoomsTR5[roomIndex].layers[i].numLayerTriangles;
  2799. }
  2800. return count;
  2801. case TR_VERSION_1:
  2802. case TR_VERSION_2:
  2803. case TR_VERSION_3:
  2804. case TR_VERSION_4:
  2805. return ((_rooms[roomIndex].room_data.num_triangles < 0) ? 0 :
  2806. _rooms[roomIndex].room_data.num_triangles);
  2807. }
  2808. return 0;
  2809. }
  2810. //! \fixme No TR5 support
  2811. void TombRaider::getRoomVertex(unsigned int roomIndex,unsigned int vertexIndex,
  2812. float *xyz, float *rgba)
  2813. {
  2814. float color_value;
  2815. unsigned int index = roomIndex, i = vertexIndex;
  2816. tr2_vertex_t *vertex = 0x0;
  2817. switch (getEngine())
  2818. {
  2819. case TR_VERSION_5:
  2820. //! \fixme !!!
  2821. print("getRoomVertex", "Error: No TRC implementation is the answer");
  2822. break;
  2823. case TR_VERSION_1:
  2824. case TR_VERSION_2:
  2825. case TR_VERSION_3:
  2826. case TR_VERSION_4:
  2827. case TR_VERSION_UNKNOWN:
  2828. vertex = &_rooms[index].room_data.vertices[i].vertex;
  2829. xyz[0] = vertex->x;
  2830. xyz[1] = vertex->y;
  2831. xyz[2] = vertex->z;
  2832. switch (getEngine())
  2833. {
  2834. case TR_VERSION_1:
  2835. color_value = _rooms[index].room_data.vertices[i].attributes;
  2836. color_value = (1.1f - (color_value / 8192.0f));
  2837. rgba[0] = color_value;
  2838. rgba[1] = color_value;
  2839. rgba[2] = color_value;
  2840. break;
  2841. case TR_VERSION_3:
  2842. case TR_VERSION_4:
  2843. color_value = _rooms[index].room_data.vertices[i].lighting1;
  2844. color_value /= 16384.0;
  2845. rgba[0] = color_value + ((float)_rooms[index].room_light_colour.r /
  2846. mRoomVertexLightingFactor);
  2847. rgba[1] = color_value + ((float)_rooms[index].room_light_colour.g /
  2848. mRoomVertexLightingFactor);
  2849. rgba[2] = color_value + ((float)_rooms[index].room_light_colour.b /
  2850. mRoomVertexLightingFactor);
  2851. break;
  2852. case TR_VERSION_2:
  2853. case TR_VERSION_5: // Not really...
  2854. case TR_VERSION_UNKNOWN:
  2855. color_value = _rooms[index].room_data.vertices[i].lighting1;
  2856. color_value = (1.1f - (color_value / 8192.0f));
  2857. rgba[0] = color_value;
  2858. rgba[1] = color_value;
  2859. rgba[2] = color_value;
  2860. }
  2861. // Underwater rooms have an ambient color with a shade of blue
  2862. if (_rooms[index].flags & 0x0001)
  2863. {
  2864. rgba[0] *= 0.6;
  2865. rgba[2] *= 1.2;
  2866. }
  2867. // Alpha color
  2868. rgba[3] = 1.0;
  2869. }
  2870. }
  2871. void TombRaider::getRoomVertexArrays(unsigned int roomIndex,
  2872. unsigned int *vertexCount, float **vertices,
  2873. unsigned int *normalCount, float **normals,
  2874. unsigned int *colorCount, float **colors)
  2875. {
  2876. float color_value;
  2877. float rgba[4];
  2878. unsigned int i, j, k, count;
  2879. unsigned char c;
  2880. tr2_vertex_t *vertex = 0x0;
  2881. count = getRoomVertexCount(roomIndex);
  2882. *vertexCount = 0;
  2883. *vertices = 0x0;
  2884. *normalCount = 0;
  2885. *normals = 0x0;
  2886. *colorCount = 0;
  2887. *colors = 0x0;
  2888. if (count == 0)
  2889. return;
  2890. switch (getEngine())
  2891. {
  2892. case TR_VERSION_UNKNOWN:
  2893. break;
  2894. case TR_VERSION_5:
  2895. *vertexCount = count;
  2896. *vertices = new float[count*3];
  2897. *normalCount = count;
  2898. *normals = new float[count*3];
  2899. *colorCount = count;
  2900. *colors = new float[count*4];
  2901. for (i = 0, k = 0; i < mRoomsTR5[roomIndex].numLayers; ++i)
  2902. {
  2903. for (j = 0; j < mRoomsTR5[roomIndex].layers[i].numLayerVertices; ++j)
  2904. {
  2905. (*vertices)[k*3] = mRoomsTR5[roomIndex].faces[i].verts[j].x;
  2906. (*vertices)[k*3+1] = mRoomsTR5[roomIndex].faces[i].verts[j].y;
  2907. (*vertices)[k*3+2] = mRoomsTR5[roomIndex].faces[i].verts[j].z;
  2908. (*normals)[k*3] = mRoomsTR5[roomIndex].faces[i].verts[j].nx;
  2909. (*normals)[k*3+1] = mRoomsTR5[roomIndex].faces[i].verts[j].ny;
  2910. (*normals)[k*3+2] = mRoomsTR5[roomIndex].faces[i].verts[j].nz;
  2911. //! \fixme Ah, yeah this may be wrong
  2912. c = ((unsigned char *)&mRoomsTR5[roomIndex].faces[i].verts[j].vColor)[1];
  2913. (*colors)[k*4] = (float)c / 255.0f;
  2914. c = ((unsigned char *)&mRoomsTR5[roomIndex].faces[i].verts[j].vColor)[2];
  2915. (*colors)[k*4+1] = (float)c / 255.0f;
  2916. c = ((unsigned char *)&mRoomsTR5[roomIndex].faces[i].verts[j].vColor)[3];
  2917. (*colors)[k*4+2] = (float)c / 255.0f;
  2918. c = ((unsigned char *)&mRoomsTR5[roomIndex].faces[i].verts[j].vColor)[0];
  2919. (*colors)[k*4+3] = (float)c / 255.0f;
  2920. }
  2921. }
  2922. break;
  2923. case TR_VERSION_1:
  2924. case TR_VERSION_2:
  2925. case TR_VERSION_3:
  2926. case TR_VERSION_4:
  2927. *vertexCount = count;
  2928. *vertices = new float[count*3];
  2929. *normalCount = 0; //! \fixme Do some TR1-TR4 levels support normals here?
  2930. *normals = 0x0;
  2931. *colorCount = count;
  2932. *colors = new float[count*4];
  2933. // Setup vertex coloring and vertices
  2934. for (i = 0; i < count; ++i)
  2935. {
  2936. vertex = &_rooms[roomIndex].room_data.vertices[i].vertex;
  2937. (*vertices)[i*3] = vertex->x;
  2938. (*vertices)[i*3+1] = vertex->y;
  2939. (*vertices)[i*3+2] = vertex->z;
  2940. switch (getEngine())
  2941. {
  2942. case TR_VERSION_1:
  2943. color_value = _rooms[roomIndex].room_data.vertices[i].attributes;
  2944. color_value = (1.1f - (color_value / 8192.0f));
  2945. break;
  2946. case TR_VERSION_4:
  2947. case TR_VERSION_3:
  2948. color_value = _rooms[roomIndex].room_data.vertices[i].lighting1;
  2949. color_value /= 16384.0;
  2950. break;
  2951. case TR_VERSION_2:
  2952. case TR_VERSION_5:
  2953. case TR_VERSION_UNKNOWN:
  2954. color_value = _rooms[roomIndex].room_data.vertices[i].lighting1;
  2955. color_value = (1.1f - (color_value / 8192.0f));
  2956. }
  2957. switch (getEngine())
  2958. {
  2959. case TR_VERSION_4:
  2960. case TR_VERSION_3:
  2961. rgba[0] = (color_value +
  2962. ((float)_rooms[roomIndex].room_light_colour.r /
  2963. mRoomVertexLightingFactor));
  2964. rgba[1] = (color_value +
  2965. ((float)_rooms[roomIndex].room_light_colour.g /
  2966. mRoomVertexLightingFactor));
  2967. rgba[2] = (color_value +
  2968. ((float)_rooms[roomIndex].room_light_colour.b /
  2969. mRoomVertexLightingFactor));
  2970. break;
  2971. case TR_VERSION_1:
  2972. case TR_VERSION_2:
  2973. case TR_VERSION_5:
  2974. case TR_VERSION_UNKNOWN:
  2975. rgba[0] = color_value;
  2976. rgba[1] = color_value;
  2977. rgba[2] = color_value;
  2978. }
  2979. // Underwater rooms have an ambient color with a shade of blue
  2980. if (_rooms[roomIndex].flags & 0x0001)
  2981. {
  2982. rgba[0] *= 0.6;
  2983. rgba[2] *= 1.2;
  2984. }
  2985. // Alpha color
  2986. rgba[3] = 1.0;
  2987. (*colors)[i*4] = rgba[0];
  2988. (*colors)[i*4+1] = rgba[1];
  2989. (*colors)[i*4+2] = rgba[2];
  2990. (*colors)[i*4+3] = rgba[3];
  2991. }
  2992. }
  2993. }
  2994. unsigned int TombRaider::getRoomVertexCount(unsigned int roomIndex)
  2995. {
  2996. unsigned int i, count;
  2997. if (!isRoomValid(roomIndex))
  2998. return 0;
  2999. switch (getEngine())
  3000. {
  3001. case TR_VERSION_UNKNOWN:
  3002. break;
  3003. case TR_VERSION_5:
  3004. for (i = 0, count = 0; i < mRoomsTR5[roomIndex].numLayers; ++i)
  3005. {
  3006. count += mRoomsTR5[roomIndex].layers[i].numLayerVertices;
  3007. }
  3008. return count;
  3009. case TR_VERSION_1:
  3010. case TR_VERSION_2:
  3011. case TR_VERSION_3:
  3012. case TR_VERSION_4:
  3013. return ((_rooms[roomIndex].room_data.num_vertices < 0) ? 0 :
  3014. _rooms[roomIndex].room_data.num_vertices);
  3015. }
  3016. return 0;
  3017. }
  3018. int TombRaider::getSkyModelId()
  3019. {
  3020. int skyMesh = -1;
  3021. //bool rot = false;
  3022. tr2_moveable_t *moveable;
  3023. unsigned int i, id;
  3024. moveable = Moveable();
  3025. switch (Engine())
  3026. {
  3027. case TR_VERSION_2:
  3028. //rot = true;
  3029. id = 254;
  3030. break;
  3031. case TR_VERSION_3:
  3032. id = 355;
  3033. break;
  3034. case TR_VERSION_4:
  3035. id = 459;
  3036. break;
  3037. case TR_VERSION_1:
  3038. case TR_VERSION_5:
  3039. case TR_VERSION_UNKNOWN:
  3040. return -1;
  3041. }
  3042. if (id > 0)
  3043. {
  3044. for (i = 0; (int)i < NumMoveables(); ++i)
  3045. {
  3046. if (moveable[i].object_id == id)
  3047. {
  3048. //sky_mesh = moveable[i].starting_mesh;
  3049. skyMesh = i;
  3050. break;
  3051. }
  3052. }
  3053. }
  3054. return skyMesh;
  3055. }
  3056. void TombRaider::getSprites()
  3057. {
  3058. #ifdef FIXME
  3059. int i, j, k, l, x, y, s_index, width, height;
  3060. float scale, width2, height2;
  3061. tr2_sprite_texture_t *sprite;
  3062. tr2_sprite_texture_t *sprite_textures;
  3063. tr2_sprite_sequence_t *sprite_sequence;
  3064. sprite_seq_t *r_mesh;
  3065. tr2_item_t *item;
  3066. r_mesh = Mesh();
  3067. item = Item();
  3068. sprite_textures = Sprite();
  3069. sprite_sequence = SpriteSequence();
  3070. scale = 4.0;
  3071. printf("Processing sprites: ");
  3072. for (i = 0; i < NumItems() - 1; ++i)
  3073. {
  3074. print(false, "Processing sprites in Items: %i/%i",
  3075. i, NumItems());
  3076. // It's a mesh, skip it
  3077. if (Engine() == TR_VERSION_1 && item[i].intensity1 == -1)
  3078. continue;
  3079. k = item[i].object_id;
  3080. // Search the SpriteSequence list
  3081. // (if we didn't already decide that it's a mesh)
  3082. for (j = 0; j < (int)NumSpriteSequences(); ++j)
  3083. {
  3084. if (sprite_sequence[j].object_id == k)
  3085. {
  3086. k = item[i].object_id;
  3087. s_index = sprite_sequence[j].offset;
  3088. spriteSequence = new sprite_seq_t;
  3089. spriteSequence->spriteCount = -sprite_sequence[j].negative_length;
  3090. spriteSequence->sprites = new sprite_t[r_mesh->num_sprites];
  3091. sprite = spriteSequence->sprites;
  3092. callbackAddSpriteSequence(spriteSequence);
  3093. for (l = 0; l < r_mesh->num_sprites; ++l)
  3094. {
  3095. sprite = &sprite_textures[s_index];
  3096. width = sprite->width >> 8;
  3097. height = sprite->height >> 8;
  3098. x = sprite->x;
  3099. y = sprite->y;
  3100. width2 = width * scale;
  3101. height2 = height * scale;
  3102. // For external use
  3103. sprite[l].pos[0] = item[i].x;
  3104. sprite[l].pos[1] = item[i].y;
  3105. sprite[l].pos[2] = item[i].z;
  3106. sprite[l].textureIndex = sprite->tile;
  3107. sprite[l].radius = width2 / 2.0;
  3108. sprite[l].vertex[0].pos[0] = -width2 / 2.0;
  3109. sprite[l].vertex[1].pos[0] = -width2 / 2.0;
  3110. sprite[l].vertex[2].pos[0] = width2 / 2.0;
  3111. sprite[l].vertex[3].pos[0] = width2 / 2.0;
  3112. sprite[l].vertex[0].pos[1] = 0;
  3113. sprite[l].vertex[1].pos[1] = -height2;
  3114. sprite[l].vertex[2].pos[1] = -height2;
  3115. sprite[l].vertex[3].pos[1] = 0;
  3116. sprite[l].vertex[0].pos[2] = 0;
  3117. sprite[l].vertex[1].pos[2] = 0;
  3118. sprite[l].vertex[2].pos[2] = 0;
  3119. sprite[l].vertex[3].pos[2] = 0;
  3120. sprite[l].texel[3].st[0] = (double)(x+width) / mTexelScale;
  3121. sprite[l].texel[3].st[1] = (double)(y+height) / mTexelScale;
  3122. sprite[l].texel[2].st[0] = (double)(x+width) / mTexelScale;
  3123. sprite[l].texel[2].st[1] = (double)(y) / mTexelScale;
  3124. sprite[l].texel[1].st[0] = (double)(x) / mTexelScale;
  3125. sprite[l].texel[1].st[1] = (double)(y) / mTexelScale;
  3126. sprite[l].texel[0].st[0] = (double)(x) / mTexelScale;
  3127. sprite[l].texel[0].st[1] = (double)(y+height) / mTexelScale;
  3128. printf(".");
  3129. fflush(stdout);
  3130. }
  3131. }
  3132. }
  3133. }
  3134. printf("\n");
  3135. #endif
  3136. }
  3137. void TombRaider::getSoundSample(unsigned int index,
  3138. unsigned int *bytes, unsigned char **data)
  3139. {
  3140. unsigned char *riff;
  3141. unsigned int offset, altIndex;
  3142. *bytes = 0;
  3143. *data = 0x0;
  3144. switch (Engine())
  3145. {
  3146. case TR_VERSION_1:
  3147. //! \fixme This implies higher tmp memory cost ( copy safety )
  3148. getRiffData(bytes, &riff);
  3149. if (riff && (int)index < mNumSampleIndices)
  3150. {
  3151. offset = mSampleIndices[index];
  3152. if ((int)index < mNumSampleIndices - 1)
  3153. {
  3154. *bytes = mSampleIndices[index+1] - mSampleIndices[index];
  3155. }
  3156. else
  3157. {
  3158. *bytes = *bytes - mSampleIndices[index];
  3159. }
  3160. *data = new unsigned char[*bytes];
  3161. memcpy(*data, riff+offset, *bytes);
  3162. }
  3163. if (riff)
  3164. {
  3165. delete [] riff;
  3166. }
  3167. break;
  3168. case TR_VERSION_2:
  3169. case TR_VERSION_3:
  3170. if (mRiffAlternateLoaded &&
  3171. mRiffData && mRiffAlternateOffsets && (int)index < mNumSampleIndices)
  3172. {
  3173. altIndex = mSampleIndices[index];
  3174. offset = mRiffAlternateOffsets[altIndex];
  3175. if ((int)offset > mRiffDataSz)
  3176. {
  3177. print("getSoundSample", "WARNING: offset too large, may be mismatched SFX and game pak, handling...\n");
  3178. return;
  3179. }
  3180. if (altIndex < mNumTR4Samples - 1)
  3181. {
  3182. *bytes = mRiffAlternateOffsets[altIndex+1] - offset;
  3183. }
  3184. else
  3185. {
  3186. *bytes = mRiffDataSz - offset;
  3187. }
  3188. *data = new unsigned char[*bytes];
  3189. memcpy(*data, mRiffData+offset, *bytes);
  3190. }
  3191. break;
  3192. case TR_VERSION_4:
  3193. case TR_VERSION_5:
  3194. getRiffDataTR4(index, bytes, data);
  3195. break;
  3196. case TR_VERSION_UNKNOWN:
  3197. break;
  3198. }
  3199. }
  3200. unsigned int TombRaider::getSoundSamplesCount()
  3201. {
  3202. unsigned int count = 0;
  3203. switch (Engine())
  3204. {
  3205. case TR_VERSION_1:
  3206. count = mNumSampleIndices;
  3207. break;
  3208. case TR_VERSION_2:
  3209. case TR_VERSION_3:
  3210. if (mRiffAlternateLoaded)
  3211. {
  3212. count = mNumSampleIndices;
  3213. }
  3214. break;
  3215. case TR_VERSION_4:
  3216. case TR_VERSION_5:
  3217. count = mNumTR4Samples;
  3218. break;
  3219. case TR_VERSION_UNKNOWN:
  3220. count = 0;
  3221. break;
  3222. }
  3223. return count;
  3224. }
  3225. bool TombRaider::isMeshValid(int index)
  3226. {
  3227. return !(index < 0 ||
  3228. index > mMeshCount ||
  3229. ((mMeshes[index].num_vertices < 0 ||
  3230. mMeshes[index].vertices == NULL)));
  3231. }
  3232. bool TombRaider::isRoomValid(int index)
  3233. {
  3234. // Yes, you MUST support signed indices due to legacy engines
  3235. if (index < 0)
  3236. return false;
  3237. switch (getEngine())
  3238. {
  3239. case TR_VERSION_UNKNOWN:
  3240. break;
  3241. case TR_VERSION_5:
  3242. if (index < _num_rooms &&
  3243. //mRoomsTR5[index].roomX != 0xcdcdcd &&
  3244. //mRoomsTR5[index].roomZ != 0xcdcdcd)
  3245. *((int *)&mRoomsTR5[index].roomX) != 0xcdcdcd &&
  3246. *((int *)&mRoomsTR5[index].roomZ) != 0xcdcdcd)
  3247. // Cast to int * as it was comparing with float 0xcdcdcd before
  3248. // -- xythobuz
  3249. {
  3250. return true;
  3251. }
  3252. break;
  3253. case TR_VERSION_1:
  3254. case TR_VERSION_2:
  3255. case TR_VERSION_3:
  3256. case TR_VERSION_4:
  3257. if (index < _num_rooms &&
  3258. _rooms[index].room_data.num_vertices > 0)
  3259. {
  3260. return true;
  3261. }
  3262. }
  3263. return false;
  3264. }
  3265. ////////////////////////////////////////////////////////////
  3266. // Public Mutators
  3267. ////////////////////////////////////////////////////////////
  3268. int TombRaider::loadSFX(char *filename)
  3269. {
  3270. FILE *f = fopen(filename, "rb");
  3271. long bytes = 0;
  3272. unsigned char *data;
  3273. if (!f)
  3274. {
  3275. perror("Couldn't load SFX file");
  3276. return -1;
  3277. }
  3278. fseek(f, 0L, SEEK_END);
  3279. bytes = ftell(f);
  3280. fseek(f, 0L, SEEK_SET);
  3281. if (bytes > 8)
  3282. {
  3283. data = new unsigned char[bytes];
  3284. fread(data, bytes, 1, f);
  3285. mNumTR4Samples = getRiffOffsets(data, bytes,
  3286. &mRiffAlternateOffsets,
  3287. mNumSampleIndices);
  3288. // This SFX must not come close to matching this game pak
  3289. if ((int)mNumTR4Samples < mNumSampleIndices)
  3290. {
  3291. delete [] data;
  3292. fclose(f);
  3293. print("loadSFX", "WARNING: SFX RIFF has less than pak's RIFF count\n");
  3294. return -2;
  3295. }
  3296. // If you ran out of room, then reallocate and parse agian =(
  3297. if (mNumSampleIndices < (int)mNumTR4Samples)
  3298. {
  3299. delete [] mRiffAlternateOffsets;
  3300. mNumTR4Samples = getRiffOffsets(data, bytes,
  3301. &mRiffAlternateOffsets,
  3302. mNumTR4Samples);
  3303. }
  3304. mRiffDataSz = bytes;
  3305. mRiffData = data;
  3306. mRiffAlternateLoaded = true;
  3307. }
  3308. fclose(f);
  3309. return 0;
  3310. }
  3311. void TombRaider::reset()
  3312. {
  3313. unsigned int i;
  3314. // Mongoose 2003.05.13, Don't let some jackass reset over and over
  3315. if (mReset)
  3316. {
  3317. return;
  3318. }
  3319. //! \fixme Palettes aren't the same size anymore
  3320. //memset(_palette8, 0, 256);
  3321. //memset(_palette16, 0, 256);
  3322. if (_anim_dispatches)
  3323. delete [] _anim_dispatches;
  3324. if (_anim_commands)
  3325. delete [] _anim_commands;
  3326. if (_mesh_trees)
  3327. delete [] _mesh_trees;
  3328. if (_frames)
  3329. delete [] _frames;
  3330. if (_moveables)
  3331. delete [] _moveables;
  3332. if (_static_meshes)
  3333. delete [] _static_meshes;
  3334. if (_object_textures)
  3335. delete [] _object_textures;
  3336. if (_sprite_textures)
  3337. delete [] _sprite_textures;
  3338. if (_sprite_sequences)
  3339. delete [] _sprite_sequences;
  3340. if (_cameras)
  3341. delete [] _cameras;
  3342. if (_sound_sources)
  3343. delete [] _sound_sources;
  3344. if (_boxes)
  3345. delete [] _boxes;
  3346. if (_overlaps)
  3347. delete [] _overlaps;
  3348. if (_zones)
  3349. delete [] _zones;
  3350. if (_animated_textures)
  3351. delete [] _animated_textures;
  3352. if (_items)
  3353. delete [] _items;
  3354. if (_light_map)
  3355. delete [] _light_map;
  3356. if (_cinematic_frames)
  3357. delete [] _cinematic_frames;
  3358. if (_demo_data)
  3359. delete [] _demo_data;
  3360. if (mRiffAlternateOffsets)
  3361. delete [] mRiffAlternateOffsets;
  3362. if (mSoundMap)
  3363. delete [] mSoundMap;
  3364. if (mSoundDetails)
  3365. delete [] mSoundDetails;
  3366. if (mSampleIndices)
  3367. delete [] mSampleIndices;
  3368. if (mRiffData)
  3369. delete [] mRiffData;
  3370. if (mTR4Samples)
  3371. {
  3372. for (i = 0; i < mNumTR4Samples; ++i)
  3373. {
  3374. if (mTR4SamplesSz[i])
  3375. delete [] mTR4Samples[i];
  3376. }
  3377. delete [] mTR4Samples;
  3378. }
  3379. if (mTR4SamplesSz)
  3380. delete [] mTR4SamplesSz;
  3381. if (_rooms)
  3382. {
  3383. for (i = 0; i < _num_rooms; ++i)
  3384. {
  3385. if (_rooms[i].room_data.num_vertices > 0)
  3386. delete [] _rooms[i].room_data.vertices;
  3387. if (_rooms[i].room_data.num_rectangles > 0)
  3388. delete [] _rooms[i].room_data.rectangles;
  3389. if (_rooms[i].room_data.num_triangles > 0)
  3390. delete [] _rooms[i].room_data.triangles;
  3391. if (_rooms[i].room_data.num_sprites > 0)
  3392. delete [] _rooms[i].room_data.sprites;
  3393. if (_rooms[i].num_portals > 0)
  3394. delete []_rooms[i].portals;
  3395. if (_rooms[i].sector_list)
  3396. delete [] _rooms[i].sector_list;
  3397. if (_rooms[i].lights)
  3398. delete [] _rooms[i].lights;
  3399. if (_rooms[i].tr4Lights)
  3400. delete [] _rooms[i].tr4Lights;
  3401. if (_rooms[i].static_meshes)
  3402. delete [] _rooms[i].static_meshes;
  3403. }
  3404. delete [] _rooms;
  3405. }
  3406. if (_floor_data)
  3407. {
  3408. delete [] _floor_data;
  3409. }
  3410. if (mMeshes)
  3411. {
  3412. for (i = 0; (int)i < mMeshCount; ++i)
  3413. {
  3414. if (mMeshes[i].vertices)
  3415. delete [] mMeshes[i].vertices;
  3416. if (mMeshes[i].mesh_lights)
  3417. delete [] mMeshes[i].mesh_lights;
  3418. if (mMeshes[i].normals)
  3419. delete [] mMeshes[i].normals;
  3420. if (mMeshes[i].textured_rectangles)
  3421. delete [] mMeshes[i].textured_rectangles;
  3422. if (mMeshes[i].textured_triangles)
  3423. delete [] mMeshes[i].textured_triangles;
  3424. if (mMeshes[i].coloured_rectangles)
  3425. delete [] mMeshes[i].coloured_rectangles;
  3426. if (mMeshes[i].coloured_triangles)
  3427. delete [] mMeshes[i].coloured_triangles;
  3428. }
  3429. delete [] mMeshes;
  3430. }
  3431. if (_animations)
  3432. delete [] _animations;
  3433. if (_state_changes)
  3434. delete _state_changes;
  3435. //delete [] _state_changes;
  3436. numMoveablesTR5 = 0;
  3437. if (moveablesTR5)
  3438. delete [] moveablesTR5;
  3439. numAnimationsTR5 = 0;
  3440. if (animationsTR5)
  3441. delete [] animationsTR5;
  3442. numObjectTexturesTR5 = 0;
  3443. if (objectTexturesTR5)
  3444. delete [] objectTexturesTR5;
  3445. numCinematicFramesTR5 = 0;
  3446. if (cinematicFramesTR5)
  3447. delete [] cinematicFramesTR5;
  3448. numFlyByCamerasTR5 = 0;
  3449. if (flyByCamerasTR5)
  3450. delete [] flyByCamerasTR5;
  3451. // Texture use
  3452. if (_tex_special)
  3453. delete [] _tex_special;
  3454. if (_textile8)
  3455. delete [] _textile8;
  3456. if (_textile16)
  3457. delete [] _textile16;
  3458. if (_textile32)
  3459. delete [] _textile32;
  3460. //! \fixme Damaged memory causes delete to not set pointer to NULL?
  3461. _tex_special = NULL;
  3462. _textile8 = NULL;
  3463. _textile16 = NULL;
  3464. _textile32 = NULL;
  3465. // Compressed level use
  3466. if (mCompressedLevelData)
  3467. delete [] mCompressedLevelData;
  3468. mCompressedLevelDataOffset = 0;
  3469. mCompressedLevelSize = 0;
  3470. mFreadMode = TR_FREAD_NORMAL;
  3471. // Clear out vars
  3472. mNumTR4Samples = 0;
  3473. mPakVersion = 0;
  3474. mEngineVersion = TR_VERSION_UNKNOWN;
  3475. mNumSampleIndices = 0;
  3476. mNumSoundDetails = 0;
  3477. mRiffAlternateLoaded = false;
  3478. _num_floor_data = 0;
  3479. _num_textiles = 0;
  3480. _num_tex_special = 0;
  3481. _num_room_textures = 0;
  3482. _num_misc_textures = 0;
  3483. _num_bump_map_textures = 0;
  3484. _unknown_t = 0;
  3485. _num_rooms = 0;
  3486. _num_anim_dispatches = 0;
  3487. mMeshCount = 0;
  3488. _num_state_changes = 0;
  3489. _num_animations = 0;
  3490. _num_anim_commands = 0;
  3491. _num_mesh_trees = 0;
  3492. _num_frames = 0;
  3493. _num_moveables = 0;
  3494. _num_demo_data = 0;
  3495. _num_cinematic_frames = 0;
  3496. _num_items = 0;
  3497. _num_animated_textures = 0;
  3498. _num_cameras = 0;
  3499. _num_sound_sources = 0;
  3500. _num_boxes = 0;
  3501. _num_static_meshes = 0;
  3502. _num_object_textures = 0;
  3503. _num_sprite_textures = 0;
  3504. _num_sprite_sequences = 0;
  3505. _num_overlaps = 0;
  3506. mReset = true;
  3507. }
  3508. void TombRaider::setDebug(bool toggle)
  3509. {
  3510. mDebug = toggle;
  3511. }
  3512. void TombRaider::setRoomVertexLightingFactor(float f)
  3513. {
  3514. mRoomVertexLightingFactor = f;
  3515. }
  3516. void TombRaider::setTexelScalingFactor(float f)
  3517. {
  3518. mTexelScale = f;
  3519. }
  3520. ////////////////////////////////////////////////////////////
  3521. // Private Accessors
  3522. ////////////////////////////////////////////////////////////
  3523. void TombRaider::extractMeshes(unsigned char *mesh_data,
  3524. unsigned int num_mesh_pointers,
  3525. unsigned int *mesh_pointers)
  3526. {
  3527. unsigned int size, i;
  3528. unsigned char *mesh_pointer;
  3529. int negative_size;
  3530. /* Alloc space for mesh */
  3531. mMeshCount = num_mesh_pointers;
  3532. printDebug("ExtractMeshes", "mMeshCount = %u", mMeshCount);
  3533. mMeshes = new tr2_mesh_t[mMeshCount];
  3534. printDebug("ExtractMeshes", "num_mesh_pointers = %u", num_mesh_pointers);
  3535. for (i = 0; i < num_mesh_pointers; ++i)
  3536. {
  3537. /* Get mesh start */
  3538. mesh_pointer = &mesh_data[mesh_pointers[i]];
  3539. /* Get Centre + Unknowns */
  3540. memcpy(&mMeshes[i].centre.x, mesh_pointer, 10);
  3541. //! \fixme endian
  3542. // depending on the interpretation of the unknowns that follow the Centre
  3543. // element, more endian conversion may be necessary
  3544. mesh_pointer += 10;
  3545. /* Get number of vertices */
  3546. memcpy(&mMeshes[i].num_vertices, mesh_pointer, 2);
  3547. //! \fixme endian
  3548. printDebug("ExtractMeshes", "mMeshes[%i].num_vertices = %u",
  3549. i, mMeshes[i].num_vertices);
  3550. mesh_pointer += sizeof(unsigned short);
  3551. mMeshes[i].num_vertices = (short)abs(mMeshes[i].num_vertices);
  3552. /* Get vertex list */
  3553. size = sizeof(tr2_vertex_t) * mMeshes[i].num_vertices;
  3554. mMeshes[i].vertices = 0x0;
  3555. if (mMeshes[i].num_vertices > 0)
  3556. mMeshes[i].vertices = new tr2_vertex_t[mMeshes[i].num_vertices];
  3557. memcpy(mMeshes[i].vertices, mesh_pointer, size);
  3558. //! \fixme endian
  3559. mesh_pointer += size;
  3560. /* Get number of normals */
  3561. memcpy(&mMeshes[i].num_normals, mesh_pointer, sizeof(unsigned short));
  3562. //! \fixme endian
  3563. mesh_pointer += sizeof(unsigned short);
  3564. negative_size = (mMeshes[i].num_normals < 0);
  3565. mMeshes[i].num_normals = (short)abs(mMeshes[i].num_normals);
  3566. printDebug("ExtractMeshes", "negative_size = %u", negative_size);
  3567. mMeshes[i].mesh_lights = 0x0;
  3568. mMeshes[i].normals = 0x0;
  3569. /* Get normal list */
  3570. if (negative_size)
  3571. {
  3572. negative_size = 0;
  3573. size = mMeshes[i].num_normals * sizeof(unsigned short);
  3574. mMeshes[i].mesh_lights = 0x0;
  3575. if (mMeshes[i].num_normals > 0)
  3576. mMeshes[i].mesh_lights = new short[mMeshes[i].num_normals];
  3577. memcpy(mMeshes[i].mesh_lights, mesh_pointer, size);
  3578. }
  3579. else
  3580. {
  3581. size = sizeof(tr2_vertex_t) * mMeshes[i].num_normals;
  3582. mMeshes[i].normals = 0x0;
  3583. if (mMeshes[i].num_normals > 0)
  3584. mMeshes[i].normals = new tr2_vertex_t[mMeshes[i].num_normals];
  3585. memcpy(mMeshes[i].normals, mesh_pointer, size);
  3586. }
  3587. //! \fixme endian
  3588. mesh_pointer += size;
  3589. /* Get number of textured rectangles */
  3590. memcpy(&mMeshes[i].num_textured_rectangles,
  3591. mesh_pointer, sizeof(unsigned short));
  3592. //! \fixme endian
  3593. mesh_pointer += sizeof(unsigned short);
  3594. mMeshes[i].num_textured_rectangles =
  3595. (short)abs(mMeshes[i].num_textured_rectangles);
  3596. size = sizeof(tr2_quad_t) * mMeshes[i].num_textured_rectangles;
  3597. mMeshes[i].textured_rectangles = 0x0;
  3598. if (mMeshes[i].num_textured_rectangles > 0)
  3599. mMeshes[i].textured_rectangles =
  3600. new tr2_quad_t[mMeshes[i].num_textured_rectangles];
  3601. printDebug("ExtractMeshes", "mMeshes[%i].num_textured_rectangles = %u",
  3602. i, mMeshes[i].num_textured_rectangles);
  3603. /* Get list of textured rectangles */
  3604. if (mMeshes[i].num_textured_rectangles > 0)
  3605. {
  3606. if (mEngineVersion == TR_VERSION_4)
  3607. {
  3608. int j;
  3609. for (j = 0; j < mMeshes[i].num_textured_rectangles; ++j)
  3610. {
  3611. memcpy(&mMeshes[i].textured_rectangles[j],
  3612. mesh_pointer, sizeof(tr2_quad_t));
  3613. mesh_pointer += sizeof(tr2_quad_t) + sizeof(unsigned short);
  3614. }
  3615. }
  3616. else
  3617. {
  3618. memcpy(mMeshes[i].textured_rectangles, mesh_pointer, size);
  3619. }
  3620. //! \fixme endian
  3621. if (mEngineVersion != TR_VERSION_4)
  3622. mesh_pointer += size;
  3623. }
  3624. /* Get number of textured triangles */
  3625. memcpy(&mMeshes[i].num_textured_triangles,
  3626. mesh_pointer, sizeof(unsigned short));
  3627. //! \fixme endian
  3628. mesh_pointer += sizeof(unsigned short);
  3629. mMeshes[i].num_textured_triangles =
  3630. (short)abs(mMeshes[i].num_textured_triangles);
  3631. size = sizeof(tr2_tri_t) * mMeshes[i].num_textured_triangles;
  3632. //if (mEngineVersion == TR_VERSION_4)
  3633. // size += 2 * mMeshes[i].num_textured_triangles;
  3634. mMeshes[i].textured_triangles = 0x0;
  3635. if (mMeshes[i].num_textured_triangles > 0)
  3636. {
  3637. mMeshes[i].textured_triangles =
  3638. new tr2_tri_t[mMeshes[i].num_textured_triangles];
  3639. }
  3640. printDebug("ExtractMeshes", " mMeshes[%i].num_textured_triangles = %u",
  3641. i, mMeshes[i].num_textured_triangles);
  3642. /* Get list of textured triangles */
  3643. if (mMeshes[i].num_textured_triangles > 0)
  3644. {
  3645. if (mEngineVersion == TR_VERSION_4)
  3646. {
  3647. int j;
  3648. for (j = 0; j < mMeshes[i].num_textured_triangles; ++j)
  3649. {
  3650. memcpy(&mMeshes[i].textured_triangles[j],
  3651. mesh_pointer, sizeof(tr2_tri_t));
  3652. mesh_pointer += sizeof(tr2_tri_t) + sizeof(unsigned short);
  3653. }
  3654. }
  3655. else
  3656. {
  3657. memcpy(mMeshes[i].textured_triangles, mesh_pointer, size);
  3658. }
  3659. //! \fixme endian
  3660. if (mEngineVersion != TR_VERSION_4)
  3661. mesh_pointer += size;
  3662. }
  3663. if (mEngineVersion == TR_VERSION_4)
  3664. {
  3665. mMeshes[i].num_coloured_rectangles = 0;
  3666. mMeshes[i].num_coloured_triangles = 0;
  3667. mMeshes[i].coloured_rectangles = 0x0;
  3668. mMeshes[i].coloured_triangles = 0x0;
  3669. // Mongoose 2002.04.04, FIXME is this right?
  3670. mesh_pointer += 2;
  3671. continue;
  3672. }
  3673. /* Get number of coloured rectangles */
  3674. memcpy(&mMeshes[i].num_coloured_rectangles, mesh_pointer,
  3675. sizeof(unsigned short));
  3676. //! \fixme endian
  3677. mesh_pointer += sizeof(unsigned short);
  3678. mMeshes[i].num_coloured_rectangles =
  3679. (short)abs(mMeshes[i].num_coloured_rectangles);
  3680. mMeshes[i].coloured_rectangles = 0x0;
  3681. size = sizeof(tr2_quad_t) * mMeshes[i].num_coloured_rectangles;
  3682. if (mMeshes[i].num_coloured_rectangles > 0)
  3683. mMeshes[i].coloured_rectangles =
  3684. new tr2_quad_t[mMeshes[i].num_coloured_rectangles];
  3685. printDebug("ExtractMeshes", "mMeshes[%i].num_coloured_rectangles = %u",
  3686. i, mMeshes[i].num_coloured_rectangles);
  3687. /* Get list of coloured rectangles */
  3688. if (mMeshes[i].num_coloured_rectangles > 0)
  3689. {
  3690. memcpy(mMeshes[i].coloured_rectangles, mesh_pointer, size);
  3691. //! \fixme endian
  3692. mesh_pointer += size;
  3693. }
  3694. /* Get number of coloured triangles */
  3695. memcpy(&mMeshes[i].num_coloured_triangles, mesh_pointer,
  3696. sizeof(unsigned short));
  3697. //! \fixme endian
  3698. mesh_pointer += sizeof(unsigned short);
  3699. mMeshes[i].num_coloured_triangles =
  3700. (short)abs(mMeshes[i].num_coloured_triangles);
  3701. size = sizeof(tr2_tri_t) * mMeshes[i].num_coloured_triangles;
  3702. mMeshes[i].coloured_triangles = 0x0;
  3703. if (mMeshes[i].num_coloured_triangles > 0)
  3704. mMeshes[i].coloured_triangles =
  3705. new tr2_tri_t[mMeshes[i].num_coloured_triangles];
  3706. printDebug("ExtractMeshes", "mMeshes[%i].num_coloured_triangles = %u",
  3707. i, mMeshes[i].num_coloured_triangles);
  3708. /* Get list of coloured triangles */
  3709. if (mMeshes[i].num_coloured_triangles > 0)
  3710. {
  3711. memcpy(mMeshes[i].coloured_triangles, mesh_pointer, size);
  3712. //! \fixme endian
  3713. mesh_pointer += size;
  3714. }
  3715. }
  3716. }
  3717. int TombRaider::Fread(void *buffer, size_t size, size_t count, FILE *f)
  3718. {
  3719. int num_read;
  3720. if (mFreadMode == TR_FREAD_COMPRESSED)
  3721. {
  3722. num_read = count;
  3723. num_read *= size;
  3724. if ((mCompressedLevelDataOffset + num_read) <= mCompressedLevelSize)
  3725. {
  3726. memcpy(buffer, &mCompressedLevelData[mCompressedLevelDataOffset],
  3727. num_read);
  3728. mCompressedLevelDataOffset += num_read;
  3729. return count;
  3730. }
  3731. else
  3732. {
  3733. print("Fread", "(%p, %lu, %lu, %p) ERROR: Returned %d bytes too far",
  3734. buffer, size, count, f, num_read);
  3735. reset();
  3736. exit(2);
  3737. }
  3738. }
  3739. unsigned int offset = ftell(f);
  3740. if (fread(buffer, size, count, f) != count)
  3741. {
  3742. print("Fread", "ERROR: Failed fread. Should Abort. @ 0x%x", offset);
  3743. reset();
  3744. exit(2);
  3745. // return -1; // Unreachable anyways
  3746. }
  3747. return count;
  3748. }
  3749. void TombRaider::getRiffData(unsigned int *bytes, unsigned char **data)
  3750. {
  3751. *bytes = 0;
  3752. *data = 0x0;
  3753. if (mRiffDataSz)
  3754. {
  3755. *bytes = mRiffDataSz;
  3756. *data = new unsigned char[mRiffDataSz];
  3757. memcpy(*data, mRiffData, mRiffDataSz);
  3758. }
  3759. }
  3760. void TombRaider::getRiffDataTR4(unsigned int index,
  3761. unsigned int *bytes, unsigned char **data)
  3762. {
  3763. *bytes = 0;
  3764. *data = 0x0;
  3765. if (index < mNumTR4Samples)
  3766. {
  3767. *bytes = mTR4SamplesSz[index];
  3768. *data = new unsigned char[*bytes];
  3769. memcpy(*data, mTR4Samples[index], *bytes);
  3770. }
  3771. }
  3772. int TombRaider::getRiffOffsets(unsigned char *riffData,
  3773. unsigned int riffDataBytes,
  3774. unsigned int **offsets,
  3775. unsigned int numOffsets)
  3776. {
  3777. unsigned int i, j = 0, riffCount, state;
  3778. *offsets = new unsigned int[numOffsets];
  3779. for (i = 0, riffCount = 0, state = 4; i < riffDataBytes; ++i)
  3780. {
  3781. switch (riffData[i])
  3782. {
  3783. case 'R':
  3784. if (state == 4)
  3785. {
  3786. j = i; // tmp offset guess
  3787. state = 0;
  3788. continue;
  3789. }
  3790. break;
  3791. case 'I':
  3792. if (state == 0)
  3793. {
  3794. state = 1;
  3795. continue;
  3796. }
  3797. break;
  3798. case 'F':
  3799. if (state == 1)
  3800. {
  3801. state = 2;
  3802. continue;
  3803. }
  3804. else if (state == 2)
  3805. {
  3806. state = 3;
  3807. // Found RIFF header, but we can only report
  3808. // riffCount offsets ( buffer size limits )
  3809. if (riffCount < numOffsets)
  3810. {
  3811. (*offsets)[riffCount] = j;
  3812. }
  3813. ++riffCount;
  3814. continue;
  3815. }
  3816. break;
  3817. }
  3818. state = 4;
  3819. }
  3820. return riffCount;
  3821. }
  3822. unsigned char *TombRaider::getTexTile(int texture)
  3823. {
  3824. unsigned char *image;
  3825. unsigned int color;
  3826. int j, k, index, offset;
  3827. int xmin, xmax, ymin, ymax, x, y;
  3828. image = NULL;
  3829. if (texture >=0 && texture < (int)_num_textiles)
  3830. {
  3831. image = new unsigned char[256*256*4];
  3832. memset(image, 0, 256*256*4);
  3833. if (_textile32)
  3834. {
  3835. // Convert 32bit BGRA image format to 32bit RGBA
  3836. for (j = 0; j < 256; j++)
  3837. {
  3838. for (k = 0; k < 256; k++)
  3839. {
  3840. index = (j * 256) + k;
  3841. color = _textile32[texture].tile[index];
  3842. index = (j * 1024) + (k * 4);
  3843. image[index + 2] = *((unsigned char *)(&color));
  3844. image[index + 1] = *((unsigned char *)(&color)+1);
  3845. image[index + 0] = *((unsigned char *)(&color)+2);
  3846. image[index + 3] = *((unsigned char *)(&color)+3);
  3847. }
  3848. }
  3849. }
  3850. else
  3851. {
  3852. // Convert 16bit ARGB image format to 32bit RGBA
  3853. for (j = 0; j < 256; j++)
  3854. {
  3855. for (k = 0; k < 256; k++)
  3856. {
  3857. index = (j * 256) + k;
  3858. offset = _textile16[texture].tile[index];
  3859. index = (j * 1024) + (k * 4);
  3860. image[index + 0] = ((offset >> 10) & 0x1f) * 8;
  3861. image[index + 1] = ((offset >> 5) & 0x1f) * 8;
  3862. image[index + 2] = (offset & 0x1f) * 8;
  3863. image[index + 3] = (offset & 0x8000) ? 0xFF : 0;
  3864. }
  3865. }
  3866. }
  3867. switch (Engine())
  3868. {
  3869. case TR_VERSION_4:
  3870. case TR_VERSION_3: // Account for alpha flags
  3871. for (j = 0; j < (int)_num_object_textures; j++)
  3872. {
  3873. //! \fixme This kind of works for lighting - but messes up lara
  3874. #ifdef FIXME
  3875. if (_object_textures[j].tile == texture &&
  3876. _object_textures[j].transparency_flags == 1)
  3877. {
  3878. xmin = 999;
  3879. xmax = 0;
  3880. ymin = 999;
  3881. ymax = 0;
  3882. y = 4;
  3883. // Account for triangles
  3884. if (_object_textures[j].vertices[3].xpixel == 0 &&
  3885. _object_textures[j].vertices[3].ypixel == 0)
  3886. y = 3;
  3887. for (k = 0; k < y; k++)
  3888. {
  3889. if (_object_textures[j].vertices[k].xpixel > xmax)
  3890. xmax = _object_textures[j].vertices[k].xpixel;
  3891. if (_object_textures[j].vertices[k].xpixel < xmin)
  3892. xmin = _object_textures[j].vertices[k].xpixel;
  3893. if (_object_textures[j].vertices[k].ypixel > ymax)
  3894. ymax = _object_textures[j].vertices[k].ypixel;
  3895. if (_object_textures[j].vertices[k].ypixel < ymin)
  3896. ymin = _object_textures[j].vertices[k].ypixel;
  3897. }
  3898. for (x = xmin; x <= xmax; x++)
  3899. {
  3900. for (y = ymin; y <= ymax; y++)
  3901. {
  3902. index = (y * 256) + x;
  3903. offset = _textile16[texture].tile[index];
  3904. index = (y * 1024) + (x * 4);
  3905. image[index + 0] = ((offset >> 10) & 0x1f) * 8;
  3906. image[index + 1] = ((offset >> 5) & 0x1f) * 8;
  3907. image[index + 2] = (offset & 0x1f) * 8;
  3908. // Set transparency to full
  3909. if (offset & 0x8000)
  3910. {
  3911. image[index + 3] = 0x00;
  3912. }
  3913. }
  3914. }
  3915. }
  3916. else
  3917. #endif
  3918. if (_object_textures[j].tile == texture &&
  3919. _object_textures[j].transparency_flags == 2)
  3920. {
  3921. xmin = 999;
  3922. xmax = 0;
  3923. ymin = 999;
  3924. ymax = 0;
  3925. y = 4;
  3926. // Account for triangles
  3927. if (_object_textures[j].vertices[3].xpixel == 0 &&
  3928. _object_textures[j].vertices[3].ypixel == 0)
  3929. y = 3;
  3930. for (k = 0; k < y; k++)
  3931. {
  3932. if (_object_textures[j].vertices[k].xpixel > xmax)
  3933. xmax = _object_textures[j].vertices[k].xpixel;
  3934. if (_object_textures[j].vertices[k].xpixel < xmin)
  3935. xmin = _object_textures[j].vertices[k].xpixel;
  3936. if (_object_textures[j].vertices[k].ypixel > ymax)
  3937. ymax = _object_textures[j].vertices[k].ypixel;
  3938. if (_object_textures[j].vertices[k].ypixel < ymin)
  3939. ymin = _object_textures[j].vertices[k].ypixel;
  3940. }
  3941. for (x = xmin; x <= xmax; x++)
  3942. {
  3943. for (y = ymin; y <= ymax; y++)
  3944. {
  3945. if (_textile32)
  3946. {
  3947. index = (y * 256) + x;
  3948. color = _textile32[texture].tile[index];
  3949. index = (y * 1024) + (x * 4);
  3950. image[index + 2] = *((unsigned char *)(&color));
  3951. image[index + 1] = *((unsigned char *)(&color)+1);
  3952. image[index + 0] = *((unsigned char *)(&color)+2);
  3953. image[index + 3] = *((unsigned char *)(&color)+3);
  3954. k = image[index] + image[index + 1] + image[index + 2];
  3955. // Set transparency based upon intensity
  3956. image[index + 3] = (unsigned char)(k / 3);
  3957. }
  3958. else
  3959. {
  3960. index = (y * 256) + x;
  3961. offset = _textile16[texture].tile[index];
  3962. index = (y * 1024) + (x * 4);
  3963. image[index + 0] = ((offset >> 10) & 0x1f) * 8;
  3964. image[index + 1] = ((offset >> 5) & 0x1f) * 8;
  3965. image[index + 2] = (offset & 0x1f) * 8;
  3966. image[index + 3] = (offset & 0x8000) ? 0xFF : 0;
  3967. k = image[index] + image[index + 1] + image[index + 2];
  3968. // Set transparency based upon intensity
  3969. if (offset & 0x8000)
  3970. image[index + 3] = (unsigned char)(k / 3);
  3971. else
  3972. image[index + 3] = 0;
  3973. }
  3974. }
  3975. }
  3976. }
  3977. }
  3978. break;
  3979. case TR_VERSION_1:
  3980. case TR_VERSION_2:
  3981. case TR_VERSION_5:
  3982. case TR_VERSION_UNKNOWN:
  3983. break;
  3984. }
  3985. }
  3986. return image;
  3987. }
  3988. //! \fixme Move these data about to make full use in the class ;)
  3989. int TombRaider::loadTR5(FILE *f, void (*percent)(int))
  3990. {
  3991. unsigned int level_data_sz, riffOffset, seperator0;
  3992. unsigned int portalOffset, nextRoomOffset, thisRoomOffset;
  3993. int i, j, k;
  3994. u_int16_t us;
  3995. u_int32_t numMeshData, numMeshPointers, u;
  3996. u_int32_t *meshPointers;
  3997. u_int8_t *meshData;
  3998. char check[32];
  3999. if (percent)
  4000. (*percent)(5);
  4001. printDebug("Load", "mEngineVersion = 0x%x", mPakVersion);
  4002. if (mEngineVersion != TR_VERSION_5)
  4003. return -1;
  4004. unsigned int sz, usz; // compressed and uncompressed size
  4005. unsigned char *compressed_data = NULL;
  4006. int zerr;
  4007. uLongf foo;
  4008. // Read texture type offsets
  4009. Fread(&_num_room_textures, 2, 1, f);
  4010. printDebug("LoadTR5", "_num_room_textures = %u", _num_room_textures);
  4011. Fread(&_num_misc_textures, 2, 1, f);
  4012. printDebug("LoadTR5", "_num_misc_textures = %u", _num_misc_textures);
  4013. Fread(&_num_bump_map_textures, 2, 1, f);
  4014. printDebug("LoadTR5", "_num_bump_map_textures = %u", _num_bump_map_textures);
  4015. // Read the sizes of the 32-bit textures
  4016. Fread(&usz, sizeof(usz), 1, f);
  4017. Fread(&sz, sizeof(sz), 1, f);
  4018. printDebug("LoadTR5", "32-bit textures compressed size = %u bytes", sz);
  4019. printDebug("LoadTR5", "32-bit textures uncompressed size = %u bytes", usz);
  4020. _num_textiles = usz / sizeof(tr2_textile32_t);
  4021. printDebug("LoadTR5", "_num_textiles = %i/%lu = %i",
  4022. usz, sizeof(tr2_textile32_t), _num_textiles);
  4023. _textile32 = new tr2_textile32_t[_num_textiles];
  4024. // Allocate a temporary buffer for decompression
  4025. compressed_data = new unsigned char[sz];
  4026. Fread(compressed_data, sz, 1, f);
  4027. // Decompress the textures
  4028. foo = usz;
  4029. zerr = uncompress((unsigned char *)_textile32,
  4030. &foo,
  4031. compressed_data,
  4032. sz);
  4033. usz = foo;
  4034. printDebug("LoadTR5", "textile decompress [%s]",
  4035. (zerr == Z_OK) ? "OK" : "ERROR");
  4036. switch (zerr)
  4037. {
  4038. case Z_MEM_ERROR:
  4039. printDebug("LoadTR5", "There was not enough memory");
  4040. break;
  4041. case Z_BUF_ERROR:
  4042. printDebug("LoadTR5", "There was not enough room in the output buffer");
  4043. break;
  4044. case Z_DATA_ERROR:
  4045. printDebug("LoadTR5", "The input data was corrupted");
  4046. break;
  4047. default:
  4048. printDebug("LoadTR5", "textile decompress %i", zerr);
  4049. }
  4050. // Free the temporary buffer
  4051. delete [] compressed_data;
  4052. // Read in the 16-bit textures, set NumTextiles
  4053. Fread(&usz, sizeof(usz), 1, f);
  4054. Fread(&sz, sizeof(sz), 1, f);
  4055. printDebug("LoadTR5", "16-bit textures compressed size = %u bytes", sz);
  4056. printDebug("LoadTR5", "16-bit textures uncompressed size = %u bytes", usz);
  4057. _num_textiles = usz / sizeof(tr2_textile16_t);
  4058. printDebug("LoadTR5", "_num_textiles = %i/%lu = %i",
  4059. usz, sizeof(tr2_textile16_t), _num_textiles);
  4060. _textile16 = new tr2_textile16_t[_num_textiles];
  4061. // Allocate a temporary buffer for decompression
  4062. compressed_data = new unsigned char[sz];
  4063. Fread(compressed_data, sz, 1, f);
  4064. // Decompress the textures
  4065. foo = usz;
  4066. zerr = uncompress((unsigned char *)_textile16,
  4067. &foo,
  4068. compressed_data,
  4069. sz);
  4070. usz = foo;
  4071. if (percent)
  4072. (*percent)(7);
  4073. printDebug("LoadTR5", "textile decompress [%s]",
  4074. (zerr == Z_OK) ? "OK" : "ERROR");
  4075. switch (zerr)
  4076. {
  4077. case Z_MEM_ERROR:
  4078. printDebug("LoadTR5", "There was not enough memory");
  4079. break;
  4080. case Z_BUF_ERROR:
  4081. printDebug("LoadTR5", "There was not enough room in the output buffer");
  4082. break;
  4083. case Z_DATA_ERROR:
  4084. printDebug("LoadTR5", "The input data was corrupted");
  4085. break;
  4086. default:
  4087. printDebug("LoadTR5", "textile decompress %i", zerr);
  4088. }
  4089. // Free the temporary buffer
  4090. delete [] compressed_data;
  4091. // Read the sizes of the sprite textures
  4092. Fread(&usz, sizeof(usz), 1, f);
  4093. Fread(&sz, sizeof(sz), 1, f);
  4094. printDebug("LoadTR5", "sprite textures compressed size = %u bytes", sz);
  4095. printDebug("LoadTR5", "sprite textures uncompressed size = %u bytes", usz);
  4096. // Load sprite/bump map/gui/etc textures also
  4097. _num_tex_special = usz/(256*256*4);
  4098. printDebug("LoadTR5", "_num_tex_special = %i/%i = %i",
  4099. usz, 256*256*4, _num_tex_special);
  4100. printDebug("LoadTR5", "Reading %ibytes of sprite textures", usz);
  4101. if (usz)
  4102. {
  4103. _tex_special = new unsigned char[usz];
  4104. // Allocate a temporary buffer for decompression
  4105. compressed_data = new unsigned char[sz];
  4106. Fread(compressed_data, sz, 1, f);
  4107. // Decompress the textures
  4108. foo = usz;
  4109. zerr = uncompress(_tex_special,
  4110. &foo,
  4111. compressed_data,
  4112. sz);
  4113. usz = foo;
  4114. printDebug("LoadTR5", "special texture decompress [%s]",
  4115. (zerr == Z_OK) ? "OK" : "ERROR");
  4116. switch (zerr)
  4117. {
  4118. case Z_MEM_ERROR:
  4119. printDebug("LoadTR5", "There was not enough memory");
  4120. break;
  4121. case Z_BUF_ERROR:
  4122. printDebug("LoadTR5", "There was not enough room in the output buffer");
  4123. break;
  4124. case Z_DATA_ERROR:
  4125. printDebug("LoadTR5", "The input data was corrupted");
  4126. break;
  4127. default:
  4128. printDebug("LoadTR5", "textile decompress %i", zerr);
  4129. }
  4130. // Free the temporary buffer
  4131. delete [] compressed_data;
  4132. }
  4133. if (percent)
  4134. (*percent)(10);
  4135. // Mongoose 2002.01.08, Michiel has discovered the
  4136. // first 4 bytes here are 2 bitu16 flags for Lara type and weather
  4137. u_int16_t laraType, weather;
  4138. Fread(&laraType, 2, 1, f);
  4139. printDebug("LoadTR5", "laraType = 0x%x", laraType);
  4140. Fread(&weather, 2, 1, f);
  4141. printDebug("LoadTR5", "weather = 0x%x", weather);
  4142. printDebug("LoadTR5", "skipping 28bytes of unknowns");
  4143. Fread(&seperator0, 4, 1, f);
  4144. printDebug("LoadTR5", "0x%x", seperator0);
  4145. Fread(&seperator0, 4, 1, f);
  4146. printDebug("LoadTR5", "0x%x", seperator0);
  4147. Fread(&seperator0, 4, 1, f);
  4148. printDebug("LoadTR5", "0x%x", seperator0);
  4149. Fread(&seperator0, 4, 1, f);
  4150. printDebug("LoadTR5", "0x%x", seperator0);
  4151. Fread(&seperator0, 4, 1, f);
  4152. printDebug("LoadTR5", "0x%x", seperator0);
  4153. Fread(&seperator0, 4, 1, f);
  4154. printDebug("LoadTR5", "0x%x", seperator0);
  4155. Fread(&seperator0, 4, 1, f);
  4156. printDebug("LoadTR5", "0x%x", seperator0);
  4157. Fread(&level_data_sz, 4, 1, f);
  4158. printDebug("LoadTR5", "Level data size = %u", level_data_sz);
  4159. Fread(&riffOffset, 4, 1, f);
  4160. printDebug("LoadTR5", "Same as last, also offset to RIFFs = %u",
  4161. riffOffset);
  4162. Fread(&seperator0, 4, 1, f);
  4163. printDebug("LoadTR5", "Seperator, always 0x00? = %u", seperator0);
  4164. Fread(&_num_rooms, 4, 1, f);
  4165. printDebug("LoadTR5", "_num_rooms = %u", _num_rooms);
  4166. mRoomsTR5 = new tr5_room_t[_num_rooms];
  4167. for (i = 0; i < _num_rooms; ++i)
  4168. {
  4169. if (percent)
  4170. (*percent)(11 + (int)(((float)i/(float)_num_rooms)*90.0));
  4171. thisRoomOffset = ftell(f);
  4172. Fread(&mRoomsTR5[i].checkXELA, 4, 1, f);
  4173. printDebug("LoadTR5", "room[%i].checkXELA (0x414c4558)? = 0x%x",
  4174. i, mRoomsTR5[i].checkXELA);
  4175. if (mRoomsTR5[i].checkXELA != 0x414c4558)
  4176. {
  4177. print("LoadTR5", "Error #1 room[%i].checkXELA (0x414c4558) != 0x%x",
  4178. i, mRoomsTR5[i].checkXELA);
  4179. return -3;
  4180. }
  4181. Fread(&mRoomsTR5[i].roomDataSize, 4, 1, f);
  4182. printDebug("LoadTR5", "offset to next room = %u",
  4183. mRoomsTR5[i].roomDataSize);
  4184. nextRoomOffset = ftell(f) + mRoomsTR5[i].roomDataSize;
  4185. Fread(&mRoomsTR5[i].seperator, 4, 1, f);
  4186. printDebug("LoadTR5", "room[%i].seperator, CDCDCDCD = %X?",
  4187. i, mRoomsTR5[i].seperator);
  4188. if (mRoomsTR5[i].seperator != 0xcdcdcdcd)
  4189. {
  4190. print("LoadTR5", "Error #2 room[%i].seperator, CDCDCDCD != 0x%X",
  4191. i, mRoomsTR5[i].seperator);
  4192. return -3;
  4193. }
  4194. // Start 60byte struct /////////////
  4195. printDebug("LoadTR5", "60byte struct {");
  4196. // Often start of "XELA" +216 + ublock1 = FD end,
  4197. // but have seen 0xffffffff (-1). Better using next data
  4198. // and compute FD size the old way of X*Z*8
  4199. Fread(&mRoomsTR5[i].endSDOffset, 4, 1, f);
  4200. printDebug("LoadTR5", "%u + 216 + XELA start = FD end",
  4201. mRoomsTR5[i].endSDOffset);
  4202. // Start of "XELA" + 216 + ublock2 = FD start
  4203. Fread(&mRoomsTR5[i].startSDOffset, 4, 1, f);
  4204. printDebug("LoadTR5", "%u + 216 + XELA start = FD start",
  4205. mRoomsTR5[i].startSDOffset);
  4206. Fread(&mRoomsTR5[i].seperator2, 4, 1, f);
  4207. printDebug("LoadTR5", "seperator2 = %u", mRoomsTR5[i].seperator2);
  4208. if (mRoomsTR5[i].seperator2 != 0xcdcdcdcd &&
  4209. mRoomsTR5[i].seperator2 != 0x00000000)
  4210. {
  4211. print("LoadTR5", "Error #3 CDCDCDCD | 0x0 != 0x%x",
  4212. mRoomsTR5[i].seperator2);
  4213. return -3;
  4214. }
  4215. // Possibly start of "XELA" + 216 + ublock4 = end portals
  4216. Fread(&mRoomsTR5[i].endPortalOffset, 4, 1, f);
  4217. printDebug("LoadTR5", "%u + 216 + XELA start = end portals",
  4218. mRoomsTR5[i].endPortalOffset);
  4219. Fread(&mRoomsTR5[i].x, 4, 1, f);
  4220. printDebug("LoadTR5", "room.x = %u", mRoomsTR5[i].x);
  4221. Fread(&mRoomsTR5[i].seperator3, 4, 1, f);
  4222. printDebug("LoadTR5", "0x00000000 = %u ?", mRoomsTR5[i].seperator3);
  4223. Fread(&mRoomsTR5[i].z, 4, 1, f);
  4224. printDebug("LoadTR5", "room.z = %u", mRoomsTR5[i].z);
  4225. Fread(&mRoomsTR5[i].yBottom, 4, 1, f);
  4226. printDebug("LoadTR5", "room.y_bottom = %u", mRoomsTR5[i].yBottom);
  4227. Fread(&mRoomsTR5[i].yTop, 4, 1, f);
  4228. printDebug("LoadTR5", "room.y_top = %u", mRoomsTR5[i].yTop);
  4229. Fread(&mRoomsTR5[i].numZSectors, 2, 1, f);
  4230. printDebug("LoadTR5", "num_z_sectors = %i", mRoomsTR5[i].numZSectors);
  4231. Fread(&mRoomsTR5[i].numXSectors, 2, 1, f);
  4232. printDebug("LoadTR5", "num_x_sectors = %i", mRoomsTR5[i].numXSectors);
  4233. Fread(&mRoomsTR5[i].roomAmbientColor, 4, 1, f);
  4234. printDebug("LoadTR5", "room_ambient_color = 0x%x",
  4235. mRoomsTR5[i].roomAmbientColor);
  4236. Fread(&mRoomsTR5[i].numRoomLights, 2, 1, f);
  4237. printDebug("LoadTR5", "num_lights = %i", mRoomsTR5[i].numRoomLights);
  4238. Fread(&mRoomsTR5[i].numStaticMeshes, 2, 1, f);
  4239. printDebug("LoadTR5", "num_static_meshes = %i",
  4240. mRoomsTR5[i].numStaticMeshes);
  4241. Fread(&mRoomsTR5[i].unknownR1, 2, 1, f);
  4242. printDebug("LoadTR5", "unknown 0x0001? = 0x%x", mRoomsTR5[i].unknownR1);
  4243. Fread(&mRoomsTR5[i].unknownR2, 2, 1, f);
  4244. printDebug("LoadTR5", "unknown 0x0000? = 0x%x", mRoomsTR5[i].unknownR2);
  4245. Fread(&mRoomsTR5[i].filler, 4, 1, f);
  4246. printDebug("LoadTR5", "Always 0x7fff? = 0x%x", mRoomsTR5[i].filler);
  4247. Fread(&mRoomsTR5[i].filler2, 4, 1, f);
  4248. printDebug("LoadTR5", "Always 0x7fff? = 0x%x", mRoomsTR5[i].filler2);
  4249. printDebug("LoadTR5", "}");
  4250. // End 60byte structure /////////////////
  4251. Fread(&mRoomsTR5[i].seperator4, 4, 1, f);
  4252. printDebug("LoadTR5", "seperator4 CDCDCDCD = 0x%x",
  4253. mRoomsTR5[i].seperator4);
  4254. if (mRoomsTR5[i].seperator4 != 0xcdcdcdcd)
  4255. {
  4256. print("LoadTR5", "Error #5 CDCDCDCD != 0x%x",
  4257. mRoomsTR5[i].seperator4);
  4258. return -3;
  4259. }
  4260. Fread(&mRoomsTR5[i].seperator5, 4, 1, f);
  4261. printDebug("LoadTR5", "seperator5 CDCDCDCD = 0x%x",
  4262. mRoomsTR5[i].seperator5);
  4263. if (mRoomsTR5[i].seperator5 != 0xcdcdcdcd)
  4264. {
  4265. print("LoadTR5", "Error #6 CDCDCDCD != 0x%x",
  4266. mRoomsTR5[i].seperator5);
  4267. return -3;
  4268. }
  4269. // Start 20byte structure ///////////////
  4270. printDebug("LoadTR5", "20byte struct {");
  4271. Fread(mRoomsTR5[i].seperator6, 6, 1, f);
  4272. printDebug("LoadTR5", "6 bytes 0xFF = 0x%x%x%x%x%x%x",
  4273. mRoomsTR5[i].seperator6[0], mRoomsTR5[i].seperator6[1],
  4274. mRoomsTR5[i].seperator6[2], mRoomsTR5[i].seperator6[3],
  4275. mRoomsTR5[i].seperator6[4], mRoomsTR5[i].seperator6[5]);
  4276. Fread(&mRoomsTR5[i].roomFlag, 2, 1, f);
  4277. printDebug("LoadTR5", "room_flag = %i", mRoomsTR5[i].roomFlag);
  4278. Fread(&mRoomsTR5[i].unknownR5, 2, 1, f);
  4279. printDebug("LoadTR5", "unknown = %i", mRoomsTR5[i].unknownR5);
  4280. Fread(mRoomsTR5[i].seperator7, 10, 1, f);
  4281. printDebug("LoadTR5", "10 bytes 0x00 = 0x%x%x%x%x%x%x%x%x%x%x",
  4282. mRoomsTR5[i].seperator7[0], mRoomsTR5[i].seperator7[1],
  4283. mRoomsTR5[i].seperator7[2], mRoomsTR5[i].seperator7[3],
  4284. mRoomsTR5[i].seperator7[4], mRoomsTR5[i].seperator7[5],
  4285. mRoomsTR5[i].seperator7[6], mRoomsTR5[i].seperator7[7],
  4286. mRoomsTR5[i].seperator7[8], mRoomsTR5[i].seperator7[9]);
  4287. printDebug("LoadTR5", "}");
  4288. // End 20byte structure /////////////////
  4289. Fread(&mRoomsTR5[i].seperator8, 4, 1, f);
  4290. printDebug("LoadTR5", "seperator8 CDCDCDCD = 0x%x",
  4291. mRoomsTR5[i].seperator8);
  4292. if (mRoomsTR5[i].seperator8 != 0xcdcdcdcd)
  4293. {
  4294. print("LoadTR5", "Error #9 CDCDCDCD != 0x%x",
  4295. mRoomsTR5[i].seperator8);
  4296. return -3;
  4297. }
  4298. printDebug("LoadTR5", "16byte struct {");
  4299. Fread(&mRoomsTR5[i].unknownR6, 4, 1, f);
  4300. printDebug("LoadTR5", "unknownR6 = %i", mRoomsTR5[i].unknownR6);
  4301. Fread(&mRoomsTR5[i].roomX, 4, 1, f);
  4302. printDebug("LoadTR5", "roomX = %f", mRoomsTR5[i].roomX);
  4303. Fread(&mRoomsTR5[i].seperator9, 4, 1, f);
  4304. printDebug("LoadTR5", "seperator9 CDCDCDCD | 0x0 = 0x%x",
  4305. mRoomsTR5[i].seperator9);
  4306. if (mRoomsTR5[i].seperator9 != 0xcdcdcdcd &&
  4307. mRoomsTR5[i].seperator9 != 0x0)
  4308. {
  4309. print("LoadTR5", "Error #10 CDCDCDCD | 0x0 != 0x%x",
  4310. mRoomsTR5[i].seperator9);
  4311. return -3;
  4312. }
  4313. Fread(&mRoomsTR5[i].roomZ, 4, 1, f);
  4314. printDebug("LoadTR5", "roomZ = %f", mRoomsTR5[i].roomZ);
  4315. printDebug("LoadTR5", "}");
  4316. Fread(&mRoomsTR5[i].seperator10, 4, 1, f);
  4317. printDebug("LoadTR5", "seperator10 CDCDCDCD = 0x%x",
  4318. mRoomsTR5[i].seperator10);
  4319. if (mRoomsTR5[i].seperator10 != 0xcdcdcdcd)
  4320. {
  4321. print("LoadTR5", "Error #11 CDCDCDCD != 0x%x",
  4322. mRoomsTR5[i].seperator10);
  4323. return -3;
  4324. }
  4325. Fread(&mRoomsTR5[i].seperator11, 4, 1, f);
  4326. printDebug("LoadTR5", "seperator11 CDCDCDCD = 0x%x",
  4327. mRoomsTR5[i].seperator11);
  4328. if (mRoomsTR5[i].seperator11 != 0xcdcdcdcd)
  4329. {
  4330. print("LoadTR5", "Error #12 CDCDCDCD != 0x%x",
  4331. mRoomsTR5[i].seperator11);
  4332. return -3;
  4333. }
  4334. Fread(&mRoomsTR5[i].seperator12, 4, 1, f);
  4335. printDebug("LoadTR5", "seperator12 CDCDCDCD = 0x%x",
  4336. mRoomsTR5[i].seperator12);
  4337. if (mRoomsTR5[i].seperator12 != 0xcdcdcdcd)
  4338. {
  4339. print("LoadTR5", "Error #13 CDCDCDCD != 0x%x",
  4340. mRoomsTR5[i].seperator12);
  4341. return -3;
  4342. }
  4343. Fread(&mRoomsTR5[i].seperator13, 4, 1, f);
  4344. printDebug("LoadTR5", "seperator13 CDCDCDCD = 0x%x",
  4345. mRoomsTR5[i].seperator13);
  4346. if (mRoomsTR5[i].seperator13 != 0xcdcdcdcd)
  4347. {
  4348. print("LoadTR5", "Error #14 CDCDCDCD | 0x0 != 0x%x",
  4349. mRoomsTR5[i].seperator13);
  4350. return -3;
  4351. }
  4352. Fread(&mRoomsTR5[i].seperator14, 4, 1, f);
  4353. printDebug("LoadTR5", "seperator14 CDCDCDCD = 0x%x",
  4354. mRoomsTR5[i].seperator14);
  4355. if (mRoomsTR5[i].seperator14 != 0xcdcdcdcd &&
  4356. mRoomsTR5[i].seperator14 != 0x00000000)
  4357. {
  4358. print("LoadTR5", "Error #15 CDCDCDCD | 0x0 != 0x%x",
  4359. mRoomsTR5[i].seperator14);
  4360. return -3;
  4361. }
  4362. Fread(&mRoomsTR5[i].seperator15, 4, 1, f);
  4363. printDebug("LoadTR5", "seperator15 CDCDCDCD = 0x%x",
  4364. mRoomsTR5[i].seperator15);
  4365. if (mRoomsTR5[i].seperator15 != 0xcdcdcdcd)
  4366. {
  4367. print("LoadTR5", "Error #16 CDCDCDCD != 0x%x",
  4368. mRoomsTR5[i].seperator15);
  4369. return -3;
  4370. }
  4371. // 56byte struct /////////////
  4372. printDebug("LoadTR5", "56byte struct {");
  4373. Fread(&mRoomsTR5[i].numRoomTriangles, 4, 1, f);
  4374. printDebug("LoadTR5", "num_triangles = %u",
  4375. mRoomsTR5[i].numRoomTriangles);
  4376. Fread(&mRoomsTR5[i].numRoomRectangles, 4, 1, f);
  4377. printDebug("LoadTR5", "num_rectangles = %u",
  4378. mRoomsTR5[i].numRoomRectangles);
  4379. Fread(&mRoomsTR5[i].seperator16, 4, 1, f);
  4380. printDebug("LoadTR5", "seperator16, 0x00? = 0x%x",
  4381. mRoomsTR5[i].seperator16);
  4382. // Num lights * 88bytes
  4383. Fread(&mRoomsTR5[i].lightSize, 4, 1, f);
  4384. printDebug("LoadTR5", "light_size = %u", mRoomsTR5[i].lightSize);
  4385. Fread(&mRoomsTR5[i].numTotalRoomLights, 4, 1, f);
  4386. printDebug("LoadTR5", "num_lights = %u",
  4387. mRoomsTR5[i].numTotalRoomLights);
  4388. Fread(&mRoomsTR5[i].unknownR7, 4, 1, f); // was num_unknown_36byte structs to read
  4389. printDebug("LoadTR5", "unknownR7 structs = %u", mRoomsTR5[i].unknownR7);
  4390. Fread(&mRoomsTR5[i].unknownR8, 4, 1, f);
  4391. printDebug("LoadTR5", "unknownR8 = 0x%x", mRoomsTR5[i].unknownR8);
  4392. Fread(&mRoomsTR5[i].yBottom, 4, 1, f);
  4393. printDebug("LoadTR5", "lyBottom = 0x%x", mRoomsTR5[i].lyBottom);
  4394. Fread(&mRoomsTR5[i].numLayers, 4, 1, f);
  4395. printDebug("LoadTR5", "num_layers = %u", mRoomsTR5[i].numLayers);
  4396. Fread(&mRoomsTR5[i].layerOffset, 4, 1, f);
  4397. printDebug("LoadTR5", "layerOffset = 0x%x", mRoomsTR5[i].layerOffset);
  4398. Fread(&mRoomsTR5[i].verticesOffset, 4, 1, f);
  4399. printDebug("LoadTR5", "verticesOffset = 0x%x", mRoomsTR5[i].verticesOffset);
  4400. Fread(&mRoomsTR5[i].polyOffset, 4, 1, f);
  4401. printDebug("LoadTR5", "polyOffset = 0x%x", mRoomsTR5[i].polyOffset);
  4402. Fread(&mRoomsTR5[i].polyOffset2, 4, 1, f);
  4403. printDebug("LoadTR5", "polyOffset2 = 0x%x", mRoomsTR5[i].polyOffset2);
  4404. Fread(&mRoomsTR5[i].verticesSize, 4, 1, f);
  4405. printDebug("LoadTR5", "verticesSize = 0x%x", mRoomsTR5[i].verticesSize);
  4406. printDebug("LoadTR5", "}");
  4407. //////////////////////////////
  4408. Fread(&mRoomsTR5[i].seperator17, 4, 1, f);
  4409. printDebug("LoadTR5", "seperator17 CDCDCDCD = 0x%x",
  4410. mRoomsTR5[i].seperator17);
  4411. if (mRoomsTR5[i].seperator17 != 0xcdcdcdcd)
  4412. {
  4413. print("LoadTR5", "Error #18 CDCDCDCD != 0x%x",
  4414. mRoomsTR5[i].seperator17);
  4415. return -3;
  4416. }
  4417. Fread(&mRoomsTR5[i].seperator18, 4, 1, f);
  4418. printDebug("LoadTR5", "seperator18 CDCDCDCD = 0x%x",
  4419. mRoomsTR5[i].seperator18);
  4420. if (mRoomsTR5[i].seperator18 != 0xcdcdcdcd)
  4421. {
  4422. print("LoadTR5", "Error #19 CDCDCDCD != 0x%x",
  4423. mRoomsTR5[i].seperator18);
  4424. return -3;
  4425. }
  4426. Fread(&mRoomsTR5[i].seperator19, 4, 1, f);
  4427. printDebug("LoadTR5", "seperator19 CDCDCDCD = 0x%x",
  4428. mRoomsTR5[i].seperator19);
  4429. if (mRoomsTR5[i].seperator19 != 0xcdcdcdcd)
  4430. {
  4431. print("LoadTR5", "Error #20 CDCDCDCD != 0x%x",
  4432. mRoomsTR5[i].seperator19);
  4433. return -3;
  4434. }
  4435. Fread(&mRoomsTR5[i].seperator20, 4, 1, f);
  4436. printDebug("LoadTR5", "seperator20 CDCDCDCD = 0x%x",
  4437. mRoomsTR5[i].seperator20);
  4438. if (mRoomsTR5[i].seperator20 != 0xcdcdcdcd)
  4439. {
  4440. print("LoadTR5", "Error #21 CDCDCDCD != 0x%x",
  4441. mRoomsTR5[i].seperator20);
  4442. return -3;
  4443. }
  4444. // Lights
  4445. printDebug("LoadTR5", "Reading %u lights @ 88bytes each",
  4446. mRoomsTR5[i].numRoomLights);
  4447. if (mRoomsTR5[i].numRoomLights)
  4448. {
  4449. mRoomsTR5[i].lights = new tr5_light_t[mRoomsTR5[i].numRoomLights];
  4450. }
  4451. for (j = 0; j < (int)mRoomsTR5[i].numRoomLights; ++j)
  4452. {
  4453. Fread(&mRoomsTR5[i].lights[j], 88, 1, f);
  4454. //Fread(&mRoomsTR5[i].lights[j].x, 4, 1, f);
  4455. printDebug("LoadTR5", "light[%i].x? = %f", j,
  4456. mRoomsTR5[i].lights[j].x);
  4457. //Fread(&mRoomsTR5[i].lights[j].y, 4, 1, f);
  4458. printDebug("LoadTR5", "light[%i].y? = %f", j,
  4459. mRoomsTR5[i].lights[j].y);
  4460. //Fread(&mRoomsTR5[i].lights[j].z, 4, 1, f);
  4461. printDebug("LoadTR5", "light[%i].z? = %f", j,
  4462. mRoomsTR5[i].lights[j].z);
  4463. //Fread(&mRoomsTR5[i].lights[j].red, 4, 1, f);
  4464. printDebug("LoadTR5", "light[%i].r? = %f",
  4465. j, mRoomsTR5[i].lights[j].red);
  4466. //Fread(&mRoomsTR5[i].lights[j].green, 4, 1, f);
  4467. printDebug("LoadTR5", "light[%i].g? = %f",
  4468. j, mRoomsTR5[i].lights[j].green);
  4469. //Fread(&mRoomsTR5[i].lights[j].blue, 4, 1, f);
  4470. printDebug("LoadTR5", "light[%i].b? = %f",
  4471. j, mRoomsTR5[i].lights[j].blue);
  4472. // 24bytes from start of light
  4473. //Fread(&mRoomsTR5[i].lights[j].seperator, 4, 1, f);
  4474. printDebug("LoadTR5", "CDCDCDCD for some maps? = 0x%8x\t\t[%s]",
  4475. mRoomsTR5[i].lights[j].seperator,
  4476. (mRoomsTR5[i].lights[j].seperator == 0xcdcdcdcd)
  4477. ? "OK" : "ERROR");
  4478. //Fread(&mRoomsTR5[i].lights[j].input, 4, 1, f);
  4479. printDebug("LoadTR5", "light[%i].input = %f",
  4480. j, mRoomsTR5[i].lights[j].input);
  4481. //Fread(&mRoomsTR5[i].lights[j].output, 4, 1, f);
  4482. printDebug("LoadTR5", "light[%i].output = %f",
  4483. j, mRoomsTR5[i].lights[j].output);
  4484. //Fread(&mRoomsTR5[i].lights[j].range, 4, 1, f);
  4485. printDebug("LoadTR5", "light[%i].range = %f",
  4486. j, mRoomsTR5[i].lights[j].range);
  4487. //Fread(&mRoomsTR5[i].lights[j].directionVectorX, 4, 1, f);
  4488. printDebug("LoadTR5", "light[%i].directionVectorX = %f",
  4489. j, mRoomsTR5[i].lights[j].directionVectorX);
  4490. //Fread(&mRoomsTR5[i].lights[j].directionVectorY, 4, 1, f);
  4491. printDebug("LoadTR5", "light[%i].directionVectorY = %f",
  4492. j, mRoomsTR5[i].lights[j].directionVectorY);
  4493. //Fread(&mRoomsTR5[i].lights[j].directionVectorZ, 4, 1, f);
  4494. printDebug("LoadTR5", "light[%i].directionVectorZ = %f",
  4495. j, mRoomsTR5[i].lights[j].directionVectorZ);
  4496. //Fread(&mRoomsTR5[i].lights[j].x2, 4, 1, f);
  4497. printDebug("LoadTR5", "light[%i].x2 = %u",
  4498. j, mRoomsTR5[i].lights[j].x2);
  4499. //Fread(&mRoomsTR5[i].lights[j].y2, 4, 1, f);
  4500. printDebug("LoadTR5", "light[%i].y2 = %u",
  4501. j, mRoomsTR5[i].lights[j].y2);
  4502. //Fread(&mRoomsTR5[i].lights[j].z2, 4, 1, f);
  4503. printDebug("LoadTR5", "light[%i].z2 = %u",
  4504. j, mRoomsTR5[i].lights[j].z2);
  4505. //Fread(&mRoomsTR5[i].lights[j].directionVectorX2, 4, 1, f);
  4506. printDebug("LoadTR5", "light[%i].directionVectorX2 = %u",
  4507. j, mRoomsTR5[i].lights[j].directionVectorX2);
  4508. //Fread(&mRoomsTR5[i].lights[j].directionVectorY2, 4, 1, f);
  4509. printDebug("LoadTR5", "light[%i].directionVectorY2 = %u",
  4510. j, mRoomsTR5[i].lights[j].directionVectorY2);
  4511. //Fread(&mRoomsTR5[i].lights[j].directionVectorZ2, 4, 1, f);
  4512. printDebug("LoadTR5", "light[%i].directionVectorZ2 = %u",
  4513. j, mRoomsTR5[i].lights[j].directionVectorZ2);
  4514. //Fread(&mRoomsTR5[i].lights[j].lightType, 1, 1, f);
  4515. printDebug("LoadTR5", "light[%i].d3d_flag = 0x%x (%s)",
  4516. j, mRoomsTR5[i].lights[j].lightType,
  4517. (mRoomsTR5[i].lights[j].lightType == 1) ? "Point" :
  4518. (mRoomsTR5[i].lights[j].lightType == 2) ? "Spot" :
  4519. (mRoomsTR5[i].lights[j].lightType == 3) ? "Directional" : "Unknown");
  4520. //Fread(&mRoomsTR5[i].lights[j].seperator2, 3, 1, f);
  4521. printDebug("LoadTR5", "CDCDCD = %c%c%c",
  4522. mRoomsTR5[i].lights[j].seperator2[0],
  4523. mRoomsTR5[i].lights[j].seperator2[1],
  4524. mRoomsTR5[i].lights[j].seperator2[2]);
  4525. }
  4526. int numSectors = mRoomsTR5[i].numXSectors * mRoomsTR5[i].numZSectors;
  4527. if (numSectors)
  4528. {
  4529. mRoomsTR5[i].sectors = new tr2_room_sector_t[numSectors];
  4530. }
  4531. // Sectors
  4532. printDebug("LoadTR5", "Reading %u sectors @ 8bytes each",
  4533. numSectors);
  4534. for (j = 0; j < numSectors; ++j)
  4535. {
  4536. Fread(&mRoomsTR5[i].sectors[j].fd_index, 2, 1, f);
  4537. printDebug("LoadTR5", "sector[%i].fd_index = %u", j,
  4538. mRoomsTR5[i].sectors[j].fd_index);
  4539. Fread(&mRoomsTR5[i].sectors[j].box_index, 2, 1, f);
  4540. printDebug("LoadTR5", "sector[%i].box_index = %u", j,
  4541. mRoomsTR5[i].sectors[j].box_index);
  4542. Fread(&mRoomsTR5[i].sectors[j].room_below, 1, 1, f);
  4543. printDebug("LoadTR5", "sector[%i].room_below = %u", j,
  4544. mRoomsTR5[i].sectors[j].room_below);
  4545. Fread(&mRoomsTR5[i].sectors[j].floor, 1, 1, f);
  4546. printDebug("LoadTR5", "sector[%i].floor = %i", j,
  4547. (*(char*)(&mRoomsTR5[i].sectors[j].floor)));
  4548. Fread(&mRoomsTR5[i].sectors[j].room_above, 1, 1, f);
  4549. printDebug("LoadTR5", "sector[%i].room_above = %u", j,
  4550. mRoomsTR5[i].sectors[j].room_above);
  4551. Fread(&mRoomsTR5[i].sectors[j].ceiling, 1, 1, f);
  4552. printDebug("LoadTR5", "sector[%i].ceiling = %i", j,
  4553. (*(char*)(&mRoomsTR5[i].sectors[j].ceiling)));
  4554. }
  4555. portalOffset = (thisRoomOffset + mRoomsTR5[i].startSDOffset + 216 +
  4556. numSectors * 8);
  4557. u = ftell(f);
  4558. if (u != portalOffset)
  4559. {
  4560. printDebug("LoadTR5", "*** Skipping %i bytes to start of portals ***",
  4561. portalOffset - u);
  4562. fseek(f, portalOffset, SEEK_SET);
  4563. }
  4564. // Portals //////////////////////
  4565. Fread(&mRoomsTR5[i].numDoors, 2, 1, f);
  4566. printDebug("LoadTR5", "room[%i].tr5_num_portals = %u",
  4567. i, mRoomsTR5[i].numDoors);
  4568. printDebug("LoadTR5", "Reading %u portals @ 32bytes each",
  4569. mRoomsTR5[i].numDoors);
  4570. if (mRoomsTR5[i].numDoors)
  4571. {
  4572. mRoomsTR5[i].doors = new tr2_room_portal_t[mRoomsTR5[i].numDoors];
  4573. }
  4574. for (j = 0; j < (int)mRoomsTR5[i].numDoors; ++j)
  4575. {
  4576. Fread(&mRoomsTR5[i].doors[j].adjoining_room, 2, 1, f);
  4577. printDebug("LoadTR5", "room[%i].portal[%i].adjoining_room = %u",
  4578. i, j, mRoomsTR5[i].doors[j].adjoining_room);
  4579. Fread(&mRoomsTR5[i].doors[j].normal.x, 2, 1, f);
  4580. Fread(&mRoomsTR5[i].doors[j].normal.y, 2, 1, f);
  4581. Fread(&mRoomsTR5[i].doors[j].normal.z, 2, 1, f);
  4582. printDebug("LoadTR5", "portal[%i].normal = ( %i, %i, %i )", j,
  4583. mRoomsTR5[i].doors[j].normal.x,
  4584. mRoomsTR5[i].doors[j].normal.y,
  4585. mRoomsTR5[i].doors[j].normal.z);
  4586. for (k = 0; k < 4; ++k)
  4587. {
  4588. Fread(&mRoomsTR5[i].doors[j].vertices[k].x, 2, 1, f);
  4589. Fread(&mRoomsTR5[i].doors[j].vertices[k].y, 2, 1, f);
  4590. Fread(&mRoomsTR5[i].doors[j].vertices[k].z, 2, 1, f);
  4591. printDebug("LoadTR5", "portal[%i].vertices[%i] = ( %i, %i, %i )",
  4592. j, k,
  4593. mRoomsTR5[i].doors[j].vertices[k].x,
  4594. mRoomsTR5[i].doors[j].vertices[k].y,
  4595. mRoomsTR5[i].doors[j].vertices[k].z);
  4596. }
  4597. }
  4598. Fread(&mRoomsTR5[i].seperator21, 2, 1, f);
  4599. printDebug("LoadTR5", "seperator21, CDCD = 0x%x",
  4600. mRoomsTR5[i].seperator21);
  4601. if (mRoomsTR5[i].seperator21 != 0xcdcd)
  4602. {
  4603. print("LoadTR5", "Error #22 CDCD != 0x%x",
  4604. mRoomsTR5[i].seperator21);
  4605. return -3;
  4606. }
  4607. portalOffset = (thisRoomOffset + mRoomsTR5[i].endPortalOffset + 216);
  4608. u = ftell(f);
  4609. if (u != portalOffset)
  4610. {
  4611. printDebug("LoadTR5", "*** Skipping %i bytes to end of portals ***",
  4612. portalOffset - u);
  4613. fseek(f, portalOffset, SEEK_SET);
  4614. }
  4615. if (mRoomsTR5[i].numStaticMeshes)
  4616. {
  4617. mRoomsTR5[i].meshes = new tr2_room_staticmesh_t[mRoomsTR5[i].numStaticMeshes];
  4618. }
  4619. // Static meshes
  4620. for (j = 0; j < (int)mRoomsTR5[i].numStaticMeshes; ++j)
  4621. {
  4622. Fread(&mRoomsTR5[i].meshes[j].x, 4, 1, f);
  4623. printDebug("LoadTR5", "static_mesh[%i].x = %i", j,
  4624. mRoomsTR5[i].meshes[j].x);
  4625. Fread(&mRoomsTR5[i].meshes[j].y, 4, 1, f);
  4626. printDebug("LoadTR5", "static_mesh[%i].y = %i", j,
  4627. mRoomsTR5[i].meshes[j].y);
  4628. Fread(&mRoomsTR5[i].meshes[j].z, 4, 1, f);
  4629. printDebug("LoadTR5", "static_mesh[%i].z = %i", j,
  4630. mRoomsTR5[i].meshes[j].z);
  4631. Fread(&mRoomsTR5[i].meshes[j].rotation, 2, 1, f);
  4632. printDebug("LoadTR5", "static_mesh[%i].rotation = %i", j,
  4633. mRoomsTR5[i].meshes[j].rotation);
  4634. Fread(&mRoomsTR5[i].meshes[j].intensity1, 2, 1, f);
  4635. printDebug("LoadTR5", "static_mesh[%i].intensity1 = %i", j,
  4636. mRoomsTR5[i].meshes[j].intensity1);
  4637. Fread(&mRoomsTR5[i].meshes[j].intensity2, 2, 1, f);
  4638. printDebug("LoadTR5", "static_mesh[%i].intensity2 = %i", j,
  4639. mRoomsTR5[i].meshes[j].intensity2);
  4640. Fread(&mRoomsTR5[i].meshes[j].object_id, 2, 1, f);
  4641. printDebug("LoadTR5", "static_mesh[%i].object_id = %i", j,
  4642. mRoomsTR5[i].meshes[j].object_id);
  4643. }
  4644. // Layers /////////////////
  4645. if (mRoomsTR5[i].numLayers)
  4646. {
  4647. mRoomsTR5[i].layers = new tr5_room_layer_t[mRoomsTR5[i].numLayers];
  4648. }
  4649. printDebug("LoadTR5", "Reading %i layers",
  4650. mRoomsTR5[i].numLayers);
  4651. for (j = 0; j < (int)mRoomsTR5[i].numLayers; ++j)
  4652. {
  4653. Fread(&mRoomsTR5[i].layers[j], 56, 1, f);
  4654. printDebug("LoadTR5", "layer[%i].num_vertices = %i", j,
  4655. mRoomsTR5[i].layers[j].numLayerVertices);
  4656. printDebug("LoadTR5", "layer[%i].unknown1 = %i", j,
  4657. mRoomsTR5[i].layers[j].unknownL1);
  4658. printDebug("LoadTR5", "layer[%i].num_rectangles = %i", j,
  4659. mRoomsTR5[i].layers[j].numLayerRectangles);
  4660. printDebug("LoadTR5", "layer[%i].num_triangles = %i", j,
  4661. mRoomsTR5[i].layers[j].numLayerTriangles);
  4662. printDebug("LoadTR5", "layer[%i].num_2side_textures? = %i", j,
  4663. mRoomsTR5[i].layers[j].unknownL2);
  4664. printDebug("LoadTR5", "layer[%i].filler, 0? = %i", j,
  4665. mRoomsTR5[i].layers[j].filler);
  4666. printDebug("LoadTR5", "layer[%i].filler2, 0? = %i", j,
  4667. mRoomsTR5[i].layers[j].filler2);
  4668. printDebug("LoadTR5", "layer[%i].bbox[0] = {%.2f %.2f %.2f}", j,
  4669. mRoomsTR5[i].layers[j].layerBoundingBoxX1,
  4670. mRoomsTR5[i].layers[j].layerBoundingBoxX1,
  4671. mRoomsTR5[i].layers[j].layerBoundingBoxX1);
  4672. printDebug("LoadTR5", "layer[%i].bbox[1] = {%.2f %.2f %.2f}", j,
  4673. mRoomsTR5[i].layers[j].layerBoundingBoxX2,
  4674. mRoomsTR5[i].layers[j].layerBoundingBoxX2,
  4675. mRoomsTR5[i].layers[j].layerBoundingBoxX2);
  4676. printDebug("LoadTR5", "layer[%i].filler3, 0? = %i", j,
  4677. mRoomsTR5[i].layers[j].filler3);
  4678. printDebug("LoadTR5", "layer[%i].unknown6 = %i", j,
  4679. mRoomsTR5[i].layers[j].unknownL6);
  4680. printDebug("LoadTR5", "layer[%i].unknown7 = %i", j,
  4681. mRoomsTR5[i].layers[j].unknownL7);
  4682. printDebug("LoadTR5", "layer[%i].unknown8 = %i", j,
  4683. mRoomsTR5[i].layers[j].unknownL8);
  4684. }
  4685. if (mRoomsTR5[i].numLayers)
  4686. {
  4687. mRoomsTR5[i].faces = new tr5_room_geometry_t[mRoomsTR5[i].numLayers];
  4688. }
  4689. for (j = 0; j < (int)mRoomsTR5[i].numLayers; ++j)
  4690. {
  4691. mRoomsTR5[i].faces[j].quads = 0x0;
  4692. mRoomsTR5[i].faces[j].tris = 0x0;
  4693. k = mRoomsTR5[i].layers[j].numLayerRectangles;
  4694. if (k)
  4695. {
  4696. printDebug("LoadTR5", "Reading %i layer quads", k);
  4697. mRoomsTR5[i].faces[j].quads = new tr5_face4_t[k];
  4698. Fread(mRoomsTR5[i].faces[j].quads, 12, k, f);
  4699. }
  4700. k = mRoomsTR5[i].layers[j].numLayerTriangles;
  4701. if (k)
  4702. {
  4703. printDebug("LoadTR5", "Reading %i layer tris", k);
  4704. mRoomsTR5[i].faces[j].tris = new tr5_face3_t[k];
  4705. Fread(mRoomsTR5[i].faces[j].tris, 10, k, f);
  4706. }
  4707. }
  4708. for (j = 0; j < (int)mRoomsTR5[i].numLayers; ++j)
  4709. {
  4710. mRoomsTR5[i].faces[j].verts = 0x0;
  4711. k = mRoomsTR5[i].layers[j].numLayerVertices;
  4712. if (k)
  4713. {
  4714. printDebug("LoadTR5", "Reading %i layer vertices", k);
  4715. mRoomsTR5[i].faces[j].verts = new tr5_vertex_t[k];
  4716. Fread(mRoomsTR5[i].faces[j].verts, 28, k, f);
  4717. }
  4718. }
  4719. #define TR5_SKIP_TO_ROOMS
  4720. #ifdef TR5_SKIP_TO_ROOMS
  4721. unsigned int hack = ftell(f);
  4722. if (hack < nextRoomOffset)
  4723. {
  4724. printDebug("LoadTR5", "Skipping %i bytes at end of room[%i]",
  4725. nextRoomOffset - hack, i);
  4726. fseek(f, nextRoomOffset, SEEK_SET);
  4727. }
  4728. #else
  4729. long hack = 0;
  4730. // This peels padding off the end of TRCs like ANDREA1.TRC
  4731. while (hack != 0xcdcd)
  4732. {
  4733. Fread(&hack, 2, 1, f);
  4734. printDebug("LoadTR5", "hack[%i] = 0x%x", i, hack);
  4735. }
  4736. #endif
  4737. }
  4738. Fread(&_num_floor_data, 4, 1, f);
  4739. printDebug("LoadTR5", "_num_floor_data = %u", _num_floor_data);
  4740. printDebug("LoadTR5", "Reading %u floorData elements", _num_floor_data);
  4741. _floor_data = new unsigned short[_num_floor_data];
  4742. Fread(_floor_data, 2, _num_floor_data, f);
  4743. // Number of 16bits of mesh data to follow
  4744. Fread(&numMeshData, 4, 1, f);
  4745. printDebug("LoadTR5", "numMeshData = %u", numMeshData);
  4746. meshData = new unsigned char[2*numMeshData];
  4747. Fread(meshData, 2, numMeshData, f);
  4748. // Use pointers array to index in meshData array for tr5_mesh_t's
  4749. Fread(&numMeshPointers, 4, 1, f);
  4750. printDebug("LoadTR5", "numMeshPointers = %u", numMeshPointers);
  4751. meshPointers = new u_int32_t[numMeshPointers];
  4752. Fread(meshPointers, 4, numMeshPointers, f);
  4753. Fread(&numAnimationsTR5, 4, 1, f);
  4754. printDebug("LoadTR5", "numAnimationsTR5 = %u", numAnimationsTR5);
  4755. animationsTR5 = new tr5_animation_t[numAnimationsTR5];
  4756. Fread(animationsTR5, 40, numAnimationsTR5, f);
  4757. Fread(&u, 4, 1, f);
  4758. _num_state_changes = u;
  4759. printDebug("LoadTR5", "_num_state_changes = %u", _num_state_changes);
  4760. _state_changes = new tr2_state_change_t[_num_state_changes];
  4761. Fread(_state_changes, 6, _num_state_changes, f);
  4762. Fread(&u, 4, 1, f);
  4763. _num_anim_dispatches = u;
  4764. printDebug("LoadTR5", "_num_anim_dispatches = %u", _num_anim_dispatches);
  4765. _anim_dispatches = new tr2_anim_dispatch_t[_num_anim_dispatches];
  4766. Fread(_anim_dispatches, 8, _num_anim_dispatches, f);
  4767. Fread(&u, 4, 1, f);
  4768. _num_anim_commands = u;
  4769. printDebug("LoadTR5", "_num_anim_commands = %u", _num_anim_commands);
  4770. _anim_commands = new tr2_anim_command_t[_num_anim_commands];
  4771. Fread(_anim_commands, 2, _num_anim_commands, f);
  4772. Fread(&u, 4, 1, f);
  4773. _num_mesh_trees = u;
  4774. printDebug("LoadTR5", "_num_mesh_trees = %u", _num_mesh_trees);
  4775. _mesh_trees = new tr2_meshtree_t[_num_mesh_trees];
  4776. Fread(_mesh_trees, 4, _num_mesh_trees, f);
  4777. Fread(&u, 4, 1, f);
  4778. _num_frames = u;
  4779. printDebug("LoadTR5", "_num_frames = %u", _num_frames);
  4780. _frames = new u_int16_t[_num_frames];
  4781. Fread(_frames, 2, _num_frames, f);
  4782. Fread(&numMoveablesTR5, 4, 1, f);
  4783. printDebug("LoadTR5", "numMoveablesTR5 = %u", numMoveablesTR5);
  4784. moveablesTR5 = new tr5_moveable_t[numMoveablesTR5];
  4785. Fread(moveablesTR5, 20, numMoveablesTR5, f);
  4786. Fread(&u, 4, 1, f);
  4787. _num_static_meshes = u;
  4788. printDebug("LoadTR5", "_num_static_meshes = %u", _num_static_meshes);
  4789. _static_meshes = new tr2_staticmesh_t[_num_static_meshes];
  4790. Fread(_static_meshes, 32, _num_static_meshes, f);
  4791. Fread(check, 4, 1, f);
  4792. printDebug("LoadTR5", "Check: SPR = '%c%c%c'?",
  4793. check[0], check[1], check[2]);
  4794. if (check[0] != 'S' || check[1] != 'P' || check[2] != 'R')
  4795. {
  4796. print("LoadTR5", "Error: SPR != '%c%c%c'", check[0], check[1], check[2]);
  4797. return -4;
  4798. }
  4799. Fread(&u, 4, 1, f);
  4800. _num_sprite_textures = u;
  4801. printDebug("LoadTR5", "_num_sprite_textures = %u", _num_sprite_textures);
  4802. _sprite_textures = new tr2_sprite_texture_t[_num_sprite_textures];
  4803. Fread(_sprite_textures, 16, _num_sprite_textures, f);
  4804. Fread(&u, 4, 1, f);
  4805. _num_sprite_sequences = u;
  4806. printDebug("LoadTR5", "_num_sprite_sequences = %u", _num_sprite_sequences);
  4807. _sprite_sequences = new tr2_sprite_sequence_t[_num_sprite_sequences];
  4808. Fread(_sprite_sequences, 8, _num_sprite_sequences, f);
  4809. Fread(&u, 4, 1, f);
  4810. _num_cameras = u;
  4811. printDebug("LoadTR5", "_num_cameras = %u", _num_cameras);
  4812. if (_num_cameras > 0)
  4813. {
  4814. _cameras = new tr2_camera_t[_num_cameras];
  4815. Fread(_cameras, 16, _num_cameras, f);
  4816. }
  4817. else
  4818. {
  4819. _cameras = 0x0;
  4820. }
  4821. Fread(&numFlyByCamerasTR5, 4, 1, f);
  4822. printDebug("LoadTR5", "numFlyByCameras = %u", numFlyByCamerasTR5);
  4823. if (numFlyByCamerasTR5 > 0)
  4824. {
  4825. flyByCamerasTR5 = new tr5_flyby_camera_t[numFlyByCamerasTR5];
  4826. Fread(flyByCamerasTR5, 40, numFlyByCamerasTR5, f);
  4827. }
  4828. else
  4829. {
  4830. flyByCamerasTR5 = 0x0;
  4831. }
  4832. Fread(&u, 4, 1, f);
  4833. _num_sound_sources = u;
  4834. printDebug("LoadTR5", "_num_sound_sources = %u", _num_sound_sources);
  4835. if (_num_sound_sources > 0)
  4836. {
  4837. _sound_sources = new tr2_sound_source_t[_num_sound_sources];
  4838. Fread(_sound_sources, 16, _num_sound_sources, f);
  4839. }
  4840. else
  4841. {
  4842. _sound_sources = 0x0;
  4843. }
  4844. Fread(&u, 4, 1, f);
  4845. _num_boxes = u;
  4846. printDebug("LoadTR5", "_num_boxes = %u", _num_boxes);
  4847. _boxes = new tr2_box_t[_num_boxes];
  4848. Fread(_boxes, 8, _num_boxes, f);
  4849. Fread(&u, 4, 1, f);
  4850. _num_overlaps = u;
  4851. printDebug("LoadTR5", "_num_overlaps = %u", _num_overlaps);
  4852. _overlaps = new short[_num_overlaps];
  4853. Fread(_overlaps, 2, _num_overlaps, f);
  4854. _zones = new short[_num_boxes*10];
  4855. Fread(_zones, 20, _num_boxes, f);
  4856. Fread(&u, 4, 1, f);
  4857. _num_animated_textures = u;
  4858. printDebug("LoadTR5", "_num_animated_textures = %u", _num_animated_textures);
  4859. _animated_textures = new short[_num_animated_textures];
  4860. Fread(_animated_textures, 2, _num_animated_textures, f);
  4861. Fread(check, 1, 5, f);
  4862. printDebug("LoadTR5", "Check: TEX = '%c%c%c'?",
  4863. check[1], check[2], check[3]);
  4864. // check[0] is '^D'
  4865. if (check[1] != 'T' || check[2] != 'E' || check[3] != 'X')
  4866. {
  4867. print("LoadTR5", "Error: TEX != '%c%c%c' @ %lu",
  4868. check[1], check[2], check[3], ftell(f));
  4869. return -4;
  4870. }
  4871. Fread(&numObjectTexturesTR5, 4, 1, f);
  4872. printDebug("LoadTR5", "numObjectTextures = %u", numObjectTexturesTR5);
  4873. objectTexturesTR5 = new tr5_object_texture_t[numObjectTexturesTR5];
  4874. Fread(objectTexturesTR5, 40, numObjectTexturesTR5, f);
  4875. Fread(&u, 4, 1, f);
  4876. _num_items = u;
  4877. printDebug("LoadTR5", "_num_items = %u", _num_items);
  4878. _items = new tr2_item_t[_num_items];
  4879. Fread(_items, 24, _num_items, f);
  4880. Fread(&numCinematicFramesTR5, 4, 1, f);
  4881. printDebug("LoadTR5", "numCinematicFrames = %u", numCinematicFramesTR5);
  4882. if (numCinematicFramesTR5 > 0)
  4883. {
  4884. cinematicFramesTR5 = new tr5_cinematic_frame_t[numCinematicFramesTR5];
  4885. Fread(cinematicFramesTR5, 24, numCinematicFramesTR5, f);
  4886. }
  4887. else
  4888. {
  4889. cinematicFramesTR5 = 0x0;
  4890. }
  4891. Fread(&us, 2, 1, f);
  4892. _num_demo_data = us; // Could overflow? not sure
  4893. printDebug("LoadTR5", "_num_demo_data = %u", _num_demo_data);
  4894. if (_num_demo_data > 0)
  4895. {
  4896. _demo_data = new unsigned char[_num_demo_data];
  4897. Fread(_demo_data, 1, _num_demo_data, f);
  4898. }
  4899. else
  4900. {
  4901. _demo_data = 0x0;
  4902. }
  4903. printDebug("LoadTR5", "Reading soundMap");
  4904. mSoundMap = new short[450];
  4905. Fread(mSoundMap, 900, 1, f);
  4906. Fread(&u, 4, 1, f);
  4907. mNumSoundDetails = u;
  4908. printDebug("LoadTR5", "numSoundDetails = %u", mNumSoundDetails);
  4909. mSoundDetails = new tr2_sound_details_t[mNumSoundDetails];
  4910. Fread(mSoundDetails, 8, mNumSoundDetails, f);
  4911. Fread(&u, 4, 1, f);
  4912. mNumSampleIndices = u;
  4913. printDebug("LoadTR5", "numSampleIndices = %u", mNumSampleIndices);
  4914. mSampleIndicesTR5 = new unsigned int[mNumSampleIndices];
  4915. Fread(mSampleIndicesTR5, 4, mNumSampleIndices, f);
  4916. Fread(&u, 4, 1, f);
  4917. printDebug("LoadTR5", "Check 0xCDCDCDCD = 0x%X?", u);
  4918. if (u != 0xcdcdcdcd)
  4919. {
  4920. print("LoadTR5", "Check 0xCDCDCDCD != 0x%X @ %ld", u, ftell(f));
  4921. return -5;
  4922. }
  4923. // Skip over the extra short in the demo.trc, but if it's not there
  4924. // seek back
  4925. u = ftell(f);
  4926. Fread(&us, 2, 1, f);
  4927. if (us != 0xcdcd)
  4928. {
  4929. fseek(f, u, SEEK_SET);
  4930. }
  4931. //! \fixme (Endian) Read bitu32 / u_int32_t
  4932. Fread(&mNumTR4Samples, 4, 1, f);
  4933. printDebug("Load", "mNumTR4Samples = %i", mNumTR4Samples);
  4934. mRiffDataSz = 0;
  4935. mTR4Samples = new unsigned char *[mNumTR4Samples];
  4936. mTR4SamplesSz = new unsigned int[mNumTR4Samples];
  4937. memset(mTR4SamplesSz, 0, mNumTR4Samples*4);
  4938. for (i = 0; i < (int)mNumTR4Samples; ++i)
  4939. {
  4940. unsigned int sizeCompressed;
  4941. unsigned int sizeUncompressed;
  4942. unsigned char *compressedSoundSample;
  4943. unsigned char *unCompressedSoundSample;
  4944. int zErr;
  4945. uLongf libzUncompressedSize;
  4946. Fread(&sizeUncompressed, 4, 1, f);
  4947. printDebug("Load", " sizeUncompressed = %i", sizeUncompressed);
  4948. Fread(&sizeCompressed, 4, 1, f);
  4949. printDebug("Load", " sizeCompressed = %i", sizeCompressed);
  4950. compressedSoundSample = new unsigned char[sizeCompressed];
  4951. unCompressedSoundSample = new unsigned char[sizeUncompressed];
  4952. //printDebug("Load", " %lubytes read from file", ftell(f));
  4953. Fread(compressedSoundSample, sizeCompressed, 1, f);
  4954. printDebug("Load", " %c%c%c%c should be RIFF",
  4955. compressedSoundSample[0],
  4956. compressedSoundSample[1],
  4957. compressedSoundSample[2],
  4958. compressedSoundSample[3]);
  4959. // Decompress the sample
  4960. libzUncompressedSize = sizeUncompressed;
  4961. zErr = uncompress(unCompressedSoundSample,
  4962. &libzUncompressedSize,
  4963. compressedSoundSample,
  4964. sizeCompressed);
  4965. sizeUncompressed = libzUncompressedSize;
  4966. switch (zErr)
  4967. {
  4968. case Z_MEM_ERROR:
  4969. printDebug("Load", " Decompress Error: not enough memory");
  4970. break;
  4971. case Z_BUF_ERROR:
  4972. printDebug("Load", " Decompress Error: output buffer too small");
  4973. break;
  4974. case Z_DATA_ERROR:
  4975. printDebug("Load", " Decompress Error: input data was corrupted");
  4976. break;
  4977. case Z_OK:
  4978. printDebug("Load", " Decompress OK");
  4979. break;
  4980. default:
  4981. printDebug("Load", " Decompress Error: decompress error #%i", zErr);
  4982. }
  4983. // Hhhmm... handle uncompressed RIFFs too?
  4984. if (zErr == Z_OK)
  4985. {
  4986. mTR4Samples[i] = unCompressedSoundSample;
  4987. mTR4SamplesSz[i] = sizeUncompressed;
  4988. delete [] compressedSoundSample;
  4989. }
  4990. else
  4991. {
  4992. printDebug("Load", " %lubytes read from file", ftell(f));
  4993. mTR4Samples[i] = compressedSoundSample;
  4994. mTR4SamplesSz[i] = sizeCompressed;
  4995. delete [] unCompressedSoundSample;
  4996. }
  4997. }
  4998. fclose(f);
  4999. return 0;
  5000. }
  5001. void TombRaider::print(const char *methodName, const char *s, ...)
  5002. {
  5003. va_list args;
  5004. va_start(args, s);
  5005. fprintf(stderr, "TombRaider::%s> ", methodName);
  5006. vfprintf(stderr, s, args);
  5007. fprintf(stderr, "\n");
  5008. va_end(args);
  5009. }
  5010. void TombRaider::printDebug(const char *methodName, const char *s, ...)
  5011. {
  5012. va_list args;
  5013. if (!mDebug)
  5014. return;
  5015. va_start(args, s);
  5016. fprintf(stdout, "TombRaider::%s> ", methodName);
  5017. vfprintf(stdout, s, args);
  5018. fprintf(stdout, "\n");
  5019. va_end(args);
  5020. }
  5021. ////////////////////////////////////////////////////////////
  5022. // Private Mutators
  5023. ////////////////////////////////////////////////////////////