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

TombRaider.cpp 170KB

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