CMainMenu.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // This is a Demo of the Irrlicht Engine (c) 2005-2009 by N.Gebhardt.
  2. // This file is not documented.
  3. #include "CMainMenu.h"
  4. #include "CDemo.h"
  5. #include "exampleHelper.h"
  6. CMainMenu::CMainMenu()
  7. : startButton(0), MenuDevice(0), selected(0), start(false), fullscreen(false),
  8. #if defined(USE_IRRKLANG) || defined(USE_SDL_MIXER)
  9. music(true),
  10. #else
  11. music(false),
  12. #endif
  13. shadows(true), additive(false), transparent(true), vsync(true), aa(true),
  14. #ifndef _IRR_WINDOWS_
  15. driverType(video::EDT_OPENGL)
  16. #else
  17. driverType(video::EDT_DIRECT3D9)
  18. #endif
  19. //driverType(video::EDT_BURNINGSVIDEO)
  20. {
  21. }
  22. bool CMainMenu::run()
  23. {
  24. MenuDevice = createDevice(video::EDT_BURNINGSVIDEO,
  25. core::dimension2d<u32>(512, 384), 16, false, false, false, this);
  26. const io::path mediaPath = getExampleMediaPath();
  27. if (MenuDevice->getFileSystem()->existFile("irrlicht.dat"))
  28. MenuDevice->getFileSystem()->addFileArchive("irrlicht.dat");
  29. else
  30. MenuDevice->getFileSystem()->addFileArchive(mediaPath + "irrlicht.dat");
  31. video::IVideoDriver* driver = MenuDevice->getVideoDriver();
  32. scene::ISceneManager* smgr = MenuDevice->getSceneManager();
  33. gui::IGUIEnvironment* guienv = MenuDevice->getGUIEnvironment();
  34. core::stringw str = "Irrlicht Engine Demo v";
  35. str += MenuDevice->getVersion();
  36. MenuDevice->setWindowCaption(str.c_str());
  37. // set new Skin
  38. gui::IGUISkin* newskin = guienv->createSkin(gui::EGST_BURNING_SKIN);
  39. guienv->setSkin(newskin);
  40. newskin->drop();
  41. // load font
  42. gui::IGUIFont* font = guienv->getFont(mediaPath + "fonthaettenschweiler.bmp");
  43. if (font)
  44. guienv->getSkin()->setFont(font);
  45. // add images
  46. const s32 leftX = 260;
  47. // add tab control
  48. gui::IGUITabControl* tabctrl = guienv->addTabControl(core::rect<int>(leftX,10,512-10,384-10),
  49. 0, true, true);
  50. gui::IGUITab* optTab = tabctrl->addTab(L"Demo");
  51. gui::IGUITab* aboutTab = tabctrl->addTab(L"About");
  52. // add list box
  53. gui::IGUIListBox* box = guienv->addListBox(core::rect<int>(10,10,220,120), optTab, 1);
  54. box->addItem(L"OpenGL 1.5");
  55. box->addItem(L"Direct3D 9.0c");
  56. box->addItem(L"Burning's Video 0.52");
  57. box->addItem(L"Irrlicht Software Renderer 1.0");
  58. switch (driverType )
  59. {
  60. case video::EDT_OPENGL: selected = 0; break;
  61. case video::EDT_DIRECT3D9: selected = 1; break;
  62. case video::EDT_BURNINGSVIDEO: selected = 2; break;
  63. case video::EDT_SOFTWARE: selected = 3; break;
  64. default: break;
  65. }
  66. box->setSelected(selected);
  67. // add button
  68. startButton = guienv->addButton(core::rect<int>(30,295,200,324), optTab, 2, L"Start Demo");
  69. // add checkbox
  70. const s32 d = 50;
  71. guienv->addCheckBox(fullscreen, core::rect<int>(20,85+d,130,110+d),
  72. optTab, 3, L"Fullscreen");
  73. guienv->addCheckBox(music, core::rect<int>(135,85+d,245,110+d),
  74. optTab, 4, L"Music & Sfx");
  75. guienv->addCheckBox(shadows, core::rect<int>(20,110+d,135,135+d),
  76. optTab, 5, L"Realtime shadows");
  77. guienv->addCheckBox(additive, core::rect<int>(20,135+d,230,160+d),
  78. optTab, 6, L"Old HW compatible blending");
  79. guienv->addCheckBox(vsync, core::rect<int>(20,160+d,230,185+d),
  80. optTab, 7, L"Vertical synchronisation");
  81. guienv->addCheckBox(aa, core::rect<int>(20,185+d,230,210+d),
  82. optTab, 8, L"Antialiasing");
  83. // add about text
  84. const wchar_t* text2 = L"This is the tech demo of the Irrlicht engine. To start, "\
  85. L"select a video driver which works best with your hardware and press 'Start Demo'.\n"\
  86. L"What you currently see is displayed using the Burning Software Renderer (Thomas Alten).\n"\
  87. L"The Irrlicht Engine was written by me, Nikolaus Gebhardt. The models, "\
  88. L"maps and textures were placed at my disposal by B.Collins, M.Cook and J.Marton. The music was created by "\
  89. L"M.Rohde and is played back by irrKlang.\n"\
  90. L"For more information, please visit the homepage of the Irrlicht engine:\nhttp://irrlicht.sourceforge.net";
  91. guienv->addStaticText(text2, core::rect<int>(10, 10, 230, 320),
  92. true, true, aboutTab);
  93. // add md2 model
  94. scene::IAnimatedMesh* mesh = smgr->getMesh(mediaPath + "faerie.md2");
  95. scene::IAnimatedMeshSceneNode* modelNode = smgr->addAnimatedMeshSceneNode(mesh);
  96. if (modelNode)
  97. {
  98. modelNode->setPosition( core::vector3df(0.f, 0.f, -5.f) );
  99. modelNode->setMaterialTexture(0, driver->getTexture(mediaPath + "faerie2.bmp"));
  100. modelNode->setMaterialFlag(video::EMF_LIGHTING, true);
  101. modelNode->getMaterial(0).Shininess = 50.f;
  102. modelNode->getMaterial(0).NormalizeNormals = true;
  103. modelNode->setMD2Animation(scene::EMAT_STAND);
  104. }
  105. // set ambient light (no sun light in the catacombs)
  106. smgr->setAmbientLight( video::SColorf(0.2f, 0.2f, 0.2f) );
  107. scene::ILightSceneNode *light;
  108. scene::ISceneNodeAnimator* anim;
  109. scene::ISceneNode* bill;
  110. enum eLightParticle
  111. {
  112. LIGHT_NONE,
  113. LIGHT_GLOBAL,
  114. LIGHT_RED,
  115. LIGHT_BLUE
  116. };
  117. core::vector3df lightDir[2] = {
  118. core::vector3df(0.f, 0.1f, 0.4f),
  119. core::vector3df(0.f, 0.1f, -0.4f),
  120. };
  121. struct SLightParticle
  122. {
  123. eLightParticle type;
  124. u32 dir;
  125. };
  126. const SLightParticle lightParticle[] =
  127. {
  128. //LIGHT_GLOBAL,0,
  129. {LIGHT_RED,0},
  130. {LIGHT_BLUE,0},
  131. {LIGHT_RED,1},
  132. {LIGHT_BLUE,1},
  133. {LIGHT_NONE,0}
  134. };
  135. const SLightParticle *l = lightParticle;
  136. while ( l->type != LIGHT_NONE )
  137. {
  138. switch ( l->type )
  139. {
  140. case LIGHT_GLOBAL:
  141. // add illumination from the background
  142. light = smgr->addLightSceneNode(0, core::vector3df(10.f,40.f,-5.f),
  143. video::SColorf(0.2f, 0.2f, 0.2f), 90.f);
  144. break;
  145. case LIGHT_RED:
  146. // add light nearly red
  147. light = smgr->addLightSceneNode(0, core::vector3df(0,1,0),
  148. video::SColorf(0.8f, 0.f, 0.f, 0.0f), 30.0f);
  149. // attach red billboard to the light
  150. bill = smgr->addBillboardSceneNode(light, core::dimension2d<f32>(10, 10));
  151. if ( bill )
  152. {
  153. bill->setMaterialFlag(video::EMF_LIGHTING, false);
  154. bill->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);
  155. bill->setMaterialFlag(video::EMF_ZWRITE_ENABLE, false);
  156. bill->setMaterialTexture(0, driver->getTexture(mediaPath + "particlered.bmp"));
  157. }
  158. // add fly circle animator to the light
  159. anim = smgr->createFlyCircleAnimator(core::vector3df(0.f,0.f,-5.f),20.f,
  160. 0.002f, lightDir [l->dir] );
  161. light->addAnimator(anim);
  162. anim->drop();
  163. break;
  164. case LIGHT_BLUE:
  165. // add light nearly blue
  166. light = smgr->addLightSceneNode(0, core::vector3df(0,1,0),
  167. video::SColorf(0.f, 0.0f, 0.8f, 0.0f), 30.0f);
  168. // attach blue billboard to the light
  169. bill = smgr->addBillboardSceneNode(light, core::dimension2d<f32>(10, 10));
  170. if (bill)
  171. {
  172. bill->setMaterialFlag(video::EMF_LIGHTING, false);
  173. bill->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);
  174. bill->setMaterialFlag(video::EMF_ZWRITE_ENABLE, false);
  175. bill->setMaterialTexture(0, driver->getTexture(mediaPath + "portal1.bmp"));
  176. }
  177. // add fly circle animator to the light
  178. anim = smgr->createFlyCircleAnimator(core::vector3df(0.f,0.f,-5.f),20.f,
  179. -0.002f, lightDir [l->dir], 0.5f);
  180. light->addAnimator(anim);
  181. anim->drop();
  182. break;
  183. case LIGHT_NONE:
  184. break;
  185. }
  186. l += 1;
  187. }
  188. // create a fixed camera
  189. smgr->addCameraSceneNode(0, core::vector3df(45,0,0), core::vector3df(0,0,10));
  190. // irrlicht logo and background
  191. // add irrlicht logo
  192. bool oldMipMapState = driver->getTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS);
  193. driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
  194. guienv->addImage(driver->getTexture(mediaPath + "irrlichtlogo3.png"),
  195. core::position2d<s32>(5,5));
  196. video::ITexture* irrlichtBack = driver->getTexture(mediaPath + "demoback.jpg");
  197. driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, oldMipMapState);
  198. // query original skin color
  199. getOriginalSkinColor();
  200. // set transparency
  201. setTransparency();
  202. // draw all
  203. while(MenuDevice->run())
  204. {
  205. if (MenuDevice->isWindowActive())
  206. {
  207. driver->beginScene(video::ECBF_DEPTH, video::SColor(0,0,0,0));
  208. if (irrlichtBack)
  209. driver->draw2DImage(irrlichtBack,
  210. core::position2d<int>(0,0));
  211. smgr->drawAll();
  212. guienv->drawAll();
  213. driver->endScene();
  214. }
  215. }
  216. MenuDevice->drop();
  217. switch(selected)
  218. {
  219. case 0: driverType = video::EDT_OPENGL; break;
  220. case 1: driverType = video::EDT_DIRECT3D9; break;
  221. case 2: driverType = video::EDT_BURNINGSVIDEO; break;
  222. case 3: driverType = video::EDT_SOFTWARE; break;
  223. }
  224. return start;
  225. }
  226. bool CMainMenu::OnEvent(const SEvent& event)
  227. {
  228. if (event.EventType == EET_KEY_INPUT_EVENT &&
  229. event.KeyInput.Key == KEY_F9 &&
  230. event.KeyInput.PressedDown == false)
  231. {
  232. video::IImage* image = MenuDevice->getVideoDriver()->createScreenShot();
  233. if (image)
  234. {
  235. MenuDevice->getVideoDriver()->writeImageToFile(image, "screenshot_main.jpg");
  236. image->drop();
  237. }
  238. }
  239. else
  240. if (event.EventType == irr::EET_MOUSE_INPUT_EVENT &&
  241. event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP )
  242. {
  243. core::rect<s32> r(event.MouseInput.X, event.MouseInput.Y, 0, 0);
  244. gui::IGUIContextMenu* menu = MenuDevice->getGUIEnvironment()->addContextMenu(r, 0, 45);
  245. menu->addItem(L"transparent menus", 666, transparent == false);
  246. menu->addItem(L"solid menus", 666, transparent == true);
  247. menu->addSeparator();
  248. menu->addItem(L"Cancel");
  249. }
  250. else
  251. if (event.EventType == EET_GUI_EVENT)
  252. {
  253. s32 id = event.GUIEvent.Caller->getID();
  254. switch(id)
  255. {
  256. case 45: // context menu
  257. if (event.GUIEvent.EventType == gui::EGET_MENU_ITEM_SELECTED)
  258. {
  259. s32 s = ((gui::IGUIContextMenu*)event.GUIEvent.Caller)->getSelectedItem();
  260. if (s == 0 || s == 1)
  261. {
  262. transparent = !transparent;
  263. setTransparency();
  264. }
  265. }
  266. break;
  267. case 1:
  268. if (event.GUIEvent.EventType == gui::EGET_LISTBOX_CHANGED ||
  269. event.GUIEvent.EventType == gui::EGET_LISTBOX_SELECTED_AGAIN)
  270. {
  271. selected = ((gui::IGUIListBox*)event.GUIEvent.Caller)->getSelected();
  272. //startButton->setEnabled(selected != 4);
  273. startButton->setEnabled(true);
  274. }
  275. break;
  276. case 2:
  277. if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED )
  278. {
  279. MenuDevice->closeDevice();
  280. start = true;
  281. }
  282. case 3:
  283. if (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED )
  284. fullscreen = ((gui::IGUICheckBox*)event.GUIEvent.Caller)->isChecked();
  285. break;
  286. case 4:
  287. if (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED )
  288. music = ((gui::IGUICheckBox*)event.GUIEvent.Caller)->isChecked();
  289. break;
  290. case 5:
  291. if (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED )
  292. shadows = ((gui::IGUICheckBox*)event.GUIEvent.Caller)->isChecked();
  293. break;
  294. case 6:
  295. if (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED )
  296. additive = ((gui::IGUICheckBox*)event.GUIEvent.Caller)->isChecked();
  297. break;
  298. case 7:
  299. if (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED )
  300. vsync = ((gui::IGUICheckBox*)event.GUIEvent.Caller)->isChecked();
  301. break;
  302. case 8:
  303. if (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED )
  304. aa = ((gui::IGUICheckBox*)event.GUIEvent.Caller)->isChecked();
  305. break;
  306. }
  307. }
  308. return false;
  309. }
  310. void CMainMenu::getOriginalSkinColor()
  311. {
  312. irr::gui::IGUISkin * skin = MenuDevice->getGUIEnvironment()->getSkin();
  313. for (s32 i=0; i<gui::EGDC_COUNT ; ++i)
  314. {
  315. SkinColor[i] = skin->getColor( (gui::EGUI_DEFAULT_COLOR)i );
  316. }
  317. }
  318. void CMainMenu::setTransparency()
  319. {
  320. irr::gui::IGUISkin * skin = MenuDevice->getGUIEnvironment()->getSkin();
  321. for (u32 i=0; i<gui::EGDC_COUNT ; ++i)
  322. {
  323. video::SColor col = SkinColor[i];
  324. if (false == transparent)
  325. col.setAlpha(255);
  326. skin->setColor((gui::EGUI_DEFAULT_COLOR)i, col);
  327. }
  328. }