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

OpenRaider.cpp 94KB

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