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

OpenRaider.cpp 99KB

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