3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #pragma message ("Disabling unit tests")
24 #warning "Disabling unit tests"
27 #define ENABLE_TESTS 0
30 #define ENABLE_TESTS 1
34 #ifndef SERVER // Dedicated server isn't linked with Irrlicht
35 #pragma comment(lib, "Irrlicht.lib")
36 // This would get rid of the console window
37 //#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
39 #pragma comment(lib, "zlibwapi.lib")
40 #pragma comment(lib, "Shell32.lib")
43 #include "irrlicht.h" // createDevice
46 #include "mainmenumanager.h"
50 #include "common_irrlicht.h"
54 #include "constants.h"
57 #include "guiMessageMenu.h"
60 #include "guiMainMenu.h"
65 #include "defaultsettings.h"
74 These are loaded from the config file.
76 Settings main_settings;
77 Settings *g_settings = &main_settings;
80 Profiler main_profiler;
81 Profiler *g_profiler = &main_profiler;
88 std::ostream *dout_con_ptr = &dummyout;
89 std::ostream *derr_con_ptr = &verbosestream;
90 //std::ostream *dout_con_ptr = &infostream;
91 //std::ostream *derr_con_ptr = &errorstream;
94 std::ostream *dout_server_ptr = &infostream;
95 std::ostream *derr_server_ptr = &errorstream;
98 std::ostream *dout_client_ptr = &infostream;
99 std::ostream *derr_client_ptr = &errorstream;
106 /* mainmenumanager.h */
108 gui::IGUIEnvironment* guienv = NULL;
109 gui::IGUIStaticText *guiroot = NULL;
110 MainMenuManager g_menumgr;
114 return (g_menumgr.menuCount() == 0);
117 // Passed to menus to allow disconnecting and exiting
118 MainGameCallback *g_gamecallback = NULL;
122 gettime.h implementation
129 /* Use imprecise system calls directly (from porting.h) */
130 return porting::getTimeMs();
135 // A small helper class
139 virtual u32 getTime() = 0;
142 // A precise irrlicht one
143 class IrrlichtTimeGetter: public TimeGetter
146 IrrlichtTimeGetter(IrrlichtDevice *device):
153 return m_device->getTimer()->getRealTime();
156 IrrlichtDevice *m_device;
158 // Not so precise one which works without irrlicht
159 class SimpleTimeGetter: public TimeGetter
164 return porting::getTimeMs();
168 // A pointer to a global instance of the time getter
170 TimeGetter *g_timegetter = NULL;
174 if(g_timegetter == NULL)
176 return g_timegetter->getTime();
181 class StderrLogOutput: public ILogOutput
184 /* line: Full line with timestamp, level and thread */
185 void printLog(const std::string &line)
187 std::cerr<<line<<std::endl;
189 } main_stderr_log_out;
191 class DstreamNoStderrLogOutput: public ILogOutput
194 /* line: Full line with timestamp, level and thread */
195 void printLog(const std::string &line)
197 dstream_no_stderr<<line<<std::endl;
199 } main_dstream_no_stderr_log_out;
204 Event handler for Irrlicht
206 NOTE: Everything possible should be moved out from here,
207 probably to InputHandler and the_game
210 class MyEventReceiver : public IEventReceiver
213 // This is the one method that we have to implement
214 virtual bool OnEvent(const SEvent& event)
217 React to nothing here if a menu is active
219 if(noMenuActive() == false)
224 // Remember whether each key is down or up
225 if(event.EventType == irr::EET_KEY_INPUT_EVENT)
227 if(event.KeyInput.PressedDown) {
228 keyIsDown.set(event.KeyInput);
229 keyWasDown.set(event.KeyInput);
231 keyIsDown.unset(event.KeyInput);
235 if(event.EventType == irr::EET_MOUSE_INPUT_EVENT)
237 if(noMenuActive() == false)
240 middle_active = false;
241 right_active = false;
245 left_active = event.MouseInput.isLeftPressed();
246 middle_active = event.MouseInput.isMiddlePressed();
247 right_active = event.MouseInput.isRightPressed();
249 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
253 if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
257 if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
261 if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
263 rightreleased = true;
265 if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
267 mouse_wheel += event.MouseInput.Wheel;
275 bool IsKeyDown(const KeyPress &keyCode) const
277 return keyIsDown[keyCode];
280 // Checks whether a key was down and resets the state
281 bool WasKeyDown(const KeyPress &keyCode)
283 bool b = keyWasDown[keyCode];
285 keyWasDown.unset(keyCode);
302 rightclicked = false;
303 leftreleased = false;
304 rightreleased = false;
307 middle_active = false;
308 right_active = false;
330 IrrlichtDevice *m_device;
332 // The current state of keys
334 // Whether a key has been pressed or not
339 Separated input handler
342 class RealInputHandler : public InputHandler
345 RealInputHandler(IrrlichtDevice *device, MyEventReceiver *receiver):
350 virtual bool isKeyDown(const KeyPress &keyCode)
352 return m_receiver->IsKeyDown(keyCode);
354 virtual bool wasKeyDown(const KeyPress &keyCode)
356 return m_receiver->WasKeyDown(keyCode);
358 virtual v2s32 getMousePos()
360 return m_device->getCursorControl()->getPosition();
362 virtual void setMousePos(s32 x, s32 y)
364 m_device->getCursorControl()->setPosition(x, y);
367 virtual bool getLeftState()
369 return m_receiver->left_active;
371 virtual bool getRightState()
373 return m_receiver->right_active;
376 virtual bool getLeftClicked()
378 return m_receiver->leftclicked;
380 virtual bool getRightClicked()
382 return m_receiver->rightclicked;
384 virtual void resetLeftClicked()
386 m_receiver->leftclicked = false;
388 virtual void resetRightClicked()
390 m_receiver->rightclicked = false;
393 virtual bool getLeftReleased()
395 return m_receiver->leftreleased;
397 virtual bool getRightReleased()
399 return m_receiver->rightreleased;
401 virtual void resetLeftReleased()
403 m_receiver->leftreleased = false;
405 virtual void resetRightReleased()
407 m_receiver->rightreleased = false;
410 virtual s32 getMouseWheel()
412 return m_receiver->getMouseWheel();
417 m_receiver->clearInput();
420 IrrlichtDevice *m_device;
421 MyEventReceiver *m_receiver;
424 class RandomInputHandler : public InputHandler
432 rightclicked = false;
433 leftreleased = false;
434 rightreleased = false;
437 virtual bool isKeyDown(const KeyPress &keyCode)
439 return keydown[keyCode];
441 virtual bool wasKeyDown(const KeyPress &keyCode)
445 virtual v2s32 getMousePos()
449 virtual void setMousePos(s32 x, s32 y)
451 mousepos = v2s32(x,y);
454 virtual bool getLeftState()
458 virtual bool getRightState()
463 virtual bool getLeftClicked()
467 virtual bool getRightClicked()
471 virtual void resetLeftClicked()
475 virtual void resetRightClicked()
477 rightclicked = false;
480 virtual bool getLeftReleased()
484 virtual bool getRightReleased()
486 return rightreleased;
488 virtual void resetLeftReleased()
490 leftreleased = false;
492 virtual void resetRightReleased()
494 rightreleased = false;
497 virtual s32 getMouseWheel()
502 virtual void step(float dtime)
505 static float counter1 = 0;
509 counter1 = 0.1*Rand(1, 40);
510 keydown.toggle(getKeySetting("keymap_jump"));
514 static float counter1 = 0;
518 counter1 = 0.1*Rand(1, 40);
519 keydown.toggle(getKeySetting("keymap_special1"));
523 static float counter1 = 0;
527 counter1 = 0.1*Rand(1, 40);
528 keydown.toggle(getKeySetting("keymap_forward"));
532 static float counter1 = 0;
536 counter1 = 0.1*Rand(1, 40);
537 keydown.toggle(getKeySetting("keymap_left"));
541 static float counter1 = 0;
545 counter1 = 0.1*Rand(1, 20);
546 mousespeed = v2s32(Rand(-20,20), Rand(-15,20));
550 static float counter1 = 0;
554 counter1 = 0.1*Rand(1, 30);
555 leftdown = !leftdown;
563 static float counter1 = 0;
567 counter1 = 0.1*Rand(1, 15);
568 rightdown = !rightdown;
572 rightreleased = true;
575 mousepos += mousespeed;
578 s32 Rand(s32 min, s32 max)
580 return (myrand()%(max-min+1))+min;
594 void drawMenuBackground(video::IVideoDriver* driver)
596 core::dimension2d<u32> screensize = driver->getScreenSize();
598 video::ITexture *bgtexture =
599 driver->getTexture(getTexturePath("menubg.png").c_str());
602 s32 scaledsize = 128;
604 // The important difference between destsize and screensize is
605 // that destsize is rounded to whole scaled pixels.
606 // These formulas use component-wise multiplication and division of v2u32.
607 v2u32 texturesize = bgtexture->getSize();
608 v2u32 sourcesize = texturesize * screensize / scaledsize + v2u32(1,1);
609 v2u32 destsize = scaledsize * sourcesize / texturesize;
611 // Default texture wrapping mode in Irrlicht is ETC_REPEAT.
612 driver->draw2DImage(bgtexture,
613 core::rect<s32>(0, 0, destsize.X, destsize.Y),
614 core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
618 video::ITexture *logotexture =
619 driver->getTexture(getTexturePath("menulogo.png").c_str());
622 v2s32 logosize(logotexture->getOriginalSize().Width,
623 logotexture->getOriginalSize().Height);
626 video::SColor bgcolor(255,50,50,50);
627 core::rect<s32> bgrect(0, screensize.Height-logosize.Y-20,
628 screensize.Width, screensize.Height);
629 driver->draw2DRectangle(bgcolor, bgrect, NULL);
631 core::rect<s32> rect(0,0,logosize.X,logosize.Y);
632 rect += v2s32(screensize.Width/2,screensize.Height-10-logosize.Y);
633 rect -= v2s32(logosize.X/2, 0);
634 driver->draw2DImage(logotexture, rect,
635 core::rect<s32>(core::position2d<s32>(0,0),
636 core::dimension2di(logotexture->getSize())),
643 // These are defined global so that they're not optimized too much.
644 // Can't change them to volatile.
649 std::string tempstring;
650 std::string tempstring2;
655 infostream<<"The following test should take around 20ms."<<std::endl;
656 TimeTaker timer("Testing std::string speed");
657 const u32 jj = 10000;
658 for(u32 j=0; j<jj; j++)
663 for(u32 i=0; i<ii; i++){
664 tempstring2 += "asd";
666 for(u32 i=0; i<ii+1; i++){
668 if(tempstring == tempstring2)
674 infostream<<"All of the following tests should take around 100ms each."
678 TimeTaker timer("Testing floating-point conversion speed");
680 for(u32 i=0; i<4000000; i++){
687 TimeTaker timer("Testing floating-point vector speed");
689 tempv3f1 = v3f(1,2,3);
690 tempv3f2 = v3f(4,5,6);
691 for(u32 i=0; i<10000000; i++){
692 tempf += tempv3f1.dotProduct(tempv3f2);
693 tempv3f2 += v3f(7,8,9);
698 TimeTaker timer("Testing core::map speed");
700 core::map<v2s16, f32> map1;
703 for(s16 y=0; y<ii; y++){
704 for(s16 x=0; x<ii; x++){
705 map1.insert(v2s16(x,y), tempf);
709 for(s16 y=ii-1; y>=0; y--){
710 for(s16 x=0; x<ii; x++){
711 tempf = map1[v2s16(x,y)];
717 infostream<<"Around 5000/ms should do well here."<<std::endl;
718 TimeTaker timer("Testing mutex speed");
732 while(timer.getTime() < 10);
734 u32 dtime = timer.stop();
735 u32 per_ms = n / dtime;
736 infostream<<"Done. "<<dtime<<"ms, "
737 <<per_ms<<"/ms"<<std::endl;
741 int main(int argc, char *argv[])
747 log_add_output_maxlev(&main_stderr_log_out, LMT_ACTION);
748 log_add_output_all_levs(&main_dstream_no_stderr_log_out);
750 log_register_thread("main");
752 // Set locale. This is for forcing '.' as the decimal point.
753 std::locale::global(std::locale("C"));
754 // This enables printing all characters in bitmap font
755 setlocale(LC_CTYPE, "en_US");
761 // List all allowed options
762 core::map<std::string, ValueSpec> allowed_options;
763 allowed_options.insert("help", ValueSpec(VALUETYPE_FLAG,
764 "Show allowed options"));
765 allowed_options.insert("config", ValueSpec(VALUETYPE_STRING,
766 "Load configuration from specified file"));
767 allowed_options.insert("port", ValueSpec(VALUETYPE_STRING,
768 "Set network port (UDP) to use"));
769 allowed_options.insert("disable-unittests", ValueSpec(VALUETYPE_FLAG,
770 "Disable unit tests"));
771 allowed_options.insert("enable-unittests", ValueSpec(VALUETYPE_FLAG,
772 "Enable unit tests"));
773 allowed_options.insert("map-dir", ValueSpec(VALUETYPE_STRING,
774 "Map directory (where everything in the world is stored)"));
775 allowed_options.insert("info-on-stderr", ValueSpec(VALUETYPE_FLAG,
776 "Print more information to console (deprecated; use --verbose)"));
777 allowed_options.insert("verbose", ValueSpec(VALUETYPE_FLAG,
778 "Print more information to console"));
780 allowed_options.insert("speedtests", ValueSpec(VALUETYPE_FLAG,
782 allowed_options.insert("address", ValueSpec(VALUETYPE_STRING,
783 "Address to connect to"));
784 allowed_options.insert("random-input", ValueSpec(VALUETYPE_FLAG,
785 "Enable random user input, for testing"));
786 allowed_options.insert("server", ValueSpec(VALUETYPE_FLAG,
787 "Run server directly"));
792 bool ret = cmd_args.parseCommandLine(argc, argv, allowed_options);
794 if(ret == false || cmd_args.getFlag("help"))
796 dstream<<"Allowed options:"<<std::endl;
797 for(core::map<std::string, ValueSpec>::Iterator
798 i = allowed_options.getIterator();
799 i.atEnd() == false; i++)
801 dstream<<" --"<<i.getNode()->getKey();
802 if(i.getNode()->getValue().type == VALUETYPE_FLAG)
811 if(i.getNode()->getValue().help != NULL)
813 dstream<<" "<<i.getNode()->getValue().help
818 return cmd_args.getFlag("help") ? 0 : 1;
822 Low-level initialization
825 if(cmd_args.getFlag("verbose") ||
826 cmd_args.getFlag("info-on-stderr") ||
827 cmd_args.getFlag("speedtests"))
828 log_add_output(&main_stderr_log_out, LMT_INFO);
830 porting::signal_handler_init();
831 bool &kill = *porting::signal_handler_killstatus();
833 porting::initializePaths();
835 // Create user data directory
836 fs::CreateDir(porting::path_user);
838 init_gettext((porting::path_share+DIR_DELIM+".."+DIR_DELIM+"locale").c_str());
840 // Initialize debug streams
842 std::string debugfile = DEBUGFILE;
844 std::string debugfile = porting::path_user+DIR_DELIM+DEBUGFILE;
846 bool disable_stderr = false;
847 debugstreams_init(disable_stderr, debugfile.c_str());
848 // Initialize debug stacks
851 DSTACK(__FUNCTION_NAME);
853 dstream<<"path_share = "<<porting::path_share<<std::endl;
854 dstream<<"path_user = "<<porting::path_user<<std::endl;
857 BEGIN_DEBUG_EXCEPTION_HANDLER
859 // Print startup message
860 actionstream<<PROJECT_NAME<<
861 " with SER_FMT_VER_HIGHEST="<<(int)SER_FMT_VER_HIGHEST
869 // Initialize default settings
870 set_default_settings(g_settings);
872 // Initialize sockets
874 atexit(sockets_cleanup);
880 // Path of configuration file in use
881 std::string configpath = "";
883 if(cmd_args.exists("config"))
885 bool r = g_settings->readConfigFile(cmd_args.get("config").c_str());
888 errorstream<<"Could not read configuration from \""
889 <<cmd_args.get("config")<<"\""<<std::endl;
892 configpath = cmd_args.get("config");
896 core::array<std::string> filenames;
897 filenames.push_back(porting::path_user +
898 DIR_DELIM + "minetest.conf");
899 // Legacy configuration file location
900 filenames.push_back(porting::path_user +
901 DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
903 // Try also from a lower level (to aid having the same configuration
904 // for many RUN_IN_PLACE installs)
905 filenames.push_back(porting::path_user +
906 DIR_DELIM + ".." + DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
909 for(u32 i=0; i<filenames.size(); i++)
911 bool r = g_settings->readConfigFile(filenames[i].c_str());
914 configpath = filenames[i];
919 // If no path found, use the first one (menu creates the file)
921 configpath = filenames[0];
924 // Initialize random seed
932 if((ENABLE_TESTS && cmd_args.getFlag("disable-unittests") == false)
933 || cmd_args.getFlag("enable-unittests") == true)
944 if(cmd_args.exists("port"))
945 port = cmd_args.getU16("port");
946 else if(g_settings->exists("port"))
947 port = g_settings->getU16("port");
952 std::string map_dir = porting::path_user + DIR_DELIM + "server" + DIR_DELIM + "worlds" + DIR_DELIM + "world";
953 if(cmd_args.exists("map-dir"))
954 map_dir = cmd_args.get("map-dir");
955 else if(g_settings->exists("map-dir"))
956 map_dir = g_settings->get("map-dir");
958 // No map-dir option was specified.
959 // Check if the world is found from the default directory, and if
960 // not, see if the legacy world directory exists.
961 std::string legacy_map_dir = porting::path_user+DIR_DELIM+".."+DIR_DELIM+"world";
962 if(!fs::PathExists(map_dir) && fs::PathExists(legacy_map_dir)){
963 errorstream<<"Warning: Using legacy world directory \""
964 <<legacy_map_dir<<"\""<<std::endl;
965 map_dir = legacy_map_dir;
969 // Run dedicated server if asked to or no other option
971 bool run_dedicated_server = true;
973 bool run_dedicated_server = cmd_args.getFlag("server");
975 if(run_dedicated_server)
977 DSTACK("Dedicated server branch");
979 // Create time getter if built with Irrlicht
981 g_timegetter = new SimpleTimeGetter();
985 Server server(map_dir, configpath, "mesetint");
989 dedicated_server_loop(server, kill);
994 #ifndef SERVER // Exclude from dedicated server build
1000 // Address to connect to
1001 std::string address = "";
1003 if(cmd_args.exists("address"))
1005 address = cmd_args.get("address");
1009 address = g_settings->get("address");
1012 std::string playername = g_settings->get("name");
1015 Device initialization
1018 // Resolution selection
1020 bool fullscreen = false;
1021 u16 screenW = g_settings->getU16("screenW");
1022 u16 screenH = g_settings->getU16("screenH");
1026 video::E_DRIVER_TYPE driverType;
1028 std::string driverstring = g_settings->get("video_driver");
1030 if(driverstring == "null")
1031 driverType = video::EDT_NULL;
1032 else if(driverstring == "software")
1033 driverType = video::EDT_SOFTWARE;
1034 else if(driverstring == "burningsvideo")
1035 driverType = video::EDT_BURNINGSVIDEO;
1036 else if(driverstring == "direct3d8")
1037 driverType = video::EDT_DIRECT3D8;
1038 else if(driverstring == "direct3d9")
1039 driverType = video::EDT_DIRECT3D9;
1040 else if(driverstring == "opengl")
1041 driverType = video::EDT_OPENGL;
1044 errorstream<<"WARNING: Invalid video_driver specified; defaulting "
1045 "to opengl"<<std::endl;
1046 driverType = video::EDT_OPENGL;
1050 Create device and exit if creation failed
1053 MyEventReceiver receiver;
1055 IrrlichtDevice *device;
1056 device = createDevice(driverType,
1057 core::dimension2d<u32>(screenW, screenH),
1058 16, fullscreen, false, false, &receiver);
1061 return 1; // could not create selected driver.
1064 Continue initialization
1067 video::IVideoDriver* driver = device->getVideoDriver();
1069 // Disable mipmaps (because some of them look ugly)
1070 driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
1073 This changes the minimum allowed number of vertices in a VBO.
1076 //driver->setMinHardwareBufferVertexCount(50);
1078 // Set the window caption
1079 device->setWindowCaption(L"Minetest [Main Menu]");
1081 // Create time getter
1082 g_timegetter = new IrrlichtTimeGetter(device);
1084 // Create game callback for menus
1085 g_gamecallback = new MainGameCallback(device);
1088 Speed tests (done after irrlicht is loaded to get timer)
1090 if(cmd_args.getFlag("speedtests"))
1092 dstream<<"Running speed tests"<<std::endl;
1097 device->setResizable(true);
1099 bool random_input = g_settings->getBool("random_input")
1100 || cmd_args.getFlag("random-input");
1101 InputHandler *input = NULL;
1103 input = new RandomInputHandler();
1105 input = new RealInputHandler(device, &receiver);
1107 scene::ISceneManager* smgr = device->getSceneManager();
1109 guienv = device->getGUIEnvironment();
1110 gui::IGUISkin* skin = guienv->getSkin();
1111 gui::IGUIFont* font = guienv->getFont(getTexturePath("fontlucida.png").c_str());
1113 skin->setFont(font);
1115 errorstream<<"WARNING: Font file was not found."
1116 " Using default font."<<std::endl;
1117 // If font was not found, this will get us one
1118 font = skin->getFont();
1121 u32 text_height = font->getDimension(L"Hello, world!").Height;
1122 infostream<<"text_height="<<text_height<<std::endl;
1124 //skin->setColor(gui::EGDC_BUTTON_TEXT, video::SColor(255,0,0,0));
1125 skin->setColor(gui::EGDC_BUTTON_TEXT, video::SColor(255,255,255,255));
1126 //skin->setColor(gui::EGDC_3D_HIGH_LIGHT, video::SColor(0,0,0,0));
1127 //skin->setColor(gui::EGDC_3D_SHADOW, video::SColor(0,0,0,0));
1128 skin->setColor(gui::EGDC_3D_HIGH_LIGHT, video::SColor(255,0,0,0));
1129 skin->setColor(gui::EGDC_3D_SHADOW, video::SColor(255,0,0,0));
1135 ChatBackend chat_backend;
1138 If an error occurs, this is set to something and the
1139 menu-game loop is restarted. It is then displayed before
1142 std::wstring error_message = L"";
1144 // The password entered during the menu screen,
1145 std::string password;
1150 while(device->run() && kill == false)
1153 // This is used for catching disconnects
1158 Clear everything from the GUIEnvironment
1163 We need some kind of a root node to be able to add
1164 custom gui elements directly on the screen.
1165 Otherwise they won't be automatically drawn.
1167 guiroot = guienv->addStaticText(L"",
1168 core::rect<s32>(0, 0, 10000, 10000));
1171 Out-of-game menu loop.
1173 Loop quits when menu returns proper parameters.
1175 while(kill == false)
1177 // Cursor can be non-visible when coming from the game
1178 device->getCursorControl()->setVisible(true);
1179 // Some stuff are left to scene manager when coming from the game
1182 // Reset or hide the debug gui texts
1183 /*guitext->setText(L"Minetest-c55");
1184 guitext2->setVisible(false);
1185 guitext_info->setVisible(false);
1186 guitext_chat->setVisible(false);*/
1188 // Initialize menu data
1189 MainMenuData menudata;
1190 menudata.address = narrow_to_wide(address);
1191 menudata.name = narrow_to_wide(playername);
1192 menudata.port = narrow_to_wide(itos(port));
1193 menudata.fancy_trees = g_settings->getBool("new_style_leaves");
1194 menudata.smooth_lighting = g_settings->getBool("smooth_lighting");
1195 menudata.clouds_3d = g_settings->getBool("enable_3d_clouds");
1196 menudata.opaque_water = g_settings->getBool("opaque_water");
1197 menudata.creative_mode = g_settings->getBool("creative_mode");
1198 menudata.enable_damage = g_settings->getBool("enable_damage");
1201 new GUIMainMenu(guienv, guiroot, -1,
1202 &g_menumgr, &menudata, g_gamecallback);
1203 menu->allowFocusRemoval(true);
1205 if(error_message != L"")
1207 errorstream<<"error_message = "
1208 <<wide_to_narrow(error_message)<<std::endl;
1210 GUIMessageMenu *menu2 =
1211 new GUIMessageMenu(guienv, guiroot, -1,
1212 &g_menumgr, error_message.c_str());
1214 error_message = L"";
1217 video::IVideoDriver* driver = device->getVideoDriver();
1219 infostream<<"Created main menu"<<std::endl;
1221 while(device->run() && kill == false)
1223 if(menu->getStatus() == true)
1226 //driver->beginScene(true, true, video::SColor(255,0,0,0));
1227 driver->beginScene(true, true, video::SColor(255,128,128,128));
1229 drawMenuBackground(driver);
1235 // On some computers framerate doesn't seem to be
1236 // automatically limited
1240 // Break out of menu-game loop to shut down cleanly
1241 if(device->run() == false || kill == true)
1244 infostream<<"Dropping main menu"<<std::endl;
1248 // Delete map if requested
1249 if(menudata.delete_map)
1251 bool r = fs::RecursiveDeleteContent(map_dir);
1253 error_message = L"Delete failed";
1257 playername = wide_to_narrow(menudata.name);
1259 password = translatePassword(playername, menudata.password);
1261 //infostream<<"Main: password hash: '"<<password<<"'"<<std::endl;
1263 address = wide_to_narrow(menudata.address);
1264 int newport = stoi(wide_to_narrow(menudata.port));
1267 g_settings->set("new_style_leaves", itos(menudata.fancy_trees));
1268 g_settings->set("smooth_lighting", itos(menudata.smooth_lighting));
1269 g_settings->set("enable_3d_clouds", itos(menudata.clouds_3d));
1270 g_settings->set("opaque_water", itos(menudata.opaque_water));
1271 g_settings->set("creative_mode", itos(menudata.creative_mode));
1272 g_settings->set("enable_damage", itos(menudata.enable_damage));
1274 // NOTE: These are now checked server side; no need to do it
1275 // here, so let's not do it here.
1276 /*// Check for valid parameters, restart menu if invalid.
1277 if(playername == "")
1279 error_message = L"Name required.";
1282 // Check that name has only valid chars
1283 if(string_allowed(playername, PLAYERNAME_ALLOWED_CHARS)==false)
1285 error_message = L"Characters allowed: "
1286 +narrow_to_wide(PLAYERNAME_ALLOWED_CHARS);
1291 g_settings->set("name", playername);
1292 g_settings->set("address", address);
1293 g_settings->set("port", itos(port));
1294 // Update configuration file
1295 if(configpath != "")
1296 g_settings->updateConfigFile(configpath.c_str());
1302 // Break out of menu-game loop to shut down cleanly
1303 if(device->run() == false || kill == true)
1326 catch(con::PeerNotFoundException &e)
1328 errorstream<<"Connection error (timed out?)"<<std::endl;
1329 error_message = L"Connection error (timed out?)";
1331 catch(SocketException &e)
1333 errorstream<<"Socket error (port already in use?)"<<std::endl;
1334 error_message = L"Socket error (port already in use?)";
1338 errorstream<<e.what()<<std::endl;
1339 error_message = narrow_to_wide(e.what()) + L"\nCheck debug.txt for details.";
1342 catch(std::exception &e)
1344 std::string narrow_message = "Some exception, what()=\"";
1345 narrow_message += e.what();
1346 narrow_message += "\"";
1347 errorstream<<narrow_message<<std::endl;
1348 error_message = narrow_to_wide(narrow_message);
1357 In the end, delete the Irrlicht device.
1363 // Update configuration file
1364 if(configpath != "")
1365 g_settings->updateConfigFile(configpath.c_str());
1367 END_DEBUG_EXCEPTION_HANDLER(errorstream)
1369 debugstreams_deinit();