main.cpp 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161
  1. /** Example 021 Quake3 Explorer
  2. This tutorial shows how to load different Quake 3 maps.
  3. Features:
  4. - Load BSP archives at runtime from the menu
  5. - Load a map from the menu. Showing with screenshot
  6. - Set the VideoDriver at runtime from menu
  7. - Adjust GammaLevel at runtime
  8. - Create SceneNodes for the shaders
  9. - Load EntityList and create entity SceneNodes
  10. - Create players with weapons and with collision response
  11. - Play music
  12. You can download the Quake III Arena demo ( copyright id software )
  13. at the following location:
  14. ftp://ftp.idsoftware.com/idstuff/quake3/win32/q3ademo.exe
  15. Copyright 2006-2011 Burningwater, Thomas Alten
  16. */
  17. #include <irrlicht.h>
  18. #include "driverChoice.h"
  19. #include "exampleHelper.h"
  20. #include "q3factory.h"
  21. #include "sound.h"
  22. /*
  23. GameData is used to hold data which is needed to drive the game
  24. */
  25. struct GameData
  26. {
  27. GameData ( const path &startupDir) :
  28. retVal(0), StartupDir(startupDir), createExDevice(0), Device(0)
  29. {
  30. setDefault ();
  31. }
  32. void setDefault ();
  33. s32 save ( const path &filename );
  34. s32 load ( const path &filename );
  35. s32 debugState;
  36. s32 gravityState;
  37. s32 flyTroughState;
  38. s32 wireFrame;
  39. s32 guiActive;
  40. s32 guiInputActive;
  41. f32 GammaValue;
  42. s32 retVal;
  43. s32 sound;
  44. path StartupDir;
  45. stringw CurrentMapName;
  46. array<path> CurrentArchiveList;
  47. vector3df PlayerPosition;
  48. vector3df PlayerRotation;
  49. tQ3EntityList Variable;
  50. Q3LevelLoadParameter loadParam;
  51. SIrrlichtCreationParameters deviceParam;
  52. funcptr_createDeviceEx createExDevice;
  53. IrrlichtDevice *Device;
  54. };
  55. /*
  56. set default settings
  57. */
  58. void GameData::setDefault ()
  59. {
  60. debugState = EDS_OFF;
  61. gravityState = 1;
  62. flyTroughState = 0;
  63. wireFrame = 0;
  64. guiActive = 1;
  65. guiInputActive = 0;
  66. GammaValue = 1.f;
  67. // default deviceParam;
  68. #if defined ( _IRR_WINDOWS_ )
  69. deviceParam.DriverType = EDT_DIRECT3D9;
  70. #else
  71. deviceParam.DriverType = EDT_OPENGL;
  72. #endif
  73. deviceParam.WindowSize.Width = 800;
  74. deviceParam.WindowSize.Height = 600;
  75. deviceParam.Fullscreen = false;
  76. deviceParam.Bits = 24;
  77. deviceParam.ZBufferBits = 16;
  78. deviceParam.Vsync = false;
  79. deviceParam.AntiAlias = false;
  80. // default Quake3 loadParam
  81. loadParam.defaultLightMapMaterial = EMT_LIGHTMAP;
  82. loadParam.defaultModulate = EMFN_MODULATE_1X;
  83. loadParam.defaultFilter = EMF_ANISOTROPIC_FILTER;
  84. loadParam.verbose = 2;
  85. loadParam.mergeShaderBuffer = 1; // merge meshbuffers with same material
  86. loadParam.cleanUnResolvedMeshes = 1; // should unresolved meshes be cleaned. otherwise blue texture
  87. loadParam.loadAllShaders = 1; // load all scripts in the script directory
  88. loadParam.loadSkyShader = 0; // load sky shader
  89. loadParam.alpharef = 1;
  90. sound = 0;
  91. CurrentMapName = "";
  92. CurrentArchiveList.clear ();
  93. const io::path mediaPath = getExampleMediaPath();
  94. // Explorer Media directory
  95. CurrentArchiveList.push_back ( StartupDir + mediaPath );
  96. // Add the original quake3 files before you load your custom map
  97. // Most mods are using the original shaders, models&items&weapons
  98. CurrentArchiveList.push_back("/q/baseq3/");
  99. CurrentArchiveList.push_back(StartupDir + mediaPath + "map-20kdm2.pk3");
  100. }
  101. /*
  102. Load the current game State from a typical quake3 cfg file
  103. */
  104. s32 GameData::load ( const path &filename )
  105. {
  106. if (!Device)
  107. return 0;
  108. // the quake3 mesh loader can also handle *.shader and *.cfg file
  109. IQ3LevelMesh* mesh = (IQ3LevelMesh*) Device->getSceneManager()->getMesh ( filename );
  110. if (!mesh)
  111. return 0;
  112. tQ3EntityList &entityList = mesh->getEntityList ();
  113. stringc s;
  114. u32 pos;
  115. for ( u32 e = 0; e != entityList.size (); ++e )
  116. {
  117. //dumpShader ( s, &entityList[e], false );
  118. //printf ( s.c_str () );
  119. for ( u32 g = 0; g != entityList[e].getGroupSize (); ++g )
  120. {
  121. const SVarGroup *group = entityList[e].getGroup ( g );
  122. for ( u32 index = 0; index < group->Variable.size (); ++index )
  123. {
  124. const SVariable &v = group->Variable[index];
  125. pos = 0;
  126. if ( v.name == "playerposition" )
  127. {
  128. PlayerPosition = getAsVector3df ( v.content, pos );
  129. }
  130. else
  131. if ( v.name == "playerrotation" )
  132. {
  133. PlayerRotation = getAsVector3df ( v.content, pos );
  134. }
  135. }
  136. }
  137. }
  138. return 1;
  139. }
  140. /*
  141. Store the current game state in a quake3 configuration file
  142. */
  143. s32 GameData::save ( const path &filename )
  144. {
  145. return 0; // TODO: Anyone knows why it just returns?
  146. if (!Device)
  147. return 0;
  148. c8 buf[128];
  149. u32 i;
  150. // Store current archive for restart
  151. CurrentArchiveList.clear();
  152. IFileSystem *fs = Device->getFileSystem();
  153. for ( i = 0; i != fs->getFileArchiveCount(); ++i )
  154. {
  155. CurrentArchiveList.push_back ( fs->getFileArchive(i)->getFileList()->getPath() );
  156. }
  157. // Store player position and rotation
  158. ICameraSceneNode * camera = Device->getSceneManager()->getActiveCamera ();
  159. if ( camera )
  160. {
  161. PlayerPosition = camera->getPosition ();
  162. PlayerRotation = camera->getRotation ();
  163. }
  164. IWriteFile *file = fs->createAndWriteFile ( filename );
  165. if (!file)
  166. return 0;
  167. snprintf_irr ( buf, 128, "playerposition %.f %.f %.f\nplayerrotation %.f %.f %.f\n",
  168. PlayerPosition.X, PlayerPosition.Z, PlayerPosition.Y,
  169. PlayerRotation.X, PlayerRotation.Z, PlayerRotation.Y);
  170. file->write ( buf, (s32) strlen ( buf ) );
  171. for ( i = 0; i != fs->getFileArchiveCount(); ++i )
  172. {
  173. snprintf_irr ( buf, 128, "archive %s\n",stringc ( fs->getFileArchive(i)->getFileList()->getPath() ).c_str () );
  174. file->write ( buf, (s32) strlen ( buf ) );
  175. }
  176. file->drop ();
  177. return 1;
  178. }
  179. /*
  180. Representing a player
  181. */
  182. struct Q3Player : public IAnimationEndCallBack
  183. {
  184. Q3Player ()
  185. : Device(0), MapParent(0), Mesh(0), WeaponNode(0), StartPositionCurrent(0)
  186. {
  187. animation[0] = 0;
  188. memset(Anim, 0, sizeof(TimeFire)*4);
  189. }
  190. virtual void OnAnimationEnd(IAnimatedMeshSceneNode* node);
  191. void create ( IrrlichtDevice *device,
  192. IQ3LevelMesh* mesh,
  193. ISceneNode *mapNode,
  194. IMetaTriangleSelector *meta
  195. );
  196. void shutdown ();
  197. void setAnim ( const c8 *name );
  198. void respawn ();
  199. void setpos ( const vector3df &pos, const vector3df& rotation );
  200. ISceneNodeAnimatorCollisionResponse * cam() { return camCollisionResponse ( Device ); }
  201. IrrlichtDevice *Device;
  202. ISceneNode* MapParent;
  203. IQ3LevelMesh* Mesh;
  204. IAnimatedMeshSceneNode* WeaponNode;
  205. s32 StartPositionCurrent;
  206. TimeFire Anim[4];
  207. c8 animation[64];
  208. c8 buf[64];
  209. };
  210. /* End player
  211. */
  212. void Q3Player::shutdown ()
  213. {
  214. setAnim ( 0 );
  215. dropElement (WeaponNode);
  216. if ( Device )
  217. {
  218. ICameraSceneNode* camera = Device->getSceneManager()->getActiveCamera();
  219. dropElement ( camera );
  220. Device = 0;
  221. }
  222. MapParent = 0;
  223. Mesh = 0;
  224. }
  225. /* create a new player
  226. */
  227. void Q3Player::create ( IrrlichtDevice *device, IQ3LevelMesh* mesh, ISceneNode *mapNode, IMetaTriangleSelector *meta )
  228. {
  229. setTimeFire ( Anim + 0, 200, FIRED );
  230. setTimeFire ( Anim + 1, 5000 );
  231. if (!device)
  232. return;
  233. // load FPS weapon to camera
  234. Device = device;
  235. Mesh = mesh;
  236. MapParent = mapNode;
  237. ISceneManager *smgr = device->getSceneManager ();
  238. IVideoDriver * driver = device->getVideoDriver();
  239. ICameraSceneNode* camera = 0;
  240. core::array<SKeyMap> keyMap;
  241. keyMap.set_used(12);
  242. keyMap[0].Action = EKA_MOVE_FORWARD;
  243. keyMap[0].KeyCode = KEY_UP;
  244. keyMap[1].Action = EKA_MOVE_FORWARD;
  245. keyMap[1].KeyCode = KEY_KEY_W;
  246. keyMap[2].Action = EKA_MOVE_BACKWARD;
  247. keyMap[2].KeyCode = KEY_DOWN;
  248. keyMap[3].Action = EKA_MOVE_BACKWARD;
  249. keyMap[3].KeyCode = KEY_KEY_S;
  250. keyMap[4].Action = EKA_STRAFE_LEFT;
  251. keyMap[4].KeyCode = KEY_LEFT;
  252. keyMap[5].Action = EKA_STRAFE_LEFT;
  253. keyMap[5].KeyCode = KEY_KEY_A;
  254. keyMap[6].Action = EKA_STRAFE_RIGHT;
  255. keyMap[6].KeyCode = KEY_RIGHT;
  256. keyMap[7].Action = EKA_STRAFE_RIGHT;
  257. keyMap[7].KeyCode = KEY_KEY_D;
  258. keyMap[8].Action = EKA_JUMP_UP;
  259. keyMap[8].KeyCode = KEY_KEY_J;
  260. keyMap[9].Action = EKA_CROUCH;
  261. keyMap[9].KeyCode = KEY_KEY_C;
  262. keyMap[10].Action = EKA_ROTATE_LEFT;
  263. keyMap[10].KeyCode = KEY_KEY_Q;
  264. keyMap[11].Action = EKA_ROTATE_RIGHT;
  265. keyMap[11].KeyCode = KEY_KEY_E;
  266. camera = smgr->addCameraSceneNodeFPS(0, 100.0f, 0.6f, -1, keyMap.pointer(), keyMap.size(), false, 600.f);
  267. camera->setName ( "First Person Camera" );
  268. //camera->setFOV ( 100.f * core::DEGTORAD );
  269. camera->setFarValue( 20000.f );
  270. IAnimatedMeshMD2* weaponMesh = (IAnimatedMeshMD2*) smgr->getMesh("gun.md2");
  271. if ( 0 == weaponMesh )
  272. return;
  273. if ( weaponMesh->getMeshType() == EAMT_MD2 )
  274. {
  275. s32 count = weaponMesh->getAnimationCount();
  276. for ( s32 i = 0; i != count; ++i )
  277. {
  278. snprintf_irr ( buf, 64, "Animation: %s", weaponMesh->getAnimationName(i) );
  279. device->getLogger()->log(buf, ELL_INFORMATION);
  280. }
  281. }
  282. WeaponNode = smgr->addAnimatedMeshSceneNode(
  283. weaponMesh,
  284. smgr->getActiveCamera(),
  285. 10,
  286. vector3df( 0, 0, 0),
  287. vector3df(-90,-90,90)
  288. );
  289. WeaponNode->setMaterialFlag(EMF_LIGHTING, false);
  290. WeaponNode->setMaterialTexture(0, driver->getTexture( "gun.jpg"));
  291. WeaponNode->setLoopMode ( false );
  292. WeaponNode->setName ( "tommi the gun man" );
  293. //create a collision auto response animator
  294. ISceneNodeAnimator* anim =
  295. smgr->createCollisionResponseAnimator( meta, camera,
  296. vector3df(30,45,30),
  297. getGravity ( "earth" ),
  298. vector3df(0,40,0),
  299. 0.0005f
  300. );
  301. camera->addAnimator( anim );
  302. anim->drop();
  303. if ( meta )
  304. {
  305. meta->drop ();
  306. }
  307. respawn ();
  308. setAnim ( "idle" );
  309. }
  310. /*
  311. So we need a good starting position in the level.
  312. We can ask the Quake3 loader for all entities with class_name "info_player_deathmatch"
  313. */
  314. void Q3Player::respawn ()
  315. {
  316. if (!Device)
  317. return;
  318. ICameraSceneNode* camera = Device->getSceneManager()->getActiveCamera();
  319. Device->getLogger()->log( "respawn" );
  320. if (StartPositionCurrent >= Q3StartPosition(Mesh, camera,
  321. StartPositionCurrent, cam()->getEllipsoidTranslation()))
  322. StartPositionCurrent = 0;
  323. else
  324. ++StartPositionCurrent;
  325. }
  326. /*
  327. set player position from saved coordinates
  328. */
  329. void Q3Player::setpos ( const vector3df &pos, const vector3df &rotation )
  330. {
  331. if (!Device)
  332. return;
  333. Device->getLogger()->log( "setpos" );
  334. ICameraSceneNode* camera = Device->getSceneManager()->getActiveCamera();
  335. if ( camera )
  336. {
  337. camera->setPosition ( pos );
  338. camera->setRotation ( rotation );
  339. //! New. FPSCamera and animators catches reset on animate 0
  340. camera->OnAnimate ( 0 );
  341. }
  342. }
  343. /* set the animation of the player and weapon
  344. */
  345. void Q3Player::setAnim ( const c8 *name )
  346. {
  347. if ( name )
  348. {
  349. snprintf_irr ( animation, 64, "%s", name );
  350. if ( WeaponNode )
  351. {
  352. WeaponNode->setAnimationEndCallback ( this );
  353. WeaponNode->setMD2Animation ( animation );
  354. }
  355. }
  356. else
  357. {
  358. animation[0] = 0;
  359. if ( WeaponNode )
  360. {
  361. WeaponNode->setAnimationEndCallback ( 0 );
  362. }
  363. }
  364. }
  365. // Callback
  366. void Q3Player::OnAnimationEnd(IAnimatedMeshSceneNode* node)
  367. {
  368. setAnim ( 0 );
  369. }
  370. /* GUI elements
  371. */
  372. struct GUI
  373. {
  374. GUI ()
  375. {
  376. memset ( this, 0, sizeof ( *this ) );
  377. }
  378. void drop()
  379. {
  380. dropElement ( Window );
  381. dropElement ( Logo );
  382. }
  383. IGUIComboBox* VideoDriver;
  384. IGUIComboBox* VideoMode;
  385. IGUICheckBox* FullScreen;
  386. IGUICheckBox* Bit32;
  387. IGUIScrollBar* MultiSample;
  388. IGUIButton* SetVideoMode;
  389. IGUIScrollBar* Tesselation;
  390. IGUIScrollBar* Gamma;
  391. IGUICheckBox* Collision;
  392. IGUICheckBox* Visible_Map;
  393. IGUICheckBox* Visible_Shader;
  394. IGUICheckBox* Visible_Fog;
  395. IGUICheckBox* Visible_Unresolved;
  396. IGUICheckBox* Visible_Skydome;
  397. IGUIButton* Respawn;
  398. IGUITable* ArchiveList;
  399. IGUIButton* ArchiveAdd;
  400. IGUIButton* ArchiveRemove;
  401. IGUIFileOpenDialog* ArchiveFileOpen;
  402. IGUIButton* ArchiveUp;
  403. IGUIButton* ArchiveDown;
  404. IGUIListBox* MapList;
  405. IGUITreeView* SceneTree;
  406. IGUIStaticText* StatusLine;
  407. IGUIImage* Logo;
  408. IGUIWindow* Window;
  409. };
  410. /*
  411. CQuake3EventHandler controls the game
  412. */
  413. class CQuake3EventHandler : public IEventReceiver
  414. {
  415. public:
  416. CQuake3EventHandler( GameData *gameData );
  417. virtual ~CQuake3EventHandler ();
  418. void Animate();
  419. void Render();
  420. void AddArchive ( const path& archiveName );
  421. void LoadMap ( const stringw& mapName, s32 collision );
  422. void CreatePlayers();
  423. void AddSky( u32 dome, const c8 *texture );
  424. Q3Player *GetPlayer ( u32 index ) { return &Player[index]; }
  425. void CreateGUI();
  426. void SetGUIActive( s32 command);
  427. bool OnEvent(const SEvent& eve);
  428. private:
  429. GameData *Game;
  430. IQ3LevelMesh* Mesh;
  431. ISceneNode* MapParent;
  432. ISceneNode* ShaderParent;
  433. ISceneNode* ItemParent;
  434. ISceneNode* UnresolvedParent;
  435. ISceneNode* BulletParent;
  436. ISceneNode* FogParent;
  437. ISceneNode * SkyNode;
  438. IMetaTriangleSelector *Meta;
  439. c8 buf[256];
  440. Q3Player Player[2];
  441. struct SParticleImpact
  442. {
  443. u32 when;
  444. vector3df pos;
  445. vector3df outVector;
  446. };
  447. array<SParticleImpact> Impacts;
  448. void useItem( Q3Player * player);
  449. void createParticleImpacts( u32 now );
  450. void createTextures ();
  451. void addSceneTreeItem( ISceneNode * parent, IGUITreeViewNode* nodeParent);
  452. GUI gui;
  453. void dropMap ();
  454. };
  455. /* Constructor
  456. */
  457. CQuake3EventHandler::CQuake3EventHandler( GameData *game )
  458. : Game(game), Mesh(0), MapParent(0), ShaderParent(0), ItemParent(0), UnresolvedParent(0),
  459. BulletParent(0), FogParent(0), SkyNode(0), Meta(0)
  460. {
  461. buf[0]=0;
  462. // Also use 16 bit textures for 16 bit RenderDevice
  463. if ( Game->deviceParam.Bits == 16 )
  464. {
  465. game->Device->getVideoDriver()->setTextureCreationFlag(ETCF_ALWAYS_16_BIT, true);
  466. }
  467. // Quake3 shader controls Z-writing
  468. game->Device->getSceneManager()->getParameters()->setAttribute(scene::ALLOW_ZWRITE_ON_TRANSPARENT, true);
  469. // create internal textures
  470. createTextures ();
  471. sound_init ( game->Device );
  472. Game->Device->setEventReceiver ( this );
  473. }
  474. // destructor
  475. CQuake3EventHandler::~CQuake3EventHandler ()
  476. {
  477. Player[0].shutdown ();
  478. sound_shutdown ();
  479. Game->save( "explorer.cfg" );
  480. Game->Device->drop();
  481. }
  482. // create runtime textures smog, fog
  483. void CQuake3EventHandler::createTextures()
  484. {
  485. IVideoDriver * driver = Game->Device->getVideoDriver();
  486. dimension2du dim(64, 64);
  487. video::IImage* image;
  488. u32 i;
  489. for ( i = 0; i != 8; ++i )
  490. {
  491. image = driver->createImage ( video::ECF_A8R8G8B8, dim);
  492. image->fill(SColor(0xFFFFFFFF));
  493. snprintf_irr ( buf, 64, "smoke_%02d", i );
  494. driver->addTexture( buf, image );
  495. image->drop ();
  496. }
  497. // fog
  498. for ( i = 0; i != 1; ++i )
  499. {
  500. image = driver->createImage ( video::ECF_A8R8G8B8, dim);
  501. image->fill(SColor(0xFFFFFFFF));
  502. snprintf_irr ( buf, 64, "fog_%02d", i );
  503. driver->addTexture( buf, image );
  504. image->drop ();
  505. }
  506. }
  507. /*
  508. create the GUI
  509. */
  510. void CQuake3EventHandler::CreateGUI()
  511. {
  512. IGUIEnvironment *env = Game->Device->getGUIEnvironment();
  513. IVideoDriver * driver = Game->Device->getVideoDriver();
  514. gui.drop();
  515. // set skin font
  516. IGUIFont* font = env->getFont("fontlucida.png");
  517. if (font)
  518. env->getSkin()->setFont(font);
  519. env->getSkin()->setColor ( EGDC_BUTTON_TEXT, video::SColor(240,0xAA,0xAA,0xAA) );
  520. env->getSkin()->setColor ( EGDC_3D_HIGH_LIGHT, video::SColor(240,0x22,0x22,0x22) );
  521. env->getSkin()->setColor ( EGDC_3D_FACE, video::SColor(240,0x44,0x44,0x44) );
  522. env->getSkin()->setColor ( EGDC_EDITABLE, video::SColor(240,0x44,0x44,0x44) );
  523. env->getSkin()->setColor ( EGDC_FOCUSED_EDITABLE, video::SColor(240,0x54,0x54,0x54) );
  524. env->getSkin()->setColor ( EGDC_WINDOW, video::SColor(240,0x66,0x66,0x66) );
  525. // minimal gui size 800x600
  526. dimension2d<u32> dim ( 800, 600 );
  527. gui.Window = env->addWindow ( rect<s32> ( 0, 0, dim.Width, dim.Height ), false, L"Quake3 Explorer" );
  528. gui.Window->setToolTipText ( L"Quake3Explorer. Loads and show various BSP File Format and Shaders." );
  529. gui.Window->getCloseButton()->setToolTipText ( L"Quit Quake3 Explorer" );
  530. // add a status line help text
  531. gui.StatusLine = env->addStaticText( 0, rect<s32>( 5,dim.Height - 30,dim.Width - 5,dim.Height - 10),
  532. false, false, gui.Window, -1, true
  533. );
  534. env->addStaticText ( L"VideoDriver:", rect<s32>( dim.Width - 400, 24, dim.Width - 310, 40 ),false, false, gui.Window, -1, false );
  535. gui.VideoDriver = env->addComboBox(rect<s32>( dim.Width - 300, 24, dim.Width - 10, 40 ),gui.Window);
  536. gui.VideoDriver->addItem(L"Direct3D 9.0c", EDT_DIRECT3D9 );
  537. gui.VideoDriver->addItem(L"OpenGL 1.5", EDT_OPENGL);
  538. gui.VideoDriver->addItem(L"Software Renderer", EDT_SOFTWARE);
  539. gui.VideoDriver->addItem(L"Burning's Video (TM) Thomas Alten", EDT_BURNINGSVIDEO);
  540. gui.VideoDriver->setSelected ( gui.VideoDriver->getIndexForItemData ( Game->deviceParam.DriverType ) );
  541. gui.VideoDriver->setToolTipText ( L"Use a VideoDriver" );
  542. env->addStaticText ( L"VideoMode:", rect<s32>( dim.Width - 400, 44, dim.Width - 310, 60 ),false, false, gui.Window, -1, false );
  543. gui.VideoMode = env->addComboBox(rect<s32>( dim.Width - 300, 44, dim.Width - 10, 60 ),gui.Window);
  544. gui.VideoMode->setToolTipText ( L"Supported Screenmodes" );
  545. IVideoModeList *modeList = Game->Device->getVideoModeList();
  546. if ( modeList )
  547. {
  548. s32 i;
  549. for ( i = 0; i != modeList->getVideoModeCount (); ++i )
  550. {
  551. u16 d = modeList->getVideoModeDepth ( i );
  552. if ( d < 16 )
  553. continue;
  554. u16 w = modeList->getVideoModeResolution ( i ).Width;
  555. u16 h = modeList->getVideoModeResolution ( i ).Height;
  556. u32 val = w << 16 | h;
  557. if ( gui.VideoMode->getIndexForItemData ( val ) >= 0 )
  558. continue;
  559. f32 aspect = (f32) w / (f32) h;
  560. const c8 *a = "";
  561. if ( core::equals ( aspect, 1.3333333333f ) ) a = "4:3";
  562. else if ( core::equals ( aspect, 1.6666666f ) ) a = "15:9 widescreen";
  563. else if ( core::equals ( aspect, 1.7777777f ) ) a = "16:9 widescreen";
  564. else if ( core::equals ( aspect, 1.6f ) ) a = "16:10 widescreen";
  565. else if ( core::equals ( aspect, 2.133333f ) ) a = "20:9 widescreen";
  566. snprintf_irr ( buf, sizeof ( buf ), "%d x %d, %s",w, h, a );
  567. gui.VideoMode->addItem ( stringw ( buf ).c_str(), val );
  568. }
  569. }
  570. gui.VideoMode->setSelected ( gui.VideoMode->getIndexForItemData (
  571. Game->deviceParam.WindowSize.Width << 16 |
  572. Game->deviceParam.WindowSize.Height ) );
  573. gui.FullScreen = env->addCheckBox ( Game->deviceParam.Fullscreen, rect<s32>( dim.Width - 400, 64, dim.Width - 300, 80 ), gui.Window,-1, L"Fullscreen" );
  574. gui.FullScreen->setToolTipText ( L"Set Fullscreen or Window Mode" );
  575. gui.Bit32 = env->addCheckBox ( Game->deviceParam.Bits == 32, rect<s32>( dim.Width - 300, 64, dim.Width - 240, 80 ), gui.Window,-1, L"32Bit" );
  576. gui.Bit32->setToolTipText ( L"Use 16 or 32 Bit" );
  577. env->addStaticText ( L"MultiSample:", rect<s32>( dim.Width - 235, 64, dim.Width - 150, 80 ),false, false, gui.Window, -1, false );
  578. gui.MultiSample = env->addScrollBar( true, rect<s32>( dim.Width - 150, 64, dim.Width - 70, 80 ), gui.Window,-1 );
  579. gui.MultiSample->setMin ( 0 );
  580. gui.MultiSample->setMax ( 8 );
  581. gui.MultiSample->setSmallStep ( 1 );
  582. gui.MultiSample->setLargeStep ( 1 );
  583. gui.MultiSample->setPos ( Game->deviceParam.AntiAlias );
  584. gui.MultiSample->setToolTipText ( L"Set the MultiSample (disable, 1x, 2x, 4x, 8x )" );
  585. gui.SetVideoMode = env->addButton (rect<s32>( dim.Width - 60, 64, dim.Width - 10, 80 ), gui.Window, -1,L"set" );
  586. gui.SetVideoMode->setToolTipText ( L"Set Video Mode with current values" );
  587. env->addStaticText ( L"Gamma:", rect<s32>( dim.Width - 400, 104, dim.Width - 310, 120 ),false, false, gui.Window, -1, false );
  588. gui.Gamma = env->addScrollBar( true, rect<s32>( dim.Width - 300, 104, dim.Width - 10, 120 ), gui.Window,-1 );
  589. gui.Gamma->setMin ( 50 );
  590. gui.Gamma->setMax ( 350 );
  591. gui.Gamma->setSmallStep ( 1 );
  592. gui.Gamma->setLargeStep ( 10 );
  593. gui.Gamma->setPos ( core::floor32 ( Game->GammaValue * 100.f ) );
  594. gui.Gamma->setToolTipText ( L"Adjust Gamma Ramp ( 0.5 - 3.5)" );
  595. Game->Device->setGammaRamp ( Game->GammaValue, Game->GammaValue, Game->GammaValue, 0.f, 0.f );
  596. env->addStaticText ( L"Tesselation:", rect<s32>( dim.Width - 400, 124, dim.Width - 310, 140 ),false, false, gui.Window, -1, false );
  597. gui.Tesselation = env->addScrollBar( true, rect<s32>( dim.Width - 300, 124, dim.Width - 10, 140 ), gui.Window,-1 );
  598. gui.Tesselation->setMin ( 2 );
  599. gui.Tesselation->setMax ( 12 );
  600. gui.Tesselation->setSmallStep ( 1 );
  601. gui.Tesselation->setLargeStep ( 1 );
  602. gui.Tesselation->setPos ( Game->loadParam.patchTessellation );
  603. gui.Tesselation->setToolTipText ( L"How smooth should curved surfaces be rendered" );
  604. gui.Collision = env->addCheckBox ( true, rect<s32>( dim.Width - 400, 150, dim.Width - 300, 166 ), gui.Window,-1, L"Collision" );
  605. gui.Collision->setToolTipText ( L"Set collision on or off ( flythrough ). \nPress F7 on your Keyboard" );
  606. gui.Visible_Map = env->addCheckBox ( true, rect<s32>( dim.Width - 300, 150, dim.Width - 240, 166 ), gui.Window,-1, L"Map" );
  607. gui.Visible_Map->setToolTipText ( L"Show or not show the static part the Level. \nPress F3 on your Keyboard" );
  608. gui.Visible_Shader = env->addCheckBox ( true, rect<s32>( dim.Width - 240, 150, dim.Width - 170, 166 ), gui.Window,-1, L"Shader" );
  609. gui.Visible_Shader->setToolTipText ( L"Show or not show the Shader Nodes. \nPress F4 on your Keyboard" );
  610. gui.Visible_Fog = env->addCheckBox ( true, rect<s32>( dim.Width - 170, 150, dim.Width - 110, 166 ), gui.Window,-1, L"Fog" );
  611. gui.Visible_Fog->setToolTipText ( L"Show or not show the Fog Nodes. \nPress F5 on your Keyboard" );
  612. gui.Visible_Unresolved = env->addCheckBox ( true, rect<s32>( dim.Width - 110, 150, dim.Width - 10, 166 ), gui.Window,-1, L"Unresolved" );
  613. gui.Visible_Unresolved->setToolTipText ( L"Show the or not show the Nodes the Engine can't handle. \nPress F6 on your Keyboard" );
  614. gui.Visible_Skydome = env->addCheckBox ( true, rect<s32>( dim.Width - 110, 180, dim.Width - 10, 196 ), gui.Window,-1, L"Skydome" );
  615. gui.Visible_Skydome->setToolTipText ( L"Show the or not show the Skydome." );
  616. //Respawn = env->addButton ( rect<s32>( dim.Width - 260, 90, dim.Width - 10, 106 ), 0,-1, L"Respawn" );
  617. env->addStaticText ( L"Archives:", rect<s32>( 5, dim.Height - 530, dim.Width - 600,dim.Height - 514 ),false, false, gui.Window, -1, false );
  618. gui.ArchiveAdd = env->addButton ( rect<s32>( dim.Width - 725, dim.Height - 530, dim.Width - 665, dim.Height - 514 ), gui.Window,-1, L"add" );
  619. gui.ArchiveAdd->setToolTipText ( L"Add an archive, usually packed zip-archives (*.pk3) to the Filesystem" );
  620. gui.ArchiveRemove = env->addButton ( rect<s32>( dim.Width - 660, dim.Height - 530, dim.Width - 600, dim.Height - 514 ), gui.Window,-1, L"del" );
  621. gui.ArchiveRemove->setToolTipText ( L"Remove the selected archive from the FileSystem." );
  622. gui.ArchiveUp = env->addButton ( rect<s32>( dim.Width - 575, dim.Height - 530, dim.Width - 515, dim.Height - 514 ), gui.Window,-1, L"up" );
  623. gui.ArchiveUp->setToolTipText ( L"Arrange Archive Look-up Hirachy. Move the selected Archive up" );
  624. gui.ArchiveDown = env->addButton ( rect<s32>( dim.Width - 510, dim.Height - 530, dim.Width - 440, dim.Height - 514 ), gui.Window,-1, L"down" );
  625. gui.ArchiveDown->setToolTipText ( L"Arrange Archive Look-up Hirachy. Move the selected Archive down" );
  626. gui.ArchiveList = env->addTable ( rect<s32>( 5,dim.Height - 510, dim.Width - 450,dim.Height - 410 ), gui.Window );
  627. gui.ArchiveList->addColumn ( L"Type", 0 );
  628. gui.ArchiveList->addColumn ( L"Real File Path", 1 );
  629. gui.ArchiveList->setColumnWidth ( 0, 60 );
  630. gui.ArchiveList->setColumnWidth ( 1, 284 );
  631. gui.ArchiveList->setToolTipText ( L"Show the attached Archives" );
  632. env->addStaticText ( L"Maps:", rect<s32>( 5, dim.Height - 400, dim.Width - 450,dim.Height - 380 ),false, false, gui.Window, -1, false );
  633. gui.MapList = env->addListBox ( rect<s32>( 5,dim.Height - 380, dim.Width - 450,dim.Height - 40 ), gui.Window, -1, true );
  634. gui.MapList->setToolTipText ( L"Show the current Maps in all Archives.\n Double-Click the Map to start the level" );
  635. // create a visible Scene Tree
  636. env->addStaticText ( L"Scenegraph:", rect<s32>( dim.Width - 400, dim.Height - 400, dim.Width - 5,dim.Height - 380 ),false, false, gui.Window, -1, false );
  637. gui.SceneTree = env->addTreeView( rect<s32>( dim.Width - 400, dim.Height - 380, dim.Width - 5, dim.Height - 40 ),
  638. gui.Window, -1, true, true, false );
  639. gui.SceneTree->setToolTipText ( L"Show the current Scenegraph" );
  640. gui.SceneTree->getRoot()->clearChildren();
  641. addSceneTreeItem ( Game->Device->getSceneManager()->getRootSceneNode(), gui.SceneTree->getRoot() );
  642. IGUIImageList* imageList = env->createImageList( driver->getTexture ( "iconlist.png" ),
  643. dimension2di( 32, 32 ), true );
  644. if ( imageList )
  645. {
  646. gui.SceneTree->setImageList( imageList );
  647. imageList->drop ();
  648. }
  649. // load the engine logo
  650. gui.Logo = env->addImage( driver->getTexture("irrlichtlogo3.png"), position2d<s32>(5, 16 ), true, 0 );
  651. gui.Logo->setToolTipText ( L"The great Irrlicht Engine" );
  652. AddArchive ( "" );
  653. }
  654. /*
  655. Add an Archive to the FileSystems and updates the GUI
  656. */
  657. void CQuake3EventHandler::AddArchive ( const path& archiveName )
  658. {
  659. IFileSystem *fs = Game->Device->getFileSystem();
  660. u32 i;
  661. if ( archiveName.size () )
  662. {
  663. bool exists = false;
  664. for ( i = 0; i != fs->getFileArchiveCount(); ++i )
  665. {
  666. if ( fs->getFileArchive(i)->getFileList()->getPath() == archiveName )
  667. {
  668. exists = true;
  669. break;
  670. }
  671. }
  672. if (!exists)
  673. {
  674. fs->addFileArchive(archiveName, true, false);
  675. }
  676. }
  677. // store the current archives in game data
  678. // show the attached archive in proper order
  679. if ( gui.ArchiveList )
  680. {
  681. gui.ArchiveList->clearRows();
  682. for ( i = 0; i != fs->getFileArchiveCount(); ++i )
  683. {
  684. IFileArchive * archive = fs->getFileArchive ( i );
  685. u32 index = gui.ArchiveList->addRow(i);
  686. core::stringw typeName;
  687. switch(archive->getType())
  688. {
  689. case io::EFAT_ZIP:
  690. typeName = "ZIP";
  691. break;
  692. case io::EFAT_GZIP:
  693. typeName = "gzip";
  694. break;
  695. case io::EFAT_FOLDER:
  696. typeName = "Mount";
  697. break;
  698. case io::EFAT_PAK:
  699. typeName = "PAK";
  700. break;
  701. case io::EFAT_TAR:
  702. typeName = "TAR";
  703. break;
  704. default:
  705. typeName = "archive";
  706. }
  707. gui.ArchiveList->setCellText ( index, 0, typeName );
  708. gui.ArchiveList->setCellText ( index, 1, archive->getFileList()->getPath() );
  709. }
  710. }
  711. // browse the archives for maps
  712. if ( gui.MapList )
  713. {
  714. gui.MapList->clear();
  715. IGUISpriteBank *bank = Game->Device->getGUIEnvironment()->getSpriteBank("sprite_q3map");
  716. if ( 0 == bank )
  717. bank = Game->Device->getGUIEnvironment()->addEmptySpriteBank("sprite_q3map");
  718. SGUISprite sprite;
  719. SGUISpriteFrame frame;
  720. core::rect<s32> r;
  721. bank->getSprites().clear();
  722. bank->getPositions().clear ();
  723. gui.MapList->setSpriteBank ( bank );
  724. u32 g = 0;
  725. core::stringw s;
  726. // browse the attached file system
  727. fs->setFileListSystem ( FILESYSTEM_VIRTUAL );
  728. fs->changeWorkingDirectoryTo ( "/maps/" );
  729. IFileList *fileList = fs->createFileList ();
  730. fs->setFileListSystem ( FILESYSTEM_NATIVE );
  731. for ( i=0; i< fileList->getFileCount(); ++i)
  732. {
  733. s = fileList->getFullFileName(i);
  734. if ( s.find ( ".bsp" ) >= 0 )
  735. {
  736. // get level screenshot. reformat texture to 128x128
  737. path c ( s );
  738. deletePathFromFilename ( c );
  739. cutFilenameExtension ( c, c );
  740. c = path ( "levelshots/" ) + c;
  741. dimension2du dim ( 128, 128 );
  742. IVideoDriver * driver = Game->Device->getVideoDriver();
  743. IImage* image = 0;
  744. ITexture *tex = 0;
  745. path filename;
  746. filename = c + ".jpg";
  747. if ( fs->existFile ( filename ) )
  748. image = driver->createImageFromFile( filename );
  749. if ( 0 == image )
  750. {
  751. filename = c + ".tga";
  752. if ( fs->existFile ( filename ) )
  753. image = driver->createImageFromFile( filename );
  754. }
  755. if ( image )
  756. {
  757. IImage* filter = driver->createImage ( video::ECF_R8G8B8, dim );
  758. image->copyToScalingBoxFilter ( filter, 0 );
  759. image->drop ();
  760. image = filter;
  761. }
  762. if ( image )
  763. {
  764. tex = driver->addTexture ( filename, image );
  765. image->drop ();
  766. }
  767. bank->setTexture ( g, tex );
  768. r.LowerRightCorner.X = dim.Width;
  769. r.LowerRightCorner.Y = dim.Height;
  770. gui.MapList->setItemHeight ( r.LowerRightCorner.Y + 4 );
  771. frame.rectNumber = bank->getPositions().size();
  772. frame.textureNumber = g;
  773. bank->getPositions().push_back(r);
  774. sprite.Frames.set_used ( 0 );
  775. sprite.Frames.push_back(frame);
  776. sprite.frameTime = 0;
  777. bank->getSprites().push_back(sprite);
  778. gui.MapList->addItem ( s.c_str (), g );
  779. g += 1;
  780. }
  781. }
  782. fileList->drop ();
  783. gui.MapList->setSelected ( -1 );
  784. IGUIScrollBar * bar = (IGUIScrollBar*)gui.MapList->getElementFromId( 0 );
  785. if ( bar )
  786. bar->setPos ( 0 );
  787. }
  788. }
  789. /*
  790. clears the map in memory
  791. */
  792. void CQuake3EventHandler::dropMap ()
  793. {
  794. IVideoDriver * driver = Game->Device->getVideoDriver();
  795. driver->removeAllHardwareBuffers ();
  796. driver->removeAllTextures ();
  797. Player[0].shutdown ();
  798. dropElement ( ItemParent );
  799. dropElement ( ShaderParent );
  800. dropElement ( UnresolvedParent );
  801. dropElement ( FogParent );
  802. dropElement ( BulletParent );
  803. Impacts.clear();
  804. if ( Meta )
  805. {
  806. Meta = 0;
  807. }
  808. dropElement ( MapParent );
  809. dropElement ( SkyNode );
  810. // clean out meshes, because textures are invalid
  811. // TODO: better texture handling;-)
  812. IMeshCache *cache = Game->Device->getSceneManager ()->getMeshCache();
  813. cache->clear ();
  814. Mesh = 0;
  815. }
  816. /* Load new map
  817. */
  818. void CQuake3EventHandler::LoadMap ( const stringw &mapName, s32 collision )
  819. {
  820. if ( 0 == mapName.size() )
  821. return;
  822. dropMap ();
  823. IFileSystem *fs = Game->Device->getFileSystem();
  824. ISceneManager *smgr = Game->Device->getSceneManager ();
  825. IReadFile* file = fs->createMemoryReadFile(&Game->loadParam,
  826. sizeof(Game->loadParam), L"levelparameter.cfg", false);
  827. // load cfg file
  828. smgr->getMesh( file );
  829. file->drop ();
  830. // load the actual map
  831. Mesh = (IQ3LevelMesh*) smgr->getMesh(mapName);
  832. if ( 0 == Mesh )
  833. return;
  834. /*
  835. add the geometry mesh to the Scene ( polygon & patches )
  836. The Geometry mesh is optimised for faster drawing
  837. */
  838. IMesh *geometry = Mesh->getMesh(E_Q3_MESH_GEOMETRY);
  839. if ( 0 == geometry || geometry->getMeshBufferCount() == 0)
  840. return;
  841. Game->CurrentMapName = mapName;
  842. //create a collision list
  843. Meta = 0;
  844. ITriangleSelector * selector = 0;
  845. if (collision)
  846. Meta = smgr->createMetaTriangleSelector();
  847. //IMeshBuffer *b0 = geometry->getMeshBuffer(0);
  848. //s32 minimalNodes = b0 ? core::s32_max ( 2048, b0->getVertexCount() / 32 ) : 2048;
  849. s32 minimalNodes = 2048;
  850. MapParent = smgr->addOctreeSceneNode(geometry, 0, -1, minimalNodes);
  851. MapParent->setName ( mapName );
  852. if ( Meta )
  853. {
  854. selector = smgr->createOctreeTriangleSelector( geometry,MapParent, minimalNodes);
  855. //selector = smgr->createTriangleSelector ( geometry, MapParent );
  856. Meta->addTriangleSelector( selector);
  857. selector->drop ();
  858. }
  859. // logical parent for the items
  860. ItemParent = smgr->addEmptySceneNode();
  861. if ( ItemParent )
  862. ItemParent->setName ( "Item Container" );
  863. ShaderParent = smgr->addEmptySceneNode();
  864. if ( ShaderParent )
  865. ShaderParent->setName ( "Shader Container" );
  866. UnresolvedParent = smgr->addEmptySceneNode();
  867. if ( UnresolvedParent )
  868. UnresolvedParent->setName ( "Unresolved Container" );
  869. FogParent = smgr->addEmptySceneNode();
  870. if ( FogParent )
  871. FogParent->setName ( "Fog Container" );
  872. // logical parent for the bullets
  873. BulletParent = smgr->addEmptySceneNode();
  874. if ( BulletParent )
  875. BulletParent->setName ( "Bullet Container" );
  876. /*
  877. now construct SceneNodes for each shader
  878. The objects are stored in the quake mesh E_Q3_MESH_ITEMS
  879. and the shader ID is stored in the MaterialParameters
  880. mostly dark looking skulls and moving lava.. or green flashing tubes?
  881. */
  882. Q3ShaderFactory ( Game->loadParam, Game->Device, Mesh, E_Q3_MESH_ITEMS,ShaderParent, Meta, false );
  883. Q3ShaderFactory ( Game->loadParam, Game->Device, Mesh, E_Q3_MESH_FOG,FogParent, 0, false );
  884. Q3ShaderFactory ( Game->loadParam, Game->Device, Mesh, E_Q3_MESH_UNRESOLVED,UnresolvedParent, Meta, true );
  885. /*
  886. Now construct models from entity list
  887. */
  888. Q3ModelFactory ( Game->loadParam, Game->Device, Mesh, ItemParent, false );
  889. }
  890. /*
  891. Adds a SceneNode with an icon to the scene tree
  892. */
  893. void CQuake3EventHandler::addSceneTreeItem( ISceneNode * parent, IGUITreeViewNode* nodeParent)
  894. {
  895. IGUITreeViewNode* node;
  896. wchar_t msg[128];
  897. s32 imageIndex;
  898. list<ISceneNode*>::ConstIterator it = parent->getChildren().begin();
  899. for (; it != parent->getChildren().end(); ++it)
  900. {
  901. switch ( (*it)->getType () )
  902. {
  903. case ESNT_Q3SHADER_SCENE_NODE: imageIndex = 0; break;
  904. case ESNT_CAMERA: imageIndex = 1; break;
  905. case ESNT_EMPTY: imageIndex = 2; break;
  906. case ESNT_MESH: imageIndex = 3; break;
  907. case ESNT_OCTREE: imageIndex = 3; break;
  908. case ESNT_ANIMATED_MESH: imageIndex = 4; break;
  909. case ESNT_SKY_BOX: imageIndex = 5; break;
  910. case ESNT_BILLBOARD: imageIndex = 6; break;
  911. case ESNT_PARTICLE_SYSTEM: imageIndex = 7; break;
  912. case ESNT_TEXT: imageIndex = 8; break;
  913. default:imageIndex = -1; break;
  914. }
  915. if ( imageIndex < 0 )
  916. {
  917. swprintf_irr ( msg, 128, L"%hs,%hs",
  918. Game->Device->getSceneManager ()->getSceneNodeTypeName ( (*it)->getType () ),
  919. (*it)->getName()
  920. );
  921. }
  922. else
  923. {
  924. swprintf_irr ( msg, 128, L"%hs",(*it)->getName() );
  925. }
  926. node = nodeParent->addChildBack( msg, 0, imageIndex );
  927. // Add all animators
  928. list<ISceneNodeAnimator*>::ConstIterator ait = (*it)->getAnimators().begin();
  929. for (; ait != (*it)->getAnimators().end(); ++ait)
  930. {
  931. imageIndex = -1;
  932. swprintf_irr ( msg, 128, L"%hs",
  933. Game->Device->getSceneManager ()->getAnimatorTypeName ( (*ait)->getType () )
  934. );
  935. switch ( (*ait)->getType () )
  936. {
  937. case ESNAT_FLY_CIRCLE:
  938. case ESNAT_FLY_STRAIGHT:
  939. case ESNAT_FOLLOW_SPLINE:
  940. case ESNAT_ROTATION:
  941. case ESNAT_TEXTURE:
  942. case ESNAT_DELETION:
  943. case ESNAT_COLLISION_RESPONSE:
  944. case ESNAT_CAMERA_FPS:
  945. case ESNAT_CAMERA_MAYA:
  946. default:
  947. break;
  948. }
  949. node->addChildBack( msg, 0, imageIndex );
  950. }
  951. addSceneTreeItem ( *it, node );
  952. }
  953. }
  954. // Adds life!
  955. void CQuake3EventHandler::CreatePlayers()
  956. {
  957. Player[0].create ( Game->Device, Mesh, MapParent, Meta );
  958. }
  959. // Adds a skydome to the scene
  960. void CQuake3EventHandler::AddSky( u32 dome, const c8 *texture)
  961. {
  962. ISceneManager *smgr = Game->Device->getSceneManager ();
  963. IVideoDriver * driver = Game->Device->getVideoDriver();
  964. bool oldMipMapState = driver->getTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS);
  965. driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
  966. if ( 0 == dome )
  967. {
  968. // irrlicht order
  969. //static const c8*p[] = { "ft", "lf", "bk", "rt", "up", "dn" };
  970. // quake3 order
  971. static const c8*p[] = { "ft", "rt", "bk", "lf", "up", "dn" };
  972. u32 i = 0;
  973. snprintf_irr ( buf, 64, "%s_%s.jpg", texture, p[i] );
  974. SkyNode = smgr->addSkyBoxSceneNode( driver->getTexture ( buf ), 0, 0, 0, 0, 0 );
  975. if (SkyNode)
  976. {
  977. for ( i = 0; i < 6; ++i )
  978. {
  979. snprintf_irr ( buf, 64, "%s_%s.jpg", texture, p[i] );
  980. SkyNode->getMaterial(i).setTexture ( 0, driver->getTexture ( buf ) );
  981. }
  982. }
  983. }
  984. else
  985. if ( 1 == dome )
  986. {
  987. snprintf_irr ( buf, 64, "%s.jpg", texture );
  988. SkyNode = smgr->addSkyDomeSceneNode(
  989. driver->getTexture( buf ), 32,32,
  990. 1.f, 1.f, 1000.f, 0, 11);
  991. }
  992. else
  993. if ( 2 == dome )
  994. {
  995. snprintf_irr ( buf, 64, "%s.jpg", texture );
  996. SkyNode = smgr->addSkyDomeSceneNode(
  997. driver->getTexture( buf ), 16,8,
  998. 0.95f, 2.f, 1000.f, 0, 11);
  999. }
  1000. if (SkyNode)
  1001. SkyNode->setName("Skydome");
  1002. //SkyNode->getMaterial(0).ZBuffer = video::EMDF_DEPTH_LESS_EQUAL;
  1003. driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, oldMipMapState);
  1004. }
  1005. // enable GUI elements
  1006. void CQuake3EventHandler::SetGUIActive( s32 command)
  1007. {
  1008. bool inputState = false;
  1009. ICameraSceneNode * camera = Game->Device->getSceneManager()->getActiveCamera ();
  1010. switch ( command )
  1011. {
  1012. case 0: Game->guiActive = 0; inputState = !Game->guiActive; break;
  1013. case 1: Game->guiActive = 1; inputState = !Game->guiActive;;break;
  1014. case 2: Game->guiActive ^= 1; inputState = !Game->guiActive;break;
  1015. case 3:
  1016. if ( camera )
  1017. inputState = !camera->isInputReceiverEnabled();
  1018. break;
  1019. }
  1020. if ( camera )
  1021. {
  1022. camera->setInputReceiverEnabled ( inputState );
  1023. Game->Device->getCursorControl()->setVisible( !inputState );
  1024. }
  1025. if ( gui.Window )
  1026. {
  1027. gui.Window->setVisible ( Game->guiActive != 0 );
  1028. }
  1029. if ( Game->guiActive &&
  1030. gui.SceneTree && Game->Device->getGUIEnvironment()->getFocus() != gui.SceneTree
  1031. )
  1032. {
  1033. gui.SceneTree->getRoot()->clearChildren();
  1034. addSceneTreeItem ( Game->Device->getSceneManager()->getRootSceneNode(), gui.SceneTree->getRoot() );
  1035. }
  1036. Game->Device->getGUIEnvironment()->setFocus ( Game->guiActive ? gui.Window: 0 );
  1037. }
  1038. /*
  1039. Handle game input
  1040. */
  1041. bool CQuake3EventHandler::OnEvent(const SEvent& eve)
  1042. {
  1043. if ( eve.EventType == EET_LOG_TEXT_EVENT )
  1044. {
  1045. return false;
  1046. }
  1047. if ( Game->guiActive && eve.EventType == EET_GUI_EVENT )
  1048. {
  1049. if ( eve.GUIEvent.Caller == gui.MapList && eve.GUIEvent.EventType == gui::EGET_LISTBOX_SELECTED_AGAIN )
  1050. {
  1051. s32 selected = gui.MapList->getSelected();
  1052. if ( selected >= 0 )
  1053. {
  1054. stringw loadMap = gui.MapList->getListItem ( selected );
  1055. if ( 0 == MapParent || loadMap != Game->CurrentMapName )
  1056. {
  1057. printf ( "Loading map %ls\n", loadMap.c_str() );
  1058. LoadMap ( loadMap , 1 );
  1059. if ( 0 == Game->loadParam.loadSkyShader )
  1060. {
  1061. AddSky ( 1, "skydome2" );
  1062. }
  1063. CreatePlayers ();
  1064. CreateGUI ();
  1065. SetGUIActive ( 0 );
  1066. return true;
  1067. }
  1068. }
  1069. }
  1070. else
  1071. if ( eve.GUIEvent.Caller == gui.ArchiveRemove && eve.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED )
  1072. {
  1073. Game->Device->getFileSystem()->removeFileArchive( gui.ArchiveList->getSelected() );
  1074. Game->CurrentMapName = "";
  1075. AddArchive ( "" );
  1076. }
  1077. else
  1078. if ( eve.GUIEvent.Caller == gui.ArchiveAdd && eve.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED )
  1079. {
  1080. if ( 0 == gui.ArchiveFileOpen )
  1081. {
  1082. Game->Device->getFileSystem()->setFileListSystem ( FILESYSTEM_NATIVE );
  1083. gui.ArchiveFileOpen = Game->Device->getGUIEnvironment()->addFileOpenDialog ( L"Add Game Archive" , false,gui.Window );
  1084. }
  1085. }
  1086. else
  1087. if ( eve.GUIEvent.Caller == gui.ArchiveFileOpen && eve.GUIEvent.EventType == gui::EGET_FILE_SELECTED )
  1088. {
  1089. AddArchive ( gui.ArchiveFileOpen->getFileNameP() );
  1090. gui.ArchiveFileOpen = 0;
  1091. }
  1092. else
  1093. if ( eve.GUIEvent.Caller == gui.ArchiveFileOpen && eve.GUIEvent.EventType == gui::EGET_DIRECTORY_SELECTED )
  1094. {
  1095. AddArchive ( gui.ArchiveFileOpen->getDirectoryName() );
  1096. }
  1097. else
  1098. if ( eve.GUIEvent.Caller == gui.ArchiveFileOpen && eve.GUIEvent.EventType == gui::EGET_FILE_CHOOSE_DIALOG_CANCELLED )
  1099. {
  1100. gui.ArchiveFileOpen = 0;
  1101. }
  1102. else
  1103. if ( ( eve.GUIEvent.Caller == gui.ArchiveUp || eve.GUIEvent.Caller == gui.ArchiveDown ) &&
  1104. eve.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED )
  1105. {
  1106. s32 rel = eve.GUIEvent.Caller == gui.ArchiveUp ? -1 : 1;
  1107. if ( Game->Device->getFileSystem()->moveFileArchive ( gui.ArchiveList->getSelected (), rel ) )
  1108. {
  1109. s32 newIndex = core::s32_clamp ( gui.ArchiveList->getSelected() + rel, 0, gui.ArchiveList->getRowCount() - 1 );
  1110. AddArchive ( "" );
  1111. gui.ArchiveList->setSelected ( newIndex );
  1112. Game->CurrentMapName = "";
  1113. }
  1114. }
  1115. else
  1116. if ( eve.GUIEvent.Caller == gui.VideoDriver && eve.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED )
  1117. {
  1118. Game->deviceParam.DriverType = (E_DRIVER_TYPE) gui.VideoDriver->getItemData ( gui.VideoDriver->getSelected() );
  1119. }
  1120. else
  1121. if ( eve.GUIEvent.Caller == gui.VideoMode && eve.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED )
  1122. {
  1123. u32 val = gui.VideoMode->getItemData ( gui.VideoMode->getSelected() );
  1124. Game->deviceParam.WindowSize.Width = val >> 16;
  1125. Game->deviceParam.WindowSize.Height = val & 0xFFFF;
  1126. }
  1127. else
  1128. if ( eve.GUIEvent.Caller == gui.FullScreen && eve.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED )
  1129. {
  1130. Game->deviceParam.Fullscreen = gui.FullScreen->isChecked();
  1131. }
  1132. else
  1133. if ( eve.GUIEvent.Caller == gui.Bit32 && eve.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED )
  1134. {
  1135. Game->deviceParam.Bits = gui.Bit32->isChecked() ? 32 : 16;
  1136. }
  1137. else
  1138. if ( eve.GUIEvent.Caller == gui.MultiSample && eve.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED )
  1139. {
  1140. Game->deviceParam.AntiAlias = gui.MultiSample->getPos();
  1141. }
  1142. else
  1143. if ( eve.GUIEvent.Caller == gui.Tesselation && eve.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED )
  1144. {
  1145. Game->loadParam.patchTessellation = gui.Tesselation->getPos ();
  1146. }
  1147. else
  1148. if ( eve.GUIEvent.Caller == gui.Gamma && eve.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED )
  1149. {
  1150. Game->GammaValue = gui.Gamma->getPos () * 0.01f;
  1151. Game->Device->setGammaRamp ( Game->GammaValue, Game->GammaValue, Game->GammaValue, 0.f, 0.f );
  1152. }
  1153. else
  1154. if ( eve.GUIEvent.Caller == gui.SetVideoMode && eve.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED )
  1155. {
  1156. Game->retVal = 2;
  1157. Game->Device->closeDevice();
  1158. }
  1159. else
  1160. if ( eve.GUIEvent.Caller == gui.Window && eve.GUIEvent.EventType == gui::EGET_ELEMENT_CLOSED )
  1161. {
  1162. Game->Device->closeDevice();
  1163. }
  1164. else
  1165. if ( eve.GUIEvent.Caller == gui.Collision && eve.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED )
  1166. {
  1167. // set fly through active
  1168. Game->flyTroughState ^= 1;
  1169. Player[0].cam()->setAnimateTarget ( Game->flyTroughState == 0 );
  1170. printf ( "collision %d\n", Game->flyTroughState == 0 );
  1171. }
  1172. else
  1173. if ( eve.GUIEvent.Caller == gui.Visible_Map && eve.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED )
  1174. {
  1175. bool v = gui.Visible_Map->isChecked();
  1176. if ( MapParent )
  1177. {
  1178. printf ( "static node set visible %d\n",v );
  1179. MapParent->setVisible ( v );
  1180. }
  1181. }
  1182. else
  1183. if ( eve.GUIEvent.Caller == gui.Visible_Shader && eve.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED )
  1184. {
  1185. bool v = gui.Visible_Shader->isChecked();
  1186. if ( ShaderParent )
  1187. {
  1188. printf ( "shader node set visible %d\n",v );
  1189. ShaderParent->setVisible ( v );
  1190. }
  1191. }
  1192. else
  1193. if ( eve.GUIEvent.Caller == gui.Visible_Skydome && eve.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED )
  1194. {
  1195. if ( SkyNode )
  1196. {
  1197. bool v = !SkyNode->isVisible();
  1198. printf ( "skynode set visible %d\n",v );
  1199. SkyNode->setVisible ( v );
  1200. }
  1201. }
  1202. else
  1203. if ( eve.GUIEvent.Caller == gui.Respawn && eve.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED )
  1204. {
  1205. Player[0].respawn ();
  1206. }
  1207. return false;
  1208. }
  1209. // fire
  1210. if ((eve.EventType == EET_KEY_INPUT_EVENT && eve.KeyInput.Key == KEY_SPACE &&
  1211. eve.KeyInput.PressedDown == false) ||
  1212. (eve.EventType == EET_MOUSE_INPUT_EVENT && eve.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
  1213. )
  1214. {
  1215. ICameraSceneNode * camera = Game->Device->getSceneManager()->getActiveCamera ();
  1216. if ( camera && camera->isInputReceiverEnabled () )
  1217. {
  1218. useItem( Player + 0 );
  1219. }
  1220. }
  1221. // gui active
  1222. if ((eve.EventType == EET_KEY_INPUT_EVENT && eve.KeyInput.Key == KEY_F1 &&
  1223. eve.KeyInput.PressedDown == false) ||
  1224. (eve.EventType == EET_MOUSE_INPUT_EVENT && eve.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
  1225. )
  1226. {
  1227. SetGUIActive ( 2 );
  1228. }
  1229. // check if user presses the key
  1230. if ( eve.EventType == EET_KEY_INPUT_EVENT && eve.KeyInput.PressedDown == false)
  1231. {
  1232. // Escape toggles camera Input
  1233. if ( eve.KeyInput.Key == irr::KEY_ESCAPE )
  1234. {
  1235. SetGUIActive ( 3 );
  1236. }
  1237. else
  1238. if (eve.KeyInput.Key == KEY_F11)
  1239. {
  1240. // screenshot are taken without gamma!
  1241. IImage* image = Game->Device->getVideoDriver()->createScreenShot();
  1242. if (image)
  1243. {
  1244. core::vector3df pos;
  1245. core::vector3df rot;
  1246. ICameraSceneNode * cam = Game->Device->getSceneManager()->getActiveCamera ();
  1247. if ( cam )
  1248. {
  1249. pos = cam->getPosition ();
  1250. rot = cam->getRotation ();
  1251. }
  1252. snprintf_irr(buf, 256, "%s_%ls_%.0f_%.0f_%.0f_%.0f_%.0f_%.0f.jpg",
  1253. DRIVER_TYPE_NAMES_SHORT[Game->Device->getVideoDriver()->getDriverType()],
  1254. Game->CurrentMapName.c_str(),
  1255. pos.X, pos.Y, pos.Z,
  1256. rot.X, rot.Y, rot.Z
  1257. );
  1258. path filename ( buf );
  1259. filename.replace ( '/', '_' );
  1260. printf ( "screenshot : %s\n", filename.c_str() );
  1261. Game->Device->getVideoDriver()->writeImageToFile(image, filename, 100 );
  1262. image->drop();
  1263. }
  1264. }
  1265. else
  1266. if (eve.KeyInput.Key == KEY_F9)
  1267. {
  1268. s32 value = EDS_OFF;
  1269. Game->debugState = ( Game->debugState + 1 ) & 3;
  1270. switch ( Game->debugState )
  1271. {
  1272. case 1: value = EDS_NORMALS | EDS_MESH_WIRE_OVERLAY | EDS_BBOX_ALL; break;
  1273. case 2: value = EDS_NORMALS | EDS_MESH_WIRE_OVERLAY | EDS_SKELETON; break;
  1274. }
  1275. /*
  1276. // set debug map data on/off
  1277. debugState = debugState == EDS_OFF ?
  1278. EDS_NORMALS | EDS_MESH_WIRE_OVERLAY | EDS_BBOX_ALL:
  1279. EDS_OFF;
  1280. */
  1281. if ( ItemParent )
  1282. {
  1283. list<ISceneNode*>::ConstIterator it = ItemParent->getChildren().begin();
  1284. for (; it != ItemParent->getChildren().end(); ++it)
  1285. {
  1286. (*it)->setDebugDataVisible ( value );
  1287. }
  1288. }
  1289. if ( ShaderParent )
  1290. {
  1291. list<ISceneNode*>::ConstIterator it = ShaderParent->getChildren().begin();
  1292. for (; it != ShaderParent->getChildren().end(); ++it)
  1293. {
  1294. (*it)->setDebugDataVisible ( value );
  1295. }
  1296. }
  1297. if ( UnresolvedParent )
  1298. {
  1299. list<ISceneNode*>::ConstIterator it = UnresolvedParent->getChildren().begin();
  1300. for (; it != UnresolvedParent->getChildren().end(); ++it)
  1301. {
  1302. (*it)->setDebugDataVisible ( value );
  1303. }
  1304. }
  1305. if ( FogParent )
  1306. {
  1307. list<ISceneNode*>::ConstIterator it = FogParent->getChildren().begin();
  1308. for (; it != FogParent->getChildren().end(); ++it)
  1309. {
  1310. (*it)->setDebugDataVisible ( value );
  1311. }
  1312. }
  1313. if ( SkyNode )
  1314. {
  1315. SkyNode->setDebugDataVisible ( value );
  1316. }
  1317. }
  1318. else
  1319. if (eve.KeyInput.Key == KEY_F8)
  1320. {
  1321. // set gravity on/off
  1322. Game->gravityState ^= 1;
  1323. Player[0].cam()->setGravity ( getGravity ( Game->gravityState ? "earth" : "none" ) );
  1324. printf ( "gravity %s\n", Game->gravityState ? "earth" : "none" );
  1325. }
  1326. else
  1327. if (eve.KeyInput.Key == KEY_F7)
  1328. {
  1329. // set fly through active
  1330. Game->flyTroughState ^= 1;
  1331. Player[0].cam()->setAnimateTarget ( Game->flyTroughState == 0 );
  1332. if ( gui.Collision )
  1333. gui.Collision->setChecked ( Game->flyTroughState == 0 );
  1334. printf ( "collision %d\n", Game->flyTroughState == 0 );
  1335. }
  1336. else
  1337. if (eve.KeyInput.Key == KEY_F2)
  1338. {
  1339. Player[0].respawn ();
  1340. }
  1341. else
  1342. if (eve.KeyInput.Key == KEY_F3)
  1343. {
  1344. if ( MapParent )
  1345. {
  1346. bool v = !MapParent->isVisible ();
  1347. printf ( "static node set visible %d\n",v );
  1348. MapParent->setVisible ( v );
  1349. if ( gui.Visible_Map )
  1350. gui.Visible_Map->setChecked ( v );
  1351. }
  1352. }
  1353. else
  1354. if (eve.KeyInput.Key == KEY_F4)
  1355. {
  1356. if ( ShaderParent )
  1357. {
  1358. bool v = !ShaderParent->isVisible ();
  1359. printf ( "shader node set visible %d\n",v );
  1360. ShaderParent->setVisible ( v );
  1361. if ( gui.Visible_Shader )
  1362. gui.Visible_Shader->setChecked ( v );
  1363. }
  1364. }
  1365. else
  1366. if (eve.KeyInput.Key == KEY_F5)
  1367. {
  1368. if ( FogParent )
  1369. {
  1370. bool v = !FogParent->isVisible ();
  1371. printf ( "fog node set visible %d\n",v );
  1372. FogParent->setVisible ( v );
  1373. if ( gui.Visible_Fog )
  1374. gui.Visible_Fog->setChecked ( v );
  1375. }
  1376. }
  1377. else
  1378. if (eve.KeyInput.Key == KEY_F6)
  1379. {
  1380. if ( UnresolvedParent )
  1381. {
  1382. bool v = !UnresolvedParent->isVisible ();
  1383. printf ( "unresolved node set visible %d\n",v );
  1384. UnresolvedParent->setVisible ( v );
  1385. if ( gui.Visible_Unresolved )
  1386. gui.Visible_Unresolved->setChecked ( v );
  1387. }
  1388. }
  1389. }
  1390. // check if user presses the key C ( for crouch)
  1391. if ( eve.EventType == EET_KEY_INPUT_EVENT && eve.KeyInput.Key == KEY_KEY_C )
  1392. {
  1393. // crouch
  1394. ISceneNodeAnimatorCollisionResponse *anim = Player[0].cam ();
  1395. if ( anim && 0 == Game->flyTroughState )
  1396. {
  1397. if ( false == eve.KeyInput.PressedDown )
  1398. {
  1399. // stand up
  1400. anim->setEllipsoidRadius ( vector3df(30,45,30) );
  1401. anim->setEllipsoidTranslation ( vector3df(0,40,0));
  1402. }
  1403. else
  1404. {
  1405. // on your knees
  1406. anim->setEllipsoidRadius ( vector3df(30,20,30) );
  1407. anim->setEllipsoidTranslation ( vector3df(0,20,0));
  1408. }
  1409. return true;
  1410. }
  1411. }
  1412. return false;
  1413. }
  1414. /*
  1415. useItem
  1416. */
  1417. void CQuake3EventHandler::useItem( Q3Player * player)
  1418. {
  1419. ISceneManager* smgr = Game->Device->getSceneManager();
  1420. ICameraSceneNode* camera = smgr->getActiveCamera();
  1421. if (!camera)
  1422. return;
  1423. SParticleImpact imp;
  1424. imp.when = 0;
  1425. // get line of camera
  1426. vector3df start = camera->getPosition();
  1427. if ( player->WeaponNode )
  1428. {
  1429. start.X += 0.f;
  1430. start.Y += 0.f;
  1431. start.Z += 0.f;
  1432. }
  1433. vector3df end = (camera->getTarget() - start);
  1434. end.normalize();
  1435. start += end*20.0f;
  1436. end = start + (end * camera->getFarValue());
  1437. triangle3df triangle;
  1438. line3d<f32> line(start, end);
  1439. // get intersection point with map
  1440. scene::ISceneNode* hitNode;
  1441. if (smgr->getSceneCollisionManager()->getCollisionPoint(
  1442. line, Meta, end, triangle,hitNode))
  1443. {
  1444. // collides with wall
  1445. vector3df out = triangle.getNormal();
  1446. out.setLength(0.03f);
  1447. imp.when = 1;
  1448. imp.outVector = out;
  1449. imp.pos = end;
  1450. player->setAnim ( "pow" );
  1451. player->Anim[1].next += player->Anim[1].delta;
  1452. }
  1453. else
  1454. {
  1455. // doesn't collide with wall
  1456. vector3df start = camera->getPosition();
  1457. if ( player->WeaponNode )
  1458. {
  1459. //start.X += 10.f;
  1460. //start.Y += -5.f;
  1461. //start.Z += 1.f;
  1462. }
  1463. vector3df end = (camera->getTarget() - start);
  1464. end.normalize();
  1465. start += end*20.0f;
  1466. end = start + (end * camera->getFarValue());
  1467. }
  1468. // create fire ball
  1469. ISceneNode* node = 0;
  1470. node = smgr->addBillboardSceneNode( BulletParent,dimension2d<f32>(10,10), start);
  1471. node->setMaterialFlag(EMF_LIGHTING, false);
  1472. node->setMaterialTexture(0, Game->Device->getVideoDriver()->getTexture("fireball.bmp"));
  1473. node->setMaterialFlag(video::EMF_ZWRITE_ENABLE, false);
  1474. node->setMaterialType(EMT_TRANSPARENT_ADD_COLOR);
  1475. f32 length = (f32)(end - start).getLength();
  1476. const f32 speed = 5.8f;
  1477. u32 time = (u32)(length / speed);
  1478. ISceneNodeAnimator* anim = 0;
  1479. // set flight line
  1480. anim = smgr->createFlyStraightAnimator(start, end, time);
  1481. node->addAnimator(anim);
  1482. anim->drop();
  1483. snprintf_irr ( buf, 64, "bullet: %s on %.1f,%1.f,%1.f",
  1484. imp.when ? "hit" : "nohit", end.X, end.Y, end.Z );
  1485. node->setName ( buf );
  1486. anim = smgr->createDeleteAnimator(time);
  1487. node->addAnimator(anim);
  1488. anim->drop();
  1489. if (imp.when)
  1490. {
  1491. // create impact note
  1492. imp.when = Game->Device->getTimer()->getTime() +
  1493. (time + (s32) ( ( 1.f + Noiser::get() ) * 250.f ));
  1494. Impacts.push_back(imp);
  1495. }
  1496. // play sound
  1497. }
  1498. // rendered when bullets hit something
  1499. void CQuake3EventHandler::createParticleImpacts( u32 now )
  1500. {
  1501. ISceneManager* sm = Game->Device->getSceneManager();
  1502. struct smokeLayer
  1503. {
  1504. const c8 * texture;
  1505. f32 scale;
  1506. f32 minparticleSize;
  1507. f32 maxparticleSize;
  1508. f32 boxSize;
  1509. u32 minParticle;
  1510. u32 maxParticle;
  1511. u32 fadeout;
  1512. u32 lifetime;
  1513. };
  1514. smokeLayer smoke[] =
  1515. {
  1516. { "smoke2.jpg", 0.4f, 1.5f, 18.f, 20.f, 20, 50, 2000, 10000 },
  1517. { "smoke3.jpg", 0.2f, 1.2f, 15.f, 20.f, 10, 30, 1000, 12000 }
  1518. };
  1519. u32 i;
  1520. u32 g;
  1521. s32 factor = 1;
  1522. for ( g = 0; g != 2; ++g )
  1523. {
  1524. smoke[g].minParticle *= factor;
  1525. smoke[g].maxParticle *= factor;
  1526. smoke[g].lifetime *= factor;
  1527. smoke[g].boxSize *= Noiser::get() * 0.5f;
  1528. }
  1529. for ( i=0; i < Impacts.size(); ++i)
  1530. {
  1531. if (now < Impacts[i].when)
  1532. continue;
  1533. // create smoke particle system
  1534. IParticleSystemSceneNode* pas = 0;
  1535. for ( g = 0; g != 2; ++g )
  1536. {
  1537. pas = sm->addParticleSystemSceneNode(false, BulletParent, -1, Impacts[i].pos);
  1538. snprintf_irr ( buf, 64, "bullet impact smoke at %.1f,%.1f,%1.f",
  1539. Impacts[i].pos.X,Impacts[i].pos.Y,Impacts[i].pos.Z);
  1540. pas->setName ( buf );
  1541. // create a flat smoke
  1542. vector3df direction = Impacts[i].outVector;
  1543. direction *= smoke[g].scale;
  1544. IParticleEmitter* em = pas->createBoxEmitter(
  1545. aabbox3d<f32>(-4.f,0.f,-4.f,20.f,smoke[g].minparticleSize,20.f),
  1546. direction,smoke[g].minParticle, smoke[g].maxParticle,
  1547. video::SColor(0,0,0,0),video::SColor(0,128,128,128),
  1548. 250,4000, 60);
  1549. em->setMinStartSize (dimension2d<f32>( smoke[g].minparticleSize, smoke[g].minparticleSize));
  1550. em->setMaxStartSize (dimension2d<f32>( smoke[g].maxparticleSize, smoke[g].maxparticleSize));
  1551. pas->setEmitter(em);
  1552. em->drop();
  1553. // particles get invisible
  1554. IParticleAffector* paf = pas->createFadeOutParticleAffector(
  1555. video::SColor ( 0, 0, 0, 0 ), smoke[g].fadeout);
  1556. pas->addAffector(paf);
  1557. paf->drop();
  1558. // particle system life time
  1559. ISceneNodeAnimator* anim = sm->createDeleteAnimator( smoke[g].lifetime);
  1560. pas->addAnimator(anim);
  1561. anim->drop();
  1562. pas->setMaterialFlag(video::EMF_LIGHTING, false);
  1563. pas->setMaterialFlag(video::EMF_ZWRITE_ENABLE, false);
  1564. pas->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR );
  1565. pas->setMaterialTexture(0, Game->Device->getVideoDriver()->getTexture( smoke[g].texture ));
  1566. }
  1567. // play impact sound
  1568. #ifdef USE_IRRKLANG
  1569. /*
  1570. if (irrKlang)
  1571. {
  1572. audio::ISound* sound =
  1573. irrKlang->play3D(impactSound, Impacts[i].pos, false, false, true);
  1574. if (sound)
  1575. {
  1576. // adjust max value a bit to make to sound of an impact louder
  1577. sound->setMinDistance(400);
  1578. sound->drop();
  1579. }
  1580. }
  1581. */
  1582. #endif
  1583. // delete entry
  1584. Impacts.erase(i);
  1585. i--;
  1586. }
  1587. }
  1588. /*
  1589. render
  1590. */
  1591. void CQuake3EventHandler::Render()
  1592. {
  1593. IVideoDriver * driver = Game->Device->getVideoDriver();
  1594. if ( 0 == driver )
  1595. return;
  1596. // TODO: This does not work, yet.
  1597. const bool anaglyph=false;
  1598. if (anaglyph)
  1599. {
  1600. scene::ICameraSceneNode* cameraOld = Game->Device->getSceneManager()->getActiveCamera();
  1601. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, SColor(0,0,0,0));
  1602. driver->getOverrideMaterial().Material.ColorMask = ECP_NONE;
  1603. driver->getOverrideMaterial().EnableFlags = EMF_COLOR_MASK;
  1604. driver->getOverrideMaterial().EnablePasses = ESNRP_SKY_BOX +
  1605. ESNRP_SOLID +
  1606. ESNRP_TRANSPARENT +
  1607. ESNRP_TRANSPARENT_EFFECT +
  1608. ESNRP_SHADOW;
  1609. Game->Device->getSceneManager()->drawAll();
  1610. driver->clearBuffers(video::ECBF_DEPTH, video::SColor(255,0,0,0));
  1611. const vector3df oldPosition = cameraOld->getPosition();
  1612. const vector3df oldTarget = cameraOld->getTarget();
  1613. const matrix4 startMatrix = cameraOld->getAbsoluteTransformation();
  1614. const vector3df focusPoint = (oldTarget -
  1615. cameraOld->getAbsolutePosition()).setLength(10000) +
  1616. cameraOld->getAbsolutePosition() ;
  1617. scene::ICameraSceneNode* camera = cameraOld;//Game->Device->getSceneManager()->addCameraSceneNode();
  1618. //Left eye...
  1619. vector3df pos;
  1620. matrix4 move;
  1621. move.setTranslation( vector3df(-1.5f,0.0f,0.0f) );
  1622. pos=(startMatrix*move).getTranslation();
  1623. driver->getOverrideMaterial().Material.ColorMask = ECP_RED;
  1624. driver->getOverrideMaterial().EnableFlags = EMF_COLOR_MASK;
  1625. driver->getOverrideMaterial().EnablePasses =
  1626. ESNRP_SKY_BOX|ESNRP_SOLID|ESNRP_TRANSPARENT|
  1627. ESNRP_TRANSPARENT_EFFECT|ESNRP_SHADOW;
  1628. camera->setPosition(pos);
  1629. camera->setTarget(focusPoint);
  1630. Game->Device->getSceneManager()->drawAll();
  1631. driver->clearBuffers(video::ECBF_DEPTH, video::SColor(255, 0, 0, 0));
  1632. //Right eye...
  1633. move.setTranslation( vector3df(1.5f,0.0f,0.0f) );
  1634. pos=(startMatrix*move).getTranslation();
  1635. driver->getOverrideMaterial().Material.ColorMask = ECP_GREEN + ECP_BLUE;
  1636. driver->getOverrideMaterial().EnableFlags = EMF_COLOR_MASK;
  1637. driver->getOverrideMaterial().EnablePasses =
  1638. ESNRP_SKY_BOX|ESNRP_SOLID|ESNRP_TRANSPARENT|
  1639. ESNRP_TRANSPARENT_EFFECT|ESNRP_SHADOW;
  1640. camera->setPosition(pos);
  1641. camera->setTarget(focusPoint);
  1642. Game->Device->getSceneManager()->drawAll();
  1643. driver->getOverrideMaterial().Material.ColorMask=ECP_ALL;
  1644. driver->getOverrideMaterial().EnableFlags=0;
  1645. driver->getOverrideMaterial().EnablePasses=0;
  1646. if (camera != cameraOld)
  1647. {
  1648. Game->Device->getSceneManager()->setActiveCamera(cameraOld);
  1649. camera->remove();
  1650. }
  1651. else
  1652. {
  1653. camera->setPosition(oldPosition);
  1654. camera->setTarget(oldTarget);
  1655. }
  1656. }
  1657. else
  1658. {
  1659. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, SColor(0,0,0,0));
  1660. Game->Device->getSceneManager()->drawAll();
  1661. }
  1662. Game->Device->getGUIEnvironment()->drawAll();
  1663. driver->endScene();
  1664. }
  1665. /*
  1666. update the generic scene node
  1667. */
  1668. void CQuake3EventHandler::Animate()
  1669. {
  1670. u32 now = Game->Device->getTimer()->getTime();
  1671. Q3Player * player = Player + 0;
  1672. checkTimeFire ( player->Anim, 4, now );
  1673. // Query Scene Manager attributes
  1674. if ( player->Anim[0].flags & FIRED )
  1675. {
  1676. wchar_t msg[128];
  1677. IVideoDriver * driver = Game->Device->getVideoDriver();
  1678. #ifdef _IRR_SCENEMANAGER_DEBUG
  1679. IAttributes * attr = Game->Device->getSceneManager()->getParameters();
  1680. swprintf_irr ( msg, 128,
  1681. L"Q3 %s [%ls], FPS:%03d Tri:%.03fm Cull %d/%d nodes (%d,%d,%d)",
  1682. Game->CurrentMapName.c_str(),
  1683. driver->getName(),
  1684. driver->getFPS (),
  1685. (f32) driver->getPrimitiveCountDrawn( 0 ) * ( 1.f / 1000000.f ),
  1686. attr->getAttributeAsInt ( "culled" ),
  1687. attr->getAttributeAsInt ( "calls" ),
  1688. attr->getAttributeAsInt ( "drawn_solid" ),
  1689. attr->getAttributeAsInt ( "drawn_transparent" ),
  1690. attr->getAttributeAsInt ( "drawn_transparent_effect" )
  1691. );
  1692. #else
  1693. swprintf_irr ( msg, 128,
  1694. L"Q3 %s [%ls], FPS:%03d Tri:%.03fm",
  1695. Game->CurrentMapName.c_str(),
  1696. driver->getName(),
  1697. driver->getFPS (),
  1698. (f32) driver->getPrimitiveCountDrawn( 0 ) * ( 1.f / 1000000.f )
  1699. );
  1700. #endif
  1701. Game->Device->setWindowCaption( msg );
  1702. swprintf_irr ( msg, 128,
  1703. L"%03d fps, F1 GUI on/off, F2 respawn, F3-F6 toggle Nodes, F7 Collision on/off"
  1704. L", F8 Gravity on/off, Right Mouse Toggle GUI",
  1705. Game->Device->getVideoDriver()->getFPS ()
  1706. );
  1707. if ( gui.StatusLine )
  1708. gui.StatusLine->setText ( msg );
  1709. player->Anim[0].flags &= ~FIRED;
  1710. }
  1711. // idle..
  1712. if ( player->Anim[1].flags & FIRED )
  1713. {
  1714. if ( strcmp ( player->animation, "idle" ) )
  1715. player->setAnim ( "idle" );
  1716. player->Anim[1].flags &= ~FIRED;
  1717. }
  1718. createParticleImpacts ( now );
  1719. }
  1720. /* The main game states
  1721. */
  1722. void runGame ( GameData *game )
  1723. {
  1724. if ( game->retVal >= 3 )
  1725. return;
  1726. game->Device = (*game->createExDevice) ( game->deviceParam );
  1727. if ( 0 == game->Device)
  1728. {
  1729. // could not create selected driver.
  1730. game->retVal = 0;
  1731. return;
  1732. }
  1733. // create an event receiver based on current game data
  1734. CQuake3EventHandler *eventHandler = new CQuake3EventHandler( game );
  1735. // load stored config
  1736. game->load ( "explorer.cfg" );
  1737. // add our media directory and archive to the file system
  1738. for ( u32 i = 0; i < game->CurrentArchiveList.size(); ++i )
  1739. {
  1740. eventHandler->AddArchive ( game->CurrentArchiveList[i] );
  1741. }
  1742. // Load a map or startup to the GUI
  1743. if ( game->CurrentMapName.size () )
  1744. {
  1745. eventHandler->LoadMap ( game->CurrentMapName, 1 );
  1746. if ( 0 == game->loadParam.loadSkyShader )
  1747. eventHandler->AddSky ( 1, "skydome2" );
  1748. eventHandler->CreatePlayers ();
  1749. eventHandler->CreateGUI ();
  1750. eventHandler->SetGUIActive ( 0 );
  1751. // set player to last position on restart
  1752. if ( game->retVal == 2 )
  1753. {
  1754. eventHandler->GetPlayer( 0 )->setpos ( game->PlayerPosition, game->PlayerRotation );
  1755. }
  1756. }
  1757. else
  1758. {
  1759. // start up empty
  1760. eventHandler->AddSky ( 1, "skydome2" );
  1761. eventHandler->CreatePlayers ();
  1762. eventHandler->CreateGUI ();
  1763. eventHandler->SetGUIActive ( 1 );
  1764. background_music ( "IrrlichtTheme.ogg" );
  1765. }
  1766. game->retVal = 3;
  1767. while( game->Device->run() )
  1768. {
  1769. eventHandler->Animate ();
  1770. eventHandler->Render ();
  1771. //if ( !game->Device->isWindowActive() )
  1772. game->Device->yield();
  1773. }
  1774. game->Device->setGammaRamp ( 1.f, 1.f, 1.f, 0.f, 0.f );
  1775. delete eventHandler;
  1776. }
  1777. #if defined (_IRR_WINDOWS_) && 0
  1778. #pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
  1779. #endif
  1780. /* The main routine, doing all setup
  1781. */
  1782. int IRRCALLCONV main(int argc, char* argv[])
  1783. {
  1784. path prgname(argv[0]);
  1785. GameData game ( deletePathFromPath ( prgname, 1 ) );
  1786. // dynamically load irrlicht
  1787. const c8 * dllName = argc > 1 ? argv[1] : "irrlicht.dll";
  1788. game.createExDevice = load_createDeviceEx ( dllName );
  1789. if ( 0 == game.createExDevice )
  1790. {
  1791. game.retVal = 3;
  1792. printf ( "Could not load %s.\n", dllName );
  1793. return game.retVal; // could not load dll
  1794. }
  1795. // start without asking for driver
  1796. game.retVal = 1;
  1797. do
  1798. {
  1799. // if driver could not created, ask for another driver
  1800. if ( game.retVal == 0 )
  1801. {
  1802. game.setDefault ();
  1803. // ask user for driver
  1804. game.deviceParam.DriverType=driverChoiceConsole();
  1805. if (game.deviceParam.DriverType==video::EDT_COUNT)
  1806. game.retVal = 3;
  1807. }
  1808. runGame ( &game );
  1809. } while ( game.retVal < 3 );
  1810. return game.retVal;
  1811. }
  1812. /*
  1813. **/