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 76KB

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