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 157KB

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