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

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