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

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