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

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