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

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