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

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