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

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