Open Source Tomb Raider Engine
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TombRaider.cpp 191KB

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