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

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