1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325 |
- /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
- /*================================================================
- *
- * Project : OpenRaider
- * Author : Mongoose
- * Website : http://www.westga.edu/~stu7440/
- * Email : stu7440@westga.edu
- * Object : OpenRaider
- * License : No use w/o permission (C) 2001 Mongoose
- * Comments: Loads TR 1, 2, 3, and 4 maps, meshes, and textures
- *
- * This file was generated using Mongoose's C++
- * template generator script. <stu7440@westga.edu>
- *
- *-- History ------------------------------------------------
- *
- * 2003.05.13:
- * Mongoose - New API, maintance cost was becoming so high
- * it was needed to sort out methods in groups
- * like my newer source code -- of course some
- * methods were altered or removed ( clean up )
- *
- * 2001.06.19:
- * Mongoose - New texture API for the new TR4/TR5 bump map
- * support, also purged textile exposure
- *
- * 2001.05.21:
- * Mongoose - Added to project OpenRaider, more documentation
- * than Freyja version I wrote ( 3d modeler )
- *
- *
- * 2000-05-13:
- * Mongoose - Added gcc and VC++ pragmas for packing
- *
- * id style typedefs for structs
- *
- * Heavy clean up and ported to C++
- *
- * I saved yuri's notes as best I could and
- * reformatted and corected as needed
- *
- * Mongoose - Created, based on:
- * tr_view's tr2io.c by Yuri Zhivago, PhD,
- * TR Rosetta Stone ( TombRaider pak format specs )
- ================================================================*/
-
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- #include <string.h>
- #include <stdarg.h>
-
- #include <TombRaider.h>
-
- #include <zlib.h>
-
- #ifdef __TEST_TR5_DUMP_TGA
- #include <TGA.h>
- #endif
-
- #ifdef DEBUG_MEMORY
- #include <memory_test.h>
- #endif
-
-
-
- TombRaider::TombRaider()
- {
- _textile8 = NULL;
- _textile16 = NULL;
- _textile32 = NULL;
- _tex_special = NULL;
- _rooms = NULL;
- _floor_data = NULL;
- _animations = NULL;
- _state_changes = NULL;
- _anim_dispatches = NULL;
- _anim_commands = NULL;
- _mesh_trees = NULL;
- _frames = NULL;
- _moveables = NULL;
- _static_meshes = NULL;
- _object_textures = NULL;
- _sprite_textures = NULL;
- _sprite_sequences = NULL;
- _cameras = NULL;
- _sound_sources = NULL;
- _boxes = NULL;
- _overlaps = NULL;
- _zones = NULL;
- _animated_textures = NULL;
- _items = NULL;
- _light_map = NULL;
- _cinematic_frames = NULL;
- _demo_data = NULL;
-
- mRoomsTR5 = 0x0;
- mMeshes = 0x0;
- mSoundMap = 0x0;
- mSoundDetails = 0x0;
- mSampleIndices = 0x0;
- mSampleIndicesTR5 = 0x0;
- mRiffData = 0x0;
- mTR4Samples = 0x0;
- mTR4SamplesSz = 0x0;
- mRiffAlternateOffsets = 0x0;
- mCompressedLevelData = 0x0;
- moveablesTR5 = 0x0;
- animationsTR5 = 0x0;
- objectTexturesTR5 = 0x0;
- cinematicFramesTR5 = 0x0;
- flyByCamerasTR5 = 0x0;
-
- mNumTR4Samples = 0;
- mReset = false;
- mDebug = false;
- mRiffAlternateLoaded = false;
- mRoomVertexLightingFactor = 50.0f;
- mTexelScale = 256.0f;
- mRiffDataSz = 0;
-
- reset();
- }
-
-
- TombRaider::~TombRaider()
- {
- reset();
- }
-
-
- int TombRaider::NumMoveables()
- {
- return _num_moveables;
- }
-
-
- int TombRaider::NumRooms()
- {
- return _num_rooms;
- }
-
-
- int TombRaider::NumAnimations()
- {
- return _num_animations;
- }
-
-
- unsigned int TombRaider::NumFrames()
- {
- return _num_frames;
- }
-
-
- int TombRaider::NumStaticMeshes()
- {
- return _num_static_meshes;
- }
-
-
- int TombRaider::NumSprites()
- {
- return _num_sprite_textures;
- }
-
-
- int TombRaider::NumSpriteSequences()
- {
- return _num_sprite_sequences;
- }
-
-
- int TombRaider::NumItems()
- {
- return _num_items;
- }
-
-
- int TombRaider::NumTextures()
- {
- //return _num_room_textures + _num_misc_textures + _num_bump_map_textures / 2;
- return _num_textiles - _num_bump_map_textures / 2;
- }
-
-
- tr2_room_t *TombRaider::Room()
- {
- return _rooms;
- }
-
-
- tr2_item_t *TombRaider::Item()
- {
- return _items;
- }
-
-
- tr2_object_texture_t *TombRaider::ObjectTextures()
- {
- return _object_textures;
- }
-
-
- unsigned int TombRaider::getNumBoxes()
- {
- return _num_boxes;
- }
-
-
- tr2_box_t *TombRaider::Box()
- {
- return _boxes;
- }
-
-
- tr2_mesh_t *TombRaider::Mesh()
- {
- /*
- if (n > 0 || n > mMeshCount)
- return NULL;
-
- return _meshes+n;
- */
-
- return mMeshes;
- }
-
-
- int TombRaider::getNumAnimsForMoveable(int moveable_index)
- {
- /***************************************************************************
- * It seems the number of animations isn't available in the moveable,
- * so we have to calculate it:
- * - Get the "# starting anim" for the next moveable (->N)
- * - Substract the "# starting anim" for moveable to N
- *
- * Doing this, we assume that the next moveable has its animations following
- * the animations of the current moveable (seems right for all tested
- * levels, but...)
- *
- * We also have to deal with the fact that the next moveable
- * could have "# starting anim" == -1
- * (ie. anim handled by the engine, like the ponytail anim).
- * If it's the case, we skip the moveable
- * and use the next moveable for our computation
- *
- * - Mongoose, Notes I edited from TRViewer
- **************************************************************************/
-
- int start_anim;
- int next_start_anim = 0xFFFF;
- tr2_moveable_t *last_moveable = 0x0;
- tr2_moveable_t *moveable = 0x0;
- tr2_moveable_t *next_moveable = 0x0;
-
-
- if ((moveable_index >= 0 &&
- moveable_index <= (int)_num_moveables) || _num_moveables < 1)
- {
- moveable = &_moveables[moveable_index];
- }
-
- if (!moveable)
- {
- return -1; // Was 0
- }
-
- last_moveable = &_moveables[moveable_index-1];
-
- start_anim = moveable->animation;
-
- while (moveable != last_moveable)
- {
- next_moveable = moveable + 1;
- next_start_anim = next_moveable->animation;
-
- if (next_start_anim != 0xFFFF)
- break;
-
- moveable = next_moveable++;
- }
-
- if (moveable == last_moveable)
- {
- next_start_anim = _num_animations;
- }
-
- return ((start_anim != 0xFFFF) ? next_start_anim - start_anim : 0);
- }
-
-
- tr2_staticmesh_t *TombRaider::StaticMesh()
- {
- return _static_meshes;
- }
-
-
- tr2_version_type TombRaider::Engine()
- {
- return mEngineVersion;
- }
-
-
- tr2_animation_t *TombRaider::Animation()
- {
- return _animations;
- }
-
-
- unsigned short *TombRaider::Frame()
- {
- return _frames;
- }
-
-
- tr2_moveable_t *TombRaider::Moveable()
- {
- /*
- if (n > 0 || n > (int)_num_moveables)
- return NULL;
- */
-
- return _moveables;
- }
-
-
- tr2_meshtree_t *TombRaider::MeshTree()
- {
- /*
- if (n > 0 || n > (int)_num_mesh_trees)
- return NULL;
- */
-
- return _mesh_trees;
- }
-
-
- tr2_sprite_texture_t *TombRaider::Sprite()
- {
- return _sprite_textures;
- }
-
-
- tr2_sprite_sequence_t *TombRaider::SpriteSequence()
- {
- return _sprite_sequences;
- }
-
- unsigned char *TombRaider::SpecialTexTile(int texture)
- {
- unsigned char *image;
- unsigned char *ptr;
-
-
- image = NULL;
-
- if (texture >=0 && texture < NumSpecialTextures())
- {
- // Get base and offset into 32bit special textures/bump maps
- ptr = _tex_special;
- ptr += 256*256*4*texture;
-
- // Clone it as a single 256x256 @ 32bpp image
- image = new unsigned char[256*256*4];
- memcpy(image, ptr, 256*256*4);
- }
-
- return image;
- }
-
-
- int TombRaider::NumSpecialTextures()
- {
- return _num_tex_special;
- }
-
-
- void TombRaider::Texture(int texture, unsigned char **image,
- unsigned char **bumpmap)
- {
- int bumpmap_base = _num_room_textures + _num_misc_textures;
-
-
- *image = getTexTile(texture);
- *bumpmap = NULL;
-
- if (_num_bump_map_textures && texture >= bumpmap_base)
- {
- *bumpmap = getTexTile(texture + _num_bump_map_textures/2);
- }
- }
-
-
- unsigned int *TombRaider::Palette16()
- {
- return _palette16;
- }
-
-
- unsigned char *TombRaider::Palette8()
- {
- return (unsigned char *)_palette8;
- }
-
-
- int TombRaider::checkMime(char *filename)
- {
- FILE *f;
- unsigned int version;
-
-
- if (!filename || !filename[0])
- {
- print("checkFile", "Given filename was empty string or NULL");
- return -1;
- }
-
- f = fopen(filename, "rb");
-
- if (!f)
- {
- perror(filename);
- return -1;
- }
-
- //! \fixme Endianess
- fread(&version, sizeof(version), 1, f);
- fclose(f);
-
- switch (version)
- {
- case 0x00000020:
- case 0x0000002d:
- case 0xff080038:
- case 0xff180038:
- case 0xfffffff0: // bogus
- case 0x00345254: // "TR4\0"
- return 0;
- default:
- ;
- }
-
- return 1;
- }
-
-
- int TombRaider::Load(char *filename, void (*percent)(int))
- {
- FILE *f;
- int i, j, l;
- unsigned int num_mesh_data_words, num_mesh_pointers, data_size, data_offset;
- unsigned int *mesh_pointer_list;
- unsigned char *raw_mesh_data;
- bool tr5;
- long debugf;
-
-
- f = fopen(filename, "rb");
-
- if (!f)
- {
- perror(filename);
- return -1;
- }
-
-
- if (percent)
- (*percent)(1);
-
- mReset = false;
-
- Fread(&mPakVersion, sizeof(mPakVersion), 1, f);
- //! \fixme endian
-
- printDebug("Load", "mPakVersion = %u", mPakVersion);
-
- tr5 = false;
-
- switch (mPakVersion)
- {
- case 0x00000020:
- mEngineVersion = TR_VERSION_1;
- break;
- case 0x0000002d:
- mEngineVersion = TR_VERSION_2;
- break;
- case 0xff080038:
- case 0xff180038:
- mEngineVersion = TR_VERSION_3;
- break;
- case 0xfffffff0: // bogus
- case 0x00345254: // "TR4\0"
- mEngineVersion = TR_VERSION_4;
-
- // Check to see if this is really a TR5 demo
- l = strlen(filename);
-
- // Looking for pattern "filename.trc"
- if ((filename[l-1] == 'c' || filename[l-1] == 'C') &&
- (filename[l-2] == 'r' || filename[l-2] == 'R') &&
- (filename[l-3] == 't' || filename[l-3] == 'T'))
- {
- printDebug("Load", "This is really a TR5 pak");
- mEngineVersion = TR_VERSION_5;
- return loadTR5(f, percent);
- }
-
- break;
- default:
- mEngineVersion = TR_VERSION_UNKNOWN;
- }
-
- if (percent)
- (*percent)(5);
-
- printDebug("Load", "mEngineVersion = 0x%x", mPakVersion);
-
- if (mEngineVersion == TR_VERSION_UNKNOWN)
- return -1;
-
- if (mEngineVersion == TR_VERSION_4)
- {
- unsigned int sz, usz; // compressed and uncompressed size
- unsigned char *compressed_data = NULL;
- int zerr;
- uLongf foo;
-
-
- // Read texture type offsets
- Fread(&_num_room_textures, 2, 1, f);
- printDebug("LoadTR4", "_num_room_textures = %u", _num_room_textures);
- Fread(&_num_misc_textures, 2, 1, f);
- printDebug("LoadTR4", "_num_misc_textures = %u", _num_misc_textures);
- Fread(&_num_bump_map_textures, 2, 1, f);
- printDebug("LoadTR4", "_num_bump_map_textures = %u", _num_bump_map_textures);
-
- // Read the sizes of the 32-bit textures
- Fread(&usz, sizeof(usz), 1, f);
- Fread(&sz, sizeof(sz), 1, f);
-
- printDebug("Load", "TR4 32-bit textures compressed size = %u bytes", sz);
- printDebug("Load", "TR4 32-bit textures uncompressed size = %u bytes", usz);
-
- _num_textiles = usz / sizeof(tr2_textile32_t);
-
- printDebug("LoadTR4", "_num_textiles = %i/%lu = %i",
- usz, sizeof(tr2_textile32_t), _num_textiles);
-
- _textile32 = new tr2_textile32_t[_num_textiles];
-
- // Allocate a temporary buffer for decompression
- compressed_data = new unsigned char[sz];
- Fread(compressed_data, sz, 1, f);
-
- // Decompress the textures
- foo = usz;
- zerr = uncompress((unsigned char *)_textile32,
- &foo,
- compressed_data,
- sz);
- usz = foo;
-
- if (percent)
- (*percent)(6);
-
- printDebug("LoadTR4", "textile decompress [%s]",
- (zerr == Z_OK) ? "OK" : "ERROR");
-
- switch (zerr)
- {
- case Z_MEM_ERROR:
- printDebug("LoadTR4", "There was not enough memory");
- break;
- case Z_BUF_ERROR:
- printDebug("LoadTR4", "There was not enough room in the output buffer");
- break;
- case Z_DATA_ERROR:
- printDebug("LoadTR4", "The input data was corrupted");
- break;
- default:
- printDebug("LoadTR4", "textile decompress %i", zerr);
- }
-
- // Free the temporary buffer
- delete [] compressed_data;
-
- // Read in the 16-bit textures, set NumTextiles
- Fread(&usz, sizeof(usz), 1, f);
- Fread(&sz, sizeof(sz), 1, f);
-
- printDebug("Load", "TR4 16-bit textures compressed size = %u bytes", sz);
- printDebug("Load", "TR4 16-bit textures uncompressed size = %u bytes", usz);
-
- _num_textiles = usz / sizeof(tr2_textile16_t);
-
- printDebug("Load", "TR4 _num_textiles = %i/%lu = %i",
- usz, sizeof(tr2_textile16_t), _num_textiles);
-
- _textile16 = new tr2_textile16_t[_num_textiles];
-
- // Allocate a temporary buffer for decompression
- compressed_data = new unsigned char[sz];
- Fread(compressed_data, sz, 1, f);
-
- // Decompress the textures
- foo = usz;
- zerr = uncompress((unsigned char *)_textile16,
- &foo,
- compressed_data,
- sz);
- usz = foo;
-
- if (percent)
- (*percent)(7);
-
- // printDebug("Load", "TR4 textile decompress [%s]",
- // (zerr == Z_OK) ? "OK" : "ERROR");
-
- switch (zerr)
- {
- case Z_MEM_ERROR:
- printDebug("Load", "TR4 textile decompress [ERROR]");
- printDebug("Load", "TR4 There was not enough memory");
- break;
- case Z_BUF_ERROR:
- printDebug("Load", "TR4 textile decompress [ERROR]");
- printDebug("Load", "TR4 There was not enough room in the output buffer");
- break;
- case Z_DATA_ERROR:
- printDebug("Load", "TR4 textile decompress [ERROR]");
- printDebug("Load", "TR4 The input data was corrupted");
- break;
- case Z_OK:
- printDebug("Load", "TR4 textile decompress [OK]");
- break;
- default:
- printDebug("Load", "TR4 textile decompress %i", zerr);
- }
-
- // Free the temporary buffer
- delete [] compressed_data;
-
- // Read the sizes of the sprite textures
- Fread(&usz, sizeof(usz), 1, f);
- Fread(&sz, sizeof(sz), 1, f);
-
- printDebug("Load", "TR4 sprite textures compressed size = %u bytes", sz);
- printDebug("Load", "TR4 sprite textures uncompressed size = %u bytes", usz);
-
- // Load sprite/bump map/gui/etc textures also
- _num_tex_special = usz/(256*256*4);
-
- printDebug("LoadTR5", "_num_tex_special = %i/%i = %i",
- usz, 256*256*4, _num_tex_special);
-
- printDebug("LoadTR5", "Reading %ibytes of sprite textures", usz);
-
- if (usz)
- {
- _tex_special = new unsigned char[usz];
-
- // Allocate a temporary buffer for decompression
- compressed_data = new unsigned char[sz];
- Fread(compressed_data, sz, 1, f);
-
- // Decompress the textures
- foo = usz;
- zerr = uncompress(_tex_special,
- &foo,
- compressed_data,
- sz);
- usz = foo;
-
- printDebug("LoadTR5", "special texture decompress [%s]",
- (zerr == Z_OK) ? "OK" : "ERROR");
-
- switch (zerr)
- {
- case Z_MEM_ERROR:
- printDebug("LoadTR5", "There was not enough memory");
- break;
- case Z_BUF_ERROR:
- printDebug("LoadTR5", "There was not enough room in the output buffer");
- break;
- case Z_DATA_ERROR:
- printDebug("LoadTR5", "The input data was corrupted");
- break;
- default:
- printDebug("LoadTR5", "textile decompress %i", zerr);
- }
-
- // Free the temporary buffer
- delete [] compressed_data;
- }
-
- if (percent)
- (*percent)(9);
-
- // Read the sizes of the level data
- Fread(&usz, sizeof(usz), 1, f);
- Fread(&sz, sizeof(sz), 1, f);
-
- printDebug("Load", "TR4 level data compressed size = %u bytes", sz);
- printDebug("Load", "TR4 level data uncompressed size = %u bytes", usz);
-
- // Allocate a temporary buffer for decompression
- compressed_data = new unsigned char[sz];
- Fread(compressed_data, sz, 1, f);
- mCompressedLevelData = new unsigned char[usz];
-
- // Decompress the level data
- foo = usz;
- zerr = uncompress(mCompressedLevelData, &foo, compressed_data, sz);
-
- usz = foo;
-
- printDebug("Load", "TR4 level data decompress [%s]",
- (zerr == Z_OK) ? "OK" : "ERROR");
-
- switch (zerr)
- {
- case Z_MEM_ERROR:
- printDebug("Load", "TR4 There was not enough memory");
- break;
- case Z_BUF_ERROR:
- printDebug("Load", "TR4 There was not enough room in the output buffer");
- break;
- case Z_DATA_ERROR:
- printDebug("Load", "TR4 The input data was corrupted");
- break;
- }
-
- delete [] compressed_data;
-
- mCompressedLevelDataOffset = 0;
- mCompressedLevelSize = usz;
-
- // Toggle Fread mode to read from decompressed data in memory, not diskfile
- mFreadMode = TR_FREAD_COMPRESSED;
- }
-
- if (mEngineVersion == TR_VERSION_2 || mEngineVersion == TR_VERSION_3)
- {
- /* Read the 8-bit palette */
- Fread(_palette8, sizeof(tr2_colour_t), 256, f);
-
- /* Read 16-bit palette */
- Fread(_palette16, sizeof(_palette16), 1, f);
-
- printDebug("Load", "Read TR 2|3 8bit and 16bit palettes");
- }
-
- if (mEngineVersion != TR_VERSION_4)
- {
- /* Read the textiles */
- Fread(&_num_textiles, sizeof(_num_textiles), 1, f);
-
- printDebug("Load", "_num_textiles = %i", _num_textiles);
-
- /* 8-bit textiles come first */
- _textile8 = new tr2_textile8_t[_num_textiles];
- Fread(_textile8, sizeof(tr2_textile8_t), _num_textiles, f);
-
- /* 16-bit textiles come second */
- _textile16 = new tr2_textile16_t[_num_textiles];
-
- if (mEngineVersion != TR_VERSION_1)
- {
- //! \fixme need endian checking here
- Fread(_textile16, sizeof(tr2_textile16_t), _num_textiles, f);
- printDebug("Load", "Read in 16bit texture tiles");
- }
- }
-
- if (percent)
- (*percent)(10);
-
- /* 32-bit unknown - seems to always be 0 */
- Fread(&_unknown_t, sizeof(_unknown_t), 1, f);
- printDebug("Load", "_unknown_t = 0x%x", _unknown_t);
-
- /* Read raw room data */
- //! \fixme needs endian checking
- Fread(&_num_rooms, sizeof(_num_rooms), 1, f);
- printDebug("Load", "_num_rooms = %i", _num_rooms);
-
- data_size = _num_rooms * sizeof(tr2_room_t);
- _rooms = new tr2_room_t[_num_rooms];
-
- /* Extract room details */
- for (i = 0; i < _num_rooms; ++i)
- {
- if (percent)
- {
- (*percent)(11 + (int)(((float)i/(float)_num_rooms)*70.0));
- }
-
- /* Read RoomInfo */
- //! \fixme endian check needed
- Fread(&_rooms[i].info, sizeof(tr2_room_info_t), 1, f);
-
- printDebug("Load", "_rooms[%i].info =\n { x=%i, z=%i, yt=%i, yb=%i}",
- i,
- _rooms[i].info.x, _rooms[i].info.z,
- _rooms[i].info.y_top, _rooms[i].info.y_bottom);
-
- /* Read raw data for rest of room */
- Fread(&_rooms[i].num_data_words, sizeof(_rooms[i].num_data_words), 1, f);
-
- printDebug("Load", "_rooms[%i].num_data_words = %u",
- i, _rooms[i].num_data_words);
-
- _rooms[i].data = new unsigned char[_rooms[i].num_data_words*2];
- Fread(_rooms[i].data, 2, _rooms[i].num_data_words, f);
-
- /* Identify vertices */
- data_offset = 0;
- //! \fixme endian
- _rooms[i].room_data.num_vertices = *(short *)(void *)(_rooms[i].data);
-
- data_offset += sizeof(_rooms[0].room_data.num_vertices);
- data_size = _rooms[i].room_data.num_vertices * sizeof(tr2_vertex_room_t);
-
- printDebug("Load", "_rooms[%i].room_data.num_vertices = %u",
- i, _rooms[i].room_data.num_vertices);
-
- _rooms[i].room_data.vertices = 0x0;
-
- if (_rooms[i].room_data.num_vertices > 0)
- {
- _rooms[i].room_data.vertices =
- new tr2_vertex_room_t[_rooms[i].room_data.num_vertices];
-
- if (mEngineVersion == TR_VERSION_1)
- {
- data_size = _rooms[i].room_data.num_vertices *
- (sizeof(tr2_vertex_room_t) - 4);
-
- for (j = 0; j < _rooms[i].room_data.num_vertices; ++j)
- {
- memcpy(&_rooms[i].room_data.vertices[j],
- _rooms[i].data + data_offset +
- (j * (sizeof(tr2_vertex_room_t) - 4)),
- sizeof(tr2_vertex_room_t) - 4);
-
- // ??? Adjust for what's missing?
- _rooms[i].room_data.vertices[j].lighting2 =
- _rooms[i].room_data.vertices[j].lighting1;
- _rooms[i].room_data.vertices[j].attributes = 0;
- }
- }
- else
- {
- memcpy(_rooms[i].room_data.vertices,
- _rooms[i].data + data_offset, data_size);
- }
-
- //! \fixme endian conversions for verts needed
- }
-
- data_offset += data_size;
-
- /* identify rectangles */
- //! \fixme endian conversion
- _rooms[i].room_data.num_rectangles =
- *(short *)(void *)(_rooms[i].data + data_offset);
-
- data_offset += sizeof(_rooms[0].room_data.num_rectangles);
- data_size = _rooms[i].room_data.num_rectangles * sizeof(tr2_quad_t);
-
- printDebug("Load", "_rooms[%i].room_data.num_rectangles = %i",
- i, _rooms[i].room_data.num_rectangles);
-
- _rooms[i].room_data.rectangles = 0x0;
-
- if (_rooms[i].room_data.num_rectangles > 0)
- {
- _rooms[i].room_data.rectangles =
- new tr2_quad_t[_rooms[i].room_data.num_rectangles];
- memcpy(_rooms[i].room_data.rectangles,
- _rooms[i].data + data_offset, data_size);
-
- if (mEngineVersion >= TR_VERSION_3)
- {
- for (j = 0; j < _rooms[i].room_data.num_rectangles; ++j)
- {
- _rooms[i].room_data.rectangles[j].texture &= 0x7fff;
- }
- }
- //! \fixme endian conversion
- }
-
- data_offset += data_size;
-
- /* Identify triangles */
- _rooms[i].room_data.num_triangles =
- *(short *)(void *)(_rooms[i].data + data_offset);
- //! \fixme endian
-
- data_offset += sizeof(_rooms[0].room_data.num_triangles);
- data_size = _rooms[i].room_data.num_triangles * sizeof(tr2_tri_t);
-
- printDebug("Load", "_rooms[%i].room_data.num_triangles = %i",
- i, _rooms[i].room_data.num_triangles);
-
- _rooms[i].room_data.triangles = 0x0;
-
- if (_rooms[i].room_data.num_triangles > 0)
- {
- _rooms[i].room_data.triangles =
- new tr2_tri_t[_rooms[i].room_data.num_triangles];
-
- memcpy(_rooms[i].room_data.triangles,
- _rooms[i].data + data_offset, data_size);
-
- if (mEngineVersion >= TR_VERSION_3)
- {
- for (j = 0; j < _rooms[i].room_data.num_triangles; ++j)
- {
- _rooms[i].room_data.triangles[j].texture &= 0x7fff;
- }
- //! \fixme endian
- }
- }
-
- data_offset += data_size;
-
- /* Identify sprites */
- _rooms[i].room_data.num_sprites =
- *(short *)(void *)(_rooms[i].data + data_offset);
- //! \fixme endian
-
- data_offset += sizeof(_rooms[0].room_data.num_sprites);
- data_size = _rooms[i].room_data.num_sprites * sizeof(tr2_room_sprite_t);
-
- printDebug("Load", "_rooms[%i].room_data.num_sprites = %i",
- i, _rooms[i].room_data.num_sprites);
-
- _rooms[i].room_data.sprites = 0x0;
-
- if (_rooms[i].room_data.num_sprites > 0)
- {
- _rooms[i].room_data.sprites =
- new tr2_room_sprite_t[_rooms[i].room_data.num_sprites];
- memcpy(_rooms[i].room_data.sprites,
- _rooms[i].data + data_offset, data_size);
-
- if (mEngineVersion >= TR_VERSION_3)
- {
- for (j = 0; j < _rooms[i].room_data.num_sprites; j++)
- {
- _rooms[i].room_data.sprites[j].texture &= 0x7fff;
- }
- }
-
- //! \fixme endian
- }
-
- /* Free the raw room data */
- delete [] _rooms[i].data;
- _rooms[i].data = NULL;
-
- /* Read door info */
- //! \fixme endian
- Fread(&_rooms[i].num_portals, sizeof(_rooms[0].num_portals), 1, f);
-
- printDebug("Load", "_rooms[%i].num_portals = %i",
- i, _rooms[i].num_portals);
-
- if (_rooms[i].num_portals > 0)
- _rooms[i].portals = new tr2_room_portal_t[_rooms[i].num_portals];
- else
- _rooms[i].portals = 0;
-
- Fread(_rooms[i].portals, sizeof(tr2_room_portal_t),
- _rooms[i].num_portals, f);
- //! \fixme endian
-
- /* Read sector info */
- //! \fixme endian
- Fread(&_rooms[i].num_zsectors, sizeof(_rooms[0].num_zsectors), 1, f);
- Fread(&_rooms[i].num_xsectors, sizeof(_rooms[0].num_xsectors), 1, f);
-
- printDebug("Load", "_rooms[%i].num_zsectors = %i",
- i, _rooms[i].num_zsectors);
- printDebug("Load", "_rooms[%i].num_xsectors = %i",
- i, _rooms[i].num_xsectors);
-
- if (_rooms[i].num_zsectors > 0 && _rooms[i].num_xsectors > 0)
- {
- _rooms[i].sector_list =
- new tr2_room_sector_t[_rooms[i].num_zsectors * _rooms[i].num_xsectors];
- }
- else
- {
- _rooms[i].sector_list = 0x0;
- }
-
- Fread(_rooms[i].sector_list, sizeof(tr2_room_sector_t),
- _rooms[i].num_zsectors * _rooms[i].num_xsectors, f);
- //! \fixme endian
-
- printDebug("Load", "Read %u room sectors",
- _rooms[i].num_zsectors * _rooms[i].num_xsectors);
-
- /* Read room lighting & mode */
- if (mEngineVersion >= TR_VERSION_3)
- {
- Fread(&_rooms[i].intensity1, 4, 1, f);
- // Fake TR2 record:
- _rooms[i].light_mode = 0;
- }
- else if (mEngineVersion == TR_VERSION_1)
- {
- Fread(&_rooms[i].intensity1, 2, 1, f);
- // Is this intensity or LightMode?
-
- printDebug("Load", "_rooms[%i].intensity1 = %u",
- i, _rooms[i].intensity1);
-
- _rooms[i].intensity2 = _rooms[i].intensity1;
- _rooms[i].light_mode = 0;
- }
- else
- { // TR2
- Fread(&_rooms[i].intensity1, 6, 1, f);
- printDebug("Load", "TR2 _rooms[%i].intensity1 = %u",
- i, _rooms[i].intensity1);
- }
-
- /* Read room lighting info */
- //! \fixme endian
- Fread(&_rooms[i].num_lights, sizeof(_rooms[i].num_lights), 1, f);
-
- printDebug("Load", "_rooms[%i].num_lights = %u",
- i, _rooms[i].num_lights);
-
- _rooms[i].lights = 0x0;
- _rooms[i].tr4Lights = 0x0;
-
- // Mongoose 2002.04.03, New TR4 light struct, removed old
- // double size for others
- if (_rooms[i].num_lights > 0)
- {
- if (mEngineVersion == TR_VERSION_1)
- {
- _rooms[i].lights = new tr2_room_light_t[_rooms[i].num_lights];
-
- for (j = 0; j < _rooms[i].num_lights; ++j)
- {
- Fread(&_rooms[i].lights[j].x, sizeof(_rooms[0].lights[0].x), 3,f);
- // x, y, z
-
- printDebug("Load", "_rooms[%i].lights[%i] = <%i %i %i>",
- i, j,
- _rooms[i].lights[j].x,
- _rooms[i].lights[j].y,
- _rooms[i].lights[j].z);
-
- Fread(&_rooms[i].lights[j].intensity1, sizeof(short), 1, f);
- // Intensity1
-
- printDebug("Load", "_rooms[%i].lights[%i].intensity1 = %u",
- i, j,
- _rooms[i].lights[j].intensity1);
-
- _rooms[i].lights[j].intensity2 = _rooms[i].lights[j].intensity1;
- Fread(&_rooms[i].lights[j].fade1, sizeof(unsigned int), 1, f);
- // Fade1
-
- printDebug("Load", "_rooms[%i].lights[%i].fade1 = %u",
- i, j,
- _rooms[i].lights[j].fade1);
-
- _rooms[i].lights[j].fade2 = _rooms[i].lights[j].fade1;
- }
- }
- else if (mEngineVersion == TR_VERSION_4)
- {
- _rooms[i].tr4Lights = new tr4_room_light_t[_rooms[i].num_lights];
-
- Fread(_rooms[i].tr4Lights, sizeof(tr4_room_light_t),
- _rooms[i].num_lights, f);
- }
- else
- {
- _rooms[i].lights = new tr2_room_light_t[_rooms[i].num_lights];
-
- Fread(_rooms[i].lights, sizeof(tr2_room_light_t),
- _rooms[i].num_lights, f);
- }
- }
- //! \fixme endian
-
- /* Read Static Mesh Data */
- Fread(&_rooms[i].num_static_meshes, sizeof(unsigned short), 1, f);
- //! \fixme endian
-
- printDebug("Load", "_rooms[%i].num_static_meshes = %u",
- i, _rooms[i].num_static_meshes);
-
- _rooms[i].static_meshes = 0x0;
-
- if (_rooms[i].num_static_meshes > 0)
- {
- _rooms[i].static_meshes =
- new tr2_room_staticmesh_t[_rooms[i].num_static_meshes];
-
- if (mEngineVersion == TR_VERSION_1)
- {
- for (j = 0; j < _rooms[i].num_static_meshes; j++)
- {
- Fread(&_rooms[i].static_meshes[j], 18, 1, f);
- // Account for the missing .intensity2
-
- _rooms[i].static_meshes[j].object_id =
- _rooms[i].static_meshes[j].intensity2;
-
- _rooms[i].static_meshes[j].intensity2 =
- _rooms[i].static_meshes[j].intensity1;
- }
- }
- else
- {
- Fread(_rooms[i].static_meshes, sizeof(tr2_room_staticmesh_t),
- _rooms[i].num_static_meshes, f);
- }
- }
- //! \fixme endian
-
- Fread(&_rooms[i].alternate_room, sizeof(short), 1, f);
- //! \fixme endian
-
- printDebug("Load", "_rooms[%i].alternate_room = %i",
- i, _rooms[i].alternate_room);
-
- Fread(&_rooms[i].flags, sizeof(short), 1, f);
- //! \fixme endian
-
- printDebug("Load", "_rooms[%i].flags = 0x%x",
- i, _rooms[i].flags);
-
- /* Read TR3 room light colour */
- if (mEngineVersion >= TR_VERSION_3)
- {
- /* we force this to be 3 bytes
- (instead of just sizeof(room_light_colour))
- for Macs and others that can't handle odd-length structures...
- */
- Fread(&_rooms[i].room_light_colour, 3, 1, f);
- printDebug("Load", "TR3 _rooms[%i].room_light_colour {%i %i %i}",
- i,
- _rooms[i].room_light_colour.r,
- _rooms[i].room_light_colour.g,
- _rooms[i].room_light_colour.b);
- }
- }
-
- /* Read floor data */
- /*
- * Really, FloorData should be a per-sector dynamic allocation; however,
- * that requires a parser that can accurately determine where one sector's
- * FloorData ends and another's begins. Until we have that, we'll stick to
- * this crude (but effective) method...
- */
- Fread(&_num_floor_data, sizeof(_num_floor_data), 1, f);
- printDebug("Load", "_num_floor_data = %u", _num_floor_data);
-
- _floor_data = 0x0;
-
- if (_num_floor_data > 0)
- {
- _floor_data = new unsigned short[_num_floor_data];
- Fread(_floor_data, sizeof(short), _num_floor_data, f);
- //! \fixme endian
- }
-
- /* Read mesh data */
- Fread(&num_mesh_data_words, sizeof(num_mesh_data_words), 1, f);
- //! \fixme endian
-
- printDebug("Load", "num_mesh_data_words = %u", num_mesh_data_words);
-
- raw_mesh_data = new unsigned char[num_mesh_data_words*2];
- Fread(raw_mesh_data, 2, num_mesh_data_words, f);
- // Endian-conversion of this data occurs in ExtractMeshes()
-
- printDebug("Load", "Read raw_mesh_data");
-
- /* Read mesh pointers */
- Fread(&num_mesh_pointers, sizeof(num_mesh_pointers), 1, f);
- //! \fixme endian
-
- printDebug("Load", "num_mesh_pointers = %u", num_mesh_pointers);
-
- mesh_pointer_list = new unsigned int[num_mesh_pointers];
- Fread(mesh_pointer_list, sizeof(unsigned int), num_mesh_pointers, f);
- //! \fixme endian
-
- printDebug("Load", "Read mesh_pointer_list");
-
- /* Extract meshes */
- extractMeshes(raw_mesh_data, num_mesh_pointers, mesh_pointer_list);
- delete [] raw_mesh_data;
- delete [] mesh_pointer_list;
-
- /* Read animations */
- Fread(&_num_animations, sizeof(_num_animations), 1, f);
- //! \fixme endian
-
- printDebug("Load", "_num_animations = %u", _num_animations);
-
- _animations = 0x0;
-
- if (_num_animations > 0)
- {
- _animations = new tr2_animation_t[_num_animations];
-
- if (mEngineVersion == TR_VERSION_4)
- {
- tr4_animation_t tr4_anim;
-
- for (i = 0; i < (int)_num_animations; ++i)
- {
- Fread(&tr4_anim, 40, 1, f);
-
- _animations[i].frame_offset = tr4_anim.frame_offset;
- _animations[i].frame_rate = tr4_anim.frame_rate;
- _animations[i].frame_size = tr4_anim.frame_size;
- _animations[i].state_id = tr4_anim.state_id;
- _animations[i].unknown1 = tr4_anim.unknown;
- _animations[i].unknown2 = tr4_anim.speed;
- _animations[i].unknown3 = tr4_anim.accel_lo;
- _animations[i].unknown4 = tr4_anim.accel_hi;
- _animations[i].frame_start = tr4_anim.frame_start;
- _animations[i].frame_end = tr4_anim.frame_end;
- _animations[i].next_animation = tr4_anim.next_animation;
- _animations[i].next_frame = tr4_anim.next_frame;
- _animations[i].num_state_changes = tr4_anim.num_state_changes;
- _animations[i].state_change_offset = tr4_anim.state_change_offset;
- _animations[i].num_anim_commands = (tr4_anim.num_anim_commands > 256)
- ? 0 : tr4_anim.num_anim_commands;
- _animations[i].anim_command = tr4_anim.anim_command;
- }
- }
- else
- {
- Fread(_animations, sizeof(tr2_animation_t), _num_animations, f);
- }
- }
- //! \fixme endian
-
- /* Read state changes */
- Fread(&_num_state_changes, sizeof(_num_state_changes), 1, f);
- //! \fixme endian
-
-
- if (percent)
- (*percent)(80);
-
- printDebug("Load", "_num_state_changes = %u", _num_state_changes);
-
- if (_num_state_changes > 0)
- {
- _state_changes = new tr2_state_change_t[_num_state_changes];
-
- Fread(_state_changes, sizeof(tr2_state_change_t), _num_state_changes, f);
- }
- //! \fixme endian
-
- /* Read AnimDispatches */
- Fread(&_num_anim_dispatches, sizeof(_num_anim_dispatches), 1, f);
- //! \fixme endian
-
- printDebug("Load", "_num_anim_dispatches = %u", _num_anim_dispatches);
-
- _anim_dispatches = 0x0;
-
- if (_num_anim_dispatches > 0)
- {
- _anim_dispatches = new tr2_anim_dispatch_t[_num_anim_dispatches];
- Fread(_anim_dispatches, sizeof(tr2_anim_dispatch_t),
- _num_anim_dispatches, f);
- }
- //! \fixme endian
-
- /* Read anim commands */
- Fread(&_num_anim_commands, sizeof(_num_anim_commands), 1, f);
- //! \fixme endian
-
- printDebug("Load", "_num_anim_commands = %u", _num_anim_commands);
-
- _anim_commands = 0x0;
-
- if (_num_anim_commands > 0)
- {
- _anim_commands = new tr2_anim_command_t[_num_anim_commands];
- Fread(_anim_commands, sizeof(tr2_anim_command_t), _num_anim_commands, f);
- }
- //! \fixme endian
-
- /* Read MeshTrees */
- Fread(&_num_mesh_trees, sizeof(_num_mesh_trees), 1, f);
- //! \fixme endian
-
- printDebug("Load", "_num_mesh_trees = %u", _num_mesh_trees);
-
- _mesh_trees = 0x0;
-
- if (_num_mesh_trees > 0)
- {
- _mesh_trees = new tr2_meshtree_t[_num_mesh_trees];
- Fread(_mesh_trees, sizeof(int), _num_mesh_trees, f);
- }
- //! \fixme endian
-
- /* Read frames */
- Fread(&_num_frames, sizeof(_num_frames), 1, f);
- //! \fixme endian
-
- printDebug("Load", "_num_frames = %u", _num_frames);
-
- _frames = 0x0;
-
- if (_num_frames > 0)
- {
- _frames = new unsigned short[_num_frames];
- Fread(_frames, 2, _num_frames, f);
- //! \fixme endian
-
- if (mEngineVersion == TR_VERSION_1)
- {
- // re-format the frames[] to look like TR2 frames
- int num_frames;
-
- for (j = 0; j < (int)_num_animations; ++j)
- {
- int fo = _animations[j].frame_offset / 2;
- _animations[j].frame_size = (unsigned char)(_frames[fo + 9] * 2) + 10;
- }
-
- for (i = 0; i < (int)_num_frames; )
- {
- i += 9; // point to num_frames;
- j = i; // get rid of (overwrite) num_frames
- num_frames = _frames[i++];
-
- while (num_frames--)
- {
- _frames[j++] = _frames[i + 1]; // reverse the words as we go
- _frames[j++] = _frames[i];
- i += 2;
- }
- }
- }
- }
-
- /* Read moveables */
- Fread(&_num_moveables, sizeof(_num_moveables), 1, f);
- //! \fixme endian
- printDebug("Load", "_num_moveables = %u", _num_moveables);
-
- _moveables = 0x0;
-
- if (_num_moveables > 0)
- {
- debugf = ftell(f);
- _moveables = new tr2_moveable_t[_num_moveables];
- Fread(_moveables, 18, _num_moveables, f);
- }
- //! \fixme endian
-
- Fread(&_num_static_meshes, sizeof(int), 1, f);
- //! \fixme endian
- printDebug("Load", "_num_static_meshes = %u", _num_static_meshes);
-
- // SAFE EXIT //////////////////////////
-
- _static_meshes = 0x0;
-
- if (_num_static_meshes > 0)
- {
- _static_meshes = new tr2_staticmesh_t[_num_static_meshes];
- Fread(_static_meshes, sizeof(tr2_staticmesh_t),
- _num_static_meshes, f);
- //! \fixme endian
- }
-
- _object_textures = 0x0;
-
- if (mEngineVersion < TR_VERSION_3)
- {
- /* Read object textures */
- Fread(&_num_object_textures, sizeof(int), 1, f);
- printDebug("Load", "_num_object_textures = %u", _num_object_textures);
- //! \fixme endian
-
- if (_num_object_textures > 0)
- {
- _object_textures = new tr2_object_texture_t[_num_object_textures];
-
- Fread(_object_textures, sizeof(tr2_object_texture_t),
- _num_object_textures, f);
- }
- //! \fixme endian
- }
-
-
- if (percent)
- (*percent)(90);
-
- if (mEngineVersion == TR_VERSION_4)
- {
- unsigned char zzbuf[4];
- Fread(zzbuf, 1, 3, f); // skip "SPR"
- zzbuf[3] = 0;
-
- printDebug("Load", "TR4 checking if %s == SPR", zzbuf);
- }
-
- /* Read sprite textures */
- Fread(&_num_sprite_textures, sizeof(int), 1, f);
- //! \fixme endian
- printDebug("Load", "_num_sprite_textures = %u", _num_sprite_textures);
-
- _sprite_textures = 0x0;
-
- if (_num_sprite_textures > 0)
- {
- _sprite_textures = new tr2_sprite_texture_t[_num_sprite_textures];
- Fread(_sprite_textures, sizeof(tr2_sprite_texture_t),
- _num_sprite_textures, f);
- }
- //! \fixme endian
-
- /* Read sprite texture data (?) */
- Fread(&_num_sprite_sequences, sizeof(int), 1, f);
- //! \fixme endian
- printDebug("Load", "_num_sprite_sequences = %u", _num_sprite_sequences);
-
- _sprite_sequences = 0x0;
-
- if (_num_sprite_sequences > 0)
- {
- _sprite_sequences = new tr2_sprite_sequence_t[_num_sprite_sequences];
- Fread(_sprite_sequences, sizeof(tr2_sprite_sequence_t),
- _num_sprite_sequences, f);
- }
- //! \fixme endian
-
- /* Read cameras */
- Fread(&_num_cameras, sizeof(_num_cameras), 1, f);
- //! \fixme endian
- printDebug("Load", "_num_cameras = %i", _num_cameras);
-
- _cameras = 0x0;
-
- if (_num_cameras > 0)
- {
- _cameras = new tr2_camera_t[_num_cameras];
- Fread(_cameras, sizeof(tr2_camera_t), _num_cameras, f);
- //! \fixme endian
- }
-
- if (mEngineVersion == TR_VERSION_4)
- {
- int num_ex_cam;
- tr4_extra_camera_t *ex_cam;
-
- Fread(&num_ex_cam, 4, 1, f);
-
- printDebug("Load", "num_extra_cam = %i", num_ex_cam);
-
- if (num_ex_cam > 0)
- {
- ex_cam = new tr4_extra_camera_t[num_ex_cam];
- Fread(ex_cam, sizeof(tr4_extra_camera_t), num_ex_cam, f);
- delete [] ex_cam;
- }
- }
-
- /* Read sound effects (?) */
- Fread(&_num_sound_sources, sizeof(_num_sound_sources), 1, f);
- //! \fixme endian
- printDebug("Load", "_num_sound_sources = %i", _num_sound_sources);
-
- _sound_sources = 0x0;
-
- if (_num_sound_sources > 0)
- {
- _sound_sources =
- (tr2_sound_source_t*) new unsigned char[_num_sound_sources*40];
-
- Fread(_sound_sources, sizeof(tr2_sound_source_t),
- _num_sound_sources, f);
- //! \fixme endian
- }
-
- #ifdef OBSOLETE
- if (mEngineVersion == TR_VERSION_4)
- {
- unsigned int num_ZZ;
- unsigned char zzbuf[17];
- Fread(&num_ZZ, 1, sizeof(num_ZZ), f);
-
- while (num_ZZ--)
- {
- Fread(zzbuf, 1, 16, f);
- zzbuf[16] = 0;
- printDebug("Load", "TR4 zz dump '%s'", zzbuf);
- }
- }
- #endif
-
- /* Read boxes */
- Fread(&_num_boxes, sizeof(_num_boxes), 1, f);
- //! \fixme endian
- printDebug("Load", "_num_boxes = %i", _num_boxes);
-
- _boxes = 0x0;
-
- if (_num_boxes > 0)
- {
- _boxes = new tr2_box_t[_num_boxes];
-
- if (mEngineVersion == TR_VERSION_1)
- {
- struct tr1_box
- {
- int zmin, zmax, xmin, xmax;
- short true_floor, overlap_index;
- } __attribute__ ((packed)) *tr1box;
-
- tr1box = new tr1_box[_num_boxes];
-
- Fread(tr1box, sizeof(struct tr1_box), _num_boxes, f);
- //! \fixme endian
-
- for (j = 0; j < _num_boxes; ++j)
- {
- _boxes[j].zmin = (unsigned char)(tr1box[j].zmin / 1024);
- _boxes[j].zmax = (unsigned char)(tr1box[j].zmax / 1024);
- _boxes[j].xmin = (unsigned char)(tr1box[j].xmin / 1024);
- _boxes[j].xmax = (unsigned char)(tr1box[j].xmax / 1024);
- _boxes[j].true_floor = tr1box[j].true_floor;
- _boxes[j].overlap_index = tr1box[j].overlap_index;
- }
-
- delete [] tr1box;
- }
- else
- {
- Fread(_boxes, sizeof(tr2_box_t), _num_boxes, f);
- }
- //! \fixme endian
- }
-
- /* Read overlaps (?) */
- Fread(&_num_overlaps, sizeof(_num_overlaps), 1, f);
- //! \fixme endian
- printDebug("Load", "_num_overlaps = %i", _num_overlaps);
-
- _overlaps = 0x0;
-
- if (_num_overlaps > 0)
- {
- _overlaps = new short[_num_overlaps];
- Fread(_overlaps, 2, _num_overlaps, f);
- //! \fixme endian
- }
-
- _zones = 0x0;
-
- /* Read Zones */
- if (_num_boxes > 0)
- {
- _zones = new short[_num_boxes*10];
-
- if (mEngineVersion == TR_VERSION_1)
- {
- Fread(_zones, 12, _num_boxes, f);
- }
- else
- {
- Fread(_zones, 20, _num_boxes, f);
- }
- //! \fixme endian
- }
-
- /* Read animation textures (?) */
- Fread(&_num_animated_textures, sizeof(_num_animated_textures), 1, f);
- //! \fixme endian
- printDebug("Load", "_num_animated_textures = %i", _num_animated_textures);
-
- _animated_textures = 0x0;
-
- if (_num_animated_textures > 0)
- {
- _animated_textures = new short[_num_animated_textures];
- Fread(_animated_textures, 2, _num_animated_textures, f);
- //! \fixme endian
- }
-
- if (mEngineVersion >= TR_VERSION_3)
- {
- /* Read object textures */
- if (mEngineVersion == TR_VERSION_4)
- {
- unsigned char zzbuf[5];
- Fread(zzbuf, 1, 4, f); // skip "TEX"
- //!! this should be 3, but we have a bug...
- zzbuf[4] = 0;
- printDebug("Load", "TR4 checking %s == TEX", zzbuf);
- }
-
- Fread(&_num_object_textures, sizeof(_num_object_textures), 1, f);
- //! \fixme endian
-
- printDebug("Load", "_num_object_textures = %i", _num_object_textures);
-
- _object_textures = 0x0;
-
- if (_num_object_textures > 0)
- {
- // Used to be 2 * num, and I forgot why...
- _object_textures = new tr2_object_texture_t[_num_object_textures];
-
- //! \fixme This is fu fu fu fu fu fu
- if (mEngineVersion == TR_VERSION_4)
- {
- int jjj, kkk;
- tr4_object_texture_t *tr4_tex;
-
-
- tr4_tex = new tr4_object_texture_t[_num_object_textures];
-
- Fread(tr4_tex, 38, _num_object_textures, f);
-
- for (jjj = 0; jjj < (int)_num_object_textures; ++jjj)
- {
- _object_textures[jjj].transparency_flags =
- tr4_tex[jjj].attribute;
-
- _object_textures[jjj].tile =
- (unsigned short)tr4_tex[jjj].tile & 0x7fff;
-
- for (kkk = 0; kkk < 4; ++kkk)
- {
- _object_textures[jjj].vertices[kkk].xcoordinate =
- tr4_tex[jjj].vertices[kkk].xcoordinate;
- _object_textures[jjj].vertices[kkk].xpixel =
- tr4_tex[jjj].vertices[kkk].xpixel;
- _object_textures[jjj].vertices[kkk].ycoordinate =
- tr4_tex[jjj].vertices[kkk].ycoordinate;
- _object_textures[jjj].vertices[kkk].ypixel =
- tr4_tex[jjj].vertices[kkk].ypixel;
- }
- }
-
- delete [] tr4_tex;
- }
- else
- {
- Fread(_object_textures, sizeof(tr2_object_texture_t),
- _num_object_textures, f);
- }
- }
- //! \fixme endian
- }
-
- /* Read items */
- Fread(&_num_items, sizeof(_num_items), 1, f);
- //! \fixme endian
- printDebug("Load", "_num_items = %i", _num_items);
-
- _items = 0x0;
-
- if (_num_items > 0)
- {
- _items = new tr2_item_t[_num_items];
-
- if (mEngineVersion == TR_VERSION_1)
- {
- for (i = 0; i < _num_items; ++i)
- {
- Fread(&_items[i], sizeof(tr2_item_t) - 2, 1, f);
- _items[i].flags = _items[i].intensity2;
- _items[i].intensity2 = _items[i].intensity1;
- }
- }
- else
- {
- Fread(_items, sizeof(tr2_item_t), _num_items, f);
- }
- }
- //! \fixme endian
-
- /* Read LightMaps */
- _light_map = new unsigned char[32 * 256];
-
- if (mEngineVersion != TR_VERSION_4)
- {
- Fread(_light_map, 32, 256, f);
- }
-
- if (mEngineVersion == TR_VERSION_1)
- {
- /* read the 8-bit palette */
- Fread(_palette8, sizeof(tr2_colour_t), 256, f);
- printDebug("Load", "Read TR 1 palette");
-
- // build 16-bit textiles from 8-bit
- // (no extra colours, but creates consistent .TR2 file)
- for (i = 0; i < (int)_num_textiles; ++i)
- {
- unsigned short argb;
- double colour_tmp;
-
- for (j = 0; j < (256 * 256); ++j)
- {
- colour_tmp = _palette8[_textile8[i].tile[j]].r & 0x3f;
- colour_tmp = colour_tmp * 31.0 / 63.0;
- argb = (unsigned short)(((int)colour_tmp) << 10);
- colour_tmp = _palette8[_textile8[i].tile[j]].g & 0x3f;
- colour_tmp = colour_tmp * 31.0 / 63.0;
- argb |= (unsigned short)(((int)colour_tmp) << 5);
- colour_tmp = _palette8[_textile8[i].tile[j]].b & 0x3f;
- colour_tmp = colour_tmp * 31.0 / 63.0;
- argb |= (unsigned short)((int)colour_tmp);
- argb &= 0x7fff; // ???
-
- if (_textile8[i].tile[j] != 0)
- argb |= 0x8000;
-
- _textile16[i].tile[j] = argb;
- }
- }
- }
-
- /* Read cinematic frames */
- if (mEngineVersion == TR_VERSION_4)
- {
- unsigned int num_ai_data;
-
-
- Fread(&num_ai_data, 4, 1, f);
- printDebug("Load", "num_ai_data = %i", num_ai_data);
-
- tr4_ai_object_t *ai_obj = 0x0;
-
- if (num_ai_data > 0)
- {
- ai_obj = new tr4_ai_object_t[num_ai_data];
- Fread(ai_obj, sizeof(tr4_ai_object_t), num_ai_data, f);
-
- delete [] ai_obj;
- }
- }
- else
- {
- unsigned short num_cinematic_frames;
-
- Fread(&num_cinematic_frames, sizeof(num_cinematic_frames), 1, f);
- //! \fixme endian
-
- _num_cinematic_frames = num_cinematic_frames;
-
- printDebug("Load", "_num_cinematic_frames = %i", _num_cinematic_frames);
-
- _cinematic_frames = 0x0;
-
- if (_num_cinematic_frames > 0)
- {
- _cinematic_frames = new tr2_cinematic_frame_t[_num_cinematic_frames];
-
- Fread(_cinematic_frames, sizeof(tr2_cinematic_frame_t),
- _num_cinematic_frames, f);
- // There may or may not be endian conversion required here - I have
- // no idea what this data is.
- }
- }
-
- /* Read demodata (?) */
- Fread(&_num_demo_data, sizeof(_num_demo_data), 1, f);
- //! \fixme endian
- printDebug("Load", "_num_demo_data = %i", _num_demo_data);
-
- _demo_data = 0x0;
-
- if (_num_demo_data > 0)
- {
- _demo_data = new unsigned char[_num_demo_data];
- Fread(_demo_data, 1, _num_demo_data, f);
- // There may or may not be endian conversion required here - I have
- // no idea what this data is.
- }
-
- /* Read SoundMap */
- mSoundMap = new short[370];
-
- if (mEngineVersion == TR_VERSION_1)
- {
- Fread(mSoundMap, sizeof(short), 256, f);
- //memset(_sound_map, 0, 370 * sizeof(short)); //! \fixme KLUDGE!!!
- }
- else
- {
- Fread(mSoundMap, sizeof(short), 370, f);
- }
- //! \fixme endian
-
- /* Read SoundDetails */
- Fread(&mNumSoundDetails, sizeof(mNumSoundDetails), 1, f);
- printDebug("Load", "mNumSoundDetails = %i", mNumSoundDetails);
- //! \fixme endian
-
- mSoundDetails = 0x0;
-
- if (mNumSoundDetails > 0)
- {
- mSoundDetails = new tr2_sound_details_t[mNumSoundDetails];
- Fread(mSoundDetails, sizeof(tr2_sound_details_t), mNumSoundDetails, f);
- }
- //! \fixme endian
-
- // Read sound sample indices
- mSampleIndices = 0x0;
- mNumSampleIndices = 0;
- mRiffDataSz = 0;
- mRiffData = 0x0;
- mNumTR4Samples = 0;
- mTR4Samples = 0x0;
-
- switch (mEngineVersion)
- {
- case TR_VERSION_1:
- Fread(&mRiffDataSz, 4, 1, f);
- printDebug("Load", "mRiffDataSz = %ibytes", mRiffDataSz);
-
- if (mRiffDataSz > 0)
- {
- mRiffData = new unsigned char[mRiffDataSz];
- Fread(mRiffData, 1, mRiffDataSz, f);
- }
-
- Fread(&mNumSampleIndices, 4, 1, f);
- printDebug("Load", "mNumSampleIndices = %i", mNumSampleIndices);
-
- if (mNumSampleIndices > 0)
- {
- mSampleIndices = new int[mNumSampleIndices];
- //! \fixme (Endian)
- Fread(mSampleIndices, 4, mNumSampleIndices, f);
- }
- break;
- case TR_VERSION_4:
- mFreadMode = TR_FREAD_NORMAL;
- // 0x46464952
-
- //! \fixme (Endian) Read bitu32 / u_int32_t
- Fread(&mNumTR4Samples, 4, 1, f);
- printDebug("Load", "mNumTR4Samples = %i", mNumTR4Samples);
-
- mRiffDataSz = 0;
- mTR4Samples = new unsigned char *[mNumTR4Samples];
- mTR4SamplesSz = new unsigned int[mNumTR4Samples];
-
- memset(mTR4SamplesSz, 0, mNumTR4Samples*4);
-
- for (i = 0; i < (int)mNumTR4Samples; ++i)
- {
- unsigned int sizeCompressed;
- unsigned int sizeUncompressed;
- unsigned char *compressedSoundSample;
- unsigned char *unCompressedSoundSample;
- int zErr;
- uLongf libzUncompressedSize;
-
-
- Fread(&sizeUncompressed, 4, 1, f);
- printDebug("Load", " sizeUncompressed = %i", sizeUncompressed);
- Fread(&sizeCompressed, 4, 1, f);
- printDebug("Load", " sizeCompressed = %i", sizeCompressed);
-
- compressedSoundSample = new unsigned char[sizeCompressed];
- unCompressedSoundSample = new unsigned char[sizeUncompressed];
-
- //printDebug("Load", " %lubytes read from file", ftell(f));
- Fread(compressedSoundSample, sizeCompressed, 1, f);
-
- printDebug("Load", " %c%c%c%c should be RIFF",
- compressedSoundSample[0],
- compressedSoundSample[1],
- compressedSoundSample[2],
- compressedSoundSample[3]);
-
- //#define NEVER_DECOMPRESS
- #ifdef NEVER_DECOMPRESS
- mTR4Samples[i] = compressedSoundSample;
- mTR4SamplesSz[i] = sizeCompressed;
-
- delete [] unCompressedSoundSample;
- #else
- // Decompress the sample
- libzUncompressedSize = sizeUncompressed;
-
- zErr = uncompress(unCompressedSoundSample,
- &libzUncompressedSize,
- compressedSoundSample,
- sizeCompressed);
-
- sizeUncompressed = libzUncompressedSize;
-
- switch (zErr)
- {
- case Z_MEM_ERROR:
- printDebug("Load", " Decompress Error: not enough memory");
- break;
- case Z_BUF_ERROR:
- printDebug("Load", " Decompress Error: output buffer too small");
- break;
- case Z_DATA_ERROR:
- printDebug("Load", " Decompress Error: input data was corrupted");
- break;
- case Z_OK:
- printDebug("Load", " Decompress OK");
- break;
- default:
- printDebug("Load", " Decompress Error: decompress error #%i", zErr);
- }
-
- // Hhhmm... handle uncompressed RIFFs too?
- if (zErr == Z_OK)
- {
- mTR4Samples[i] = unCompressedSoundSample;
- mTR4SamplesSz[i] = sizeUncompressed;
- delete [] compressedSoundSample;
- }
- else
- {
- printDebug("Load", " %lubytes read from file", ftell(f));
- mTR4Samples[i] = compressedSoundSample;
- mTR4SamplesSz[i] = sizeCompressed;
- delete [] unCompressedSoundSample;
- }
- #endif
- }
- break;
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_5:
- case TR_VERSION_UNKNOWN:
- //! \fixme (Endian) Read bit32 / int32_t
- Fread(&mNumSampleIndices, 4, 1, f);
- printDebug("Load", "mNumSampleIndices = %i", mNumSampleIndices);
- if (mNumSampleIndices > 0)
- {
- mSampleIndices = new int[mNumSampleIndices];
- //! \fixme (Endian)
- Fread(mSampleIndices, 4, mNumSampleIndices, f);
- }
- break;
- }
-
- if (mCompressedLevelData)
- {
- printDebug("Load", "Freeing uncompressed TR4 data");
- delete [] mCompressedLevelData;
- }
-
- //! \fixme memory damage?
- mCompressedLevelData = NULL;
-
- fclose(f);
-
- if (percent)
- (*percent)(100);
-
- return 0;
- }
-
-
- ////////////////////////////////////////////////////////////
- // Public Accessors
- ////////////////////////////////////////////////////////////
-
- float TombRaider::adjustTexel(unsigned char texel, char offset)
- {
- if (offset >= 0)
- texel++;
- else
- texel--;
-
- return ((float)texel / 255.0f);
- }
-
-
- void TombRaider::computeRotationAngles(unsigned short **frame,
- unsigned int *frame_offset,
- unsigned int *angle_offset,
- float *x, float *y, float *z)
- {
- unsigned short itmp, itmp2;
- float angle;
-
-
- itmp = (*frame)[(*frame_offset) + (*angle_offset)++];
-
- if (Engine() == TR_VERSION_1)
- {
- // All angles are three-axis
- angle = (itmp >> 4) & 0x03ff;
- angle *= 360.0 / 1024.0;
- *x = angle;
-
- itmp2 = (itmp << 6) & 0x03c0;
-
- // Get Z rotation
- itmp = (*frame)[(*frame_offset) + (*angle_offset)];
- ++(*angle_offset);
-
- itmp2 |= (itmp >> 10) & 0x003f;
- angle = itmp2;
- angle *= 360.0 / 1024.0;
- *y = angle;
-
- angle = itmp & 0x3ff;
- angle *= 360.0 / 1024.0;
- *z = angle;
- }
- else if (itmp & 0xc000)
- {
- // TR2, TR3, TR4 - single axis of rotation
- if (Engine() == TR_VERSION_4)
- {
- angle = itmp & 0x0fff;
- angle /= 4096.0;
- angle *= 360.0;
- }
- else
- {
- angle = itmp & 0x3ff;
- angle /= 1024.0;
- angle *= 360.0;
- }
-
- switch (itmp & 0xc000)
- {
- case 0x4000:
- *x = angle;
- break;
- case 0x8000:
- *y = angle;
- break;
- case 0xc000:
- *z = angle;
- break;
- }
- }
- else // TR2, TR3, TR4 - three axes
- {
- angle = (itmp >> 4) & 0x03ff;
- angle *= 360.0 / 1024.0;
- *x = angle;
-
- itmp2 = (itmp << 6) & 0x03c0;
- itmp = (*frame)[(*frame_offset) + (*angle_offset)++]; // get Z rotation
-
- itmp2 |= (itmp >> 10) & 0x003f;
- angle = itmp2;
- angle *= 360.0 / 1024.0;
- *y = angle;
-
- angle = itmp & 0x3ff;
- angle *= 360.0 / 1024.0;
- *z = angle;
- }
- }
-
-
- void TombRaider::computeUV(tr2_object_texture_vert_t *st, float *u, float *v)
- {
- unsigned char x, y;
-
-
- if (!st || !u || !v)
- return;
-
- x = st->xpixel;
- y = st->ypixel;
-
- x += (char)st->xcoordinate;
- y += (char)st->ycoordinate;
-
- //x += ((char)st->xcoordinate >= 0) ? 1 : -1;
- //y += ((char)st->ycoordinate >= 0) ? 1 : -1;
-
- *u = (float)x / 255.0f;
- *v = (float)y / 255.0f;
- }
-
-
- int TombRaider::getBumpMapCount()
- {
- return _num_bump_map_textures / 2;
- }
-
-
- void TombRaider::getColor(int index, float color[4])
- {
- switch (getEngine())
- {
- case TR_VERSION_1:
- color[0] = _palette8[index].r / 64.0f;
- color[1] = _palette8[index].g / 64.0f;
- color[2] = _palette8[index].b / 64.0f;
- //color[0] = (_palette8[index].r & 0xfd) / 64.0f;
- //color[1] = (_palette8[index].g & 0xfd) / 64.0f;
- //color[2] = (_palette8[index].b & 0xfd) / 64.0f;
- color[3] = 1.0;
- break;
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- case TR_VERSION_5:
- case TR_VERSION_UNKNOWN:
- color[0] = (float)(_palette16[index] & 0xff) / 256.0f;
- color[1] = (float)((_palette16[index] >> 8) & 0xff) / 256.0f;
- color[2] = (float)((_palette16[index] >> 16) & 0xff) / 256.0f;
- color[3] = 1.0;
- break;
- }
- }
-
-
- tr2_version_type TombRaider::getEngine()
- {
- return mEngineVersion;
- }
-
-
- void TombRaider::getMeshCollisionInfo(unsigned int meshIndex,
- float center[3], float *radius)
- {
- if ((int)meshIndex > mMeshCount)
- {
- print("getMeshCollisionInfo", "Assertion error: invalid mesh index\n");
- return;
- }
-
- center[0] = mMeshes[meshIndex].centre.x;
- center[1] = mMeshes[meshIndex].centre.y;
- center[2] = mMeshes[meshIndex].centre.z;
- *radius = mMeshes[meshIndex].collision_size;
- }
-
-
- int TombRaider::getMeshCount()
- {
- return mMeshCount;
- }
-
-
- //! \fixme Needs refinement once the ideal format it's feeding is refined
- // I should stick a HACK postfix on the method name - it's temporary
- // until an array format can be crafted from a pinned down design and
- // RE notes review session ( eg what about TR5? )
- void TombRaider::getMeshColoredRectangle(unsigned int meshIndex,
- unsigned int faceIndex,
- int *index, float *color)
- {
- unsigned int t1_1, t1_2, t1_3, t2_1, t2_2, t2_3;
-
-
- if ((int)meshIndex > mMeshCount)
- {
- print("getMeshColoredRectangle", "Assertion error: invalid mesh index\n");
- return;
- }
-
- // Make 2 triangles from one quad!
- t1_1 = 0;
- t1_2 = 1;
- t1_3 = 2;
-
- t2_1 = 3;
- t2_2 = 0;
- t2_3 = 2;
-
- index[0] = mMeshes[meshIndex].coloured_rectangles[faceIndex].vertices[t1_1];
- index[1] = mMeshes[meshIndex].coloured_rectangles[faceIndex].vertices[t1_2];
- index[2] = mMeshes[meshIndex].coloured_rectangles[faceIndex].vertices[t1_3];
- index[3] = mMeshes[meshIndex].coloured_rectangles[faceIndex].vertices[t2_1];
- index[4] = mMeshes[meshIndex].coloured_rectangles[faceIndex].vertices[t2_2];
- index[5] = mMeshes[meshIndex].coloured_rectangles[faceIndex].vertices[t2_3];
-
- switch (Engine())
- {
- case TR_VERSION_1:
- getColor(mMeshes[meshIndex].coloured_rectangles[faceIndex].texture & 0xff, color);
- break;
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- case TR_VERSION_5:
- case TR_VERSION_UNKNOWN:
- getColor((mMeshes[meshIndex].coloured_rectangles[faceIndex].texture>>8) & 0xff, color);
- break;
- }
- }
-
-
- void TombRaider::getMeshColoredTriangle(unsigned int meshIndex,
- unsigned int faceIndex,
- int *index, float *color)
- {
- if ((int)meshIndex > mMeshCount)
- {
- print("getMeshColoredTriangle", "Assertion error: invalid mesh index\n");
- return;
- }
-
- index[0] = mMeshes[meshIndex].coloured_triangles[faceIndex].vertices[0];
- index[1] = mMeshes[meshIndex].coloured_triangles[faceIndex].vertices[1];
- index[2] = mMeshes[meshIndex].coloured_triangles[faceIndex].vertices[2];
-
- switch (Engine())
- {
- case TR_VERSION_1:
- getColor(mMeshes[meshIndex].coloured_triangles[faceIndex].texture & 0xff,
- color);
- break;
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- case TR_VERSION_5:
- case TR_VERSION_UNKNOWN:
- getColor((mMeshes[meshIndex].coloured_triangles[faceIndex].texture>>8) & 0xff,
- color);
- break;
- }
- }
-
-
- void TombRaider::getMeshTexturedRectangle(unsigned int meshIndex,
- unsigned int faceIndex,
- int *index, float *st, int *texture,
- unsigned short *transparency)
- {
- unsigned int t1_1, t1_2, t1_3, t2_1, t2_2, t2_3;
- static bool givenWarning = false; // hahaha... okay less spewing
- tr2_mesh_t *meshes;
- tr2_object_texture_t *object_texture;
- int t_index;
- unsigned int i, m;
-
-
- if ((int)meshIndex > mMeshCount)
- {
- print("getMeshTexturedRectangle", "Assertion error: invalid mesh index\n");
- return;
- }
-
- m = meshIndex; // lazy
- meshes = mMeshes; // lazy
- object_texture = _object_textures; // lazy
- i = faceIndex; // lazy
-
- // Make 2 triangles from one quad!
- t1_1 = 0;
- t1_2 = 1;
- t1_3 = 2;
-
- t2_1 = 3;
- t2_2 = 0;
- t2_3 = 2;
-
- t_index = meshes[m].textured_rectangles[i].texture;
-
- index[0] = meshes[m].textured_rectangles[i].vertices[t1_1];
- index[1] = meshes[m].textured_rectangles[i].vertices[t1_2];
- index[2] = meshes[m].textured_rectangles[i].vertices[t1_3];
- index[3] = meshes[m].textured_rectangles[i].vertices[t2_1];
- index[4] = meshes[m].textured_rectangles[i].vertices[t2_2];
- index[5] = meshes[m].textured_rectangles[i].vertices[t2_3];
-
- computeUV(object_texture[t_index].vertices+t1_1, (st), (st)+1);
- computeUV(object_texture[t_index].vertices+t1_2, (st)+2, (st)+3);
- computeUV(object_texture[t_index].vertices+t1_3, (st)+4, (st)+5);
- computeUV(object_texture[t_index].vertices+t2_1, (st)+6, (st)+7);
- computeUV(object_texture[t_index].vertices+t2_2, (st)+8, (st)+9);
- computeUV(object_texture[t_index].vertices+t2_3, (st)+10, (st)+11);
-
- *texture = object_texture[t_index].tile;
- *transparency = object_texture[t_index].transparency_flags;
-
- // TR3+ alpha Textured polygons
- if (!givenWarning && *transparency == 2)
- {
- givenWarning = true;
-
- //! \fixme Use Material class to handle greyscale alpha intensity
- // (partial alpha)
- print("getMeshTexturedRectangle",
- "TR3+ greyscale alpha intensity not implmented, %s:%i",
- __FILE__, __LINE__);
- }
- }
-
-
- void TombRaider::getMeshTexturedTriangle(unsigned int meshIndex,
- unsigned int faceIndex,
- int *index, float *st, int *texture,
- unsigned short *transparency)
- {
- static bool givenWarning = false; // hahaha... okay less spewing
- tr2_mesh_t *meshes;
- tr2_object_texture_t *object_texture;
- int t_index;
- unsigned int i;
-
-
- if ((int)meshIndex > mMeshCount)
- {
- print("getMeshTexturedTriangle", "Assertion error: invalid mesh index\n");
- return;
- }
-
- meshes = mMeshes; // lazy
- object_texture = _object_textures; // lazy
- i = faceIndex; // lazy
-
- t_index = meshes[meshIndex].textured_triangles[i].texture;
-
- index[0] = meshes[meshIndex].textured_triangles[i].vertices[0];
- index[1] = meshes[meshIndex].textured_triangles[i].vertices[1];
- index[2] = meshes[meshIndex].textured_triangles[i].vertices[2];
-
- computeUV(object_texture[t_index].vertices, (st), (st)+1);
- computeUV(object_texture[t_index].vertices+1, (st)+2, (st)+3);
- computeUV(object_texture[t_index].vertices+2, (st)+4, (st)+5);
-
- *texture = object_texture[t_index].tile;
- *transparency = object_texture[t_index].transparency_flags;
-
- // TR3+ alpha Textured polygons
- if (!givenWarning && *transparency == 2)
- {
- givenWarning = true;
-
- //! \fixme Use Material class to handle greyscale alpha intensity
- // (partial alpha)
- print("getMeshTexturedTriangle",
- "TR3+ greyscale alpha intensity not implmented, %s:%i",
- __FILE__, __LINE__);
- }
- }
-
-
- int TombRaider::getMeshTexturedTriangleCount(unsigned int meshIndex)
- {
- if ((int)meshIndex > mMeshCount)
- {
- print("getMeshTexturedTriangleCount", "Assertion error: invalid mesh index\n");
- return 0;
- }
-
- return ((mMeshes[meshIndex].num_textured_triangles <= 0) ? 0 :
- mMeshes[meshIndex].num_textured_triangles);
- }
-
-
- int TombRaider::getMeshColoredTriangleCount(unsigned int meshIndex)
- {
- if ((int)meshIndex > mMeshCount)
- {
- print("getMeshColoredTriangleCount", "Assertion error: invalid mesh index\n");
- return 0;
- }
-
- return ((mMeshes[meshIndex].num_coloured_triangles <= 0) ? 0 :
- mMeshes[meshIndex].num_coloured_triangles);
- }
-
-
- int TombRaider::getMeshTexturedRectangleCount(unsigned int meshIndex)
- {
- if ((int)meshIndex > mMeshCount)
- {
- print("getMeshTexturedRectangleCount", "Assertion error: invalid mesh index\n");
- return 0;
- }
-
- return ((mMeshes[meshIndex].num_textured_rectangles <= 0) ? 0 :
- mMeshes[meshIndex].num_textured_rectangles);
- }
-
-
- int TombRaider::getMeshColoredRectangleCount(unsigned int meshIndex)
- {
- if ((int)meshIndex > mMeshCount)
- {
- print("getMeshColoredRectangleCount", "Assertion error: invalid mesh index\n");
- return 0;
- }
-
- return ((mMeshes[meshIndex].num_coloured_rectangles <= 0) ? 0:
- mMeshes[meshIndex].num_coloured_rectangles);
- }
-
-
- //! \fixme Perhaps making color an 8bit intensity would be a better idea
- void TombRaider::getMeshVertexArrays(unsigned int meshIndex,
- unsigned int *vertexCount, float **verts,
- unsigned int *normalCount, float **norms,
- unsigned int *colorCount, float **colors)
- {
- unsigned int i;
- float colorValue;
-
-
- *vertexCount = 0;
- *verts = 0x0;
- *normalCount = 0;
- *norms = 0x0;
- *colorCount = 0;
- *colors = 0x0;
-
- if ((int)meshIndex > mMeshCount || mMeshes[meshIndex].num_vertices < 0)
- return;
-
- // Vertices
- *vertexCount = mMeshes[meshIndex].num_vertices;
- *verts = new float[*vertexCount * 3];
-
- for (i = 0; i < *vertexCount; ++i)
- {
- (*verts)[i*3] = mMeshes[meshIndex].vertices[i].x;
- (*verts)[i*3+1] = mMeshes[meshIndex].vertices[i].y;
- (*verts)[i*3+2] = mMeshes[meshIndex].vertices[i].z;
- }
-
-
- // Normals, if any
- if (mMeshes[meshIndex].num_normals > 0 &&
- mMeshes[meshIndex].normals &&
- mMeshes[meshIndex].num_normals == mMeshes[meshIndex].num_vertices)
- {
- *normalCount = mMeshes[meshIndex].num_vertices;
- *norms = new float[*normalCount * 3];
-
- for (i = 0; i < *normalCount; ++i)
- {
- (*norms)[i*3] = mMeshes[meshIndex].normals[i].x;
- (*norms)[i*3+1] = mMeshes[meshIndex].normals[i].y;
- (*norms)[i*3+2] = mMeshes[meshIndex].normals[i].z;
- }
- }
- // Vertex lighting/colors, if any
- else if (mMeshes[meshIndex].num_vertices > 0 &&
- mMeshes[meshIndex].mesh_lights)
- {
- *colorCount = mMeshes[meshIndex].num_vertices;
-
- #ifdef MESH_COLORS_SHOULD_BR_RGBA
- *colors = new float[*colorCount * 4];
- #else
- *colors = new float[*colorCount];
- #endif
-
- for (i = 0; i < *colorCount; ++i)
- {
- colorValue = mMeshes[meshIndex].mesh_lights[i];
-
- switch (Engine())
- {
- case TR_VERSION_4:
- case TR_VERSION_3:
- colorValue /= 16384.0;
- //! \fixme Should we really fall through here?!?
- break; // just testing -- xythobuz, 20140119
-
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_5:
- case TR_VERSION_UNKNOWN:
- colorValue = (1.0f - (colorValue / 8192.0f));
- break;
- }
-
- #ifdef MESH_COLORS_SHOULD_BR_RGBA
- (*colors)[i*4+0] = colorValue;
- (*colors)[i*4+1] = colorValue;
- (*colors)[i*4+2] = colorValue;
- (*colors)[i*4+3] = 1.0;
- #else
- (*colors)[i] = colorValue;
- #endif
- }
- }
- }
-
-
- int TombRaider::getRoomBox(unsigned int roomIndex, unsigned int index,
- float *xyzA, float *xyzB,
- float *xyzC, float *xyzD)
- {
-
- if (!getRoomBoxCount(roomIndex) || index > getRoomBoxCount(roomIndex))
- return -1;
-
- switch (getEngine())
- {
- case TR_VERSION_UNKNOWN:
- break;
-
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- case TR_VERSION_5:
- xyzA[0] = (unsigned short)_boxes[index].xmin * 1024.0f;
- xyzA[1] = (short)_boxes[index].true_floor;
- xyzA[2] = (unsigned short)_boxes[index].zmin * 1024.0f;
-
- xyzB[0] = (unsigned short)_boxes[index].xmax * 1024.0f;
- xyzB[1] = (short)_boxes[index].true_floor;
- xyzB[2] = (unsigned short)_boxes[index].zmin * 1024.0f;
-
- xyzC[0] = (unsigned short)_boxes[index].xmax * 1024.0f;
- xyzC[1] = (short)_boxes[index].true_floor;
- xyzC[2] = (unsigned short)_boxes[index].zmax * 1024.0f;
-
- xyzD[0] = (unsigned short)_boxes[index].xmin * 1024.0f;
- xyzD[1] = (short)_boxes[index].true_floor;
- xyzD[2] = (unsigned short)_boxes[index].zmax * 1024.0f;
- }
-
- return 0;
- }
-
-
- unsigned int TombRaider::getRoomBoxCount(unsigned int roomIndex)
- {
- if (!isRoomValid(roomIndex))
- return 0;
-
- switch (getEngine())
- {
- case TR_VERSION_UNKNOWN:
- break;
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- case TR_VERSION_5:
- return _num_boxes;
- }
-
- return 0;
- }
-
-
- void TombRaider::getRoomInfo(unsigned int index,
- unsigned int *flags, float pos[3],
- float bboxMin[3], float bboxMax[3])
- {
- unsigned int i, n;
- float f;
-
-
- if (!isRoomValid(index))
- return;
-
- switch (getEngine())
- {
- case TR_VERSION_UNKNOWN:
- break;
- case TR_VERSION_5:
- // Flags
- *flags = mRoomsTR5[index].roomFlag; // Needs to be generic flags in class
-
- // Positioning
- pos[0] = mRoomsTR5[index].roomX;
- pos[1] = 0.0f;
- pos[2] = mRoomsTR5[index].roomZ;
-
- // Bounding box setup
- bboxMin[0] = 0.0;
- bboxMin[1] = 0.0;
- bboxMin[2] = 0.0;
- bboxMax[0] = 0.0;
- bboxMax[1] = 0.0;
- bboxMax[2] = 0.0;
-
- // Bounding Box setup
- for (i = 0; i < mRoomsTR5[index].numLayers; ++i)
- {
- //! \fixme check the boxes are in min, max order in TRC
-
- if (i == 0)
- {
- bboxMin[0] = mRoomsTR5[index].layers[i].layerBoundingBoxX1;
- bboxMin[1] = mRoomsTR5[index].layers[i].layerBoundingBoxY1;
- bboxMin[2] = mRoomsTR5[index].layers[i].layerBoundingBoxZ1;
-
- bboxMax[0] = mRoomsTR5[index].layers[i].layerBoundingBoxX1;
- bboxMax[1] = mRoomsTR5[index].layers[i].layerBoundingBoxY2;
- bboxMax[2] = mRoomsTR5[index].layers[i].layerBoundingBoxZ2;
-
- continue;
- }
-
- if (mRoomsTR5[index].layers[i].layerBoundingBoxX1 < bboxMin[0])
- bboxMin[0] = mRoomsTR5[index].layers[i].layerBoundingBoxX1;
-
- if (mRoomsTR5[index].layers[i].layerBoundingBoxY1 < bboxMin[1])
- bboxMin[1] = mRoomsTR5[index].layers[i].layerBoundingBoxY1;
-
- if (mRoomsTR5[index].layers[i].layerBoundingBoxZ1 < bboxMin[2])
- bboxMin[2] = mRoomsTR5[index].layers[i].layerBoundingBoxZ1;
-
-
- if (mRoomsTR5[index].layers[i].layerBoundingBoxX2 < bboxMax[0])
- bboxMax[0] = mRoomsTR5[index].layers[i].layerBoundingBoxX2;
-
- if (mRoomsTR5[index].layers[i].layerBoundingBoxY2 < bboxMax[1])
- bboxMax[1] = mRoomsTR5[index].layers[i].layerBoundingBoxY2;
-
- if (mRoomsTR5[index].layers[i].layerBoundingBoxZ2 < bboxMax[2])
- bboxMax[2] = mRoomsTR5[index].layers[i].layerBoundingBoxZ2;
- }
- break;
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- // Flags
- *flags = _rooms[index].flags; // Needs to be generic flags in class
-
- // Positioning
- pos[0] = _rooms[index].info.x;
- pos[1] = 0.0f;
- pos[2] = _rooms[index].info.z;
-
- bboxMin[0] = 0.0;
- bboxMin[1] = 0.0;
- bboxMin[2] = 0.0;
- bboxMax[0] = 0.0;
- bboxMax[1] = 0.0;
- bboxMax[2] = 0.0;
-
- // Bounding Box setup
- n = ((_rooms[index].room_data.num_vertices < 0) ? 0 :
- _rooms[index].room_data.num_vertices);
-
- for (i = 0; i < n; ++i)
- {
- if (i == 0)
- {
- bboxMin[0] = _rooms[index].room_data.vertices[i].vertex.x;
- bboxMin[1] = _rooms[index].room_data.vertices[i].vertex.y;
- bboxMin[2] = _rooms[index].room_data.vertices[i].vertex.z;
-
- bboxMax[0] = _rooms[index].room_data.vertices[i].vertex.x;
- bboxMax[1] = _rooms[index].room_data.vertices[i].vertex.y;
- bboxMax[2] = _rooms[index].room_data.vertices[i].vertex.z;
-
- continue;
- }
-
- f = _rooms[index].room_data.vertices[i].vertex.x;
-
- if (f < bboxMin[0])
- bboxMin[0] = f;
- if (f > bboxMax[0])
- bboxMax[0] = f;
-
- f = _rooms[index].room_data.vertices[i].vertex.y;
-
- if (f < bboxMin[1])
- bboxMin[1] = f;
- if (f > bboxMax[1])
- bboxMax[1] = f;
-
- f = _rooms[index].room_data.vertices[i].vertex.z;
-
- if (f < bboxMin[2])
- bboxMin[2] = f;
- if (f > bboxMax[2])
- bboxMax[2] = f;
- }
-
- break;
- }
- }
-
-
- int TombRaider::getRoomLight(unsigned int roomIndex, unsigned int index,
- float pos[4], float color[4], float dir[3],
- float *attenuation, float *cutoffAngle,
- unsigned int *type, unsigned int *flags)
- {
- const float dd = 10000.0; // 4095.0;
- float f;
-
-
- *flags = 0; // reset
-
- switch (getEngine())
- {
- case TR_VERSION_UNKNOWN:
- return -1;
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_3:
- if (_rooms[roomIndex].num_lights <= 0 ||
- (int)index > _rooms[roomIndex].num_lights)
- {
- return -1;
- }
-
- if (_rooms[roomIndex].lights[index].fade1 == 100730731)
- {
- f = 1.0;
- }
- else
- {
- f = _rooms[roomIndex].lights[index].fade1;
- f /= dd;
- }
-
- *attenuation = f;
- *flags |= tombraiderLight_useAttenuation;
-
- pos[0] = _rooms[roomIndex].lights[index].x;
- pos[1] = _rooms[roomIndex].lights[index].y;
- pos[2] = _rooms[roomIndex].lights[index].z;
- pos[3] = 1.0f;
-
- color[0] = _rooms[roomIndex].lights[index].intensity1 / 409.60f;
- color[1] = color[0];
- color[2] = color[0];
- color[3] = 1.0f;
-
- if (Engine() == TR_VERSION_3)
- {
- color[0] = _rooms[roomIndex].room_light_colour.r;
- color[1] = _rooms[roomIndex].room_light_colour.g;
- color[2] = _rooms[roomIndex].room_light_colour.b;
- }
-
- *type = tombraiderLight_typeDirectional;
- break;
- case TR_VERSION_4:
- if (_rooms[roomIndex].num_lights <= 0 ||
- (int)index > _rooms[roomIndex].num_lights)
- {
- return -1;
- }
-
- pos[0] = _rooms[roomIndex].tr4Lights[index].xPosition;
- pos[1] = _rooms[roomIndex].tr4Lights[index].yPosition;
- pos[2] = _rooms[roomIndex].tr4Lights[index].zPosition;
- pos[3] = 0.0f;
-
- color[0] = _rooms[roomIndex].tr4Lights[index].color.r;
- color[1] = _rooms[roomIndex].tr4Lights[index].color.g;
- color[2] = _rooms[roomIndex].tr4Lights[index].color.b;
-
- dir[0] = _rooms[roomIndex].tr4Lights[index].xDir;
- dir[1] = _rooms[roomIndex].tr4Lights[index].yDir;
- dir[2] = _rooms[roomIndex].tr4Lights[index].zDir;
-
- *cutoffAngle = _rooms[roomIndex].tr4Lights[index].cutoff;
- *flags |= tombraiderLight_useCutoff;
-
- switch (_rooms[roomIndex].tr4Lights[index].lightType)
- {
- case 1:
- *type = tombraiderLight_typeDirectional;
- break;
- case 2:
- *type = tombraiderLight_typeSpot;
- break;
- default:
- *type = tombraiderLight_typePoint;
- }
- break;
- case TR_VERSION_5:
- if (mRoomsTR5[roomIndex].numRoomLights <= 0 ||
- (int)index > mRoomsTR5[roomIndex].numRoomLights)
- {
- return -1;
- }
-
- pos[0] = mRoomsTR5[roomIndex].lights[index].x;
- pos[1] = mRoomsTR5[roomIndex].lights[index].y;
- pos[2] = mRoomsTR5[roomIndex].lights[index].z;
- pos[3] = 0.0f;
-
- color[0] = mRoomsTR5[roomIndex].lights[index].red;
- color[1] = mRoomsTR5[roomIndex].lights[index].green;
- color[2] = mRoomsTR5[roomIndex].lights[index].blue;
-
- dir[0] = mRoomsTR5[roomIndex].lights[index].directionVectorX;
- dir[1] = mRoomsTR5[roomIndex].lights[index].directionVectorY;
- dir[2] = mRoomsTR5[roomIndex].lights[index].directionVectorZ;
-
- switch (mRoomsTR5[roomIndex].lights[index].lightType)
- {
- case 2:
- *type = tombraiderLight_typeSpot;
- break;
- case 3:
- *type = tombraiderLight_typeDirectional;
- break;
- default:
- *type = tombraiderLight_typePoint;
- }
- break;
- }
-
- return 0;
- }
-
-
- unsigned int TombRaider::getRoomLightCount(unsigned int roomIndex)
- {
- if (!isRoomValid(roomIndex))
- return 0;
-
- switch (getEngine())
- {
- case TR_VERSION_UNKNOWN:
- break;
- case TR_VERSION_5:
- return mRoomsTR5[roomIndex].numRoomLights;
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- return _rooms[roomIndex].num_lights;
- }
-
- return 0;
- }
-
-
- int TombRaider::getRoomModel(unsigned int roomIndex, unsigned int index,
- int *modelIndex, float pos[3], float *yaw)
- {
- unsigned int i, count;
-
-
- if (!getRoomModelCount(roomIndex) || index > getRoomModelCount(roomIndex))
- return -1;
-
- switch (getEngine())
- {
- case TR_VERSION_UNKNOWN:
- return -1;
- case TR_VERSION_5:
- count = NumStaticMeshes();
-
- for (i = 0; i < count; ++i)
- {
- if (mRoomsTR5[roomIndex].meshes[index].object_id ==
- _static_meshes[i].object_id)
- {
- *modelIndex = _static_meshes[i].starting_mesh;
-
- pos[0] = mRoomsTR5[roomIndex].meshes[index].x;
- pos[1] = mRoomsTR5[roomIndex].meshes[index].y;
- pos[2] = mRoomsTR5[roomIndex].meshes[index].z;
-
- *yaw = ((mRoomsTR5[roomIndex].meshes[index].rotation >> 14)
- & 0x03) * 90;
- }
- }
- break;
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- count = NumStaticMeshes();
-
- for (i = 0; i < count; ++i)
- {
- if (_rooms[roomIndex].static_meshes[index].object_id ==
- _static_meshes[i].object_id)
- {
- *modelIndex = _static_meshes[i].starting_mesh;
-
- pos[0] = _rooms[roomIndex].static_meshes[index].x;
- pos[1] = _rooms[roomIndex].static_meshes[index].y;
- pos[2] = _rooms[roomIndex].static_meshes[index].z;
-
- *yaw = ((_rooms[roomIndex].static_meshes[index].rotation >> 14)
- & 0x03) * 90;
- }
- }
- }
-
- return 0;
- }
-
-
- unsigned int TombRaider::getRoomModelCount(unsigned int roomIndex)
- {
- if (!isRoomValid(roomIndex))
- return 0;
-
- switch (getEngine())
- {
- case TR_VERSION_UNKNOWN:
- break;
- case TR_VERSION_5:
- return mRoomsTR5[roomIndex].numStaticMeshes;
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- return _rooms[roomIndex].num_static_meshes;
- }
-
- return 0;
- }
-
-
- int TombRaider::getRoomPortal(unsigned int roomIndex, unsigned int index,
- int *adjoiningRoom,
- float normal[3], float vertices[12])
- {
- if (!getRoomPortalCount(roomIndex) || index > getRoomPortalCount(roomIndex))
- return 0;
-
- switch (getEngine())
- {
- case TR_VERSION_UNKNOWN:
- break;
- case TR_VERSION_5:
- *adjoiningRoom = mRoomsTR5[roomIndex].doors[index].adjoining_room;
-
- normal[0] = mRoomsTR5[roomIndex].doors[index].normal.x;// / NORMAL_SCALE;
- normal[1] = mRoomsTR5[roomIndex].doors[index].normal.y;// / NORMAL_SCALE;
- normal[2] = mRoomsTR5[roomIndex].doors[index].normal.z;// / NORMAL_SCALE;
-
- vertices[0] = mRoomsTR5[roomIndex].doors[index].vertices[0].x;
- vertices[1] = mRoomsTR5[roomIndex].doors[index].vertices[0].y;
- vertices[2] = mRoomsTR5[roomIndex].doors[index].vertices[0].z;
- vertices[3] = mRoomsTR5[roomIndex].doors[index].vertices[1].x;
- vertices[4] = mRoomsTR5[roomIndex].doors[index].vertices[1].y;
- vertices[5] = mRoomsTR5[roomIndex].doors[index].vertices[1].z;
- vertices[6] = mRoomsTR5[roomIndex].doors[index].vertices[2].x;
- vertices[7] = mRoomsTR5[roomIndex].doors[index].vertices[2].y;
- vertices[8] = mRoomsTR5[roomIndex].doors[index].vertices[2].z;
- vertices[9] = mRoomsTR5[roomIndex].doors[index].vertices[3].x;
- vertices[10] = mRoomsTR5[roomIndex].doors[index].vertices[3].y;
- vertices[11] = mRoomsTR5[roomIndex].doors[index].vertices[3].z;
- break;
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- *adjoiningRoom = _rooms[roomIndex].portals[index].adjoining_room;
-
- normal[0] = _rooms[roomIndex].portals[index].normal.x;// / NORMAL_SCALE;
- normal[1] = _rooms[roomIndex].portals[index].normal.y;// / NORMAL_SCALE;
- normal[2] = _rooms[roomIndex].portals[index].normal.z;// / NORMAL_SCALE;
-
- vertices[0] = _rooms[roomIndex].portals[index].vertices[0].x;
- vertices[1] = _rooms[roomIndex].portals[index].vertices[0].y;
- vertices[2] = _rooms[roomIndex].portals[index].vertices[0].z;
- vertices[3] = _rooms[roomIndex].portals[index].vertices[1].x;
- vertices[4] = _rooms[roomIndex].portals[index].vertices[1].y;
- vertices[5] = _rooms[roomIndex].portals[index].vertices[1].z;
- vertices[6] = _rooms[roomIndex].portals[index].vertices[2].x;
- vertices[7] = _rooms[roomIndex].portals[index].vertices[2].y;
- vertices[8] = _rooms[roomIndex].portals[index].vertices[2].z;
- vertices[9] = _rooms[roomIndex].portals[index].vertices[3].x;
- vertices[10] = _rooms[roomIndex].portals[index].vertices[3].y;
- vertices[11] = _rooms[roomIndex].portals[index].vertices[3].z;
- }
-
- return 0;
- }
-
-
- unsigned int TombRaider::getRoomPortalCount(unsigned int roomIndex)
- {
- if (!isRoomValid(roomIndex))
- return 0;
-
- switch (getEngine())
- {
- case TR_VERSION_UNKNOWN:
- break;
- case TR_VERSION_5:
- return mRoomsTR5[roomIndex].numDoors;
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- return _rooms[roomIndex].num_portals;
- }
-
- return 0;
- }
-
-
- //! \fixme No TRC support
- void TombRaider::getRoomRectangle(unsigned int roomIndex,
- unsigned int rectangleIndex,
- unsigned int *indices, float *texCoords,
- int *texture, unsigned int *flags)
- {
- int tIndex;
- unsigned int count, i, j, k;
-
-
- switch (getEngine())
- {
- case TR_VERSION_UNKNOWN:
- break;
- case TR_VERSION_5:
- for (i = 0, count = 0; i < mRoomsTR5[roomIndex].numLayers; ++i)
- {
- if (count + mRoomsTR5[roomIndex].layers[i].numLayerRectangles >
- rectangleIndex)
- {
- k = rectangleIndex - count;
-
- *texture = *flags = 0; // FIXME
-
- // Setup per vertex
- for (j = 0; j < 3; ++j)
- {
- // Get vertex index { (0, a), (1, b), (2, c), (3, d) }
- indices[j] = mRoomsTR5[roomIndex].faces[i].quads[k].vertices[j];
-
- // FIXME
- //tIndex = mRoomsTR5[roomIndex].faces[i].vertices[j].texture & 0xff;
- //computeUV(objectTexturesTR5[tIndex].vertices + i,
- // texCoords+i*2, texCoords+i*2+1);
- }
- break;
- }
-
- count += mRoomsTR5[roomIndex].layers[i].numLayerRectangles;
- }
- break;
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- tIndex = _rooms[roomIndex].room_data.rectangles[rectangleIndex].texture;
- *texture = _object_textures[tIndex].tile;
- *flags = 0;
-
- switch (_object_textures[tIndex].transparency_flags)
- {
- case 0:
- break;
- case 2:
- *flags |= tombraiderFace_PartialAlpha;
- break;
- default:
- *flags |= tombraiderFace_Alpha;
- break;
- }
-
- // Setup per vertex
- for (i = 0; i < 4; ++i)
- {
- // Get vertex index {(0, a), (1, b), (2, c), (3, d)}
- indices[i] = _rooms[roomIndex].room_data.rectangles[rectangleIndex].vertices[i];
-
- computeUV(_object_textures[tIndex].vertices + i,
- texCoords+i*2, texCoords+i*2+1);
- }
- }
- }
-
-
- unsigned int TombRaider::getRoomRectangleCount(unsigned int roomIndex)
- {
- unsigned int i, count;
-
-
- if (!isRoomValid(roomIndex))
- return 0;
-
- switch (getEngine())
- {
- case TR_VERSION_UNKNOWN:
- break;
- case TR_VERSION_5:
- for (i = 0, count = 0; i < mRoomsTR5[roomIndex].numLayers; ++i)
- {
- count += mRoomsTR5[roomIndex].layers[i].numLayerRectangles;
- }
-
- return count;
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- return ((_rooms[roomIndex].room_data.num_rectangles < 0) ? 0 :
- _rooms[roomIndex].room_data.num_rectangles);
- }
-
- return 0;
- }
-
- int TombRaider::getRoomSector(unsigned int roomIndex, unsigned int index,
- unsigned int *flags,
- float *ceiling, float *floor,
- int *floorDataIndex, int *boxIndex,
- int *roomBelow, int *roomAbove)
- {
- unsigned int count;
- unsigned int zSectorsCount;
- unsigned int xSectorsCount;
-
- count = getRoomSectorCount(roomIndex, &zSectorsCount, &xSectorsCount);
-
- if (!count || index > count)
- return -1;
-
- *flags = 0;
-
- switch (getEngine())
- {
- case TR_VERSION_UNKNOWN:
- break;
- case TR_VERSION_5:
- *floorDataIndex = mRoomsTR5[roomIndex].sectors[index].fd_index;
- *boxIndex = mRoomsTR5[roomIndex].sectors[index].box_index;
- *roomBelow = mRoomsTR5[roomIndex].sectors[index].room_below;
- *roomAbove = mRoomsTR5[roomIndex].sectors[index].room_above;
-
- if ((unsigned char)mRoomsTR5[roomIndex].sectors[index].floor == 0x81 ||
- (unsigned char)mRoomsTR5[roomIndex].sectors[index].ceiling == 0x81)
- {
- *flags |= tombraiderSector_wall;
- }
-
- *floor = mRoomsTR5[roomIndex].sectors[index].floor * 256.0f;
- *ceiling = mRoomsTR5[roomIndex].sectors[index].ceiling * 256.0f;
- break;
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- *floorDataIndex = _rooms[roomIndex].sector_list[index].fd_index;
- *boxIndex = _rooms[roomIndex].sector_list[index].box_index;
- *roomBelow = _rooms[roomIndex].sector_list[index].room_below;
- *roomAbove = _rooms[roomIndex].sector_list[index].room_above;
-
- if ((unsigned char)_rooms[roomIndex].sector_list[index].floor == 0x81 ||
- (unsigned char)_rooms[roomIndex].sector_list[index].ceiling == 0x81)
- {
- *flags |= tombraiderSector_wall;
- }
-
- *floor = _rooms[roomIndex].sector_list[index].floor * 256.0f;
- *ceiling = _rooms[roomIndex].sector_list[index].ceiling * 256.0f;
- }
-
- if (*boxIndex == 65536)
- {
- *boxIndex = -1;
- }
-
- if (*roomBelow == 255)
- {
- *roomBelow = -1;
- }
-
- if (*roomAbove == 255)
- {
- *roomAbove = -1;
- }
-
- return 0;
- }
-
-
- unsigned int TombRaider::getRoomSectorCount(unsigned int roomIndex,
- unsigned int *zSectorsCount,
- unsigned int *xSectorsCount)
- {
- unsigned int count;
-
-
- if (!isRoomValid(roomIndex))
- return 0;
-
- switch (getEngine())
- {
- case TR_VERSION_UNKNOWN:
- break;
- case TR_VERSION_5:
- // width of sector list
- *zSectorsCount = mRoomsTR5[roomIndex].numZSectors;
-
- // height of sector list
- *xSectorsCount = mRoomsTR5[roomIndex].numXSectors;
-
- return (mRoomsTR5[roomIndex].numZSectors *
- mRoomsTR5[roomIndex].numXSectors);
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- // width of sector list
- *zSectorsCount = _rooms[roomIndex].num_zsectors;
-
- // height of sector list
- *xSectorsCount = _rooms[roomIndex].num_xsectors;
-
- count = _rooms[roomIndex].num_zsectors * _rooms[roomIndex].num_xsectors;
-
- return count;
- }
-
- return 0;
- }
-
-
- void TombRaider::getRoomSprite(unsigned int roomIndex, unsigned int index,
- float scale, int *texture,
- float *pos, float *vertices, float *texcoords)
- {
- tr2_sprite_texture_t *sprite;
- tr2_vertex_t *vertex;
- int t_index, width, height, x, y;
- float width2, height2;
- unsigned int spriteCount;
-
-
- spriteCount = getRoomSpriteCount(roomIndex);
-
- if (spriteCount == 0 || index > spriteCount)
- return;
-
- if (scale == 0.0)
- scale = 10.0;
-
- t_index = _rooms[roomIndex].room_data.sprites[index].texture;
-
- vertex =
- &_rooms[roomIndex].room_data.vertices[_rooms[roomIndex].room_data.sprites[index].vertex].vertex;
-
- sprite = &_sprite_textures[t_index];
-
- // Info, offset in room added to position
- pos[0] = _rooms[roomIndex].info.x + vertex->x;
- pos[1] = vertex->y;
- pos[2] = _rooms[roomIndex].info.z + vertex->z;
- *texture = sprite->tile;
-
- width = sprite->width >> 8;
- height = sprite->height >> 8;
- x = sprite->x;
- y = sprite->y;
- width2 = width * scale; // scale it up a bit (sprites are rather tiny)
- height2 = height * scale;
-
- // Quad
- vertices[0] = -width2 / 2.0f;
- vertices[1] = 0;
- vertices[2] = 0;
- vertices[3] = -width2 / 2.0f;
- vertices[4] = -height2;
- vertices[5] = 0;
- vertices[6] = width2 / 2.0f;
- vertices[7] = -height2;
- vertices[8] = 0;
- vertices[9] = width2 / 2.0f;
- vertices[10] = 0;
- vertices[11] = 0;
-
- texcoords[0] = ((float)y + height) / mTexelScale;
- texcoords[1] = (float)(x) / mTexelScale;
- texcoords[2] = (float)(y) / mTexelScale;
- texcoords[3] = (float)(x) / mTexelScale;
- texcoords[4] = (float)(y) / mTexelScale;
- texcoords[5] = ((float)x + width) / mTexelScale;
- texcoords[6] = ((float)y + height) / mTexelScale;
- texcoords[7] = ((float)x + width) / mTexelScale;
- }
-
-
- #ifdef OPTIONAL_ARRAY_INTERFACE
- void TombRaider::getRoomSpriteArray(unsigned int index, float scale,
- unsigned int *spriteCount, int *textures,
- float *pos, float *vertices, float *texcoords)
- {
- int i, t_index;
- tr2_vertex_t *vertex;
- int width, height, x, y;
- float width2, height2;
-
-
- *spriteCount = ((room[index].room_data.num_sprites < 0) ?
- 0 : room[index].room_data.num_sprites);
-
- if (*spriteCount == 0)
- return;
-
- textures = new int[*spriteCount];
- pos = new float[*spriteCount * 3];
- vertices = new float[*spriteCount * 12];
- texcoords = new float[*spriteCount * 8];
-
- if (scale == 0.0)
- scale = 10.0;
-
- for (i = 0; i < *spriteCount; ++i)
- {
- t_index = room[index].room_data.sprites[i].texture;
-
- vertex =
- &room[index].room_data.vertices[room[index].room_data.sprites[i].vertex].vertex;
-
- sprite = &sprite_textures[t_index];
-
- // Info, offset in room added to position
- pos[0+i*3] = room[index].info.x + vertex->x;
- pos[1+i*3] = vertex->y;
- pos[2+i*3] = room[index].info.z + vertex->z;
- textures[i] = sprite->tile;
-
- width = sprite->width >> 8;
- height = sprite->height >> 8;
- x = sprite->x;
- y = sprite->y;
- width2 = width * scale; // scale it up a bit (sprites are rather tiny)
- height2 = height * scale;
-
- // Quad
- vertices[0+i*12] = -width2 / 2.0;
- vertices[1+i*12] = 0;
- vertices[2+i*12] = 0;
- vertices[3+i*12] = -width2 / 2.0;
- vertices[4+i*12] = -height2;
- vertices[5+i*12] = 0;
- vertices[6+i*12] = width2 / 2.0;
- vertices[7+i*12] = -height2;
- vertices[8+i*12] = 0;
- vertices[9+i*12] = width2 / 2.0;
- vertices[10+i*12] = 0;
- vertices[11+i*12] = 0;
-
- texcoords[0+i*8] = ((float)y + height) / mTexelScale;
- texcoords[1+i*8] = (float)(x) / mTexelScale;
- texcoords[2+i*8] = (float)(y) / mTexelScale;
- texcoords[3+i*8] = (float)(x) / mTexelScale;
- texcoords[4+i*8] = (float)(y) / mTexelScale;
- texcoords[5+i*8] = ((float)x + width) / mTexelScale;
- texcoords[6+i*8] = ((float)y + height) / mTexelScale;
- texcoords[7+i*8] = ((float)x + width) / mTexelScale;
- }
- }
- #endif
-
-
- unsigned int TombRaider::getRoomSpriteCount(unsigned int roomIndex)
- {
- if (!isRoomValid(roomIndex))
- return 0;
-
- switch (getEngine())
- {
- case TR_VERSION_UNKNOWN:
- break;
- case TR_VERSION_5:
- return 0; // No room sprites in TRC
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- return ((_rooms[roomIndex].room_data.num_sprites < 0) ?
- 0 : _rooms[roomIndex].room_data.num_sprites);
- }
-
- return 0;
- }
-
-
- void TombRaider::getRoomTriangle(unsigned int roomIndex,
- unsigned int triangleIndex,
- unsigned int *indices, float *texCoords,
- int *texture, unsigned int *flags)
- {
- int tIndex;
- unsigned int count, i, j, k;
-
-
- switch (getEngine())
- {
- case TR_VERSION_UNKNOWN:
- break;
- case TR_VERSION_5:
- for (i = 0, count = 0; i < mRoomsTR5[roomIndex].numLayers; ++i)
- {
- if (count + mRoomsTR5[roomIndex].layers[i].numLayerTriangles >
- triangleIndex)
- {
- k = triangleIndex - count;
-
- *texture = *flags = 0; // FIXME
-
- // Setup per vertex
- for (j = 0; j < 3; ++j)
- {
- // Get vertex index {(0, a), (1, b), (2, c) }
- indices[j] = mRoomsTR5[roomIndex].faces[i].tris[k].vertices[j];
-
- // FIXME
- //tIndex = mRoomsTR5[roomIndex].faces[i].vertices[j].texture & 0xff;
- //computeUV(objectTexturesTR5[tIndex].vertices + i,
- // texCoords+i*2, texCoords+i*2+1);
- }
- break;
- }
-
- count += mRoomsTR5[roomIndex].layers[i].numLayerTriangles;
- }
- break;
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- tIndex = _rooms[roomIndex].room_data.triangles[triangleIndex].texture;
- *texture = _object_textures[tIndex].tile;
- *flags = 0;
-
- switch (_object_textures[tIndex].transparency_flags)
- {
- case 0:
- break;
- case 2:
- *flags |= tombraiderFace_PartialAlpha;
- break;
- default:
- *flags |= tombraiderFace_Alpha;
- break;
- }
-
- // Setup per vertex
- for (i = 0; i < 3; ++i)
- {
- // Get vertex index {(0, a), (1, b), (2, c) }
- indices[i] = _rooms[roomIndex].room_data.triangles[triangleIndex].vertices[i];
-
- computeUV(_object_textures[tIndex].vertices + i,
- texCoords+i*2, texCoords+i*2+1);
- }
- }
- }
-
-
- void TombRaider::getRoomTriangles(unsigned int index, int textureOffset,
- unsigned int *triangleCount,
- unsigned int **indices,
- float **texCoords, int **textures,
- unsigned int **flags)
- {
- unsigned int count, i, j;
- int texture;
-
-
- if (!isRoomValid(index))
- return;
-
- switch (getEngine())
- {
- case TR_VERSION_UNKNOWN:
- break;
- case TR_VERSION_5:
- for (i = 0, count = 0; i < mRoomsTR5[index].numLayers; ++i)
- {
- count += mRoomsTR5[index].layers[i].numLayerTriangles;
- }
-
- //! \fixme !!!
- break;
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- // Generate textured triangles
- count = ((_rooms[index].room_data.num_triangles < 0) ? 0 :
- _rooms[index].room_data.num_triangles);
-
- *triangleCount = count;
-
- *indices = new unsigned int[count * 3];
- *texCoords = new float[count * 6];
- *textures = new int[count];
- *flags = new unsigned int[count];
-
- for (i = 0; i < count; ++i)
- {
- texture = _rooms[index].room_data.triangles[i].texture;
-
- // Adjust texture id using m_texOffset to map into
- // correct textures
- (*textures)[i] = _object_textures[texture].tile + textureOffset;
- (*flags)[i] = 0;
-
- switch (_object_textures[texture].transparency_flags)
- {
- case 0:
- break;
- case 2:
- (*flags)[i] |= tombraiderFace_PartialAlpha;
- break;
- default:
- (*flags)[i] |= tombraiderFace_Alpha;
- break;
- }
-
- // Setup per vertex
- for (j = 0; j < 3; ++j)
- {
- // Get vertex index {(0, a), (1, b), (2, c)}
- (*indices)[i*3+j] = _rooms[index].room_data.triangles[i].vertices[j];
-
- computeUV(_object_textures[texture].vertices+j,
- (*texCoords)+i*3+j*2, (*texCoords)+i*3+j*2+1);
- }
- }
- }
- }
-
-
- unsigned int TombRaider::getRoomTriangleCount(unsigned int roomIndex)
- {
- unsigned int i, count;
-
-
- if (!isRoomValid(roomIndex))
- return 0;
-
- switch (getEngine())
- {
- case TR_VERSION_UNKNOWN:
- break;
- case TR_VERSION_5:
- for (i = 0, count = 0; i < mRoomsTR5[roomIndex].numLayers; ++i)
- {
- count += mRoomsTR5[roomIndex].layers[i].numLayerTriangles;
- }
-
- return count;
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- return ((_rooms[roomIndex].room_data.num_triangles < 0) ? 0 :
- _rooms[roomIndex].room_data.num_triangles);
- }
-
- return 0;
- }
-
-
- //! \fixme No TR5 support
- void TombRaider::getRoomVertex(unsigned int roomIndex,unsigned int vertexIndex,
- float *xyz, float *rgba)
- {
- float color_value;
- unsigned int index = roomIndex, i = vertexIndex;
- tr2_vertex_t *vertex = 0x0;
-
-
- switch (getEngine())
- {
- case TR_VERSION_5:
- //! \fixme !!!
- print("getRoomVertex", "Error: No TRC implementation is the answer");
- break;
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- case TR_VERSION_UNKNOWN:
- vertex = &_rooms[index].room_data.vertices[i].vertex;
-
- xyz[0] = vertex->x;
- xyz[1] = vertex->y;
- xyz[2] = vertex->z;
-
- switch (getEngine())
- {
- case TR_VERSION_1:
- color_value = _rooms[index].room_data.vertices[i].attributes;
- color_value = (1.1f - (color_value / 8192.0f));
-
- rgba[0] = color_value;
- rgba[1] = color_value;
- rgba[2] = color_value;
- break;
- case TR_VERSION_3:
- case TR_VERSION_4:
- color_value = _rooms[index].room_data.vertices[i].lighting1;
- color_value /= 16384.0;
-
- rgba[0] = color_value + ((float)_rooms[index].room_light_colour.r /
- mRoomVertexLightingFactor);
-
- rgba[1] = color_value + ((float)_rooms[index].room_light_colour.g /
- mRoomVertexLightingFactor);
-
- rgba[2] = color_value + ((float)_rooms[index].room_light_colour.b /
- mRoomVertexLightingFactor);
- break;
- case TR_VERSION_2:
- case TR_VERSION_5: // Not really...
- case TR_VERSION_UNKNOWN:
- color_value = _rooms[index].room_data.vertices[i].lighting1;
- color_value = (1.1f - (color_value / 8192.0f));
-
- rgba[0] = color_value;
- rgba[1] = color_value;
- rgba[2] = color_value;
- }
-
- // Underwater rooms have an ambient color with a shade of blue
- if (_rooms[index].flags & 0x0001)
- {
- rgba[0] *= 0.6;
- rgba[2] *= 1.2;
- }
-
- // Alpha color
- rgba[3] = 1.0;
- }
- }
-
-
- void TombRaider::getRoomVertexArrays(unsigned int roomIndex,
- unsigned int *vertexCount, float **vertices,
- unsigned int *normalCount, float **normals,
- unsigned int *colorCount, float **colors)
- {
- float color_value;
- float rgba[4];
- unsigned int i, j, k, count;
- unsigned char c;
- tr2_vertex_t *vertex = 0x0;
-
-
- count = getRoomVertexCount(roomIndex);
-
- *vertexCount = 0;
- *vertices = 0x0;
- *normalCount = 0;
- *normals = 0x0;
- *colorCount = 0;
- *colors = 0x0;
-
- if (count == 0)
- return;
-
- switch (getEngine())
- {
- case TR_VERSION_UNKNOWN:
- break;
- case TR_VERSION_5:
- *vertexCount = count;
- *vertices = new float[count*3];
- *normalCount = count;
- *normals = new float[count*3];
- *colorCount = count;
- *colors = new float[count*4];
-
- for (i = 0, k = 0; i < mRoomsTR5[roomIndex].numLayers; ++i)
- {
- for (j = 0; j < mRoomsTR5[roomIndex].layers[i].numLayerVertices; ++j)
- {
- (*vertices)[k*3] = mRoomsTR5[roomIndex].faces[i].verts[j].x;
- (*vertices)[k*3+1] = mRoomsTR5[roomIndex].faces[i].verts[j].y;
- (*vertices)[k*3+2] = mRoomsTR5[roomIndex].faces[i].verts[j].z;
-
- (*normals)[k*3] = mRoomsTR5[roomIndex].faces[i].verts[j].nx;
- (*normals)[k*3+1] = mRoomsTR5[roomIndex].faces[i].verts[j].ny;
- (*normals)[k*3+2] = mRoomsTR5[roomIndex].faces[i].verts[j].nz;
-
- //! \fixme Ah, yeah this may be wrong
- c = ((unsigned char *)&mRoomsTR5[roomIndex].faces[i].verts[j].vColor)[1];
- (*colors)[k*4] = (float)c / 255.0f;
- c = ((unsigned char *)&mRoomsTR5[roomIndex].faces[i].verts[j].vColor)[2];
- (*colors)[k*4+1] = (float)c / 255.0f;
- c = ((unsigned char *)&mRoomsTR5[roomIndex].faces[i].verts[j].vColor)[3];
- (*colors)[k*4+2] = (float)c / 255.0f;
- c = ((unsigned char *)&mRoomsTR5[roomIndex].faces[i].verts[j].vColor)[0];
- (*colors)[k*4+3] = (float)c / 255.0f;
- }
- }
- break;
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- *vertexCount = count;
- *vertices = new float[count*3];
- *normalCount = 0; //! \fixme Do some TR1-TR4 levels support normals here?
- *normals = 0x0;
- *colorCount = count;
- *colors = new float[count*4];
-
- // Setup vertex coloring and vertices
- for (i = 0; i < count; ++i)
- {
- vertex = &_rooms[roomIndex].room_data.vertices[i].vertex;
-
- (*vertices)[i*3] = vertex->x;
- (*vertices)[i*3+1] = vertex->y;
- (*vertices)[i*3+2] = vertex->z;
-
- switch (getEngine())
- {
- case TR_VERSION_1:
- color_value = _rooms[roomIndex].room_data.vertices[i].attributes;
- color_value = (1.1f - (color_value / 8192.0f));
- break;
- case TR_VERSION_4:
- case TR_VERSION_3:
- color_value = _rooms[roomIndex].room_data.vertices[i].lighting1;
- color_value /= 16384.0;
- break;
- case TR_VERSION_2:
- case TR_VERSION_5:
- case TR_VERSION_UNKNOWN:
- color_value = _rooms[roomIndex].room_data.vertices[i].lighting1;
- color_value = (1.1f - (color_value / 8192.0f));
- }
-
- switch (getEngine())
- {
- case TR_VERSION_4:
- case TR_VERSION_3:
- rgba[0] = (color_value +
- ((float)_rooms[roomIndex].room_light_colour.r /
- mRoomVertexLightingFactor));
-
- rgba[1] = (color_value +
- ((float)_rooms[roomIndex].room_light_colour.g /
- mRoomVertexLightingFactor));
-
- rgba[2] = (color_value +
- ((float)_rooms[roomIndex].room_light_colour.b /
- mRoomVertexLightingFactor));
- break;
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_5:
- case TR_VERSION_UNKNOWN:
- rgba[0] = color_value;
- rgba[1] = color_value;
- rgba[2] = color_value;
- }
-
- // Underwater rooms have an ambient color with a shade of blue
- if (_rooms[roomIndex].flags & 0x0001)
- {
- rgba[0] *= 0.6;
- rgba[2] *= 1.2;
- }
-
- // Alpha color
- rgba[3] = 1.0;
-
- (*colors)[i*4] = rgba[0];
- (*colors)[i*4+1] = rgba[1];
- (*colors)[i*4+2] = rgba[2];
- (*colors)[i*4+3] = rgba[3];
- }
- }
- }
-
-
- unsigned int TombRaider::getRoomVertexCount(unsigned int roomIndex)
- {
- unsigned int i, count;
-
-
- if (!isRoomValid(roomIndex))
- return 0;
-
- switch (getEngine())
- {
- case TR_VERSION_UNKNOWN:
- break;
- case TR_VERSION_5:
- for (i = 0, count = 0; i < mRoomsTR5[roomIndex].numLayers; ++i)
- {
- count += mRoomsTR5[roomIndex].layers[i].numLayerVertices;
- }
-
- return count;
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- return ((_rooms[roomIndex].room_data.num_vertices < 0) ? 0 :
- _rooms[roomIndex].room_data.num_vertices);
- }
-
- return 0;
- }
-
-
- int TombRaider::getSkyModelId()
- {
- int skyMesh = -1;
- //bool rot = false;
- tr2_moveable_t *moveable;
- unsigned int i, id;
-
-
- moveable = Moveable();
-
- switch (Engine())
- {
- case TR_VERSION_2:
- //rot = true;
- id = 254;
- break;
- case TR_VERSION_3:
- id = 355;
- break;
- case TR_VERSION_4:
- id = 459;
- break;
- case TR_VERSION_1:
- case TR_VERSION_5:
- case TR_VERSION_UNKNOWN:
- return -1;
- }
-
- if (id > 0)
- {
- for (i = 0; (int)i < NumMoveables(); ++i)
- {
- if (moveable[i].object_id == id)
- {
- //sky_mesh = moveable[i].starting_mesh;
- skyMesh = i;
- break;
- }
- }
- }
-
- return skyMesh;
- }
-
-
- void TombRaider::getSprites()
- {
- #ifdef FIXME
- int i, j, k, l, x, y, s_index, width, height;
- float scale, width2, height2;
- tr2_sprite_texture_t *sprite;
- tr2_sprite_texture_t *sprite_textures;
- tr2_sprite_sequence_t *sprite_sequence;
- sprite_seq_t *r_mesh;
- tr2_item_t *item;
-
- r_mesh = Mesh();
- item = Item();
- sprite_textures = Sprite();
- sprite_sequence = SpriteSequence();
- scale = 4.0;
-
- printf("Processing sprites: ");
-
- for (i = 0; i < NumItems() - 1; ++i)
- {
- print(false, "Processing sprites in Items: %i/%i",
- i, NumItems());
-
- // It's a mesh, skip it
- if (Engine() == TR_VERSION_1 && item[i].intensity1 == -1)
- continue;
-
- k = item[i].object_id;
-
- // Search the SpriteSequence list
- // (if we didn't already decide that it's a mesh)
- for (j = 0; j < (int)NumSpriteSequences(); ++j)
- {
- if (sprite_sequence[j].object_id == k)
- {
- k = item[i].object_id;
-
- s_index = sprite_sequence[j].offset;
-
- spriteSequence = new sprite_seq_t;
- spriteSequence->spriteCount = -sprite_sequence[j].negative_length;
- spriteSequence->sprites = new sprite_t[r_mesh->num_sprites];
- sprite = spriteSequence->sprites;
-
- callbackAddSpriteSequence(spriteSequence);
-
- for (l = 0; l < r_mesh->num_sprites; ++l)
- {
- sprite = &sprite_textures[s_index];
-
- width = sprite->width >> 8;
- height = sprite->height >> 8;
- x = sprite->x;
- y = sprite->y;
- width2 = width * scale;
- height2 = height * scale;
-
- // For external use
- sprite[l].pos[0] = item[i].x;
- sprite[l].pos[1] = item[i].y;
- sprite[l].pos[2] = item[i].z;
- sprite[l].textureIndex = sprite->tile;
-
- sprite[l].radius = width2 / 2.0;
-
- sprite[l].vertex[0].pos[0] = -width2 / 2.0;
- sprite[l].vertex[1].pos[0] = -width2 / 2.0;
- sprite[l].vertex[2].pos[0] = width2 / 2.0;
- sprite[l].vertex[3].pos[0] = width2 / 2.0;
-
- sprite[l].vertex[0].pos[1] = 0;
- sprite[l].vertex[1].pos[1] = -height2;
- sprite[l].vertex[2].pos[1] = -height2;
- sprite[l].vertex[3].pos[1] = 0;
-
- sprite[l].vertex[0].pos[2] = 0;
- sprite[l].vertex[1].pos[2] = 0;
- sprite[l].vertex[2].pos[2] = 0;
- sprite[l].vertex[3].pos[2] = 0;
-
- sprite[l].texel[3].st[0] = (double)(x+width) / mTexelScale;
- sprite[l].texel[3].st[1] = (double)(y+height) / mTexelScale;
-
- sprite[l].texel[2].st[0] = (double)(x+width) / mTexelScale;
- sprite[l].texel[2].st[1] = (double)(y) / mTexelScale;
-
- sprite[l].texel[1].st[0] = (double)(x) / mTexelScale;
- sprite[l].texel[1].st[1] = (double)(y) / mTexelScale;
-
- sprite[l].texel[0].st[0] = (double)(x) / mTexelScale;
- sprite[l].texel[0].st[1] = (double)(y+height) / mTexelScale;
-
- printf(".");
- fflush(stdout);
- }
- }
- }
- }
-
- printf("\n");
- #endif
- }
-
-
- void TombRaider::getSoundSample(unsigned int index,
- unsigned int *bytes, unsigned char **data)
- {
- unsigned char *riff;
- unsigned int offset, altIndex;
-
- *bytes = 0;
- *data = 0x0;
-
- switch (Engine())
- {
- case TR_VERSION_1:
- //! \fixme This implies higher tmp memory cost ( copy safety )
- getRiffData(bytes, &riff);
-
- if (riff && (int)index < mNumSampleIndices)
- {
- offset = mSampleIndices[index];
-
- if ((int)index < mNumSampleIndices - 1)
- {
- *bytes = mSampleIndices[index+1] - mSampleIndices[index];
- }
- else
- {
- *bytes = *bytes - mSampleIndices[index];
- }
-
- *data = new unsigned char[*bytes];
-
- memcpy(*data, riff+offset, *bytes);
- }
-
- if (riff)
- {
- delete [] riff;
- }
- break;
- case TR_VERSION_2:
- case TR_VERSION_3:
- if (mRiffAlternateLoaded &&
- mRiffData && mRiffAlternateOffsets && (int)index < mNumSampleIndices)
- {
- altIndex = mSampleIndices[index];
-
- offset = mRiffAlternateOffsets[altIndex];
-
- if ((int)offset > mRiffDataSz)
- {
- print("getSoundSample", "WARNING: offset too large, may be mismatched SFX and game pak, handling...\n");
- return;
- }
-
- if (altIndex < mNumTR4Samples - 1)
- {
- *bytes = mRiffAlternateOffsets[altIndex+1] - offset;
- }
- else
- {
- *bytes = mRiffDataSz - offset;
- }
-
- *data = new unsigned char[*bytes];
-
- memcpy(*data, mRiffData+offset, *bytes);
- }
- break;
- case TR_VERSION_4:
- case TR_VERSION_5:
- getRiffDataTR4(index, bytes, data);
- break;
- case TR_VERSION_UNKNOWN:
- break;
- }
- }
-
-
- unsigned int TombRaider::getSoundSamplesCount()
- {
- unsigned int count = 0;
-
-
- switch (Engine())
- {
- case TR_VERSION_1:
- count = mNumSampleIndices;
- break;
- case TR_VERSION_2:
- case TR_VERSION_3:
- if (mRiffAlternateLoaded)
- {
- count = mNumSampleIndices;
- }
- break;
- case TR_VERSION_4:
- case TR_VERSION_5:
- count = mNumTR4Samples;
- break;
- case TR_VERSION_UNKNOWN:
- count = 0;
- break;
- }
-
- return count;
- }
-
-
- bool TombRaider::isMeshValid(int index)
- {
- return !(index < 0 ||
- index > mMeshCount ||
- ((mMeshes[index].num_vertices < 0 ||
- mMeshes[index].vertices == NULL)));
- }
-
-
- bool TombRaider::isRoomValid(int index)
- {
- // Yes, you MUST support signed indices due to legacy engines
- if (index < 0)
- return false;
-
- switch (getEngine())
- {
- case TR_VERSION_UNKNOWN:
- break;
- case TR_VERSION_5:
- if (index < _num_rooms &&
- //mRoomsTR5[index].roomX != 0xcdcdcd &&
- //mRoomsTR5[index].roomZ != 0xcdcdcd)
- *((int *)&mRoomsTR5[index].roomX) != 0xcdcdcd &&
- *((int *)&mRoomsTR5[index].roomZ) != 0xcdcdcd)
- // Cast to int * as it was comparing with float 0xcdcdcd before
- // -- xythobuz
- {
- return true;
- }
- break;
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_3:
- case TR_VERSION_4:
- if (index < _num_rooms &&
- _rooms[index].room_data.num_vertices > 0)
- {
- return true;
- }
- }
-
- return false;
- }
-
-
- ////////////////////////////////////////////////////////////
- // Public Mutators
- ////////////////////////////////////////////////////////////
-
- int TombRaider::loadSFX(char *filename)
- {
- FILE *f = fopen(filename, "rb");
- long bytes = 0;
- unsigned char *data;
-
- if (!f)
- {
- perror("Couldn't load SFX file");
- return -1;
- }
-
- fseek(f, 0L, SEEK_END);
- bytes = ftell(f);
- fseek(f, 0L, SEEK_SET);
-
- if (bytes > 8)
- {
- data = new unsigned char[bytes];
- fread(data, bytes, 1, f);
-
- mNumTR4Samples = getRiffOffsets(data, bytes,
- &mRiffAlternateOffsets,
- mNumSampleIndices);
-
- // This SFX must not come close to matching this game pak
- if ((int)mNumTR4Samples < mNumSampleIndices)
- {
- delete [] data;
- fclose(f);
-
- print("loadSFX", "WARNING: SFX RIFF has less than pak's RIFF count\n");
- return -2;
- }
-
- // If you ran out of room, then reallocate and parse agian =(
- if (mNumSampleIndices < (int)mNumTR4Samples)
- {
- delete [] mRiffAlternateOffsets;
-
- mNumTR4Samples = getRiffOffsets(data, bytes,
- &mRiffAlternateOffsets,
- mNumTR4Samples);
- }
-
- mRiffDataSz = bytes;
- mRiffData = data;
- mRiffAlternateLoaded = true;
- }
-
- fclose(f);
-
- return 0;
- }
-
-
- void TombRaider::reset()
- {
- unsigned int i;
-
-
- // Mongoose 2003.05.13, Don't let some jackass reset over and over
- if (mReset)
- {
- return;
- }
-
- //! \fixme Palettes aren't the same size anymore
- //memset(_palette8, 0, 256);
- //memset(_palette16, 0, 256);
-
- if (_anim_dispatches)
- delete [] _anim_dispatches;
-
- if (_anim_commands)
- delete [] _anim_commands;
-
- if (_mesh_trees)
- delete [] _mesh_trees;
-
- if (_frames)
- delete [] _frames;
-
- if (_moveables)
- delete [] _moveables;
-
- if (_static_meshes)
- delete [] _static_meshes;
-
- if (_object_textures)
- delete [] _object_textures;
-
- if (_sprite_textures)
- delete [] _sprite_textures;
-
- if (_sprite_sequences)
- delete [] _sprite_sequences;
-
- if (_cameras)
- delete [] _cameras;
-
- if (_sound_sources)
- delete [] _sound_sources;
-
- if (_boxes)
- delete [] _boxes;
-
- if (_overlaps)
- delete [] _overlaps;
-
- if (_zones)
- delete [] _zones;
-
- if (_animated_textures)
- delete [] _animated_textures;
-
- if (_items)
- delete [] _items;
-
- if (_light_map)
- delete [] _light_map;
-
- if (_cinematic_frames)
- delete [] _cinematic_frames;
-
- if (_demo_data)
- delete [] _demo_data;
-
- if (mRiffAlternateOffsets)
- delete [] mRiffAlternateOffsets;
-
- if (mSoundMap)
- delete [] mSoundMap;
-
- if (mSoundDetails)
- delete [] mSoundDetails;
-
- if (mSampleIndices)
- delete [] mSampleIndices;
-
- if (mRiffData)
- delete [] mRiffData;
-
- if (mTR4Samples)
- {
- for (i = 0; i < mNumTR4Samples; ++i)
- {
- if (mTR4SamplesSz[i])
- delete [] mTR4Samples[i];
- }
-
- delete [] mTR4Samples;
- }
-
- if (mTR4SamplesSz)
- delete [] mTR4SamplesSz;
-
- if (_rooms)
- {
- for (i = 0; i < _num_rooms; ++i)
- {
- if (_rooms[i].room_data.num_vertices > 0)
- delete [] _rooms[i].room_data.vertices;
-
- if (_rooms[i].room_data.num_rectangles > 0)
- delete [] _rooms[i].room_data.rectangles;
-
- if (_rooms[i].room_data.num_triangles > 0)
- delete [] _rooms[i].room_data.triangles;
-
- if (_rooms[i].room_data.num_sprites > 0)
- delete [] _rooms[i].room_data.sprites;
-
- if (_rooms[i].num_portals > 0)
- delete []_rooms[i].portals;
-
- if (_rooms[i].sector_list)
- delete [] _rooms[i].sector_list;
-
- if (_rooms[i].lights)
- delete [] _rooms[i].lights;
-
- if (_rooms[i].tr4Lights)
- delete [] _rooms[i].tr4Lights;
-
- if (_rooms[i].static_meshes)
- delete [] _rooms[i].static_meshes;
- }
-
- delete [] _rooms;
- }
-
- if (_floor_data)
- {
- delete [] _floor_data;
- }
-
- if (mMeshes)
- {
- for (i = 0; (int)i < mMeshCount; ++i)
- {
- if (mMeshes[i].vertices)
- delete [] mMeshes[i].vertices;
-
- if (mMeshes[i].mesh_lights)
- delete [] mMeshes[i].mesh_lights;
-
- if (mMeshes[i].normals)
- delete [] mMeshes[i].normals;
-
- if (mMeshes[i].textured_rectangles)
- delete [] mMeshes[i].textured_rectangles;
-
- if (mMeshes[i].textured_triangles)
- delete [] mMeshes[i].textured_triangles;
-
- if (mMeshes[i].coloured_rectangles)
- delete [] mMeshes[i].coloured_rectangles;
-
- if (mMeshes[i].coloured_triangles)
- delete [] mMeshes[i].coloured_triangles;
- }
-
- delete [] mMeshes;
- }
-
- if (_animations)
- delete [] _animations;
-
- if (_state_changes)
- delete _state_changes;
- //delete [] _state_changes;
-
- numMoveablesTR5 = 0;
- if (moveablesTR5)
- delete [] moveablesTR5;
-
- numAnimationsTR5 = 0;
- if (animationsTR5)
- delete [] animationsTR5;
-
- numObjectTexturesTR5 = 0;
- if (objectTexturesTR5)
- delete [] objectTexturesTR5;
-
- numCinematicFramesTR5 = 0;
- if (cinematicFramesTR5)
- delete [] cinematicFramesTR5;
-
- numFlyByCamerasTR5 = 0;
- if (flyByCamerasTR5)
- delete [] flyByCamerasTR5;
-
- // Texture use
- if (_tex_special)
- delete [] _tex_special;
-
- if (_textile8)
- delete [] _textile8;
-
- if (_textile16)
- delete [] _textile16;
-
- if (_textile32)
- delete [] _textile32;
-
- //! \fixme Damaged memory causes delete to not set pointer to NULL?
- _tex_special = NULL;
- _textile8 = NULL;
- _textile16 = NULL;
- _textile32 = NULL;
-
- // Compressed level use
- if (mCompressedLevelData)
- delete [] mCompressedLevelData;
-
- mCompressedLevelDataOffset = 0;
- mCompressedLevelSize = 0;
- mFreadMode = TR_FREAD_NORMAL;
-
- // Clear out vars
- mNumTR4Samples = 0;
- mPakVersion = 0;
- mEngineVersion = TR_VERSION_UNKNOWN;
- mNumSampleIndices = 0;
- mNumSoundDetails = 0;
- mRiffAlternateLoaded = false;
- _num_floor_data = 0;
- _num_textiles = 0;
- _num_tex_special = 0;
- _num_room_textures = 0;
- _num_misc_textures = 0;
- _num_bump_map_textures = 0;
- _unknown_t = 0;
- _num_rooms = 0;
- _num_anim_dispatches = 0;
- mMeshCount = 0;
- _num_state_changes = 0;
- _num_animations = 0;
- _num_anim_commands = 0;
- _num_mesh_trees = 0;
- _num_frames = 0;
- _num_moveables = 0;
- _num_demo_data = 0;
- _num_cinematic_frames = 0;
- _num_items = 0;
- _num_animated_textures = 0;
- _num_cameras = 0;
- _num_sound_sources = 0;
- _num_boxes = 0;
- _num_static_meshes = 0;
- _num_object_textures = 0;
- _num_sprite_textures = 0;
- _num_sprite_sequences = 0;
- _num_overlaps = 0;
-
- mReset = true;
- }
-
-
- void TombRaider::setDebug(bool toggle)
- {
- mDebug = toggle;
- }
-
-
- void TombRaider::setRoomVertexLightingFactor(float f)
- {
- mRoomVertexLightingFactor = f;
- }
-
- void TombRaider::setTexelScalingFactor(float f)
- {
- mTexelScale = f;
- }
-
- ////////////////////////////////////////////////////////////
- // Private Accessors
- ////////////////////////////////////////////////////////////
-
- void TombRaider::extractMeshes(unsigned char *mesh_data,
- unsigned int num_mesh_pointers,
- unsigned int *mesh_pointers)
- {
- unsigned int size, i;
- unsigned char *mesh_pointer;
- int negative_size;
-
-
- /* Alloc space for mesh */
- mMeshCount = num_mesh_pointers;
-
- printDebug("ExtractMeshes", "mMeshCount = %u", mMeshCount);
-
- mMeshes = new tr2_mesh_t[mMeshCount];
-
- printDebug("ExtractMeshes", "num_mesh_pointers = %u", num_mesh_pointers);
-
- for (i = 0; i < num_mesh_pointers; ++i)
- {
- /* Get mesh start */
- mesh_pointer = &mesh_data[mesh_pointers[i]];
-
- /* Get Centre + Unknowns */
- memcpy(&mMeshes[i].centre.x, mesh_pointer, 10);
- //! \fixme endian
-
- // depending on the interpretation of the unknowns that follow the Centre
- // element, more endian conversion may be necessary
- mesh_pointer += 10;
-
- /* Get number of vertices */
- memcpy(&mMeshes[i].num_vertices, mesh_pointer, 2);
- //! \fixme endian
-
- printDebug("ExtractMeshes", "mMeshes[%i].num_vertices = %u",
- i, mMeshes[i].num_vertices);
-
- mesh_pointer += sizeof(unsigned short);
- mMeshes[i].num_vertices = (short)abs(mMeshes[i].num_vertices);
-
- /* Get vertex list */
- size = sizeof(tr2_vertex_t) * mMeshes[i].num_vertices;
- mMeshes[i].vertices = 0x0;
-
- if (mMeshes[i].num_vertices > 0)
- mMeshes[i].vertices = new tr2_vertex_t[mMeshes[i].num_vertices];
-
- memcpy(mMeshes[i].vertices, mesh_pointer, size);
- //! \fixme endian
-
- mesh_pointer += size;
-
- /* Get number of normals */
- memcpy(&mMeshes[i].num_normals, mesh_pointer, sizeof(unsigned short));
- //! \fixme endian
-
- mesh_pointer += sizeof(unsigned short);
- negative_size = (mMeshes[i].num_normals < 0);
- mMeshes[i].num_normals = (short)abs(mMeshes[i].num_normals);
-
- printDebug("ExtractMeshes", "negative_size = %u", negative_size);
-
- mMeshes[i].mesh_lights = 0x0;
- mMeshes[i].normals = 0x0;
-
- /* Get normal list */
- if (negative_size)
- {
- negative_size = 0;
-
- size = mMeshes[i].num_normals * sizeof(unsigned short);
-
- mMeshes[i].mesh_lights = 0x0;
-
- if (mMeshes[i].num_normals > 0)
- mMeshes[i].mesh_lights = new short[mMeshes[i].num_normals];
-
- memcpy(mMeshes[i].mesh_lights, mesh_pointer, size);
- }
- else
- {
- size = sizeof(tr2_vertex_t) * mMeshes[i].num_normals;
-
- mMeshes[i].normals = 0x0;
-
- if (mMeshes[i].num_normals > 0)
- mMeshes[i].normals = new tr2_vertex_t[mMeshes[i].num_normals];
-
- memcpy(mMeshes[i].normals, mesh_pointer, size);
- }
- //! \fixme endian
-
- mesh_pointer += size;
-
- /* Get number of textured rectangles */
- memcpy(&mMeshes[i].num_textured_rectangles,
- mesh_pointer, sizeof(unsigned short));
- //! \fixme endian
-
- mesh_pointer += sizeof(unsigned short);
- mMeshes[i].num_textured_rectangles =
- (short)abs(mMeshes[i].num_textured_rectangles);
-
- size = sizeof(tr2_quad_t) * mMeshes[i].num_textured_rectangles;
-
- mMeshes[i].textured_rectangles = 0x0;
-
- if (mMeshes[i].num_textured_rectangles > 0)
- mMeshes[i].textured_rectangles =
- new tr2_quad_t[mMeshes[i].num_textured_rectangles];
-
- printDebug("ExtractMeshes", "mMeshes[%i].num_textured_rectangles = %u",
- i, mMeshes[i].num_textured_rectangles);
-
- /* Get list of textured rectangles */
- if (mMeshes[i].num_textured_rectangles > 0)
- {
- if (mEngineVersion == TR_VERSION_4)
- {
- int j;
-
- for (j = 0; j < mMeshes[i].num_textured_rectangles; ++j)
- {
- memcpy(&mMeshes[i].textured_rectangles[j],
- mesh_pointer, sizeof(tr2_quad_t));
- mesh_pointer += sizeof(tr2_quad_t) + sizeof(unsigned short);
- }
- }
- else
- {
- memcpy(mMeshes[i].textured_rectangles, mesh_pointer, size);
- }
- //! \fixme endian
-
- if (mEngineVersion != TR_VERSION_4)
- mesh_pointer += size;
- }
-
- /* Get number of textured triangles */
- memcpy(&mMeshes[i].num_textured_triangles,
- mesh_pointer, sizeof(unsigned short));
- //! \fixme endian
-
- mesh_pointer += sizeof(unsigned short);
- mMeshes[i].num_textured_triangles =
- (short)abs(mMeshes[i].num_textured_triangles);
-
- size = sizeof(tr2_tri_t) * mMeshes[i].num_textured_triangles;
-
- //if (mEngineVersion == TR_VERSION_4)
- // size += 2 * mMeshes[i].num_textured_triangles;
-
- mMeshes[i].textured_triangles = 0x0;
-
- if (mMeshes[i].num_textured_triangles > 0)
- {
- mMeshes[i].textured_triangles =
- new tr2_tri_t[mMeshes[i].num_textured_triangles];
- }
-
- printDebug("ExtractMeshes", " mMeshes[%i].num_textured_triangles = %u",
- i, mMeshes[i].num_textured_triangles);
-
- /* Get list of textured triangles */
- if (mMeshes[i].num_textured_triangles > 0)
- {
- if (mEngineVersion == TR_VERSION_4)
- {
- int j;
-
- for (j = 0; j < mMeshes[i].num_textured_triangles; ++j)
- {
- memcpy(&mMeshes[i].textured_triangles[j],
- mesh_pointer, sizeof(tr2_tri_t));
- mesh_pointer += sizeof(tr2_tri_t) + sizeof(unsigned short);
- }
- }
- else
- {
- memcpy(mMeshes[i].textured_triangles, mesh_pointer, size);
- }
- //! \fixme endian
-
- if (mEngineVersion != TR_VERSION_4)
- mesh_pointer += size;
- }
-
- if (mEngineVersion == TR_VERSION_4)
- {
- mMeshes[i].num_coloured_rectangles = 0;
- mMeshes[i].num_coloured_triangles = 0;
- mMeshes[i].coloured_rectangles = 0x0;
- mMeshes[i].coloured_triangles = 0x0;
-
- // Mongoose 2002.04.04, FIXME is this right?
- mesh_pointer += 2;
- continue;
- }
-
- /* Get number of coloured rectangles */
- memcpy(&mMeshes[i].num_coloured_rectangles, mesh_pointer,
- sizeof(unsigned short));
- //! \fixme endian
- mesh_pointer += sizeof(unsigned short);
- mMeshes[i].num_coloured_rectangles =
- (short)abs(mMeshes[i].num_coloured_rectangles);
-
- mMeshes[i].coloured_rectangles = 0x0;
-
- size = sizeof(tr2_quad_t) * mMeshes[i].num_coloured_rectangles;
-
- if (mMeshes[i].num_coloured_rectangles > 0)
- mMeshes[i].coloured_rectangles =
- new tr2_quad_t[mMeshes[i].num_coloured_rectangles];
-
- printDebug("ExtractMeshes", "mMeshes[%i].num_coloured_rectangles = %u",
- i, mMeshes[i].num_coloured_rectangles);
-
- /* Get list of coloured rectangles */
- if (mMeshes[i].num_coloured_rectangles > 0)
- {
- memcpy(mMeshes[i].coloured_rectangles, mesh_pointer, size);
- //! \fixme endian
- mesh_pointer += size;
- }
-
- /* Get number of coloured triangles */
- memcpy(&mMeshes[i].num_coloured_triangles, mesh_pointer,
- sizeof(unsigned short));
- //! \fixme endian
-
- mesh_pointer += sizeof(unsigned short);
- mMeshes[i].num_coloured_triangles =
- (short)abs(mMeshes[i].num_coloured_triangles);
-
- size = sizeof(tr2_tri_t) * mMeshes[i].num_coloured_triangles;
-
- mMeshes[i].coloured_triangles = 0x0;
-
- if (mMeshes[i].num_coloured_triangles > 0)
- mMeshes[i].coloured_triangles =
- new tr2_tri_t[mMeshes[i].num_coloured_triangles];
-
- printDebug("ExtractMeshes", "mMeshes[%i].num_coloured_triangles = %u",
- i, mMeshes[i].num_coloured_triangles);
-
- /* Get list of coloured triangles */
- if (mMeshes[i].num_coloured_triangles > 0)
- {
- memcpy(mMeshes[i].coloured_triangles, mesh_pointer, size);
- //! \fixme endian
-
- mesh_pointer += size;
- }
- }
- }
-
-
- int TombRaider::Fread(void *buffer, size_t size, size_t count, FILE *f)
- {
- int num_read;
-
- if (mFreadMode == TR_FREAD_COMPRESSED)
- {
- num_read = count;
- num_read *= size;
-
- if ((mCompressedLevelDataOffset + num_read) <= mCompressedLevelSize)
- {
- memcpy(buffer, &mCompressedLevelData[mCompressedLevelDataOffset],
- num_read);
-
- mCompressedLevelDataOffset += num_read;
-
- return count;
- }
- else
- {
- print("Fread", "(%p, %lu, %lu, %p) ERROR: Returned %d bytes too far",
- buffer, size, count, f, num_read);
- reset();
- exit(2);
- }
- }
-
-
- unsigned int offset = ftell(f);
-
- if (fread(buffer, size, count, f) != count)
- {
- print("Fread", "ERROR: Failed fread. Should Abort. @ 0x%x", offset);
- reset();
- exit(2);
- // return -1; // Unreachable anyways
- }
-
- return count;
- }
-
-
- void TombRaider::getRiffData(unsigned int *bytes, unsigned char **data)
- {
- *bytes = 0;
- *data = 0x0;
-
- if (mRiffDataSz)
- {
- *bytes = mRiffDataSz;
- *data = new unsigned char[mRiffDataSz];
-
- memcpy(*data, mRiffData, mRiffDataSz);
- }
- }
-
-
- void TombRaider::getRiffDataTR4(unsigned int index,
- unsigned int *bytes, unsigned char **data)
- {
- *bytes = 0;
- *data = 0x0;
-
- if (index < mNumTR4Samples)
- {
- *bytes = mTR4SamplesSz[index];
- *data = new unsigned char[*bytes];
-
- memcpy(*data, mTR4Samples[index], *bytes);
- }
- }
-
-
- int TombRaider::getRiffOffsets(unsigned char *riffData,
- unsigned int riffDataBytes,
- unsigned int **offsets,
- unsigned int numOffsets)
- {
- unsigned int i, j = 0, riffCount, state;
-
- *offsets = new unsigned int[numOffsets];
-
- for (i = 0, riffCount = 0, state = 4; i < riffDataBytes; ++i)
- {
- switch (riffData[i])
- {
- case 'R':
- if (state == 4)
- {
- j = i; // tmp offset guess
- state = 0;
- continue;
- }
- break;
- case 'I':
- if (state == 0)
- {
- state = 1;
- continue;
- }
- break;
- case 'F':
- if (state == 1)
- {
- state = 2;
- continue;
- }
- else if (state == 2)
- {
- state = 3;
-
- // Found RIFF header, but we can only report
- // riffCount offsets ( buffer size limits )
- if (riffCount < numOffsets)
- {
- (*offsets)[riffCount] = j;
- }
-
- ++riffCount;
- continue;
- }
- break;
- }
-
- state = 4;
- }
-
- return riffCount;
- }
-
-
- unsigned char *TombRaider::getTexTile(int texture)
- {
- unsigned char *image;
- unsigned int color;
- int j, k, index, offset;
- int xmin, xmax, ymin, ymax, x, y;
-
-
- image = NULL;
-
- if (texture >=0 && texture < (int)_num_textiles)
- {
- image = new unsigned char[256*256*4];
- memset(image, 0, 256*256*4);
-
- if (_textile32)
- {
- // Convert 32bit BGRA image format to 32bit RGBA
- for (j = 0; j < 256; j++)
- {
- for (k = 0; k < 256; k++)
- {
- index = (j * 256) + k;
- color = _textile32[texture].tile[index];
- index = (j * 1024) + (k * 4);
-
- image[index + 2] = *((unsigned char *)(&color));
- image[index + 1] = *((unsigned char *)(&color)+1);
- image[index + 0] = *((unsigned char *)(&color)+2);
- image[index + 3] = *((unsigned char *)(&color)+3);
- }
- }
- }
- else
- {
- // Convert 16bit ARGB image format to 32bit RGBA
- for (j = 0; j < 256; j++)
- {
- for (k = 0; k < 256; k++)
- {
- index = (j * 256) + k;
- offset = _textile16[texture].tile[index];
- index = (j * 1024) + (k * 4);
-
- image[index + 0] = ((offset >> 10) & 0x1f) * 8;
- image[index + 1] = ((offset >> 5) & 0x1f) * 8;
- image[index + 2] = (offset & 0x1f) * 8;
- image[index + 3] = (offset & 0x8000) ? 0xFF : 0;
- }
- }
- }
-
- switch (Engine())
- {
- case TR_VERSION_4:
- case TR_VERSION_3: // Account for alpha flags
- for (j = 0; j < (int)_num_object_textures; j++)
- {
- //! \fixme This kind of works for lighting - but messes up lara
- #ifdef FIXME
- if (_object_textures[j].tile == texture &&
- _object_textures[j].transparency_flags == 1)
- {
- xmin = 999;
- xmax = 0;
- ymin = 999;
- ymax = 0;
- y = 4;
-
- // Account for triangles
- if (_object_textures[j].vertices[3].xpixel == 0 &&
- _object_textures[j].vertices[3].ypixel == 0)
- y = 3;
-
- for (k = 0; k < y; k++)
- {
- if (_object_textures[j].vertices[k].xpixel > xmax)
- xmax = _object_textures[j].vertices[k].xpixel;
-
- if (_object_textures[j].vertices[k].xpixel < xmin)
- xmin = _object_textures[j].vertices[k].xpixel;
-
- if (_object_textures[j].vertices[k].ypixel > ymax)
- ymax = _object_textures[j].vertices[k].ypixel;
-
- if (_object_textures[j].vertices[k].ypixel < ymin)
- ymin = _object_textures[j].vertices[k].ypixel;
- }
-
- for (x = xmin; x <= xmax; x++)
- {
- for (y = ymin; y <= ymax; y++)
- {
- index = (y * 256) + x;
- offset = _textile16[texture].tile[index];
- index = (y * 1024) + (x * 4);
-
- image[index + 0] = ((offset >> 10) & 0x1f) * 8;
- image[index + 1] = ((offset >> 5) & 0x1f) * 8;
- image[index + 2] = (offset & 0x1f) * 8;
-
- // Set transparency to full
- if (offset & 0x8000)
- {
- image[index + 3] = 0x00;
- }
- }
- }
- }
- else
- #endif
- if (_object_textures[j].tile == texture &&
- _object_textures[j].transparency_flags == 2)
- {
- xmin = 999;
- xmax = 0;
- ymin = 999;
- ymax = 0;
- y = 4;
-
- // Account for triangles
- if (_object_textures[j].vertices[3].xpixel == 0 &&
- _object_textures[j].vertices[3].ypixel == 0)
- y = 3;
-
- for (k = 0; k < y; k++)
- {
- if (_object_textures[j].vertices[k].xpixel > xmax)
- xmax = _object_textures[j].vertices[k].xpixel;
-
- if (_object_textures[j].vertices[k].xpixel < xmin)
- xmin = _object_textures[j].vertices[k].xpixel;
-
- if (_object_textures[j].vertices[k].ypixel > ymax)
- ymax = _object_textures[j].vertices[k].ypixel;
-
- if (_object_textures[j].vertices[k].ypixel < ymin)
- ymin = _object_textures[j].vertices[k].ypixel;
- }
-
- for (x = xmin; x <= xmax; x++)
- {
- for (y = ymin; y <= ymax; y++)
- {
- if (_textile32)
- {
- index = (y * 256) + x;
- color = _textile32[texture].tile[index];
- index = (y * 1024) + (x * 4);
-
- image[index + 2] = *((unsigned char *)(&color));
- image[index + 1] = *((unsigned char *)(&color)+1);
- image[index + 0] = *((unsigned char *)(&color)+2);
- image[index + 3] = *((unsigned char *)(&color)+3);
-
- k = image[index] + image[index + 1] + image[index + 2];
-
- // Set transparency based upon intensity
- image[index + 3] = (unsigned char)(k / 3);
- }
- else
- {
- index = (y * 256) + x;
- offset = _textile16[texture].tile[index];
- index = (y * 1024) + (x * 4);
-
- image[index + 0] = ((offset >> 10) & 0x1f) * 8;
- image[index + 1] = ((offset >> 5) & 0x1f) * 8;
- image[index + 2] = (offset & 0x1f) * 8;
- image[index + 3] = (offset & 0x8000) ? 0xFF : 0;
-
- k = image[index] + image[index + 1] + image[index + 2];
-
- // Set transparency based upon intensity
- if (offset & 0x8000)
- image[index + 3] = (unsigned char)(k / 3);
- else
- image[index + 3] = 0;
- }
- }
- }
- }
- }
- break;
- case TR_VERSION_1:
- case TR_VERSION_2:
- case TR_VERSION_5:
- case TR_VERSION_UNKNOWN:
- break;
- }
- }
-
- return image;
- }
-
-
- //! \fixme Move these data about to make full use in the class ;)
- int TombRaider::loadTR5(FILE *f, void (*percent)(int))
- {
- unsigned int level_data_sz, riffOffset, seperator0;
- unsigned int portalOffset, nextRoomOffset, thisRoomOffset;
- int i, j, k;
- u_int16_t us;
-
- u_int32_t numMeshData, numMeshPointers, u;
- u_int32_t *meshPointers;
- u_int8_t *meshData;
- char check[32];
-
- if (percent)
- (*percent)(5);
-
- printDebug("Load", "mEngineVersion = 0x%x", mPakVersion);
-
- if (mEngineVersion != TR_VERSION_5)
- return -1;
-
- unsigned int sz, usz; // compressed and uncompressed size
- unsigned char *compressed_data = NULL;
- int zerr;
- uLongf foo;
-
-
- // Read texture type offsets
- Fread(&_num_room_textures, 2, 1, f);
- printDebug("LoadTR5", "_num_room_textures = %u", _num_room_textures);
- Fread(&_num_misc_textures, 2, 1, f);
- printDebug("LoadTR5", "_num_misc_textures = %u", _num_misc_textures);
- Fread(&_num_bump_map_textures, 2, 1, f);
- printDebug("LoadTR5", "_num_bump_map_textures = %u", _num_bump_map_textures);
-
-
- // Read the sizes of the 32-bit textures
- Fread(&usz, sizeof(usz), 1, f);
- Fread(&sz, sizeof(sz), 1, f);
-
- printDebug("LoadTR5", "32-bit textures compressed size = %u bytes", sz);
- printDebug("LoadTR5", "32-bit textures uncompressed size = %u bytes", usz);
-
- _num_textiles = usz / sizeof(tr2_textile32_t);
-
- printDebug("LoadTR5", "_num_textiles = %i/%lu = %i",
- usz, sizeof(tr2_textile32_t), _num_textiles);
-
- _textile32 = new tr2_textile32_t[_num_textiles];
-
- // Allocate a temporary buffer for decompression
- compressed_data = new unsigned char[sz];
- Fread(compressed_data, sz, 1, f);
-
- // Decompress the textures
- foo = usz;
- zerr = uncompress((unsigned char *)_textile32,
- &foo,
- compressed_data,
- sz);
- usz = foo;
-
- printDebug("LoadTR5", "textile decompress [%s]",
- (zerr == Z_OK) ? "OK" : "ERROR");
-
- switch (zerr)
- {
- case Z_MEM_ERROR:
- printDebug("LoadTR5", "There was not enough memory");
- break;
- case Z_BUF_ERROR:
- printDebug("LoadTR5", "There was not enough room in the output buffer");
- break;
- case Z_DATA_ERROR:
- printDebug("LoadTR5", "The input data was corrupted");
- break;
- default:
- printDebug("LoadTR5", "textile decompress %i", zerr);
- }
-
- // Free the temporary buffer
- delete [] compressed_data;
-
- // Read in the 16-bit textures, set NumTextiles
- Fread(&usz, sizeof(usz), 1, f);
- Fread(&sz, sizeof(sz), 1, f);
-
- printDebug("LoadTR5", "16-bit textures compressed size = %u bytes", sz);
- printDebug("LoadTR5", "16-bit textures uncompressed size = %u bytes", usz);
-
- _num_textiles = usz / sizeof(tr2_textile16_t);
-
- printDebug("LoadTR5", "_num_textiles = %i/%lu = %i",
- usz, sizeof(tr2_textile16_t), _num_textiles);
-
- _textile16 = new tr2_textile16_t[_num_textiles];
-
- // Allocate a temporary buffer for decompression
- compressed_data = new unsigned char[sz];
- Fread(compressed_data, sz, 1, f);
-
- // Decompress the textures
- foo = usz;
- zerr = uncompress((unsigned char *)_textile16,
- &foo,
- compressed_data,
- sz);
- usz = foo;
-
- if (percent)
- (*percent)(7);
-
- printDebug("LoadTR5", "textile decompress [%s]",
- (zerr == Z_OK) ? "OK" : "ERROR");
-
- switch (zerr)
- {
- case Z_MEM_ERROR:
- printDebug("LoadTR5", "There was not enough memory");
- break;
- case Z_BUF_ERROR:
- printDebug("LoadTR5", "There was not enough room in the output buffer");
- break;
- case Z_DATA_ERROR:
- printDebug("LoadTR5", "The input data was corrupted");
- break;
- default:
- printDebug("LoadTR5", "textile decompress %i", zerr);
- }
-
- // Free the temporary buffer
- delete [] compressed_data;
-
- // Read the sizes of the sprite textures
- Fread(&usz, sizeof(usz), 1, f);
- Fread(&sz, sizeof(sz), 1, f);
-
- printDebug("LoadTR5", "sprite textures compressed size = %u bytes", sz);
- printDebug("LoadTR5", "sprite textures uncompressed size = %u bytes", usz);
-
- // Load sprite/bump map/gui/etc textures also
- _num_tex_special = usz/(256*256*4);
-
- printDebug("LoadTR5", "_num_tex_special = %i/%i = %i",
- usz, 256*256*4, _num_tex_special);
-
- printDebug("LoadTR5", "Reading %ibytes of sprite textures", usz);
-
- if (usz)
- {
- _tex_special = new unsigned char[usz];
-
- // Allocate a temporary buffer for decompression
- compressed_data = new unsigned char[sz];
- Fread(compressed_data, sz, 1, f);
-
- // Decompress the textures
- foo = usz;
- zerr = uncompress(_tex_special,
- &foo,
- compressed_data,
- sz);
- usz = foo;
-
- printDebug("LoadTR5", "special texture decompress [%s]",
- (zerr == Z_OK) ? "OK" : "ERROR");
-
- switch (zerr)
- {
- case Z_MEM_ERROR:
- printDebug("LoadTR5", "There was not enough memory");
- break;
- case Z_BUF_ERROR:
- printDebug("LoadTR5", "There was not enough room in the output buffer");
- break;
- case Z_DATA_ERROR:
- printDebug("LoadTR5", "The input data was corrupted");
- break;
- default:
- printDebug("LoadTR5", "textile decompress %i", zerr);
- }
-
- // Free the temporary buffer
- delete [] compressed_data;
- }
-
- if (percent)
- (*percent)(10);
-
- // Mongoose 2002.01.08, Michiel has discovered the
- // first 4 bytes here are 2 bitu16 flags for Lara type and weather
- u_int16_t laraType, weather;
-
- Fread(&laraType, 2, 1, f);
- printDebug("LoadTR5", "laraType = 0x%x", laraType);
- Fread(&weather, 2, 1, f);
- printDebug("LoadTR5", "weather = 0x%x", weather);
-
- printDebug("LoadTR5", "skipping 28bytes of unknowns");
- Fread(&seperator0, 4, 1, f);
- printDebug("LoadTR5", "0x%x", seperator0);
- Fread(&seperator0, 4, 1, f);
- printDebug("LoadTR5", "0x%x", seperator0);
- Fread(&seperator0, 4, 1, f);
- printDebug("LoadTR5", "0x%x", seperator0);
- Fread(&seperator0, 4, 1, f);
- printDebug("LoadTR5", "0x%x", seperator0);
- Fread(&seperator0, 4, 1, f);
- printDebug("LoadTR5", "0x%x", seperator0);
- Fread(&seperator0, 4, 1, f);
- printDebug("LoadTR5", "0x%x", seperator0);
- Fread(&seperator0, 4, 1, f);
- printDebug("LoadTR5", "0x%x", seperator0);
-
- Fread(&level_data_sz, 4, 1, f);
- printDebug("LoadTR5", "Level data size = %u", level_data_sz);
-
- Fread(&riffOffset, 4, 1, f);
- printDebug("LoadTR5", "Same as last, also offset to RIFFs = %u",
- riffOffset);
-
- Fread(&seperator0, 4, 1, f);
- printDebug("LoadTR5", "Seperator, always 0x00? = %u", seperator0);
-
- Fread(&_num_rooms, 4, 1, f);
- printDebug("LoadTR5", "_num_rooms = %u", _num_rooms);
-
- mRoomsTR5 = new tr5_room_t[_num_rooms];
-
- for (i = 0; i < _num_rooms; ++i)
- {
- if (percent)
- (*percent)(11 + (int)(((float)i/(float)_num_rooms)*90.0));
-
- thisRoomOffset = ftell(f);
-
- Fread(&mRoomsTR5[i].checkXELA, 4, 1, f);
- printDebug("LoadTR5", "room[%i].checkXELA (0x414c4558)? = 0x%x",
- i, mRoomsTR5[i].checkXELA);
-
- if (mRoomsTR5[i].checkXELA != 0x414c4558)
- {
- print("LoadTR5", "Error #1 room[%i].checkXELA (0x414c4558) != 0x%x",
- i, mRoomsTR5[i].checkXELA);
- return -3;
- }
-
- Fread(&mRoomsTR5[i].roomDataSize, 4, 1, f);
- printDebug("LoadTR5", "offset to next room = %u",
- mRoomsTR5[i].roomDataSize);
-
- nextRoomOffset = ftell(f) + mRoomsTR5[i].roomDataSize;
-
- Fread(&mRoomsTR5[i].seperator, 4, 1, f);
- printDebug("LoadTR5", "room[%i].seperator, CDCDCDCD = %X?",
- i, mRoomsTR5[i].seperator);
-
- if (mRoomsTR5[i].seperator != 0xcdcdcdcd)
- {
- print("LoadTR5", "Error #2 room[%i].seperator, CDCDCDCD != 0x%X",
- i, mRoomsTR5[i].seperator);
- return -3;
- }
-
- // Start 60byte struct /////////////
- printDebug("LoadTR5", "60byte struct {");
-
- // Often start of "XELA" +216 + ublock1 = FD end,
- // but have seen 0xffffffff (-1). Better using next data
- // and compute FD size the old way of X*Z*8
- Fread(&mRoomsTR5[i].endSDOffset, 4, 1, f);
- printDebug("LoadTR5", "%u + 216 + XELA start = FD end",
- mRoomsTR5[i].endSDOffset);
-
- // Start of "XELA" + 216 + ublock2 = FD start
- Fread(&mRoomsTR5[i].startSDOffset, 4, 1, f);
- printDebug("LoadTR5", "%u + 216 + XELA start = FD start",
- mRoomsTR5[i].startSDOffset);
-
- Fread(&mRoomsTR5[i].seperator2, 4, 1, f);
- printDebug("LoadTR5", "seperator2 = %u", mRoomsTR5[i].seperator2);
-
- if (mRoomsTR5[i].seperator2 != 0xcdcdcdcd &&
- mRoomsTR5[i].seperator2 != 0x00000000)
- {
- print("LoadTR5", "Error #3 CDCDCDCD | 0x0 != 0x%x",
- mRoomsTR5[i].seperator2);
- return -3;
- }
-
- // Possibly start of "XELA" + 216 + ublock4 = end portals
- Fread(&mRoomsTR5[i].endPortalOffset, 4, 1, f);
- printDebug("LoadTR5", "%u + 216 + XELA start = end portals",
- mRoomsTR5[i].endPortalOffset);
-
- Fread(&mRoomsTR5[i].x, 4, 1, f);
- printDebug("LoadTR5", "room.x = %u", mRoomsTR5[i].x);
- Fread(&mRoomsTR5[i].seperator3, 4, 1, f);
- printDebug("LoadTR5", "0x00000000 = %u ?", mRoomsTR5[i].seperator3);
- Fread(&mRoomsTR5[i].z, 4, 1, f);
- printDebug("LoadTR5", "room.z = %u", mRoomsTR5[i].z);
- Fread(&mRoomsTR5[i].yBottom, 4, 1, f);
- printDebug("LoadTR5", "room.y_bottom = %u", mRoomsTR5[i].yBottom);
- Fread(&mRoomsTR5[i].yTop, 4, 1, f);
- printDebug("LoadTR5", "room.y_top = %u", mRoomsTR5[i].yTop);
-
- Fread(&mRoomsTR5[i].numZSectors, 2, 1, f);
- printDebug("LoadTR5", "num_z_sectors = %i", mRoomsTR5[i].numZSectors);
- Fread(&mRoomsTR5[i].numXSectors, 2, 1, f);
- printDebug("LoadTR5", "num_x_sectors = %i", mRoomsTR5[i].numXSectors);
-
- Fread(&mRoomsTR5[i].roomAmbientColor, 4, 1, f);
- printDebug("LoadTR5", "room_ambient_color = 0x%x",
- mRoomsTR5[i].roomAmbientColor);
-
- Fread(&mRoomsTR5[i].numRoomLights, 2, 1, f);
- printDebug("LoadTR5", "num_lights = %i", mRoomsTR5[i].numRoomLights);
- Fread(&mRoomsTR5[i].numStaticMeshes, 2, 1, f);
- printDebug("LoadTR5", "num_static_meshes = %i",
- mRoomsTR5[i].numStaticMeshes);
-
- Fread(&mRoomsTR5[i].unknownR1, 2, 1, f);
- printDebug("LoadTR5", "unknown 0x0001? = 0x%x", mRoomsTR5[i].unknownR1);
- Fread(&mRoomsTR5[i].unknownR2, 2, 1, f);
- printDebug("LoadTR5", "unknown 0x0000? = 0x%x", mRoomsTR5[i].unknownR2);
- Fread(&mRoomsTR5[i].filler, 4, 1, f);
- printDebug("LoadTR5", "Always 0x7fff? = 0x%x", mRoomsTR5[i].filler);
- Fread(&mRoomsTR5[i].filler2, 4, 1, f);
- printDebug("LoadTR5", "Always 0x7fff? = 0x%x", mRoomsTR5[i].filler2);
-
- printDebug("LoadTR5", "}");
- // End 60byte structure /////////////////
-
- Fread(&mRoomsTR5[i].seperator4, 4, 1, f);
- printDebug("LoadTR5", "seperator4 CDCDCDCD = 0x%x",
- mRoomsTR5[i].seperator4);
-
- if (mRoomsTR5[i].seperator4 != 0xcdcdcdcd)
- {
- print("LoadTR5", "Error #5 CDCDCDCD != 0x%x",
- mRoomsTR5[i].seperator4);
- return -3;
- }
-
- Fread(&mRoomsTR5[i].seperator5, 4, 1, f);
- printDebug("LoadTR5", "seperator5 CDCDCDCD = 0x%x",
- mRoomsTR5[i].seperator5);
-
- if (mRoomsTR5[i].seperator5 != 0xcdcdcdcd)
- {
- print("LoadTR5", "Error #6 CDCDCDCD != 0x%x",
- mRoomsTR5[i].seperator5);
- return -3;
- }
-
- // Start 20byte structure ///////////////
- printDebug("LoadTR5", "20byte struct {");
- Fread(mRoomsTR5[i].seperator6, 6, 1, f);
- printDebug("LoadTR5", "6 bytes 0xFF = 0x%x%x%x%x%x%x",
- mRoomsTR5[i].seperator6[0], mRoomsTR5[i].seperator6[1],
- mRoomsTR5[i].seperator6[2], mRoomsTR5[i].seperator6[3],
- mRoomsTR5[i].seperator6[4], mRoomsTR5[i].seperator6[5]);
- Fread(&mRoomsTR5[i].roomFlag, 2, 1, f);
- printDebug("LoadTR5", "room_flag = %i", mRoomsTR5[i].roomFlag);
- Fread(&mRoomsTR5[i].unknownR5, 2, 1, f);
- printDebug("LoadTR5", "unknown = %i", mRoomsTR5[i].unknownR5);
- Fread(mRoomsTR5[i].seperator7, 10, 1, f);
- printDebug("LoadTR5", "10 bytes 0x00 = 0x%x%x%x%x%x%x%x%x%x%x",
- mRoomsTR5[i].seperator7[0], mRoomsTR5[i].seperator7[1],
- mRoomsTR5[i].seperator7[2], mRoomsTR5[i].seperator7[3],
- mRoomsTR5[i].seperator7[4], mRoomsTR5[i].seperator7[5],
- mRoomsTR5[i].seperator7[6], mRoomsTR5[i].seperator7[7],
- mRoomsTR5[i].seperator7[8], mRoomsTR5[i].seperator7[9]);
- printDebug("LoadTR5", "}");
- // End 20byte structure /////////////////
-
- Fread(&mRoomsTR5[i].seperator8, 4, 1, f);
- printDebug("LoadTR5", "seperator8 CDCDCDCD = 0x%x",
- mRoomsTR5[i].seperator8);
-
- if (mRoomsTR5[i].seperator8 != 0xcdcdcdcd)
- {
- print("LoadTR5", "Error #9 CDCDCDCD != 0x%x",
- mRoomsTR5[i].seperator8);
- return -3;
- }
-
- printDebug("LoadTR5", "16byte struct {");
- Fread(&mRoomsTR5[i].unknownR6, 4, 1, f);
- printDebug("LoadTR5", "unknownR6 = %i", mRoomsTR5[i].unknownR6);
- Fread(&mRoomsTR5[i].roomX, 4, 1, f);
- printDebug("LoadTR5", "roomX = %f", mRoomsTR5[i].roomX);
-
- Fread(&mRoomsTR5[i].seperator9, 4, 1, f);
- printDebug("LoadTR5", "seperator9 CDCDCDCD | 0x0 = 0x%x",
- mRoomsTR5[i].seperator9);
-
- if (mRoomsTR5[i].seperator9 != 0xcdcdcdcd &&
- mRoomsTR5[i].seperator9 != 0x0)
- {
- print("LoadTR5", "Error #10 CDCDCDCD | 0x0 != 0x%x",
- mRoomsTR5[i].seperator9);
- return -3;
- }
-
- Fread(&mRoomsTR5[i].roomZ, 4, 1, f);
- printDebug("LoadTR5", "roomZ = %f", mRoomsTR5[i].roomZ);
- printDebug("LoadTR5", "}");
-
-
- Fread(&mRoomsTR5[i].seperator10, 4, 1, f);
- printDebug("LoadTR5", "seperator10 CDCDCDCD = 0x%x",
- mRoomsTR5[i].seperator10);
-
- if (mRoomsTR5[i].seperator10 != 0xcdcdcdcd)
- {
- print("LoadTR5", "Error #11 CDCDCDCD != 0x%x",
- mRoomsTR5[i].seperator10);
- return -3;
- }
-
- Fread(&mRoomsTR5[i].seperator11, 4, 1, f);
- printDebug("LoadTR5", "seperator11 CDCDCDCD = 0x%x",
- mRoomsTR5[i].seperator11);
-
- if (mRoomsTR5[i].seperator11 != 0xcdcdcdcd)
- {
- print("LoadTR5", "Error #12 CDCDCDCD != 0x%x",
- mRoomsTR5[i].seperator11);
- return -3;
- }
-
- Fread(&mRoomsTR5[i].seperator12, 4, 1, f);
- printDebug("LoadTR5", "seperator12 CDCDCDCD = 0x%x",
- mRoomsTR5[i].seperator12);
-
- if (mRoomsTR5[i].seperator12 != 0xcdcdcdcd)
- {
- print("LoadTR5", "Error #13 CDCDCDCD != 0x%x",
- mRoomsTR5[i].seperator12);
- return -3;
- }
-
- Fread(&mRoomsTR5[i].seperator13, 4, 1, f);
- printDebug("LoadTR5", "seperator13 CDCDCDCD = 0x%x",
- mRoomsTR5[i].seperator13);
-
- if (mRoomsTR5[i].seperator13 != 0xcdcdcdcd)
- {
- print("LoadTR5", "Error #14 CDCDCDCD | 0x0 != 0x%x",
- mRoomsTR5[i].seperator13);
- return -3;
- }
-
- Fread(&mRoomsTR5[i].seperator14, 4, 1, f);
- printDebug("LoadTR5", "seperator14 CDCDCDCD = 0x%x",
- mRoomsTR5[i].seperator14);
-
- if (mRoomsTR5[i].seperator14 != 0xcdcdcdcd &&
- mRoomsTR5[i].seperator14 != 0x00000000)
- {
- print("LoadTR5", "Error #15 CDCDCDCD | 0x0 != 0x%x",
- mRoomsTR5[i].seperator14);
- return -3;
- }
-
- Fread(&mRoomsTR5[i].seperator15, 4, 1, f);
- printDebug("LoadTR5", "seperator15 CDCDCDCD = 0x%x",
- mRoomsTR5[i].seperator15);
-
- if (mRoomsTR5[i].seperator15 != 0xcdcdcdcd)
- {
- print("LoadTR5", "Error #16 CDCDCDCD != 0x%x",
- mRoomsTR5[i].seperator15);
- return -3;
- }
-
-
- // 56byte struct /////////////
- printDebug("LoadTR5", "56byte struct {");
- Fread(&mRoomsTR5[i].numRoomTriangles, 4, 1, f);
- printDebug("LoadTR5", "num_triangles = %u",
- mRoomsTR5[i].numRoomTriangles);
- Fread(&mRoomsTR5[i].numRoomRectangles, 4, 1, f);
- printDebug("LoadTR5", "num_rectangles = %u",
- mRoomsTR5[i].numRoomRectangles);
- Fread(&mRoomsTR5[i].seperator16, 4, 1, f);
- printDebug("LoadTR5", "seperator16, 0x00? = 0x%x",
- mRoomsTR5[i].seperator16);
-
- // Num lights * 88bytes
- Fread(&mRoomsTR5[i].lightSize, 4, 1, f);
- printDebug("LoadTR5", "light_size = %u", mRoomsTR5[i].lightSize);
- Fread(&mRoomsTR5[i].numTotalRoomLights, 4, 1, f);
- printDebug("LoadTR5", "num_lights = %u",
- mRoomsTR5[i].numTotalRoomLights);
-
- Fread(&mRoomsTR5[i].unknownR7, 4, 1, f); // was num_unknown_36byte structs to read
- printDebug("LoadTR5", "unknownR7 structs = %u", mRoomsTR5[i].unknownR7);
-
- Fread(&mRoomsTR5[i].unknownR8, 4, 1, f);
- printDebug("LoadTR5", "unknownR8 = 0x%x", mRoomsTR5[i].unknownR8);
-
- Fread(&mRoomsTR5[i].yBottom, 4, 1, f);
- printDebug("LoadTR5", "lyBottom = 0x%x", mRoomsTR5[i].lyBottom);
-
- Fread(&mRoomsTR5[i].numLayers, 4, 1, f);
- printDebug("LoadTR5", "num_layers = %u", mRoomsTR5[i].numLayers);
-
- Fread(&mRoomsTR5[i].layerOffset, 4, 1, f);
- printDebug("LoadTR5", "layerOffset = 0x%x", mRoomsTR5[i].layerOffset);
- Fread(&mRoomsTR5[i].verticesOffset, 4, 1, f);
- printDebug("LoadTR5", "verticesOffset = 0x%x", mRoomsTR5[i].verticesOffset);
- Fread(&mRoomsTR5[i].polyOffset, 4, 1, f);
- printDebug("LoadTR5", "polyOffset = 0x%x", mRoomsTR5[i].polyOffset);
- Fread(&mRoomsTR5[i].polyOffset2, 4, 1, f);
- printDebug("LoadTR5", "polyOffset2 = 0x%x", mRoomsTR5[i].polyOffset2);
- Fread(&mRoomsTR5[i].verticesSize, 4, 1, f);
- printDebug("LoadTR5", "verticesSize = 0x%x", mRoomsTR5[i].verticesSize);
-
- printDebug("LoadTR5", "}");
- //////////////////////////////
-
- Fread(&mRoomsTR5[i].seperator17, 4, 1, f);
- printDebug("LoadTR5", "seperator17 CDCDCDCD = 0x%x",
- mRoomsTR5[i].seperator17);
-
- if (mRoomsTR5[i].seperator17 != 0xcdcdcdcd)
- {
- print("LoadTR5", "Error #18 CDCDCDCD != 0x%x",
- mRoomsTR5[i].seperator17);
- return -3;
- }
-
- Fread(&mRoomsTR5[i].seperator18, 4, 1, f);
- printDebug("LoadTR5", "seperator18 CDCDCDCD = 0x%x",
- mRoomsTR5[i].seperator18);
-
- if (mRoomsTR5[i].seperator18 != 0xcdcdcdcd)
- {
- print("LoadTR5", "Error #19 CDCDCDCD != 0x%x",
- mRoomsTR5[i].seperator18);
- return -3;
- }
-
- Fread(&mRoomsTR5[i].seperator19, 4, 1, f);
- printDebug("LoadTR5", "seperator19 CDCDCDCD = 0x%x",
- mRoomsTR5[i].seperator19);
-
- if (mRoomsTR5[i].seperator19 != 0xcdcdcdcd)
- {
- print("LoadTR5", "Error #20 CDCDCDCD != 0x%x",
- mRoomsTR5[i].seperator19);
- return -3;
- }
-
- Fread(&mRoomsTR5[i].seperator20, 4, 1, f);
- printDebug("LoadTR5", "seperator20 CDCDCDCD = 0x%x",
- mRoomsTR5[i].seperator20);
-
- if (mRoomsTR5[i].seperator20 != 0xcdcdcdcd)
- {
- print("LoadTR5", "Error #21 CDCDCDCD != 0x%x",
- mRoomsTR5[i].seperator20);
- return -3;
- }
-
- // Lights
- printDebug("LoadTR5", "Reading %u lights @ 88bytes each",
- mRoomsTR5[i].numRoomLights);
-
- if (mRoomsTR5[i].numRoomLights)
- {
- mRoomsTR5[i].lights = new tr5_light_t[mRoomsTR5[i].numRoomLights];
- }
-
- for (j = 0; j < (int)mRoomsTR5[i].numRoomLights; ++j)
- {
- Fread(&mRoomsTR5[i].lights[j], 88, 1, f);
-
- //Fread(&mRoomsTR5[i].lights[j].x, 4, 1, f);
- printDebug("LoadTR5", "light[%i].x? = %f", j,
- mRoomsTR5[i].lights[j].x);
-
- //Fread(&mRoomsTR5[i].lights[j].y, 4, 1, f);
- printDebug("LoadTR5", "light[%i].y? = %f", j,
- mRoomsTR5[i].lights[j].y);
-
- //Fread(&mRoomsTR5[i].lights[j].z, 4, 1, f);
- printDebug("LoadTR5", "light[%i].z? = %f", j,
- mRoomsTR5[i].lights[j].z);
-
- //Fread(&mRoomsTR5[i].lights[j].red, 4, 1, f);
- printDebug("LoadTR5", "light[%i].r? = %f",
- j, mRoomsTR5[i].lights[j].red);
-
- //Fread(&mRoomsTR5[i].lights[j].green, 4, 1, f);
- printDebug("LoadTR5", "light[%i].g? = %f",
- j, mRoomsTR5[i].lights[j].green);
-
- //Fread(&mRoomsTR5[i].lights[j].blue, 4, 1, f);
- printDebug("LoadTR5", "light[%i].b? = %f",
- j, mRoomsTR5[i].lights[j].blue);
-
- // 24bytes from start of light
- //Fread(&mRoomsTR5[i].lights[j].seperator, 4, 1, f);
- printDebug("LoadTR5", "CDCDCDCD for some maps? = 0x%8x\t\t[%s]",
- mRoomsTR5[i].lights[j].seperator,
- (mRoomsTR5[i].lights[j].seperator == 0xcdcdcdcd)
- ? "OK" : "ERROR");
-
- //Fread(&mRoomsTR5[i].lights[j].input, 4, 1, f);
- printDebug("LoadTR5", "light[%i].input = %f",
- j, mRoomsTR5[i].lights[j].input);
-
- //Fread(&mRoomsTR5[i].lights[j].output, 4, 1, f);
- printDebug("LoadTR5", "light[%i].output = %f",
- j, mRoomsTR5[i].lights[j].output);
-
- //Fread(&mRoomsTR5[i].lights[j].range, 4, 1, f);
- printDebug("LoadTR5", "light[%i].range = %f",
- j, mRoomsTR5[i].lights[j].range);
-
- //Fread(&mRoomsTR5[i].lights[j].directionVectorX, 4, 1, f);
- printDebug("LoadTR5", "light[%i].directionVectorX = %f",
- j, mRoomsTR5[i].lights[j].directionVectorX);
- //Fread(&mRoomsTR5[i].lights[j].directionVectorY, 4, 1, f);
- printDebug("LoadTR5", "light[%i].directionVectorY = %f",
- j, mRoomsTR5[i].lights[j].directionVectorY);
- //Fread(&mRoomsTR5[i].lights[j].directionVectorZ, 4, 1, f);
- printDebug("LoadTR5", "light[%i].directionVectorZ = %f",
- j, mRoomsTR5[i].lights[j].directionVectorZ);
-
- //Fread(&mRoomsTR5[i].lights[j].x2, 4, 1, f);
- printDebug("LoadTR5", "light[%i].x2 = %u",
- j, mRoomsTR5[i].lights[j].x2);
- //Fread(&mRoomsTR5[i].lights[j].y2, 4, 1, f);
- printDebug("LoadTR5", "light[%i].y2 = %u",
- j, mRoomsTR5[i].lights[j].y2);
- //Fread(&mRoomsTR5[i].lights[j].z2, 4, 1, f);
- printDebug("LoadTR5", "light[%i].z2 = %u",
- j, mRoomsTR5[i].lights[j].z2);
-
-
- //Fread(&mRoomsTR5[i].lights[j].directionVectorX2, 4, 1, f);
- printDebug("LoadTR5", "light[%i].directionVectorX2 = %u",
- j, mRoomsTR5[i].lights[j].directionVectorX2);
- //Fread(&mRoomsTR5[i].lights[j].directionVectorY2, 4, 1, f);
- printDebug("LoadTR5", "light[%i].directionVectorY2 = %u",
- j, mRoomsTR5[i].lights[j].directionVectorY2);
- //Fread(&mRoomsTR5[i].lights[j].directionVectorZ2, 4, 1, f);
- printDebug("LoadTR5", "light[%i].directionVectorZ2 = %u",
- j, mRoomsTR5[i].lights[j].directionVectorZ2);
-
- //Fread(&mRoomsTR5[i].lights[j].lightType, 1, 1, f);
- printDebug("LoadTR5", "light[%i].d3d_flag = 0x%x (%s)",
- j, mRoomsTR5[i].lights[j].lightType,
- (mRoomsTR5[i].lights[j].lightType == 1) ? "Point" :
- (mRoomsTR5[i].lights[j].lightType == 2) ? "Spot" :
- (mRoomsTR5[i].lights[j].lightType == 3) ? "Directional" : "Unknown");
-
- //Fread(&mRoomsTR5[i].lights[j].seperator2, 3, 1, f);
- printDebug("LoadTR5", "CDCDCD = %c%c%c",
- mRoomsTR5[i].lights[j].seperator2[0],
- mRoomsTR5[i].lights[j].seperator2[1],
- mRoomsTR5[i].lights[j].seperator2[2]);
- }
-
- int numSectors = mRoomsTR5[i].numXSectors * mRoomsTR5[i].numZSectors;
-
- if (numSectors)
- {
- mRoomsTR5[i].sectors = new tr2_room_sector_t[numSectors];
- }
-
- // Sectors
- printDebug("LoadTR5", "Reading %u sectors @ 8bytes each",
- numSectors);
-
- for (j = 0; j < numSectors; ++j)
- {
- Fread(&mRoomsTR5[i].sectors[j].fd_index, 2, 1, f);
- printDebug("LoadTR5", "sector[%i].fd_index = %u", j,
- mRoomsTR5[i].sectors[j].fd_index);
- Fread(&mRoomsTR5[i].sectors[j].box_index, 2, 1, f);
- printDebug("LoadTR5", "sector[%i].box_index = %u", j,
- mRoomsTR5[i].sectors[j].box_index);
- Fread(&mRoomsTR5[i].sectors[j].room_below, 1, 1, f);
- printDebug("LoadTR5", "sector[%i].room_below = %u", j,
- mRoomsTR5[i].sectors[j].room_below);
- Fread(&mRoomsTR5[i].sectors[j].floor, 1, 1, f);
- printDebug("LoadTR5", "sector[%i].floor = %i", j,
- (*(char*)(&mRoomsTR5[i].sectors[j].floor)));
- Fread(&mRoomsTR5[i].sectors[j].room_above, 1, 1, f);
- printDebug("LoadTR5", "sector[%i].room_above = %u", j,
- mRoomsTR5[i].sectors[j].room_above);
- Fread(&mRoomsTR5[i].sectors[j].ceiling, 1, 1, f);
- printDebug("LoadTR5", "sector[%i].ceiling = %i", j,
- (*(char*)(&mRoomsTR5[i].sectors[j].ceiling)));
- }
-
- portalOffset = (thisRoomOffset + mRoomsTR5[i].startSDOffset + 216 +
- numSectors * 8);
-
- u = ftell(f);
-
- if (u != portalOffset)
- {
- printDebug("LoadTR5", "*** Skipping %i bytes to start of portals ***",
- portalOffset - u);
- fseek(f, portalOffset, SEEK_SET);
- }
-
- // Portals //////////////////////
- Fread(&mRoomsTR5[i].numDoors, 2, 1, f);
- printDebug("LoadTR5", "room[%i].tr5_num_portals = %u",
- i, mRoomsTR5[i].numDoors);
-
- printDebug("LoadTR5", "Reading %u portals @ 32bytes each",
- mRoomsTR5[i].numDoors);
-
- if (mRoomsTR5[i].numDoors)
- {
- mRoomsTR5[i].doors = new tr2_room_portal_t[mRoomsTR5[i].numDoors];
- }
-
- for (j = 0; j < (int)mRoomsTR5[i].numDoors; ++j)
- {
- Fread(&mRoomsTR5[i].doors[j].adjoining_room, 2, 1, f);
- printDebug("LoadTR5", "room[%i].portal[%i].adjoining_room = %u",
- i, j, mRoomsTR5[i].doors[j].adjoining_room);
-
- Fread(&mRoomsTR5[i].doors[j].normal.x, 2, 1, f);
- Fread(&mRoomsTR5[i].doors[j].normal.y, 2, 1, f);
- Fread(&mRoomsTR5[i].doors[j].normal.z, 2, 1, f);
- printDebug("LoadTR5", "portal[%i].normal = ( %i, %i, %i )", j,
- mRoomsTR5[i].doors[j].normal.x,
- mRoomsTR5[i].doors[j].normal.y,
- mRoomsTR5[i].doors[j].normal.z);
-
- for (k = 0; k < 4; ++k)
- {
- Fread(&mRoomsTR5[i].doors[j].vertices[k].x, 2, 1, f);
- Fread(&mRoomsTR5[i].doors[j].vertices[k].y, 2, 1, f);
- Fread(&mRoomsTR5[i].doors[j].vertices[k].z, 2, 1, f);
- printDebug("LoadTR5", "portal[%i].vertices[%i] = ( %i, %i, %i )",
- j, k,
- mRoomsTR5[i].doors[j].vertices[k].x,
- mRoomsTR5[i].doors[j].vertices[k].y,
- mRoomsTR5[i].doors[j].vertices[k].z);
- }
- }
-
- Fread(&mRoomsTR5[i].seperator21, 2, 1, f);
- printDebug("LoadTR5", "seperator21, CDCD = 0x%x",
- mRoomsTR5[i].seperator21);
-
- if (mRoomsTR5[i].seperator21 != 0xcdcd)
- {
- print("LoadTR5", "Error #22 CDCD != 0x%x",
- mRoomsTR5[i].seperator21);
- return -3;
- }
-
- portalOffset = (thisRoomOffset + mRoomsTR5[i].endPortalOffset + 216);
-
- u = ftell(f);
-
- if (u != portalOffset)
- {
- printDebug("LoadTR5", "*** Skipping %i bytes to end of portals ***",
- portalOffset - u);
- fseek(f, portalOffset, SEEK_SET);
- }
-
- if (mRoomsTR5[i].numStaticMeshes)
- {
- mRoomsTR5[i].meshes = new tr2_room_staticmesh_t[mRoomsTR5[i].numStaticMeshes];
- }
-
- // Static meshes
- for (j = 0; j < (int)mRoomsTR5[i].numStaticMeshes; ++j)
- {
- Fread(&mRoomsTR5[i].meshes[j].x, 4, 1, f);
- printDebug("LoadTR5", "static_mesh[%i].x = %i", j,
- mRoomsTR5[i].meshes[j].x);
- Fread(&mRoomsTR5[i].meshes[j].y, 4, 1, f);
- printDebug("LoadTR5", "static_mesh[%i].y = %i", j,
- mRoomsTR5[i].meshes[j].y);
- Fread(&mRoomsTR5[i].meshes[j].z, 4, 1, f);
- printDebug("LoadTR5", "static_mesh[%i].z = %i", j,
- mRoomsTR5[i].meshes[j].z);
-
- Fread(&mRoomsTR5[i].meshes[j].rotation, 2, 1, f);
- printDebug("LoadTR5", "static_mesh[%i].rotation = %i", j,
- mRoomsTR5[i].meshes[j].rotation);
-
- Fread(&mRoomsTR5[i].meshes[j].intensity1, 2, 1, f);
- printDebug("LoadTR5", "static_mesh[%i].intensity1 = %i", j,
- mRoomsTR5[i].meshes[j].intensity1);
- Fread(&mRoomsTR5[i].meshes[j].intensity2, 2, 1, f);
- printDebug("LoadTR5", "static_mesh[%i].intensity2 = %i", j,
- mRoomsTR5[i].meshes[j].intensity2);
-
- Fread(&mRoomsTR5[i].meshes[j].object_id, 2, 1, f);
- printDebug("LoadTR5", "static_mesh[%i].object_id = %i", j,
- mRoomsTR5[i].meshes[j].object_id);
- }
-
- // Layers /////////////////
- if (mRoomsTR5[i].numLayers)
- {
- mRoomsTR5[i].layers = new tr5_room_layer_t[mRoomsTR5[i].numLayers];
- }
-
- printDebug("LoadTR5", "Reading %i layers",
- mRoomsTR5[i].numLayers);
-
- for (j = 0; j < (int)mRoomsTR5[i].numLayers; ++j)
- {
- Fread(&mRoomsTR5[i].layers[j], 56, 1, f);
-
- printDebug("LoadTR5", "layer[%i].num_vertices = %i", j,
- mRoomsTR5[i].layers[j].numLayerVertices);
- printDebug("LoadTR5", "layer[%i].unknown1 = %i", j,
- mRoomsTR5[i].layers[j].unknownL1);
- printDebug("LoadTR5", "layer[%i].num_rectangles = %i", j,
- mRoomsTR5[i].layers[j].numLayerRectangles);
- printDebug("LoadTR5", "layer[%i].num_triangles = %i", j,
- mRoomsTR5[i].layers[j].numLayerTriangles);
- printDebug("LoadTR5", "layer[%i].num_2side_textures? = %i", j,
- mRoomsTR5[i].layers[j].unknownL2);
- printDebug("LoadTR5", "layer[%i].filler, 0? = %i", j,
- mRoomsTR5[i].layers[j].filler);
- printDebug("LoadTR5", "layer[%i].filler2, 0? = %i", j,
- mRoomsTR5[i].layers[j].filler2);
- printDebug("LoadTR5", "layer[%i].bbox[0] = {%.2f %.2f %.2f}", j,
- mRoomsTR5[i].layers[j].layerBoundingBoxX1,
- mRoomsTR5[i].layers[j].layerBoundingBoxX1,
- mRoomsTR5[i].layers[j].layerBoundingBoxX1);
- printDebug("LoadTR5", "layer[%i].bbox[1] = {%.2f %.2f %.2f}", j,
- mRoomsTR5[i].layers[j].layerBoundingBoxX2,
- mRoomsTR5[i].layers[j].layerBoundingBoxX2,
- mRoomsTR5[i].layers[j].layerBoundingBoxX2);
- printDebug("LoadTR5", "layer[%i].filler3, 0? = %i", j,
- mRoomsTR5[i].layers[j].filler3);
- printDebug("LoadTR5", "layer[%i].unknown6 = %i", j,
- mRoomsTR5[i].layers[j].unknownL6);
- printDebug("LoadTR5", "layer[%i].unknown7 = %i", j,
- mRoomsTR5[i].layers[j].unknownL7);
- printDebug("LoadTR5", "layer[%i].unknown8 = %i", j,
- mRoomsTR5[i].layers[j].unknownL8);
- }
-
- if (mRoomsTR5[i].numLayers)
- {
- mRoomsTR5[i].faces = new tr5_room_geometry_t[mRoomsTR5[i].numLayers];
- }
-
- for (j = 0; j < (int)mRoomsTR5[i].numLayers; ++j)
- {
- mRoomsTR5[i].faces[j].quads = 0x0;
- mRoomsTR5[i].faces[j].tris = 0x0;
-
- k = mRoomsTR5[i].layers[j].numLayerRectangles;
-
- if (k)
- {
- printDebug("LoadTR5", "Reading %i layer quads", k);
- mRoomsTR5[i].faces[j].quads = new tr5_face4_t[k];
- Fread(mRoomsTR5[i].faces[j].quads, 12, k, f);
- }
-
- k = mRoomsTR5[i].layers[j].numLayerTriangles;
-
- if (k)
- {
- printDebug("LoadTR5", "Reading %i layer tris", k);
- mRoomsTR5[i].faces[j].tris = new tr5_face3_t[k];
- Fread(mRoomsTR5[i].faces[j].tris, 10, k, f);
- }
- }
-
- for (j = 0; j < (int)mRoomsTR5[i].numLayers; ++j)
- {
- mRoomsTR5[i].faces[j].verts = 0x0;
-
- k = mRoomsTR5[i].layers[j].numLayerVertices;
-
- if (k)
- {
- printDebug("LoadTR5", "Reading %i layer vertices", k);
- mRoomsTR5[i].faces[j].verts = new tr5_vertex_t[k];
- Fread(mRoomsTR5[i].faces[j].verts, 28, k, f);
- }
- }
-
- #define TR5_SKIP_TO_ROOMS
- #ifdef TR5_SKIP_TO_ROOMS
- unsigned int hack = ftell(f);
-
-
- if (hack < nextRoomOffset)
- {
- printDebug("LoadTR5", "Skipping %i bytes at end of room[%i]",
- nextRoomOffset - hack, i);
- fseek(f, nextRoomOffset, SEEK_SET);
- }
- #else
- long hack = 0;
-
- // This peels padding off the end of TRCs like ANDREA1.TRC
- while (hack != 0xcdcd)
- {
- Fread(&hack, 2, 1, f);
- printDebug("LoadTR5", "hack[%i] = 0x%x", i, hack);
- }
- #endif
- }
-
- Fread(&_num_floor_data, 4, 1, f);
- printDebug("LoadTR5", "_num_floor_data = %u", _num_floor_data);
-
- printDebug("LoadTR5", "Reading %u floorData elements", _num_floor_data);
- _floor_data = new unsigned short[_num_floor_data];
- Fread(_floor_data, 2, _num_floor_data, f);
-
- // Number of 16bits of mesh data to follow
- Fread(&numMeshData, 4, 1, f);
- printDebug("LoadTR5", "numMeshData = %u", numMeshData);
-
- meshData = new unsigned char[2*numMeshData];
- Fread(meshData, 2, numMeshData, f);
-
- // Use pointers array to index in meshData array for tr5_mesh_t's
- Fread(&numMeshPointers, 4, 1, f);
- printDebug("LoadTR5", "numMeshPointers = %u", numMeshPointers);
- meshPointers = new u_int32_t[numMeshPointers];
- Fread(meshPointers, 4, numMeshPointers, f);
-
- Fread(&numAnimationsTR5, 4, 1, f);
- printDebug("LoadTR5", "numAnimationsTR5 = %u", numAnimationsTR5);
- animationsTR5 = new tr5_animation_t[numAnimationsTR5];
- Fread(animationsTR5, 40, numAnimationsTR5, f);
-
- Fread(&u, 4, 1, f);
- _num_state_changes = u;
- printDebug("LoadTR5", "_num_state_changes = %u", _num_state_changes);
- _state_changes = new tr2_state_change_t[_num_state_changes];
- Fread(_state_changes, 6, _num_state_changes, f);
-
- Fread(&u, 4, 1, f);
- _num_anim_dispatches = u;
- printDebug("LoadTR5", "_num_anim_dispatches = %u", _num_anim_dispatches);
- _anim_dispatches = new tr2_anim_dispatch_t[_num_anim_dispatches];
- Fread(_anim_dispatches, 8, _num_anim_dispatches, f);
-
- Fread(&u, 4, 1, f);
- _num_anim_commands = u;
- printDebug("LoadTR5", "_num_anim_commands = %u", _num_anim_commands);
- _anim_commands = new tr2_anim_command_t[_num_anim_commands];
- Fread(_anim_commands, 2, _num_anim_commands, f);
-
- Fread(&u, 4, 1, f);
- _num_mesh_trees = u;
- printDebug("LoadTR5", "_num_mesh_trees = %u", _num_mesh_trees);
- _mesh_trees = new tr2_meshtree_t[_num_mesh_trees];
- Fread(_mesh_trees, 4, _num_mesh_trees, f);
-
- Fread(&u, 4, 1, f);
- _num_frames = u;
- printDebug("LoadTR5", "_num_frames = %u", _num_frames);
- _frames = new u_int16_t[_num_frames];
- Fread(_frames, 2, _num_frames, f);
-
- Fread(&numMoveablesTR5, 4, 1, f);
- printDebug("LoadTR5", "numMoveablesTR5 = %u", numMoveablesTR5);
- moveablesTR5 = new tr5_moveable_t[numMoveablesTR5];
- Fread(moveablesTR5, 20, numMoveablesTR5, f);
-
- Fread(&u, 4, 1, f);
- _num_static_meshes = u;
- printDebug("LoadTR5", "_num_static_meshes = %u", _num_static_meshes);
- _static_meshes = new tr2_staticmesh_t[_num_static_meshes];
- Fread(_static_meshes, 32, _num_static_meshes, f);
-
- Fread(check, 4, 1, f);
- printDebug("LoadTR5", "Check: SPR = '%c%c%c'?",
- check[0], check[1], check[2]);
-
- if (check[0] != 'S' || check[1] != 'P' || check[2] != 'R')
- {
- print("LoadTR5", "Error: SPR != '%c%c%c'", check[0], check[1], check[2]);
- return -4;
- }
-
- Fread(&u, 4, 1, f);
- _num_sprite_textures = u;
- printDebug("LoadTR5", "_num_sprite_textures = %u", _num_sprite_textures);
- _sprite_textures = new tr2_sprite_texture_t[_num_sprite_textures];
- Fread(_sprite_textures, 16, _num_sprite_textures, f);
-
- Fread(&u, 4, 1, f);
- _num_sprite_sequences = u;
- printDebug("LoadTR5", "_num_sprite_sequences = %u", _num_sprite_sequences);
- _sprite_sequences = new tr2_sprite_sequence_t[_num_sprite_sequences];
- Fread(_sprite_sequences, 8, _num_sprite_sequences, f);
-
- Fread(&u, 4, 1, f);
- _num_cameras = u;
- printDebug("LoadTR5", "_num_cameras = %u", _num_cameras);
-
- if (_num_cameras > 0)
- {
- _cameras = new tr2_camera_t[_num_cameras];
- Fread(_cameras, 16, _num_cameras, f);
- }
- else
- {
- _cameras = 0x0;
- }
-
- Fread(&numFlyByCamerasTR5, 4, 1, f);
- printDebug("LoadTR5", "numFlyByCameras = %u", numFlyByCamerasTR5);
-
- if (numFlyByCamerasTR5 > 0)
- {
- flyByCamerasTR5 = new tr5_flyby_camera_t[numFlyByCamerasTR5];
- Fread(flyByCamerasTR5, 40, numFlyByCamerasTR5, f);
- }
- else
- {
- flyByCamerasTR5 = 0x0;
- }
-
- Fread(&u, 4, 1, f);
- _num_sound_sources = u;
- printDebug("LoadTR5", "_num_sound_sources = %u", _num_sound_sources);
-
- if (_num_sound_sources > 0)
- {
- _sound_sources = new tr2_sound_source_t[_num_sound_sources];
- Fread(_sound_sources, 16, _num_sound_sources, f);
- }
- else
- {
- _sound_sources = 0x0;
- }
-
- Fread(&u, 4, 1, f);
- _num_boxes = u;
- printDebug("LoadTR5", "_num_boxes = %u", _num_boxes);
- _boxes = new tr2_box_t[_num_boxes];
- Fread(_boxes, 8, _num_boxes, f);
-
- Fread(&u, 4, 1, f);
- _num_overlaps = u;
- printDebug("LoadTR5", "_num_overlaps = %u", _num_overlaps);
- _overlaps = new short[_num_overlaps];
- Fread(_overlaps, 2, _num_overlaps, f);
-
- _zones = new short[_num_boxes*10];
- Fread(_zones, 20, _num_boxes, f);
-
- Fread(&u, 4, 1, f);
- _num_animated_textures = u;
- printDebug("LoadTR5", "_num_animated_textures = %u", _num_animated_textures);
- _animated_textures = new short[_num_animated_textures];
- Fread(_animated_textures, 2, _num_animated_textures, f);
-
- Fread(check, 1, 5, f);
- printDebug("LoadTR5", "Check: TEX = '%c%c%c'?",
- check[1], check[2], check[3]);
-
- // check[0] is '^D'
- if (check[1] != 'T' || check[2] != 'E' || check[3] != 'X')
- {
- print("LoadTR5", "Error: TEX != '%c%c%c' @ %lu",
- check[1], check[2], check[3], ftell(f));
- return -4;
- }
-
- Fread(&numObjectTexturesTR5, 4, 1, f);
- printDebug("LoadTR5", "numObjectTextures = %u", numObjectTexturesTR5);
- objectTexturesTR5 = new tr5_object_texture_t[numObjectTexturesTR5];
- Fread(objectTexturesTR5, 40, numObjectTexturesTR5, f);
-
- Fread(&u, 4, 1, f);
- _num_items = u;
- printDebug("LoadTR5", "_num_items = %u", _num_items);
- _items = new tr2_item_t[_num_items];
- Fread(_items, 24, _num_items, f);
-
- Fread(&numCinematicFramesTR5, 4, 1, f);
- printDebug("LoadTR5", "numCinematicFrames = %u", numCinematicFramesTR5);
-
- if (numCinematicFramesTR5 > 0)
- {
- cinematicFramesTR5 = new tr5_cinematic_frame_t[numCinematicFramesTR5];
- Fread(cinematicFramesTR5, 24, numCinematicFramesTR5, f);
- }
- else
- {
- cinematicFramesTR5 = 0x0;
- }
-
- Fread(&us, 2, 1, f);
- _num_demo_data = us; // Could overflow? not sure
- printDebug("LoadTR5", "_num_demo_data = %u", _num_demo_data);
-
- if (_num_demo_data > 0)
- {
- _demo_data = new unsigned char[_num_demo_data];
- Fread(_demo_data, 1, _num_demo_data, f);
- }
- else
- {
- _demo_data = 0x0;
- }
-
- printDebug("LoadTR5", "Reading soundMap");
- mSoundMap = new short[450];
- Fread(mSoundMap, 900, 1, f);
-
- Fread(&u, 4, 1, f);
- mNumSoundDetails = u;
- printDebug("LoadTR5", "numSoundDetails = %u", mNumSoundDetails);
- mSoundDetails = new tr2_sound_details_t[mNumSoundDetails];
- Fread(mSoundDetails, 8, mNumSoundDetails, f);
-
- Fread(&u, 4, 1, f);
- mNumSampleIndices = u;
- printDebug("LoadTR5", "numSampleIndices = %u", mNumSampleIndices);
- mSampleIndicesTR5 = new unsigned int[mNumSampleIndices];
- Fread(mSampleIndicesTR5, 4, mNumSampleIndices, f);
-
- Fread(&u, 4, 1, f);
- printDebug("LoadTR5", "Check 0xCDCDCDCD = 0x%X?", u);
-
- if (u != 0xcdcdcdcd)
- {
- print("LoadTR5", "Check 0xCDCDCDCD != 0x%X @ %ld", u, ftell(f));
- return -5;
- }
-
- // Skip over the extra short in the demo.trc, but if it's not there
- // seek back
- u = ftell(f);
- Fread(&us, 2, 1, f);
-
- if (us != 0xcdcd)
- {
- fseek(f, u, SEEK_SET);
- }
-
- //! \fixme (Endian) Read bitu32 / u_int32_t
- Fread(&mNumTR4Samples, 4, 1, f);
- printDebug("Load", "mNumTR4Samples = %i", mNumTR4Samples);
-
- mRiffDataSz = 0;
- mTR4Samples = new unsigned char *[mNumTR4Samples];
- mTR4SamplesSz = new unsigned int[mNumTR4Samples];
-
- memset(mTR4SamplesSz, 0, mNumTR4Samples*4);
-
- for (i = 0; i < (int)mNumTR4Samples; ++i)
- {
- unsigned int sizeCompressed;
- unsigned int sizeUncompressed;
- unsigned char *compressedSoundSample;
- unsigned char *unCompressedSoundSample;
- int zErr;
- uLongf libzUncompressedSize;
-
-
- Fread(&sizeUncompressed, 4, 1, f);
- printDebug("Load", " sizeUncompressed = %i", sizeUncompressed);
- Fread(&sizeCompressed, 4, 1, f);
- printDebug("Load", " sizeCompressed = %i", sizeCompressed);
-
- compressedSoundSample = new unsigned char[sizeCompressed];
- unCompressedSoundSample = new unsigned char[sizeUncompressed];
-
- //printDebug("Load", " %lubytes read from file", ftell(f));
- Fread(compressedSoundSample, sizeCompressed, 1, f);
-
- printDebug("Load", " %c%c%c%c should be RIFF",
- compressedSoundSample[0],
- compressedSoundSample[1],
- compressedSoundSample[2],
- compressedSoundSample[3]);
-
- // Decompress the sample
- libzUncompressedSize = sizeUncompressed;
-
- zErr = uncompress(unCompressedSoundSample,
- &libzUncompressedSize,
- compressedSoundSample,
- sizeCompressed);
-
- sizeUncompressed = libzUncompressedSize;
-
- switch (zErr)
- {
- case Z_MEM_ERROR:
- printDebug("Load", " Decompress Error: not enough memory");
- break;
- case Z_BUF_ERROR:
- printDebug("Load", " Decompress Error: output buffer too small");
- break;
- case Z_DATA_ERROR:
- printDebug("Load", " Decompress Error: input data was corrupted");
- break;
- case Z_OK:
- printDebug("Load", " Decompress OK");
- break;
- default:
- printDebug("Load", " Decompress Error: decompress error #%i", zErr);
- }
-
- // Hhhmm... handle uncompressed RIFFs too?
- if (zErr == Z_OK)
- {
- mTR4Samples[i] = unCompressedSoundSample;
- mTR4SamplesSz[i] = sizeUncompressed;
- delete [] compressedSoundSample;
- }
- else
- {
- printDebug("Load", " %lubytes read from file", ftell(f));
- mTR4Samples[i] = compressedSoundSample;
- mTR4SamplesSz[i] = sizeCompressed;
- delete [] unCompressedSoundSample;
- }
- }
-
- fclose(f);
-
- return 0;
- }
-
- void TombRaider::print(const char *methodName, const char *s, ...)
- {
- va_list args;
-
- va_start(args, s);
- fprintf(stderr, "TombRaider::%s> ", methodName);
- vfprintf(stderr, s, args);
- fprintf(stderr, "\n");
- va_end(args);
- }
-
-
- void TombRaider::printDebug(const char *methodName, const char *s, ...)
- {
- va_list args;
-
- if (!mDebug)
- return;
-
- va_start(args, s);
- fprintf(stdout, "TombRaider::%s> ", methodName);
- vfprintf(stdout, s, args);
- fprintf(stdout, "\n");
- va_end(args);
- }
-
-
-
- ////////////////////////////////////////////////////////////
- // Private Mutators
- ////////////////////////////////////////////////////////////
|