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

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