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

OpenRaider.cpp 98KB

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