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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594
  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[Mongoose MEMEORY_DEBUG]\nMemory leak table:\n");
  78. dump_memory_report();
  79. #endif
  80. printf("\nThanks for testing %s\n", VERSION);
  81. printf("Please file bug reports\n\n");
  82. printf("Build date: %s @ %s\n", __DATE__, __TIME__);
  83. printf("Build host: %s\n", BUILD_HOST);
  84. printf("Web site : http://github.com/xythobuz/OpenRaider\n");
  85. }
  86. OpenRaider::OpenRaider() : SDLSystem()
  87. {
  88. Network::Instance();
  89. for (unsigned int i = 0; i < 32; ++i)
  90. {
  91. gClientPlayer[i] = 0x0;
  92. }
  93. m_pakDir = 0x0;
  94. m_audioDir = 0x0;
  95. m_homeDir = 0x0;
  96. m_mouseX = 2.5;
  97. m_mouseY = 0.5;
  98. m_texOffset = 0;
  99. mLevelTextureOffset = 0;
  100. m_testSFX = -1;
  101. mNoClipping = 0;
  102. // TODO, Replace numbers with enum modes
  103. // Only do this when you know the amount of commands + 1 ( 0 reserved )
  104. mMode[addCommandMode("[OpenRaider.Engine]")] = 2;
  105. mMode[addCommandMode("[Network.Server]")] = 5;
  106. mMode[addCommandMode("[Video.OpenGL]")] = 0;
  107. mMode[addCommandMode("[Audio.OpenAL]")] = 1;
  108. mMode[addCommandMode("[OpenRaider.Console]")] = 4;
  109. mMode[addCommandMode("[Input.Mouse]")] = 3;
  110. }
  111. OpenRaider::~OpenRaider()
  112. {
  113. // Mongoose 2002.01.02, FIXME GL call to critical section,
  114. // needs mutex really
  115. m_render.setMode(Render::modeDisabled);
  116. sleep(1);
  117. printf("Removing World...\n");
  118. gWorld.destroy();
  119. printf("Cleaning up...\n");
  120. for (mMapList.start(); mMapList.forward(); mMapList.next())
  121. {
  122. if (mMapList.current())
  123. {
  124. delete [] mMapList.current();
  125. }
  126. }
  127. for (mMusicList.start(); mMusicList.forward(); mMusicList.next())
  128. {
  129. if (mMusicList.current())
  130. {
  131. delete [] mMusicList.current();
  132. }
  133. }
  134. if (m_pakDir)
  135. delete [] m_pakDir;
  136. if (m_audioDir)
  137. delete [] m_audioDir;
  138. if (m_homeDir)
  139. delete [] m_homeDir;
  140. if (gFontFilename)
  141. delete [] gFontFilename;
  142. }
  143. ////////////////////////////////////////////////////////////
  144. // Public Accessors
  145. ////////////////////////////////////////////////////////////
  146. ////////////////////////////////////////////////////////////
  147. // Public Mutators
  148. ////////////////////////////////////////////////////////////
  149. void eventAnimTest(int anim)
  150. {
  151. #ifdef USING_MD3
  152. switch (anim)
  153. {
  154. case TR_ANIAMTION_TURN_R:
  155. case TR_ANIAMTION_TURN_L:
  156. //gMd3.setAnimLower(LEGS_TURN);
  157. break;
  158. case TR_ANIAMTION_RUN:
  159. gMd3.setAnimLower(LEGS_RUN);
  160. break;
  161. default:
  162. gMd3.setAnimLower(LEGS_IDLE);
  163. }
  164. #endif
  165. if (LARA)
  166. {
  167. SkeletalModel *mdl = (SkeletalModel *)LARA->tmpHook;
  168. if (mdl)
  169. {
  170. mdl->setAnimation(anim);
  171. }
  172. }
  173. }
  174. void percent_callback(int p)
  175. {
  176. OpenRaider &game = *OpenRaider::Instance();
  177. switch (p)
  178. {
  179. case 10:
  180. game.print(true, "Level textures loaded");
  181. break;
  182. default:
  183. // %% has problems with OpenRaider::print it seems
  184. game.print(false, "Level pak %i/100 loaded", p);
  185. }
  186. }
  187. void openraider_warning(const char *func_name, const char *warning,
  188. const char *filename, int line)
  189. {
  190. printf("%s> WARNING %s %s:%i\n", func_name, warning, filename, line);
  191. }
  192. void openraider_error(const char *func_name, const char *error,
  193. const char *filename, int line)
  194. {
  195. printf("%s> ERROR %s %s:%i\n", func_name, error, filename, line);
  196. }
  197. void from_network_layer(network_packet_t *p, unsigned int *last_id)
  198. {
  199. static unsigned int i = 0;
  200. *last_id = i++;
  201. if (gNetTicks > gNetLastTicks + 200) // 200ms
  202. {
  203. gNetLastTicks = gNetTicks;
  204. p->send = 1;
  205. }
  206. if (LARA)
  207. {
  208. p->pos[0] = LARA->pos[0];
  209. p->pos[1] = LARA->pos[1];
  210. p->pos[2] = LARA->pos[2];
  211. p->yaw = LARA->angles[1];
  212. p->pitch = LARA->angles[1];
  213. p->view_model = LARA->modelId;
  214. SkeletalModel *mdl = (SkeletalModel *)LARA->tmpHook;
  215. if (mdl)
  216. {
  217. p->aframe = mdl->getAnimation();
  218. p->bframe = mdl->getFrame();
  219. }
  220. }
  221. #ifdef DEBUG_NETWORK
  222. printf("<> Sending data\n");
  223. #endif
  224. }
  225. void to_network_layer(network_packet_t p)
  226. {
  227. OpenRaider &game = *OpenRaider::Instance();
  228. #ifdef DEBUG_NETWORK
  229. printf("<> Recieved data\n");
  230. #endif
  231. if (gClientPlayer[p.cid])
  232. {
  233. gClientPlayer[p.cid]->pos[0] = p.pos[0];
  234. gClientPlayer[p.cid]->pos[1] = p.pos[1];
  235. gClientPlayer[p.cid]->pos[2] = p.pos[2];
  236. gClientPlayer[p.cid]->angles[1] = p.yaw;
  237. gClientPlayer[p.cid]->angles[2] = p.pitch;
  238. }
  239. else if (LARA)
  240. {
  241. gClientPlayer[p.cid] = new entity_t;
  242. gClientPlayer[p.cid]->pos[0] = p.pos[0];
  243. gClientPlayer[p.cid]->pos[1] = p.pos[1];
  244. gClientPlayer[p.cid]->pos[2] = p.pos[2];
  245. gClientPlayer[p.cid]->angles[1] = p.yaw;
  246. gClientPlayer[p.cid]->angles[2] = p.pitch;
  247. gClientPlayer[p.cid]->moving = true;
  248. gClientPlayer[p.cid]->objectId = 0;
  249. gClientPlayer[p.cid]->id = LARA->id+1000+p.cid;
  250. gClientPlayer[p.cid]->type = 0x02;
  251. SkeletalModel *mdl = new SkeletalModel();
  252. gClientPlayer[p.cid]->tmpHook = mdl;
  253. mdl->setAnimation(p.aframe);
  254. mdl->setFrame(p.bframe);
  255. mdl->setIdleAnimation(TR_ANIAMTION_STAND);
  256. gClientPlayer[p.cid]->modelId = LARA->modelId; //p.view_model;
  257. gClientPlayer[p.cid]->animate = true;
  258. gWorld.addEntity(gClientPlayer[p.cid]);
  259. game.print(true, "A new player (%u) connected", p.cid);
  260. }
  261. if (LARA)
  262. {
  263. Network &network = *Network::Instance();
  264. network_frame_t &gPiggyBack = network.getPiggyBack();
  265. if (gNetTicks > gNetLastTicks + 200)
  266. {
  267. gNetLastTicks = gNetTicks;
  268. gPiggyBack.data.send = 1;
  269. }
  270. gPiggyBack.data.pos[0] = LARA->pos[0];
  271. gPiggyBack.data.pos[1] = LARA->pos[1];
  272. gPiggyBack.data.pos[2] = LARA->pos[2];
  273. gPiggyBack.data.yaw = LARA->angles[1];
  274. gPiggyBack.data.pitch = LARA->angles[2];
  275. gPiggyBack.data.view_model = LARA->modelId;
  276. SkeletalModel *mdl = (SkeletalModel *)LARA->tmpHook;
  277. if (mdl)
  278. {
  279. gPiggyBack.data.aframe = mdl->getAnimation();
  280. gPiggyBack.data.bframe = mdl->getFrame();
  281. }
  282. }
  283. }
  284. void OpenRaider::handleMouseMotionEvent(float x, float y)
  285. {
  286. if (x > 0)
  287. {
  288. m_camera.command(CAMERA_ROTATE_RIGHT);
  289. if (LARA)
  290. {
  291. switch (LARA->moveType)
  292. {
  293. case worldMoveType_swim:
  294. case worldMoveType_fly:
  295. case worldMoveType_noClipping:
  296. eventAnimTest(TR_ANIAMTION_SWIM_R);
  297. break;
  298. default:
  299. eventAnimTest(TR_ANIAMTION_TURN_L); // TURN left always for now
  300. }
  301. LARA->moving = true;
  302. }
  303. }
  304. else if (x < 0)
  305. {
  306. m_camera.command(CAMERA_ROTATE_LEFT);
  307. if (LARA)
  308. {
  309. switch (LARA->moveType)
  310. {
  311. case worldMoveType_swim:
  312. case worldMoveType_fly:
  313. case worldMoveType_noClipping:
  314. eventAnimTest(TR_ANIAMTION_SWIM_L);
  315. break;
  316. default:
  317. eventAnimTest(TR_ANIAMTION_TURN_L);
  318. }
  319. LARA->moving = true;
  320. }
  321. }
  322. if (y > 0)
  323. m_camera.command(CAMERA_ROTATE_UP);
  324. else if (y < 0)
  325. m_camera.command(CAMERA_ROTATE_DOWN);
  326. if (LARA)
  327. {
  328. LARA->angles[1] = m_camera.getRadianYaw();
  329. LARA->angles[2] = m_camera.getRadianPitch();
  330. }
  331. }
  332. void OpenRaider::handleKeyReleaseEvent(unsigned int key, unsigned int mod)
  333. {
  334. }
  335. void OpenRaider::handleBoundKeyPressEvent(unsigned int key)
  336. {
  337. switch (key)
  338. {
  339. case OpenRaiderKey_jump:
  340. m_camera.command(CAMERA_MOVE_UP);
  341. if (LARA)
  342. {
  343. LARA->moving = true;
  344. }
  345. break;
  346. case OpenRaiderKey_crouch:
  347. m_camera.command(CAMERA_MOVE_DOWN);
  348. if (LARA)
  349. {
  350. LARA->moving = true;
  351. }
  352. break;
  353. case OpenRaiderKey_forward:
  354. gWorld.moveEntity(LARA, 'f');
  355. if (LARA && LARA->moving)
  356. {
  357. switch (LARA->moveType)
  358. {
  359. case worldMoveType_swim:
  360. case worldMoveType_fly:
  361. case worldMoveType_noClipping:
  362. eventAnimTest(TR_ANIAMTION_SWIM);
  363. break;
  364. default:
  365. eventAnimTest(TR_ANIAMTION_RUN);
  366. }
  367. }
  368. else if (LARA)
  369. {
  370. switch (LARA->moveType)
  371. {
  372. case worldMoveType_swim:
  373. case worldMoveType_fly:
  374. case worldMoveType_noClipping:
  375. eventAnimTest(TR_ANIAMTION_SWIM_IDLE);
  376. break;
  377. default:
  378. eventAnimTest(TR_ANIAMTION_HIT_WALL_FRONT);
  379. }
  380. }
  381. else
  382. {
  383. m_camera.command(CAMERA_MOVE_FORWARD);
  384. }
  385. break;
  386. case OpenRaiderKey_backward:
  387. gWorld.moveEntity(LARA, 'b');
  388. if (LARA && LARA->moving)
  389. {
  390. switch (LARA->moveType)
  391. {
  392. case worldMoveType_swim:
  393. case worldMoveType_fly:
  394. case worldMoveType_noClipping:
  395. eventAnimTest(TR_ANIAMTION_SWIM);
  396. break;
  397. default:
  398. eventAnimTest(TR_ANIAMTION_RUN);
  399. }
  400. }
  401. break;
  402. case OpenRaiderKey_left:
  403. gWorld.moveEntity(LARA, 'l');
  404. if (LARA && LARA->moving)
  405. {
  406. switch (LARA->moveType)
  407. {
  408. case worldMoveType_swim:
  409. case worldMoveType_fly:
  410. case worldMoveType_noClipping:
  411. eventAnimTest(TR_ANIAMTION_SWIM);
  412. break;
  413. default:
  414. eventAnimTest(TR_ANIAMTION_RUN);
  415. }
  416. }
  417. else if (!LARA)
  418. {
  419. m_camera.command(CAMERA_MOVE_LEFT);
  420. }
  421. break;
  422. case OpenRaiderKey_right:
  423. gWorld.moveEntity(LARA, 'r');
  424. if (LARA && LARA->moving)
  425. {
  426. switch (LARA->moveType)
  427. {
  428. case worldMoveType_swim:
  429. case worldMoveType_fly:
  430. case worldMoveType_noClipping:
  431. eventAnimTest(TR_ANIAMTION_SWIM);
  432. break;
  433. default:
  434. eventAnimTest(TR_ANIAMTION_RUN);
  435. }
  436. }
  437. else if (!LARA)
  438. {
  439. m_camera.command(CAMERA_MOVE_RIGHT);
  440. }
  441. break;
  442. case OpenRaiderKey_panRight:
  443. m_camera.command(CAMERA_ROTATE_RIGHT);
  444. break;
  445. case OpenRaiderKey_panLeft:
  446. m_camera.command(CAMERA_ROTATE_LEFT);
  447. break;
  448. case OpenRaiderKey_tiltDown:
  449. m_camera.command(CAMERA_ROTATE_DOWN);
  450. break;
  451. case OpenRaiderKey_tiltUp:
  452. m_camera.command(CAMERA_ROTATE_UP);
  453. break;
  454. }
  455. }
  456. void OpenRaider::handleBoundKeyReleaseEvent(unsigned int key)
  457. {
  458. switch (key)
  459. {
  460. case '`': // Temp hack also until console is rolled back
  461. //i = 2;
  462. if (mConsoleMode)
  463. {
  464. //buffer[0] = '>';
  465. //buffer[1] = ' ';
  466. //buffer[2] = 0;
  467. mText->setActive(textConsole, true);
  468. }
  469. else
  470. {
  471. mText->setActive(textConsole, false);
  472. //buffer[0] = 0;
  473. }
  474. break;
  475. case OpenRaiderKey_forward:
  476. case OpenRaiderKey_backward:
  477. case OpenRaiderKey_right:
  478. case OpenRaiderKey_left:
  479. if (LARA)
  480. {
  481. LARA->moving = false;
  482. }
  483. }
  484. }
  485. void OpenRaider::handleConsoleKeyPressEvent(unsigned int key,unsigned int mod)
  486. {
  487. static char lastCmd[64] = "2 ";
  488. static unsigned int i = 0;
  489. char *buffer;
  490. unsigned int len;
  491. // Cheap console test
  492. len = mText->GetStringLen(textConsole);
  493. buffer = mText->GetBuffer(textConsole);
  494. //printf("- 0x%x\n", key);
  495. if (key == mConsoleKey)
  496. {
  497. i = 2;
  498. if (mConsoleMode)
  499. {
  500. buffer[0] = '>';
  501. buffer[1] = ' ';
  502. buffer[2] = 0;
  503. mText->setActive(textConsole, true);
  504. }
  505. else
  506. {
  507. mText->setActive(textConsole, false);
  508. buffer[0] = 0;
  509. }
  510. }
  511. else if (mConsoleMode && len > i+1)
  512. {
  513. switch (key)
  514. {
  515. case SYS_KEY_UP:
  516. i = lastCmd[0];
  517. lastCmd[0] = '>';
  518. mText->SetString(textConsole, lastCmd);
  519. break;
  520. case 0:
  521. break;
  522. case '\b':
  523. if (i > 2)
  524. {
  525. buffer[--i] = 0;
  526. }
  527. break;
  528. case '\r':
  529. case '\n':
  530. strncpy(lastCmd, buffer, 63);
  531. lastCmd[0] = i;
  532. command("[OpenRaider.Console]");
  533. command(buffer);
  534. i = 2;
  535. buffer[0] = '>';
  536. buffer[1] = ' ';
  537. buffer[2] = 0;
  538. break;
  539. default:
  540. if (mod & (SYS_MOD_KEY_RSHIFT | SYS_MOD_KEY_LSHIFT) &&
  541. key > 96 && key < 122)
  542. {
  543. buffer[i++] = (key - 32);
  544. }
  545. else
  546. {
  547. if (mod & (SYS_MOD_KEY_RSHIFT | SYS_MOD_KEY_LSHIFT))
  548. {
  549. if (key == '-')
  550. key = '_';
  551. }
  552. buffer[i++] = key;
  553. }
  554. buffer[i] = 0;
  555. }
  556. }
  557. }
  558. void OpenRaider::handleKeyPressEvent(unsigned int key, unsigned int mod)
  559. {
  560. static bool menu = false;
  561. // Cheap menu test
  562. if (menu)
  563. {
  564. switch (key)
  565. {
  566. case SYS_KEY_ESC:
  567. menu = false;
  568. mText->SetString(textMenu, " ");
  569. mText->setActive(textMenu, false);
  570. break;
  571. case SYS_KEY_ENTER:
  572. loadLevel(mMapList.current());
  573. mText->SetString(textMenu, " ");
  574. mText->setActive(textMenu, false);
  575. menu = false;
  576. break;
  577. case SYS_KEY_DOWN:
  578. mMapList.prev();
  579. if (!mMapList.backward())
  580. {
  581. mMapList.finish();
  582. }
  583. if (mMapList.current())
  584. {
  585. mText->SetString(textMenu, "Load %s?", mMapList.current());
  586. }
  587. else
  588. {
  589. mText->SetString(textMenu, "See README for map install");
  590. }
  591. break;
  592. case SYS_KEY_UP:
  593. mMapList.next();
  594. if (!mMapList.forward())
  595. {
  596. mMapList.start();
  597. }
  598. if (mMapList.current())
  599. {
  600. mText->SetString(textMenu, "Load %s?", mMapList.current());
  601. }
  602. else
  603. {
  604. mText->SetString(textMenu, "See README for map install");
  605. }
  606. }
  607. return;
  608. }
  609. switch (key)
  610. {
  611. case SYS_KEY_ESC:
  612. menu = true;
  613. mText->SetString(textMenu, "Select a map UP/DOWN");
  614. mText->setActive(textMenu, true);
  615. break;
  616. case SYS_KEY_ENTER:
  617. if (mod & SYS_MOD_KEY_LALT)
  618. {
  619. toggleFullscreen();
  620. }
  621. break;
  622. }
  623. ////////////////////////////////////////////////////////////
  624. switch (key)
  625. {
  626. case SYS_MOUSE_LEFT:
  627. mSound.play(m_testSFX);
  628. if (LARA)
  629. {
  630. eventAnimTest(15);
  631. }
  632. break;
  633. case '/':
  634. if (LARA)
  635. {
  636. entity_t *e = new entity_t;
  637. e->pos[0] = LARA->pos[0];
  638. e->pos[1] = LARA->pos[1] - 256.0f; // crawling H
  639. e->pos[2] = LARA->pos[2];
  640. e->type = 61;
  641. e->master = LARA->master;
  642. LARA->master = e;
  643. }
  644. break;
  645. case '[':
  646. if (LARA)
  647. {
  648. SkeletalModel *mdl = (SkeletalModel *)LARA->tmpHook;
  649. if (mdl)
  650. {
  651. mdl->setIdleAnimation(mdl->getIdleAnimation() - 1);
  652. mdl->setAnimation(mdl->getIdleAnimation());
  653. print(false, "AFRAME %i\n", mdl->getIdleAnimation());
  654. }
  655. }
  656. break;
  657. case ']':
  658. if (LARA)
  659. {
  660. SkeletalModel *mdl = (SkeletalModel *)LARA->tmpHook;
  661. if (mdl)
  662. {
  663. mdl->setIdleAnimation(mdl->getIdleAnimation() + 1);
  664. mdl->setAnimation(mdl->getIdleAnimation());
  665. print(false, "AFRAME %i\n", mdl->getIdleAnimation());
  666. }
  667. }
  668. break;
  669. case 'r':
  670. mSound.play(m_testSFX);
  671. break;
  672. }
  673. }
  674. void OpenRaider::print(bool dump_stdout, const char *format, ...)
  675. {
  676. static char buffer[128];
  677. va_list args;
  678. unsigned int l;
  679. va_start(args, format);
  680. vsnprintf(buffer, 128, format, args);
  681. l = strlen(buffer);
  682. if (!l || !buffer[0])
  683. {
  684. return;
  685. }
  686. // Strip message of an trailing carrage return
  687. if (buffer[l-1] == '\n')
  688. {
  689. buffer[l-1] = 0;
  690. }
  691. // Print to game 'console'
  692. if (mText)
  693. {
  694. mText->SetString(textOutput, buffer);
  695. }
  696. // Print to stdout
  697. if (dump_stdout)
  698. {
  699. fprintf(stdout, "> %s\n", buffer);
  700. }
  701. va_end(args);
  702. // Mongoose 2002.08.14, Currently, you must request
  703. // frame redraws in this mode
  704. if (m_flags & OpenRaider_Loading)
  705. {
  706. gameFrame();
  707. }
  708. }
  709. void OpenRaider::initGL()
  710. {
  711. // Mongoose 2002.08.13, Handled in start() actually
  712. }
  713. void OpenRaider::start()
  714. {
  715. vec3_t up;
  716. char *filename;
  717. printf("\n[Starting %s]\n\n", VERSION);
  718. filename = fullPath("~/.OpenRaider/OpenRaider.init", 0);
  719. if (loadResourceFile(filename) < 0)
  720. {
  721. printf("Could not find ~/.OpenRaider/OpenRaider.init\n");
  722. if (loadResourceFile("OpenRaider.init") < 0)
  723. {
  724. shutdown(-1);
  725. }
  726. printf("Loaded defaults...\n");
  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. }