Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TombRaider.cpp 189KB

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