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

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