CMainMenu.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // This is a Demo of the Irrlicht Engine (c) 2005-2008 by N.Gebhardt.
  2. // This file is not documented.
  3. #include "CMainMenu.h"
  4. //! we want the lights follow the model when it's moving
  5. class CSceneNodeAnimatorFollowBoundingBox : public irr::scene::ISceneNodeAnimator
  6. {
  7. public:
  8. //! constructor
  9. CSceneNodeAnimatorFollowBoundingBox(irr::scene::ISceneNode* tofollow,
  10. const core::vector3df &offset, u32 frequency, s32 phase)
  11. : Offset(offset), ToFollow(tofollow), Frequency(frequency), Phase(phase)
  12. {
  13. if (ToFollow)
  14. ToFollow->grab();
  15. }
  16. //! destructor
  17. virtual ~CSceneNodeAnimatorFollowBoundingBox()
  18. {
  19. if (ToFollow)
  20. ToFollow->drop();
  21. }
  22. //! animates a scene node
  23. virtual void animateNode(irr::scene::ISceneNode* node, u32 timeMs)
  24. {
  25. if (0 == node || node->getType() != irr::scene::ESNT_LIGHT)
  26. return;
  27. irr::scene::ILightSceneNode* l = (irr::scene::ILightSceneNode*) node;
  28. if (ToFollow)
  29. {
  30. core::vector3df now = l->getPosition();
  31. now += ToFollow->getBoundingBox().getCenter();
  32. now += Offset;
  33. l->setPosition(now);
  34. }
  35. irr::video::SColorHSL color;
  36. irr::video::SColorf rgb(0);
  37. color.Hue = ( (timeMs + Phase) % Frequency ) * ( 2.f * irr::core::PI / Frequency );
  38. color.Saturation = 1.f;
  39. color.Luminance = 0.5f;
  40. color.toRGB(rgb);
  41. video::SLight light = l->getLightData();
  42. light.DiffuseColor = rgb;
  43. l->setLightData(light);
  44. }
  45. virtual scene::ISceneNodeAnimator* createClone(scene::ISceneNode* node, scene::ISceneManager* newManager=0) {return 0;}
  46. private:
  47. core::vector3df Offset;
  48. irr::scene::ISceneNode* ToFollow;
  49. s32 Frequency;
  50. s32 Phase;
  51. };
  52. CMainMenu::CMainMenu()
  53. : startButton(0), MenuDevice(0), selected(2), start(false),
  54. shadows(false), additive(false), transparent(true), vsync(false), aa(false),
  55. #ifdef _DEBUG
  56. fullscreen(false), music(false)
  57. #else
  58. fullscreen(true), music(true)
  59. #endif
  60. {
  61. }
  62. bool CMainMenu::run(bool& outFullscreen, bool& outMusic, bool& outShadows,
  63. bool& outAdditive, bool& outVSync, bool& outAA,
  64. video::E_DRIVER_TYPE& outDriver, core::stringw &playerName)
  65. {
  66. //video::E_DRIVER_TYPE driverType = video::EDT_DIRECT3D9;
  67. //video::E_DRIVER_TYPE driverType = video::EDT_OPENGL;
  68. video::E_DRIVER_TYPE driverType = video::EDT_BURNINGSVIDEO;
  69. //video::E_DRIVER_TYPE driverType = video::EDT_SOFTWARE;
  70. MenuDevice = createDevice(driverType,
  71. core::dimension2d<u32>(512, 384), 16, false, false, false, this);
  72. if (MenuDevice->getFileSystem()->existFile("irrlicht.dat"))
  73. MenuDevice->getFileSystem()->addFileArchive("irrlicht.dat", true, true, io::EFAT_ZIP);
  74. else
  75. MenuDevice->getFileSystem()->addFileArchive("../../media/irrlicht.dat", true, true, io::EFAT_ZIP);
  76. video::IVideoDriver* driver = MenuDevice->getVideoDriver();
  77. scene::ISceneManager* smgr = MenuDevice->getSceneManager();
  78. gui::IGUIEnvironment* guienv = MenuDevice->getGUIEnvironment();
  79. core::stringw str = "Irrlicht Engine Demo v";
  80. str += MenuDevice->getVersion();
  81. MenuDevice->setWindowCaption(str.c_str());
  82. // set new Skin
  83. gui::IGUISkin* newskin = guienv->createSkin(gui::EGST_BURNING_SKIN);
  84. guienv->setSkin(newskin);
  85. newskin->drop();
  86. // load font
  87. gui::IGUIFont* font = guienv->getFont("../../media/fonthaettenschweiler.bmp");
  88. if (font)
  89. guienv->getSkin()->setFont(font);
  90. // add images
  91. const s32 leftX = 260;
  92. // add tab control
  93. gui::IGUITabControl* tabctrl = guienv->addTabControl(core::rect<int>(leftX,10,512-10,384-10),
  94. 0, true, true);
  95. gui::IGUITab* optTab = tabctrl->addTab(L"Demo");
  96. gui::IGUITab* aboutTab = tabctrl->addTab(L"About");
  97. // add list box
  98. gui::IGUIListBox* box = guienv->addListBox(core::rect<int>(10,10,220,120), optTab, 1);
  99. box->addItem(L"OpenGL 1.5");
  100. box->addItem(L"Direct3D 8.1");
  101. box->addItem(L"Direct3D 9.0c");
  102. box->addItem(L"Burning's Video 0.39");
  103. box->addItem(L"Irrlicht Software Renderer 1.0");
  104. box->setSelected(selected);
  105. // add button
  106. startButton = guienv->addButton(core::rect<int>(30,295,200,324), optTab, 2, L"Start Demo");
  107. // add checkbox
  108. const s32 d = 50;
  109. guienv->addCheckBox(fullscreen, core::rect<int>(20,85+d,130,110+d),
  110. optTab, 3, L"Fullscreen");
  111. guienv->addCheckBox(music, core::rect<int>(135,85+d,245,110+d),
  112. optTab, 4, L"Music & Sfx");
  113. guienv->addCheckBox(shadows, core::rect<int>(20,110+d,135,135+d),
  114. optTab, 5, L"Realtime shadows");
  115. guienv->addCheckBox(additive, core::rect<int>(20,135+d,230,160+d),
  116. optTab, 6, L"Old HW compatible blending");
  117. guienv->addCheckBox(vsync, core::rect<int>(20,160+d,230,185+d),
  118. optTab, 7, L"Vertical synchronisation");
  119. guienv->addCheckBox(aa, core::rect<int>(135,110+d,245,135+d),
  120. optTab, 8, L"Antialiasing");
  121. // RakNet: Add edit box
  122. nameEditBox=guienv->addEditBox(L"Your name here", core::rect<int>(20,185+d,230,210+d), true, optTab, 9);
  123. // add about text
  124. wchar_t* text2 = L"This is the tech demo of the Irrlicht engine. To start, "\
  125. L"select a video driver which works best with your hardware and press 'Start Demo'.\n"\
  126. L"What you currently see is displayed using the Burning Software Renderer (Thomas Alten).\n"\
  127. L"The Irrlicht Engine was written by me, Nikolaus Gebhardt. The models, "\
  128. L"maps and textures were placed at my disposal by B.Collins, M.Cook and J.Marton. The music was created by "\
  129. L"M.Rohde and is played back by irrKlang.\n"\
  130. L"For more informations, please visit the homepage of the Irrlicht engine:\nhttp://irrlicht.sourceforge.net\n"\
  131. L"\n*** MULTIPLAYER UPDATE ***\n"\
  132. L"Peer to peer multiplayer added in two days using RakNet.\n"\
  133. L"For a description of the networking design, see included readme.txt .\n";
  134. guienv->addStaticText(text2, core::rect<int>(10, 10, 230, 320),
  135. true, true, aboutTab);
  136. // add md2 model
  137. scene::IAnimatedMesh* mesh = smgr->getMesh("../../media/faerie.md2");
  138. scene::IAnimatedMeshSceneNode* modelNode = smgr->addAnimatedMeshSceneNode(mesh);
  139. if (modelNode)
  140. {
  141. modelNode->setPosition( core::vector3df(0.f, 0.f, -5.f) );
  142. modelNode->setMaterialTexture(0, driver->getTexture("../../media/faerie2.bmp"));
  143. modelNode->setMaterialFlag(video::EMF_LIGHTING, true);
  144. modelNode->getMaterial(0).Shininess = 28.f;
  145. modelNode->getMaterial(0).NormalizeNormals = true;
  146. modelNode->setMD2Animation(scene::EMAT_STAND);
  147. }
  148. // set ambient light (no sun light in the catacombs)
  149. smgr->setAmbientLight( video::SColorf(0.f, 0.f, 0.f) );
  150. scene::ISceneNodeAnimator* anim;
  151. scene::ISceneNode* bill;
  152. // add light 1 (sunset orange)
  153. scene::ILightSceneNode* light1 =
  154. smgr->addLightSceneNode(0, core::vector3df(10.f,10.f,0),
  155. video::SColorf(0.86f, 0.38f, 0.05f), 200.0f);
  156. // add fly circle animator to light 1
  157. anim = smgr->createFlyCircleAnimator(core::vector3df(0,0,0),30.0f, -0.004f, core::vector3df(0.41f, 0.4f, 0.0f));
  158. light1->addAnimator(anim);
  159. anim->drop();
  160. // let the lights follow the model...
  161. anim = new CSceneNodeAnimatorFollowBoundingBox(modelNode, core::vector3df(0,16,0), 4000, 0);
  162. //light1->addAnimator(anim);
  163. anim->drop();
  164. // attach billboard to the light
  165. bill = smgr->addBillboardSceneNode(light1, core::dimension2d<f32>(10, 10));
  166. bill->setMaterialFlag(video::EMF_LIGHTING, false);
  167. bill->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);
  168. bill->setMaterialTexture(0, driver->getTexture("../../media/particlered.bmp"));
  169. #if 1
  170. // add light 2 (nearly red)
  171. scene::ILightSceneNode* light2 =
  172. smgr->addLightSceneNode(0, core::vector3df(0,1,0),
  173. video::SColorf(0.9f, 1.0f, 0.f, 0.0f), 200.0f);
  174. // add fly circle animator to light 1
  175. anim = smgr->createFlyCircleAnimator(core::vector3df(0,0,0),30.0f, 0.004f, core::vector3df(0.41f, 0.4f, 0.0f));
  176. light2->addAnimator(anim);
  177. anim->drop();
  178. // let the lights follow the model...
  179. anim = new CSceneNodeAnimatorFollowBoundingBox( modelNode, core::vector3df(0,-8,0), 2000, 0 );
  180. //light2->addAnimator(anim);
  181. anim->drop();
  182. // attach billboard to the light
  183. bill = smgr->addBillboardSceneNode(light2, core::dimension2d<f32>(10, 10));
  184. bill->setMaterialFlag(video::EMF_LIGHTING, false);
  185. bill->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);
  186. bill->setMaterialTexture(0, driver->getTexture("../../media/particlered.bmp"));
  187. // add light 3 (nearly blue)
  188. scene::ILightSceneNode* light3 =
  189. smgr->addLightSceneNode(0, core::vector3df(0,-1,0),
  190. video::SColorf(0.f, 0.0f, 0.9f, 0.0f), 40.0f);
  191. // add fly circle animator to light 2
  192. anim = smgr->createFlyCircleAnimator(core::vector3df(0,0,0),40.0f, 0.004f, core::vector3df(-0.41f, -0.4f, 0.0f));
  193. light3->addAnimator(anim);
  194. anim->drop();
  195. // let the lights follow the model...
  196. anim = new CSceneNodeAnimatorFollowBoundingBox(modelNode, core::vector3df(0,8,0), 8000, 0);
  197. //light3->addAnimator(anim);
  198. anim->drop();
  199. // attach billboard to the light
  200. bill = smgr->addBillboardSceneNode(light3, core::dimension2d<f32>(10, 10));
  201. if (bill)
  202. {
  203. bill->setMaterialFlag(video::EMF_LIGHTING, false);
  204. bill->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);
  205. bill->setMaterialTexture(0, driver->getTexture("../../media/portal1.bmp"));
  206. }
  207. #endif
  208. // create a fixed camera
  209. smgr->addCameraSceneNode(0, core::vector3df(45,0,0), core::vector3df(0,0,10));
  210. // irrlicht logo and background
  211. // add irrlicht logo
  212. bool oldMipMapState = driver->getTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS);
  213. driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
  214. guienv->addImage(driver->getTexture("../../media/irrlichtlogo2.png"),
  215. core::position2d<s32>(5,5));
  216. video::ITexture* irrlichtBack = driver->getTexture("../../media/demoback.jpg");
  217. driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, oldMipMapState);
  218. // query original skin color
  219. getOriginalSkinColor();
  220. // set transparency
  221. setTransparency();
  222. // draw all
  223. while(MenuDevice->run())
  224. {
  225. if (MenuDevice->isWindowActive())
  226. {
  227. driver->beginScene(false, true, video::SColor(0,0,0,0));
  228. if (irrlichtBack)
  229. driver->draw2DImage(irrlichtBack,
  230. core::position2d<int>(0,0));
  231. smgr->drawAll();
  232. guienv->drawAll();
  233. driver->endScene();
  234. }
  235. }
  236. playerName=nameEditBox->getText();
  237. MenuDevice->drop();
  238. outFullscreen = fullscreen;
  239. outMusic = music;
  240. outShadows = shadows;
  241. outAdditive = additive;
  242. outVSync = vsync;
  243. outAA = aa;
  244. switch(selected)
  245. {
  246. case 0: outDriver = video::EDT_OPENGL; break;
  247. case 1: outDriver = video::EDT_DIRECT3D8; break;
  248. case 2: outDriver = video::EDT_DIRECT3D9; break;
  249. case 3: outDriver = video::EDT_BURNINGSVIDEO; break;
  250. case 4: outDriver = video::EDT_SOFTWARE; break;
  251. }
  252. return start;
  253. }
  254. bool CMainMenu::OnEvent(const SEvent& event)
  255. {
  256. if (event.EventType == EET_KEY_INPUT_EVENT &&
  257. event.KeyInput.Key == KEY_F9 &&
  258. event.KeyInput.PressedDown == false)
  259. {
  260. video::IImage* image = MenuDevice->getVideoDriver()->createScreenShot();
  261. if (image)
  262. {
  263. MenuDevice->getVideoDriver()->writeImageToFile(image, "screenshot_main.jpg");
  264. image->drop();
  265. }
  266. }
  267. else
  268. if (event.EventType == irr::EET_MOUSE_INPUT_EVENT &&
  269. event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP )
  270. {
  271. core::rect<s32> r(event.MouseInput.X, event.MouseInput.Y, 0, 0);
  272. gui::IGUIContextMenu* menu = MenuDevice->getGUIEnvironment()->addContextMenu(r, 0, 45);
  273. menu->addItem(L"transparent menus", 666, transparent == false);
  274. menu->addItem(L"solid menus", 666, transparent == true);
  275. menu->addSeparator();
  276. menu->addItem(L"Cancel");
  277. }
  278. else
  279. if (event.EventType == EET_GUI_EVENT)
  280. {
  281. s32 id = event.GUIEvent.Caller->getID();
  282. switch(id)
  283. {
  284. case 45: // context menu
  285. if (event.GUIEvent.EventType == gui::EGET_MENU_ITEM_SELECTED)
  286. {
  287. s32 s = ((gui::IGUIContextMenu*)event.GUIEvent.Caller)->getSelectedItem();
  288. if (s == 0 || s == 1)
  289. {
  290. transparent = !transparent;
  291. setTransparency();
  292. }
  293. }
  294. break;
  295. case 1:
  296. if (event.GUIEvent.EventType == gui::EGET_LISTBOX_CHANGED ||
  297. event.GUIEvent.EventType == gui::EGET_LISTBOX_SELECTED_AGAIN)
  298. {
  299. selected = ((gui::IGUIListBox*)event.GUIEvent.Caller)->getSelected();
  300. //startButton->setEnabled(selected != 4);
  301. startButton->setEnabled(true);
  302. }
  303. break;
  304. case 2:
  305. if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED )
  306. {
  307. MenuDevice->closeDevice();
  308. start = true;
  309. }
  310. case 3:
  311. if (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED )
  312. fullscreen = ((gui::IGUICheckBox*)event.GUIEvent.Caller)->isChecked();
  313. break;
  314. case 4:
  315. if (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED )
  316. music = ((gui::IGUICheckBox*)event.GUIEvent.Caller)->isChecked();
  317. break;
  318. case 5:
  319. if (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED )
  320. shadows = ((gui::IGUICheckBox*)event.GUIEvent.Caller)->isChecked();
  321. break;
  322. case 6:
  323. if (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED )
  324. additive = ((gui::IGUICheckBox*)event.GUIEvent.Caller)->isChecked();
  325. break;
  326. case 7:
  327. if (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED )
  328. vsync = ((gui::IGUICheckBox*)event.GUIEvent.Caller)->isChecked();
  329. break;
  330. case 8:
  331. if (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED )
  332. aa = ((gui::IGUICheckBox*)event.GUIEvent.Caller)->isChecked();
  333. break;
  334. }
  335. }
  336. return false;
  337. }
  338. void CMainMenu::getOriginalSkinColor()
  339. {
  340. irr::gui::IGUISkin * skin = MenuDevice->getGUIEnvironment()->getSkin();
  341. for (s32 i=0; i<gui::EGDC_COUNT ; ++i)
  342. {
  343. SkinColor[i] = skin->getColor( (gui::EGUI_DEFAULT_COLOR)i );
  344. }
  345. }
  346. void CMainMenu::setTransparency()
  347. {
  348. irr::gui::IGUISkin * skin = MenuDevice->getGUIEnvironment()->getSkin();
  349. for (u32 i=0; i<gui::EGDC_COUNT ; ++i)
  350. {
  351. video::SColor col = SkinColor[i];
  352. if (false == transparent)
  353. col.setAlpha(255);
  354. skin->setColor((gui::EGUI_DEFAULT_COLOR)i, col);
  355. }
  356. }
粤ICP备19079148号