Open Source Tomb Raider Engine
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

OpenRaider.cpp 99KB

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