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

OpenRaider.cpp 98KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611
  1. /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
  2. /*================================================================
  3. *
  4. * Project : OpenRaider
  5. * Author : Mongoose
  6. * Website : http://www.westga.edu/~stu7440/
  7. * Email : stu7440@westga.edu
  8. * Object : OpenRaider
  9. * License : No use w/o permission (C) 2001 Mongoose
  10. * Comments: This is the main class for OpenRaider
  11. *
  12. *
  13. * This file was generated using Mongoose's C++
  14. * template generator script. <stu7440@westga.edu>
  15. *
  16. *-- History -------------------------------------------------
  17. *
  18. * 2001.05.21:
  19. * Mongoose - Created
  20. =================================================================*/
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <stdarg.h>
  25. #include <unistd.h>
  26. #include <math.h>
  27. #include <sys/types.h>
  28. #ifdef DEBUG_MEMORY
  29. #include <memory_test.h>
  30. #endif
  31. #include <World.h>
  32. #include <SkeletalModel.h>
  33. #include <TombRaider1.h> // tmp stop-gap
  34. #include <OpenRaider.h>
  35. enum OpenRaiderText { textConsole = 2, textMenu = 3, textOutput = 4 };
  36. const unsigned int TextureLimit = 24;
  37. const float TexelScale = 256.0f;
  38. World gWorld;
  39. Map<int, int> gMapTex2Bump;
  40. Vector <unsigned int> gColorTextureHACK;
  41. int gTextureOffset;
  42. entity_t *LARA = 0x0;
  43. entity_t *gClientPlayer[32];
  44. unsigned int gNetTicks = 0;
  45. unsigned int gNetLastTicks = 0;
  46. bool gStartServer = false;
  47. skeletal_model_t *gLaraModel = 0x0;
  48. char *gFontFilename = 0x0;
  49. unsigned int getTicks()
  50. {
  51. OpenRaider *game = OpenRaider::Instance();
  52. return game->getTicks();
  53. }
  54. ////////////////////////////////////////////////////////////
  55. // Constructors
  56. ////////////////////////////////////////////////////////////
  57. OpenRaider *OpenRaider::mInstance = 0x0;
  58. OpenRaider *OpenRaider::Instance()
  59. {
  60. if (mInstance == 0x0)
  61. {
  62. mInstance = new OpenRaider();
  63. }
  64. return mInstance;
  65. }
  66. void killOpenRaiderSingleton()
  67. {
  68. killNetworkSingleton();
  69. printf("Shutting down Game...\n");
  70. // Requires public deconstructor
  71. // Causes pointer-being-freed-not-allocated error!
  72. //delete OpenRaider::Instance();
  73. #warning "Can't delete OpenRaider::Instance() without a not-allocated-free error. Something is fishy here..."
  74. #ifdef DEBUG_MEMORY
  75. printf("\n[Mongoose MEMORY_DEBUG]\nMemory leak table:\n");
  76. dump_memory_report();
  77. #endif
  78. printf("\nThanks for testing %s\n", VERSION);
  79. printf("Please file bug reports\n\n");
  80. printf("Build date: %s @ %s\n", __DATE__, __TIME__);
  81. printf("Build host: %s\n", BUILD_HOST);
  82. printf("Web site : http://github.com/xythobuz/OpenRaider\n");
  83. }
  84. OpenRaider::OpenRaider() : SDLSystem()
  85. {
  86. Network::Instance();
  87. for (unsigned int i = 0; i < 32; ++i)
  88. {
  89. gClientPlayer[i] = 0x0;
  90. }
  91. m_pakDir = 0x0;
  92. m_audioDir = 0x0;
  93. m_homeDir = 0x0;
  94. m_mouseX = 2.5;
  95. m_mouseY = 0.5;
  96. m_texOffset = 0;
  97. mLevelTextureOffset = 0;
  98. m_testSFX = -1;
  99. mNoClipping = 0;
  100. /*! \todo Replace numbers with enum modes.
  101. * Only do this when you know the amount of commands + 1 (0 reserved)
  102. */
  103. mMode[addCommandMode("[OpenRaider.Engine]")] = 2;
  104. mMode[addCommandMode("[Network.Server]")] = 5;
  105. mMode[addCommandMode("[Video.OpenGL]")] = 0;
  106. mMode[addCommandMode("[Audio.OpenAL]")] = 1;
  107. mMode[addCommandMode("[OpenRaider.Console]")] = 4;
  108. mMode[addCommandMode("[Input.Mouse]")] = 3;
  109. }
  110. OpenRaider::~OpenRaider()
  111. {
  112. /*! \fixme GL call to critical section,
  113. * needs mutex really -- Mongoose 2002.01.02
  114. */
  115. m_render.setMode(Render::modeDisabled);
  116. sleep(1);
  117. printf("Removing World...\n");
  118. gWorld.destroy();
  119. printf("Cleaning up...\n");
  120. for (mMapList.start(); mMapList.forward(); mMapList.next())
  121. {
  122. if (mMapList.current())
  123. {
  124. delete [] mMapList.current();
  125. }
  126. }
  127. for (mMusicList.start(); mMusicList.forward(); mMusicList.next())
  128. {
  129. if (mMusicList.current())
  130. {
  131. delete [] mMusicList.current();
  132. }
  133. }
  134. if (m_pakDir)
  135. delete [] m_pakDir;
  136. if (m_audioDir)
  137. delete [] m_audioDir;
  138. if (m_homeDir)
  139. delete [] m_homeDir;
  140. if (gFontFilename)
  141. delete [] gFontFilename;
  142. }
  143. ////////////////////////////////////////////////////////////
  144. // Public Accessors
  145. ////////////////////////////////////////////////////////////
  146. ////////////////////////////////////////////////////////////
  147. // Public Mutators
  148. ////////////////////////////////////////////////////////////
  149. void eventAnimTest(int anim)
  150. {
  151. if (LARA)
  152. {
  153. SkeletalModel *mdl = (SkeletalModel *)LARA->tmpHook;
  154. if (mdl)
  155. {
  156. mdl->setAnimation(anim);
  157. }
  158. }
  159. }
  160. void percent_callback(int p)
  161. {
  162. OpenRaider &game = *OpenRaider::Instance();
  163. switch (p)
  164. {
  165. case 10:
  166. game.print(true, "Level textures loaded");
  167. break;
  168. default:
  169. // %% has problems with OpenRaider::print it seems
  170. game.print(false, "Level pak %i/100 loaded", p);
  171. }
  172. }
  173. void openraider_warning(const char *func_name, const char *warning,
  174. const char *filename, int line)
  175. {
  176. printf("%s> WARNING %s %s:%i\n", func_name, warning, filename, line);
  177. }
  178. void openraider_error(const char *func_name, const char *error,
  179. const char *filename, int line)
  180. {
  181. printf("%s> ERROR %s %s:%i\n", func_name, error, filename, line);
  182. }
  183. void from_network_layer(network_packet_t *p, unsigned int *last_id)
  184. {
  185. static unsigned int i = 0;
  186. *last_id = i++;
  187. if (gNetTicks > gNetLastTicks + 200) // 200ms
  188. {
  189. gNetLastTicks = gNetTicks;
  190. p->send = 1;
  191. }
  192. if (LARA)
  193. {
  194. p->pos[0] = LARA->pos[0];
  195. p->pos[1] = LARA->pos[1];
  196. p->pos[2] = LARA->pos[2];
  197. p->yaw = LARA->angles[1];
  198. p->pitch = LARA->angles[1];
  199. p->view_model = LARA->modelId;
  200. SkeletalModel *mdl = (SkeletalModel *)LARA->tmpHook;
  201. if (mdl)
  202. {
  203. p->aframe = mdl->getAnimation();
  204. p->bframe = mdl->getFrame();
  205. }
  206. }
  207. #ifdef DEBUG_NETWORK
  208. printf("<> Sending data\n");
  209. #endif
  210. }
  211. void to_network_layer(network_packet_t p)
  212. {
  213. OpenRaider &game = *OpenRaider::Instance();
  214. #ifdef DEBUG_NETWORK
  215. printf("<> Recieved data\n");
  216. #endif
  217. if (gClientPlayer[p.cid])
  218. {
  219. gClientPlayer[p.cid]->pos[0] = p.pos[0];
  220. gClientPlayer[p.cid]->pos[1] = p.pos[1];
  221. gClientPlayer[p.cid]->pos[2] = p.pos[2];
  222. gClientPlayer[p.cid]->angles[1] = p.yaw;
  223. gClientPlayer[p.cid]->angles[2] = p.pitch;
  224. }
  225. else if (LARA)
  226. {
  227. gClientPlayer[p.cid] = new entity_t;
  228. gClientPlayer[p.cid]->pos[0] = p.pos[0];
  229. gClientPlayer[p.cid]->pos[1] = p.pos[1];
  230. gClientPlayer[p.cid]->pos[2] = p.pos[2];
  231. gClientPlayer[p.cid]->angles[1] = p.yaw;
  232. gClientPlayer[p.cid]->angles[2] = p.pitch;
  233. gClientPlayer[p.cid]->moving = true;
  234. gClientPlayer[p.cid]->objectId = 0;
  235. gClientPlayer[p.cid]->id = LARA->id+1000+p.cid;
  236. gClientPlayer[p.cid]->type = 0x02;
  237. SkeletalModel *mdl = new SkeletalModel();
  238. gClientPlayer[p.cid]->tmpHook = mdl;
  239. mdl->setAnimation(p.aframe);
  240. mdl->setFrame(p.bframe);
  241. mdl->setIdleAnimation(TR_ANIAMTION_STAND);
  242. gClientPlayer[p.cid]->modelId = LARA->modelId; //p.view_model;
  243. gClientPlayer[p.cid]->animate = true;
  244. gWorld.addEntity(gClientPlayer[p.cid]);
  245. game.print(true, "A new player (%u) connected", p.cid);
  246. }
  247. if (LARA)
  248. {
  249. Network &network = *Network::Instance();
  250. network_frame_t &gPiggyBack = network.getPiggyBack();
  251. if (gNetTicks > gNetLastTicks + 200)
  252. {
  253. gNetLastTicks = gNetTicks;
  254. gPiggyBack.data.send = 1;
  255. }
  256. gPiggyBack.data.pos[0] = LARA->pos[0];
  257. gPiggyBack.data.pos[1] = LARA->pos[1];
  258. gPiggyBack.data.pos[2] = LARA->pos[2];
  259. gPiggyBack.data.yaw = LARA->angles[1];
  260. gPiggyBack.data.pitch = LARA->angles[2];
  261. gPiggyBack.data.view_model = LARA->modelId;
  262. SkeletalModel *mdl = (SkeletalModel *)LARA->tmpHook;
  263. if (mdl)
  264. {
  265. gPiggyBack.data.aframe = mdl->getAnimation();
  266. gPiggyBack.data.bframe = mdl->getFrame();
  267. }
  268. }
  269. }
  270. void OpenRaider::handleMouseMotionEvent(float x, float y)
  271. {
  272. if (x > 0)
  273. {
  274. while (x-- > 0) // Tried to fix very slow camera movements
  275. m_camera.command(CAMERA_ROTATE_RIGHT);
  276. if (LARA)
  277. {
  278. switch (LARA->moveType)
  279. {
  280. case worldMoveType_swim:
  281. case worldMoveType_fly:
  282. case worldMoveType_noClipping:
  283. eventAnimTest(TR_ANIAMTION_SWIM_R);
  284. break;
  285. default:
  286. case worldMoveType_walk:
  287. case worldMoveType_walkNoSwim:
  288. eventAnimTest(TR_ANIAMTION_TURN_L); // TURN left always for now
  289. }
  290. LARA->moving = true;
  291. }
  292. }
  293. else if (x < 0)
  294. {
  295. while (x++ < 0) // Tried to fix very slow camera movements
  296. m_camera.command(CAMERA_ROTATE_LEFT);
  297. if (LARA)
  298. {
  299. switch (LARA->moveType)
  300. {
  301. case worldMoveType_swim:
  302. case worldMoveType_fly:
  303. case worldMoveType_noClipping:
  304. eventAnimTest(TR_ANIAMTION_SWIM_L);
  305. break;
  306. default:
  307. case worldMoveType_walk:
  308. case worldMoveType_walkNoSwim:
  309. eventAnimTest(TR_ANIAMTION_TURN_L);
  310. }
  311. LARA->moving = true;
  312. }
  313. }
  314. if (y > 0)
  315. while (y-- > 0) // Tried to fix very slow camera movements
  316. m_camera.command(CAMERA_ROTATE_UP);
  317. else if (y < 0)
  318. while (y++ < 0) // Tried to fix very slow camera movements
  319. m_camera.command(CAMERA_ROTATE_DOWN);
  320. if (LARA)
  321. {
  322. LARA->angles[1] = m_camera.getRadianYaw();
  323. LARA->angles[2] = m_camera.getRadianPitch();
  324. }
  325. }
  326. void OpenRaider::handleKeyReleaseEvent(unsigned int key, unsigned int mod)
  327. {
  328. }
  329. void OpenRaider::handleBoundKeyPressEvent(unsigned int key)
  330. {
  331. switch (key)
  332. {
  333. case OpenRaiderKey_jump:
  334. m_camera.command(CAMERA_MOVE_UP);
  335. if (LARA)
  336. {
  337. LARA->moving = true;
  338. }
  339. break;
  340. case OpenRaiderKey_crouch:
  341. m_camera.command(CAMERA_MOVE_DOWN);
  342. if (LARA)
  343. {
  344. LARA->moving = true;
  345. }
  346. break;
  347. case OpenRaiderKey_forward:
  348. gWorld.moveEntity(LARA, 'f');
  349. if (LARA && LARA->moving)
  350. {
  351. switch (LARA->moveType)
  352. {
  353. case worldMoveType_swim:
  354. case worldMoveType_fly:
  355. case worldMoveType_noClipping:
  356. eventAnimTest(TR_ANIAMTION_SWIM);
  357. break;
  358. default:
  359. case worldMoveType_walk:
  360. case worldMoveType_walkNoSwim:
  361. eventAnimTest(TR_ANIAMTION_RUN);
  362. }
  363. }
  364. else if (LARA)
  365. {
  366. switch (LARA->moveType)
  367. {
  368. case worldMoveType_swim:
  369. case worldMoveType_fly:
  370. case worldMoveType_noClipping:
  371. eventAnimTest(TR_ANIAMTION_SWIM_IDLE);
  372. break;
  373. default:
  374. case worldMoveType_walk:
  375. case worldMoveType_walkNoSwim:
  376. eventAnimTest(TR_ANIAMTION_HIT_WALL_FRONT);
  377. }
  378. }
  379. else
  380. {
  381. m_camera.command(CAMERA_MOVE_FORWARD);
  382. }
  383. break;
  384. case OpenRaiderKey_backward:
  385. gWorld.moveEntity(LARA, 'b');
  386. if (LARA && LARA->moving)
  387. {
  388. switch (LARA->moveType)
  389. {
  390. case worldMoveType_swim:
  391. case worldMoveType_fly:
  392. case worldMoveType_noClipping:
  393. eventAnimTest(TR_ANIAMTION_SWIM);
  394. break;
  395. default:
  396. case worldMoveType_walk:
  397. case worldMoveType_walkNoSwim:
  398. eventAnimTest(TR_ANIAMTION_RUN);
  399. }
  400. }
  401. break;
  402. case OpenRaiderKey_left:
  403. gWorld.moveEntity(LARA, 'l');
  404. if (LARA && LARA->moving)
  405. {
  406. switch (LARA->moveType)
  407. {
  408. case worldMoveType_swim:
  409. case worldMoveType_fly:
  410. case worldMoveType_noClipping:
  411. eventAnimTest(TR_ANIAMTION_SWIM);
  412. break;
  413. default:
  414. case worldMoveType_walk:
  415. case worldMoveType_walkNoSwim:
  416. eventAnimTest(TR_ANIAMTION_RUN);
  417. }
  418. }
  419. else if (!LARA)
  420. {
  421. m_camera.command(CAMERA_MOVE_LEFT);
  422. }
  423. break;
  424. case OpenRaiderKey_right:
  425. gWorld.moveEntity(LARA, 'r');
  426. if (LARA && LARA->moving)
  427. {
  428. switch (LARA->moveType)
  429. {
  430. case worldMoveType_swim:
  431. case worldMoveType_fly:
  432. case worldMoveType_noClipping:
  433. eventAnimTest(TR_ANIAMTION_SWIM);
  434. break;
  435. default:
  436. case worldMoveType_walk:
  437. case worldMoveType_walkNoSwim:
  438. eventAnimTest(TR_ANIAMTION_RUN);
  439. }
  440. }
  441. else if (!LARA)
  442. {
  443. m_camera.command(CAMERA_MOVE_RIGHT);
  444. }
  445. break;
  446. case OpenRaiderKey_panRight:
  447. m_camera.command(CAMERA_ROTATE_RIGHT);
  448. break;
  449. case OpenRaiderKey_panLeft:
  450. m_camera.command(CAMERA_ROTATE_LEFT);
  451. break;
  452. case OpenRaiderKey_tiltDown:
  453. m_camera.command(CAMERA_ROTATE_DOWN);
  454. break;
  455. case OpenRaiderKey_tiltUp:
  456. m_camera.command(CAMERA_ROTATE_UP);
  457. break;
  458. }
  459. }
  460. void OpenRaider::handleBoundKeyReleaseEvent(unsigned int key)
  461. {
  462. switch (key)
  463. {
  464. case '`': // Temp hack also until console is rolled back
  465. //i = 2;
  466. if (mConsoleMode)
  467. {
  468. //buffer[0] = '>';
  469. //buffer[1] = ' ';
  470. //buffer[2] = 0;
  471. mText->setActive(textConsole, true);
  472. }
  473. else
  474. {
  475. mText->setActive(textConsole, false);
  476. //buffer[0] = 0;
  477. }
  478. break;
  479. case OpenRaiderKey_forward:
  480. case OpenRaiderKey_backward:
  481. case OpenRaiderKey_right:
  482. case OpenRaiderKey_left:
  483. if (LARA)
  484. {
  485. LARA->moving = false;
  486. }
  487. }
  488. }
  489. void OpenRaider::handleConsoleKeyPressEvent(unsigned int key,unsigned int mod)
  490. {
  491. static char lastCmd[64] = "2 ";
  492. static unsigned int i = 0;
  493. char *buffer;
  494. unsigned int len;
  495. // Cheap console test
  496. len = mText->GetStringLen(textConsole);
  497. buffer = mText->GetBuffer(textConsole);
  498. //printf("- 0x%x\n", key);
  499. if (key == mConsoleKey)
  500. {
  501. i = 2;
  502. if (mConsoleMode)
  503. {
  504. buffer[0] = '>';
  505. buffer[1] = ' ';
  506. buffer[2] = 0;
  507. mText->setActive(textConsole, true);
  508. }
  509. else
  510. {
  511. mText->setActive(textConsole, false);
  512. buffer[0] = 0;
  513. }
  514. }
  515. else if (mConsoleMode && len > i+1)
  516. {
  517. switch (key)
  518. {
  519. case SYS_KEY_UP:
  520. i = lastCmd[0];
  521. lastCmd[0] = '>';
  522. mText->SetString(textConsole, lastCmd);
  523. break;
  524. case 0:
  525. break;
  526. case '\b':
  527. if (i > 2)
  528. {
  529. buffer[--i] = 0;
  530. }
  531. break;
  532. case '\r':
  533. case '\n':
  534. strncpy(lastCmd, buffer, 63);
  535. lastCmd[0] = i;
  536. command("[OpenRaider.Console]");
  537. command(buffer);
  538. i = 2;
  539. buffer[0] = '>';
  540. buffer[1] = ' ';
  541. buffer[2] = 0;
  542. break;
  543. default:
  544. if (mod & (SYS_MOD_KEY_RSHIFT | SYS_MOD_KEY_LSHIFT) &&
  545. key > 96 && key < 122)
  546. {
  547. buffer[i++] = (key - 32);
  548. }
  549. else
  550. {
  551. if (mod & (SYS_MOD_KEY_RSHIFT | SYS_MOD_KEY_LSHIFT))
  552. {
  553. if (key == '-')
  554. key = '_';
  555. }
  556. buffer[i++] = key;
  557. }
  558. buffer[i] = 0;
  559. }
  560. }
  561. }
  562. void OpenRaider::handleKeyPressEvent(unsigned int key, unsigned int mod)
  563. {
  564. static bool menu = false;
  565. // Cheap menu test
  566. if (menu)
  567. {
  568. switch (key)
  569. {
  570. case SYS_KEY_ESC:
  571. menu = false;
  572. mText->SetString(textMenu, " ");
  573. mText->setActive(textMenu, false);
  574. break;
  575. case SYS_KEY_ENTER:
  576. loadLevel(mMapList.current());
  577. mText->SetString(textMenu, " ");
  578. mText->setActive(textMenu, false);
  579. menu = false;
  580. break;
  581. case SYS_KEY_DOWN:
  582. mMapList.prev();
  583. if (!mMapList.backward())
  584. {
  585. mMapList.finish();
  586. }
  587. if (mMapList.current())
  588. {
  589. mText->SetString(textMenu, "Load %s?", mMapList.current());
  590. }
  591. else
  592. {
  593. mText->SetString(textMenu, "See README for map install");
  594. }
  595. break;
  596. case SYS_KEY_UP:
  597. mMapList.next();
  598. if (!mMapList.forward())
  599. {
  600. mMapList.start();
  601. }
  602. if (mMapList.current())
  603. {
  604. mText->SetString(textMenu, "Load %s?", mMapList.current());
  605. }
  606. else
  607. {
  608. mText->SetString(textMenu, "See README for map install");
  609. }
  610. }
  611. return;
  612. }
  613. switch (key)
  614. {
  615. case SYS_KEY_ESC:
  616. menu = true;
  617. mText->SetString(textMenu, "Select a map UP/DOWN");
  618. mText->setActive(textMenu, true);
  619. break;
  620. case SYS_KEY_ENTER:
  621. if (mod & SYS_MOD_KEY_LALT)
  622. {
  623. toggleFullscreen();
  624. }
  625. break;
  626. }
  627. ////////////////////////////////////////////////////////////
  628. switch (key)
  629. {
  630. case SYS_MOUSE_LEFT:
  631. mSound.play(m_testSFX);
  632. if (LARA)
  633. {
  634. eventAnimTest(15);
  635. }
  636. break;
  637. case '/':
  638. if (LARA)
  639. {
  640. entity_t *e = new entity_t;
  641. e->pos[0] = LARA->pos[0];
  642. e->pos[1] = LARA->pos[1] - 256.0f; // crawling H
  643. e->pos[2] = LARA->pos[2];
  644. e->type = 61;
  645. e->master = LARA->master;
  646. LARA->master = e;
  647. }
  648. break;
  649. case '[':
  650. if (LARA)
  651. {
  652. SkeletalModel *mdl = (SkeletalModel *)LARA->tmpHook;
  653. if (mdl)
  654. {
  655. mdl->setIdleAnimation(mdl->getIdleAnimation() - 1);
  656. mdl->setAnimation(mdl->getIdleAnimation());
  657. print(false, "AFRAME %i\n", mdl->getIdleAnimation());
  658. }
  659. }
  660. break;
  661. case ']':
  662. if (LARA)
  663. {
  664. SkeletalModel *mdl = (SkeletalModel *)LARA->tmpHook;
  665. if (mdl)
  666. {
  667. mdl->setIdleAnimation(mdl->getIdleAnimation() + 1);
  668. mdl->setAnimation(mdl->getIdleAnimation());
  669. print(false, "AFRAME %i\n", mdl->getIdleAnimation());
  670. }
  671. }
  672. break;
  673. case 'r':
  674. mSound.play(m_testSFX);
  675. break;
  676. }
  677. }
  678. void OpenRaider::print(bool dump_stdout, const char *format, ...)
  679. {
  680. static char buffer[128];
  681. va_list args;
  682. unsigned int l;
  683. va_start(args, format);
  684. vsnprintf(buffer, 128, format, args);
  685. l = strlen(buffer);
  686. if (!l || !buffer[0])
  687. {
  688. return;
  689. }
  690. // Strip message of an trailing carrage return
  691. if (buffer[l-1] == '\n')
  692. {
  693. buffer[l-1] = 0;
  694. }
  695. // Print to game 'console'
  696. if (mText)
  697. {
  698. mText->SetString(textOutput, buffer);
  699. }
  700. // Print to stdout
  701. if (dump_stdout)
  702. {
  703. fprintf(stdout, "> %s\n", buffer);
  704. }
  705. va_end(args);
  706. // Mongoose 2002.08.14, Currently, you must request
  707. // frame redraws in this mode
  708. if (m_flags & OpenRaider_Loading)
  709. {
  710. gameFrame();
  711. }
  712. }
  713. void OpenRaider::initGL()
  714. {
  715. // Mongoose 2002.08.13, Handled in start() actually
  716. }
  717. void OpenRaider::start()
  718. {
  719. vec3_t up;
  720. char *filename;
  721. printf("\n[Starting %s]\n\n", VERSION);
  722. filename = fullPath("~/.OpenRaider/OpenRaider.init", 0);
  723. if (loadResourceFile(filename) < 0)
  724. {
  725. printf("Could not find ~/.OpenRaider/OpenRaider.init\n");
  726. if (loadResourceFile("OpenRaider.init") < 0)
  727. {
  728. shutdown(-1);
  729. }
  730. printf("Loaded defaults...\n");
  731. }
  732. delete [] filename;
  733. m_camera.reset();
  734. m_camera.setSensitivityX(m_mouseX);
  735. m_camera.setSensitivityY(m_mouseY);
  736. m_camera.setSpeed(512);
  737. up[0] = 0.0;
  738. up[1] = -1.0;
  739. up[2] = 0.0;
  740. m_camera.setUp(up);
  741. if (mMapList.empty())
  742. {
  743. printf("No maps in map list, please edit your OpenRaider.init\n");
  744. shutdown(0);
  745. }
  746. // Mongoose 2002.08.13, System protected members abuse
  747. m_fovY = 40.0f;
  748. m_clipFar = 600000.0f;
  749. m_clipNear = 10.0f;
  750. // Mongoose 2002.08.13, This starts GL context
  751. printf("\n[Starting video subsystem...]\n");
  752. setFastCardPerformance(true);
  753. initVideo(m_width, m_height, false);
  754. // Mongoose 2002.01.02, Disable rendering to avoid GL call conflicts
  755. m_render.setMode(Render::modeDisabled);
  756. m_render.Init(m_width, m_height, m_fastCard);
  757. m_render.initTextures(m_homeDir, &m_texOffset, &mLevelTextureOffset);
  758. m_render.RegisterCamera(&m_camera);
  759. mText = m_render.GetString();
  760. // Mongoose 2002.01.03, Good idea?
  761. m_render.setMode(Render::modeLoadScreen);
  762. // Mongoose 2002.01.02, Load external audio tracks and fx
  763. // General audio init
  764. initSound();
  765. // Old room movement
  766. gWorld.setFlag(World::fEnableHopping);
  767. // reenabled, what should be the new room movement? --xythobuz
  768. resetTicks();
  769. runGame();
  770. }
  771. void OpenRaider::initSound()
  772. {
  773. //char filename[128];
  774. //unsigned int len;
  775. //int id;
  776. if (!(m_flags & OpenRaider_EnableSound) || mSound.init() < 0)
  777. {
  778. print(true, "Disabling sound...\n");
  779. m_flags |= OpenRaider_EnableSound;
  780. m_flags ^= OpenRaider_EnableSound;
  781. return;
  782. }
  783. #ifdef OBSOLETE
  784. printf("Processing external sound files: ");
  785. // Mongoose 2001.12.31, Use music list from init script
  786. if (mMusicList.Empty())
  787. {
  788. print(true, "No music in map list, please edit your OpenRaider.init\n");
  789. }
  790. else
  791. {
  792. snprintf(filename, 126, "%s%s", m_audioDir, mMusicList[0]);
  793. filename[127] = 0;
  794. if (!mSound.addFile(filename, &id, mSound.SoundFlagsLoop))
  795. {
  796. mSound.play(id);
  797. printf("m");
  798. fflush(stdout);
  799. }
  800. }
  801. //snprintf(filename, 126, "%s%s", m_homeDir, "sample.wav");
  802. //filename[127] = 0;
  803. //mSound.addFile(filename, &m_testSFX, mSound.SoundFlagsNone);
  804. printf(".");
  805. fflush(stdout);
  806. printf("\n");
  807. #endif
  808. }
  809. void OpenRaider::processPakSounds()
  810. {
  811. unsigned char *riff;
  812. unsigned int riffSz;
  813. //tr2_sound_source_t *sound;
  814. //tr2_sound_details_t *detail;
  815. //float pos[3];
  816. unsigned int i;
  817. int id;
  818. /* detail
  819. short sample;
  820. short volume;
  821. short sound_range;
  822. short flags; // bits 8-15: priority?, 2-7: number of sound samples
  823. // in this group, bits 0-1: channel number
  824. */
  825. if (m_flags & OpenRaider_EnableSound)
  826. {
  827. printf("Processing pak sound files: ");
  828. }
  829. else
  830. {
  831. return;
  832. }
  833. for (i = 0; i < m_tombraider.getSoundSamplesCount(); ++i)
  834. {
  835. print(false, "Processing pak sounds: %i / %i",
  836. i, m_tombraider.getSoundSamplesCount());
  837. m_tombraider.getSoundSample(i, &riffSz, &riff);
  838. mSound.addWave(riff, riffSz, &id, mSound.SoundFlagsNone);
  839. if (id == TR_SOUND_F_PISTOL && id > 0)
  840. {
  841. m_testSFX = id;
  842. }
  843. delete [] riff;
  844. // sound[i].sound_id; // internal sound index
  845. // sound[i].flags; // 0x40, 0x80, or 0xc0
  846. //pos[0] = sound[i].x;
  847. //pos[1] = sound[i].y;
  848. //pos[2] = sound[i].z;
  849. //mSound.SourceAt(id, pos);
  850. printf(".");
  851. fflush(stdout);
  852. }
  853. printf("\n");
  854. }
  855. void OpenRaider::loadLevel(char *mapname)
  856. {
  857. int i, j, len;
  858. char filename[256];
  859. // Mongoose 2002.03.23, Clear last loaded 'world'
  860. m_render.setMode(Render::modeDisabled);
  861. m_render.ClearWorld();
  862. gWorld.destroy();
  863. // Mongoose 2002.01.02, Now draw load screen, since external
  864. // GL textures are loaded and renderer is initialized
  865. m_render.setMode(Render::modeLoadScreen);
  866. gameFrame();
  867. /* 2003.07.05, Mongoose - No longer seeding, and looking for
  868. map to load -- load this map or fail and return */
  869. snprintf(filename, 254, "%s%s", m_pakDir, mapname);
  870. filename[255] = 0;
  871. len = strlen(filename);
  872. for (i = len, j = 0; i > 0; --i, ++j)
  873. {
  874. if (filename[i] == '/' || filename[i] == '\\')
  875. break;
  876. }
  877. j--;
  878. memset(m_mapName, 0, 32);
  879. for (i = 0; i < len - j && i < 30; ++i)
  880. {
  881. m_mapName[i] = filename[i + len - j];
  882. }
  883. m_tombraider.setDebug(m_flags & OpenRaider_DebugMap);
  884. m_flags |= OpenRaider_Loading;
  885. print(true, "Level %s loading", m_mapName);
  886. if (m_tombraider.Load(filename, percent_callback))
  887. {
  888. print(true, "ERROR: Failed to load level %s\n", m_mapName);
  889. return;
  890. }
  891. else
  892. {
  893. char altSfx[256];
  894. switch (m_tombraider.getEngine())
  895. {
  896. case TR_VERSION_2:
  897. case TR_VERSION_3:
  898. // Now loads eg. "tr3/MAIN.SFX" instead of "tr3/SHORE.TR2.sfx"
  899. filename[len - j] = '\0';
  900. snprintf(altSfx, 255, "%sMAIN.SFX", filename);
  901. print(true, "Loading alternate SFX for TR2 or TR3 pak at %sMAIN.SFX\n", filename);
  902. m_tombraider.loadSFX(altSfx);
  903. break;
  904. default:
  905. case TR_VERSION_1:
  906. case TR_VERSION_4:
  907. case TR_VERSION_5:
  908. case TR_VERSION_UNKNOWN:
  909. ;
  910. }
  911. }
  912. print(true, "Optimizing and translating pak data...");
  913. m_flags ^= OpenRaider_Loading;
  914. // Mongoose 2002.08.16, Used to cache TR4 textures at 10% of file load
  915. // in a callback - then with other engines process them here
  916. m_render.setMode(Render::modeDisabled);
  917. //sleep(1); // Why should we sleep here? -- xythobuz
  918. processTextures();
  919. m_render.setMode(Render::modeLoadScreen);
  920. print(true, "Level textures cached\n");
  921. // Cache/process rooms
  922. printf("Processing rooms: ");
  923. for (i = 0; i < m_tombraider.NumRooms(); ++i)
  924. {
  925. processRoom(i);
  926. print(false, "Processing rooms: %i/%i", i, m_tombraider.NumRooms());
  927. }
  928. printf("\n");
  929. // Cache/process meshes
  930. printf("Processing meshes: ");
  931. for (i = 0; i < m_tombraider.getMeshCount(); ++i)
  932. {
  933. m_render.setMode(Render::modeDisabled);
  934. processModel(i);
  935. m_render.setMode(Render::modeLoadScreen);
  936. print(false, "Processing meshes: %i/%i", i, m_tombraider.getMeshCount());
  937. }
  938. printf("\n");
  939. // Cache/process sprites
  940. processSprites();
  941. // Cache/process skeletal models
  942. processMoveables();
  943. // Cache/process sound fx
  944. processPakSounds();
  945. #ifdef DEBUG_MEMORY
  946. // Right before pak is cleared will be highest use or memory
  947. display_memory_usage();
  948. #endif
  949. // Mongoose 2002.01.02, Go ahead and free the TR pak
  950. m_tombraider.reset();
  951. print(true, "Level pak freed from memory, Starting game...");
  952. #ifdef DEBUG_MEMORY
  953. display_memory_usage();
  954. #endif
  955. /*! \fixme GL call to critical section,
  956. * needs mutex really -- Mongoose 2002.01.02
  957. */
  958. m_render.setMode(Render::modeDisabled);
  959. sleep(1);
  960. // Draw game, level is loaded
  961. m_render.setMode(Render::modeVertexLight);
  962. m_flags ^= OpenRaider_Loading;
  963. }
  964. void OpenRaider::gameFrame()
  965. {
  966. SkeletalModel *mdl = 0x0;
  967. unsigned int ticks;
  968. float time;
  969. // Remember: ticks in milliseconds, time in hundredths
  970. gNetTicks = ticks = getTicks();
  971. time = gNetTicks * 0.1f;
  972. switch (m_render.getMode())
  973. {
  974. case Render::modeDisabled:
  975. break;
  976. case Render::modeLoadScreen:
  977. if ((ticks % 10000) > 1)
  978. {
  979. m_render.drawLoadScreen();
  980. swapBuffersGL();
  981. }
  982. break;
  983. default:
  984. if (LARA)
  985. {
  986. mdl = (SkeletalModel *)LARA->tmpHook;
  987. if (mdl)
  988. {
  989. float elapsed = time - mdl->lastTime;
  990. if (elapsed > mdl->rate)
  991. {
  992. if (mdl->getAnimation() == 0 &&
  993. (mdl->getFrame() == 2 || mdl->getFrame() == 10))
  994. {
  995. if (m_flags & OpenRaider_EnableSound)
  996. {
  997. mSound.play(TR_SOUND_FOOTSTEP0);
  998. }
  999. }
  1000. else if (mdl->getAnimation() == 15)
  1001. {
  1002. eventAnimTest(TR_ANIAMTION_RUN);
  1003. }
  1004. mdl->setFrame(mdl->getFrame()+1);
  1005. mdl->lastTime = elapsed / mdl->rate;
  1006. }
  1007. else
  1008. {
  1009. mdl->lastTime = time;
  1010. }
  1011. }
  1012. }
  1013. m_render.Display();
  1014. swapBuffersGL();
  1015. if (mdl && mdl->getAnimation() == 12)
  1016. {
  1017. switch (LARA->moveType)
  1018. {
  1019. case worldMoveType_swim:
  1020. case worldMoveType_fly:
  1021. case worldMoveType_noClipping:
  1022. eventAnimTest(TR_ANIAMTION_SWIM_IDLE);
  1023. break;
  1024. default:
  1025. case worldMoveType_walk:
  1026. case worldMoveType_walkNoSwim:
  1027. eventAnimTest(TR_ANIAMTION_STAND);
  1028. }
  1029. LARA->moving = false;
  1030. }
  1031. }
  1032. if (m_flags & OpenRaider_ShowFPS)
  1033. {
  1034. static unsigned int frames = 0;
  1035. static float lastTime = 0.0f;
  1036. ++frames;
  1037. // Only update every second, so it can't be affected
  1038. // by fast/slow cards it only counts frames instead
  1039. // of estimating
  1040. if (time - lastTime > 100.0f)
  1041. {
  1042. if (mText)
  1043. mText->SetString(1, "%dFPS", frames);
  1044. lastTime = time;
  1045. frames = 0;
  1046. }
  1047. }
  1048. }
  1049. void OpenRaider::processTextures()
  1050. {
  1051. unsigned char *image;
  1052. unsigned char *bumpmap;
  1053. int i;
  1054. printf("Processing TR textures: ");
  1055. //if ( m_tombraider.getNumBumpMaps())
  1056. // gBumpMapStart = m_tombraider.NumTextures();
  1057. for (i = 0; i < m_tombraider.NumTextures(); ++i)
  1058. {
  1059. print(false, "Processing textures: %i/%i", i, m_tombraider.NumTextures());
  1060. m_tombraider.Texture(i, &image, &bumpmap);
  1061. // Overwrite any previous level textures on load
  1062. m_render.loadTexture(image, 256, 256, mLevelTextureOffset + i);
  1063. gMapTex2Bump.Add(mLevelTextureOffset + i, -1);
  1064. if (bumpmap)
  1065. {
  1066. gMapTex2Bump.Add(mLevelTextureOffset + i, mLevelTextureOffset + i +
  1067. m_tombraider.NumTextures());
  1068. m_render.loadTexture(bumpmap, 256, 256, mLevelTextureOffset + i +
  1069. m_tombraider.NumTextures());
  1070. }
  1071. if (image)
  1072. delete [] image;
  1073. if (bumpmap)
  1074. delete [] bumpmap;
  1075. printf(".");
  1076. fflush(stdout);
  1077. }
  1078. gTextureOffset = mLevelTextureOffset + m_tombraider.NumTextures();
  1079. printf("\n");
  1080. }
  1081. ////////////////////////////////////////////////////////////
  1082. // Private Accessors
  1083. ////////////////////////////////////////////////////////////
  1084. ////////////////////////////////////////////////////////////
  1085. // Private Mutators
  1086. ////////////////////////////////////////////////////////////
  1087. void OpenRaider::soundEvent(int type, int id, vec3_t pos, vec3_t angles)
  1088. {
  1089. switch (type)
  1090. {
  1091. case 0: // Reset listener position
  1092. mSound.listenAt(pos, angles);
  1093. break;
  1094. default:
  1095. mSound.sourceAt(id, pos);
  1096. mSound.play(id);
  1097. }
  1098. }
  1099. void OpenRaider::processSprites()
  1100. {
  1101. int i, j, k, l, x, y, s_index, width, height;
  1102. float scale, width2, height2;
  1103. tr2_sprite_texture_t *sprite;
  1104. tr2_sprite_texture_t *sprite_textures;
  1105. tr2_sprite_sequence_t *sprite_sequence;
  1106. sprite_seq_t *r_mesh;
  1107. tr2_item_t *item;
  1108. item = m_tombraider.Item();
  1109. sprite_textures = m_tombraider.Sprite();
  1110. sprite_sequence = m_tombraider.SpriteSequence();
  1111. scale = 4.0;
  1112. printf("Processing sprites: ");
  1113. for (i = 0; i < m_tombraider.NumItems() - 1; ++i)
  1114. {
  1115. print(false, "Processing sprites in Items: %i/%i",
  1116. i, m_tombraider.NumItems());
  1117. // It's a mesh, skip it
  1118. if (m_tombraider.Engine() == TR_VERSION_1 && item[i].intensity1 == -1)
  1119. continue;
  1120. k = item[i].object_id;
  1121. // Search the SpriteSequence list
  1122. // (if we didn't already decide that it's a mesh)
  1123. for (j = 0; j < (int)m_tombraider.NumSpriteSequences(); ++j)
  1124. {
  1125. if (sprite_sequence[j].object_id == k)
  1126. {
  1127. k = item[i].object_id;
  1128. s_index = sprite_sequence[j].offset;
  1129. r_mesh = new sprite_seq_t;
  1130. gWorld.addSprite(r_mesh);
  1131. r_mesh->num_sprites = -sprite_sequence[j].negative_length;
  1132. r_mesh->sprite = new sprite_t[r_mesh->num_sprites];
  1133. for (l = 0; l < r_mesh->num_sprites; ++l)
  1134. {
  1135. sprite = &sprite_textures[s_index];
  1136. width = sprite->width >> 8;
  1137. height = sprite->height >> 8;
  1138. x = sprite->x;
  1139. y = sprite->y;
  1140. width2 = width * scale;
  1141. height2 = height * scale;
  1142. // For vising use
  1143. r_mesh->sprite[l].pos[0] = item[i].x;
  1144. r_mesh->sprite[l].pos[1] = item[i].y;
  1145. r_mesh->sprite[l].pos[2] = item[i].z;
  1146. r_mesh->sprite[l].texture = sprite->tile + m_texOffset;
  1147. r_mesh->sprite[l].radius = width2 / 2.0;
  1148. r_mesh->sprite[l].vertex[0].pos[0] = -width2 / 2.0;
  1149. r_mesh->sprite[l].vertex[1].pos[0] = -width2 / 2.0;
  1150. r_mesh->sprite[l].vertex[2].pos[0] = width2 / 2.0;
  1151. r_mesh->sprite[l].vertex[3].pos[0] = width2 / 2.0;
  1152. r_mesh->sprite[l].vertex[0].pos[1] = 0;
  1153. r_mesh->sprite[l].vertex[1].pos[1] = -height2;
  1154. r_mesh->sprite[l].vertex[2].pos[1] = -height2;
  1155. r_mesh->sprite[l].vertex[3].pos[1] = 0;
  1156. r_mesh->sprite[l].vertex[0].pos[2] = 0;
  1157. r_mesh->sprite[l].vertex[1].pos[2] = 0;
  1158. r_mesh->sprite[l].vertex[2].pos[2] = 0;
  1159. r_mesh->sprite[l].vertex[3].pos[2] = 0;
  1160. r_mesh->sprite[l].texel[3].st[0] = (double)(x+width)/TexelScale;
  1161. r_mesh->sprite[l].texel[3].st[1] = (double)(y+height)/TexelScale;
  1162. r_mesh->sprite[l].texel[2].st[0] = (double)(x+width)/TexelScale;
  1163. r_mesh->sprite[l].texel[2].st[1] = (double)(y)/TexelScale;
  1164. r_mesh->sprite[l].texel[1].st[0] = (double)(x) /TexelScale;
  1165. r_mesh->sprite[l].texel[1].st[1] = (double)(y) /TexelScale;
  1166. r_mesh->sprite[l].texel[0].st[0] = (double)(x) / TexelScale;
  1167. r_mesh->sprite[l].texel[0].st[1] = (double)(y+height)/TexelScale;
  1168. printf(".");
  1169. fflush(stdout);
  1170. }
  1171. }
  1172. }
  1173. }
  1174. printf("\n");
  1175. }
  1176. void OpenRaider::processMoveables()
  1177. {
  1178. List <unsigned int> cache;
  1179. List <skeletal_model_t *> cache2;
  1180. tr2_mesh_t *mesh = NULL;
  1181. tr2_moveable_t *moveable = NULL;
  1182. tr2_meshtree_t *meshtree = NULL;
  1183. tr2_item_t *item = NULL;
  1184. tr2_animation_t *animation = NULL;
  1185. unsigned short *frame = NULL;
  1186. tr2_sprite_sequence_t *sprite_sequence = NULL;
  1187. tr2_object_texture_t *object_texture = NULL;
  1188. int i, j, object_id;
  1189. int ent = 0;
  1190. frame = m_tombraider.Frame();
  1191. moveable = m_tombraider.Moveable();
  1192. meshtree = m_tombraider.MeshTree();
  1193. mesh = m_tombraider.Mesh();
  1194. object_texture = m_tombraider.ObjectTextures();
  1195. item = m_tombraider.Item();
  1196. animation = m_tombraider.Animation();
  1197. sprite_sequence = m_tombraider.SpriteSequence();
  1198. printf("Processing skeletal models: ");
  1199. for (i = 0; i < m_tombraider.NumItems(); ++i)
  1200. {
  1201. object_id = item[i].object_id;
  1202. // It may not be a moveable, test for sprite
  1203. if (!(m_tombraider.Engine() == TR_VERSION_1 && item[i].intensity1 == -1))
  1204. {
  1205. for (j = 0; j < (int)m_tombraider.NumSpriteSequences(); ++j)
  1206. {
  1207. if (sprite_sequence[j].object_id == object_id)
  1208. break;
  1209. }
  1210. // It's not a moveable, skip sprite
  1211. if (j != (int)m_tombraider.NumSpriteSequences())
  1212. {
  1213. printf("s");
  1214. fflush(stdout);
  1215. continue;
  1216. }
  1217. }
  1218. for (j = 0; j < (int)m_tombraider.NumMoveables(); ++j)
  1219. {
  1220. if ((int)moveable[j].object_id == object_id)
  1221. break;
  1222. }
  1223. // It's not a moveable or even a sprite?, skip unknown
  1224. if (j == (int)m_tombraider.NumMoveables())
  1225. {
  1226. printf("?"); // what the wolf?
  1227. fflush(stdout);
  1228. continue;
  1229. }
  1230. processMoveable(j, i, &ent, cache2, cache, object_id);
  1231. }
  1232. // Get models that aren't items
  1233. for (i = 0; i < m_tombraider.NumMoveables(); ++i)
  1234. {
  1235. print(false, "Processing skeletal models: %i/%i",
  1236. i, m_tombraider.NumMoveables());
  1237. switch ((int)moveable[i].object_id)
  1238. {
  1239. case 30:
  1240. case 2: // Which tr needs this as model agian?
  1241. processMoveable(i, i, &ent, cache2, cache,
  1242. (int)moveable[i].object_id);
  1243. break;
  1244. default:
  1245. switch (m_tombraider.Engine())
  1246. {
  1247. case TR_VERSION_1:
  1248. switch ((int)moveable[i].object_id)
  1249. {
  1250. case TombRaider1::LaraMutant:
  1251. processMoveable(i, i, &ent, cache2, cache,
  1252. (int)moveable[i].object_id);
  1253. break;
  1254. }
  1255. break;
  1256. case TR_VERSION_4:
  1257. switch ((int)moveable[i].object_id)
  1258. {
  1259. case TR4_PISTOLS_ANIM:
  1260. case TR4_UZI_ANIM:
  1261. case TR4_SHOTGUN_ANIM:
  1262. case TR4_CROSSBOW_ANIM:
  1263. case TR4_GRENADE_GUN_ANIM:
  1264. case TR4_SIXSHOOTER_ANIM:
  1265. processMoveable(i, i, &ent, cache2, cache,
  1266. (int)moveable[i].object_id);
  1267. break;
  1268. }
  1269. break;
  1270. default:
  1271. case TR_VERSION_2:
  1272. case TR_VERSION_3:
  1273. case TR_VERSION_5:
  1274. case TR_VERSION_UNKNOWN:
  1275. ;
  1276. }
  1277. }
  1278. }
  1279. printf("\n");
  1280. }
  1281. void OpenRaider::processMoveable(int index, int i, int *ent,
  1282. List <skeletal_model_t *> &cache2,
  1283. List <unsigned int> &cache, int object_id)
  1284. {
  1285. skeletal_model_t *r_model = NULL;
  1286. skeletal_model_t *c_model = NULL;
  1287. animation_frame_t *animation_frame = NULL;
  1288. tr2_mesh_t *mesh = NULL;
  1289. tr2_moveable_t *moveable = NULL;
  1290. tr2_meshtree_t *meshtree = NULL;
  1291. tr2_item_t *item = NULL;
  1292. tr2_animation_t *animation = NULL;
  1293. tr2_meshtree_t *mesh_tree = NULL;
  1294. bone_frame_t *bone = NULL;
  1295. bone_tag_t *tag = NULL;
  1296. entity_t *thing = NULL;
  1297. SkeletalModel *sModel = 0x0;
  1298. unsigned short *frame;
  1299. int j, k, a, frame_step;
  1300. unsigned int l, frame_offset, frame_count, f;
  1301. float pos[3];
  1302. float yaw;
  1303. bool lara = false;
  1304. int skyMesh;
  1305. skyMesh = m_tombraider.getSkyModelId();
  1306. frame = m_tombraider.Frame();
  1307. moveable = m_tombraider.Moveable();
  1308. meshtree = m_tombraider.MeshTree();
  1309. mesh = m_tombraider.Mesh();
  1310. item = m_tombraider.Item();
  1311. animation = m_tombraider.Animation();
  1312. pos[0] = item[i].x;
  1313. pos[1] = item[i].y;
  1314. pos[2] = item[i].z;
  1315. yaw = ((item[i].angle >> 14) & 0x03);
  1316. yaw *= 90;
  1317. thing = new entity_t;
  1318. thing->id = (*ent)++;
  1319. thing->type = 0x00;
  1320. thing->pos[0] = item[i].x;
  1321. thing->pos[1] = item[i].y;
  1322. thing->pos[2] = item[i].z;
  1323. thing->angles[1] = yaw;
  1324. thing->objectId = moveable[index].object_id;
  1325. thing->moving = false;
  1326. thing->animate = false;
  1327. sModel = new SkeletalModel();
  1328. m_render.addSkeletalModel(sModel);
  1329. thing->tmpHook = sModel; // temp hack to keep a running version during refactoring
  1330. if (m_tombraider.Engine() == TR_VERSION_1)
  1331. {
  1332. switch (thing->objectId)
  1333. {
  1334. case TombRaider1::Wolf:
  1335. thing->state = TombRaider1::WolfState_Lying;
  1336. //thing->animate = true;
  1337. sModel->setAnimation(3);
  1338. sModel->setFrame(0);
  1339. break;
  1340. }
  1341. }
  1342. //! \fixme Check here and see if we already have one for object_id later
  1343. // if (gWorld.isCachedSkeletalModel(moveable[index].object_id))
  1344. // {
  1345. // thing->modelId = m_render.add(sModel);
  1346. // return;
  1347. // }
  1348. r_model = new skeletal_model_t;
  1349. r_model->id = moveable[index].object_id;
  1350. // Gather more info if this is lara
  1351. if (moveable[index].object_id == 0)
  1352. {
  1353. gLaraModel = r_model; // hack to avoid broken system until new event sys
  1354. lara = true;
  1355. m_camera.translate(pos[0], pos[1] - 470, pos[2]);
  1356. thing->type = 0x02;
  1357. LARA = thing; // Mongoose 2002.03.22, Cheap hack for now
  1358. LARA->master = 0x0;
  1359. switch (m_tombraider.Engine())
  1360. {
  1361. case TR_VERSION_3:
  1362. LARA->modelId = i;
  1363. sModel->setAnimation(TR_ANIAMTION_RUN);
  1364. sModel->setIdleAnimation(TR_ANIAMTION_STAND);
  1365. r_model->tr4Overlay = false;
  1366. break;
  1367. case TR_VERSION_4:
  1368. LARA->modelId = i;
  1369. sModel->setAnimation(TR_ANIAMTION_RUN);
  1370. sModel->setIdleAnimation(TR_ANIAMTION_STAND);
  1371. // Only TR4 lara has 2 layer bone tags/meshes per bone frame
  1372. r_model->tr4Overlay = true;
  1373. break;
  1374. default:
  1375. case TR_VERSION_1:
  1376. case TR_VERSION_2:
  1377. case TR_VERSION_5:
  1378. case TR_VERSION_UNKNOWN:
  1379. LARA->modelId = index;
  1380. sModel->setAnimation(TR_ANIAMTION_RUN);
  1381. sModel->setIdleAnimation(TR_ANIAMTION_STAND);
  1382. r_model->tr4Overlay = false;
  1383. }
  1384. r_model->ponytailId = 0;
  1385. }
  1386. else
  1387. {
  1388. lara = false;
  1389. r_model->ponytailId = -1;
  1390. }
  1391. // Animation
  1392. a = moveable[index].animation;
  1393. frame_offset = animation[a].frame_offset / 2;
  1394. frame_step = animation[a].frame_size;
  1395. int frame_cycle = 0;
  1396. if (a >= (int)m_tombraider.NumAnimations())
  1397. {
  1398. a = m_tombraider.NumFrames() - frame_offset;
  1399. }
  1400. else
  1401. {
  1402. a = (animation[a].frame_offset / 2) - frame_offset;
  1403. }
  1404. if (frame_step != 0) // prevent divide-by-zero errors
  1405. a /= frame_step;
  1406. if (a != 0) // prevent divide-by-zero errors
  1407. frame_offset += frame_step * (frame_cycle % a);
  1408. if (a < 0)
  1409. {
  1410. // continue;
  1411. return;
  1412. }
  1413. //! \fixme Might be better UID for each model, but this seems to work well
  1414. j = object_id;
  1415. // We only want one copy of the skeletal model in memory
  1416. if (cache.Empty() || cache.SearchKey(j) == UINT_MAX)
  1417. {
  1418. sModel->model = r_model;
  1419. gWorld.addEntity(thing);
  1420. k = gWorld.addModel(r_model);
  1421. cache.Add(j);
  1422. cache2.Add(r_model);
  1423. switch (m_tombraider.Engine())
  1424. {
  1425. case TR_VERSION_4:
  1426. if (LARA && moveable[index].object_id == 30)
  1427. {
  1428. r_model->ponytailId = k;
  1429. r_model->ponytailMeshId = moveable[index].starting_mesh;
  1430. r_model->ponytailNumMeshes = ((moveable[index].num_meshes > 0) ?
  1431. moveable[index].num_meshes : 0);
  1432. r_model->ponytailAngle = -90.0f;
  1433. r_model->ponytail[0] = -3;
  1434. r_model->ponytail[1] = -22;
  1435. r_model->ponytail[2] = -20;
  1436. r_model->ponyOff = 40;
  1437. r_model->ponyOff2 = 32;
  1438. r_model->pigtails = false;
  1439. // Try to guess pigtails by looking for certian num verts in head
  1440. if (mesh[moveable[0].starting_mesh].num_vertices > 80)
  1441. {
  1442. r_model->pigtails = true;
  1443. r_model->ponyOff -= 20;
  1444. r_model->ponytail[1] -= 32;
  1445. }
  1446. m_render.setFlags(Render::fRenderPonytail);
  1447. print(true, "Found known ponytail\n");
  1448. }
  1449. break; // ?
  1450. default:
  1451. case TR_VERSION_1:
  1452. case TR_VERSION_2:
  1453. case TR_VERSION_3:
  1454. case TR_VERSION_5:
  1455. case TR_VERSION_UNKNOWN:
  1456. if (LARA && moveable[index].object_id == 2)
  1457. {
  1458. r_model->ponytailId = k;
  1459. r_model->ponytailMeshId = moveable[index].starting_mesh;
  1460. r_model->ponytailNumMeshes = ((moveable[index].num_meshes > 0) ?
  1461. moveable[index].num_meshes : 0);
  1462. r_model->ponytailAngle = -90.0f;
  1463. r_model->ponytail[0] = 0;
  1464. r_model->ponytail[1] = -20;
  1465. r_model->ponytail[2] = -20;
  1466. r_model->ponyOff = 40;
  1467. r_model->ponyOff2 = 0;
  1468. m_render.setFlags(Render::fRenderPonytail);
  1469. print(true, "Found ponytail?\n");
  1470. }
  1471. }
  1472. }
  1473. else
  1474. {
  1475. delete r_model;
  1476. c_model = cache2[cache.SearchKey(j)];
  1477. sModel->model = c_model;
  1478. gWorld.addEntity(thing);
  1479. gWorld.addModel(c_model);
  1480. printf("c"); // it's already cached
  1481. fflush(stdout);
  1482. // continue;
  1483. return;
  1484. }
  1485. int aloop = m_tombraider.getNumAnimsForMoveable(index);
  1486. #ifdef DEBUG
  1487. if (m_flags & OpenRaider_DebugModel)
  1488. {
  1489. printf("\nanimation = %i, num_animations = %i\n",
  1490. moveable[index].animation, aloop);
  1491. printf("\nitem[%i].flags = %i\nentity[%i]\n",
  1492. i, item[i].flags, thing->id);
  1493. }
  1494. #endif
  1495. //a = moveable[index].animation;
  1496. //frame_offset = animation[a].frame_offset / 2;
  1497. //frame_step = animation[a].frame_size;
  1498. for (; a < aloop; ++a,
  1499. frame_offset = animation[a].frame_offset / 2,
  1500. frame_step = animation[a].frame_size)
  1501. {
  1502. animation_frame = new animation_frame_t;
  1503. r_model->animation.pushBack(animation_frame);
  1504. frame_count = (animation[a].frame_end - animation[a].frame_start) + 1;
  1505. animation_frame->rate = animation[a].frame_rate;
  1506. #ifdef DEBUG
  1507. if (m_flags & OpenRaider_DebugModel)
  1508. {
  1509. printf("animation[%i] state and unknowns = %i, %i, %i, %i, %i\n",
  1510. a, animation[a].state_id, animation[a].unknown1,
  1511. animation[a].unknown2, animation[a].unknown3,
  1512. animation[a].unknown4);
  1513. printf("animation[%i].frame_rate = %i\n",
  1514. a, animation[a].frame_rate);
  1515. printf("animation[%i].next_animation = %i\n",
  1516. a, animation[a].next_animation);
  1517. printf("animation[%i].frame_offset = %i\n",
  1518. a, animation[a].frame_offset);
  1519. printf("animation[%i].anim_command = %i\n",
  1520. a, animation[a].anim_command);
  1521. printf("animation[%i].num_anim_commands = %i\n",
  1522. a, animation[a].num_anim_commands);
  1523. printf("animation[%i].state_change_offset = %i\n",
  1524. a, animation[a].state_change_offset);
  1525. printf(" frame_offset = %i\n",
  1526. frame_offset);
  1527. }
  1528. #endif
  1529. // Get all the frames for aniamtion
  1530. for (f = 0; f < frame_count; ++f, frame_offset += frame_step)
  1531. {
  1532. // HACK: Lara's ObjectID is 315, but her meshes start at 0, so make a
  1533. // quick substitution (so she doesn't appear as a bunch of thighs)
  1534. if (index == 0 && m_tombraider.Engine() == TR_VERSION_3)
  1535. {
  1536. for (j = 0; j < (int)m_tombraider.NumMoveables() && !index; ++j)
  1537. {
  1538. if (moveable[j].object_id == 315)
  1539. index = j;
  1540. }
  1541. }
  1542. // Fix Lara in TR4
  1543. if (index == 0 && m_tombraider.Engine() == TR_VERSION_4)
  1544. {
  1545. for (j = 0; j < (int)m_tombraider.NumMoveables() && !index; ++j)
  1546. {
  1547. // Body is ItemID 8, joints are ItemID 9
  1548. // (TR4 demo: body is ItemID 10, joints are ItemID 11)
  1549. if (moveable[j].object_id == 8)
  1550. index = j;
  1551. }
  1552. }
  1553. else if (moveable[index].object_id == 8 &&
  1554. m_tombraider.Engine() == TR_VERSION_4)
  1555. {
  1556. // KLUDGE to do "skinning"
  1557. index = 0;
  1558. for (j = 0; j < (int)m_tombraider.NumMoveables() && !index; ++j)
  1559. {
  1560. // Body is ItemID 8, joints are ItemID 9
  1561. // (TR4 demo: body is ItemID 10, joints are ItemID 11)
  1562. if (moveable[j].object_id == 9)
  1563. index = j;
  1564. }
  1565. }
  1566. #ifdef DEBUG
  1567. if (m_flags & OpenRaider_DebugModel)
  1568. {
  1569. printf("animation[%i].boneframe[%i] = offset %i, step %i\n",
  1570. a, f, frame_offset, frame_step);
  1571. }
  1572. #endif
  1573. // Mongoose 2002.08.15, Was
  1574. // if (frame_offset + 8 > _tombraider.NumFrames())
  1575. if (frame_offset > m_tombraider.NumFrames())
  1576. {
  1577. print(true, "WARNING: Bad animation frame %i > %i\n",
  1578. frame_offset, m_tombraider.NumFrames());
  1579. // Mongoose 2002.08.15, Atempt to skip more likely bad animation data
  1580. print(true, "WARNING: Handling bad animation data...");
  1581. return; //continue;
  1582. }
  1583. // Generate bone frames and tags per frame ////////////
  1584. bone = new bone_frame_t;
  1585. animation_frame->frame.pushBack(bone);
  1586. // Init translate for bone frame
  1587. bone->pos[0] = (short)frame[frame_offset + 6];
  1588. bone->pos[1] = (short)frame[frame_offset + 7];
  1589. bone->pos[2] = (short)frame[frame_offset + 8];
  1590. bone->yaw = yaw;
  1591. //printf("%f %f %f\n", bone->pos[0], bone->pos[1], bone->pos[2]);
  1592. l = 9; // First angle offset in this Frame
  1593. // Run through the tag and calculate the rotation and offset
  1594. for (j = 0; j < (int)moveable[index].num_meshes; ++j)
  1595. {
  1596. tag = new bone_tag_t;
  1597. bone->tag.pushBack(tag);
  1598. tag->off[0] = 0.0;
  1599. tag->off[1] = 0.0;
  1600. tag->off[2] = 0.0;
  1601. tag->flag = 0x00;
  1602. tag->rot[0] = 0.0;
  1603. tag->rot[1] = 0.0;
  1604. tag->rot[2] = 0.0;
  1605. tag->mesh = moveable[index].starting_mesh + j;
  1606. // Setup offsets to produce skeletion
  1607. if (j == 0)
  1608. {
  1609. // Since we use bone's offset, these aren't used
  1610. tag->off[0] = 0.0;
  1611. tag->off[1] = 0.0;
  1612. tag->off[2] = 0.0;
  1613. // Always push tag[0], this isn't really used either
  1614. tag->flag = 0x02;
  1615. }
  1616. else // Nonprimary tag - position relative to first tag
  1617. {
  1618. int *tree;
  1619. // Hack: moveable[index].mesh_tree is a byte offset
  1620. // into mesh_tree[], so we have to convert to index
  1621. tree = (int *)(void *)meshtree;
  1622. mesh_tree = (tr2_meshtree_t *)&tree[moveable[index].mesh_tree
  1623. + ((j - 1) * 4)];
  1624. tag->off[0] = mesh_tree->x;
  1625. tag->off[1] = mesh_tree->y;
  1626. tag->off[2] = mesh_tree->z;
  1627. tag->flag = mesh_tree->flags;
  1628. }
  1629. // Setup tag rotations
  1630. m_tombraider.computeRotationAngles(&frame, &frame_offset, &l,
  1631. tag->rot, tag->rot+1, tag->rot+2);
  1632. }
  1633. }
  1634. }
  1635. if (i == skyMesh)
  1636. {
  1637. m_render.setSkyMesh(i, //moveable[i].starting_mesh,
  1638. (m_tombraider.Engine() == TR_VERSION_2));
  1639. }
  1640. printf(".");
  1641. fflush(stdout);
  1642. }
  1643. int compareFaceTextureId(const void *voidA, const void *voidB)
  1644. {
  1645. texture_tri_t *a = (texture_tri_t *)voidA, *b = (texture_tri_t *)voidB;
  1646. if (!a || !b)
  1647. return -1; // error really
  1648. // less than
  1649. if (a->texture < b->texture)
  1650. return -1;
  1651. // greater than ( no need for equal )
  1652. return 1;
  1653. }
  1654. void setupTextureColor(texture_tri_t *r_tri, Render *render, float *colorf)
  1655. {
  1656. unsigned char color[4];
  1657. unsigned int colorI;
  1658. color[0] = (unsigned char)(colorf[0]*255.0f);
  1659. color[1] = (unsigned char)(colorf[1]*255.0f);
  1660. color[2] = (unsigned char)(colorf[2]*255.0f);
  1661. color[3] = (unsigned char)(colorf[3]*255.0f);
  1662. ((unsigned char *)(&colorI))[3] = color[0];
  1663. ((unsigned char *)(&colorI))[2] = color[1];
  1664. ((unsigned char *)(&colorI))[1] = color[2];
  1665. ((unsigned char *)(&colorI))[0] = color[3];
  1666. if (!gColorTextureHACK.find(colorI))
  1667. {
  1668. gColorTextureHACK.pushBack(colorI);
  1669. r_tri->texture = gTextureOffset + gColorTextureHACK.size();
  1670. render->loadTexture(Texture::generateColorTexture(color, 32, 32),
  1671. 32, 32,
  1672. r_tri->texture);
  1673. #ifdef DEBUG_COLOR_TEXTURE_GEN
  1674. printf("Color 0x%02x%02x%02x%02x | 0x%08xto texture[%i]?\n",
  1675. color[0], color[1], color[2], color[3], colorI,
  1676. gColorTextureHACK.size());
  1677. #endif
  1678. }
  1679. else
  1680. {
  1681. //printf("Color already loaded %i -> 0x%08x\n",
  1682. // gColorTextureHACK.getCurrentIndex(),
  1683. // gColorTextureHACK.current());
  1684. r_tri->texture = gTextureOffset + gColorTextureHACK.getCurrentIndex();
  1685. }
  1686. //r_tri->texture = white; // White texture
  1687. }
  1688. void OpenRaider::processModel(int index)
  1689. {
  1690. int i, j, count, texture, white;
  1691. int vertexIndices[6];
  1692. float st[12];
  1693. float color[4];
  1694. unsigned short transparency;
  1695. texture_tri_t *r_tri;
  1696. // Assert common sense
  1697. if (index < 0 || !m_tombraider.isMeshValid(index))
  1698. {
  1699. //! \fixme allow sparse lists with matching ids instead?
  1700. gWorld.addMesh(NULL); // Filler, to make meshes array ids align
  1701. printf("x");
  1702. fflush(stdout);
  1703. return;
  1704. }
  1705. // WHITE texture id
  1706. white = 0;
  1707. model_mesh_t *mesh = new model_mesh_t;
  1708. // Mongoose 2002.08.30, Testing support for 'shootable' models ( traceable )
  1709. m_tombraider.getMeshCollisionInfo(index, mesh->center, &mesh->radius);
  1710. //! \fixme Arrays don't work either =)
  1711. // Mesh geometery, colors, etc
  1712. m_tombraider.getMeshVertexArrays(index,
  1713. &mesh->vertexCount, &mesh->vertices,
  1714. &mesh->normalCount, &mesh->normals,
  1715. &mesh->colorCount, &mesh->colors);
  1716. // Textured Triangles
  1717. count = m_tombraider.getMeshTexturedTriangleCount(index);
  1718. mesh->texturedTriangles.reserve(count); // little faster
  1719. for (i = 0; i < count; ++i)
  1720. {
  1721. r_tri = new texture_tri_t;
  1722. m_tombraider.getMeshTexturedTriangle(index, i,
  1723. r_tri->index,
  1724. r_tri->st,
  1725. &r_tri->texture,
  1726. &r_tri->transparency);
  1727. r_tri->texture += m_texOffset;
  1728. // Add to face vector
  1729. mesh->texturedTriangles.pushBack(r_tri);
  1730. }
  1731. // Coloured Triangles
  1732. count = m_tombraider.getMeshColoredTriangleCount(index);
  1733. mesh->coloredTriangles.reserve(count); // little faster
  1734. for (i = 0; i < count; i++)
  1735. {
  1736. r_tri = new texture_tri_t;
  1737. m_tombraider.getMeshColoredTriangle(index, i,
  1738. r_tri->index,
  1739. color);
  1740. r_tri->st[0] = color[0];
  1741. r_tri->st[1] = color[1];
  1742. r_tri->st[2] = color[2];
  1743. r_tri->st[3] = color[3];
  1744. r_tri->st[4] = 1.0;
  1745. r_tri->st[5] = 1.0;
  1746. #ifdef EXPERIMENTAL
  1747. setupTextureColor(r_tri, &m_render, color);
  1748. #else
  1749. r_tri->texture = white; // White texture
  1750. #endif
  1751. r_tri->transparency = 0;
  1752. // Add to face vector
  1753. mesh->coloredTriangles.pushBack(r_tri);
  1754. }
  1755. // Textured Rectangles
  1756. count = m_tombraider.getMeshTexturedRectangleCount(index);
  1757. mesh->texturedRectangles.reserve(count*2); // little faster
  1758. for (i = 0; i < count; ++i)
  1759. {
  1760. m_tombraider.getMeshTexturedRectangle(index, i,
  1761. vertexIndices,
  1762. st,
  1763. &texture,
  1764. &transparency);
  1765. r_tri = new texture_tri_t;
  1766. for (j = 0; j < 3; ++j)
  1767. r_tri->index[j] = vertexIndices[j];
  1768. for (j = 0; j < 6; ++j)
  1769. r_tri->st[j] = st[j];
  1770. r_tri->texture = texture + m_texOffset;
  1771. r_tri->transparency = transparency;
  1772. // Add to face vector
  1773. mesh->texturedRectangles.pushBack(r_tri);
  1774. r_tri = new texture_tri_t;
  1775. for (j = 3; j < 6; ++j)
  1776. r_tri->index[j-3] = vertexIndices[j];
  1777. for (j = 6; j < 12; ++j)
  1778. r_tri->st[j-6] = st[j];
  1779. r_tri->texture = texture + m_texOffset;
  1780. r_tri->transparency = transparency;
  1781. // Add to face vector
  1782. mesh->texturedRectangles.pushBack(r_tri);
  1783. }
  1784. // Coloured Rectangles
  1785. count = m_tombraider.getMeshColoredRectangleCount(index);
  1786. mesh->coloredRectangles.reserve(count*2); // little faster
  1787. for (i = 0; i < count; ++i)
  1788. {
  1789. m_tombraider.getMeshColoredRectangle(index, i,
  1790. vertexIndices,
  1791. color);
  1792. r_tri = new texture_tri_t;
  1793. for (j = 0; j < 3; ++j)
  1794. r_tri->index[j] = vertexIndices[j];
  1795. //for (j = 0; j < 6; ++j)
  1796. // r_tri->st[j] = st[j];
  1797. r_tri->st[0] = color[0];
  1798. r_tri->st[1] = color[1];
  1799. r_tri->st[2] = color[2];
  1800. r_tri->st[3] = color[3];
  1801. r_tri->st[4] = 1.0;
  1802. r_tri->st[5] = 1.0;
  1803. #ifdef EXPERIMENTAL
  1804. //for (j = 6; j < 12; ++j)
  1805. // r_tri->st[j-6] = st[j];
  1806. setupTextureColor(r_tri, &m_render, color);
  1807. #else
  1808. r_tri->texture = white; // White texture
  1809. #endif
  1810. r_tri->transparency = 0;
  1811. // Add to face vector
  1812. mesh->coloredRectangles.pushBack(r_tri);
  1813. r_tri = new texture_tri_t;
  1814. for (j = 3; j < 6; ++j)
  1815. r_tri->index[j-3] = vertexIndices[j];
  1816. //for (j = 6; j < 12; ++j)
  1817. // r_tri->st[j-6] = st[j];
  1818. r_tri->st[0] = color[0];
  1819. r_tri->st[1] = color[1];
  1820. r_tri->st[2] = color[2];
  1821. r_tri->st[3] = color[3];
  1822. r_tri->st[4] = 1.0;
  1823. r_tri->st[5] = 1.0;
  1824. #ifdef EXPERIMENTAL
  1825. setupTextureColor(r_tri, &m_render, color);
  1826. #else
  1827. r_tri->texture = white; // White texture
  1828. #endif
  1829. r_tri->transparency = 0;
  1830. // Add to face vector
  1831. mesh->coloredRectangles.pushBack(r_tri);
  1832. }
  1833. // Sort faces by texture
  1834. mesh->texturedTriangles.qSort(compareFaceTextureId);
  1835. mesh->coloredTriangles.qSort(compareFaceTextureId);
  1836. mesh->texturedRectangles.qSort(compareFaceTextureId);
  1837. mesh->coloredRectangles.qSort(compareFaceTextureId);
  1838. gWorld.addMesh(mesh);
  1839. printf(".");
  1840. fflush(stdout);
  1841. }
  1842. #define MATRIX_TRANSFORMS
  1843. void OpenRaider::processRoom(int index)
  1844. {
  1845. unsigned int i, j, count;
  1846. room_mesh_t *r_mesh = NULL;
  1847. RenderRoom *rRoom = NULL;
  1848. #ifdef MATRIX_TRANSFORMS
  1849. //matrix_t transform;
  1850. Matrix transform;
  1851. #endif
  1852. if (!m_tombraider.isRoomValid(index))
  1853. {
  1854. openraider_error("OpenRaider::RoomSetup", "Invalid room index",
  1855. __FILE__, __LINE__);
  1856. print(false, "WARNING: Handling invalid vertex array in room");
  1857. gWorld.addRoom(0x0);
  1858. m_render.addRoom(0x0);
  1859. printf("x");
  1860. fflush(stdout);
  1861. return;
  1862. }
  1863. rRoom = new RenderRoom();
  1864. r_mesh = new room_mesh_t;
  1865. r_mesh->id = index;
  1866. m_tombraider.getRoomInfo(index, &r_mesh->flags, r_mesh->pos,
  1867. r_mesh->bbox_min, r_mesh->bbox_max);
  1868. // Adjust positioning for OR world coord translation
  1869. r_mesh->bbox_min[0] += r_mesh->pos[0];
  1870. r_mesh->bbox_max[0] += r_mesh->pos[0];
  1871. r_mesh->bbox_min[2] += r_mesh->pos[2];
  1872. r_mesh->bbox_max[2] += r_mesh->pos[2];
  1873. // Mongoose 2002.04.03, Setup 3d transform
  1874. #ifdef MATRIX_TRANSFORMS
  1875. //mtkMatrixIdentity(transform);
  1876. //mtkMatrixTranslate(transform,
  1877. // r_mesh->pos[0], r_mesh->pos[1], r_mesh->pos[2]);
  1878. transform.setIdentity();
  1879. transform.translate(r_mesh->pos);
  1880. #else
  1881. #endif
  1882. // Setup portals
  1883. float portalVertices[12];
  1884. count = m_tombraider.getRoomPortalCount(index);
  1885. //! \fixme OR wrongly uses a cached adj room list for rendering vis
  1886. r_mesh->adjacentRooms.reserve(count + 1);
  1887. r_mesh->adjacentRooms.setError(-1);
  1888. // Current room is always first
  1889. r_mesh->adjacentRooms.pushBack(index);
  1890. for (i = 0; i < count; ++i)
  1891. {
  1892. portal_t *portal = new portal_t;
  1893. m_tombraider.getRoomPortal(index, i,
  1894. &portal->adjoining_room, portal->normal,
  1895. portalVertices);
  1896. for (j = 0; j < 4; ++j)
  1897. {
  1898. portal->vertices[j][0] = portalVertices[j*3];
  1899. portal->vertices[j][1] = portalVertices[j*3+1];
  1900. portal->vertices[j][2] = portalVertices[j*3+2];
  1901. // Relative coors in vis portals
  1902. #ifdef MATRIX_TRANSFORMS
  1903. //mtkMatrixTransform(transform, portal->vertices[j]);
  1904. transform.multiply3v(portal->vertices[j], portal->vertices[j]);
  1905. #else
  1906. portal->vertices[j][0] += r_mesh->pos[0];
  1907. portal->vertices[j][1] += r_mesh->pos[1];
  1908. portal->vertices[j][2] += r_mesh->pos[2];
  1909. #endif
  1910. }
  1911. r_mesh->adjacentRooms.pushBack(portal->adjoining_room);
  1912. r_mesh->portals.pushBack(portal);
  1913. }
  1914. // Physics/gameplay use /////////////////////////////
  1915. //! \fixme Use more of sector structure, boxes, and floordata
  1916. // List of sectors in this room
  1917. unsigned int sectorFlags;
  1918. int floorDataIndex, boxIndex, roomBelow, roomAbove;
  1919. count = m_tombraider.getRoomSectorCount(index, &r_mesh->numZSectors,
  1920. &r_mesh->numXSectors);
  1921. r_mesh->sectors.reserve(count);
  1922. r_mesh->sectors.setError(0x0);
  1923. for (i = 0; i < count; ++i)
  1924. {
  1925. sector_t *sector = new sector_t;
  1926. m_tombraider.getRoomSector(index, i, &sectorFlags,
  1927. &sector->ceiling, &sector->floor,
  1928. &floorDataIndex, &boxIndex, &roomBelow,
  1929. &roomAbove);
  1930. if (sectorFlags & tombraiderSector_wall)
  1931. {
  1932. sector->wall = true;
  1933. }
  1934. else
  1935. {
  1936. sector->wall = false;
  1937. }
  1938. r_mesh->sectors.pushBack(sector);
  1939. }
  1940. // Setup collision boxes ( Should use sectors, but this is a test )
  1941. count = m_tombraider.getRoomBoxCount(index);
  1942. r_mesh->boxes.reserve(count);
  1943. r_mesh->boxes.setError(0x0);
  1944. //! fixme Only to be done only on room[0]? I don't think so...
  1945. for (i = 0; !index && i < count; ++i)
  1946. {
  1947. box_t *box = new box_t;
  1948. m_tombraider.getRoomBox(index, i,
  1949. box->a.pos, box->b.pos, box->c.pos, box->d.pos);
  1950. r_mesh->boxes.pushBack(box);
  1951. }
  1952. // Setup room lights /////////////////////////////////////
  1953. unsigned int lightFlags, lightType;
  1954. count = m_tombraider.getRoomLightCount(index);
  1955. rRoom->lights.reserve(count);
  1956. rRoom->lights.setError(0x0);
  1957. for (i = 0; i < count; ++i)
  1958. {
  1959. Light *light = new Light();
  1960. m_tombraider.getRoomLight(index, i,
  1961. light->mPos, light->mColor, light->mDir,
  1962. &light->mAtt, &light->mCutoff,
  1963. &lightType, &lightFlags);
  1964. switch (lightType)
  1965. {
  1966. case tombraiderLight_typeDirectional:
  1967. light->mType = Light::typeDirectional;
  1968. break;
  1969. case tombraiderLight_typeSpot:
  1970. light->mType = Light::typeSpot;
  1971. break;
  1972. case tombraiderLight_typePoint:
  1973. default:
  1974. light->mType = Light::typePoint;
  1975. }
  1976. rRoom->lights.pushBack(light);
  1977. }
  1978. // Room geometery //////////////////////////////////
  1979. //#define EXPERIMENTAL_UNFIFIED_ROOM_GEOMETERY
  1980. #ifdef EXPERIMENTAL_UNFIFIED_ROOM_GEOMETERY
  1981. unsigned int vertexCount, normalCount, colorCount, triCount;
  1982. vec_t *vertexArray;
  1983. vec_t *normalArray;
  1984. vec_t *colorArray;
  1985. unsigned int *indices, *flags;
  1986. float *texCoords;
  1987. int *textures;
  1988. m_tombraider.getRoomVertexArrays(index,
  1989. &vertexCount, &vertexArray,
  1990. &normalCount, &normalArray,
  1991. &colorCount, &colorArray);
  1992. rRoom->mesh.bufferVertexArray(vertexCount, (vec_t *)vertexArray);
  1993. rRoom->mesh.bufferNormalArray(normalCount, (vec_t *)normalArray);
  1994. rRoom->mesh.bufferColorArray(vertexCount, (vec_t *)colorArray, 4);
  1995. m_tombraider.getRoomTriangles(index, m_texOffset,
  1996. &triCount, &indices, &texCoords, &textures,
  1997. &flags);
  1998. rRoom->mesh.bufferTriangles(triCount, indices, texCoords, textures, flags);
  1999. #else
  2000. float rgba[4];
  2001. float xyz[3];
  2002. count = m_tombraider.getRoomVertexCount(index);
  2003. rRoom->mesh.allocateVertices(count);
  2004. rRoom->mesh.allocateNormals(0); // count
  2005. rRoom->mesh.allocateColors(count);
  2006. for (i = 0; i < count; ++i)
  2007. {
  2008. m_tombraider.getRoomVertex(index, i, xyz, rgba);
  2009. rRoom->mesh.setVertex(i, xyz[0], xyz[1], xyz[2]);
  2010. rRoom->mesh.setColor(i, rgba);
  2011. }
  2012. // Mongoose 2002.06.09, Setup allocation of meshes and polygons
  2013. // Counters ( Textured polygon lists are allocated per texture)
  2014. // ( Textures are mapped to these meshes )
  2015. int triangle_counter[TextureLimit];
  2016. int triangle_counter_alpha[TextureLimit];
  2017. int rectangle_counter[TextureLimit];
  2018. int rectangle_counter_alpha[TextureLimit];
  2019. int tris_mesh_map[TextureLimit];
  2020. int rect_mesh_map[TextureLimit];
  2021. for (i = 0; i < TextureLimit; ++i)
  2022. {
  2023. triangle_counter[i] = 0;
  2024. triangle_counter_alpha[i] = 0;
  2025. rectangle_counter[i] = 0;
  2026. rectangle_counter_alpha[i] = 0;
  2027. tris_mesh_map[i] = -1;
  2028. rect_mesh_map[i] = -1;
  2029. }
  2030. unsigned int numTris = 0;
  2031. unsigned int numQuads = 0;
  2032. int texture;
  2033. unsigned int r, t, q, v, flags;
  2034. unsigned int indices[4];
  2035. float texCoords[8];
  2036. count = m_tombraider.getRoomTriangleCount(index);
  2037. // Mongoose 2002.08.15, Presort by alpha and texture and setup mapping
  2038. for (t = 0; t < count; ++t)
  2039. {
  2040. m_tombraider.getRoomTriangle(index, t,
  2041. indices, texCoords, &texture, &flags);
  2042. texture += m_texOffset;
  2043. if (texture > (int)TextureLimit)
  2044. {
  2045. print(true, "Handling bad room[%i].tris[%i].texture = %i",
  2046. index, t, texture);
  2047. texture = TextureLimit - 1;
  2048. }
  2049. // Counters set up polygon allocation
  2050. if (flags & tombraiderFace_Alpha ||
  2051. flags & tombraiderFace_PartialAlpha)
  2052. {
  2053. triangle_counter_alpha[texture] += 1;
  2054. }
  2055. else
  2056. {
  2057. triangle_counter[texture] += 1;
  2058. }
  2059. // Counter sets up texture id to mesh id mapping
  2060. if (tris_mesh_map[texture] == -1)
  2061. {
  2062. tris_mesh_map[texture] = ++numTris;
  2063. }
  2064. }
  2065. count = m_tombraider.getRoomRectangleCount(index);
  2066. for (r = 0; r < count; ++r)
  2067. {
  2068. m_tombraider.getRoomRectangle(index, r,
  2069. indices, texCoords, &texture, &flags);
  2070. texture += m_texOffset;
  2071. if (texture > (int)TextureLimit)
  2072. {
  2073. print(true, "Handling bad room[%i].quad[%i].texture = %i",
  2074. index, r, texture);
  2075. texture = TextureLimit - 1;
  2076. }
  2077. if (flags & tombraiderFace_Alpha ||
  2078. flags & tombraiderFace_PartialAlpha)
  2079. {
  2080. rectangle_counter_alpha[texture] += 1;
  2081. }
  2082. else
  2083. {
  2084. rectangle_counter[texture] += 1;
  2085. }
  2086. if (rect_mesh_map[texture] == -1)
  2087. {
  2088. rect_mesh_map[texture] = ++numQuads;
  2089. }
  2090. }
  2091. // Allocate indexed polygon meshes
  2092. rRoom->mesh.allocateTriangles(numTris);
  2093. rRoom->mesh.allocateRectangles(numQuads);
  2094. for (i = 0, j = 0; i < TextureLimit; ++i)
  2095. {
  2096. if (tris_mesh_map[i] > 0)
  2097. {
  2098. j = tris_mesh_map[i] - 1;
  2099. t = triangle_counter[i];
  2100. rRoom->mesh.mTris[j].texture = i;
  2101. #ifdef MULTITEXTURE
  2102. rRoom->mesh.mTris[j].bumpmap = gMapTex2Bump[i];
  2103. #endif
  2104. rRoom->mesh.mTris[j].cnum_triangles = 0;
  2105. rRoom->mesh.mTris[j].num_triangles = 0;
  2106. rRoom->mesh.mTris[j].cnum_alpha_triangles = 0;
  2107. rRoom->mesh.mTris[j].num_alpha_triangles = 0;
  2108. rRoom->mesh.mTris[j].triangles = 0x0;
  2109. rRoom->mesh.mTris[j].alpha_triangles = 0x0;
  2110. rRoom->mesh.mTris[j].texcoors = 0x0;
  2111. rRoom->mesh.mTris[j].texcoors2 = 0x0;
  2112. if (t > 0)
  2113. {
  2114. rRoom->mesh.mTris[j].num_triangles = t;
  2115. rRoom->mesh.mTris[j].triangles = new unsigned int[t*3];
  2116. rRoom->mesh.mTris[j].num_texcoors = t * 3;
  2117. rRoom->mesh.mTris[j].texcoors = new vec2_t[t * 3];
  2118. }
  2119. t = triangle_counter_alpha[i];
  2120. if (t > 0)
  2121. {
  2122. rRoom->mesh.mTris[j].num_alpha_triangles = t;
  2123. rRoom->mesh.mTris[j].alpha_triangles = new unsigned int[t*3];
  2124. rRoom->mesh.mTris[j].num_texcoors2 = t * 3;
  2125. rRoom->mesh.mTris[j].texcoors2 = new vec2_t[t * 3];
  2126. }
  2127. }
  2128. ///////////////////////////////////////////
  2129. if (rect_mesh_map[i] > 0)
  2130. {
  2131. j = rect_mesh_map[i] - 1;
  2132. r = rectangle_counter[i];
  2133. rRoom->mesh.mQuads[j].texture = i;
  2134. #ifdef MULTITEXTURE
  2135. rRoom->mesh.mQuads[j].bumpmap = gMapTex2Bump[i];
  2136. #endif
  2137. rRoom->mesh.mQuads[j].cnum_quads = 0;
  2138. rRoom->mesh.mQuads[j].num_quads = 0;
  2139. rRoom->mesh.mQuads[j].cnum_alpha_quads = 0;
  2140. rRoom->mesh.mQuads[j].num_alpha_quads = 0;
  2141. rRoom->mesh.mQuads[j].quads = 0x0;
  2142. rRoom->mesh.mQuads[j].alpha_quads = 0x0;
  2143. rRoom->mesh.mQuads[j].texcoors = 0x0;
  2144. rRoom->mesh.mQuads[j].texcoors2 = 0x0;
  2145. if (r > 0)
  2146. {
  2147. rRoom->mesh.mQuads[j].num_quads = r;
  2148. rRoom->mesh.mQuads[j].quads = new unsigned int[r*4];
  2149. rRoom->mesh.mQuads[j].num_texcoors = r * 4;
  2150. rRoom->mesh.mQuads[j].texcoors = new vec2_t[r * 4];
  2151. }
  2152. r = rectangle_counter_alpha[i];
  2153. if (r > 0)
  2154. {
  2155. rRoom->mesh.mQuads[j].num_alpha_quads = r;
  2156. rRoom->mesh.mQuads[j].alpha_quads = new unsigned int[r*4];
  2157. rRoom->mesh.mQuads[j].num_texcoors2 = r * 4;
  2158. rRoom->mesh.mQuads[j].texcoors2 = new vec2_t[r * 4];
  2159. }
  2160. }
  2161. }
  2162. // Generate textured triangles
  2163. count = m_tombraider.getRoomTriangleCount(index);
  2164. for (t = 0; t < count; ++t)
  2165. {
  2166. m_tombraider.getRoomTriangle(index, t,
  2167. indices, texCoords, &texture, &flags);
  2168. // Adjust texture id using m_texOffset to map into
  2169. // correct textures
  2170. texture += m_texOffset;
  2171. j = tris_mesh_map[texture] - 1;
  2172. // Setup per vertex
  2173. for (i = 0; i < 3; ++i)
  2174. {
  2175. // Get vertex index {(0, a), (1, b), (2, c)}
  2176. v = indices[i];
  2177. if ((flags & tombraiderFace_Alpha ||
  2178. flags & tombraiderFace_PartialAlpha) &&
  2179. rRoom->mesh.mTris[j].num_alpha_triangles > 0)
  2180. {
  2181. q = rRoom->mesh.mTris[j].cnum_alpha_triangles*3+i;
  2182. rRoom->mesh.mTris[j].alpha_triangles[q] = v;
  2183. rRoom->mesh.mTris[j].texcoors2[q][0] = texCoords[i*2];
  2184. rRoom->mesh.mTris[j].texcoors2[q][1] = texCoords[i*2+1];
  2185. }
  2186. else if (rRoom->mesh.mTris[j].num_triangles > 0)
  2187. {
  2188. q = rRoom->mesh.mTris[j].cnum_triangles*3+i;
  2189. rRoom->mesh.mTris[j].triangles[q] = v;
  2190. rRoom->mesh.mTris[j].texcoors[q][0] = texCoords[i*2];
  2191. rRoom->mesh.mTris[j].texcoors[q][1] = texCoords[i*2+1];
  2192. }
  2193. // Partial alpha hack
  2194. if (flags & tombraiderFace_PartialAlpha)
  2195. {
  2196. //rRoom->mesh.colors[v].rgba[3] = 0.45;
  2197. }
  2198. }
  2199. if (flags & tombraiderFace_Alpha ||
  2200. flags & tombraiderFace_PartialAlpha)
  2201. {
  2202. rRoom->mesh.mTris[j].cnum_alpha_triangles++;
  2203. }
  2204. else
  2205. {
  2206. rRoom->mesh.mTris[j].cnum_triangles++;
  2207. }
  2208. }
  2209. // Generate textured quads
  2210. count = m_tombraider.getRoomRectangleCount(index);
  2211. for (r = 0; r < count; ++r)
  2212. {
  2213. m_tombraider.getRoomRectangle(index, r,
  2214. indices, texCoords, &texture, &flags);
  2215. // Adjust texture id using m_texOffset to map into
  2216. // correct textures
  2217. texture += m_texOffset;
  2218. if (texture > (int)TextureLimit)
  2219. {
  2220. texture = TextureLimit - 1;
  2221. }
  2222. j = rect_mesh_map[texture] - 1;
  2223. if (rRoom->mesh.mQuads[j].num_quads <= 0 &&
  2224. rRoom->mesh.mQuads[j].num_alpha_quads <= 0)
  2225. continue;
  2226. // Setup per vertex
  2227. for (i = 0; i < 4; ++i)
  2228. {
  2229. // Get vertex index {(0, a), (1, b), (2, c), (3, d)}
  2230. v = indices[i];
  2231. if ((flags & tombraiderFace_Alpha ||
  2232. flags & tombraiderFace_PartialAlpha) &&
  2233. rRoom->mesh.mQuads[j].num_alpha_quads > 0)
  2234. {
  2235. q = rRoom->mesh.mQuads[j].cnum_alpha_quads*4+i;
  2236. rRoom->mesh.mQuads[j].alpha_quads[q] = v;
  2237. rRoom->mesh.mQuads[j].texcoors2[q][0] = texCoords[i*2];
  2238. rRoom->mesh.mQuads[j].texcoors2[q][1] = texCoords[i*2+1];
  2239. }
  2240. else if (rRoom->mesh.mQuads[j].num_quads > 0)
  2241. {
  2242. q = rRoom->mesh.mQuads[j].cnum_quads*4+i;
  2243. rRoom->mesh.mQuads[j].quads[q] = v;
  2244. rRoom->mesh.mQuads[j].texcoors[q][0] = texCoords[i*2];
  2245. rRoom->mesh.mQuads[j].texcoors[q][1] = texCoords[i*2+1];
  2246. }
  2247. // Partial alpha hack
  2248. if (flags & tombraiderFace_PartialAlpha)
  2249. {
  2250. //rRoom->mesh.colors[v].rgba[3] = 0.45;
  2251. }
  2252. }
  2253. if (flags & tombraiderFace_Alpha ||
  2254. flags & tombraiderFace_PartialAlpha)
  2255. {
  2256. rRoom->mesh.mQuads[j].cnum_alpha_quads++;
  2257. }
  2258. else
  2259. {
  2260. rRoom->mesh.mQuads[j].cnum_quads++;
  2261. }
  2262. }
  2263. #endif
  2264. // Room models
  2265. count = m_tombraider.getRoomModelCount(index);
  2266. r_mesh->models.reserve(count);
  2267. r_mesh->models.setError(0x0);
  2268. for (i = 0; i < count; ++i)
  2269. {
  2270. static_model_t *model = new static_model_t;
  2271. m_tombraider.getRoomModel(index, i,
  2272. &model->index, model->pos, &model->yaw);
  2273. r_mesh->models.pushBack(model);
  2274. }
  2275. // Room sprites
  2276. float spriteVertices[12];
  2277. float spriteTexCoords[8];
  2278. count = m_tombraider.getRoomSpriteCount(index);
  2279. r_mesh->sprites.reserve(count);
  2280. r_mesh->sprites.setError(0x0);
  2281. for (i = 0; i < count; ++i)
  2282. {
  2283. sprite_t *sprite = new sprite_t;
  2284. m_tombraider.getRoomSprite(index, i,
  2285. 10.0f, &sprite->texture, sprite->pos,
  2286. spriteVertices, spriteTexCoords);
  2287. sprite->texture += m_texOffset; // OpenRaider preloads some textures
  2288. sprite->vertex[0].pos[0] = spriteVertices[0];
  2289. sprite->vertex[0].pos[1] = spriteVertices[1];
  2290. sprite->vertex[0].pos[2] = spriteVertices[2];
  2291. sprite->vertex[1].pos[0] = spriteVertices[3];
  2292. sprite->vertex[1].pos[1] = spriteVertices[4];
  2293. sprite->vertex[1].pos[2] = spriteVertices[5];
  2294. sprite->vertex[2].pos[0] = spriteVertices[6];
  2295. sprite->vertex[2].pos[1] = spriteVertices[7];
  2296. sprite->vertex[2].pos[2] = spriteVertices[8];
  2297. sprite->vertex[3].pos[0] = spriteVertices[9];
  2298. sprite->vertex[3].pos[1] = spriteVertices[10];
  2299. sprite->vertex[3].pos[2] = spriteVertices[11];
  2300. sprite->texel[0].st[0] = spriteTexCoords[0];
  2301. sprite->texel[0].st[1] = spriteTexCoords[1];
  2302. sprite->texel[1].st[0] = spriteTexCoords[2];
  2303. sprite->texel[1].st[1] = spriteTexCoords[3];
  2304. sprite->texel[2].st[0] = spriteTexCoords[4];
  2305. sprite->texel[2].st[1] = spriteTexCoords[5];
  2306. sprite->texel[3].st[0] = spriteTexCoords[6];
  2307. sprite->texel[3].st[1] = spriteTexCoords[7];
  2308. r_mesh->sprites.pushBack(sprite);
  2309. }
  2310. gWorld.addRoom(r_mesh);
  2311. rRoom->room = r_mesh;
  2312. m_render.addRoom(rRoom);
  2313. printf(".");
  2314. fflush(stdout);
  2315. }
  2316. void OpenRaider::consoleCommand(char *cmd)
  2317. {
  2318. if (!cmd || !cmd[0])
  2319. return;
  2320. // Mongoose 2003.05.31, Strip off decp console prefix
  2321. if (cmd[0] == '>')
  2322. {
  2323. rc_command(">", cmd);
  2324. }
  2325. if (rc_command("quit", cmd))
  2326. {
  2327. shutdown(0);
  2328. }
  2329. else if (rc_command("port", cmd))
  2330. {
  2331. Network &net = *Network::Instance();
  2332. net.setPort(atoi(cmd));
  2333. }
  2334. else if (rc_command("killserver", cmd))
  2335. {
  2336. Network &net = *Network::Instance();
  2337. print(true, "Stopping network server...");
  2338. net.killServerThread();
  2339. }
  2340. else if (rc_command("server", cmd))
  2341. {
  2342. Network &net = *Network::Instance();
  2343. print(true, "Starting network server...");
  2344. net.spawnServerThread();
  2345. }
  2346. else if (rc_command("disconnect", cmd))
  2347. {
  2348. Network &net = *Network::Instance();
  2349. print(true, "Diconnecting...");
  2350. net.killClientThread();
  2351. }
  2352. else if (rc_command("connect", cmd))
  2353. {
  2354. Network &net = *Network::Instance();
  2355. print(true, "Connecting to %s...", cmd);
  2356. net.setRemoteHost(cmd);
  2357. net.spawnClientThread();
  2358. }
  2359. else if (rc_command("fly", cmd))
  2360. {
  2361. mNoClipping = worldMoveType_fly;
  2362. if (LARA)
  2363. {
  2364. LARA->moveType = worldMoveType_fly;
  2365. }
  2366. print(false, "World clipping is [SPACEY]");
  2367. }
  2368. else if (rc_command("walk", cmd))
  2369. {
  2370. mNoClipping = worldMoveType_walk;
  2371. if (LARA)
  2372. {
  2373. LARA->moveType = worldMoveType_walk;
  2374. }
  2375. print(false, "World clipping is [ON]");
  2376. }
  2377. else if (rc_command("ghost", cmd))
  2378. {
  2379. mNoClipping = worldMoveType_noClipping;
  2380. if (LARA)
  2381. {
  2382. LARA->moveType = worldMoveType_noClipping;
  2383. }
  2384. print(false, "World clipping is [OFF]");
  2385. }
  2386. else if (rc_command("mem", cmd))
  2387. {
  2388. #ifdef DEBUG_MEMORY
  2389. if (rc_command("usage", cmd))
  2390. {
  2391. display_memory_usage();
  2392. }
  2393. else if (rc_command("report", cmd))
  2394. {
  2395. dump_memory_report();
  2396. }
  2397. #else
  2398. printf("This build isn't DEBUG_MEMORY enabled\n");
  2399. #endif
  2400. }
  2401. else if (rc_command("loadlevel", cmd))
  2402. {
  2403. loadLevel(cmd);
  2404. }
  2405. else if (rc_command("play", cmd))
  2406. {
  2407. if (m_flags & OpenRaider_EnableSound)
  2408. {
  2409. mSound.play(atoi(cmd));
  2410. }
  2411. }
  2412. else if (rc_command("sensitivity.x", cmd))
  2413. {
  2414. m_mouseX = atof(cmd);
  2415. }
  2416. else if (rc_command("sensitivity.y", cmd))
  2417. {
  2418. m_mouseY = atof(cmd);
  2419. }
  2420. else if (rc_command("r_pigtails", cmd))
  2421. {
  2422. if (gLaraModel)
  2423. {
  2424. gLaraModel->pigtails = atoi(cmd);
  2425. if (gLaraModel->pigtails)
  2426. {
  2427. gLaraModel->ponyOff -= 20;
  2428. gLaraModel->ponytail[1] -= 32;
  2429. }
  2430. else
  2431. {
  2432. gLaraModel->ponyOff += 20;
  2433. gLaraModel->ponytail[1] += 32;
  2434. }
  2435. }
  2436. }
  2437. else if (rc_command("r_ponyangle", cmd))
  2438. {
  2439. if (gLaraModel)
  2440. {
  2441. gLaraModel->ponytailAngle = atoi(cmd);
  2442. }
  2443. }
  2444. else if (rc_command("r_ponyx", cmd))
  2445. {
  2446. if (gLaraModel)
  2447. {
  2448. gLaraModel->ponytail[0] = atoi(cmd);
  2449. }
  2450. }
  2451. else if (rc_command("r_ponyy", cmd))
  2452. {
  2453. if (gLaraModel)
  2454. {
  2455. gLaraModel->ponytail[1] = atoi(cmd);
  2456. }
  2457. }
  2458. else if (rc_command("r_ponyz", cmd))
  2459. {
  2460. if (gLaraModel)
  2461. {
  2462. gLaraModel->ponytail[2] = atoi(cmd);
  2463. }
  2464. }
  2465. else if (rc_command("r_animate", cmd))
  2466. {
  2467. if (atoi(cmd))
  2468. {
  2469. m_render.setFlags(Render::fAnimateAllModels);
  2470. print(false, "Animating all models");
  2471. }
  2472. else
  2473. {
  2474. m_render.clearFlags(Render::fAnimateAllModels);
  2475. print(false, "No longer animating all models");
  2476. }
  2477. }
  2478. else if (rc_command("r_ponytail", cmd))
  2479. {
  2480. if (atoi(cmd))
  2481. {
  2482. m_render.setFlags(Render::fRenderPonytail);
  2483. print(false, "Rendering ponytail");
  2484. }
  2485. else
  2486. {
  2487. m_render.clearFlags(Render::fRenderPonytail);
  2488. print(false, "No longer rendering ponytail");
  2489. }
  2490. }
  2491. else if (rc_command("r_light", cmd))
  2492. {
  2493. if (atoi(cmd))
  2494. {
  2495. m_render.setFlags(Render::fGL_Lights);
  2496. }
  2497. else
  2498. {
  2499. m_render.clearFlags(Render::fGL_Lights);
  2500. }
  2501. }
  2502. else if (rc_command("hop", cmd))
  2503. {
  2504. if (atoi(cmd))
  2505. {
  2506. gWorld.setFlag(World::fEnableHopping);
  2507. print(true, "Room hopping is on");
  2508. }
  2509. else
  2510. {
  2511. gWorld.clearFlag(World::fEnableHopping);
  2512. print(true, "Room hopping is off");
  2513. }
  2514. }
  2515. else if (rc_command("r_fog", cmd))
  2516. {
  2517. if (atoi(cmd))
  2518. {
  2519. m_render.setFlags(Render::fFog);
  2520. }
  2521. else
  2522. {
  2523. m_render.clearFlags(Render::fFog);
  2524. }
  2525. }
  2526. else if (rc_command("wireframe", cmd))
  2527. {
  2528. m_render.setMode(Render::modeWireframe);
  2529. print(false, "wireframe mode");
  2530. }
  2531. else if (rc_command("solid", cmd))
  2532. {
  2533. m_render.setMode(Render::modeSolid);
  2534. print(false, "solid mode");
  2535. }
  2536. else if (rc_command("texture", cmd))
  2537. {
  2538. m_render.setMode(Render::modeTexture);
  2539. print(false, "texture mode");
  2540. }
  2541. else if (rc_command("vertexlight", cmd))
  2542. {
  2543. m_render.setMode(Render::modeVertexLight);
  2544. print(false, "vertexlight mode");
  2545. }
  2546. else if (rc_command("titlescreen", cmd))
  2547. {
  2548. m_render.setMode(Render::modeLoadScreen);
  2549. print(false, "titlescreen mode");
  2550. }
  2551. else if (rc_command("r_viewmodel", cmd))
  2552. {
  2553. if (LARA)
  2554. {
  2555. SkeletalModel *smdl = (SkeletalModel *)LARA->tmpHook;
  2556. skeletal_model_t *mdl = gWorld.getModel(atoi(cmd));
  2557. if (smdl)
  2558. {
  2559. smdl->setModel(mdl);
  2560. }
  2561. }
  2562. //m_render.ViewModel(LARA, atoi(cmd));
  2563. }
  2564. else if (rc_command("r_oneroom", cmd))
  2565. {
  2566. if (atoi(cmd))
  2567. {
  2568. m_render.setFlags(Render::fOneRoom);
  2569. }
  2570. else
  2571. {
  2572. m_render.clearFlags(Render::fOneRoom);
  2573. }
  2574. }
  2575. else if (rc_command("r_allrooms", cmd))
  2576. {
  2577. if (atoi(cmd))
  2578. {
  2579. m_render.setFlags(Render::fAllRooms);
  2580. }
  2581. else
  2582. {
  2583. m_render.clearFlags(Render::fAllRooms);
  2584. }
  2585. print(true, "Rendering all rooms [%s]", (atoi(cmd) == 0) ? "off" : "on");
  2586. }
  2587. else if (rc_command("r_sprite", cmd))
  2588. {
  2589. if (atoi(cmd))
  2590. {
  2591. m_render.setFlags(Render::fSprites);
  2592. }
  2593. else
  2594. {
  2595. m_render.clearFlags(Render::fSprites);
  2596. }
  2597. }
  2598. else if (rc_command("r_roommodel", cmd))
  2599. {
  2600. if (atoi(cmd))
  2601. {
  2602. m_render.setFlags(Render::fRoomModels);
  2603. }
  2604. else
  2605. {
  2606. m_render.clearFlags(Render::fRoomModels);
  2607. }
  2608. }
  2609. else if (rc_command("r_entmodel", cmd))
  2610. {
  2611. if (atoi(cmd))
  2612. {
  2613. m_render.setFlags(Render::fEntityModels);
  2614. }
  2615. else
  2616. {
  2617. m_render.clearFlags(Render::fEntityModels);
  2618. }
  2619. }
  2620. else if (rc_command("r_particle", cmd))
  2621. {
  2622. if (atoi(cmd))
  2623. {
  2624. m_render.setFlags(Render::fParticles);
  2625. }
  2626. else
  2627. {
  2628. m_render.clearFlags(Render::fParticles);
  2629. }
  2630. }
  2631. else if (rc_command("r_vis", cmd))
  2632. {
  2633. if (atoi(cmd))
  2634. {
  2635. m_render.clearFlags(Render::fUsePortals);
  2636. }
  2637. else
  2638. {
  2639. m_render.clearFlags(Render::fUsePortals);
  2640. }
  2641. }
  2642. else if (rc_command("r_upf", cmd))
  2643. {
  2644. if (atoi(cmd))
  2645. {
  2646. m_render.setFlags(Render::fUpdateRoomListPerFrame);
  2647. }
  2648. else
  2649. {
  2650. m_render.clearFlags(Render::fUpdateRoomListPerFrame);
  2651. }
  2652. }
  2653. else if (rc_command("r_portal", cmd))
  2654. {
  2655. if (atoi(cmd))
  2656. {
  2657. m_render.setFlags(Render::fPortals);
  2658. }
  2659. else
  2660. {
  2661. m_render.clearFlags(Render::fPortals);
  2662. }
  2663. }
  2664. else if (rc_command("r_vmodel", cmd))
  2665. {
  2666. if (atoi(cmd))
  2667. {
  2668. m_render.setFlags(Render::fViewModel);
  2669. }
  2670. else
  2671. {
  2672. m_render.clearFlags(Render::fViewModel);
  2673. }
  2674. }
  2675. else if (rc_command("r_ralpha", cmd))
  2676. {
  2677. if (atoi(cmd))
  2678. {
  2679. m_render.setFlags(Render::fRoomAlpha);
  2680. }
  2681. else
  2682. {
  2683. m_render.clearFlags(Render::fRoomAlpha);
  2684. }
  2685. }
  2686. else if (rc_command("resize", cmd))
  2687. {
  2688. if (rc_command("xga", cmd))
  2689. {
  2690. resize(1024, 768);
  2691. m_render.Update(1024, 768);
  2692. }
  2693. else if (rc_command("svga", cmd))
  2694. {
  2695. resize(800, 600);
  2696. m_render.Update(800, 600);
  2697. }
  2698. else if (rc_command("vga", cmd))
  2699. {
  2700. resize(640, 460);
  2701. m_render.Update(640, 460);
  2702. }
  2703. }
  2704. else if (rc_command("sshot", cmd))
  2705. {
  2706. char sfilename[1024];
  2707. char *tmp = fullPath("~/.OpenRaider/sshots/", '/');
  2708. /* Not very pretty, but gets it done */
  2709. snprintf(sfilename, 1024, "%s%s", tmp, VERSION);
  2710. delete [] tmp;
  2711. m_render.screenShot(sfilename);
  2712. print(false, "Took screenshot");
  2713. }
  2714. else if (rc_command("fullscreen", cmd))
  2715. {
  2716. toggleFullscreen();
  2717. }
  2718. else if (rc_command("showfps", cmd))
  2719. {
  2720. m_flags |= OpenRaider_ShowFPS;
  2721. if (!atoi(cmd))
  2722. {
  2723. m_flags ^= OpenRaider_ShowFPS;
  2724. }
  2725. }
  2726. else if (rc_command("bind", cmd))
  2727. {
  2728. if (rc_command("+forward", cmd))
  2729. {
  2730. bindKeyCommand("+forward", atoi(cmd), OpenRaiderKey_forward);
  2731. }
  2732. else if (rc_command("+console", cmd))
  2733. {
  2734. bindKeyCommand("+console", atoi(cmd), OpenRaiderKey_console);
  2735. }
  2736. else if (rc_command("+backward", cmd))
  2737. {
  2738. bindKeyCommand("+backward", atoi(cmd), OpenRaiderKey_backward);
  2739. }
  2740. else if (rc_command("+jump", cmd))
  2741. {
  2742. bindKeyCommand("+jump", atoi(cmd), OpenRaiderKey_jump);
  2743. }
  2744. else if (rc_command("+crouch", cmd))
  2745. {
  2746. bindKeyCommand("+crouch", atoi(cmd), OpenRaiderKey_crouch);
  2747. }
  2748. else if (rc_command("+right", cmd))
  2749. {
  2750. bindKeyCommand("+right", atoi(cmd), OpenRaiderKey_right);
  2751. }
  2752. else if (rc_command("+left", cmd))
  2753. {
  2754. bindKeyCommand("+left", atoi(cmd), OpenRaiderKey_left);
  2755. }
  2756. else if (rc_command("+attack", cmd))
  2757. {
  2758. bindKeyCommand("+attack", atoi(cmd), OpenRaiderKey_attack);
  2759. }
  2760. }
  2761. else if (rc_command("set", cmd))
  2762. {
  2763. if (rc_command("mousegrab", cmd))
  2764. {
  2765. setGrabMouse(atoi(cmd));
  2766. print(true, "Mouse grabbing [%s]", atoi(cmd) ? "on" : "off");
  2767. }
  2768. }
  2769. else if (rc_command("stat", cmd))
  2770. {
  2771. if (rc_command("fps", cmd))
  2772. {
  2773. m_flags ^= OpenRaider_ShowFPS;
  2774. }
  2775. else if (rc_command("pos", cmd))
  2776. {
  2777. if (LARA)
  2778. {
  2779. print(true, "Room %2i Pos %.0f %.0f %.0f Yaw %.0f Pitch %.0f",
  2780. LARA->room,
  2781. LARA->pos[0], LARA->pos[1], LARA->pos[2],
  2782. HEL_RAD_TO_DEG(LARA->angles[1]),
  2783. HEL_RAD_TO_DEG(LARA->angles[2]));
  2784. }
  2785. }
  2786. else if (rc_command("room", cmd))
  2787. {
  2788. if (rc_command("flags", cmd))
  2789. {
  2790. if (LARA)
  2791. {
  2792. print(true, "Room[%i] flags: 0x%x",
  2793. LARA->room,
  2794. gWorld.getRoomInfo(LARA->room));
  2795. }
  2796. }
  2797. }
  2798. }
  2799. }
  2800. void OpenRaider::handleCommand(char *cmd, unsigned int mode)
  2801. {
  2802. bool b;
  2803. int i;
  2804. // So we can use switch stmt, translate the Ids
  2805. mode = mMode[mode];
  2806. switch (mode)
  2807. {
  2808. case 0: // [Video.OpenGL]
  2809. if (rc_command("Width", cmd))
  2810. {
  2811. m_width = atoi(cmd);
  2812. }
  2813. else if (rc_command("Height", cmd))
  2814. {
  2815. m_height = atoi(cmd);
  2816. }
  2817. else if (rc_command("FastCard", cmd))
  2818. {
  2819. rc_get_bool(cmd, &m_fastCard);
  2820. }
  2821. else if (rc_command("FullScreen", cmd))
  2822. {
  2823. rc_get_bool(cmd, &b);
  2824. m_flags |= OpenRaider_FullScreen;
  2825. if (!b)
  2826. {
  2827. m_flags ^= OpenRaider_FullScreen;
  2828. }
  2829. }
  2830. else if (rc_command("Font", cmd))
  2831. {
  2832. gFontFilename = fullPath(cmd, '/');
  2833. }
  2834. else if (rc_command("Driver", cmd))
  2835. {
  2836. if (cmd[0])
  2837. {
  2838. setDriverGL(cmd);
  2839. }
  2840. }
  2841. else
  2842. {
  2843. printf("Command> [Video.OpenGL] Unknown command '%s'\n", cmd);
  2844. }
  2845. break;
  2846. case 1: // [Audio.OpenAL]
  2847. if (rc_command("Enable", cmd))
  2848. {
  2849. rc_get_bool(cmd, &b);
  2850. m_flags |= OpenRaider_EnableSound;
  2851. if (!b)
  2852. {
  2853. m_flags ^= OpenRaider_EnableSound;
  2854. }
  2855. }
  2856. else
  2857. {
  2858. printf("Command> [Audio.OpenAL] Unknown command '%s'\n", cmd);
  2859. }
  2860. break;
  2861. case 2: // [OpenRaider.Engine]
  2862. if (rc_command("PakDir", cmd))
  2863. {
  2864. if (m_pakDir)
  2865. {
  2866. delete [] m_pakDir;
  2867. }
  2868. m_pakDir = fullPath(cmd, '/');
  2869. }
  2870. else if (rc_command("HomeDir", cmd))
  2871. {
  2872. if (m_homeDir)
  2873. {
  2874. delete [] m_homeDir;
  2875. }
  2876. i = strlen(cmd);
  2877. m_homeDir = fullPath(cmd, '/');
  2878. }
  2879. else if (rc_command("AudioDir", cmd))
  2880. {
  2881. if (m_audioDir)
  2882. {
  2883. delete [] m_audioDir;
  2884. }
  2885. m_audioDir = fullPath(cmd, '/');
  2886. }
  2887. else if (rc_command("MapDebug", cmd))
  2888. {
  2889. rc_get_bool(cmd, &b);
  2890. m_flags |= OpenRaider_DebugMap;
  2891. if (!b)
  2892. {
  2893. m_flags ^= OpenRaider_DebugMap;
  2894. }
  2895. }
  2896. else if (rc_command("Map", cmd))
  2897. {
  2898. if (cmd[0])
  2899. {
  2900. char *fullPathMap;
  2901. fullPathMap = bufferString("%s%s", m_pakDir, cmd);
  2902. if (m_tombraider.checkMime(fullPathMap) == 0)
  2903. {
  2904. printf("Validated pak: '%s'\n",
  2905. fullPathMap);
  2906. delete [] fullPathMap;
  2907. /* Just load relative filename */
  2908. mMapList.pushBack(bufferString("%s", cmd));
  2909. }
  2910. else
  2911. {
  2912. printf("ERROR: pak file '%s' not found or invalid\n",
  2913. fullPathMap);
  2914. delete [] fullPathMap;
  2915. }
  2916. }
  2917. }
  2918. // Mongoose 2001.12.31, Added music list back
  2919. else if (rc_command("Music", cmd))
  2920. {
  2921. if (cmd[0])
  2922. {
  2923. char *music;
  2924. i = strlen(cmd);
  2925. music = new char[i+1];
  2926. strncpy(music, cmd, i);
  2927. music[i] = 0;
  2928. mMusicList.pushBack(music);
  2929. }
  2930. }
  2931. else if (rc_command("DisplayFPS", cmd))
  2932. {
  2933. rc_get_bool(cmd, &b);
  2934. m_flags |= OpenRaider_ShowFPS;
  2935. if (!b)
  2936. {
  2937. m_flags ^= OpenRaider_ShowFPS;
  2938. }
  2939. }
  2940. else if (rc_command("ModelDebug", cmd))
  2941. {
  2942. rc_get_bool(cmd, &b);
  2943. m_flags |= OpenRaider_DebugModel;
  2944. if (!b)
  2945. {
  2946. m_flags ^= OpenRaider_DebugModel;
  2947. }
  2948. }
  2949. else if (rc_command("DumpTexture", cmd))
  2950. {
  2951. rc_get_bool(cmd, &b);
  2952. m_flags |= OpenRaider_DumpTexture;
  2953. if (!b)
  2954. {
  2955. m_flags ^= OpenRaider_DumpTexture;
  2956. }
  2957. }
  2958. else
  2959. {
  2960. printf("Command> [OpenRaider.Engine] Unknown command '%s'\n", cmd);
  2961. }
  2962. break;
  2963. case 3: // [Input.Mouse]
  2964. if (rc_command("SensitivityX", cmd))
  2965. {
  2966. m_mouseX = atof(cmd);
  2967. }
  2968. else if (rc_command("SensitivityY", cmd))
  2969. {
  2970. m_mouseY = atof(cmd);
  2971. }
  2972. else
  2973. {
  2974. printf("Command> [Input.Mouse] Unknown command '%s'\n", cmd);
  2975. }
  2976. break;
  2977. case 4:
  2978. if (cmd[1] == '.')
  2979. {
  2980. cmd[0] = '>';
  2981. cmd[1] = ' ';
  2982. for (i = strlen(cmd) - 1; i >= 0; --i)
  2983. {
  2984. if (cmd[i] == '.' || cmd[i] == '=')
  2985. {
  2986. cmd[i] = ' ';
  2987. }
  2988. }
  2989. }
  2990. consoleCommand(cmd);
  2991. break;
  2992. case 5:
  2993. if (rc_command("Enable", cmd))
  2994. {
  2995. rc_get_bool(cmd, &gStartServer);
  2996. }
  2997. else if (rc_command("Port", cmd))
  2998. {
  2999. if (gStartServer)
  3000. {
  3001. Network &net = *Network::Instance();
  3002. int port = atoi(cmd);
  3003. net.setPort(port);
  3004. print(true, "Starting network server on port %i...", port);
  3005. net.spawnServerThread();
  3006. }
  3007. }
  3008. else
  3009. {
  3010. printf("Command> [Network.Server] '%s' not implemented\n", cmd);
  3011. }
  3012. break;
  3013. }
  3014. }
  3015. int main(int argc, char *argv[]) {
  3016. if (argc != 1) {
  3017. printf("Usage:\n\t%s\n", argv[0]);
  3018. return 1;
  3019. }
  3020. OpenRaider *game = OpenRaider::Instance();
  3021. atexit(killOpenRaiderSingleton);
  3022. game->start();
  3023. return 0;
  3024. }