Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TombRaider.cpp 193KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370
  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 *)(void *)(_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 *)(void *)(_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 *)(void *)(_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 *)(void *)(_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 = (unsigned char)(_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. Fread(_sound_sources, sizeof(tr2_sound_source_t),
  1172. _num_sound_sources, f);
  1173. //! \fixme endian
  1174. }
  1175. #ifdef OBSOLETE
  1176. if (mEngineVersion == TR_VERSION_4)
  1177. {
  1178. unsigned int num_ZZ;
  1179. unsigned char zzbuf[17];
  1180. Fread(&num_ZZ, 1, sizeof(num_ZZ), f);
  1181. while (num_ZZ--)
  1182. {
  1183. Fread(zzbuf, 1, 16, f);
  1184. zzbuf[16] = 0;
  1185. printDebug("Load", "TR4 zz dump '%s'", zzbuf);
  1186. }
  1187. }
  1188. #endif
  1189. /* Read boxes */
  1190. Fread(&_num_boxes, sizeof(_num_boxes), 1, f);
  1191. //! \fixme endian
  1192. printDebug("Load", "_num_boxes = %i", _num_boxes);
  1193. _boxes = 0x0;
  1194. if (_num_boxes > 0)
  1195. {
  1196. _boxes = new tr2_box_t[_num_boxes];
  1197. if (mEngineVersion == TR_VERSION_1)
  1198. {
  1199. struct tr1_box
  1200. {
  1201. int zmin, zmax, xmin, xmax;
  1202. short true_floor, overlap_index;
  1203. } __attribute__ ((packed)) *tr1box;
  1204. tr1box = new tr1_box[_num_boxes];
  1205. Fread(tr1box, sizeof(struct tr1_box), _num_boxes, f);
  1206. //! \fixme endian
  1207. for (j = 0; j < _num_boxes; ++j)
  1208. {
  1209. _boxes[j].zmin = (unsigned char)(tr1box[j].zmin / 1024);
  1210. _boxes[j].zmax = (unsigned char)(tr1box[j].zmax / 1024);
  1211. _boxes[j].xmin = (unsigned char)(tr1box[j].xmin / 1024);
  1212. _boxes[j].xmax = (unsigned char)(tr1box[j].xmax / 1024);
  1213. _boxes[j].true_floor = tr1box[j].true_floor;
  1214. _boxes[j].overlap_index = tr1box[j].overlap_index;
  1215. }
  1216. delete [] tr1box;
  1217. }
  1218. else
  1219. {
  1220. Fread(_boxes, sizeof(tr2_box_t), _num_boxes, f);
  1221. }
  1222. //! \fixme endian
  1223. }
  1224. /* Read overlaps (?) */
  1225. Fread(&_num_overlaps, sizeof(_num_overlaps), 1, f);
  1226. //! \fixme endian
  1227. printDebug("Load", "_num_overlaps = %i", _num_overlaps);
  1228. _overlaps = 0x0;
  1229. if (_num_overlaps > 0)
  1230. {
  1231. _overlaps = new short[_num_overlaps];
  1232. Fread(_overlaps, 2, _num_overlaps, f);
  1233. //! \fixme endian
  1234. }
  1235. _zones = 0x0;
  1236. /* Read Zones */
  1237. if (_num_boxes > 0)
  1238. {
  1239. _zones = new short[_num_boxes*10];
  1240. if (mEngineVersion == TR_VERSION_1)
  1241. {
  1242. Fread(_zones, 12, _num_boxes, f);
  1243. }
  1244. else
  1245. {
  1246. Fread(_zones, 20, _num_boxes, f);
  1247. }
  1248. //! \fixme endian
  1249. }
  1250. /* Read animation textures (?) */
  1251. Fread(&_num_animated_textures, sizeof(_num_animated_textures), 1, f);
  1252. //! \fixme endian
  1253. printDebug("Load", "_num_animated_textures = %i", _num_animated_textures);
  1254. _animated_textures = 0x0;
  1255. if (_num_animated_textures > 0)
  1256. {
  1257. _animated_textures = new short[_num_animated_textures];
  1258. Fread(_animated_textures, 2, _num_animated_textures, f);
  1259. //! \fixme endian
  1260. }
  1261. if (mEngineVersion >= TR_VERSION_3)
  1262. {
  1263. /* Read object textures */
  1264. if (mEngineVersion == TR_VERSION_4)
  1265. {
  1266. unsigned char zzbuf[5];
  1267. Fread(zzbuf, 1, 4, f); // skip "TEX"
  1268. //!! this should be 3, but we have a bug...
  1269. zzbuf[4] = 0;
  1270. printDebug("Load", "TR4 checking %s == TEX", zzbuf);
  1271. }
  1272. Fread(&_num_object_textures, sizeof(_num_object_textures), 1, f);
  1273. //! \fixme endian
  1274. printDebug("Load", "_num_object_textures = %i", _num_object_textures);
  1275. _object_textures = 0x0;
  1276. if (_num_object_textures > 0)
  1277. {
  1278. // Used to be 2 * num, and I forgot why...
  1279. _object_textures = new tr2_object_texture_t[_num_object_textures];
  1280. //! \fixme This is fu fu fu fu fu fu
  1281. if (mEngineVersion == TR_VERSION_4)
  1282. {
  1283. int jjj, kkk;
  1284. tr4_object_texture_t *tr4_tex;
  1285. tr4_tex = new tr4_object_texture_t[_num_object_textures];
  1286. Fread(tr4_tex, 38, _num_object_textures, f);
  1287. for (jjj = 0; jjj < (int)_num_object_textures; ++jjj)
  1288. {
  1289. _object_textures[jjj].transparency_flags =
  1290. tr4_tex[jjj].attribute;
  1291. _object_textures[jjj].tile =
  1292. (unsigned short)tr4_tex[jjj].tile & 0x7fff;
  1293. for (kkk = 0; kkk < 4; ++kkk)
  1294. {
  1295. _object_textures[jjj].vertices[kkk].xcoordinate =
  1296. tr4_tex[jjj].vertices[kkk].xcoordinate;
  1297. _object_textures[jjj].vertices[kkk].xpixel =
  1298. tr4_tex[jjj].vertices[kkk].xpixel;
  1299. _object_textures[jjj].vertices[kkk].ycoordinate =
  1300. tr4_tex[jjj].vertices[kkk].ycoordinate;
  1301. _object_textures[jjj].vertices[kkk].ypixel =
  1302. tr4_tex[jjj].vertices[kkk].ypixel;
  1303. }
  1304. }
  1305. delete [] tr4_tex;
  1306. }
  1307. else
  1308. {
  1309. Fread(_object_textures, sizeof(tr2_object_texture_t),
  1310. _num_object_textures, f);
  1311. }
  1312. }
  1313. //! \fixme endian
  1314. }
  1315. /* Read items */
  1316. Fread(&_num_items, sizeof(_num_items), 1, f);
  1317. //! \fixme endian
  1318. printDebug("Load", "_num_items = %i", _num_items);
  1319. _items = 0x0;
  1320. if (_num_items > 0)
  1321. {
  1322. _items = new tr2_item_t[_num_items];
  1323. if (mEngineVersion == TR_VERSION_1)
  1324. {
  1325. for (i = 0; i < _num_items; ++i)
  1326. {
  1327. Fread(&_items[i], sizeof(tr2_item_t) - 2, 1, f);
  1328. _items[i].flags = _items[i].intensity2;
  1329. _items[i].intensity2 = _items[i].intensity1;
  1330. }
  1331. }
  1332. else
  1333. {
  1334. Fread(_items, sizeof(tr2_item_t), _num_items, f);
  1335. }
  1336. }
  1337. //! \fixme endian
  1338. /* Read LightMaps */
  1339. _light_map = new unsigned char[32 * 256];
  1340. if (mEngineVersion != TR_VERSION_4)
  1341. {
  1342. Fread(_light_map, 32, 256, f);
  1343. }
  1344. if (mEngineVersion == TR_VERSION_1)
  1345. {
  1346. /* read the 8-bit palette */
  1347. Fread(_palette8, sizeof(tr2_colour_t), 256, f);
  1348. printDebug("Load", "Read TR 1 palette");
  1349. // build 16-bit textiles from 8-bit
  1350. // (no extra colours, but creates consistent .TR2 file)
  1351. for (i = 0; i < (int)_num_textiles; ++i)
  1352. {
  1353. unsigned short argb;
  1354. double colour_tmp;
  1355. for (j = 0; j < (256 * 256); ++j)
  1356. {
  1357. colour_tmp = _palette8[_textile8[i].tile[j]].r & 0x3f;
  1358. colour_tmp = colour_tmp * 31.0 / 63.0;
  1359. argb = (unsigned short)(((int)colour_tmp) << 10);
  1360. colour_tmp = _palette8[_textile8[i].tile[j]].g & 0x3f;
  1361. colour_tmp = colour_tmp * 31.0 / 63.0;
  1362. argb |= (unsigned short)(((int)colour_tmp) << 5);
  1363. colour_tmp = _palette8[_textile8[i].tile[j]].b & 0x3f;
  1364. colour_tmp = colour_tmp * 31.0 / 63.0;
  1365. argb |= (unsigned short)((int)colour_tmp);
  1366. argb &= 0x7fff; // ???
  1367. if (_textile8[i].tile[j] != 0)
  1368. argb |= 0x8000;
  1369. _textile16[i].tile[j] = argb;
  1370. }
  1371. }
  1372. }
  1373. /* Read cinematic frames */
  1374. if (mEngineVersion == TR_VERSION_4)
  1375. {
  1376. unsigned int num_ai_data;
  1377. Fread(&num_ai_data, 4, 1, f);
  1378. printDebug("Load", "num_ai_data = %i", num_ai_data);
  1379. tr4_ai_object_t *ai_obj = 0x0;
  1380. if (num_ai_data > 0)
  1381. {
  1382. ai_obj = new tr4_ai_object_t[num_ai_data];
  1383. Fread(ai_obj, sizeof(tr4_ai_object_t), num_ai_data, f);
  1384. delete [] ai_obj;
  1385. }
  1386. }
  1387. else
  1388. {
  1389. unsigned short num_cinematic_frames;
  1390. Fread(&num_cinematic_frames, sizeof(num_cinematic_frames), 1, f);
  1391. //! \fixme endian
  1392. _num_cinematic_frames = num_cinematic_frames;
  1393. printDebug("Load", "_num_cinematic_frames = %i", _num_cinematic_frames);
  1394. _cinematic_frames = 0x0;
  1395. if (_num_cinematic_frames > 0)
  1396. {
  1397. _cinematic_frames = new tr2_cinematic_frame_t[_num_cinematic_frames];
  1398. Fread(_cinematic_frames, sizeof(tr2_cinematic_frame_t),
  1399. _num_cinematic_frames, f);
  1400. // There may or may not be endian conversion required here - I have
  1401. // no idea what this data is.
  1402. }
  1403. }
  1404. /* Read demodata (?) */
  1405. Fread(&_num_demo_data, sizeof(_num_demo_data), 1, f);
  1406. //! \fixme endian
  1407. printDebug("Load", "_num_demo_data = %i", _num_demo_data);
  1408. _demo_data = 0x0;
  1409. if (_num_demo_data > 0)
  1410. {
  1411. _demo_data = new unsigned char[_num_demo_data];
  1412. Fread(_demo_data, 1, _num_demo_data, f);
  1413. // There may or may not be endian conversion required here - I have
  1414. // no idea what this data is.
  1415. }
  1416. /* Read SoundMap */
  1417. mSoundMap = new short[370];
  1418. if (mEngineVersion == TR_VERSION_1)
  1419. {
  1420. Fread(mSoundMap, sizeof(short), 256, f);
  1421. //memset(_sound_map, 0, 370 * sizeof(short)); //! \fixme KLUDGE!!!
  1422. }
  1423. else
  1424. {
  1425. Fread(mSoundMap, sizeof(short), 370, f);
  1426. }
  1427. //! \fixme endian
  1428. /* Read SoundDetails */
  1429. Fread(&mNumSoundDetails, sizeof(mNumSoundDetails), 1, f);
  1430. printDebug("Load", "mNumSoundDetails = %i", mNumSoundDetails);
  1431. //! \fixme endian
  1432. mSoundDetails = 0x0;
  1433. if (mNumSoundDetails > 0)
  1434. {
  1435. mSoundDetails = new tr2_sound_details_t[mNumSoundDetails];
  1436. Fread(mSoundDetails, sizeof(tr2_sound_details_t), mNumSoundDetails, f);
  1437. }
  1438. //! \fixme endian
  1439. // Read sound sample indices
  1440. mSampleIndices = 0x0;
  1441. mNumSampleIndices = 0;
  1442. mRiffDataSz = 0;
  1443. mRiffData = 0x0;
  1444. mNumTR4Samples = 0;
  1445. mTR4Samples = 0x0;
  1446. switch (mEngineVersion)
  1447. {
  1448. case TR_VERSION_1:
  1449. Fread(&mRiffDataSz, 4, 1, f);
  1450. printDebug("Load", "mRiffDataSz = %ibytes", mRiffDataSz);
  1451. if (mRiffDataSz > 0)
  1452. {
  1453. mRiffData = new unsigned char[mRiffDataSz];
  1454. Fread(mRiffData, 1, mRiffDataSz, f);
  1455. }
  1456. Fread(&mNumSampleIndices, 4, 1, f);
  1457. printDebug("Load", "mNumSampleIndices = %i", mNumSampleIndices);
  1458. if (mNumSampleIndices > 0)
  1459. {
  1460. mSampleIndices = new int[mNumSampleIndices];
  1461. //! \fixme (Endian)
  1462. Fread(mSampleIndices, 4, mNumSampleIndices, f);
  1463. }
  1464. break;
  1465. case TR_VERSION_4:
  1466. mFreadMode = TR_FREAD_NORMAL;
  1467. // 0x46464952
  1468. //! \fixme (Endian) Read bitu32 / u_int32_t
  1469. Fread(&mNumTR4Samples, 4, 1, f);
  1470. printDebug("Load", "mNumTR4Samples = %i", mNumTR4Samples);
  1471. mRiffDataSz = 0;
  1472. mTR4Samples = new unsigned char *[mNumTR4Samples];
  1473. mTR4SamplesSz = new unsigned int[mNumTR4Samples];
  1474. memset(mTR4SamplesSz, 0, mNumTR4Samples*4);
  1475. for (i = 0; i < (int)mNumTR4Samples; ++i)
  1476. {
  1477. unsigned int sizeCompressed;
  1478. unsigned int sizeUncompressed;
  1479. unsigned char *compressedSoundSample;
  1480. unsigned char *unCompressedSoundSample;
  1481. int zErr;
  1482. uLongf libzUncompressedSize;
  1483. Fread(&sizeUncompressed, 4, 1, f);
  1484. printDebug("Load", " sizeUncompressed = %i", sizeUncompressed);
  1485. Fread(&sizeCompressed, 4, 1, f);
  1486. printDebug("Load", " sizeCompressed = %i", sizeCompressed);
  1487. compressedSoundSample = new unsigned char[sizeCompressed];
  1488. unCompressedSoundSample = new unsigned char[sizeUncompressed];
  1489. //printDebug("Load", " %lubytes read from file", ftell(f));
  1490. Fread(compressedSoundSample, sizeCompressed, 1, f);
  1491. printDebug("Load", " %c%c%c%c should be RIFF",
  1492. compressedSoundSample[0],
  1493. compressedSoundSample[1],
  1494. compressedSoundSample[2],
  1495. compressedSoundSample[3]);
  1496. //#define NEVER_DECOMPRESS
  1497. #ifdef NEVER_DECOMPRESS
  1498. mTR4Samples[i] = compressedSoundSample;
  1499. mTR4SamplesSz[i] = sizeCompressed;
  1500. delete [] unCompressedSoundSample;
  1501. #else
  1502. // Decompress the sample
  1503. libzUncompressedSize = sizeUncompressed;
  1504. zErr = uncompress(unCompressedSoundSample,
  1505. &libzUncompressedSize,
  1506. compressedSoundSample,
  1507. sizeCompressed);
  1508. sizeUncompressed = libzUncompressedSize;
  1509. switch (zErr)
  1510. {
  1511. case Z_MEM_ERROR:
  1512. printDebug("Load", " Decompress Error: not enough memory");
  1513. break;
  1514. case Z_BUF_ERROR:
  1515. printDebug("Load", " Decompress Error: output buffer too small");
  1516. break;
  1517. case Z_DATA_ERROR:
  1518. printDebug("Load", " Decompress Error: input data was corrupted");
  1519. break;
  1520. case Z_OK:
  1521. printDebug("Load", " Decompress OK");
  1522. break;
  1523. default:
  1524. printDebug("Load", " Decompress Error: decompress error #%i", zErr);
  1525. }
  1526. // Hhhmm... handle uncompressed RIFFs too?
  1527. if (zErr == Z_OK)
  1528. {
  1529. mTR4Samples[i] = unCompressedSoundSample;
  1530. mTR4SamplesSz[i] = sizeUncompressed;
  1531. delete [] compressedSoundSample;
  1532. }
  1533. else
  1534. {
  1535. printDebug("Load", " %lubytes read from file", ftell(f));
  1536. mTR4Samples[i] = compressedSoundSample;
  1537. mTR4SamplesSz[i] = sizeCompressed;
  1538. delete [] unCompressedSoundSample;
  1539. }
  1540. #endif
  1541. }
  1542. break;
  1543. case TR_VERSION_2:
  1544. case TR_VERSION_3:
  1545. case TR_VERSION_5:
  1546. case TR_VERSION_UNKNOWN:
  1547. default:
  1548. //! \fixme (Endian) Read bit32 / int32_t
  1549. Fread(&mNumSampleIndices, 4, 1, f);
  1550. printDebug("Load", "mNumSampleIndices = %i", mNumSampleIndices);
  1551. if (mNumSampleIndices > 0)
  1552. {
  1553. mSampleIndices = new int[mNumSampleIndices];
  1554. //! \fixme (Endian)
  1555. Fread(mSampleIndices, 4, mNumSampleIndices, f);
  1556. }
  1557. break;
  1558. }
  1559. #ifdef ZLIB_SUPPORT
  1560. if (mCompressedLevelData)
  1561. {
  1562. printDebug("Load", "Freeing uncompressed TR4 data");
  1563. delete [] mCompressedLevelData;
  1564. }
  1565. //! \fixme memory damage?
  1566. mCompressedLevelData = NULL;
  1567. #endif
  1568. fclose(f);
  1569. if (percent)
  1570. (*percent)(100);
  1571. return 0;
  1572. }
  1573. ////////////////////////////////////////////////////////////
  1574. // Public Accessors
  1575. ////////////////////////////////////////////////////////////
  1576. float TombRaider::adjustTexel(unsigned char texel, char offset)
  1577. {
  1578. if (offset >= 0)
  1579. texel++;
  1580. else
  1581. texel--;
  1582. return ((float)texel / 255.0f);
  1583. }
  1584. void TombRaider::computeRotationAngles(unsigned short **frame,
  1585. unsigned int *frame_offset,
  1586. unsigned int *angle_offset,
  1587. float *x, float *y, float *z)
  1588. {
  1589. unsigned short itmp, itmp2;
  1590. float angle;
  1591. itmp = (*frame)[(*frame_offset) + (*angle_offset)++];
  1592. if (Engine() == TR_VERSION_1)
  1593. {
  1594. // All angles are three-axis
  1595. angle = (itmp >> 4) & 0x03ff;
  1596. angle *= 360.0 / 1024.0;
  1597. *x = angle;
  1598. itmp2 = (itmp << 6) & 0x03c0;
  1599. // Get Z rotation
  1600. itmp = (*frame)[(*frame_offset) + (*angle_offset)];
  1601. ++(*angle_offset);
  1602. itmp2 |= (itmp >> 10) & 0x003f;
  1603. angle = itmp2;
  1604. angle *= 360.0 / 1024.0;
  1605. *y = angle;
  1606. angle = itmp & 0x3ff;
  1607. angle *= 360.0 / 1024.0;
  1608. *z = angle;
  1609. }
  1610. else if (itmp & 0xc000)
  1611. {
  1612. // TR2, TR3, TR4 - single axis of rotation
  1613. if (Engine() == TR_VERSION_4)
  1614. {
  1615. angle = itmp & 0x0fff;
  1616. angle /= 4096.0;
  1617. angle *= 360.0;
  1618. }
  1619. else
  1620. {
  1621. angle = itmp & 0x3ff;
  1622. angle /= 1024.0;
  1623. angle *= 360.0;
  1624. }
  1625. switch (itmp & 0xc000)
  1626. {
  1627. case 0x4000:
  1628. *x = angle;
  1629. break;
  1630. case 0x8000:
  1631. *y = angle;
  1632. break;
  1633. case 0xc000:
  1634. *z = angle;
  1635. break;
  1636. }
  1637. }
  1638. else // TR2, TR3, TR4 - three axes
  1639. {
  1640. angle = (itmp >> 4) & 0x03ff;
  1641. angle *= 360.0 / 1024.0;
  1642. *x = angle;
  1643. itmp2 = (itmp << 6) & 0x03c0;
  1644. itmp = (*frame)[(*frame_offset) + (*angle_offset)++]; // get Z rotation
  1645. itmp2 |= (itmp >> 10) & 0x003f;
  1646. angle = itmp2;
  1647. angle *= 360.0 / 1024.0;
  1648. *y = angle;
  1649. angle = itmp & 0x3ff;
  1650. angle *= 360.0 / 1024.0;
  1651. *z = angle;
  1652. }
  1653. }
  1654. void TombRaider::computeUV(tr2_object_texture_vert_t *st, float *u, float *v)
  1655. {
  1656. unsigned char x, y;
  1657. if (!st || !u || !v)
  1658. return;
  1659. x = st->xpixel;
  1660. y = st->ypixel;
  1661. x += (char)st->xcoordinate;
  1662. y += (char)st->ycoordinate;
  1663. //x += ((char)st->xcoordinate >= 0) ? 1 : -1;
  1664. //y += ((char)st->ycoordinate >= 0) ? 1 : -1;
  1665. *u = (float)x / 255.0f;
  1666. *v = (float)y / 255.0f;
  1667. }
  1668. int TombRaider::getBumpMapCount()
  1669. {
  1670. return _num_bump_map_textures / 2;
  1671. }
  1672. void TombRaider::getColor(int index, float color[4])
  1673. {
  1674. switch (getEngine())
  1675. {
  1676. case TR_VERSION_1:
  1677. color[0] = _palette8[index].r / 64.0f;
  1678. color[1] = _palette8[index].g / 64.0f;
  1679. color[2] = _palette8[index].b / 64.0f;
  1680. //color[0] = (_palette8[index].r & 0xfd) / 64.0f;
  1681. //color[1] = (_palette8[index].g & 0xfd) / 64.0f;
  1682. //color[2] = (_palette8[index].b & 0xfd) / 64.0f;
  1683. color[3] = 1.0;
  1684. break;
  1685. case TR_VERSION_2:
  1686. case TR_VERSION_3:
  1687. case TR_VERSION_4:
  1688. case TR_VERSION_5:
  1689. case TR_VERSION_UNKNOWN:
  1690. default:
  1691. color[0] = (float)(_palette16[index] & 0xff) / 256.0f;
  1692. color[1] = (float)((_palette16[index] >> 8) & 0xff) / 256.0f;
  1693. color[2] = (float)((_palette16[index] >> 16) & 0xff) / 256.0f;
  1694. color[3] = 1.0;
  1695. }
  1696. }
  1697. tr2_version_type TombRaider::getEngine()
  1698. {
  1699. return mEngineVersion;
  1700. }
  1701. void TombRaider::getMeshCollisionInfo(unsigned int meshIndex,
  1702. float center[3], float *radius)
  1703. {
  1704. if ((int)meshIndex > mMeshCount)
  1705. {
  1706. print("getMeshCollisionInfo", "Assertion error: invalid mesh index\n");
  1707. return;
  1708. }
  1709. center[0] = mMeshes[meshIndex].centre.x;
  1710. center[1] = mMeshes[meshIndex].centre.y;
  1711. center[2] = mMeshes[meshIndex].centre.z;
  1712. *radius = mMeshes[meshIndex].collision_size;
  1713. }
  1714. int TombRaider::getMeshCount()
  1715. {
  1716. return mMeshCount;
  1717. }
  1718. //! \fixme Needs refinement once the ideal format it's feeding is refined
  1719. // I should stick a HACK postfix on the method name - it's temporary
  1720. // until an array format can be crafted from a pinned down design and
  1721. // RE notes review session ( eg what about TR5? )
  1722. void TombRaider::getMeshColoredRectangle(unsigned int meshIndex,
  1723. unsigned int faceIndex,
  1724. int *index, float *color)
  1725. {
  1726. unsigned int t1_1, t1_2, t1_3, t2_1, t2_2, t2_3;
  1727. if ((int)meshIndex > mMeshCount)
  1728. {
  1729. print("getMeshColoredRectangle", "Assertion error: invalid mesh index\n");
  1730. return;
  1731. }
  1732. // Make 2 triangles from one quad!
  1733. t1_1 = 0;
  1734. t1_2 = 1;
  1735. t1_3 = 2;
  1736. t2_1 = 3;
  1737. t2_2 = 0;
  1738. t2_3 = 2;
  1739. index[0] = mMeshes[meshIndex].coloured_rectangles[faceIndex].vertices[t1_1];
  1740. index[1] = mMeshes[meshIndex].coloured_rectangles[faceIndex].vertices[t1_2];
  1741. index[2] = mMeshes[meshIndex].coloured_rectangles[faceIndex].vertices[t1_3];
  1742. index[3] = mMeshes[meshIndex].coloured_rectangles[faceIndex].vertices[t2_1];
  1743. index[4] = mMeshes[meshIndex].coloured_rectangles[faceIndex].vertices[t2_2];
  1744. index[5] = mMeshes[meshIndex].coloured_rectangles[faceIndex].vertices[t2_3];
  1745. switch (Engine())
  1746. {
  1747. case TR_VERSION_1:
  1748. getColor(mMeshes[meshIndex].coloured_rectangles[faceIndex].texture & 0xff, color);
  1749. break;
  1750. case TR_VERSION_2:
  1751. case TR_VERSION_3:
  1752. case TR_VERSION_4:
  1753. case TR_VERSION_5:
  1754. case TR_VERSION_UNKNOWN:
  1755. default:
  1756. getColor((mMeshes[meshIndex].coloured_rectangles[faceIndex].texture>>8) & 0xff, color);
  1757. }
  1758. }
  1759. void TombRaider::getMeshColoredTriangle(unsigned int meshIndex,
  1760. unsigned int faceIndex,
  1761. int *index, float *color)
  1762. {
  1763. if ((int)meshIndex > mMeshCount)
  1764. {
  1765. print("getMeshColoredTriangle", "Assertion error: invalid mesh index\n");
  1766. return;
  1767. }
  1768. index[0] = mMeshes[meshIndex].coloured_triangles[faceIndex].vertices[0];
  1769. index[1] = mMeshes[meshIndex].coloured_triangles[faceIndex].vertices[1];
  1770. index[2] = mMeshes[meshIndex].coloured_triangles[faceIndex].vertices[2];
  1771. switch (Engine())
  1772. {
  1773. case TR_VERSION_1:
  1774. getColor(mMeshes[meshIndex].coloured_triangles[faceIndex].texture & 0xff,
  1775. color);
  1776. break;
  1777. case TR_VERSION_2:
  1778. case TR_VERSION_3:
  1779. case TR_VERSION_4:
  1780. case TR_VERSION_5:
  1781. case TR_VERSION_UNKNOWN:
  1782. default:
  1783. getColor((mMeshes[meshIndex].coloured_triangles[faceIndex].texture>>8) & 0xff,
  1784. color);
  1785. }
  1786. }
  1787. void TombRaider::getMeshTexturedRectangle(unsigned int meshIndex,
  1788. unsigned int faceIndex,
  1789. int *index, float *st, int *texture,
  1790. unsigned short *transparency)
  1791. {
  1792. unsigned int t1_1, t1_2, t1_3, t2_1, t2_2, t2_3;
  1793. static bool givenWarning = false; // hahaha... okay less spewing
  1794. tr2_mesh_t *meshes;
  1795. tr2_object_texture_t *object_texture;
  1796. int t_index;
  1797. unsigned int i, m;
  1798. if ((int)meshIndex > mMeshCount)
  1799. {
  1800. print("getMeshTexturedRectangle", "Assertion error: invalid mesh index\n");
  1801. return;
  1802. }
  1803. m = meshIndex; // lazy
  1804. meshes = mMeshes; // lazy
  1805. object_texture = _object_textures; // lazy
  1806. i = faceIndex; // lazy
  1807. // Make 2 triangles from one quad!
  1808. t1_1 = 0;
  1809. t1_2 = 1;
  1810. t1_3 = 2;
  1811. t2_1 = 3;
  1812. t2_2 = 0;
  1813. t2_3 = 2;
  1814. t_index = meshes[m].textured_rectangles[i].texture;
  1815. index[0] = meshes[m].textured_rectangles[i].vertices[t1_1];
  1816. index[1] = meshes[m].textured_rectangles[i].vertices[t1_2];
  1817. index[2] = meshes[m].textured_rectangles[i].vertices[t1_3];
  1818. index[3] = meshes[m].textured_rectangles[i].vertices[t2_1];
  1819. index[4] = meshes[m].textured_rectangles[i].vertices[t2_2];
  1820. index[5] = meshes[m].textured_rectangles[i].vertices[t2_3];
  1821. computeUV(object_texture[t_index].vertices+t1_1, (st), (st)+1);
  1822. computeUV(object_texture[t_index].vertices+t1_2, (st)+2, (st)+3);
  1823. computeUV(object_texture[t_index].vertices+t1_3, (st)+4, (st)+5);
  1824. computeUV(object_texture[t_index].vertices+t2_1, (st)+6, (st)+7);
  1825. computeUV(object_texture[t_index].vertices+t2_2, (st)+8, (st)+9);
  1826. computeUV(object_texture[t_index].vertices+t2_3, (st)+10, (st)+11);
  1827. *texture = object_texture[t_index].tile;
  1828. *transparency = object_texture[t_index].transparency_flags;
  1829. // TR3+ alpha Textured polygons
  1830. if (!givenWarning && *transparency == 2)
  1831. {
  1832. givenWarning = true;
  1833. //! \fixme Use Material class to handle greyscale alpha intensity
  1834. // (partial alpha)
  1835. print("getMeshTexturedRectangle",
  1836. "TR3+ greyscale alpha intensity not implmented, %s:%i",
  1837. __FILE__, __LINE__);
  1838. }
  1839. }
  1840. void TombRaider::getMeshTexturedTriangle(unsigned int meshIndex,
  1841. unsigned int faceIndex,
  1842. int *index, float *st, int *texture,
  1843. unsigned short *transparency)
  1844. {
  1845. static bool givenWarning = false; // hahaha... okay less spewing
  1846. tr2_mesh_t *meshes;
  1847. tr2_object_texture_t *object_texture;
  1848. int t_index;
  1849. unsigned int i;
  1850. if ((int)meshIndex > mMeshCount)
  1851. {
  1852. print("getMeshTexturedTriangle", "Assertion error: invalid mesh index\n");
  1853. return;
  1854. }
  1855. meshes = mMeshes; // lazy
  1856. object_texture = _object_textures; // lazy
  1857. i = faceIndex; // lazy
  1858. t_index = meshes[meshIndex].textured_triangles[i].texture;
  1859. index[0] = meshes[meshIndex].textured_triangles[i].vertices[0];
  1860. index[1] = meshes[meshIndex].textured_triangles[i].vertices[1];
  1861. index[2] = meshes[meshIndex].textured_triangles[i].vertices[2];
  1862. computeUV(object_texture[t_index].vertices, (st), (st)+1);
  1863. computeUV(object_texture[t_index].vertices+1, (st)+2, (st)+3);
  1864. computeUV(object_texture[t_index].vertices+2, (st)+4, (st)+5);
  1865. *texture = object_texture[t_index].tile;
  1866. *transparency = object_texture[t_index].transparency_flags;
  1867. // TR3+ alpha Textured polygons
  1868. if (!givenWarning && *transparency == 2)
  1869. {
  1870. givenWarning = true;
  1871. //! \fixme Use Material class to handle greyscale alpha intensity
  1872. // (partial alpha)
  1873. print("getMeshTexturedTriangle",
  1874. "TR3+ greyscale alpha intensity not implmented, %s:%i",
  1875. __FILE__, __LINE__);
  1876. }
  1877. }
  1878. int TombRaider::getMeshTexturedTriangleCount(unsigned int meshIndex)
  1879. {
  1880. if ((int)meshIndex > mMeshCount)
  1881. {
  1882. print("getMeshTexturedTriangleCount", "Assertion error: invalid mesh index\n");
  1883. return 0;
  1884. }
  1885. return ((mMeshes[meshIndex].num_textured_triangles <= 0) ? 0 :
  1886. mMeshes[meshIndex].num_textured_triangles);
  1887. }
  1888. int TombRaider::getMeshColoredTriangleCount(unsigned int meshIndex)
  1889. {
  1890. if ((int)meshIndex > mMeshCount)
  1891. {
  1892. print("getMeshColoredTriangleCount", "Assertion error: invalid mesh index\n");
  1893. return 0;
  1894. }
  1895. return ((mMeshes[meshIndex].num_coloured_triangles <= 0) ? 0 :
  1896. mMeshes[meshIndex].num_coloured_triangles);
  1897. }
  1898. int TombRaider::getMeshTexturedRectangleCount(unsigned int meshIndex)
  1899. {
  1900. if ((int)meshIndex > mMeshCount)
  1901. {
  1902. print("getMeshTexturedRectangleCount", "Assertion error: invalid mesh index\n");
  1903. return 0;
  1904. }
  1905. return ((mMeshes[meshIndex].num_textured_rectangles <= 0) ? 0 :
  1906. mMeshes[meshIndex].num_textured_rectangles);
  1907. }
  1908. int TombRaider::getMeshColoredRectangleCount(unsigned int meshIndex)
  1909. {
  1910. if ((int)meshIndex > mMeshCount)
  1911. {
  1912. print("getMeshColoredRectangleCount", "Assertion error: invalid mesh index\n");
  1913. return 0;
  1914. }
  1915. return ((mMeshes[meshIndex].num_coloured_rectangles <= 0) ? 0:
  1916. mMeshes[meshIndex].num_coloured_rectangles);
  1917. }
  1918. //! \fixme Perhaps making color an 8bit intensity would be a better idea
  1919. void TombRaider::getMeshVertexArrays(unsigned int meshIndex,
  1920. unsigned int *vertexCount, float **verts,
  1921. unsigned int *normalCount, float **norms,
  1922. unsigned int *colorCount, float **colors)
  1923. {
  1924. unsigned int i;
  1925. float colorValue;
  1926. *vertexCount = 0;
  1927. *verts = 0x0;
  1928. *normalCount = 0;
  1929. *norms = 0x0;
  1930. *colorCount = 0;
  1931. *colors = 0x0;
  1932. if ((int)meshIndex > mMeshCount || mMeshes[meshIndex].num_vertices < 0)
  1933. return;
  1934. // Vertices
  1935. *vertexCount = mMeshes[meshIndex].num_vertices;
  1936. *verts = new float[*vertexCount * 3];
  1937. for (i = 0; i < *vertexCount; ++i)
  1938. {
  1939. (*verts)[i*3] = mMeshes[meshIndex].vertices[i].x;
  1940. (*verts)[i*3+1] = mMeshes[meshIndex].vertices[i].y;
  1941. (*verts)[i*3+2] = mMeshes[meshIndex].vertices[i].z;
  1942. }
  1943. // Normals, if any
  1944. if (mMeshes[meshIndex].num_normals > 0 &&
  1945. mMeshes[meshIndex].normals &&
  1946. mMeshes[meshIndex].num_normals == mMeshes[meshIndex].num_vertices)
  1947. {
  1948. *normalCount = mMeshes[meshIndex].num_vertices;
  1949. *norms = new float[*normalCount * 3];
  1950. for (i = 0; i < *normalCount; ++i)
  1951. {
  1952. (*norms)[i*3] = mMeshes[meshIndex].normals[i].x;
  1953. (*norms)[i*3+1] = mMeshes[meshIndex].normals[i].y;
  1954. (*norms)[i*3+2] = mMeshes[meshIndex].normals[i].z;
  1955. }
  1956. }
  1957. // Vertex lighting/colors, if any
  1958. else if (mMeshes[meshIndex].num_vertices > 0 &&
  1959. mMeshes[meshIndex].mesh_lights)
  1960. {
  1961. *colorCount = mMeshes[meshIndex].num_vertices;
  1962. #ifdef MESH_COLORS_SHOULD_BR_RGBA
  1963. *colors = new float[*colorCount * 4];
  1964. #else
  1965. *colors = new float[*colorCount];
  1966. #endif
  1967. for (i = 0; i < *colorCount; ++i)
  1968. {
  1969. colorValue = mMeshes[meshIndex].mesh_lights[i];
  1970. switch (Engine())
  1971. {
  1972. case TR_VERSION_4:
  1973. case TR_VERSION_3:
  1974. colorValue /= 16384.0;
  1975. //! \fixme Should we really fall through here?!?
  1976. break; // just testing -- xythobuz, 20140119
  1977. case TR_VERSION_1:
  1978. case TR_VERSION_2:
  1979. case TR_VERSION_5:
  1980. case TR_VERSION_UNKNOWN:
  1981. default:
  1982. colorValue = (1.0f - (colorValue / 8192.0f));
  1983. break;
  1984. }
  1985. #ifdef MESH_COLORS_SHOULD_BR_RGBA
  1986. (*colors)[i*4+0] = colorValue;
  1987. (*colors)[i*4+1] = colorValue;
  1988. (*colors)[i*4+2] = colorValue;
  1989. (*colors)[i*4+3] = 1.0;
  1990. #else
  1991. (*colors)[i] = colorValue;
  1992. #endif
  1993. }
  1994. }
  1995. }
  1996. int TombRaider::getRoomBox(unsigned int roomIndex, unsigned int index,
  1997. float *xyzA, float *xyzB,
  1998. float *xyzC, float *xyzD)
  1999. {
  2000. if (!getRoomBoxCount(roomIndex) || index > getRoomBoxCount(roomIndex))
  2001. return -1;
  2002. switch (getEngine())
  2003. {
  2004. case TR_VERSION_UNKNOWN:
  2005. default:
  2006. break;
  2007. case TR_VERSION_1:
  2008. case TR_VERSION_2:
  2009. case TR_VERSION_3:
  2010. case TR_VERSION_4:
  2011. case TR_VERSION_5:
  2012. xyzA[0] = (unsigned short)_boxes[index].xmin * 1024.0f;
  2013. xyzA[1] = (short)_boxes[index].true_floor;
  2014. xyzA[2] = (unsigned short)_boxes[index].zmin * 1024.0f;
  2015. xyzB[0] = (unsigned short)_boxes[index].xmax * 1024.0f;
  2016. xyzB[1] = (short)_boxes[index].true_floor;
  2017. xyzB[2] = (unsigned short)_boxes[index].zmin * 1024.0f;
  2018. xyzC[0] = (unsigned short)_boxes[index].xmax * 1024.0f;
  2019. xyzC[1] = (short)_boxes[index].true_floor;
  2020. xyzC[2] = (unsigned short)_boxes[index].zmax * 1024.0f;
  2021. xyzD[0] = (unsigned short)_boxes[index].xmin * 1024.0f;
  2022. xyzD[1] = (short)_boxes[index].true_floor;
  2023. xyzD[2] = (unsigned short)_boxes[index].zmax * 1024.0f;
  2024. }
  2025. return 0;
  2026. }
  2027. unsigned int TombRaider::getRoomBoxCount(unsigned int roomIndex)
  2028. {
  2029. if (!isRoomValid(roomIndex))
  2030. return 0;
  2031. switch (getEngine())
  2032. {
  2033. case TR_VERSION_UNKNOWN:
  2034. default:
  2035. break;
  2036. case TR_VERSION_1:
  2037. case TR_VERSION_2:
  2038. case TR_VERSION_3:
  2039. case TR_VERSION_4:
  2040. case TR_VERSION_5:
  2041. return _num_boxes;
  2042. }
  2043. return 0;
  2044. }
  2045. void TombRaider::getRoomInfo(unsigned int index,
  2046. unsigned int *flags, float pos[3],
  2047. float bboxMin[3], float bboxMax[3])
  2048. {
  2049. unsigned int i, n;
  2050. float f;
  2051. if (!isRoomValid(index))
  2052. return;
  2053. switch (getEngine())
  2054. {
  2055. case TR_VERSION_UNKNOWN:
  2056. break;
  2057. case TR_VERSION_5:
  2058. // Flags
  2059. *flags = mRoomsTR5[index].roomFlag; // Needs to be generic flags in class
  2060. // Positioning
  2061. pos[0] = mRoomsTR5[index].roomX;
  2062. pos[1] = 0.0f;
  2063. pos[2] = mRoomsTR5[index].roomZ;
  2064. // Bounding box setup
  2065. bboxMin[0] = 0.0;
  2066. bboxMin[1] = 0.0;
  2067. bboxMin[2] = 0.0;
  2068. bboxMax[0] = 0.0;
  2069. bboxMax[1] = 0.0;
  2070. bboxMax[2] = 0.0;
  2071. // Bounding Box setup
  2072. for (i = 0; i < mRoomsTR5[index].numLayers; ++i)
  2073. {
  2074. //! \fixme check the boxes are in min, max order in TRC
  2075. if (i == 0)
  2076. {
  2077. bboxMin[0] = mRoomsTR5[index].layers[i].layerBoundingBoxX1;
  2078. bboxMin[1] = mRoomsTR5[index].layers[i].layerBoundingBoxY1;
  2079. bboxMin[2] = mRoomsTR5[index].layers[i].layerBoundingBoxZ1;
  2080. bboxMax[0] = mRoomsTR5[index].layers[i].layerBoundingBoxX1;
  2081. bboxMax[1] = mRoomsTR5[index].layers[i].layerBoundingBoxY2;
  2082. bboxMax[2] = mRoomsTR5[index].layers[i].layerBoundingBoxZ2;
  2083. continue;
  2084. }
  2085. if (mRoomsTR5[index].layers[i].layerBoundingBoxX1 < bboxMin[0])
  2086. bboxMin[0] = mRoomsTR5[index].layers[i].layerBoundingBoxX1;
  2087. if (mRoomsTR5[index].layers[i].layerBoundingBoxY1 < bboxMin[1])
  2088. bboxMin[1] = mRoomsTR5[index].layers[i].layerBoundingBoxY1;
  2089. if (mRoomsTR5[index].layers[i].layerBoundingBoxZ1 < bboxMin[2])
  2090. bboxMin[2] = mRoomsTR5[index].layers[i].layerBoundingBoxZ1;
  2091. if (mRoomsTR5[index].layers[i].layerBoundingBoxX2 < bboxMax[0])
  2092. bboxMax[0] = mRoomsTR5[index].layers[i].layerBoundingBoxX2;
  2093. if (mRoomsTR5[index].layers[i].layerBoundingBoxY2 < bboxMax[1])
  2094. bboxMax[1] = mRoomsTR5[index].layers[i].layerBoundingBoxY2;
  2095. if (mRoomsTR5[index].layers[i].layerBoundingBoxZ2 < bboxMax[2])
  2096. bboxMax[2] = mRoomsTR5[index].layers[i].layerBoundingBoxZ2;
  2097. }
  2098. break;
  2099. case TR_VERSION_1:
  2100. case TR_VERSION_2:
  2101. case TR_VERSION_3:
  2102. case TR_VERSION_4:
  2103. // Flags
  2104. *flags = _rooms[index].flags; // Needs to be generic flags in class
  2105. // Positioning
  2106. pos[0] = _rooms[index].info.x;
  2107. pos[1] = 0.0f;
  2108. pos[2] = _rooms[index].info.z;
  2109. bboxMin[0] = 0.0;
  2110. bboxMin[1] = 0.0;
  2111. bboxMin[2] = 0.0;
  2112. bboxMax[0] = 0.0;
  2113. bboxMax[1] = 0.0;
  2114. bboxMax[2] = 0.0;
  2115. // Bounding Box setup
  2116. n = ((_rooms[index].room_data.num_vertices < 0) ? 0 :
  2117. _rooms[index].room_data.num_vertices);
  2118. for (i = 0; i < n; ++i)
  2119. {
  2120. if (i == 0)
  2121. {
  2122. bboxMin[0] = _rooms[index].room_data.vertices[i].vertex.x;
  2123. bboxMin[1] = _rooms[index].room_data.vertices[i].vertex.y;
  2124. bboxMin[2] = _rooms[index].room_data.vertices[i].vertex.z;
  2125. bboxMax[0] = _rooms[index].room_data.vertices[i].vertex.x;
  2126. bboxMax[1] = _rooms[index].room_data.vertices[i].vertex.y;
  2127. bboxMax[2] = _rooms[index].room_data.vertices[i].vertex.z;
  2128. continue;
  2129. }
  2130. f = _rooms[index].room_data.vertices[i].vertex.x;
  2131. if (f < bboxMin[0])
  2132. bboxMin[0] = f;
  2133. if (f > bboxMax[0])
  2134. bboxMax[0] = f;
  2135. f = _rooms[index].room_data.vertices[i].vertex.y;
  2136. if (f < bboxMin[1])
  2137. bboxMin[1] = f;
  2138. if (f > bboxMax[1])
  2139. bboxMax[1] = f;
  2140. f = _rooms[index].room_data.vertices[i].vertex.z;
  2141. if (f < bboxMin[2])
  2142. bboxMin[2] = f;
  2143. if (f > bboxMax[2])
  2144. bboxMax[2] = f;
  2145. }
  2146. break;
  2147. }
  2148. }
  2149. int TombRaider::getRoomLight(unsigned int roomIndex, unsigned int index,
  2150. float pos[4], float color[4], float dir[3],
  2151. float *attenuation, float *cutoffAngle,
  2152. unsigned int *type, unsigned int *flags)
  2153. {
  2154. const float dd = 10000.0; // 4095.0;
  2155. float f;
  2156. *flags = 0; // reset
  2157. switch (getEngine())
  2158. {
  2159. case TR_VERSION_UNKNOWN:
  2160. return -1;
  2161. case TR_VERSION_1:
  2162. case TR_VERSION_2:
  2163. case TR_VERSION_3:
  2164. if (_rooms[roomIndex].num_lights <= 0 ||
  2165. (int)index > _rooms[roomIndex].num_lights)
  2166. {
  2167. return -1;
  2168. }
  2169. if (_rooms[roomIndex].lights[index].fade1 == 100730731)
  2170. {
  2171. f = 1.0;
  2172. }
  2173. else
  2174. {
  2175. f = _rooms[roomIndex].lights[index].fade1;
  2176. f /= dd;
  2177. }
  2178. *attenuation = f;
  2179. *flags |= tombraiderLight_useAttenuation;
  2180. pos[0] = _rooms[roomIndex].lights[index].x;
  2181. pos[1] = _rooms[roomIndex].lights[index].y;
  2182. pos[2] = _rooms[roomIndex].lights[index].z;
  2183. pos[3] = 1.0f;
  2184. color[0] = _rooms[roomIndex].lights[index].intensity1 / 409.60f;
  2185. color[1] = color[0];
  2186. color[2] = color[0];
  2187. color[3] = 1.0f;
  2188. if (Engine() == TR_VERSION_3)
  2189. {
  2190. color[0] = _rooms[roomIndex].room_light_colour.r;
  2191. color[1] = _rooms[roomIndex].room_light_colour.g;
  2192. color[2] = _rooms[roomIndex].room_light_colour.b;
  2193. }
  2194. *type = tombraiderLight_typeDirectional;
  2195. break;
  2196. case TR_VERSION_4:
  2197. if (_rooms[roomIndex].num_lights <= 0 ||
  2198. (int)index > _rooms[roomIndex].num_lights)
  2199. {
  2200. return -1;
  2201. }
  2202. pos[0] = _rooms[roomIndex].tr4Lights[index].xPosition;
  2203. pos[1] = _rooms[roomIndex].tr4Lights[index].yPosition;
  2204. pos[2] = _rooms[roomIndex].tr4Lights[index].zPosition;
  2205. pos[3] = 0.0f;
  2206. color[0] = _rooms[roomIndex].tr4Lights[index].color.r;
  2207. color[1] = _rooms[roomIndex].tr4Lights[index].color.g;
  2208. color[2] = _rooms[roomIndex].tr4Lights[index].color.b;
  2209. dir[0] = _rooms[roomIndex].tr4Lights[index].xDir;
  2210. dir[1] = _rooms[roomIndex].tr4Lights[index].yDir;
  2211. dir[2] = _rooms[roomIndex].tr4Lights[index].zDir;
  2212. *cutoffAngle = _rooms[roomIndex].tr4Lights[index].cutoff;
  2213. *flags |= tombraiderLight_useCutoff;
  2214. switch (_rooms[roomIndex].tr4Lights[index].lightType)
  2215. {
  2216. case 1:
  2217. *type = tombraiderLight_typeDirectional;
  2218. break;
  2219. case 2:
  2220. *type = tombraiderLight_typeSpot;
  2221. break;
  2222. default:
  2223. *type = tombraiderLight_typePoint;
  2224. }
  2225. break;
  2226. case TR_VERSION_5:
  2227. if (mRoomsTR5[roomIndex].numRoomLights <= 0 ||
  2228. (int)index > mRoomsTR5[roomIndex].numRoomLights)
  2229. {
  2230. return -1;
  2231. }
  2232. pos[0] = mRoomsTR5[roomIndex].lights[index].x;
  2233. pos[1] = mRoomsTR5[roomIndex].lights[index].y;
  2234. pos[2] = mRoomsTR5[roomIndex].lights[index].z;
  2235. pos[3] = 0.0f;
  2236. color[0] = mRoomsTR5[roomIndex].lights[index].red;
  2237. color[1] = mRoomsTR5[roomIndex].lights[index].green;
  2238. color[2] = mRoomsTR5[roomIndex].lights[index].blue;
  2239. dir[0] = mRoomsTR5[roomIndex].lights[index].directionVectorX;
  2240. dir[1] = mRoomsTR5[roomIndex].lights[index].directionVectorY;
  2241. dir[2] = mRoomsTR5[roomIndex].lights[index].directionVectorZ;
  2242. switch (mRoomsTR5[roomIndex].lights[index].lightType)
  2243. {
  2244. case 2:
  2245. *type = tombraiderLight_typeSpot;
  2246. break;
  2247. case 3:
  2248. *type = tombraiderLight_typeDirectional;
  2249. break;
  2250. default:
  2251. *type = tombraiderLight_typePoint;
  2252. }
  2253. break;
  2254. }
  2255. return 0;
  2256. }
  2257. unsigned int TombRaider::getRoomLightCount(unsigned int roomIndex)
  2258. {
  2259. if (!isRoomValid(roomIndex))
  2260. return 0;
  2261. switch (getEngine())
  2262. {
  2263. case TR_VERSION_UNKNOWN:
  2264. break;
  2265. case TR_VERSION_5:
  2266. return mRoomsTR5[roomIndex].numRoomLights;
  2267. case TR_VERSION_1:
  2268. case TR_VERSION_2:
  2269. case TR_VERSION_3:
  2270. case TR_VERSION_4:
  2271. default:
  2272. return _rooms[roomIndex].num_lights;
  2273. }
  2274. return 0;
  2275. }
  2276. int TombRaider::getRoomModel(unsigned int roomIndex, unsigned int index,
  2277. int *modelIndex, float pos[3], float *yaw)
  2278. {
  2279. unsigned int i, count;
  2280. if (!getRoomModelCount(roomIndex) || index > getRoomModelCount(roomIndex))
  2281. return -1;
  2282. switch (getEngine())
  2283. {
  2284. case TR_VERSION_UNKNOWN:
  2285. default:
  2286. return -1;
  2287. case TR_VERSION_5:
  2288. count = NumStaticMeshes();
  2289. for (i = 0; i < count; ++i)
  2290. {
  2291. if (mRoomsTR5[roomIndex].meshes[index].object_id ==
  2292. _static_meshes[i].object_id)
  2293. {
  2294. *modelIndex = _static_meshes[i].starting_mesh;
  2295. pos[0] = mRoomsTR5[roomIndex].meshes[index].x;
  2296. pos[1] = mRoomsTR5[roomIndex].meshes[index].y;
  2297. pos[2] = mRoomsTR5[roomIndex].meshes[index].z;
  2298. *yaw = ((mRoomsTR5[roomIndex].meshes[index].rotation >> 14)
  2299. & 0x03) * 90;
  2300. }
  2301. }
  2302. break;
  2303. case TR_VERSION_1:
  2304. case TR_VERSION_2:
  2305. case TR_VERSION_3:
  2306. case TR_VERSION_4:
  2307. count = NumStaticMeshes();
  2308. for (i = 0; i < count; ++i)
  2309. {
  2310. if (_rooms[roomIndex].static_meshes[index].object_id ==
  2311. _static_meshes[i].object_id)
  2312. {
  2313. *modelIndex = _static_meshes[i].starting_mesh;
  2314. pos[0] = _rooms[roomIndex].static_meshes[index].x;
  2315. pos[1] = _rooms[roomIndex].static_meshes[index].y;
  2316. pos[2] = _rooms[roomIndex].static_meshes[index].z;
  2317. *yaw = ((_rooms[roomIndex].static_meshes[index].rotation >> 14)
  2318. & 0x03) * 90;
  2319. }
  2320. }
  2321. }
  2322. return 0;
  2323. }
  2324. unsigned int TombRaider::getRoomModelCount(unsigned int roomIndex)
  2325. {
  2326. if (!isRoomValid(roomIndex))
  2327. return 0;
  2328. switch (getEngine())
  2329. {
  2330. case TR_VERSION_UNKNOWN:
  2331. default:
  2332. break;
  2333. case TR_VERSION_5:
  2334. return mRoomsTR5[roomIndex].numStaticMeshes;
  2335. case TR_VERSION_1:
  2336. case TR_VERSION_2:
  2337. case TR_VERSION_3:
  2338. case TR_VERSION_4:
  2339. return _rooms[roomIndex].num_static_meshes;
  2340. }
  2341. return 0;
  2342. }
  2343. int TombRaider::getRoomPortal(unsigned int roomIndex, unsigned int index,
  2344. int *adjoiningRoom,
  2345. float normal[3], float vertices[12])
  2346. {
  2347. if (!getRoomPortalCount(roomIndex) || index > getRoomPortalCount(roomIndex))
  2348. return 0;
  2349. switch (getEngine())
  2350. {
  2351. case TR_VERSION_UNKNOWN:
  2352. default:
  2353. break;
  2354. case TR_VERSION_5:
  2355. *adjoiningRoom = mRoomsTR5[roomIndex].doors[index].adjoining_room;
  2356. normal[0] = mRoomsTR5[roomIndex].doors[index].normal.x;// / NORMAL_SCALE;
  2357. normal[1] = mRoomsTR5[roomIndex].doors[index].normal.y;// / NORMAL_SCALE;
  2358. normal[2] = mRoomsTR5[roomIndex].doors[index].normal.z;// / NORMAL_SCALE;
  2359. vertices[0] = mRoomsTR5[roomIndex].doors[index].vertices[0].x;
  2360. vertices[1] = mRoomsTR5[roomIndex].doors[index].vertices[0].y;
  2361. vertices[2] = mRoomsTR5[roomIndex].doors[index].vertices[0].z;
  2362. vertices[3] = mRoomsTR5[roomIndex].doors[index].vertices[1].x;
  2363. vertices[4] = mRoomsTR5[roomIndex].doors[index].vertices[1].y;
  2364. vertices[5] = mRoomsTR5[roomIndex].doors[index].vertices[1].z;
  2365. vertices[6] = mRoomsTR5[roomIndex].doors[index].vertices[2].x;
  2366. vertices[7] = mRoomsTR5[roomIndex].doors[index].vertices[2].y;
  2367. vertices[8] = mRoomsTR5[roomIndex].doors[index].vertices[2].z;
  2368. vertices[9] = mRoomsTR5[roomIndex].doors[index].vertices[3].x;
  2369. vertices[10] = mRoomsTR5[roomIndex].doors[index].vertices[3].y;
  2370. vertices[11] = mRoomsTR5[roomIndex].doors[index].vertices[3].z;
  2371. break;
  2372. case TR_VERSION_1:
  2373. case TR_VERSION_2:
  2374. case TR_VERSION_3:
  2375. case TR_VERSION_4:
  2376. *adjoiningRoom = _rooms[roomIndex].portals[index].adjoining_room;
  2377. normal[0] = _rooms[roomIndex].portals[index].normal.x;// / NORMAL_SCALE;
  2378. normal[1] = _rooms[roomIndex].portals[index].normal.y;// / NORMAL_SCALE;
  2379. normal[2] = _rooms[roomIndex].portals[index].normal.z;// / NORMAL_SCALE;
  2380. vertices[0] = _rooms[roomIndex].portals[index].vertices[0].x;
  2381. vertices[1] = _rooms[roomIndex].portals[index].vertices[0].y;
  2382. vertices[2] = _rooms[roomIndex].portals[index].vertices[0].z;
  2383. vertices[3] = _rooms[roomIndex].portals[index].vertices[1].x;
  2384. vertices[4] = _rooms[roomIndex].portals[index].vertices[1].y;
  2385. vertices[5] = _rooms[roomIndex].portals[index].vertices[1].z;
  2386. vertices[6] = _rooms[roomIndex].portals[index].vertices[2].x;
  2387. vertices[7] = _rooms[roomIndex].portals[index].vertices[2].y;
  2388. vertices[8] = _rooms[roomIndex].portals[index].vertices[2].z;
  2389. vertices[9] = _rooms[roomIndex].portals[index].vertices[3].x;
  2390. vertices[10] = _rooms[roomIndex].portals[index].vertices[3].y;
  2391. vertices[11] = _rooms[roomIndex].portals[index].vertices[3].z;
  2392. }
  2393. return 0;
  2394. }
  2395. unsigned int TombRaider::getRoomPortalCount(unsigned int roomIndex)
  2396. {
  2397. if (!isRoomValid(roomIndex))
  2398. return 0;
  2399. switch (getEngine())
  2400. {
  2401. case TR_VERSION_UNKNOWN:
  2402. default:
  2403. break;
  2404. case TR_VERSION_5:
  2405. return mRoomsTR5[roomIndex].numDoors;
  2406. case TR_VERSION_1:
  2407. case TR_VERSION_2:
  2408. case TR_VERSION_3:
  2409. case TR_VERSION_4:
  2410. return _rooms[roomIndex].num_portals;
  2411. }
  2412. return 0;
  2413. }
  2414. //! \fixme No TRC support
  2415. void TombRaider::getRoomRectangle(unsigned int roomIndex,
  2416. unsigned int rectangleIndex,
  2417. unsigned int *indices, float *texCoords,
  2418. int *texture, unsigned int *flags)
  2419. {
  2420. int tIndex;
  2421. unsigned int count, i, j, k;
  2422. switch (getEngine())
  2423. {
  2424. case TR_VERSION_UNKNOWN:
  2425. default:
  2426. break;
  2427. case TR_VERSION_5:
  2428. for (i = 0, count = 0; i < mRoomsTR5[roomIndex].numLayers; ++i)
  2429. {
  2430. if (count + mRoomsTR5[roomIndex].layers[i].numLayerRectangles >
  2431. rectangleIndex)
  2432. {
  2433. k = rectangleIndex - count;
  2434. *texture = *flags = 0; // FIXME
  2435. // Setup per vertex
  2436. for (j = 0; j < 3; ++j)
  2437. {
  2438. // Get vertex index { (0, a), (1, b), (2, c), (3, d) }
  2439. indices[j] = mRoomsTR5[roomIndex].faces[i].quads[k].vertices[j];
  2440. // FIXME
  2441. //tIndex = mRoomsTR5[roomIndex].faces[i].vertices[j].texture & 0xff;
  2442. //computeUV(objectTexturesTR5[tIndex].vertices + i,
  2443. // texCoords+i*2, texCoords+i*2+1);
  2444. }
  2445. break;
  2446. }
  2447. count += mRoomsTR5[roomIndex].layers[i].numLayerRectangles;
  2448. }
  2449. break;
  2450. case TR_VERSION_1:
  2451. case TR_VERSION_2:
  2452. case TR_VERSION_3:
  2453. case TR_VERSION_4:
  2454. tIndex = _rooms[roomIndex].room_data.rectangles[rectangleIndex].texture;
  2455. *texture = _object_textures[tIndex].tile;
  2456. *flags = 0;
  2457. switch (_object_textures[tIndex].transparency_flags)
  2458. {
  2459. case 0:
  2460. break;
  2461. case 2:
  2462. *flags |= tombraiderFace_PartialAlpha;
  2463. break;
  2464. default:
  2465. *flags |= tombraiderFace_Alpha;
  2466. break;
  2467. }
  2468. // Setup per vertex
  2469. for (i = 0; i < 4; ++i)
  2470. {
  2471. // Get vertex index {(0, a), (1, b), (2, c), (3, d)}
  2472. indices[i] = _rooms[roomIndex].room_data.rectangles[rectangleIndex].vertices[i];
  2473. computeUV(_object_textures[tIndex].vertices + i,
  2474. texCoords+i*2, texCoords+i*2+1);
  2475. }
  2476. }
  2477. }
  2478. unsigned int TombRaider::getRoomRectangleCount(unsigned int roomIndex)
  2479. {
  2480. unsigned int i, count;
  2481. if (!isRoomValid(roomIndex))
  2482. return 0;
  2483. switch (getEngine())
  2484. {
  2485. case TR_VERSION_UNKNOWN:
  2486. default:
  2487. break;
  2488. case TR_VERSION_5:
  2489. for (i = 0, count = 0; i < mRoomsTR5[roomIndex].numLayers; ++i)
  2490. {
  2491. count += mRoomsTR5[roomIndex].layers[i].numLayerRectangles;
  2492. }
  2493. return count;
  2494. case TR_VERSION_1:
  2495. case TR_VERSION_2:
  2496. case TR_VERSION_3:
  2497. case TR_VERSION_4:
  2498. return ((_rooms[roomIndex].room_data.num_rectangles < 0) ? 0 :
  2499. _rooms[roomIndex].room_data.num_rectangles);
  2500. }
  2501. return 0;
  2502. }
  2503. int TombRaider::getRoomSector(unsigned int roomIndex, unsigned int index,
  2504. unsigned int *flags,
  2505. float *ceiling, float *floor,
  2506. int *floorDataIndex, int *boxIndex,
  2507. int *roomBelow, int *roomAbove)
  2508. {
  2509. unsigned int count;
  2510. unsigned int zSectorsCount;
  2511. unsigned int xSectorsCount;
  2512. count = getRoomSectorCount(roomIndex, &zSectorsCount, &xSectorsCount);
  2513. if (!count || index > count)
  2514. return -1;
  2515. *flags = 0;
  2516. switch (getEngine())
  2517. {
  2518. case TR_VERSION_UNKNOWN:
  2519. default:
  2520. break;
  2521. case TR_VERSION_5:
  2522. *floorDataIndex = mRoomsTR5[roomIndex].sectors[index].fd_index;
  2523. *boxIndex = mRoomsTR5[roomIndex].sectors[index].box_index;
  2524. *roomBelow = mRoomsTR5[roomIndex].sectors[index].room_below;
  2525. *roomAbove = mRoomsTR5[roomIndex].sectors[index].room_above;
  2526. if ((unsigned char)mRoomsTR5[roomIndex].sectors[index].floor == 0x81 ||
  2527. (unsigned char)mRoomsTR5[roomIndex].sectors[index].ceiling == 0x81)
  2528. {
  2529. *flags |= tombraiderSector_wall;
  2530. }
  2531. *floor = mRoomsTR5[roomIndex].sectors[index].floor * 256.0f;
  2532. *ceiling = mRoomsTR5[roomIndex].sectors[index].ceiling * 256.0f;
  2533. break;
  2534. case TR_VERSION_1:
  2535. case TR_VERSION_2:
  2536. case TR_VERSION_3:
  2537. case TR_VERSION_4:
  2538. *floorDataIndex = _rooms[roomIndex].sector_list[index].fd_index;
  2539. *boxIndex = _rooms[roomIndex].sector_list[index].box_index;
  2540. *roomBelow = _rooms[roomIndex].sector_list[index].room_below;
  2541. *roomAbove = _rooms[roomIndex].sector_list[index].room_above;
  2542. if ((unsigned char)_rooms[roomIndex].sector_list[index].floor == 0x81 ||
  2543. (unsigned char)_rooms[roomIndex].sector_list[index].ceiling == 0x81)
  2544. {
  2545. *flags |= tombraiderSector_wall;
  2546. }
  2547. *floor = _rooms[roomIndex].sector_list[index].floor * 256.0f;
  2548. *ceiling = _rooms[roomIndex].sector_list[index].ceiling * 256.0f;
  2549. }
  2550. if (*boxIndex == 65536)
  2551. {
  2552. *boxIndex = -1;
  2553. }
  2554. if (*roomBelow == 255)
  2555. {
  2556. *roomBelow = -1;
  2557. }
  2558. if (*roomAbove == 255)
  2559. {
  2560. *roomAbove = -1;
  2561. }
  2562. return 0;
  2563. }
  2564. unsigned int TombRaider::getRoomSectorCount(unsigned int roomIndex,
  2565. unsigned int *zSectorsCount,
  2566. unsigned int *xSectorsCount)
  2567. {
  2568. unsigned int count;
  2569. if (!isRoomValid(roomIndex))
  2570. return 0;
  2571. switch (getEngine())
  2572. {
  2573. case TR_VERSION_UNKNOWN:
  2574. default:
  2575. break;
  2576. case TR_VERSION_5:
  2577. // width of sector list
  2578. *zSectorsCount = mRoomsTR5[roomIndex].numZSectors;
  2579. // height of sector list
  2580. *xSectorsCount = mRoomsTR5[roomIndex].numXSectors;
  2581. return (mRoomsTR5[roomIndex].numZSectors *
  2582. mRoomsTR5[roomIndex].numXSectors);
  2583. case TR_VERSION_1:
  2584. case TR_VERSION_2:
  2585. case TR_VERSION_3:
  2586. case TR_VERSION_4:
  2587. // width of sector list
  2588. *zSectorsCount = _rooms[roomIndex].num_zsectors;
  2589. // height of sector list
  2590. *xSectorsCount = _rooms[roomIndex].num_xsectors;
  2591. count = _rooms[roomIndex].num_zsectors * _rooms[roomIndex].num_xsectors;
  2592. return count;
  2593. }
  2594. return 0;
  2595. }
  2596. void TombRaider::getRoomSprite(unsigned int roomIndex, unsigned int index,
  2597. float scale, int *texture,
  2598. float *pos, float *vertices, float *texcoords)
  2599. {
  2600. tr2_sprite_texture_t *sprite;
  2601. tr2_vertex_t *vertex;
  2602. int t_index, width, height, x, y;
  2603. float width2, height2;
  2604. unsigned int spriteCount;
  2605. spriteCount = getRoomSpriteCount(roomIndex);
  2606. if (spriteCount == 0 || index > spriteCount)
  2607. return;
  2608. if (scale == 0.0)
  2609. scale = 10.0;
  2610. t_index = _rooms[roomIndex].room_data.sprites[index].texture;
  2611. vertex =
  2612. &_rooms[roomIndex].room_data.vertices[_rooms[roomIndex].room_data.sprites[index].vertex].vertex;
  2613. sprite = &_sprite_textures[t_index];
  2614. // Info, offset in room added to position
  2615. pos[0] = _rooms[roomIndex].info.x + vertex->x;
  2616. pos[1] = vertex->y;
  2617. pos[2] = _rooms[roomIndex].info.z + vertex->z;
  2618. *texture = sprite->tile;
  2619. width = sprite->width >> 8;
  2620. height = sprite->height >> 8;
  2621. x = sprite->x;
  2622. y = sprite->y;
  2623. width2 = width * scale; // scale it up a bit (sprites are rather tiny)
  2624. height2 = height * scale;
  2625. // Quad
  2626. vertices[0] = -width2 / 2.0f;
  2627. vertices[1] = 0;
  2628. vertices[2] = 0;
  2629. vertices[3] = -width2 / 2.0f;
  2630. vertices[4] = -height2;
  2631. vertices[5] = 0;
  2632. vertices[6] = width2 / 2.0f;
  2633. vertices[7] = -height2;
  2634. vertices[8] = 0;
  2635. vertices[9] = width2 / 2.0f;
  2636. vertices[10] = 0;
  2637. vertices[11] = 0;
  2638. texcoords[0] = ((float)y + height) / mTexelScale;
  2639. texcoords[1] = (float)(x) / mTexelScale;
  2640. texcoords[2] = (float)(y) / mTexelScale;
  2641. texcoords[3] = (float)(x) / mTexelScale;
  2642. texcoords[4] = (float)(y) / mTexelScale;
  2643. texcoords[5] = ((float)x + width) / mTexelScale;
  2644. texcoords[6] = ((float)y + height) / mTexelScale;
  2645. texcoords[7] = ((float)x + width) / mTexelScale;
  2646. }
  2647. #ifdef OPTIONAL_ARRAY_INTERFACE
  2648. void TombRaider::getRoomSpriteArray(unsigned int index, float scale,
  2649. unsigned int *spriteCount, int *textures,
  2650. float *pos, float *vertices, float *texcoords)
  2651. {
  2652. int i, t_index;
  2653. tr2_vertex_t *vertex;
  2654. int width, height, x, y;
  2655. float width2, height2;
  2656. *spriteCount = ((room[index].room_data.num_sprites < 0) ?
  2657. 0 : room[index].room_data.num_sprites);
  2658. if (*spriteCount == 0)
  2659. return;
  2660. textures = new int[*spriteCount];
  2661. pos = new float[*spriteCount * 3];
  2662. vertices = new float[*spriteCount * 12];
  2663. texcoords = new float[*spriteCount * 8];
  2664. if (scale == 0.0)
  2665. scale = 10.0;
  2666. for (i = 0; i < *spriteCount; ++i)
  2667. {
  2668. t_index = room[index].room_data.sprites[i].texture;
  2669. vertex =
  2670. &room[index].room_data.vertices[room[index].room_data.sprites[i].vertex].vertex;
  2671. sprite = &sprite_textures[t_index];
  2672. // Info, offset in room added to position
  2673. pos[0+i*3] = room[index].info.x + vertex->x;
  2674. pos[1+i*3] = vertex->y;
  2675. pos[2+i*3] = room[index].info.z + vertex->z;
  2676. textures[i] = sprite->tile;
  2677. width = sprite->width >> 8;
  2678. height = sprite->height >> 8;
  2679. x = sprite->x;
  2680. y = sprite->y;
  2681. width2 = width * scale; // scale it up a bit (sprites are rather tiny)
  2682. height2 = height * scale;
  2683. // Quad
  2684. vertices[0+i*12] = -width2 / 2.0;
  2685. vertices[1+i*12] = 0;
  2686. vertices[2+i*12] = 0;
  2687. vertices[3+i*12] = -width2 / 2.0;
  2688. vertices[4+i*12] = -height2;
  2689. vertices[5+i*12] = 0;
  2690. vertices[6+i*12] = width2 / 2.0;
  2691. vertices[7+i*12] = -height2;
  2692. vertices[8+i*12] = 0;
  2693. vertices[9+i*12] = width2 / 2.0;
  2694. vertices[10+i*12] = 0;
  2695. vertices[11+i*12] = 0;
  2696. texcoords[0+i*8] = ((float)y + height) / mTexelScale;
  2697. texcoords[1+i*8] = (float)(x) / mTexelScale;
  2698. texcoords[2+i*8] = (float)(y) / mTexelScale;
  2699. texcoords[3+i*8] = (float)(x) / mTexelScale;
  2700. texcoords[4+i*8] = (float)(y) / mTexelScale;
  2701. texcoords[5+i*8] = ((float)x + width) / mTexelScale;
  2702. texcoords[6+i*8] = ((float)y + height) / mTexelScale;
  2703. texcoords[7+i*8] = ((float)x + width) / mTexelScale;
  2704. }
  2705. }
  2706. #endif
  2707. unsigned int TombRaider::getRoomSpriteCount(unsigned int roomIndex)
  2708. {
  2709. if (!isRoomValid(roomIndex))
  2710. return 0;
  2711. switch (getEngine())
  2712. {
  2713. case TR_VERSION_UNKNOWN:
  2714. default:
  2715. break;
  2716. case TR_VERSION_5:
  2717. return 0; // No room sprites in TRC
  2718. case TR_VERSION_1:
  2719. case TR_VERSION_2:
  2720. case TR_VERSION_3:
  2721. case TR_VERSION_4:
  2722. return ((_rooms[roomIndex].room_data.num_sprites < 0) ?
  2723. 0 : _rooms[roomIndex].room_data.num_sprites);
  2724. }
  2725. return 0;
  2726. }
  2727. void TombRaider::getRoomTriangle(unsigned int roomIndex,
  2728. unsigned int triangleIndex,
  2729. unsigned int *indices, float *texCoords,
  2730. int *texture, unsigned int *flags)
  2731. {
  2732. int tIndex;
  2733. unsigned int count, i, j, k;
  2734. switch (getEngine())
  2735. {
  2736. case TR_VERSION_UNKNOWN:
  2737. default:
  2738. break;
  2739. case TR_VERSION_5:
  2740. for (i = 0, count = 0; i < mRoomsTR5[roomIndex].numLayers; ++i)
  2741. {
  2742. if (count + mRoomsTR5[roomIndex].layers[i].numLayerTriangles >
  2743. triangleIndex)
  2744. {
  2745. k = triangleIndex - count;
  2746. *texture = *flags = 0; // FIXME
  2747. // Setup per vertex
  2748. for (j = 0; j < 3; ++j)
  2749. {
  2750. // Get vertex index {(0, a), (1, b), (2, c) }
  2751. indices[j] = mRoomsTR5[roomIndex].faces[i].tris[k].vertices[j];
  2752. // FIXME
  2753. //tIndex = mRoomsTR5[roomIndex].faces[i].vertices[j].texture & 0xff;
  2754. //computeUV(objectTexturesTR5[tIndex].vertices + i,
  2755. // texCoords+i*2, texCoords+i*2+1);
  2756. }
  2757. break;
  2758. }
  2759. count += mRoomsTR5[roomIndex].layers[i].numLayerTriangles;
  2760. }
  2761. break;
  2762. case TR_VERSION_1:
  2763. case TR_VERSION_2:
  2764. case TR_VERSION_3:
  2765. case TR_VERSION_4:
  2766. tIndex = _rooms[roomIndex].room_data.triangles[triangleIndex].texture;
  2767. *texture = _object_textures[tIndex].tile;
  2768. *flags = 0;
  2769. switch (_object_textures[tIndex].transparency_flags)
  2770. {
  2771. case 0:
  2772. break;
  2773. case 2:
  2774. *flags |= tombraiderFace_PartialAlpha;
  2775. break;
  2776. default:
  2777. *flags |= tombraiderFace_Alpha;
  2778. break;
  2779. }
  2780. // Setup per vertex
  2781. for (i = 0; i < 3; ++i)
  2782. {
  2783. // Get vertex index {(0, a), (1, b), (2, c) }
  2784. indices[i] = _rooms[roomIndex].room_data.triangles[triangleIndex].vertices[i];
  2785. computeUV(_object_textures[tIndex].vertices + i,
  2786. texCoords+i*2, texCoords+i*2+1);
  2787. }
  2788. }
  2789. }
  2790. void TombRaider::getRoomTriangles(unsigned int index, int textureOffset,
  2791. unsigned int *triangleCount,
  2792. unsigned int **indices,
  2793. float **texCoords, int **textures,
  2794. unsigned int **flags)
  2795. {
  2796. unsigned int count, i, j;
  2797. int texture;
  2798. if (!isRoomValid(index))
  2799. return;
  2800. switch (getEngine())
  2801. {
  2802. case TR_VERSION_UNKNOWN:
  2803. default:
  2804. break;
  2805. case TR_VERSION_5:
  2806. for (i = 0, count = 0; i < mRoomsTR5[index].numLayers; ++i)
  2807. {
  2808. count += mRoomsTR5[index].layers[i].numLayerTriangles;
  2809. }
  2810. //! \fixme !!!
  2811. break;
  2812. case TR_VERSION_1:
  2813. case TR_VERSION_2:
  2814. case TR_VERSION_3:
  2815. case TR_VERSION_4:
  2816. // Generate textured triangles
  2817. count = ((_rooms[index].room_data.num_triangles < 0) ? 0 :
  2818. _rooms[index].room_data.num_triangles);
  2819. *triangleCount = count;
  2820. *indices = new unsigned int[count * 3];
  2821. *texCoords = new float[count * 6];
  2822. *textures = new int[count];
  2823. *flags = new unsigned int[count];
  2824. for (i = 0; i < count; ++i)
  2825. {
  2826. texture = _rooms[index].room_data.triangles[i].texture;
  2827. // Adjust texture id using m_texOffset to map into
  2828. // correct textures
  2829. (*textures)[i] = _object_textures[texture].tile + textureOffset;
  2830. (*flags)[i] = 0;
  2831. switch (_object_textures[texture].transparency_flags)
  2832. {
  2833. case 0:
  2834. break;
  2835. case 2:
  2836. (*flags)[i] |= tombraiderFace_PartialAlpha;
  2837. break;
  2838. default:
  2839. (*flags)[i] |= tombraiderFace_Alpha;
  2840. break;
  2841. }
  2842. // Setup per vertex
  2843. for (j = 0; j < 3; ++j)
  2844. {
  2845. // Get vertex index {(0, a), (1, b), (2, c)}
  2846. (*indices)[i*3+j] = _rooms[index].room_data.triangles[i].vertices[j];
  2847. computeUV(_object_textures[texture].vertices+j,
  2848. (*texCoords)+i*3+j*2, (*texCoords)+i*3+j*2+1);
  2849. }
  2850. }
  2851. }
  2852. }
  2853. unsigned int TombRaider::getRoomTriangleCount(unsigned int roomIndex)
  2854. {
  2855. unsigned int i, count;
  2856. if (!isRoomValid(roomIndex))
  2857. return 0;
  2858. switch (getEngine())
  2859. {
  2860. case TR_VERSION_UNKNOWN:
  2861. default:
  2862. break;
  2863. case TR_VERSION_5:
  2864. for (i = 0, count = 0; i < mRoomsTR5[roomIndex].numLayers; ++i)
  2865. {
  2866. count += mRoomsTR5[roomIndex].layers[i].numLayerTriangles;
  2867. }
  2868. return count;
  2869. case TR_VERSION_1:
  2870. case TR_VERSION_2:
  2871. case TR_VERSION_3:
  2872. case TR_VERSION_4:
  2873. return ((_rooms[roomIndex].room_data.num_triangles < 0) ? 0 :
  2874. _rooms[roomIndex].room_data.num_triangles);
  2875. }
  2876. return 0;
  2877. }
  2878. //! \fixme No TR5 support
  2879. void TombRaider::getRoomVertex(unsigned int roomIndex,unsigned int vertexIndex,
  2880. float *xyz, float *rgba)
  2881. {
  2882. float color_value;
  2883. unsigned int index = roomIndex, i = vertexIndex;
  2884. tr2_vertex_t *vertex = 0x0;
  2885. switch (getEngine())
  2886. {
  2887. case TR_VERSION_5:
  2888. //! \fixme !!!
  2889. print("getRoomVertex", "Error: No TRC implementation is the answer");
  2890. break;
  2891. case TR_VERSION_1:
  2892. case TR_VERSION_2:
  2893. case TR_VERSION_3:
  2894. case TR_VERSION_4:
  2895. case TR_VERSION_UNKNOWN:
  2896. default:
  2897. vertex = &_rooms[index].room_data.vertices[i].vertex;
  2898. xyz[0] = vertex->x;
  2899. xyz[1] = vertex->y;
  2900. xyz[2] = vertex->z;
  2901. switch (getEngine())
  2902. {
  2903. case TR_VERSION_1:
  2904. color_value = _rooms[index].room_data.vertices[i].attributes;
  2905. color_value = (1.1f - (color_value / 8192.0f));
  2906. rgba[0] = color_value;
  2907. rgba[1] = color_value;
  2908. rgba[2] = color_value;
  2909. break;
  2910. case TR_VERSION_3:
  2911. case TR_VERSION_4:
  2912. color_value = _rooms[index].room_data.vertices[i].lighting1;
  2913. color_value /= 16384.0;
  2914. rgba[0] = color_value + ((float)_rooms[index].room_light_colour.r /
  2915. mRoomVertexLightingFactor);
  2916. rgba[1] = color_value + ((float)_rooms[index].room_light_colour.g /
  2917. mRoomVertexLightingFactor);
  2918. rgba[2] = color_value + ((float)_rooms[index].room_light_colour.b /
  2919. mRoomVertexLightingFactor);
  2920. break;
  2921. case TR_VERSION_2:
  2922. case TR_VERSION_5: // Not really...
  2923. case TR_VERSION_UNKNOWN:
  2924. default:
  2925. color_value = _rooms[index].room_data.vertices[i].lighting1;
  2926. color_value = (1.1f - (color_value / 8192.0f));
  2927. rgba[0] = color_value;
  2928. rgba[1] = color_value;
  2929. rgba[2] = color_value;
  2930. }
  2931. // Underwater rooms have an ambient color with a shade of blue
  2932. if (_rooms[index].flags & 0x0001)
  2933. {
  2934. rgba[0] *= 0.6;
  2935. rgba[2] *= 1.2;
  2936. }
  2937. // Alpha color
  2938. rgba[3] = 1.0;
  2939. }
  2940. }
  2941. void TombRaider::getRoomVertexArrays(unsigned int roomIndex,
  2942. unsigned int *vertexCount, float **vertices,
  2943. unsigned int *normalCount, float **normals,
  2944. unsigned int *colorCount, float **colors)
  2945. {
  2946. float color_value;
  2947. float rgba[4];
  2948. unsigned int i, j, k, count;
  2949. unsigned char c;
  2950. tr2_vertex_t *vertex = 0x0;
  2951. count = getRoomVertexCount(roomIndex);
  2952. *vertexCount = 0;
  2953. *vertices = 0x0;
  2954. *normalCount = 0;
  2955. *normals = 0x0;
  2956. *colorCount = 0;
  2957. *colors = 0x0;
  2958. if (count == 0)
  2959. return;
  2960. switch (getEngine())
  2961. {
  2962. case TR_VERSION_UNKNOWN:
  2963. default:
  2964. break;
  2965. case TR_VERSION_5:
  2966. *vertexCount = count;
  2967. *vertices = new float[count*3];
  2968. *normalCount = count;
  2969. *normals = new float[count*3];
  2970. *colorCount = count;
  2971. *colors = new float[count*4];
  2972. for (i = 0, k = 0; i < mRoomsTR5[roomIndex].numLayers; ++i)
  2973. {
  2974. for (j = 0; j < mRoomsTR5[roomIndex].layers[i].numLayerVertices; ++j)
  2975. {
  2976. (*vertices)[k*3] = mRoomsTR5[roomIndex].faces[i].verts[j].x;
  2977. (*vertices)[k*3+1] = mRoomsTR5[roomIndex].faces[i].verts[j].y;
  2978. (*vertices)[k*3+2] = mRoomsTR5[roomIndex].faces[i].verts[j].z;
  2979. (*normals)[k*3] = mRoomsTR5[roomIndex].faces[i].verts[j].nx;
  2980. (*normals)[k*3+1] = mRoomsTR5[roomIndex].faces[i].verts[j].ny;
  2981. (*normals)[k*3+2] = mRoomsTR5[roomIndex].faces[i].verts[j].nz;
  2982. //! \fixme Ah, yeah this may be wrong
  2983. c = ((unsigned char *)&mRoomsTR5[roomIndex].faces[i].verts[j].vColor)[1];
  2984. (*colors)[k*4] = (float)c / 255.0f;
  2985. c = ((unsigned char *)&mRoomsTR5[roomIndex].faces[i].verts[j].vColor)[2];
  2986. (*colors)[k*4+1] = (float)c / 255.0f;
  2987. c = ((unsigned char *)&mRoomsTR5[roomIndex].faces[i].verts[j].vColor)[3];
  2988. (*colors)[k*4+2] = (float)c / 255.0f;
  2989. c = ((unsigned char *)&mRoomsTR5[roomIndex].faces[i].verts[j].vColor)[0];
  2990. (*colors)[k*4+3] = (float)c / 255.0f;
  2991. }
  2992. }
  2993. break;
  2994. case TR_VERSION_1:
  2995. case TR_VERSION_2:
  2996. case TR_VERSION_3:
  2997. case TR_VERSION_4:
  2998. *vertexCount = count;
  2999. *vertices = new float[count*3];
  3000. *normalCount = 0; //! \fixme Do some TR1-TR4 levels support normals here?
  3001. *normals = 0x0;
  3002. *colorCount = count;
  3003. *colors = new float[count*4];
  3004. // Setup vertex coloring and vertices
  3005. for (i = 0; i < count; ++i)
  3006. {
  3007. vertex = &_rooms[roomIndex].room_data.vertices[i].vertex;
  3008. (*vertices)[i*3] = vertex->x;
  3009. (*vertices)[i*3+1] = vertex->y;
  3010. (*vertices)[i*3+2] = vertex->z;
  3011. switch (getEngine())
  3012. {
  3013. case TR_VERSION_1:
  3014. color_value = _rooms[roomIndex].room_data.vertices[i].attributes;
  3015. color_value = (1.1f - (color_value / 8192.0f));
  3016. break;
  3017. case TR_VERSION_4:
  3018. case TR_VERSION_3:
  3019. color_value = _rooms[roomIndex].room_data.vertices[i].lighting1;
  3020. color_value /= 16384.0;
  3021. break;
  3022. case TR_VERSION_2:
  3023. case TR_VERSION_5:
  3024. case TR_VERSION_UNKNOWN:
  3025. default:
  3026. color_value = _rooms[roomIndex].room_data.vertices[i].lighting1;
  3027. color_value = (1.1f - (color_value / 8192.0f));
  3028. }
  3029. switch (getEngine())
  3030. {
  3031. case TR_VERSION_4:
  3032. case TR_VERSION_3:
  3033. rgba[0] = (color_value +
  3034. ((float)_rooms[roomIndex].room_light_colour.r /
  3035. mRoomVertexLightingFactor));
  3036. rgba[1] = (color_value +
  3037. ((float)_rooms[roomIndex].room_light_colour.g /
  3038. mRoomVertexLightingFactor));
  3039. rgba[2] = (color_value +
  3040. ((float)_rooms[roomIndex].room_light_colour.b /
  3041. mRoomVertexLightingFactor));
  3042. break;
  3043. case TR_VERSION_1:
  3044. case TR_VERSION_2:
  3045. case TR_VERSION_5:
  3046. case TR_VERSION_UNKNOWN:
  3047. default:
  3048. rgba[0] = color_value;
  3049. rgba[1] = color_value;
  3050. rgba[2] = color_value;
  3051. }
  3052. // Underwater rooms have an ambient color with a shade of blue
  3053. if (_rooms[roomIndex].flags & 0x0001)
  3054. {
  3055. rgba[0] *= 0.6;
  3056. rgba[2] *= 1.2;
  3057. }
  3058. // Alpha color
  3059. rgba[3] = 1.0;
  3060. (*colors)[i*4] = rgba[0];
  3061. (*colors)[i*4+1] = rgba[1];
  3062. (*colors)[i*4+2] = rgba[2];
  3063. (*colors)[i*4+3] = rgba[3];
  3064. }
  3065. }
  3066. }
  3067. unsigned int TombRaider::getRoomVertexCount(unsigned int roomIndex)
  3068. {
  3069. unsigned int i, count;
  3070. if (!isRoomValid(roomIndex))
  3071. return 0;
  3072. switch (getEngine())
  3073. {
  3074. case TR_VERSION_UNKNOWN:
  3075. default:
  3076. break;
  3077. case TR_VERSION_5:
  3078. for (i = 0, count = 0; i < mRoomsTR5[roomIndex].numLayers; ++i)
  3079. {
  3080. count += mRoomsTR5[roomIndex].layers[i].numLayerVertices;
  3081. }
  3082. return count;
  3083. case TR_VERSION_1:
  3084. case TR_VERSION_2:
  3085. case TR_VERSION_3:
  3086. case TR_VERSION_4:
  3087. return ((_rooms[roomIndex].room_data.num_vertices < 0) ? 0 :
  3088. _rooms[roomIndex].room_data.num_vertices);
  3089. }
  3090. return 0;
  3091. }
  3092. int TombRaider::getSkyModelId()
  3093. {
  3094. int skyMesh = -1;
  3095. //bool rot = false;
  3096. tr2_moveable_t *moveable;
  3097. unsigned int i, id;
  3098. moveable = Moveable();
  3099. switch (Engine())
  3100. {
  3101. case TR_VERSION_2:
  3102. //rot = true;
  3103. id = 254;
  3104. break;
  3105. case TR_VERSION_3:
  3106. id = 355;
  3107. break;
  3108. case TR_VERSION_4:
  3109. id = 459;
  3110. break;
  3111. case TR_VERSION_1:
  3112. case TR_VERSION_5:
  3113. case TR_VERSION_UNKNOWN:
  3114. default:
  3115. return -1;
  3116. }
  3117. if (id > 0)
  3118. {
  3119. for (i = 0; (int)i < NumMoveables(); ++i)
  3120. {
  3121. if (moveable[i].object_id == id)
  3122. {
  3123. //sky_mesh = moveable[i].starting_mesh;
  3124. skyMesh = i;
  3125. break;
  3126. }
  3127. }
  3128. }
  3129. return skyMesh;
  3130. }
  3131. void TombRaider::getSprites()
  3132. {
  3133. #ifdef FIXME
  3134. int i, j, k, l, x, y, s_index, width, height;
  3135. float scale, width2, height2;
  3136. tr2_sprite_texture_t *sprite;
  3137. tr2_sprite_texture_t *sprite_textures;
  3138. tr2_sprite_sequence_t *sprite_sequence;
  3139. sprite_seq_t *r_mesh;
  3140. tr2_item_t *item;
  3141. r_mesh = Mesh();
  3142. item = Item();
  3143. sprite_textures = Sprite();
  3144. sprite_sequence = SpriteSequence();
  3145. scale = 4.0;
  3146. printf("Processing sprites: ");
  3147. for (i = 0; i < NumItems() - 1; ++i)
  3148. {
  3149. print(false, "Processing sprites in Items: %i/%i",
  3150. i, NumItems());
  3151. // It's a mesh, skip it
  3152. if (Engine() == TR_VERSION_1 && item[i].intensity1 == -1)
  3153. continue;
  3154. k = item[i].object_id;
  3155. // Search the SpriteSequence list
  3156. // (if we didn't already decide that it's a mesh)
  3157. for (j = 0; j < (int)NumSpriteSequences(); ++j)
  3158. {
  3159. if (sprite_sequence[j].object_id == k)
  3160. {
  3161. k = item[i].object_id;
  3162. s_index = sprite_sequence[j].offset;
  3163. spriteSequence = new sprite_seq_t;
  3164. spriteSequence->spriteCount = -sprite_sequence[j].negative_length;
  3165. spriteSequence->sprites = new sprite_t[r_mesh->num_sprites];
  3166. sprite = spriteSequence->sprites;
  3167. callbackAddSpriteSequence(spriteSequence);
  3168. for (l = 0; l < r_mesh->num_sprites; ++l)
  3169. {
  3170. sprite = &sprite_textures[s_index];
  3171. width = sprite->width >> 8;
  3172. height = sprite->height >> 8;
  3173. x = sprite->x;
  3174. y = sprite->y;
  3175. width2 = width * scale;
  3176. height2 = height * scale;
  3177. // For external use
  3178. sprite[l].pos[0] = item[i].x;
  3179. sprite[l].pos[1] = item[i].y;
  3180. sprite[l].pos[2] = item[i].z;
  3181. sprite[l].textureIndex = sprite->tile;
  3182. sprite[l].radius = width2 / 2.0;
  3183. sprite[l].vertex[0].pos[0] = -width2 / 2.0;
  3184. sprite[l].vertex[1].pos[0] = -width2 / 2.0;
  3185. sprite[l].vertex[2].pos[0] = width2 / 2.0;
  3186. sprite[l].vertex[3].pos[0] = width2 / 2.0;
  3187. sprite[l].vertex[0].pos[1] = 0;
  3188. sprite[l].vertex[1].pos[1] = -height2;
  3189. sprite[l].vertex[2].pos[1] = -height2;
  3190. sprite[l].vertex[3].pos[1] = 0;
  3191. sprite[l].vertex[0].pos[2] = 0;
  3192. sprite[l].vertex[1].pos[2] = 0;
  3193. sprite[l].vertex[2].pos[2] = 0;
  3194. sprite[l].vertex[3].pos[2] = 0;
  3195. sprite[l].texel[3].st[0] = (double)(x+width) / mTexelScale;
  3196. sprite[l].texel[3].st[1] = (double)(y+height) / mTexelScale;
  3197. sprite[l].texel[2].st[0] = (double)(x+width) / mTexelScale;
  3198. sprite[l].texel[2].st[1] = (double)(y) / mTexelScale;
  3199. sprite[l].texel[1].st[0] = (double)(x) / mTexelScale;
  3200. sprite[l].texel[1].st[1] = (double)(y) / mTexelScale;
  3201. sprite[l].texel[0].st[0] = (double)(x) / mTexelScale;
  3202. sprite[l].texel[0].st[1] = (double)(y+height) / mTexelScale;
  3203. printf(".");
  3204. fflush(stdout);
  3205. }
  3206. }
  3207. }
  3208. }
  3209. printf("\n");
  3210. #endif
  3211. }
  3212. void TombRaider::getSoundSample(unsigned int index,
  3213. unsigned int *bytes, unsigned char **data)
  3214. {
  3215. unsigned char *riff;
  3216. unsigned int offset, altIndex;
  3217. *bytes = 0;
  3218. *data = 0x0;
  3219. switch (Engine())
  3220. {
  3221. case TR_VERSION_1:
  3222. //! \fixme This implies higher tmp memory cost ( copy safety )
  3223. getRiffData(bytes, &riff);
  3224. if (riff && (int)index < mNumSampleIndices)
  3225. {
  3226. offset = mSampleIndices[index];
  3227. if ((int)index < mNumSampleIndices - 1)
  3228. {
  3229. *bytes = mSampleIndices[index+1] - mSampleIndices[index];
  3230. }
  3231. else
  3232. {
  3233. *bytes = *bytes - mSampleIndices[index];
  3234. }
  3235. *data = new unsigned char[*bytes];
  3236. memcpy(*data, riff+offset, *bytes);
  3237. }
  3238. if (riff)
  3239. {
  3240. delete [] riff;
  3241. }
  3242. break;
  3243. case TR_VERSION_2:
  3244. case TR_VERSION_3:
  3245. if (mRiffAlternateLoaded &&
  3246. mRiffData && mRiffAlternateOffsets && (int)index < mNumSampleIndices)
  3247. {
  3248. altIndex = mSampleIndices[index];
  3249. offset = mRiffAlternateOffsets[altIndex];
  3250. if ((int)offset > mRiffDataSz)
  3251. {
  3252. print("getSoundSample", "WARNING: offset too large, may be mismatched SFX and game pak, handling...\n");
  3253. return;
  3254. }
  3255. if (altIndex < mNumTR4Samples - 1)
  3256. {
  3257. *bytes = mRiffAlternateOffsets[altIndex+1] - offset;
  3258. }
  3259. else
  3260. {
  3261. *bytes = mRiffDataSz - offset;
  3262. }
  3263. *data = new unsigned char[*bytes];
  3264. memcpy(*data, mRiffData+offset, *bytes);
  3265. }
  3266. break;
  3267. case TR_VERSION_4:
  3268. case TR_VERSION_5:
  3269. getRiffDataTR4(index, bytes, data);
  3270. break;
  3271. case TR_VERSION_UNKNOWN:
  3272. default:
  3273. ;
  3274. }
  3275. }
  3276. unsigned int TombRaider::getSoundSamplesCount()
  3277. {
  3278. unsigned int count = 0;
  3279. switch (Engine())
  3280. {
  3281. case TR_VERSION_1:
  3282. count = mNumSampleIndices;
  3283. break;
  3284. case TR_VERSION_2:
  3285. case TR_VERSION_3:
  3286. if (mRiffAlternateLoaded)
  3287. {
  3288. count = mNumSampleIndices;
  3289. }
  3290. break;
  3291. case TR_VERSION_4:
  3292. case TR_VERSION_5:
  3293. count = mNumTR4Samples;
  3294. break;
  3295. case TR_VERSION_UNKNOWN:
  3296. default:
  3297. count = 0;
  3298. }
  3299. return count;
  3300. }
  3301. bool TombRaider::isMeshValid(int index)
  3302. {
  3303. return !(index < 0 ||
  3304. index > mMeshCount ||
  3305. ((mMeshes[index].num_vertices < 0 ||
  3306. mMeshes[index].vertices == NULL)));
  3307. }
  3308. bool TombRaider::isRoomValid(int index)
  3309. {
  3310. // Yes, you MUST support signed indices due to legacy engines
  3311. if (index < 0)
  3312. return false;
  3313. switch (getEngine())
  3314. {
  3315. case TR_VERSION_UNKNOWN:
  3316. default:
  3317. break;
  3318. case TR_VERSION_5:
  3319. if (index < _num_rooms &&
  3320. //mRoomsTR5[index].roomX != 0xcdcdcd &&
  3321. //mRoomsTR5[index].roomZ != 0xcdcdcd)
  3322. *((int *)&mRoomsTR5[index].roomX) != 0xcdcdcd &&
  3323. *((int *)&mRoomsTR5[index].roomZ) != 0xcdcdcd)
  3324. // Cast to int * as it was comparing with float 0xcdcdcd before
  3325. // -- xythobuz
  3326. {
  3327. return true;
  3328. }
  3329. break;
  3330. case TR_VERSION_1:
  3331. case TR_VERSION_2:
  3332. case TR_VERSION_3:
  3333. case TR_VERSION_4:
  3334. if (index < _num_rooms &&
  3335. _rooms[index].room_data.num_vertices > 0)
  3336. {
  3337. return true;
  3338. }
  3339. }
  3340. return false;
  3341. }
  3342. ////////////////////////////////////////////////////////////
  3343. // Public Mutators
  3344. ////////////////////////////////////////////////////////////
  3345. int TombRaider::loadSFX(char *filename)
  3346. {
  3347. FILE *f = fopen(filename, "rb");
  3348. long bytes = 0;
  3349. unsigned char *data;
  3350. if (!f)
  3351. {
  3352. perror("Couldn't load SFX file");
  3353. return -1;
  3354. }
  3355. fseek(f, 0L, SEEK_END);
  3356. bytes = ftell(f);
  3357. fseek(f, 0L, SEEK_SET);
  3358. if (bytes > 8)
  3359. {
  3360. data = new unsigned char[bytes];
  3361. fread(data, bytes, 1, f);
  3362. mNumTR4Samples = getRiffOffsets(data, bytes,
  3363. &mRiffAlternateOffsets,
  3364. mNumSampleIndices);
  3365. // This SFX must not come close to matching this game pak
  3366. if ((int)mNumTR4Samples < mNumSampleIndices)
  3367. {
  3368. delete [] data;
  3369. fclose(f);
  3370. print("loadSFX", "WARNING: SFX RIFF has less than pak's RIFF count\n");
  3371. return -2;
  3372. }
  3373. // If you ran out of room, then reallocate and parse agian =(
  3374. if (mNumSampleIndices < (int)mNumTR4Samples)
  3375. {
  3376. delete [] mRiffAlternateOffsets;
  3377. mNumTR4Samples = getRiffOffsets(data, bytes,
  3378. &mRiffAlternateOffsets,
  3379. mNumTR4Samples);
  3380. }
  3381. mRiffDataSz = bytes;
  3382. mRiffData = data;
  3383. mRiffAlternateLoaded = true;
  3384. }
  3385. fclose(f);
  3386. return 0;
  3387. }
  3388. void TombRaider::reset()
  3389. {
  3390. unsigned int i;
  3391. // Mongoose 2003.05.13, Don't let some jackass reset over and over
  3392. if (mReset)
  3393. {
  3394. return;
  3395. }
  3396. //! \fixme Palettes aren't the same size anymore
  3397. //memset(_palette8, 0, 256);
  3398. //memset(_palette16, 0, 256);
  3399. if (_anim_dispatches)
  3400. delete [] _anim_dispatches;
  3401. if (_anim_commands)
  3402. delete [] _anim_commands;
  3403. if (_mesh_trees)
  3404. delete [] _mesh_trees;
  3405. if (_frames)
  3406. delete [] _frames;
  3407. if (_moveables)
  3408. delete [] _moveables;
  3409. if (_static_meshes)
  3410. delete [] _static_meshes;
  3411. if (_object_textures)
  3412. delete [] _object_textures;
  3413. if (_sprite_textures)
  3414. delete [] _sprite_textures;
  3415. if (_sprite_sequences)
  3416. delete [] _sprite_sequences;
  3417. if (_cameras)
  3418. delete [] _cameras;
  3419. if (_sound_sources)
  3420. delete [] _sound_sources;
  3421. if (_boxes)
  3422. delete [] _boxes;
  3423. if (_overlaps)
  3424. delete [] _overlaps;
  3425. if (_zones)
  3426. delete [] _zones;
  3427. if (_animated_textures)
  3428. delete [] _animated_textures;
  3429. if (_items)
  3430. delete [] _items;
  3431. if (_light_map)
  3432. delete [] _light_map;
  3433. if (_cinematic_frames)
  3434. delete [] _cinematic_frames;
  3435. if (_demo_data)
  3436. delete [] _demo_data;
  3437. if (mRiffAlternateOffsets)
  3438. delete [] mRiffAlternateOffsets;
  3439. if (mSoundMap)
  3440. delete [] mSoundMap;
  3441. if (mSoundDetails)
  3442. delete [] mSoundDetails;
  3443. if (mSampleIndices)
  3444. delete [] mSampleIndices;
  3445. if (mRiffData)
  3446. delete [] mRiffData;
  3447. if (mTR4Samples)
  3448. {
  3449. for (i = 0; i < mNumTR4Samples; ++i)
  3450. {
  3451. if (mTR4SamplesSz[i])
  3452. delete [] mTR4Samples[i];
  3453. }
  3454. delete [] mTR4Samples;
  3455. }
  3456. if (mTR4SamplesSz)
  3457. delete [] mTR4SamplesSz;
  3458. if (_rooms)
  3459. {
  3460. for (i = 0; i < _num_rooms; ++i)
  3461. {
  3462. if (_rooms[i].room_data.num_vertices > 0)
  3463. delete [] _rooms[i].room_data.vertices;
  3464. if (_rooms[i].room_data.num_rectangles > 0)
  3465. delete [] _rooms[i].room_data.rectangles;
  3466. if (_rooms[i].room_data.num_triangles > 0)
  3467. delete [] _rooms[i].room_data.triangles;
  3468. if (_rooms[i].room_data.num_sprites > 0)
  3469. delete [] _rooms[i].room_data.sprites;
  3470. if (_rooms[i].num_portals > 0)
  3471. delete []_rooms[i].portals;
  3472. if (_rooms[i].sector_list)
  3473. delete [] _rooms[i].sector_list;
  3474. if (_rooms[i].lights)
  3475. delete [] _rooms[i].lights;
  3476. if (_rooms[i].tr4Lights)
  3477. delete [] _rooms[i].tr4Lights;
  3478. if (_rooms[i].static_meshes)
  3479. delete [] _rooms[i].static_meshes;
  3480. }
  3481. delete [] _rooms;
  3482. }
  3483. if (_floor_data)
  3484. {
  3485. delete [] _floor_data;
  3486. }
  3487. if (mMeshes)
  3488. {
  3489. for (i = 0; (int)i < mMeshCount; ++i)
  3490. {
  3491. if (mMeshes[i].vertices)
  3492. delete [] mMeshes[i].vertices;
  3493. if (mMeshes[i].mesh_lights)
  3494. delete [] mMeshes[i].mesh_lights;
  3495. if (mMeshes[i].normals)
  3496. delete [] mMeshes[i].normals;
  3497. if (mMeshes[i].textured_rectangles)
  3498. delete [] mMeshes[i].textured_rectangles;
  3499. if (mMeshes[i].textured_triangles)
  3500. delete [] mMeshes[i].textured_triangles;
  3501. if (mMeshes[i].coloured_rectangles)
  3502. delete [] mMeshes[i].coloured_rectangles;
  3503. if (mMeshes[i].coloured_triangles)
  3504. delete [] mMeshes[i].coloured_triangles;
  3505. }
  3506. delete [] mMeshes;
  3507. }
  3508. if (_animations)
  3509. delete [] _animations;
  3510. if (_state_changes)
  3511. delete _state_changes;
  3512. //delete [] _state_changes;
  3513. numMoveablesTR5 = 0;
  3514. if (moveablesTR5)
  3515. delete [] moveablesTR5;
  3516. numAnimationsTR5 = 0;
  3517. if (animationsTR5)
  3518. delete [] animationsTR5;
  3519. numObjectTexturesTR5 = 0;
  3520. if (objectTexturesTR5)
  3521. delete [] objectTexturesTR5;
  3522. numCinematicFramesTR5 = 0;
  3523. if (cinematicFramesTR5)
  3524. delete [] cinematicFramesTR5;
  3525. numFlyByCamerasTR5 = 0;
  3526. if (flyByCamerasTR5)
  3527. delete [] flyByCamerasTR5;
  3528. // Texture use
  3529. if (_tex_special)
  3530. delete [] _tex_special;
  3531. if (_textile8)
  3532. delete [] _textile8;
  3533. if (_textile16)
  3534. delete [] _textile16;
  3535. if (_textile32)
  3536. delete [] _textile32;
  3537. //! \fixme Damaged memory causes delete to not set pointer to NULL?
  3538. _tex_special = NULL;
  3539. _textile8 = NULL;
  3540. _textile16 = NULL;
  3541. _textile32 = NULL;
  3542. // Compressed level use
  3543. if (mCompressedLevelData)
  3544. delete [] mCompressedLevelData;
  3545. mCompressedLevelDataOffset = 0;
  3546. mCompressedLevelSize = 0;
  3547. mFreadMode = TR_FREAD_NORMAL;
  3548. // Clear out vars
  3549. mNumTR4Samples = 0;
  3550. mPakVersion = 0;
  3551. mEngineVersion = TR_VERSION_UNKNOWN;
  3552. mNumSampleIndices = 0;
  3553. mNumSoundDetails = 0;
  3554. mRiffAlternateLoaded = false;
  3555. _num_floor_data = 0;
  3556. _num_textiles = 0;
  3557. _num_tex_special = 0;
  3558. _num_room_textures = 0;
  3559. _num_misc_textures = 0;
  3560. _num_bump_map_textures = 0;
  3561. _unknown_t = 0;
  3562. _num_rooms = 0;
  3563. _num_anim_dispatches = 0;
  3564. mMeshCount = 0;
  3565. _num_state_changes = 0;
  3566. _num_animations = 0;
  3567. _num_anim_commands = 0;
  3568. _num_mesh_trees = 0;
  3569. _num_frames = 0;
  3570. _num_moveables = 0;
  3571. _num_demo_data = 0;
  3572. _num_cinematic_frames = 0;
  3573. _num_items = 0;
  3574. _num_animated_textures = 0;
  3575. _num_cameras = 0;
  3576. _num_sound_sources = 0;
  3577. _num_boxes = 0;
  3578. _num_static_meshes = 0;
  3579. _num_object_textures = 0;
  3580. _num_sprite_textures = 0;
  3581. _num_sprite_sequences = 0;
  3582. _num_overlaps = 0;
  3583. mReset = true;
  3584. }
  3585. void TombRaider::setDebug(bool toggle)
  3586. {
  3587. mDebug = toggle;
  3588. }
  3589. void TombRaider::setRoomVertexLightingFactor(float f)
  3590. {
  3591. mRoomVertexLightingFactor = f;
  3592. }
  3593. void TombRaider::setTexelScalingFactor(float f)
  3594. {
  3595. mTexelScale = f;
  3596. }
  3597. ////////////////////////////////////////////////////////////
  3598. // Private Accessors
  3599. ////////////////////////////////////////////////////////////
  3600. void TombRaider::extractMeshes(unsigned char *mesh_data,
  3601. unsigned int num_mesh_pointers,
  3602. unsigned int *mesh_pointers)
  3603. {
  3604. unsigned int size, i;
  3605. unsigned char *mesh_pointer;
  3606. int negative_size;
  3607. /* Alloc space for mesh */
  3608. mMeshCount = num_mesh_pointers;
  3609. printDebug("ExtractMeshes", "mMeshCount = %u", mMeshCount);
  3610. mMeshes = new tr2_mesh_t[mMeshCount];
  3611. printDebug("ExtractMeshes", "num_mesh_pointers = %u", num_mesh_pointers);
  3612. for (i = 0; i < num_mesh_pointers; ++i)
  3613. {
  3614. /* Get mesh start */
  3615. mesh_pointer = &mesh_data[mesh_pointers[i]];
  3616. /* Get Centre + Unknowns */
  3617. memcpy(&mMeshes[i].centre.x, mesh_pointer, 10);
  3618. //! \fixme endian
  3619. // depending on the interpretation of the unknowns that follow the Centre
  3620. // element, more endian conversion may be necessary
  3621. mesh_pointer += 10;
  3622. /* Get number of vertices */
  3623. memcpy(&mMeshes[i].num_vertices, mesh_pointer, 2);
  3624. //! \fixme endian
  3625. printDebug("ExtractMeshes", "mMeshes[%i].num_vertices = %u",
  3626. i, mMeshes[i].num_vertices);
  3627. mesh_pointer += sizeof(unsigned short);
  3628. mMeshes[i].num_vertices = (short)abs(mMeshes[i].num_vertices);
  3629. /* Get vertex list */
  3630. size = sizeof(tr2_vertex_t) * mMeshes[i].num_vertices;
  3631. mMeshes[i].vertices = 0x0;
  3632. if (mMeshes[i].num_vertices > 0)
  3633. mMeshes[i].vertices = new tr2_vertex_t[mMeshes[i].num_vertices];
  3634. memcpy(mMeshes[i].vertices, mesh_pointer, size);
  3635. //! \fixme endian
  3636. mesh_pointer += size;
  3637. /* Get number of normals */
  3638. memcpy(&mMeshes[i].num_normals, mesh_pointer, sizeof(unsigned short));
  3639. //! \fixme endian
  3640. mesh_pointer += sizeof(unsigned short);
  3641. negative_size = (mMeshes[i].num_normals < 0);
  3642. mMeshes[i].num_normals = (short)abs(mMeshes[i].num_normals);
  3643. printDebug("ExtractMeshes", "negative_size = %u", negative_size);
  3644. mMeshes[i].mesh_lights = 0x0;
  3645. mMeshes[i].normals = 0x0;
  3646. /* Get normal list */
  3647. if (negative_size)
  3648. {
  3649. negative_size = 0;
  3650. size = mMeshes[i].num_normals * sizeof(unsigned short);
  3651. mMeshes[i].mesh_lights = 0x0;
  3652. if (mMeshes[i].num_normals > 0)
  3653. mMeshes[i].mesh_lights = new short[mMeshes[i].num_normals];
  3654. memcpy(mMeshes[i].mesh_lights, mesh_pointer, size);
  3655. }
  3656. else
  3657. {
  3658. size = sizeof(tr2_vertex_t) * mMeshes[i].num_normals;
  3659. mMeshes[i].normals = 0x0;
  3660. if (mMeshes[i].num_normals > 0)
  3661. mMeshes[i].normals = new tr2_vertex_t[mMeshes[i].num_normals];
  3662. memcpy(mMeshes[i].normals, mesh_pointer, size);
  3663. }
  3664. //! \fixme endian
  3665. mesh_pointer += size;
  3666. /* Get number of textured rectangles */
  3667. memcpy(&mMeshes[i].num_textured_rectangles,
  3668. mesh_pointer, sizeof(unsigned short));
  3669. //! \fixme endian
  3670. mesh_pointer += sizeof(unsigned short);
  3671. mMeshes[i].num_textured_rectangles =
  3672. (short)abs(mMeshes[i].num_textured_rectangles);
  3673. size = sizeof(tr2_quad_t) * mMeshes[i].num_textured_rectangles;
  3674. mMeshes[i].textured_rectangles = 0x0;
  3675. if (mMeshes[i].num_textured_rectangles > 0)
  3676. mMeshes[i].textured_rectangles =
  3677. new tr2_quad_t[mMeshes[i].num_textured_rectangles];
  3678. printDebug("ExtractMeshes", "mMeshes[%i].num_textured_rectangles = %u",
  3679. i, mMeshes[i].num_textured_rectangles);
  3680. /* Get list of textured rectangles */
  3681. if (mMeshes[i].num_textured_rectangles > 0)
  3682. {
  3683. if (mEngineVersion == TR_VERSION_4)
  3684. {
  3685. int j;
  3686. for (j = 0; j < mMeshes[i].num_textured_rectangles; ++j)
  3687. {
  3688. memcpy(&mMeshes[i].textured_rectangles[j],
  3689. mesh_pointer, sizeof(tr2_quad_t));
  3690. mesh_pointer += sizeof(tr2_quad_t) + sizeof(unsigned short);
  3691. }
  3692. }
  3693. else
  3694. {
  3695. memcpy(mMeshes[i].textured_rectangles, mesh_pointer, size);
  3696. }
  3697. //! \fixme endian
  3698. if (mEngineVersion != TR_VERSION_4)
  3699. mesh_pointer += size;
  3700. }
  3701. /* Get number of textured triangles */
  3702. memcpy(&mMeshes[i].num_textured_triangles,
  3703. mesh_pointer, sizeof(unsigned short));
  3704. //! \fixme endian
  3705. mesh_pointer += sizeof(unsigned short);
  3706. mMeshes[i].num_textured_triangles =
  3707. (short)abs(mMeshes[i].num_textured_triangles);
  3708. size = sizeof(tr2_tri_t) * mMeshes[i].num_textured_triangles;
  3709. //if (mEngineVersion == TR_VERSION_4)
  3710. // size += 2 * mMeshes[i].num_textured_triangles;
  3711. mMeshes[i].textured_triangles = 0x0;
  3712. if (mMeshes[i].num_textured_triangles > 0)
  3713. {
  3714. mMeshes[i].textured_triangles =
  3715. new tr2_tri_t[mMeshes[i].num_textured_triangles];
  3716. }
  3717. printDebug("ExtractMeshes", " mMeshes[%i].num_textured_triangles = %u",
  3718. i, mMeshes[i].num_textured_triangles);
  3719. /* Get list of textured triangles */
  3720. if (mMeshes[i].num_textured_triangles > 0)
  3721. {
  3722. if (mEngineVersion == TR_VERSION_4)
  3723. {
  3724. int j;
  3725. for (j = 0; j < mMeshes[i].num_textured_triangles; ++j)
  3726. {
  3727. memcpy(&mMeshes[i].textured_triangles[j],
  3728. mesh_pointer, sizeof(tr2_tri_t));
  3729. mesh_pointer += sizeof(tr2_tri_t) + sizeof(unsigned short);
  3730. }
  3731. }
  3732. else
  3733. {
  3734. memcpy(mMeshes[i].textured_triangles, mesh_pointer, size);
  3735. }
  3736. //! \fixme endian
  3737. if (mEngineVersion != TR_VERSION_4)
  3738. mesh_pointer += size;
  3739. }
  3740. if (mEngineVersion == TR_VERSION_4)
  3741. {
  3742. mMeshes[i].num_coloured_rectangles = 0;
  3743. mMeshes[i].num_coloured_triangles = 0;
  3744. mMeshes[i].coloured_rectangles = 0x0;
  3745. mMeshes[i].coloured_triangles = 0x0;
  3746. // Mongoose 2002.04.04, FIXME is this right?
  3747. mesh_pointer += 2;
  3748. continue;
  3749. }
  3750. /* Get number of coloured rectangles */
  3751. memcpy(&mMeshes[i].num_coloured_rectangles, mesh_pointer,
  3752. sizeof(unsigned short));
  3753. //! \fixme endian
  3754. mesh_pointer += sizeof(unsigned short);
  3755. mMeshes[i].num_coloured_rectangles =
  3756. (short)abs(mMeshes[i].num_coloured_rectangles);
  3757. mMeshes[i].coloured_rectangles = 0x0;
  3758. size = sizeof(tr2_quad_t) * mMeshes[i].num_coloured_rectangles;
  3759. if (mMeshes[i].num_coloured_rectangles > 0)
  3760. mMeshes[i].coloured_rectangles =
  3761. new tr2_quad_t[mMeshes[i].num_coloured_rectangles];
  3762. printDebug("ExtractMeshes", "mMeshes[%i].num_coloured_rectangles = %u",
  3763. i, mMeshes[i].num_coloured_rectangles);
  3764. /* Get list of coloured rectangles */
  3765. if (mMeshes[i].num_coloured_rectangles > 0)
  3766. {
  3767. memcpy(mMeshes[i].coloured_rectangles, mesh_pointer, size);
  3768. //! \fixme endian
  3769. mesh_pointer += size;
  3770. }
  3771. /* Get number of coloured triangles */
  3772. memcpy(&mMeshes[i].num_coloured_triangles, mesh_pointer,
  3773. sizeof(unsigned short));
  3774. //! \fixme endian
  3775. mesh_pointer += sizeof(unsigned short);
  3776. mMeshes[i].num_coloured_triangles =
  3777. (short)abs(mMeshes[i].num_coloured_triangles);
  3778. size = sizeof(tr2_tri_t) * mMeshes[i].num_coloured_triangles;
  3779. mMeshes[i].coloured_triangles = 0x0;
  3780. if (mMeshes[i].num_coloured_triangles > 0)
  3781. mMeshes[i].coloured_triangles =
  3782. new tr2_tri_t[mMeshes[i].num_coloured_triangles];
  3783. printDebug("ExtractMeshes", "mMeshes[%i].num_coloured_triangles = %u",
  3784. i, mMeshes[i].num_coloured_triangles);
  3785. /* Get list of coloured triangles */
  3786. if (mMeshes[i].num_coloured_triangles > 0)
  3787. {
  3788. memcpy(mMeshes[i].coloured_triangles, mesh_pointer, size);
  3789. //! \fixme endian
  3790. mesh_pointer += size;
  3791. }
  3792. }
  3793. }
  3794. int TombRaider::Fread(void *buffer, size_t size, size_t count, FILE *f)
  3795. {
  3796. #ifdef ZLIB_SUPPORT
  3797. int num_read;
  3798. if (mFreadMode == TR_FREAD_COMPRESSED)
  3799. {
  3800. num_read = count;
  3801. num_read *= size;
  3802. if ((mCompressedLevelDataOffset + num_read) <= mCompressedLevelSize)
  3803. {
  3804. memcpy(buffer, &mCompressedLevelData[mCompressedLevelDataOffset],
  3805. num_read);
  3806. mCompressedLevelDataOffset += num_read;
  3807. return count;
  3808. }
  3809. else
  3810. {
  3811. print("Fread(%p, %d, %d, %p)", "ERROR: Returned %d bytes too far",
  3812. buffer, size, count, f, num_read);
  3813. reset();
  3814. exit(2);
  3815. }
  3816. }
  3817. #endif
  3818. unsigned int offset = ftell(f);
  3819. if (fread(buffer, size, count, f) != count)
  3820. {
  3821. print("Fread", "ERROR: Failed fread. Should Abort. @ 0x%x", offset);
  3822. reset();
  3823. exit(2);
  3824. // return -1; // Unreachable anyways
  3825. }
  3826. return count;
  3827. }
  3828. void TombRaider::getRiffData(unsigned int *bytes, unsigned char **data)
  3829. {
  3830. *bytes = 0;
  3831. *data = 0x0;
  3832. if (mRiffDataSz)
  3833. {
  3834. *bytes = mRiffDataSz;
  3835. *data = new unsigned char[mRiffDataSz];
  3836. memcpy(*data, mRiffData, mRiffDataSz);
  3837. }
  3838. }
  3839. void TombRaider::getRiffDataTR4(unsigned int index,
  3840. unsigned int *bytes, unsigned char **data)
  3841. {
  3842. *bytes = 0;
  3843. *data = 0x0;
  3844. if (index < mNumTR4Samples)
  3845. {
  3846. *bytes = mTR4SamplesSz[index];
  3847. *data = new unsigned char[*bytes];
  3848. memcpy(*data, mTR4Samples[index], *bytes);
  3849. }
  3850. }
  3851. int TombRaider::getRiffOffsets(unsigned char *riffData,
  3852. unsigned int riffDataBytes,
  3853. unsigned int **offsets,
  3854. unsigned int numOffsets)
  3855. {
  3856. unsigned int i, j = 0, riffCount, state;
  3857. *offsets = new unsigned int[numOffsets];
  3858. for (i = 0, riffCount = 0, state = 4; i < riffDataBytes; ++i)
  3859. {
  3860. switch (riffData[i])
  3861. {
  3862. case 'R':
  3863. if (state == 4)
  3864. {
  3865. j = i; // tmp offset guess
  3866. state = 0;
  3867. continue;
  3868. }
  3869. break;
  3870. case 'I':
  3871. if (state == 0)
  3872. {
  3873. state = 1;
  3874. continue;
  3875. }
  3876. break;
  3877. case 'F':
  3878. if (state == 1)
  3879. {
  3880. state = 2;
  3881. continue;
  3882. }
  3883. else if (state == 2)
  3884. {
  3885. state = 3;
  3886. // Found RIFF header, but we can only report
  3887. // riffCount offsets ( buffer size limits )
  3888. if (riffCount < numOffsets)
  3889. {
  3890. (*offsets)[riffCount] = j;
  3891. }
  3892. ++riffCount;
  3893. continue;
  3894. }
  3895. break;
  3896. }
  3897. state = 4;
  3898. }
  3899. return riffCount;
  3900. }
  3901. unsigned char *TombRaider::getTexTile(int texture)
  3902. {
  3903. unsigned char *image;
  3904. unsigned int color;
  3905. int j, k, index, offset;
  3906. int xmin, xmax, ymin, ymax, x, y;
  3907. image = NULL;
  3908. if (texture >=0 && texture < (int)_num_textiles)
  3909. {
  3910. image = new unsigned char[256*256*4];
  3911. memset(image, 0, 256*256*4);
  3912. if (_textile32)
  3913. {
  3914. // Convert 32bit BGRA image format to 32bit RGBA
  3915. for (j = 0; j < 256; j++)
  3916. {
  3917. for (k = 0; k < 256; k++)
  3918. {
  3919. index = (j * 256) + k;
  3920. color = _textile32[texture].tile[index];
  3921. index = (j * 1024) + (k * 4);
  3922. image[index + 2] = *((unsigned char *)(&color));
  3923. image[index + 1] = *((unsigned char *)(&color)+1);
  3924. image[index + 0] = *((unsigned char *)(&color)+2);
  3925. image[index + 3] = *((unsigned char *)(&color)+3);
  3926. }
  3927. }
  3928. }
  3929. else
  3930. {
  3931. // Convert 16bit ARGB image format to 32bit RGBA
  3932. for (j = 0; j < 256; j++)
  3933. {
  3934. for (k = 0; k < 256; k++)
  3935. {
  3936. index = (j * 256) + k;
  3937. offset = _textile16[texture].tile[index];
  3938. index = (j * 1024) + (k * 4);
  3939. image[index + 0] = ((offset >> 10) & 0x1f) * 8;
  3940. image[index + 1] = ((offset >> 5) & 0x1f) * 8;
  3941. image[index + 2] = (offset & 0x1f) * 8;
  3942. image[index + 3] = (offset & 0x8000) ? 0xFF : 0;
  3943. }
  3944. }
  3945. }
  3946. switch (Engine())
  3947. {
  3948. case TR_VERSION_4:
  3949. case TR_VERSION_3: // Account for alpha flags
  3950. for (j = 0; j < (int)_num_object_textures; j++)
  3951. {
  3952. //! \fixme This kind of works for lighting - but messes up lara
  3953. #ifdef FIXME
  3954. if (_object_textures[j].tile == texture &&
  3955. _object_textures[j].transparency_flags == 1)
  3956. {
  3957. xmin = 999;
  3958. xmax = 0;
  3959. ymin = 999;
  3960. ymax = 0;
  3961. y = 4;
  3962. // Account for triangles
  3963. if (_object_textures[j].vertices[3].xpixel == 0 &&
  3964. _object_textures[j].vertices[3].ypixel == 0)
  3965. y = 3;
  3966. for (k = 0; k < y; k++)
  3967. {
  3968. if (_object_textures[j].vertices[k].xpixel > xmax)
  3969. xmax = _object_textures[j].vertices[k].xpixel;
  3970. if (_object_textures[j].vertices[k].xpixel < xmin)
  3971. xmin = _object_textures[j].vertices[k].xpixel;
  3972. if (_object_textures[j].vertices[k].ypixel > ymax)
  3973. ymax = _object_textures[j].vertices[k].ypixel;
  3974. if (_object_textures[j].vertices[k].ypixel < ymin)
  3975. ymin = _object_textures[j].vertices[k].ypixel;
  3976. }
  3977. for (x = xmin; x <= xmax; x++)
  3978. {
  3979. for (y = ymin; y <= ymax; y++)
  3980. {
  3981. index = (y * 256) + x;
  3982. offset = _textile16[texture].tile[index];
  3983. index = (y * 1024) + (x * 4);
  3984. image[index + 0] = ((offset >> 10) & 0x1f) * 8;
  3985. image[index + 1] = ((offset >> 5) & 0x1f) * 8;
  3986. image[index + 2] = (offset & 0x1f) * 8;
  3987. // Set transparency to full
  3988. if (offset & 0x8000)
  3989. {
  3990. image[index + 3] = 0x00;
  3991. }
  3992. }
  3993. }
  3994. }
  3995. else
  3996. #endif
  3997. if (_object_textures[j].tile == texture &&
  3998. _object_textures[j].transparency_flags == 2)
  3999. {
  4000. xmin = 999;
  4001. xmax = 0;
  4002. ymin = 999;
  4003. ymax = 0;
  4004. y = 4;
  4005. // Account for triangles
  4006. if (_object_textures[j].vertices[3].xpixel == 0 &&
  4007. _object_textures[j].vertices[3].ypixel == 0)
  4008. y = 3;
  4009. for (k = 0; k < y; k++)
  4010. {
  4011. if (_object_textures[j].vertices[k].xpixel > xmax)
  4012. xmax = _object_textures[j].vertices[k].xpixel;
  4013. if (_object_textures[j].vertices[k].xpixel < xmin)
  4014. xmin = _object_textures[j].vertices[k].xpixel;
  4015. if (_object_textures[j].vertices[k].ypixel > ymax)
  4016. ymax = _object_textures[j].vertices[k].ypixel;
  4017. if (_object_textures[j].vertices[k].ypixel < ymin)
  4018. ymin = _object_textures[j].vertices[k].ypixel;
  4019. }
  4020. for (x = xmin; x <= xmax; x++)
  4021. {
  4022. for (y = ymin; y <= ymax; y++)
  4023. {
  4024. if (_textile32)
  4025. {
  4026. index = (y * 256) + x;
  4027. color = _textile32[texture].tile[index];
  4028. index = (y * 1024) + (x * 4);
  4029. image[index + 2] = *((unsigned char *)(&color));
  4030. image[index + 1] = *((unsigned char *)(&color)+1);
  4031. image[index + 0] = *((unsigned char *)(&color)+2);
  4032. image[index + 3] = *((unsigned char *)(&color)+3);
  4033. k = image[index] + image[index + 1] + image[index + 2];
  4034. // Set transparency based upon intensity
  4035. image[index + 3] = (unsigned char)(k / 3);
  4036. }
  4037. else
  4038. {
  4039. index = (y * 256) + x;
  4040. offset = _textile16[texture].tile[index];
  4041. index = (y * 1024) + (x * 4);
  4042. image[index + 0] = ((offset >> 10) & 0x1f) * 8;
  4043. image[index + 1] = ((offset >> 5) & 0x1f) * 8;
  4044. image[index + 2] = (offset & 0x1f) * 8;
  4045. image[index + 3] = (offset & 0x8000) ? 0xFF : 0;
  4046. k = image[index] + image[index + 1] + image[index + 2];
  4047. // Set transparency based upon intensity
  4048. if (offset & 0x8000)
  4049. image[index + 3] = (unsigned char)(k / 3);
  4050. else
  4051. image[index + 3] = 0;
  4052. }
  4053. }
  4054. }
  4055. }
  4056. }
  4057. break;
  4058. case TR_VERSION_1:
  4059. case TR_VERSION_2:
  4060. case TR_VERSION_5:
  4061. case TR_VERSION_UNKNOWN:
  4062. default:
  4063. ;
  4064. }
  4065. }
  4066. return image;
  4067. }
  4068. //! \fixme Move these data about to make full use in the class ;)
  4069. int TombRaider::loadTR5(FILE *f, void (*percent)(int))
  4070. {
  4071. unsigned int level_data_sz, riffOffset, seperator0;
  4072. unsigned int portalOffset, nextRoomOffset, thisRoomOffset;
  4073. int i, j, k;
  4074. u_int16_t us;
  4075. u_int32_t numMeshData, numMeshPointers, u;
  4076. u_int32_t *meshPointers;
  4077. u_int8_t *meshData;
  4078. char check[32];
  4079. if (percent)
  4080. (*percent)(5);
  4081. printDebug("Load", "mEngineVersion = 0x%x", mPakVersion);
  4082. if (mEngineVersion != TR_VERSION_5)
  4083. return -1;
  4084. #ifdef ZLIB_SUPPORT
  4085. unsigned int sz, usz; // compressed and uncompressed size
  4086. unsigned char *compressed_data = NULL;
  4087. int zerr;
  4088. uLongf foo;
  4089. // Read texture type offsets
  4090. Fread(&_num_room_textures, 2, 1, f);
  4091. printDebug("LoadTR5", "_num_room_textures = %u", _num_room_textures);
  4092. Fread(&_num_misc_textures, 2, 1, f);
  4093. printDebug("LoadTR5", "_num_misc_textures = %u", _num_misc_textures);
  4094. Fread(&_num_bump_map_textures, 2, 1, f);
  4095. printDebug("LoadTR5", "_num_bump_map_textures = %u", _num_bump_map_textures);
  4096. // Read the sizes of the 32-bit textures
  4097. Fread(&usz, sizeof(usz), 1, f);
  4098. Fread(&sz, sizeof(sz), 1, f);
  4099. printDebug("LoadTR5", "32-bit textures compressed size = %u bytes", sz);
  4100. printDebug("LoadTR5", "32-bit textures uncompressed size = %u bytes", usz);
  4101. _num_textiles = usz / sizeof(tr2_textile32_t);
  4102. printDebug("LoadTR5", "_num_textiles = %i/%i = %i",
  4103. usz, sizeof(tr2_textile32_t), _num_textiles);
  4104. _textile32 = new tr2_textile32_t[_num_textiles];
  4105. // Allocate a temporary buffer for decompression
  4106. compressed_data = new unsigned char[sz];
  4107. Fread(compressed_data, sz, 1, f);
  4108. // Decompress the textures
  4109. foo = usz;
  4110. zerr = uncompress((unsigned char *)_textile32,
  4111. &foo,
  4112. compressed_data,
  4113. sz);
  4114. usz = foo;
  4115. printDebug("LoadTR5", "textile decompress [%s]",
  4116. (zerr == Z_OK) ? "OK" : "ERROR");
  4117. switch (zerr)
  4118. {
  4119. case Z_MEM_ERROR:
  4120. printDebug("LoadTR5", "There was not enough memory");
  4121. break;
  4122. case Z_BUF_ERROR:
  4123. printDebug("LoadTR5", "There was not enough room in the output buffer");
  4124. break;
  4125. case Z_DATA_ERROR:
  4126. printDebug("LoadTR5", "The input data was corrupted");
  4127. break;
  4128. default:
  4129. printDebug("LoadTR5", "textile decompress %i", zerr);
  4130. }
  4131. // Free the temporary buffer
  4132. delete [] compressed_data;
  4133. // Read in the 16-bit textures, set NumTextiles
  4134. Fread(&usz, sizeof(usz), 1, f);
  4135. Fread(&sz, sizeof(sz), 1, f);
  4136. printDebug("LoadTR5", "16-bit textures compressed size = %u bytes", sz);
  4137. printDebug("LoadTR5", "16-bit textures uncompressed size = %u bytes", usz);
  4138. _num_textiles = usz / sizeof(tr2_textile16_t);
  4139. printDebug("LoadTR5", "_num_textiles = %i/%i = %i",
  4140. usz, sizeof(tr2_textile16_t), _num_textiles);
  4141. _textile16 = new tr2_textile16_t[_num_textiles];
  4142. // Allocate a temporary buffer for decompression
  4143. compressed_data = new unsigned char[sz];
  4144. Fread(compressed_data, sz, 1, f);
  4145. // Decompress the textures
  4146. foo = usz;
  4147. zerr = uncompress((unsigned char *)_textile16,
  4148. &foo,
  4149. compressed_data,
  4150. sz);
  4151. usz = foo;
  4152. if (percent)
  4153. (*percent)(7);
  4154. printDebug("LoadTR5", "textile decompress [%s]",
  4155. (zerr == Z_OK) ? "OK" : "ERROR");
  4156. switch (zerr)
  4157. {
  4158. case Z_MEM_ERROR:
  4159. printDebug("LoadTR5", "There was not enough memory");
  4160. break;
  4161. case Z_BUF_ERROR:
  4162. printDebug("LoadTR5", "There was not enough room in the output buffer");
  4163. break;
  4164. case Z_DATA_ERROR:
  4165. printDebug("LoadTR5", "The input data was corrupted");
  4166. break;
  4167. default:
  4168. printDebug("LoadTR5", "textile decompress %i", zerr);
  4169. }
  4170. // Free the temporary buffer
  4171. delete [] compressed_data;
  4172. // Read the sizes of the sprite textures
  4173. Fread(&usz, sizeof(usz), 1, f);
  4174. Fread(&sz, sizeof(sz), 1, f);
  4175. printDebug("LoadTR5", "sprite textures compressed size = %u bytes", sz);
  4176. printDebug("LoadTR5", "sprite textures uncompressed size = %u bytes", usz);
  4177. // Load sprite/bump map/gui/etc textures also
  4178. _num_tex_special = usz/(256*256*4);
  4179. printDebug("LoadTR5", "_num_tex_special = %i/%i = %i",
  4180. usz, 256*256*4, _num_tex_special);
  4181. printDebug("LoadTR5", "Reading %ibytes of sprite textures", usz);
  4182. if (usz)
  4183. {
  4184. _tex_special = new unsigned char[usz];
  4185. // Allocate a temporary buffer for decompression
  4186. compressed_data = new unsigned char[sz];
  4187. Fread(compressed_data, sz, 1, f);
  4188. // Decompress the textures
  4189. foo = usz;
  4190. zerr = uncompress(_tex_special,
  4191. &foo,
  4192. compressed_data,
  4193. sz);
  4194. usz = foo;
  4195. printDebug("LoadTR5", "special texture decompress [%s]",
  4196. (zerr == Z_OK) ? "OK" : "ERROR");
  4197. switch (zerr)
  4198. {
  4199. case Z_MEM_ERROR:
  4200. printDebug("LoadTR5", "There was not enough memory");
  4201. break;
  4202. case Z_BUF_ERROR:
  4203. printDebug("LoadTR5", "There was not enough room in the output buffer");
  4204. break;
  4205. case Z_DATA_ERROR:
  4206. printDebug("LoadTR5", "The input data was corrupted");
  4207. break;
  4208. default:
  4209. printDebug("LoadTR5", "textile decompress %i", zerr);
  4210. }
  4211. // Free the temporary buffer
  4212. delete [] compressed_data;
  4213. }
  4214. #else
  4215. Print("LoadTR5> ERROR: TR5 support not compiled in this build.");
  4216. Print("LoadTR5> Requires zlib-devel.");
  4217. return -1;
  4218. #endif
  4219. if (percent)
  4220. (*percent)(10);
  4221. // Mongoose 2002.01.08, Michiel has discovered the
  4222. // first 4 bytes here are 2 bitu16 flags for Lara type and weather
  4223. u_int16_t laraType, weather;
  4224. Fread(&laraType, 2, 1, f);
  4225. printDebug("LoadTR5", "laraType = 0x%x", laraType);
  4226. Fread(&weather, 2, 1, f);
  4227. printDebug("LoadTR5", "weather = 0x%x", weather);
  4228. printDebug("LoadTR5", "skipping 28bytes of unknowns");
  4229. Fread(&seperator0, 4, 1, f);
  4230. printDebug("LoadTR5", "0x%x", seperator0);
  4231. Fread(&seperator0, 4, 1, f);
  4232. printDebug("LoadTR5", "0x%x", seperator0);
  4233. Fread(&seperator0, 4, 1, f);
  4234. printDebug("LoadTR5", "0x%x", seperator0);
  4235. Fread(&seperator0, 4, 1, f);
  4236. printDebug("LoadTR5", "0x%x", seperator0);
  4237. Fread(&seperator0, 4, 1, f);
  4238. printDebug("LoadTR5", "0x%x", seperator0);
  4239. Fread(&seperator0, 4, 1, f);
  4240. printDebug("LoadTR5", "0x%x", seperator0);
  4241. Fread(&seperator0, 4, 1, f);
  4242. printDebug("LoadTR5", "0x%x", seperator0);
  4243. Fread(&level_data_sz, 4, 1, f);
  4244. printDebug("LoadTR5", "Level data size = %u", level_data_sz);
  4245. Fread(&riffOffset, 4, 1, f);
  4246. printDebug("LoadTR5", "Same as last, also offset to RIFFs = %u",
  4247. riffOffset);
  4248. Fread(&seperator0, 4, 1, f);
  4249. printDebug("LoadTR5", "Seperator, always 0x00? = %u", seperator0);
  4250. Fread(&_num_rooms, 4, 1, f);
  4251. printDebug("LoadTR5", "_num_rooms = %u", _num_rooms);
  4252. mRoomsTR5 = new tr5_room_t[_num_rooms];
  4253. for (i = 0; i < _num_rooms; ++i)
  4254. {
  4255. if (percent)
  4256. (*percent)(11 + (int)(((float)i/(float)_num_rooms)*90.0));
  4257. thisRoomOffset = ftell(f);
  4258. Fread(&mRoomsTR5[i].checkXELA, 4, 1, f);
  4259. printDebug("LoadTR5", "room[%i].checkXELA (0x414c4558)? = 0x%x",
  4260. i, mRoomsTR5[i].checkXELA);
  4261. if (mRoomsTR5[i].checkXELA != 0x414c4558)
  4262. {
  4263. print("LoadTR5", "Error #1 room[%i].checkXELA (0x414c4558) != 0x%x",
  4264. i, mRoomsTR5[i].checkXELA);
  4265. return -3;
  4266. }
  4267. Fread(&mRoomsTR5[i].roomDataSize, 4, 1, f);
  4268. printDebug("LoadTR5", "offset to next room = %u",
  4269. mRoomsTR5[i].roomDataSize);
  4270. nextRoomOffset = ftell(f) + mRoomsTR5[i].roomDataSize;
  4271. Fread(&mRoomsTR5[i].seperator, 4, 1, f);
  4272. printDebug("LoadTR5", "room[%i].seperator, CDCDCDCD = %X?",
  4273. i, mRoomsTR5[i].seperator);
  4274. if (mRoomsTR5[i].seperator != 0xcdcdcdcd)
  4275. {
  4276. print("LoadTR5", "Error #2 room[%i].seperator, CDCDCDCD != 0x%X",
  4277. i, mRoomsTR5[i].seperator);
  4278. return -3;
  4279. }
  4280. // Start 60byte struct /////////////
  4281. printDebug("LoadTR5", "60byte struct {");
  4282. // Often start of "XELA" +216 + ublock1 = FD end,
  4283. // but have seen 0xffffffff (-1). Better using next data
  4284. // and compute FD size the old way of X*Z*8
  4285. Fread(&mRoomsTR5[i].endSDOffset, 4, 1, f);
  4286. printDebug("LoadTR5", "%u + 216 + XELA start = FD end",
  4287. mRoomsTR5[i].endSDOffset);
  4288. // Start of "XELA" + 216 + ublock2 = FD start
  4289. Fread(&mRoomsTR5[i].startSDOffset, 4, 1, f);
  4290. printDebug("LoadTR5", "%u + 216 + XELA start = FD start",
  4291. mRoomsTR5[i].startSDOffset);
  4292. Fread(&mRoomsTR5[i].seperator2, 4, 1, f);
  4293. printDebug("LoadTR5", "seperator2 = %u", mRoomsTR5[i].seperator2);
  4294. if (mRoomsTR5[i].seperator2 != 0xcdcdcdcd &&
  4295. mRoomsTR5[i].seperator2 != 0x00000000)
  4296. {
  4297. print("LoadTR5", "Error #3 CDCDCDCD | 0x0 != 0x%x",
  4298. mRoomsTR5[i].seperator2);
  4299. return -3;
  4300. }
  4301. // Possibly start of "XELA" + 216 + ublock4 = end portals
  4302. Fread(&mRoomsTR5[i].endPortalOffset, 4, 1, f);
  4303. printDebug("LoadTR5", "%u + 216 + XELA start = end portals",
  4304. mRoomsTR5[i].endPortalOffset);
  4305. Fread(&mRoomsTR5[i].x, 4, 1, f);
  4306. printDebug("LoadTR5", "room.x = %u", mRoomsTR5[i].x);
  4307. Fread(&mRoomsTR5[i].seperator3, 4, 1, f);
  4308. printDebug("LoadTR5", "0x00000000 = %u ?", mRoomsTR5[i].seperator3);
  4309. Fread(&mRoomsTR5[i].z, 4, 1, f);
  4310. printDebug("LoadTR5", "room.z = %u", mRoomsTR5[i].z);
  4311. Fread(&mRoomsTR5[i].yBottom, 4, 1, f);
  4312. printDebug("LoadTR5", "room.y_bottom = %u", mRoomsTR5[i].yBottom);
  4313. Fread(&mRoomsTR5[i].yTop, 4, 1, f);
  4314. printDebug("LoadTR5", "room.y_top = %u", mRoomsTR5[i].yTop);
  4315. Fread(&mRoomsTR5[i].numZSectors, 2, 1, f);
  4316. printDebug("LoadTR5", "num_z_sectors = %i", mRoomsTR5[i].numZSectors);
  4317. Fread(&mRoomsTR5[i].numXSectors, 2, 1, f);
  4318. printDebug("LoadTR5", "num_x_sectors = %i", mRoomsTR5[i].numXSectors);
  4319. Fread(&mRoomsTR5[i].roomAmbientColor, 4, 1, f);
  4320. printDebug("LoadTR5", "room_ambient_color = 0x%x",
  4321. mRoomsTR5[i].roomAmbientColor);
  4322. Fread(&mRoomsTR5[i].numRoomLights, 2, 1, f);
  4323. printDebug("LoadTR5", "num_lights = %i", mRoomsTR5[i].numRoomLights);
  4324. Fread(&mRoomsTR5[i].numStaticMeshes, 2, 1, f);
  4325. printDebug("LoadTR5", "num_static_meshes = %i",
  4326. mRoomsTR5[i].numStaticMeshes);
  4327. Fread(&mRoomsTR5[i].unknownR1, 2, 1, f);
  4328. printDebug("LoadTR5", "unknown 0x0001? = 0x%x", mRoomsTR5[i].unknownR1);
  4329. Fread(&mRoomsTR5[i].unknownR2, 2, 1, f);
  4330. printDebug("LoadTR5", "unknown 0x0000? = 0x%x", mRoomsTR5[i].unknownR2);
  4331. Fread(&mRoomsTR5[i].filler, 4, 1, f);
  4332. printDebug("LoadTR5", "Always 0x7fff? = 0x%x", mRoomsTR5[i].filler);
  4333. Fread(&mRoomsTR5[i].filler2, 4, 1, f);
  4334. printDebug("LoadTR5", "Always 0x7fff? = 0x%x", mRoomsTR5[i].filler2);
  4335. printDebug("LoadTR5", "}");
  4336. // End 60byte structure /////////////////
  4337. Fread(&mRoomsTR5[i].seperator4, 4, 1, f);
  4338. printDebug("LoadTR5", "seperator4 CDCDCDCD = 0x%x",
  4339. mRoomsTR5[i].seperator4);
  4340. if (mRoomsTR5[i].seperator4 != 0xcdcdcdcd)
  4341. {
  4342. print("LoadTR5", "Error #5 CDCDCDCD != 0x%x",
  4343. mRoomsTR5[i].seperator4);
  4344. return -3;
  4345. }
  4346. Fread(&mRoomsTR5[i].seperator5, 4, 1, f);
  4347. printDebug("LoadTR5", "seperator5 CDCDCDCD = 0x%x",
  4348. mRoomsTR5[i].seperator5);
  4349. if (mRoomsTR5[i].seperator5 != 0xcdcdcdcd)
  4350. {
  4351. print("LoadTR5", "Error #6 CDCDCDCD != 0x%x",
  4352. mRoomsTR5[i].seperator5);
  4353. return -3;
  4354. }
  4355. // Start 20byte structure ///////////////
  4356. printDebug("LoadTR5", "20byte struct {");
  4357. Fread(mRoomsTR5[i].seperator6, 6, 1, f);
  4358. printDebug("LoadTR5", "6 bytes 0xFF = 0x%x%x%x%x%x",
  4359. mRoomsTR5[i].seperator6[0], mRoomsTR5[i].seperator6[1],
  4360. mRoomsTR5[i].seperator6[2], mRoomsTR5[i].seperator6[3],
  4361. mRoomsTR5[i].seperator6[4], mRoomsTR5[i].seperator6[5]);
  4362. Fread(&mRoomsTR5[i].roomFlag, 2, 1, f);
  4363. printDebug("LoadTR5", "room_flag = %i", mRoomsTR5[i].roomFlag);
  4364. Fread(&mRoomsTR5[i].unknownR5, 2, 1, f);
  4365. printDebug("LoadTR5", "unknown = %i", mRoomsTR5[i].unknownR5);
  4366. Fread(mRoomsTR5[i].seperator7, 10, 1, f);
  4367. printDebug("LoadTR5", "10 bytes 0x00 = 0x%x%x%x%x%x%x%x%x%x%x",
  4368. mRoomsTR5[i].seperator7[0], mRoomsTR5[i].seperator7[1],
  4369. mRoomsTR5[i].seperator7[2], mRoomsTR5[i].seperator7[3],
  4370. mRoomsTR5[i].seperator7[4], mRoomsTR5[i].seperator7[5],
  4371. mRoomsTR5[i].seperator7[6], mRoomsTR5[i].seperator7[7],
  4372. mRoomsTR5[i].seperator7[8], mRoomsTR5[i].seperator7[9]);
  4373. printDebug("LoadTR5", "}");
  4374. // End 20byte structure /////////////////
  4375. Fread(&mRoomsTR5[i].seperator8, 4, 1, f);
  4376. printDebug("LoadTR5", "seperator8 CDCDCDCD = 0x%x",
  4377. mRoomsTR5[i].seperator8);
  4378. if (mRoomsTR5[i].seperator8 != 0xcdcdcdcd)
  4379. {
  4380. print("LoadTR5", "Error #9 CDCDCDCD != 0x%x",
  4381. mRoomsTR5[i].seperator8);
  4382. return -3;
  4383. }
  4384. printDebug("LoadTR5", "16byte struct {");
  4385. Fread(&mRoomsTR5[i].unknownR6, 4, 1, f);
  4386. printDebug("LoadTR5", "unknownR6 = %i", mRoomsTR5[i].unknownR6);
  4387. Fread(&mRoomsTR5[i].roomX, 4, 1, f);
  4388. printDebug("LoadTR5", "roomX = %i", mRoomsTR5[i].roomX);
  4389. Fread(&mRoomsTR5[i].seperator9, 4, 1, f);
  4390. printDebug("LoadTR5", "seperator9 CDCDCDCD | 0x0 = 0x%x",
  4391. mRoomsTR5[i].seperator9);
  4392. if (mRoomsTR5[i].seperator9 != 0xcdcdcdcd &&
  4393. mRoomsTR5[i].seperator9 != 0x0)
  4394. {
  4395. print("LoadTR5", "Error #10 CDCDCDCD | 0x0 != 0x%x",
  4396. mRoomsTR5[i].seperator9);
  4397. return -3;
  4398. }
  4399. Fread(&mRoomsTR5[i].roomZ, 4, 1, f);
  4400. printDebug("LoadTR5", "roomZ = %i", mRoomsTR5[i].roomZ);
  4401. printDebug("LoadTR5", "}");
  4402. Fread(&mRoomsTR5[i].seperator10, 4, 1, f);
  4403. printDebug("LoadTR5", "seperator10 CDCDCDCD = 0x%x",
  4404. mRoomsTR5[i].seperator10);
  4405. if (mRoomsTR5[i].seperator10 != 0xcdcdcdcd)
  4406. {
  4407. print("LoadTR5", "Error #11 CDCDCDCD != 0x%x",
  4408. mRoomsTR5[i].seperator10);
  4409. return -3;
  4410. }
  4411. Fread(&mRoomsTR5[i].seperator11, 4, 1, f);
  4412. printDebug("LoadTR5", "seperator11 CDCDCDCD = 0x%x",
  4413. mRoomsTR5[i].seperator11);
  4414. if (mRoomsTR5[i].seperator11 != 0xcdcdcdcd)
  4415. {
  4416. print("LoadTR5", "Error #12 CDCDCDCD != 0x%x",
  4417. mRoomsTR5[i].seperator11);
  4418. return -3;
  4419. }
  4420. Fread(&mRoomsTR5[i].seperator12, 4, 1, f);
  4421. printDebug("LoadTR5", "seperator12 CDCDCDCD = 0x%x",
  4422. mRoomsTR5[i].seperator12);
  4423. if (mRoomsTR5[i].seperator12 != 0xcdcdcdcd)
  4424. {
  4425. print("LoadTR5", "Error #13 CDCDCDCD != 0x%x",
  4426. mRoomsTR5[i].seperator12);
  4427. return -3;
  4428. }
  4429. Fread(&mRoomsTR5[i].seperator13, 4, 1, f);
  4430. printDebug("LoadTR5", "seperator13 CDCDCDCD = 0x%x",
  4431. mRoomsTR5[i].seperator13);
  4432. if (mRoomsTR5[i].seperator13 != 0xcdcdcdcd)
  4433. {
  4434. print("LoadTR5", "Error #14 CDCDCDCD | 0x0 != 0x%x",
  4435. mRoomsTR5[i].seperator13);
  4436. return -3;
  4437. }
  4438. Fread(&mRoomsTR5[i].seperator14, 4, 1, f);
  4439. printDebug("LoadTR5", "seperator14 CDCDCDCD = 0x%x",
  4440. mRoomsTR5[i].seperator14);
  4441. if (mRoomsTR5[i].seperator14 != 0xcdcdcdcd &&
  4442. mRoomsTR5[i].seperator14 != 0x00000000)
  4443. {
  4444. print("LoadTR5", "Error #15 CDCDCDCD | 0x0 != 0x%x",
  4445. mRoomsTR5[i].seperator14);
  4446. return -3;
  4447. }
  4448. Fread(&mRoomsTR5[i].seperator15, 4, 1, f);
  4449. printDebug("LoadTR5", "seperator15 CDCDCDCD = 0x%x",
  4450. mRoomsTR5[i].seperator15);
  4451. if (mRoomsTR5[i].seperator15 != 0xcdcdcdcd)
  4452. {
  4453. print("LoadTR5", "Error #16 CDCDCDCD != 0x%x",
  4454. mRoomsTR5[i].seperator15);
  4455. return -3;
  4456. }
  4457. // 56byte struct /////////////
  4458. printDebug("LoadTR5", "56byte struct {");
  4459. Fread(&mRoomsTR5[i].numRoomTriangles, 4, 1, f);
  4460. printDebug("LoadTR5", "num_triangles = %u",
  4461. mRoomsTR5[i].numRoomTriangles);
  4462. Fread(&mRoomsTR5[i].numRoomRectangles, 4, 1, f);
  4463. printDebug("LoadTR5", "num_rectangles = %u",
  4464. mRoomsTR5[i].numRoomRectangles);
  4465. Fread(&mRoomsTR5[i].seperator16, 4, 1, f);
  4466. printDebug("LoadTR5", "seperator16, 0x00? = 0x%x",
  4467. mRoomsTR5[i].seperator16);
  4468. // Num lights * 88bytes
  4469. Fread(&mRoomsTR5[i].lightSize, 4, 1, f);
  4470. printDebug("LoadTR5", "light_size = %u", mRoomsTR5[i].lightSize);
  4471. Fread(&mRoomsTR5[i].numTotalRoomLights, 4, 1, f);
  4472. printDebug("LoadTR5", "num_lights = %u",
  4473. mRoomsTR5[i].numTotalRoomLights);
  4474. Fread(&mRoomsTR5[i].unknownR7, 4, 1, f); // was num_unknown_36byte structs to read
  4475. printDebug("LoadTR5", "unknownR7 structs = %u", mRoomsTR5[i].unknownR7);
  4476. Fread(&mRoomsTR5[i].unknownR8, 4, 1, f);
  4477. printDebug("LoadTR5", "unknownR8 = 0x%x", mRoomsTR5[i].unknownR8);
  4478. Fread(&mRoomsTR5[i].yBottom, 4, 1, f);
  4479. printDebug("LoadTR5", "lyBottom = 0x%x", mRoomsTR5[i].lyBottom);
  4480. Fread(&mRoomsTR5[i].numLayers, 4, 1, f);
  4481. printDebug("LoadTR5", "num_layers = %u", mRoomsTR5[i].numLayers);
  4482. Fread(&mRoomsTR5[i].layerOffset, 4, 1, f);
  4483. printDebug("LoadTR5", "layerOffset = 0x%x", mRoomsTR5[i].layerOffset);
  4484. Fread(&mRoomsTR5[i].verticesOffset, 4, 1, f);
  4485. printDebug("LoadTR5", "verticesOffset = 0x%x", mRoomsTR5[i].verticesOffset);
  4486. Fread(&mRoomsTR5[i].polyOffset, 4, 1, f);
  4487. printDebug("LoadTR5", "polyOffset = 0x%x", mRoomsTR5[i].polyOffset);
  4488. Fread(&mRoomsTR5[i].polyOffset2, 4, 1, f);
  4489. printDebug("LoadTR5", "polyOffset2 = 0x%x", mRoomsTR5[i].polyOffset2);
  4490. Fread(&mRoomsTR5[i].verticesSize, 4, 1, f);
  4491. printDebug("LoadTR5", "verticesSize = 0x%x", mRoomsTR5[i].verticesSize);
  4492. printDebug("LoadTR5", "}");
  4493. //////////////////////////////
  4494. Fread(&mRoomsTR5[i].seperator17, 4, 1, f);
  4495. printDebug("LoadTR5", "seperator17 CDCDCDCD = 0x%x",
  4496. mRoomsTR5[i].seperator17);
  4497. if (mRoomsTR5[i].seperator17 != 0xcdcdcdcd)
  4498. {
  4499. print("LoadTR5", "Error #18 CDCDCDCD != 0x%x",
  4500. mRoomsTR5[i].seperator17);
  4501. return -3;
  4502. }
  4503. Fread(&mRoomsTR5[i].seperator18, 4, 1, f);
  4504. printDebug("LoadTR5", "seperator18 CDCDCDCD = 0x%x",
  4505. mRoomsTR5[i].seperator18);
  4506. if (mRoomsTR5[i].seperator18 != 0xcdcdcdcd)
  4507. {
  4508. print("LoadTR5", "Error #19 CDCDCDCD != 0x%x",
  4509. mRoomsTR5[i].seperator18);
  4510. return -3;
  4511. }
  4512. Fread(&mRoomsTR5[i].seperator19, 4, 1, f);
  4513. printDebug("LoadTR5", "seperator19 CDCDCDCD = 0x%x",
  4514. mRoomsTR5[i].seperator19);
  4515. if (mRoomsTR5[i].seperator19 != 0xcdcdcdcd)
  4516. {
  4517. print("LoadTR5", "Error #20 CDCDCDCD != 0x%x",
  4518. mRoomsTR5[i].seperator19);
  4519. return -3;
  4520. }
  4521. Fread(&mRoomsTR5[i].seperator20, 4, 1, f);
  4522. printDebug("LoadTR5", "seperator20 CDCDCDCD = 0x%x",
  4523. mRoomsTR5[i].seperator20);
  4524. if (mRoomsTR5[i].seperator20 != 0xcdcdcdcd)
  4525. {
  4526. print("LoadTR5", "Error #21 CDCDCDCD != 0x%x",
  4527. mRoomsTR5[i].seperator20);
  4528. return -3;
  4529. }
  4530. // Lights
  4531. printDebug("LoadTR5", "Reading %u lights @ 88bytes each",
  4532. mRoomsTR5[i].numRoomLights);
  4533. if (mRoomsTR5[i].numRoomLights)
  4534. {
  4535. mRoomsTR5[i].lights = new tr5_light_t[mRoomsTR5[i].numRoomLights];
  4536. }
  4537. for (j = 0; j < (int)mRoomsTR5[i].numRoomLights; ++j)
  4538. {
  4539. Fread(&mRoomsTR5[i].lights[j], 88, 1, f);
  4540. //Fread(&mRoomsTR5[i].lights[j].x, 4, 1, f);
  4541. printDebug("LoadTR5", "light[%i].x? = %f", j,
  4542. mRoomsTR5[i].lights[j].x);
  4543. //Fread(&mRoomsTR5[i].lights[j].y, 4, 1, f);
  4544. printDebug("LoadTR5", "light[%i].y? = %f", j,
  4545. mRoomsTR5[i].lights[j].y);
  4546. //Fread(&mRoomsTR5[i].lights[j].z, 4, 1, f);
  4547. printDebug("LoadTR5", "light[%i].z? = %f", j,
  4548. mRoomsTR5[i].lights[j].z);
  4549. //Fread(&mRoomsTR5[i].lights[j].red, 4, 1, f);
  4550. printDebug("LoadTR5", "light[%i].r? = %f",
  4551. j, mRoomsTR5[i].lights[j].red);
  4552. //Fread(&mRoomsTR5[i].lights[j].green, 4, 1, f);
  4553. printDebug("LoadTR5", "light[%i].g? = %f",
  4554. j, mRoomsTR5[i].lights[j].green);
  4555. //Fread(&mRoomsTR5[i].lights[j].blue, 4, 1, f);
  4556. printDebug("LoadTR5", "light[%i].b? = %f",
  4557. j, mRoomsTR5[i].lights[j].blue);
  4558. // 24bytes from start of light
  4559. //Fread(&mRoomsTR5[i].lights[j].seperator, 4, 1, f);
  4560. printDebug("LoadTR5", "CDCDCDCD for some maps? = 0x%8x\t\t[%s]",
  4561. mRoomsTR5[i].lights[j].seperator,
  4562. (mRoomsTR5[i].lights[j].seperator == 0xcdcdcdcd)
  4563. ? "OK" : "ERROR");
  4564. //Fread(&mRoomsTR5[i].lights[j].input, 4, 1, f);
  4565. printDebug("LoadTR5", "light[%i].input = %f",
  4566. j, mRoomsTR5[i].lights[j].input);
  4567. //Fread(&mRoomsTR5[i].lights[j].output, 4, 1, f);
  4568. printDebug("LoadTR5", "light[%i].output = %f",
  4569. j, mRoomsTR5[i].lights[j].output);
  4570. //Fread(&mRoomsTR5[i].lights[j].range, 4, 1, f);
  4571. printDebug("LoadTR5", "light[%i].range = %f",
  4572. j, mRoomsTR5[i].lights[j].range);
  4573. //Fread(&mRoomsTR5[i].lights[j].directionVectorX, 4, 1, f);
  4574. printDebug("LoadTR5", "light[%i].directionVectorX = %f",
  4575. j, mRoomsTR5[i].lights[j].directionVectorX);
  4576. //Fread(&mRoomsTR5[i].lights[j].directionVectorY, 4, 1, f);
  4577. printDebug("LoadTR5", "light[%i].directionVectorY = %f",
  4578. j, mRoomsTR5[i].lights[j].directionVectorY);
  4579. //Fread(&mRoomsTR5[i].lights[j].directionVectorZ, 4, 1, f);
  4580. printDebug("LoadTR5", "light[%i].directionVectorZ = %f",
  4581. j, mRoomsTR5[i].lights[j].directionVectorZ);
  4582. //Fread(&mRoomsTR5[i].lights[j].x2, 4, 1, f);
  4583. printDebug("LoadTR5", "light[%i].x2 = %u",
  4584. j, mRoomsTR5[i].lights[j].x2);
  4585. //Fread(&mRoomsTR5[i].lights[j].y2, 4, 1, f);
  4586. printDebug("LoadTR5", "light[%i].y2 = %u",
  4587. j, mRoomsTR5[i].lights[j].y2);
  4588. //Fread(&mRoomsTR5[i].lights[j].z2, 4, 1, f);
  4589. printDebug("LoadTR5", "light[%i].z2 = %u",
  4590. j, mRoomsTR5[i].lights[j].z2);
  4591. //Fread(&mRoomsTR5[i].lights[j].directionVectorX2, 4, 1, f);
  4592. printDebug("LoadTR5", "light[%i].directionVectorX2 = %u",
  4593. j, mRoomsTR5[i].lights[j].directionVectorX2);
  4594. //Fread(&mRoomsTR5[i].lights[j].directionVectorY2, 4, 1, f);
  4595. printDebug("LoadTR5", "light[%i].directionVectorY2 = %u",
  4596. j, mRoomsTR5[i].lights[j].directionVectorY2);
  4597. //Fread(&mRoomsTR5[i].lights[j].directionVectorZ2, 4, 1, f);
  4598. printDebug("LoadTR5", "light[%i].directionVectorZ2 = %u",
  4599. j, mRoomsTR5[i].lights[j].directionVectorZ2);
  4600. //Fread(&mRoomsTR5[i].lights[j].lightType, 1, 1, f);
  4601. printDebug("LoadTR5", "light[%i].d3d_flag = 0x%x (%s)",
  4602. j, mRoomsTR5[i].lights[j].lightType,
  4603. (mRoomsTR5[i].lights[j].lightType == 1) ? "Point" :
  4604. (mRoomsTR5[i].lights[j].lightType == 2) ? "Spot" :
  4605. (mRoomsTR5[i].lights[j].lightType == 3) ? "Directional" : "Unknown");
  4606. //Fread(&mRoomsTR5[i].lights[j].seperator2, 3, 1, f);
  4607. printDebug("LoadTR5", "CDCDCD = %c%c%c",
  4608. mRoomsTR5[i].lights[j].seperator2[0],
  4609. mRoomsTR5[i].lights[j].seperator2[1],
  4610. mRoomsTR5[i].lights[j].seperator2[2]);
  4611. }
  4612. int numSectors = mRoomsTR5[i].numXSectors * mRoomsTR5[i].numZSectors;
  4613. if (numSectors)
  4614. {
  4615. mRoomsTR5[i].sectors = new tr2_room_sector_t[numSectors];
  4616. }
  4617. // Sectors
  4618. printDebug("LoadTR5", "Reading %u sectors @ 8bytes each",
  4619. numSectors);
  4620. for (j = 0; j < numSectors; ++j)
  4621. {
  4622. Fread(&mRoomsTR5[i].sectors[j].fd_index, 2, 1, f);
  4623. printDebug("LoadTR5", "sector[%i].fd_index = %u", j,
  4624. mRoomsTR5[i].sectors[j].fd_index);
  4625. Fread(&mRoomsTR5[i].sectors[j].box_index, 2, 1, f);
  4626. printDebug("LoadTR5", "sector[%i].box_index = %u", j,
  4627. mRoomsTR5[i].sectors[j].box_index);
  4628. Fread(&mRoomsTR5[i].sectors[j].room_below, 1, 1, f);
  4629. printDebug("LoadTR5", "sector[%i].room_below = %u", j,
  4630. mRoomsTR5[i].sectors[j].room_below);
  4631. Fread(&mRoomsTR5[i].sectors[j].floor, 1, 1, f);
  4632. printDebug("LoadTR5", "sector[%i].floor = %i", j,
  4633. (*(char*)(&mRoomsTR5[i].sectors[j].floor)));
  4634. Fread(&mRoomsTR5[i].sectors[j].room_above, 1, 1, f);
  4635. printDebug("LoadTR5", "sector[%i].room_above = %u", j,
  4636. mRoomsTR5[i].sectors[j].room_above);
  4637. Fread(&mRoomsTR5[i].sectors[j].ceiling, 1, 1, f);
  4638. printDebug("LoadTR5", "sector[%i].ceiling = %i", j,
  4639. (*(char*)(&mRoomsTR5[i].sectors[j].ceiling)));
  4640. }
  4641. portalOffset = (thisRoomOffset + mRoomsTR5[i].startSDOffset + 216 +
  4642. numSectors * 8);
  4643. u = ftell(f);
  4644. if (u != portalOffset)
  4645. {
  4646. printDebug("LoadTR5", "*** Skipping %i bytes to start of portals ***",
  4647. portalOffset - u);
  4648. fseek(f, portalOffset, SEEK_SET);
  4649. }
  4650. // Portals //////////////////////
  4651. Fread(&mRoomsTR5[i].numDoors, 2, 1, f);
  4652. printDebug("LoadTR5", "room[%i].tr5_num_portals = %u",
  4653. i, mRoomsTR5[i].numDoors);
  4654. printDebug("LoadTR5", "Reading %u portals @ 32bytes each",
  4655. mRoomsTR5[i].numDoors);
  4656. if (mRoomsTR5[i].numDoors)
  4657. {
  4658. mRoomsTR5[i].doors = new tr2_room_portal_t[mRoomsTR5[i].numDoors];
  4659. }
  4660. for (j = 0; j < (int)mRoomsTR5[i].numDoors; ++j)
  4661. {
  4662. Fread(&mRoomsTR5[i].doors[j].adjoining_room, 2, 1, f);
  4663. printDebug("LoadTR5", "room[%i].portal[%i].adjoining_room = %u",
  4664. i, j, mRoomsTR5[i].doors[j].adjoining_room);
  4665. Fread(&mRoomsTR5[i].doors[j].normal.x, 2, 1, f);
  4666. Fread(&mRoomsTR5[i].doors[j].normal.y, 2, 1, f);
  4667. Fread(&mRoomsTR5[i].doors[j].normal.z, 2, 1, f);
  4668. printDebug("LoadTR5", "portal[%i].normal = ( %i, %i, %i )", j,
  4669. mRoomsTR5[i].doors[j].normal.x,
  4670. mRoomsTR5[i].doors[j].normal.y,
  4671. mRoomsTR5[i].doors[j].normal.z);
  4672. for (k = 0; k < 4; ++k)
  4673. {
  4674. Fread(&mRoomsTR5[i].doors[j].vertices[k].x, 2, 1, f);
  4675. Fread(&mRoomsTR5[i].doors[j].vertices[k].y, 2, 1, f);
  4676. Fread(&mRoomsTR5[i].doors[j].vertices[k].z, 2, 1, f);
  4677. printDebug("LoadTR5", "portal[%i].vertices[%i] = ( %i, %i, %i )",
  4678. j, k,
  4679. mRoomsTR5[i].doors[j].vertices[k].x,
  4680. mRoomsTR5[i].doors[j].vertices[k].y,
  4681. mRoomsTR5[i].doors[j].vertices[k].z);
  4682. }
  4683. }
  4684. Fread(&mRoomsTR5[i].seperator21, 2, 1, f);
  4685. printDebug("LoadTR5", "seperator21, CDCD = 0x%x",
  4686. mRoomsTR5[i].seperator21);
  4687. if (mRoomsTR5[i].seperator21 != 0xcdcd)
  4688. {
  4689. print("LoadTR5", "Error #22 CDCD != 0x%x",
  4690. mRoomsTR5[i].seperator21);
  4691. return -3;
  4692. }
  4693. portalOffset = (thisRoomOffset + mRoomsTR5[i].endPortalOffset + 216);
  4694. u = ftell(f);
  4695. if (u != portalOffset)
  4696. {
  4697. printDebug("LoadTR5", "*** Skipping %i bytes to end of portals ***",
  4698. portalOffset - u);
  4699. fseek(f, portalOffset, SEEK_SET);
  4700. }
  4701. if (mRoomsTR5[i].numStaticMeshes)
  4702. {
  4703. mRoomsTR5[i].meshes = new tr2_room_staticmesh_t[mRoomsTR5[i].numStaticMeshes];
  4704. }
  4705. // Static meshes
  4706. for (j = 0; j < (int)mRoomsTR5[i].numStaticMeshes; ++j)
  4707. {
  4708. Fread(&mRoomsTR5[i].meshes[j].x, 4, 1, f);
  4709. printDebug("LoadTR5", "static_mesh[%i].x = %i", j,
  4710. mRoomsTR5[i].meshes[j].x);
  4711. Fread(&mRoomsTR5[i].meshes[j].y, 4, 1, f);
  4712. printDebug("LoadTR5", "static_mesh[%i].y = %i", j,
  4713. mRoomsTR5[i].meshes[j].y);
  4714. Fread(&mRoomsTR5[i].meshes[j].z, 4, 1, f);
  4715. printDebug("LoadTR5", "static_mesh[%i].z = %i", j,
  4716. mRoomsTR5[i].meshes[j].z);
  4717. Fread(&mRoomsTR5[i].meshes[j].rotation, 2, 1, f);
  4718. printDebug("LoadTR5", "static_mesh[%i].rotation = %i", j,
  4719. mRoomsTR5[i].meshes[j].rotation);
  4720. Fread(&mRoomsTR5[i].meshes[j].intensity1, 2, 1, f);
  4721. printDebug("LoadTR5", "static_mesh[%i].intensity1 = %i", j,
  4722. mRoomsTR5[i].meshes[j].intensity1);
  4723. Fread(&mRoomsTR5[i].meshes[j].intensity2, 2, 1, f);
  4724. printDebug("LoadTR5", "static_mesh[%i].intensity2 = %i", j,
  4725. mRoomsTR5[i].meshes[j].intensity2);
  4726. Fread(&mRoomsTR5[i].meshes[j].object_id, 2, 1, f);
  4727. printDebug("LoadTR5", "static_mesh[%i].object_id = %i", j,
  4728. mRoomsTR5[i].meshes[j].object_id);
  4729. }
  4730. // Layers /////////////////
  4731. if (mRoomsTR5[i].numLayers)
  4732. {
  4733. mRoomsTR5[i].layers = new tr5_room_layer_t[mRoomsTR5[i].numLayers];
  4734. }
  4735. printDebug("LoadTR5", "Reading %i layers",
  4736. mRoomsTR5[i].numLayers);
  4737. for (j = 0; j < (int)mRoomsTR5[i].numLayers; ++j)
  4738. {
  4739. Fread(&mRoomsTR5[i].layers[j], 56, 1, f);
  4740. printDebug("LoadTR5", "layer[%i].num_vertices = %i", j,
  4741. mRoomsTR5[i].layers[j].numLayerVertices);
  4742. printDebug("LoadTR5", "layer[%i].unknown1 = %i", j,
  4743. mRoomsTR5[i].layers[j].unknownL1);
  4744. printDebug("LoadTR5", "layer[%i].num_rectangles = %i", j,
  4745. mRoomsTR5[i].layers[j].numLayerRectangles);
  4746. printDebug("LoadTR5", "layer[%i].num_triangles = %i", j,
  4747. mRoomsTR5[i].layers[j].numLayerTriangles);
  4748. printDebug("LoadTR5", "layer[%i].num_2side_textures? = %i", j,
  4749. mRoomsTR5[i].layers[j].unknownL2);
  4750. printDebug("LoadTR5", "layer[%i].filler, 0? = %i", j,
  4751. mRoomsTR5[i].layers[j].filler);
  4752. printDebug("LoadTR5", "layer[%i].filler2, 0? = %i", j,
  4753. mRoomsTR5[i].layers[j].filler2);
  4754. printDebug("LoadTR5", "layer[%i].bbox[0] = {%.2f %.2f %.2f}", j,
  4755. mRoomsTR5[i].layers[j].layerBoundingBoxX1,
  4756. mRoomsTR5[i].layers[j].layerBoundingBoxX1,
  4757. mRoomsTR5[i].layers[j].layerBoundingBoxX1);
  4758. printDebug("LoadTR5", "layer[%i].bbox[1] = {%.2f %.2f %.2f}", j,
  4759. mRoomsTR5[i].layers[j].layerBoundingBoxX2,
  4760. mRoomsTR5[i].layers[j].layerBoundingBoxX2,
  4761. mRoomsTR5[i].layers[j].layerBoundingBoxX2);
  4762. printDebug("LoadTR5", "layer[%i].filler3, 0? = %i", j,
  4763. mRoomsTR5[i].layers[j].filler3);
  4764. printDebug("LoadTR5", "layer[%i].unknown6 = %i", j,
  4765. mRoomsTR5[i].layers[j].unknownL6);
  4766. printDebug("LoadTR5", "layer[%i].unknown7 = %i", j,
  4767. mRoomsTR5[i].layers[j].unknownL7);
  4768. printDebug("LoadTR5", "layer[%i].unknown8 = %i", j,
  4769. mRoomsTR5[i].layers[j].unknownL8);
  4770. }
  4771. if (mRoomsTR5[i].numLayers)
  4772. {
  4773. mRoomsTR5[i].faces = new tr5_room_geometry_t[mRoomsTR5[i].numLayers];
  4774. }
  4775. for (j = 0; j < (int)mRoomsTR5[i].numLayers; ++j)
  4776. {
  4777. mRoomsTR5[i].faces[j].quads = 0x0;
  4778. mRoomsTR5[i].faces[j].tris = 0x0;
  4779. k = mRoomsTR5[i].layers[j].numLayerRectangles;
  4780. if (k)
  4781. {
  4782. printDebug("LoadTR5", "Reading %i layer quads", k);
  4783. mRoomsTR5[i].faces[j].quads = new tr5_face4_t[k];
  4784. Fread(mRoomsTR5[i].faces[j].quads, 12, k, f);
  4785. }
  4786. k = mRoomsTR5[i].layers[j].numLayerTriangles;
  4787. if (k)
  4788. {
  4789. printDebug("LoadTR5", "Reading %i layer tris", k);
  4790. mRoomsTR5[i].faces[j].tris = new tr5_face3_t[k];
  4791. Fread(mRoomsTR5[i].faces[j].tris, 10, k, f);
  4792. }
  4793. }
  4794. for (j = 0; j < (int)mRoomsTR5[i].numLayers; ++j)
  4795. {
  4796. mRoomsTR5[i].faces[j].verts = 0x0;
  4797. k = mRoomsTR5[i].layers[j].numLayerVertices;
  4798. if (k)
  4799. {
  4800. printDebug("LoadTR5", "Reading %i layer vertices", k);
  4801. mRoomsTR5[i].faces[j].verts = new tr5_vertex_t[k];
  4802. Fread(mRoomsTR5[i].faces[j].verts, 28, k, f);
  4803. }
  4804. }
  4805. #define TR5_SKIP_TO_ROOMS
  4806. #ifdef TR5_SKIP_TO_ROOMS
  4807. unsigned int hack = ftell(f);
  4808. if (hack < nextRoomOffset)
  4809. {
  4810. printDebug("LoadTR5", "Skipping %i bytes at end of room[%i]",
  4811. nextRoomOffset - hack, i);
  4812. fseek(f, nextRoomOffset, SEEK_SET);
  4813. }
  4814. #else
  4815. long hack = 0;
  4816. // This peels padding off the end of TRCs like ANDREA1.TRC
  4817. while (hack != 0xcdcd)
  4818. {
  4819. Fread(&hack, 2, 1, f);
  4820. printDebug("LoadTR5", "hack[%i] = 0x%x", i, hack);
  4821. }
  4822. #endif
  4823. }
  4824. Fread(&_num_floor_data, 4, 1, f);
  4825. printDebug("LoadTR5", "_num_floor_data = %u", _num_floor_data);
  4826. printDebug("LoadTR5", "Reading %u floorData elements", _num_floor_data);
  4827. _floor_data = new unsigned short[_num_floor_data];
  4828. Fread(_floor_data, 2, _num_floor_data, f);
  4829. // Number of 16bits of mesh data to follow
  4830. Fread(&numMeshData, 4, 1, f);
  4831. printDebug("LoadTR5", "numMeshData = %u", numMeshData);
  4832. meshData = new unsigned char[2*numMeshData];
  4833. Fread(meshData, 2, numMeshData, f);
  4834. // Use pointers array to index in meshData array for tr5_mesh_t's
  4835. Fread(&numMeshPointers, 4, 1, f);
  4836. printDebug("LoadTR5", "numMeshPointers = %u", numMeshPointers);
  4837. meshPointers = new u_int32_t[numMeshPointers];
  4838. Fread(meshPointers, 4, numMeshPointers, f);
  4839. Fread(&numAnimationsTR5, 4, 1, f);
  4840. printDebug("LoadTR5", "numAnimationsTR5 = %u", numAnimationsTR5);
  4841. animationsTR5 = new tr5_animation_t[numAnimationsTR5];
  4842. Fread(animationsTR5, 40, numAnimationsTR5, f);
  4843. Fread(&u, 4, 1, f);
  4844. _num_state_changes = u;
  4845. printDebug("LoadTR5", "_num_state_changes = %u", _num_state_changes);
  4846. _state_changes = new tr2_state_change_t[_num_state_changes];
  4847. Fread(_state_changes, 6, _num_state_changes, f);
  4848. Fread(&u, 4, 1, f);
  4849. _num_anim_dispatches = u;
  4850. printDebug("LoadTR5", "_num_anim_dispatches = %u", _num_anim_dispatches);
  4851. _anim_dispatches = new tr2_anim_dispatch_t[_num_anim_dispatches];
  4852. Fread(_anim_dispatches, 8, _num_anim_dispatches, f);
  4853. Fread(&u, 4, 1, f);
  4854. _num_anim_commands = u;
  4855. printDebug("LoadTR5", "_num_anim_commands = %u", _num_anim_commands);
  4856. _anim_commands = new tr2_anim_command_t[_num_anim_commands];
  4857. Fread(_anim_commands, 2, _num_anim_commands, f);
  4858. Fread(&u, 4, 1, f);
  4859. _num_mesh_trees = u;
  4860. printDebug("LoadTR5", "_num_mesh_trees = %u", _num_mesh_trees);
  4861. _mesh_trees = new tr2_meshtree_t[_num_mesh_trees];
  4862. Fread(_mesh_trees, 4, _num_mesh_trees, f);
  4863. Fread(&u, 4, 1, f);
  4864. _num_frames = u;
  4865. printDebug("LoadTR5", "_num_frames = %u", _num_frames);
  4866. _frames = new u_int16_t[_num_frames];
  4867. Fread(_frames, 2, _num_frames, f);
  4868. Fread(&numMoveablesTR5, 4, 1, f);
  4869. printDebug("LoadTR5", "numMoveablesTR5 = %u", numMoveablesTR5);
  4870. moveablesTR5 = new tr5_moveable_t[numMoveablesTR5];
  4871. Fread(moveablesTR5, 20, numMoveablesTR5, f);
  4872. Fread(&u, 4, 1, f);
  4873. _num_static_meshes = u;
  4874. printDebug("LoadTR5", "_num_static_meshes = %u", _num_static_meshes);
  4875. _static_meshes = new tr2_staticmesh_t[_num_static_meshes];
  4876. Fread(_static_meshes, 32, _num_static_meshes, f);
  4877. Fread(check, 4, 1, f);
  4878. printDebug("LoadTR5", "Check: SPR = '%c%c%c'?",
  4879. check[0], check[1], check[2]);
  4880. if (check[0] != 'S' || check[1] != 'P' || check[2] != 'R')
  4881. {
  4882. print("LoadTR5", "Error: SPR != '%c%c%c'", check[0], check[1], check[2]);
  4883. return -4;
  4884. }
  4885. Fread(&u, 4, 1, f);
  4886. _num_sprite_textures = u;
  4887. printDebug("LoadTR5", "_num_sprite_textures = %u", _num_sprite_textures);
  4888. _sprite_textures = new tr2_sprite_texture_t[_num_sprite_textures];
  4889. Fread(_sprite_textures, 16, _num_sprite_textures, f);
  4890. Fread(&u, 4, 1, f);
  4891. _num_sprite_sequences = u;
  4892. printDebug("LoadTR5", "_num_sprite_sequences = %u", _num_sprite_sequences);
  4893. _sprite_sequences = new tr2_sprite_sequence_t[_num_sprite_sequences];
  4894. Fread(_sprite_sequences, 8, _num_sprite_sequences, f);
  4895. Fread(&u, 4, 1, f);
  4896. _num_cameras = u;
  4897. printDebug("LoadTR5", "_num_cameras = %u", _num_cameras);
  4898. if (_num_cameras > 0)
  4899. {
  4900. _cameras = new tr2_camera_t[_num_cameras];
  4901. Fread(_cameras, 16, _num_cameras, f);
  4902. }
  4903. else
  4904. {
  4905. _cameras = 0x0;
  4906. }
  4907. Fread(&numFlyByCamerasTR5, 4, 1, f);
  4908. printDebug("LoadTR5", "numFlyByCameras = %u", numFlyByCamerasTR5);
  4909. if (numFlyByCamerasTR5 > 0)
  4910. {
  4911. flyByCamerasTR5 = new tr5_flyby_camera_t[numFlyByCamerasTR5];
  4912. Fread(flyByCamerasTR5, 40, numFlyByCamerasTR5, f);
  4913. }
  4914. else
  4915. {
  4916. flyByCamerasTR5 = 0x0;
  4917. }
  4918. Fread(&u, 4, 1, f);
  4919. _num_sound_sources = u;
  4920. printDebug("LoadTR5", "_num_sound_sources = %u", _num_sound_sources);
  4921. if (_num_sound_sources > 0)
  4922. {
  4923. _sound_sources = new tr2_sound_source_t[_num_sound_sources];
  4924. Fread(_sound_sources, 16, _num_sound_sources, f);
  4925. }
  4926. else
  4927. {
  4928. _sound_sources = 0x0;
  4929. }
  4930. Fread(&u, 4, 1, f);
  4931. _num_boxes = u;
  4932. printDebug("LoadTR5", "_num_boxes = %u", _num_boxes);
  4933. _boxes = new tr2_box_t[_num_boxes];
  4934. Fread(_boxes, 8, _num_boxes, f);
  4935. Fread(&u, 4, 1, f);
  4936. _num_overlaps = u;
  4937. printDebug("LoadTR5", "_num_overlaps = %u", _num_overlaps);
  4938. _overlaps = new short[_num_overlaps];
  4939. Fread(_overlaps, 2, _num_overlaps, f);
  4940. _zones = new short[_num_boxes*10];
  4941. Fread(_zones, 20, _num_boxes, f);
  4942. Fread(&u, 4, 1, f);
  4943. _num_animated_textures = u;
  4944. printDebug("LoadTR5", "_num_animated_textures = %u", _num_animated_textures);
  4945. _animated_textures = new short[_num_animated_textures];
  4946. Fread(_animated_textures, 2, _num_animated_textures, f);
  4947. Fread(check, 1, 5, f);
  4948. printDebug("LoadTR5", "Check: TEX = '%c%c%c'?",
  4949. check[1], check[2], check[3]);
  4950. // check[0] is '^D'
  4951. if (check[1] != 'T' || check[2] != 'E' || check[3] != 'X')
  4952. {
  4953. print("LoadTR5", "Error: TEX != '%c%c%c' @ %lu",
  4954. check[1], check[2], check[3], ftell(f));
  4955. return -4;
  4956. }
  4957. Fread(&numObjectTexturesTR5, 4, 1, f);
  4958. printDebug("LoadTR5", "numObjectTextures = %u", numObjectTexturesTR5);
  4959. objectTexturesTR5 = new tr5_object_texture_t[numObjectTexturesTR5];
  4960. Fread(objectTexturesTR5, 40, numObjectTexturesTR5, f);
  4961. Fread(&u, 4, 1, f);
  4962. _num_items = u;
  4963. printDebug("LoadTR5", "_num_items = %u", _num_items);
  4964. _items = new tr2_item_t[_num_items];
  4965. Fread(_items, 24, _num_items, f);
  4966. Fread(&numCinematicFramesTR5, 4, 1, f);
  4967. printDebug("LoadTR5", "numCinematicFrames = %u", numCinematicFramesTR5);
  4968. if (numCinematicFramesTR5 > 0)
  4969. {
  4970. cinematicFramesTR5 = new tr5_cinematic_frame_t[numCinematicFramesTR5];
  4971. Fread(cinematicFramesTR5, 24, numCinematicFramesTR5, f);
  4972. }
  4973. else
  4974. {
  4975. cinematicFramesTR5 = 0x0;
  4976. }
  4977. Fread(&us, 2, 1, f);
  4978. _num_demo_data = us; // Could overflow? not sure
  4979. printDebug("LoadTR5", "_num_demo_data = %u", _num_demo_data);
  4980. if (_num_demo_data > 0)
  4981. {
  4982. _demo_data = new unsigned char[_num_demo_data];
  4983. Fread(_demo_data, 1, _num_demo_data, f);
  4984. }
  4985. else
  4986. {
  4987. _demo_data = 0x0;
  4988. }
  4989. printDebug("LoadTR5", "Reading soundMap");
  4990. mSoundMap = new short[450];
  4991. Fread(mSoundMap, 900, 1, f);
  4992. Fread(&u, 4, 1, f);
  4993. mNumSoundDetails = u;
  4994. printDebug("LoadTR5", "numSoundDetails = %u", mNumSoundDetails);
  4995. mSoundDetails = new tr2_sound_details_t[mNumSoundDetails];
  4996. Fread(mSoundDetails, 8, mNumSoundDetails, f);
  4997. Fread(&u, 4, 1, f);
  4998. mNumSampleIndices = u;
  4999. printDebug("LoadTR5", "numSampleIndices = %u", mNumSampleIndices);
  5000. mSampleIndicesTR5 = new unsigned int[mNumSampleIndices];
  5001. Fread(mSampleIndicesTR5, 4, mNumSampleIndices, f);
  5002. Fread(&u, 4, 1, f);
  5003. printDebug("LoadTR5", "Check 0xCDCDCDCD = 0x%X?", u);
  5004. if (u != 0xcdcdcdcd)
  5005. {
  5006. print("LoadTR5", "Check 0xCDCDCDCD != 0x%X @ %u", u, ftell(f));
  5007. return -5;
  5008. }
  5009. // Skip over the extra short in the demo.trc, but if it's not there
  5010. // seek back
  5011. u = ftell(f);
  5012. Fread(&us, 2, 1, f);
  5013. if (us != 0xcdcd)
  5014. {
  5015. fseek(f, u, SEEK_SET);
  5016. }
  5017. //! \fixme (Endian) Read bitu32 / u_int32_t
  5018. Fread(&mNumTR4Samples, 4, 1, f);
  5019. printDebug("Load", "mNumTR4Samples = %i", mNumTR4Samples);
  5020. mRiffDataSz = 0;
  5021. mTR4Samples = new unsigned char *[mNumTR4Samples];
  5022. mTR4SamplesSz = new unsigned int[mNumTR4Samples];
  5023. memset(mTR4SamplesSz, 0, mNumTR4Samples*4);
  5024. for (i = 0; i < (int)mNumTR4Samples; ++i)
  5025. {
  5026. unsigned int sizeCompressed;
  5027. unsigned int sizeUncompressed;
  5028. unsigned char *compressedSoundSample;
  5029. unsigned char *unCompressedSoundSample;
  5030. int zErr;
  5031. uLongf libzUncompressedSize;
  5032. Fread(&sizeUncompressed, 4, 1, f);
  5033. printDebug("Load", " sizeUncompressed = %i", sizeUncompressed);
  5034. Fread(&sizeCompressed, 4, 1, f);
  5035. printDebug("Load", " sizeCompressed = %i", sizeCompressed);
  5036. compressedSoundSample = new unsigned char[sizeCompressed];
  5037. unCompressedSoundSample = new unsigned char[sizeUncompressed];
  5038. //printDebug("Load", " %lubytes read from file", ftell(f));
  5039. Fread(compressedSoundSample, sizeCompressed, 1, f);
  5040. printDebug("Load", " %c%c%c%c should be RIFF",
  5041. compressedSoundSample[0],
  5042. compressedSoundSample[1],
  5043. compressedSoundSample[2],
  5044. compressedSoundSample[3]);
  5045. // Decompress the sample
  5046. libzUncompressedSize = sizeUncompressed;
  5047. zErr = uncompress(unCompressedSoundSample,
  5048. &libzUncompressedSize,
  5049. compressedSoundSample,
  5050. sizeCompressed);
  5051. sizeUncompressed = libzUncompressedSize;
  5052. switch (zErr)
  5053. {
  5054. case Z_MEM_ERROR:
  5055. printDebug("Load", " Decompress Error: not enough memory");
  5056. break;
  5057. case Z_BUF_ERROR:
  5058. printDebug("Load", " Decompress Error: output buffer too small");
  5059. break;
  5060. case Z_DATA_ERROR:
  5061. printDebug("Load", " Decompress Error: input data was corrupted");
  5062. break;
  5063. case Z_OK:
  5064. printDebug("Load", " Decompress OK");
  5065. break;
  5066. default:
  5067. printDebug("Load", " Decompress Error: decompress error #%i", zErr);
  5068. }
  5069. // Hhhmm... handle uncompressed RIFFs too?
  5070. if (zErr == Z_OK)
  5071. {
  5072. mTR4Samples[i] = unCompressedSoundSample;
  5073. mTR4SamplesSz[i] = sizeUncompressed;
  5074. delete [] compressedSoundSample;
  5075. }
  5076. else
  5077. {
  5078. printDebug("Load", " %lubytes read from file", ftell(f));
  5079. mTR4Samples[i] = compressedSoundSample;
  5080. mTR4SamplesSz[i] = sizeCompressed;
  5081. delete [] unCompressedSoundSample;
  5082. }
  5083. }
  5084. fclose(f);
  5085. return 0;
  5086. }
  5087. void TombRaider::print(const char *methodName, const char *s, ...)
  5088. {
  5089. va_list args;
  5090. va_start(args, s);
  5091. fprintf(stderr, "TombRaider::%s> ", methodName);
  5092. vfprintf(stderr, s, args);
  5093. fprintf(stderr, "\n");
  5094. va_end(args);
  5095. }
  5096. void TombRaider::printDebug(const char *methodName, const char *s, ...)
  5097. {
  5098. va_list args;
  5099. if (!mDebug)
  5100. return;
  5101. va_start(args, s);
  5102. fprintf(stdout, "TombRaider::%s> ", methodName);
  5103. vfprintf(stdout, s, args);
  5104. fprintf(stdout, "\n");
  5105. va_end(args);
  5106. }
  5107. ////////////////////////////////////////////////////////////
  5108. // Private Mutators
  5109. ////////////////////////////////////////////////////////////