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

TombRaider.cpp 188KB

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