Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

OpenRaider.cpp 99KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616
  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_t transform;
  1857. Matrix transform;
  1858. #endif
  1859. if (!m_tombraider.isRoomValid(index))
  1860. {
  1861. openraider_error("OpenRaider::RoomSetup", "Invalid room index",
  1862. __FILE__, __LINE__);
  1863. print(false, "WARNING: Handling invalid vertex array in room");
  1864. gWorld.addRoom(0x0);
  1865. m_render.addRoom(0x0);
  1866. printf("x");
  1867. fflush(stdout);
  1868. return;
  1869. }
  1870. rRoom = new RenderRoom();
  1871. r_mesh = new room_mesh_t;
  1872. r_mesh->id = index;
  1873. m_tombraider.getRoomInfo(index, &r_mesh->flags, r_mesh->pos,
  1874. r_mesh->bbox_min, r_mesh->bbox_max);
  1875. // Adjust positioning for OR world coord translation
  1876. r_mesh->bbox_min[0] += r_mesh->pos[0];
  1877. r_mesh->bbox_max[0] += r_mesh->pos[0];
  1878. r_mesh->bbox_min[2] += r_mesh->pos[2];
  1879. r_mesh->bbox_max[2] += r_mesh->pos[2];
  1880. // Mongoose 2002.04.03, Setup 3d transform
  1881. #ifdef MATRIX_TRANSFORMS
  1882. //mtkMatrixIdentity(transform);
  1883. //mtkMatrixTranslate(transform,
  1884. // r_mesh->pos[0], r_mesh->pos[1], r_mesh->pos[2]);
  1885. transform.setIdentity();
  1886. transform.translate(r_mesh->pos);
  1887. #else
  1888. #endif
  1889. // Setup portals
  1890. float portalVertices[12];
  1891. count = m_tombraider.getRoomPortalCount(index);
  1892. //! \fixme OR wrongly uses a cached adj room list for rendering vis
  1893. r_mesh->adjacentRooms.reserve(count + 1);
  1894. r_mesh->adjacentRooms.setError(-1);
  1895. // Current room is always first
  1896. r_mesh->adjacentRooms.pushBack(index);
  1897. for (i = 0; i < count; ++i)
  1898. {
  1899. portal_t *portal = new portal_t;
  1900. m_tombraider.getRoomPortal(index, i,
  1901. &portal->adjoining_room, portal->normal,
  1902. portalVertices);
  1903. for (j = 0; j < 4; ++j)
  1904. {
  1905. portal->vertices[j][0] = portalVertices[j*3];
  1906. portal->vertices[j][1] = portalVertices[j*3+1];
  1907. portal->vertices[j][2] = portalVertices[j*3+2];
  1908. // Relative coors in vis portals
  1909. #ifdef MATRIX_TRANSFORMS
  1910. //mtkMatrixTransform(transform, portal->vertices[j]);
  1911. transform.multiply3v(portal->vertices[j], portal->vertices[j]);
  1912. #else
  1913. portal->vertices[j][0] += r_mesh->pos[0];
  1914. portal->vertices[j][1] += r_mesh->pos[1];
  1915. portal->vertices[j][2] += r_mesh->pos[2];
  1916. #endif
  1917. }
  1918. r_mesh->adjacentRooms.pushBack(portal->adjoining_room);
  1919. r_mesh->portals.pushBack(portal);
  1920. }
  1921. // Physics/gameplay use /////////////////////////////
  1922. //! \fixme Use more of sector structure, boxes, and floordata
  1923. // List of sectors in this room
  1924. unsigned int sectorFlags;
  1925. int floorDataIndex, boxIndex, roomBelow, roomAbove;
  1926. count = m_tombraider.getRoomSectorCount(index, &r_mesh->numZSectors,
  1927. &r_mesh->numXSectors);
  1928. r_mesh->sectors.reserve(count);
  1929. r_mesh->sectors.setError(0x0);
  1930. for (i = 0; i < count; ++i)
  1931. {
  1932. sector_t *sector = new sector_t;
  1933. m_tombraider.getRoomSector(index, i, &sectorFlags,
  1934. &sector->ceiling, &sector->floor,
  1935. &floorDataIndex, &boxIndex, &roomBelow,
  1936. &roomAbove);
  1937. if (sectorFlags & tombraiderSector_wall)
  1938. {
  1939. sector->wall = true;
  1940. }
  1941. else
  1942. {
  1943. sector->wall = false;
  1944. }
  1945. r_mesh->sectors.pushBack(sector);
  1946. }
  1947. // Setup collision boxes ( Should use sectors, but this is a test )
  1948. count = m_tombraider.getRoomBoxCount(index);
  1949. r_mesh->boxes.reserve(count);
  1950. r_mesh->boxes.setError(0x0);
  1951. //! fixme Only to be done only on room[0]? I don't think so...
  1952. for (i = 0; !index && i < count; ++i)
  1953. {
  1954. box_t *box = new box_t;
  1955. m_tombraider.getRoomBox(index, i,
  1956. box->a.pos, box->b.pos, box->c.pos, box->d.pos);
  1957. r_mesh->boxes.pushBack(box);
  1958. }
  1959. // Setup room lights /////////////////////////////////////
  1960. unsigned int lightFlags, lightType;
  1961. count = m_tombraider.getRoomLightCount(index);
  1962. rRoom->lights.reserve(count);
  1963. rRoom->lights.setError(0x0);
  1964. for (i = 0; i < count; ++i)
  1965. {
  1966. Light *light = new Light();
  1967. m_tombraider.getRoomLight(index, i,
  1968. light->mPos, light->mColor, light->mDir,
  1969. &light->mAtt, &light->mCutoff,
  1970. &lightType, &lightFlags);
  1971. switch (lightType)
  1972. {
  1973. case tombraiderLight_typeDirectional:
  1974. light->mType = Light::typeDirectional;
  1975. break;
  1976. case tombraiderLight_typeSpot:
  1977. light->mType = Light::typeSpot;
  1978. break;
  1979. case tombraiderLight_typePoint:
  1980. default:
  1981. light->mType = Light::typePoint;
  1982. }
  1983. rRoom->lights.pushBack(light);
  1984. }
  1985. // Room geometery //////////////////////////////////
  1986. //#define EXPERIMENTAL_UNFIFIED_ROOM_GEOMETERY
  1987. #ifdef EXPERIMENTAL_UNFIFIED_ROOM_GEOMETERY
  1988. unsigned int vertexCount, normalCount, colorCount, triCount;
  1989. vec_t *vertexArray;
  1990. vec_t *normalArray;
  1991. vec_t *colorArray;
  1992. unsigned int *indices, *flags;
  1993. float *texCoords;
  1994. int *textures;
  1995. m_tombraider.getRoomVertexArrays(index,
  1996. &vertexCount, &vertexArray,
  1997. &normalCount, &normalArray,
  1998. &colorCount, &colorArray);
  1999. rRoom->mesh.bufferVertexArray(vertexCount, (vec_t *)vertexArray);
  2000. rRoom->mesh.bufferNormalArray(normalCount, (vec_t *)normalArray);
  2001. rRoom->mesh.bufferColorArray(vertexCount, (vec_t *)colorArray);
  2002. m_tombraider.getRoomTriangles(index, m_texOffset,
  2003. &triCount, &indices, &texCoords, &textures,
  2004. &flags);
  2005. rRoom->mesh.bufferTriangles(triCount, indices, texCoords, textures, flags);
  2006. #else
  2007. float rgba[4];
  2008. float xyz[3];
  2009. count = m_tombraider.getRoomVertexCount(index);
  2010. rRoom->mesh.allocateVertices(count);
  2011. rRoom->mesh.allocateNormals(0); // count
  2012. rRoom->mesh.allocateColors(count);
  2013. for (i = 0; i < count; ++i)
  2014. {
  2015. m_tombraider.getRoomVertex(index, i, xyz, rgba);
  2016. rRoom->mesh.setVertex(i, xyz[0], xyz[1], xyz[2]);
  2017. rRoom->mesh.setColor(i, rgba);
  2018. }
  2019. // Mongoose 2002.06.09, Setup allocation of meshes and polygons
  2020. // Counters ( Textured polygon lists are allocated per texture)
  2021. // ( Textures are mapped to these meshes )
  2022. int triangle_counter[TextureLimit];
  2023. int triangle_counter_alpha[TextureLimit];
  2024. int rectangle_counter[TextureLimit];
  2025. int rectangle_counter_alpha[TextureLimit];
  2026. int tris_mesh_map[TextureLimit];
  2027. int rect_mesh_map[TextureLimit];
  2028. for (i = 0; i < TextureLimit; ++i)
  2029. {
  2030. triangle_counter[i] = 0;
  2031. triangle_counter_alpha[i] = 0;
  2032. rectangle_counter[i] = 0;
  2033. rectangle_counter_alpha[i] = 0;
  2034. tris_mesh_map[i] = -1;
  2035. rect_mesh_map[i] = -1;
  2036. }
  2037. unsigned int numTris = 0;
  2038. unsigned int numQuads = 0;
  2039. int texture;
  2040. unsigned int r, t, q, v, flags;
  2041. unsigned int indices[4];
  2042. float texCoords[8];
  2043. count = m_tombraider.getRoomTriangleCount(index);
  2044. // Mongoose 2002.08.15, Presort by alpha and texture and setup mapping
  2045. for (t = 0; t < count; ++t)
  2046. {
  2047. m_tombraider.getRoomTriangle(index, t,
  2048. indices, texCoords, &texture, &flags);
  2049. texture += m_texOffset;
  2050. if (texture > (int)TextureLimit)
  2051. {
  2052. print(true, "Handling bad room[%i].tris[%i].texture = %i",
  2053. index, t, texture);
  2054. texture = TextureLimit - 1;
  2055. }
  2056. // Counters set up polygon allocation
  2057. if (flags & tombraiderFace_Alpha ||
  2058. flags & tombraiderFace_PartialAlpha)
  2059. {
  2060. triangle_counter_alpha[texture] += 1;
  2061. }
  2062. else
  2063. {
  2064. triangle_counter[texture] += 1;
  2065. }
  2066. // Counter sets up texture id to mesh id mapping
  2067. if (tris_mesh_map[texture] == -1)
  2068. {
  2069. tris_mesh_map[texture] = ++numTris;
  2070. }
  2071. }
  2072. count = m_tombraider.getRoomRectangleCount(index);
  2073. for (r = 0; r < count; ++r)
  2074. {
  2075. m_tombraider.getRoomRectangle(index, r,
  2076. indices, texCoords, &texture, &flags);
  2077. texture += m_texOffset;
  2078. if (texture > (int)TextureLimit)
  2079. {
  2080. print(true, "Handling bad room[%i].quad[%i].texture = %i",
  2081. index, r, texture);
  2082. texture = TextureLimit - 1;
  2083. }
  2084. if (flags & tombraiderFace_Alpha ||
  2085. flags & tombraiderFace_PartialAlpha)
  2086. {
  2087. rectangle_counter_alpha[texture] += 1;
  2088. }
  2089. else
  2090. {
  2091. rectangle_counter[texture] += 1;
  2092. }
  2093. if (rect_mesh_map[texture] == -1)
  2094. {
  2095. rect_mesh_map[texture] = ++numQuads;
  2096. }
  2097. }
  2098. // Allocate indexed polygon meshes
  2099. rRoom->mesh.allocateTriangles(numTris);
  2100. rRoom->mesh.allocateRectangles(numQuads);
  2101. for (i = 0, j = 0; i < TextureLimit; ++i)
  2102. {
  2103. if (tris_mesh_map[i] > 0)
  2104. {
  2105. j = tris_mesh_map[i] - 1;
  2106. t = triangle_counter[i];
  2107. rRoom->mesh.mTris[j].texture = i;
  2108. #ifdef MULTITEXTURE
  2109. rRoom->mesh.mTris[j].bumpmap = gMapTex2Bump[i];
  2110. #endif
  2111. rRoom->mesh.mTris[j].cnum_triangles = 0;
  2112. rRoom->mesh.mTris[j].num_triangles = 0;
  2113. rRoom->mesh.mTris[j].cnum_alpha_triangles = 0;
  2114. rRoom->mesh.mTris[j].num_alpha_triangles = 0;
  2115. rRoom->mesh.mTris[j].triangles = 0x0;
  2116. rRoom->mesh.mTris[j].alpha_triangles = 0x0;
  2117. rRoom->mesh.mTris[j].texcoors = 0x0;
  2118. rRoom->mesh.mTris[j].texcoors2 = 0x0;
  2119. if (t > 0)
  2120. {
  2121. rRoom->mesh.mTris[j].num_triangles = t;
  2122. rRoom->mesh.mTris[j].triangles = new unsigned int[t*3];
  2123. rRoom->mesh.mTris[j].num_texcoors = t * 3;
  2124. rRoom->mesh.mTris[j].texcoors = new vec2_t[t * 3];
  2125. }
  2126. t = triangle_counter_alpha[i];
  2127. if (t > 0)
  2128. {
  2129. rRoom->mesh.mTris[j].num_alpha_triangles = t;
  2130. rRoom->mesh.mTris[j].alpha_triangles = new unsigned int[t*3];
  2131. rRoom->mesh.mTris[j].num_texcoors2 = t * 3;
  2132. rRoom->mesh.mTris[j].texcoors2 = new vec2_t[t * 3];
  2133. }
  2134. }
  2135. ///////////////////////////////////////////
  2136. if (rect_mesh_map[i] > 0)
  2137. {
  2138. j = rect_mesh_map[i] - 1;
  2139. r = rectangle_counter[i];
  2140. rRoom->mesh.mQuads[j].texture = i;
  2141. #ifdef MULTITEXTURE
  2142. rRoom->mesh.mQuads[j].bumpmap = gMapTex2Bump[i];
  2143. #endif
  2144. rRoom->mesh.mQuads[j].cnum_quads = 0;
  2145. rRoom->mesh.mQuads[j].num_quads = 0;
  2146. rRoom->mesh.mQuads[j].cnum_alpha_quads = 0;
  2147. rRoom->mesh.mQuads[j].num_alpha_quads = 0;
  2148. rRoom->mesh.mQuads[j].quads = 0x0;
  2149. rRoom->mesh.mQuads[j].alpha_quads = 0x0;
  2150. rRoom->mesh.mQuads[j].texcoors = 0x0;
  2151. rRoom->mesh.mQuads[j].texcoors2 = 0x0;
  2152. if (r > 0)
  2153. {
  2154. rRoom->mesh.mQuads[j].num_quads = r;
  2155. rRoom->mesh.mQuads[j].quads = new unsigned int[r*4];
  2156. rRoom->mesh.mQuads[j].num_texcoors = r * 4;
  2157. rRoom->mesh.mQuads[j].texcoors = new vec2_t[r * 4];
  2158. }
  2159. r = rectangle_counter_alpha[i];
  2160. if (r > 0)
  2161. {
  2162. rRoom->mesh.mQuads[j].num_alpha_quads = r;
  2163. rRoom->mesh.mQuads[j].alpha_quads = new unsigned int[r*4];
  2164. rRoom->mesh.mQuads[j].num_texcoors2 = r * 4;
  2165. rRoom->mesh.mQuads[j].texcoors2 = new vec2_t[r * 4];
  2166. }
  2167. }
  2168. }
  2169. // Generate textured triangles
  2170. count = m_tombraider.getRoomTriangleCount(index);
  2171. for (t = 0; t < count; ++t)
  2172. {
  2173. m_tombraider.getRoomTriangle(index, t,
  2174. indices, texCoords, &texture, &flags);
  2175. // Adjust texture id using m_texOffset to map into
  2176. // correct textures
  2177. texture += m_texOffset;
  2178. j = tris_mesh_map[texture] - 1;
  2179. // Setup per vertex
  2180. for (i = 0; i < 3; ++i)
  2181. {
  2182. // Get vertex index {(0, a), (1, b), (2, c)}
  2183. v = indices[i];
  2184. if ((flags & tombraiderFace_Alpha ||
  2185. flags & tombraiderFace_PartialAlpha) &&
  2186. rRoom->mesh.mTris[j].num_alpha_triangles > 0)
  2187. {
  2188. q = rRoom->mesh.mTris[j].cnum_alpha_triangles*3+i;
  2189. rRoom->mesh.mTris[j].alpha_triangles[q] = v;
  2190. rRoom->mesh.mTris[j].texcoors2[q][0] = texCoords[i*2];
  2191. rRoom->mesh.mTris[j].texcoors2[q][1] = texCoords[i*2+1];
  2192. }
  2193. else if (rRoom->mesh.mTris[j].num_triangles > 0)
  2194. {
  2195. q = rRoom->mesh.mTris[j].cnum_triangles*3+i;
  2196. rRoom->mesh.mTris[j].triangles[q] = v;
  2197. rRoom->mesh.mTris[j].texcoors[q][0] = texCoords[i*2];
  2198. rRoom->mesh.mTris[j].texcoors[q][1] = texCoords[i*2+1];
  2199. }
  2200. // Partial alpha hack
  2201. if (flags & tombraiderFace_PartialAlpha)
  2202. {
  2203. //rRoom->mesh.colors[v].rgba[3] = 0.45;
  2204. }
  2205. }
  2206. if (flags & tombraiderFace_Alpha ||
  2207. flags & tombraiderFace_PartialAlpha)
  2208. {
  2209. rRoom->mesh.mTris[j].cnum_alpha_triangles++;
  2210. }
  2211. else
  2212. {
  2213. rRoom->mesh.mTris[j].cnum_triangles++;
  2214. }
  2215. }
  2216. // Generate textured quads
  2217. count = m_tombraider.getRoomRectangleCount(index);
  2218. for (r = 0; r < count; ++r)
  2219. {
  2220. m_tombraider.getRoomRectangle(index, r,
  2221. indices, texCoords, &texture, &flags);
  2222. // Adjust texture id using m_texOffset to map into
  2223. // correct textures
  2224. texture += m_texOffset;
  2225. if (texture > (int)TextureLimit)
  2226. {
  2227. texture = TextureLimit - 1;
  2228. }
  2229. j = rect_mesh_map[texture] - 1;
  2230. if (rRoom->mesh.mQuads[j].num_quads <= 0 &&
  2231. rRoom->mesh.mQuads[j].num_alpha_quads <= 0)
  2232. continue;
  2233. // Setup per vertex
  2234. for (i = 0; i < 4; ++i)
  2235. {
  2236. // Get vertex index {(0, a), (1, b), (2, c), (3, d)}
  2237. v = indices[i];
  2238. if ((flags & tombraiderFace_Alpha ||
  2239. flags & tombraiderFace_PartialAlpha) &&
  2240. rRoom->mesh.mQuads[j].num_alpha_quads > 0)
  2241. {
  2242. q = rRoom->mesh.mQuads[j].cnum_alpha_quads*4+i;
  2243. rRoom->mesh.mQuads[j].alpha_quads[q] = v;
  2244. rRoom->mesh.mQuads[j].texcoors2[q][0] = texCoords[i*2];
  2245. rRoom->mesh.mQuads[j].texcoors2[q][1] = texCoords[i*2+1];
  2246. }
  2247. else if (rRoom->mesh.mQuads[j].num_quads > 0)
  2248. {
  2249. q = rRoom->mesh.mQuads[j].cnum_quads*4+i;
  2250. rRoom->mesh.mQuads[j].quads[q] = v;
  2251. rRoom->mesh.mQuads[j].texcoors[q][0] = texCoords[i*2];
  2252. rRoom->mesh.mQuads[j].texcoors[q][1] = texCoords[i*2+1];
  2253. }
  2254. // Partial alpha hack
  2255. if (flags & tombraiderFace_PartialAlpha)
  2256. {
  2257. //rRoom->mesh.colors[v].rgba[3] = 0.45;
  2258. }
  2259. }
  2260. if (flags & tombraiderFace_Alpha ||
  2261. flags & tombraiderFace_PartialAlpha)
  2262. {
  2263. rRoom->mesh.mQuads[j].cnum_alpha_quads++;
  2264. }
  2265. else
  2266. {
  2267. rRoom->mesh.mQuads[j].cnum_quads++;
  2268. }
  2269. }
  2270. #endif
  2271. // Room models
  2272. count = m_tombraider.getRoomModelCount(index);
  2273. r_mesh->models.reserve(count);
  2274. r_mesh->models.setError(0x0);
  2275. for (i = 0; i < count; ++i)
  2276. {
  2277. static_model_t *model = new static_model_t;
  2278. m_tombraider.getRoomModel(index, i,
  2279. &model->index, model->pos, &model->yaw);
  2280. r_mesh->models.pushBack(model);
  2281. }
  2282. // Room sprites
  2283. float spriteVertices[12];
  2284. float spriteTexCoords[8];
  2285. count = m_tombraider.getRoomSpriteCount(index);
  2286. r_mesh->sprites.reserve(count);
  2287. r_mesh->sprites.setError(0x0);
  2288. for (i = 0; i < count; ++i)
  2289. {
  2290. sprite_t *sprite = new sprite_t;
  2291. m_tombraider.getRoomSprite(index, i,
  2292. 10.0f, &sprite->texture, sprite->pos,
  2293. spriteVertices, spriteTexCoords);
  2294. sprite->texture += m_texOffset; // OpenRaider preloads some textures
  2295. sprite->vertex[0].pos[0] = spriteVertices[0];
  2296. sprite->vertex[0].pos[1] = spriteVertices[1];
  2297. sprite->vertex[0].pos[2] = spriteVertices[2];
  2298. sprite->vertex[1].pos[0] = spriteVertices[3];
  2299. sprite->vertex[1].pos[1] = spriteVertices[4];
  2300. sprite->vertex[1].pos[2] = spriteVertices[5];
  2301. sprite->vertex[2].pos[0] = spriteVertices[6];
  2302. sprite->vertex[2].pos[1] = spriteVertices[7];
  2303. sprite->vertex[2].pos[2] = spriteVertices[8];
  2304. sprite->vertex[3].pos[0] = spriteVertices[9];
  2305. sprite->vertex[3].pos[1] = spriteVertices[10];
  2306. sprite->vertex[3].pos[2] = spriteVertices[11];
  2307. sprite->texel[0].st[0] = spriteTexCoords[0];
  2308. sprite->texel[0].st[1] = spriteTexCoords[1];
  2309. sprite->texel[1].st[0] = spriteTexCoords[2];
  2310. sprite->texel[1].st[1] = spriteTexCoords[3];
  2311. sprite->texel[2].st[0] = spriteTexCoords[4];
  2312. sprite->texel[2].st[1] = spriteTexCoords[5];
  2313. sprite->texel[3].st[0] = spriteTexCoords[6];
  2314. sprite->texel[3].st[1] = spriteTexCoords[7];
  2315. r_mesh->sprites.pushBack(sprite);
  2316. }
  2317. gWorld.addRoom(r_mesh);
  2318. rRoom->room = r_mesh;
  2319. m_render.addRoom(rRoom);
  2320. printf(".");
  2321. fflush(stdout);
  2322. }
  2323. void OpenRaider::consoleCommand(char *cmd)
  2324. {
  2325. if (!cmd || !cmd[0])
  2326. return;
  2327. // Mongoose 2003.05.31, Strip off decp console prefix
  2328. if (cmd[0] == '>')
  2329. {
  2330. rc_command(">", cmd);
  2331. }
  2332. if (rc_command("quit", cmd))
  2333. {
  2334. shutdown(0);
  2335. }
  2336. else if (rc_command("port", cmd))
  2337. {
  2338. Network &net = *Network::Instance();
  2339. net.setPort(atoi(cmd));
  2340. }
  2341. else if (rc_command("killserver", cmd))
  2342. {
  2343. Network &net = *Network::Instance();
  2344. print(true, "Stopping network server...");
  2345. net.killServerThread();
  2346. }
  2347. else if (rc_command("server", cmd))
  2348. {
  2349. Network &net = *Network::Instance();
  2350. print(true, "Starting network server...");
  2351. net.spawnServerThread();
  2352. }
  2353. else if (rc_command("disconnect", cmd))
  2354. {
  2355. Network &net = *Network::Instance();
  2356. print(true, "Diconnecting...");
  2357. net.killClientThread();
  2358. }
  2359. else if (rc_command("connect", cmd))
  2360. {
  2361. Network &net = *Network::Instance();
  2362. print(true, "Connecting to %s...", cmd);
  2363. net.setRemoteHost(cmd);
  2364. net.spawnClientThread();
  2365. }
  2366. else if (rc_command("fly", cmd))
  2367. {
  2368. if (LARA)
  2369. {
  2370. LARA->moveType = worldMoveType_fly;
  2371. }
  2372. print(false, "World clipping is [SPACEY]");
  2373. }
  2374. else if (rc_command("walk", cmd))
  2375. {
  2376. if (LARA)
  2377. {
  2378. LARA->moveType = worldMoveType_walk;
  2379. }
  2380. print(false, "World clipping is [ON]");
  2381. }
  2382. else if (rc_command("ghost", cmd))
  2383. {
  2384. if (LARA)
  2385. {
  2386. LARA->moveType = worldMoveType_noClipping;
  2387. }
  2388. print(false, "World clipping is [OFF]");
  2389. }
  2390. else if (rc_command("loadlevel", cmd))
  2391. {
  2392. loadLevel(cmd);
  2393. }
  2394. else if (rc_command("play", cmd))
  2395. {
  2396. if (m_flags & OpenRaider_EnableSound)
  2397. {
  2398. mSound.play(atoi(cmd));
  2399. }
  2400. }
  2401. else if (rc_command("sensitivity.x", cmd))
  2402. {
  2403. m_mouseX = static_cast<float>(atof(cmd));
  2404. }
  2405. else if (rc_command("sensitivity.y", cmd))
  2406. {
  2407. m_mouseY = static_cast<float>(atof(cmd));
  2408. }
  2409. else if (rc_command("r_pigtails", cmd))
  2410. {
  2411. if (gLaraModel)
  2412. {
  2413. gLaraModel->pigtails = atoi(cmd);
  2414. if (gLaraModel->pigtails)
  2415. {
  2416. gLaraModel->ponyOff -= 20;
  2417. gLaraModel->ponytail[1] -= 32;
  2418. }
  2419. else
  2420. {
  2421. gLaraModel->ponyOff += 20;
  2422. gLaraModel->ponytail[1] += 32;
  2423. }
  2424. }
  2425. }
  2426. else if (rc_command("r_ponyangle", cmd))
  2427. {
  2428. if (gLaraModel)
  2429. {
  2430. gLaraModel->ponytailAngle = atoi(cmd);
  2431. }
  2432. }
  2433. else if (rc_command("r_ponyx", cmd))
  2434. {
  2435. if (gLaraModel)
  2436. {
  2437. gLaraModel->ponytail[0] = atoi(cmd);
  2438. }
  2439. }
  2440. else if (rc_command("r_ponyy", cmd))
  2441. {
  2442. if (gLaraModel)
  2443. {
  2444. gLaraModel->ponytail[1] = atoi(cmd);
  2445. }
  2446. }
  2447. else if (rc_command("r_ponyz", cmd))
  2448. {
  2449. if (gLaraModel)
  2450. {
  2451. gLaraModel->ponytail[2] = atoi(cmd);
  2452. }
  2453. }
  2454. else if (rc_command("r_animate", cmd))
  2455. {
  2456. if (atoi(cmd))
  2457. {
  2458. m_render.setFlags(Render::fAnimateAllModels);
  2459. print(false, "Animating all models");
  2460. }
  2461. else
  2462. {
  2463. m_render.clearFlags(Render::fAnimateAllModels);
  2464. print(false, "No longer animating all models");
  2465. }
  2466. }
  2467. else if (rc_command("r_ponytail", cmd))
  2468. {
  2469. if (atoi(cmd))
  2470. {
  2471. m_render.setFlags(Render::fRenderPonytail);
  2472. print(false, "Rendering ponytail");
  2473. }
  2474. else
  2475. {
  2476. m_render.clearFlags(Render::fRenderPonytail);
  2477. print(false, "No longer rendering ponytail");
  2478. }
  2479. }
  2480. else if (rc_command("r_light", cmd))
  2481. {
  2482. if (atoi(cmd))
  2483. {
  2484. m_render.setFlags(Render::fGL_Lights);
  2485. }
  2486. else
  2487. {
  2488. m_render.clearFlags(Render::fGL_Lights);
  2489. }
  2490. }
  2491. else if (rc_command("hop", cmd))
  2492. {
  2493. if (atoi(cmd))
  2494. {
  2495. gWorld.setFlag(World::fEnableHopping);
  2496. print(true, "Room hopping is on");
  2497. }
  2498. else
  2499. {
  2500. gWorld.clearFlag(World::fEnableHopping);
  2501. print(true, "Room hopping is off");
  2502. }
  2503. }
  2504. else if (rc_command("r_fog", cmd))
  2505. {
  2506. if (atoi(cmd))
  2507. {
  2508. m_render.setFlags(Render::fFog);
  2509. }
  2510. else
  2511. {
  2512. m_render.clearFlags(Render::fFog);
  2513. }
  2514. }
  2515. else if (rc_command("wireframe", cmd))
  2516. {
  2517. m_render.setMode(Render::modeWireframe);
  2518. print(false, "wireframe mode");
  2519. }
  2520. else if (rc_command("solid", cmd))
  2521. {
  2522. m_render.setMode(Render::modeSolid);
  2523. print(false, "solid mode");
  2524. }
  2525. else if (rc_command("texture", cmd))
  2526. {
  2527. m_render.setMode(Render::modeTexture);
  2528. print(false, "texture mode");
  2529. }
  2530. else if (rc_command("vertexlight", cmd))
  2531. {
  2532. m_render.setMode(Render::modeVertexLight);
  2533. print(false, "vertexlight mode");
  2534. }
  2535. else if (rc_command("titlescreen", cmd))
  2536. {
  2537. m_render.setMode(Render::modeLoadScreen);
  2538. print(false, "titlescreen mode");
  2539. }
  2540. else if (rc_command("r_viewmodel", cmd))
  2541. {
  2542. if (LARA)
  2543. {
  2544. SkeletalModel *smdl = static_cast<SkeletalModel *>(LARA->tmpHook);
  2545. skeletal_model_t *mdl = gWorld.getModel(atoi(cmd));
  2546. if (smdl)
  2547. {
  2548. smdl->setModel(mdl);
  2549. }
  2550. }
  2551. //m_render.ViewModel(LARA, atoi(cmd));
  2552. }
  2553. else if (rc_command("r_oneroom", cmd))
  2554. {
  2555. if (atoi(cmd))
  2556. {
  2557. m_render.setFlags(Render::fOneRoom);
  2558. }
  2559. else
  2560. {
  2561. m_render.clearFlags(Render::fOneRoom);
  2562. }
  2563. }
  2564. else if (rc_command("r_allrooms", cmd))
  2565. {
  2566. if (atoi(cmd))
  2567. {
  2568. m_render.setFlags(Render::fAllRooms);
  2569. }
  2570. else
  2571. {
  2572. m_render.clearFlags(Render::fAllRooms);
  2573. }
  2574. print(true, "Rendering all rooms [%s]", (atoi(cmd) == 0) ? "off" : "on");
  2575. }
  2576. else if (rc_command("r_sprite", cmd))
  2577. {
  2578. if (atoi(cmd))
  2579. {
  2580. m_render.setFlags(Render::fSprites);
  2581. }
  2582. else
  2583. {
  2584. m_render.clearFlags(Render::fSprites);
  2585. }
  2586. }
  2587. else if (rc_command("r_roommodel", cmd))
  2588. {
  2589. if (atoi(cmd))
  2590. {
  2591. m_render.setFlags(Render::fRoomModels);
  2592. }
  2593. else
  2594. {
  2595. m_render.clearFlags(Render::fRoomModels);
  2596. }
  2597. }
  2598. else if (rc_command("r_entmodel", cmd))
  2599. {
  2600. if (atoi(cmd))
  2601. {
  2602. m_render.setFlags(Render::fEntityModels);
  2603. }
  2604. else
  2605. {
  2606. m_render.clearFlags(Render::fEntityModels);
  2607. }
  2608. }
  2609. else if (rc_command("r_particle", cmd))
  2610. {
  2611. if (atoi(cmd))
  2612. {
  2613. m_render.setFlags(Render::fParticles);
  2614. }
  2615. else
  2616. {
  2617. m_render.clearFlags(Render::fParticles);
  2618. }
  2619. }
  2620. else if (rc_command("r_vis", cmd))
  2621. {
  2622. if (atoi(cmd))
  2623. {
  2624. m_render.setFlags(Render::fUsePortals);
  2625. }
  2626. else
  2627. {
  2628. m_render.clearFlags(Render::fUsePortals);
  2629. }
  2630. }
  2631. else if (rc_command("r_upf", cmd))
  2632. {
  2633. if (atoi(cmd))
  2634. {
  2635. m_render.setFlags(Render::fUpdateRoomListPerFrame);
  2636. }
  2637. else
  2638. {
  2639. m_render.clearFlags(Render::fUpdateRoomListPerFrame);
  2640. }
  2641. }
  2642. else if (rc_command("r_portal", cmd))
  2643. {
  2644. if (atoi(cmd))
  2645. {
  2646. m_render.setFlags(Render::fPortals);
  2647. }
  2648. else
  2649. {
  2650. m_render.clearFlags(Render::fPortals);
  2651. }
  2652. }
  2653. else if (rc_command("r_vmodel", cmd))
  2654. {
  2655. if (atoi(cmd))
  2656. {
  2657. m_render.setFlags(Render::fViewModel);
  2658. }
  2659. else
  2660. {
  2661. m_render.clearFlags(Render::fViewModel);
  2662. }
  2663. }
  2664. else if (rc_command("r_ralpha", cmd))
  2665. {
  2666. if (atoi(cmd))
  2667. {
  2668. m_render.setFlags(Render::fRoomAlpha);
  2669. }
  2670. else
  2671. {
  2672. m_render.clearFlags(Render::fRoomAlpha);
  2673. }
  2674. }
  2675. else if (rc_command("resize", cmd))
  2676. {
  2677. if (rc_command("xga", cmd))
  2678. {
  2679. resize(1024, 768);
  2680. m_render.Update(1024, 768);
  2681. }
  2682. else if (rc_command("svga", cmd))
  2683. {
  2684. resize(800, 600);
  2685. m_render.Update(800, 600);
  2686. }
  2687. else if (rc_command("vga", cmd))
  2688. {
  2689. resize(640, 460);
  2690. m_render.Update(640, 460);
  2691. }
  2692. }
  2693. else if (rc_command("sshot", cmd))
  2694. {
  2695. char sfilename[1024];
  2696. char *tmp = fullPath("~/.OpenRaider/sshots/", '/');
  2697. /* Not very pretty, but gets it done */
  2698. snprintf(sfilename, 1024, "%s%s", tmp, VERSION);
  2699. delete [] tmp;
  2700. m_render.screenShot(sfilename);
  2701. print(false, "Took screenshot");
  2702. }
  2703. else if (rc_command("fullscreen", cmd))
  2704. {
  2705. toggleFullscreen();
  2706. }
  2707. else if (rc_command("showfps", cmd))
  2708. {
  2709. m_flags |= OpenRaider_ShowFPS;
  2710. if (!atoi(cmd))
  2711. {
  2712. m_flags ^= OpenRaider_ShowFPS;
  2713. }
  2714. }
  2715. else if (rc_command("bind", cmd))
  2716. {
  2717. if (rc_command("+forward", cmd))
  2718. {
  2719. bindKeyCommand("+forward", atoi(cmd), OpenRaiderKey_forward);
  2720. }
  2721. else if (rc_command("+console", cmd))
  2722. {
  2723. bindKeyCommand("+console", atoi(cmd), OpenRaiderKey_console);
  2724. }
  2725. else if (rc_command("+backward", cmd))
  2726. {
  2727. bindKeyCommand("+backward", atoi(cmd), OpenRaiderKey_backward);
  2728. }
  2729. else if (rc_command("+jump", cmd))
  2730. {
  2731. bindKeyCommand("+jump", atoi(cmd), OpenRaiderKey_jump);
  2732. }
  2733. else if (rc_command("+crouch", cmd))
  2734. {
  2735. bindKeyCommand("+crouch", atoi(cmd), OpenRaiderKey_crouch);
  2736. }
  2737. else if (rc_command("+right", cmd))
  2738. {
  2739. bindKeyCommand("+right", atoi(cmd), OpenRaiderKey_right);
  2740. }
  2741. else if (rc_command("+left", cmd))
  2742. {
  2743. bindKeyCommand("+left", atoi(cmd), OpenRaiderKey_left);
  2744. }
  2745. else if (rc_command("+attack", cmd))
  2746. {
  2747. bindKeyCommand("+attack", atoi(cmd), OpenRaiderKey_attack);
  2748. }
  2749. }
  2750. else if (rc_command("set", cmd))
  2751. {
  2752. if (rc_command("mousegrab", cmd))
  2753. {
  2754. setGrabMouse(atoi(cmd));
  2755. print(true, "Mouse grabbing [%s]", atoi(cmd) ? "on" : "off");
  2756. }
  2757. }
  2758. else if (rc_command("stat", cmd))
  2759. {
  2760. if (rc_command("fps", cmd))
  2761. {
  2762. m_flags ^= OpenRaider_ShowFPS;
  2763. }
  2764. else if (rc_command("pos", cmd))
  2765. {
  2766. if (LARA)
  2767. {
  2768. print(true, "Room %2i Pos %.0f %.0f %.0f Yaw %.0f Pitch %.0f",
  2769. LARA->room,
  2770. LARA->pos[0], LARA->pos[1], LARA->pos[2],
  2771. HEL_RAD_TO_DEG(LARA->angles[1]),
  2772. HEL_RAD_TO_DEG(LARA->angles[2]));
  2773. }
  2774. }
  2775. else if (rc_command("room", cmd))
  2776. {
  2777. if (rc_command("flags", cmd))
  2778. {
  2779. if (LARA)
  2780. {
  2781. print(true, "Room[%i] flags: 0x%x",
  2782. LARA->room,
  2783. gWorld.getRoomInfo(LARA->room));
  2784. }
  2785. }
  2786. }
  2787. }
  2788. }
  2789. void OpenRaider::loadPakFolderRecursive(const char *dir) {
  2790. struct dirent *ep;
  2791. DIR *pakDir;
  2792. pakDir = opendir(dir);
  2793. if (pakDir != NULL) {
  2794. while ((ep = readdir(pakDir)) != NULL) {
  2795. if (ep->d_type == DT_DIR) {
  2796. if ((strcmp(".", ep->d_name) != 0)
  2797. && (strcmp("..", ep->d_name) != 0)) {
  2798. char *tmp = bufferString("%s%s", dir, ep->d_name);
  2799. char *next = fullPath(tmp, '/');
  2800. loadPakFolderRecursive(next);
  2801. delete next;
  2802. delete tmp;
  2803. }
  2804. } else {
  2805. char *fullPathMap = bufferString("%s%s", dir, ep->d_name);
  2806. char *lowerPath = bufferString("%s", fullPathMap);
  2807. for (char *p = lowerPath; *p; ++p) *p = (char)tolower(*p);
  2808. // Check for valid extension
  2809. if (stringEndsWith(lowerPath, ".phd")
  2810. || stringEndsWith(lowerPath, ".tr2")
  2811. || stringEndsWith(lowerPath, ".tr4")
  2812. || stringEndsWith(lowerPath, ".trc")) {
  2813. if (m_tombraider.checkMime(fullPathMap) == 0) {
  2814. // printf("Validated pak: '%s'\n", fullPathMap);
  2815. // Just load relative filename
  2816. mMapList.pushBack(bufferString("%s", (fullPathMap + strlen(m_pakDir))));
  2817. } else {
  2818. printf("ERROR: pak file '%s' not found or invalid\n", fullPathMap);
  2819. delete [] fullPathMap;
  2820. }
  2821. }
  2822. delete [] lowerPath;
  2823. }
  2824. }
  2825. closedir(pakDir);
  2826. } else {
  2827. printf("Could not open PAK dir %s!\n", dir);
  2828. }
  2829. }
  2830. void OpenRaider::handleCommand(char *cmd, unsigned int mode)
  2831. {
  2832. bool b;
  2833. int i;
  2834. // So we can use switch stmt, translate the Ids
  2835. mode = mMode[mode];
  2836. switch (mode)
  2837. {
  2838. case 0: // [Video.OpenGL]
  2839. if (rc_command("Width", cmd))
  2840. {
  2841. m_width = atoi(cmd);
  2842. }
  2843. else if (rc_command("Height", cmd))
  2844. {
  2845. m_height = atoi(cmd);
  2846. }
  2847. else if (rc_command("FullScreen", cmd))
  2848. {
  2849. rc_get_bool(cmd, &b);
  2850. m_flags |= OpenRaider_FullScreen;
  2851. if (!b)
  2852. {
  2853. m_flags ^= OpenRaider_FullScreen;
  2854. }
  2855. }
  2856. else if (rc_command("Font", cmd))
  2857. {
  2858. gFontFilename = fullPath(cmd, 0);
  2859. }
  2860. else if (rc_command("Driver", cmd))
  2861. {
  2862. if (cmd[0])
  2863. {
  2864. setDriverGL(cmd);
  2865. }
  2866. }
  2867. else
  2868. {
  2869. printf("Command> [Video.OpenGL] Unknown command '%s'\n", cmd);
  2870. }
  2871. break;
  2872. case 1: // [Audio.OpenAL]
  2873. if (rc_command("Enable", cmd))
  2874. {
  2875. rc_get_bool(cmd, &b);
  2876. m_flags |= OpenRaider_EnableSound;
  2877. if (!b)
  2878. {
  2879. m_flags ^= OpenRaider_EnableSound;
  2880. }
  2881. }
  2882. else
  2883. {
  2884. printf("Command> [Audio.OpenAL] Unknown command '%s'\n", cmd);
  2885. }
  2886. break;
  2887. case 2: // [OpenRaider.Engine]
  2888. if (rc_command("PakDir", cmd))
  2889. {
  2890. if (m_pakDir)
  2891. {
  2892. delete [] m_pakDir;
  2893. }
  2894. m_pakDir = fullPath(cmd, '/');
  2895. // Recursively search for level paks instead of
  2896. // naming individual levels 2014-02-21 xythobuz
  2897. loadPakFolderRecursive(m_pakDir);
  2898. }
  2899. else if (rc_command("HomeDir", cmd))
  2900. {
  2901. if (m_homeDir)
  2902. {
  2903. delete [] m_homeDir;
  2904. }
  2905. i = strlen(cmd);
  2906. m_homeDir = fullPath(cmd, '/');
  2907. }
  2908. else if (rc_command("AudioDir", cmd))
  2909. {
  2910. if (m_audioDir)
  2911. {
  2912. delete [] m_audioDir;
  2913. }
  2914. m_audioDir = fullPath(cmd, '/');
  2915. }
  2916. else if (rc_command("MapDebug", cmd))
  2917. {
  2918. rc_get_bool(cmd, &b);
  2919. m_flags |= OpenRaider_DebugMap;
  2920. if (!b)
  2921. {
  2922. m_flags ^= OpenRaider_DebugMap;
  2923. }
  2924. }
  2925. // Mongoose 2001.12.31, Added music list back
  2926. else if (rc_command("Music", cmd))
  2927. {
  2928. if (cmd[0])
  2929. {
  2930. char *music;
  2931. i = strlen(cmd);
  2932. music = new char[i+1];
  2933. strncpy(music, cmd, i);
  2934. music[i] = 0;
  2935. mMusicList.pushBack(music);
  2936. }
  2937. }
  2938. else if (rc_command("DisplayFPS", cmd))
  2939. {
  2940. rc_get_bool(cmd, &b);
  2941. m_flags |= OpenRaider_ShowFPS;
  2942. if (!b)
  2943. {
  2944. m_flags ^= OpenRaider_ShowFPS;
  2945. }
  2946. }
  2947. else if (rc_command("ModelDebug", cmd))
  2948. {
  2949. rc_get_bool(cmd, &b);
  2950. m_flags |= OpenRaider_DebugModel;
  2951. if (!b)
  2952. {
  2953. m_flags ^= OpenRaider_DebugModel;
  2954. }
  2955. }
  2956. else if (rc_command("DumpTexture", cmd))
  2957. {
  2958. rc_get_bool(cmd, &b);
  2959. m_flags |= OpenRaider_DumpTexture;
  2960. if (!b)
  2961. {
  2962. m_flags ^= OpenRaider_DumpTexture;
  2963. }
  2964. }
  2965. else
  2966. {
  2967. printf("Command> [OpenRaider.Engine] Unknown command '%s'\n", cmd);
  2968. }
  2969. break;
  2970. case 3: // [Input.Mouse]
  2971. if (rc_command("SensitivityX", cmd))
  2972. {
  2973. m_mouseX = static_cast<float>(atof(cmd));
  2974. }
  2975. else if (rc_command("SensitivityY", cmd))
  2976. {
  2977. m_mouseY = static_cast<float>(atof(cmd));
  2978. }
  2979. else
  2980. {
  2981. printf("Command> [Input.Mouse] Unknown command '%s'\n", cmd);
  2982. }
  2983. break;
  2984. case 4:
  2985. if (cmd[1] == '.')
  2986. {
  2987. cmd[0] = '>';
  2988. cmd[1] = ' ';
  2989. for (i = strlen(cmd) - 1; i >= 0; --i)
  2990. {
  2991. if (cmd[i] == '.' || cmd[i] == '=')
  2992. {
  2993. cmd[i] = ' ';
  2994. }
  2995. }
  2996. }
  2997. consoleCommand(cmd);
  2998. break;
  2999. case 5:
  3000. if (rc_command("Enable", cmd))
  3001. {
  3002. rc_get_bool(cmd, &gStartServer);
  3003. }
  3004. else if (rc_command("Port", cmd))
  3005. {
  3006. if (gStartServer)
  3007. {
  3008. Network &net = *Network::Instance();
  3009. int port = atoi(cmd);
  3010. net.setPort(port);
  3011. print(true, "Starting network server on port %i...", port);
  3012. net.spawnServerThread();
  3013. }
  3014. }
  3015. else
  3016. {
  3017. printf("Command> [Network.Server] '%s' not implemented\n", cmd);
  3018. }
  3019. break;
  3020. }
  3021. }
  3022. int main(int argc, char *argv[]) {
  3023. if (argc != 1) {
  3024. printf("Usage:\n\t%s\n", argv[0]);
  3025. return 1;
  3026. }
  3027. OpenRaider *game = OpenRaider::Instance();
  3028. atexit(killOpenRaiderSingleton);
  3029. game->start();
  3030. return 0;
  3031. }