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"
71 #include "utility_string.h"
76 These are loaded from the config file.
78 Settings main_settings;
79 Settings *g_settings = &main_settings;
82 Profiler main_profiler;
83 Profiler *g_profiler = &main_profiler;
90 std::ostream *dout_con_ptr = &dummyout;
91 std::ostream *derr_con_ptr = &verbosestream;
92 //std::ostream *dout_con_ptr = &infostream;
93 //std::ostream *derr_con_ptr = &errorstream;
96 std::ostream *dout_server_ptr = &infostream;
97 std::ostream *derr_server_ptr = &errorstream;
100 std::ostream *dout_client_ptr = &infostream;
101 std::ostream *derr_client_ptr = &errorstream;
108 /* mainmenumanager.h */
110 gui::IGUIEnvironment* guienv = NULL;
111 gui::IGUIStaticText *guiroot = NULL;
112 MainMenuManager g_menumgr;
116 return (g_menumgr.menuCount() == 0);
119 // Passed to menus to allow disconnecting and exiting
120 MainGameCallback *g_gamecallback = NULL;
124 gettime.h implementation
131 /* Use imprecise system calls directly (from porting.h) */
132 return porting::getTimeMs();
137 // A small helper class
141 virtual u32 getTime() = 0;
144 // A precise irrlicht one
145 class IrrlichtTimeGetter: public TimeGetter
148 IrrlichtTimeGetter(IrrlichtDevice *device):
155 return m_device->getTimer()->getRealTime();
158 IrrlichtDevice *m_device;
160 // Not so precise one which works without irrlicht
161 class SimpleTimeGetter: public TimeGetter
166 return porting::getTimeMs();
170 // A pointer to a global instance of the time getter
172 TimeGetter *g_timegetter = NULL;
176 if(g_timegetter == NULL)
178 return g_timegetter->getTime();
183 class StderrLogOutput: public ILogOutput
186 /* line: Full line with timestamp, level and thread */
187 void printLog(const std::string &line)
189 std::cerr<<line<<std::endl;
191 } main_stderr_log_out;
193 class DstreamNoStderrLogOutput: public ILogOutput
196 /* line: Full line with timestamp, level and thread */
197 void printLog(const std::string &line)
199 dstream_no_stderr<<line<<std::endl;
201 } main_dstream_no_stderr_log_out;
206 Event handler for Irrlicht
208 NOTE: Everything possible should be moved out from here,
209 probably to InputHandler and the_game
212 class MyEventReceiver : public IEventReceiver
215 // This is the one method that we have to implement
216 virtual bool OnEvent(const SEvent& event)
219 React to nothing here if a menu is active
221 if(noMenuActive() == false)
226 // Remember whether each key is down or up
227 if(event.EventType == irr::EET_KEY_INPUT_EVENT)
229 if(event.KeyInput.PressedDown) {
230 keyIsDown.set(event.KeyInput);
231 keyWasDown.set(event.KeyInput);
233 keyIsDown.unset(event.KeyInput);
237 if(event.EventType == irr::EET_MOUSE_INPUT_EVENT)
239 if(noMenuActive() == false)
242 middle_active = false;
243 right_active = false;
247 left_active = event.MouseInput.isLeftPressed();
248 middle_active = event.MouseInput.isMiddlePressed();
249 right_active = event.MouseInput.isRightPressed();
251 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
255 if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
259 if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
263 if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
265 rightreleased = true;
267 if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
269 mouse_wheel += event.MouseInput.Wheel;
277 bool IsKeyDown(const KeyPress &keyCode) const
279 return keyIsDown[keyCode];
282 // Checks whether a key was down and resets the state
283 bool WasKeyDown(const KeyPress &keyCode)
285 bool b = keyWasDown[keyCode];
287 keyWasDown.unset(keyCode);
304 rightclicked = false;
305 leftreleased = false;
306 rightreleased = false;
309 middle_active = false;
310 right_active = false;
332 IrrlichtDevice *m_device;
334 // The current state of keys
336 // Whether a key has been pressed or not
341 Separated input handler
344 class RealInputHandler : public InputHandler
347 RealInputHandler(IrrlichtDevice *device, MyEventReceiver *receiver):
352 virtual bool isKeyDown(const KeyPress &keyCode)
354 return m_receiver->IsKeyDown(keyCode);
356 virtual bool wasKeyDown(const KeyPress &keyCode)
358 return m_receiver->WasKeyDown(keyCode);
360 virtual v2s32 getMousePos()
362 return m_device->getCursorControl()->getPosition();
364 virtual void setMousePos(s32 x, s32 y)
366 m_device->getCursorControl()->setPosition(x, y);
369 virtual bool getLeftState()
371 return m_receiver->left_active;
373 virtual bool getRightState()
375 return m_receiver->right_active;
378 virtual bool getLeftClicked()
380 return m_receiver->leftclicked;
382 virtual bool getRightClicked()
384 return m_receiver->rightclicked;
386 virtual void resetLeftClicked()
388 m_receiver->leftclicked = false;
390 virtual void resetRightClicked()
392 m_receiver->rightclicked = false;
395 virtual bool getLeftReleased()
397 return m_receiver->leftreleased;
399 virtual bool getRightReleased()
401 return m_receiver->rightreleased;
403 virtual void resetLeftReleased()
405 m_receiver->leftreleased = false;
407 virtual void resetRightReleased()
409 m_receiver->rightreleased = false;
412 virtual s32 getMouseWheel()
414 return m_receiver->getMouseWheel();
419 m_receiver->clearInput();
422 IrrlichtDevice *m_device;
423 MyEventReceiver *m_receiver;
426 class RandomInputHandler : public InputHandler
434 rightclicked = false;
435 leftreleased = false;
436 rightreleased = false;
439 virtual bool isKeyDown(const KeyPress &keyCode)
441 return keydown[keyCode];
443 virtual bool wasKeyDown(const KeyPress &keyCode)
447 virtual v2s32 getMousePos()
451 virtual void setMousePos(s32 x, s32 y)
453 mousepos = v2s32(x,y);
456 virtual bool getLeftState()
460 virtual bool getRightState()
465 virtual bool getLeftClicked()
469 virtual bool getRightClicked()
473 virtual void resetLeftClicked()
477 virtual void resetRightClicked()
479 rightclicked = false;
482 virtual bool getLeftReleased()
486 virtual bool getRightReleased()
488 return rightreleased;
490 virtual void resetLeftReleased()
492 leftreleased = false;
494 virtual void resetRightReleased()
496 rightreleased = false;
499 virtual s32 getMouseWheel()
504 virtual void step(float dtime)
507 static float counter1 = 0;
511 counter1 = 0.1*Rand(1, 40);
512 keydown.toggle(getKeySetting("keymap_jump"));
516 static float counter1 = 0;
520 counter1 = 0.1*Rand(1, 40);
521 keydown.toggle(getKeySetting("keymap_special1"));
525 static float counter1 = 0;
529 counter1 = 0.1*Rand(1, 40);
530 keydown.toggle(getKeySetting("keymap_forward"));
534 static float counter1 = 0;
538 counter1 = 0.1*Rand(1, 40);
539 keydown.toggle(getKeySetting("keymap_left"));
543 static float counter1 = 0;
547 counter1 = 0.1*Rand(1, 20);
548 mousespeed = v2s32(Rand(-20,20), Rand(-15,20));
552 static float counter1 = 0;
556 counter1 = 0.1*Rand(1, 30);
557 leftdown = !leftdown;
565 static float counter1 = 0;
569 counter1 = 0.1*Rand(1, 15);
570 rightdown = !rightdown;
574 rightreleased = true;
577 mousepos += mousespeed;
580 s32 Rand(s32 min, s32 max)
582 return (myrand()%(max-min+1))+min;
596 void drawMenuBackground(video::IVideoDriver* driver)
598 core::dimension2d<u32> screensize = driver->getScreenSize();
600 video::ITexture *bgtexture =
601 driver->getTexture(getTexturePath("menubg.png").c_str());
604 s32 scaledsize = 128;
606 // The important difference between destsize and screensize is
607 // that destsize is rounded to whole scaled pixels.
608 // These formulas use component-wise multiplication and division of v2u32.
609 v2u32 texturesize = bgtexture->getSize();
610 v2u32 sourcesize = texturesize * screensize / scaledsize + v2u32(1,1);
611 v2u32 destsize = scaledsize * sourcesize / texturesize;
613 // Default texture wrapping mode in Irrlicht is ETC_REPEAT.
614 driver->draw2DImage(bgtexture,
615 core::rect<s32>(0, 0, destsize.X, destsize.Y),
616 core::rect<s32>(0, 0, sourcesize.X, sourcesize.Y),
620 video::ITexture *logotexture =
621 driver->getTexture(getTexturePath("menulogo.png").c_str());
624 v2s32 logosize(logotexture->getOriginalSize().Width,
625 logotexture->getOriginalSize().Height);
628 video::SColor bgcolor(255,50,50,50);
629 core::rect<s32> bgrect(0, screensize.Height-logosize.Y-20,
630 screensize.Width, screensize.Height);
631 driver->draw2DRectangle(bgcolor, bgrect, NULL);
633 core::rect<s32> rect(0,0,logosize.X,logosize.Y);
634 rect += v2s32(screensize.Width/2,screensize.Height-10-logosize.Y);
635 rect -= v2s32(logosize.X/2, 0);
636 driver->draw2DImage(logotexture, rect,
637 core::rect<s32>(core::position2d<s32>(0,0),
638 core::dimension2di(logotexture->getSize())),
645 // These are defined global so that they're not optimized too much.
646 // Can't change them to volatile.
651 std::string tempstring;
652 std::string tempstring2;
657 infostream<<"The following test should take around 20ms."<<std::endl;
658 TimeTaker timer("Testing std::string speed");
659 const u32 jj = 10000;
660 for(u32 j=0; j<jj; j++)
665 for(u32 i=0; i<ii; i++){
666 tempstring2 += "asd";
668 for(u32 i=0; i<ii+1; i++){
670 if(tempstring == tempstring2)
676 infostream<<"All of the following tests should take around 100ms each."
680 TimeTaker timer("Testing floating-point conversion speed");
682 for(u32 i=0; i<4000000; i++){
689 TimeTaker timer("Testing floating-point vector speed");
691 tempv3f1 = v3f(1,2,3);
692 tempv3f2 = v3f(4,5,6);
693 for(u32 i=0; i<10000000; i++){
694 tempf += tempv3f1.dotProduct(tempv3f2);
695 tempv3f2 += v3f(7,8,9);
700 TimeTaker timer("Testing core::map speed");
702 core::map<v2s16, f32> map1;
705 for(s16 y=0; y<ii; y++){
706 for(s16 x=0; x<ii; x++){
707 map1.insert(v2s16(x,y), tempf);
711 for(s16 y=ii-1; y>=0; y--){
712 for(s16 x=0; x<ii; x++){
713 tempf = map1[v2s16(x,y)];
719 infostream<<"Around 5000/ms should do well here."<<std::endl;
720 TimeTaker timer("Testing mutex speed");
734 while(timer.getTime() < 10);
736 u32 dtime = timer.stop();
737 u32 per_ms = n / dtime;
738 infostream<<"Done. "<<dtime<<"ms, "
739 <<per_ms<<"/ms"<<std::endl;
743 int main(int argc, char *argv[])
751 log_add_output_maxlev(&main_stderr_log_out, LMT_ACTION);
752 log_add_output_all_levs(&main_dstream_no_stderr_log_out);
754 log_register_thread("main");
756 // Set locale. This is for forcing '.' as the decimal point.
757 std::locale::global(std::locale("C"));
758 // This enables printing all characters in bitmap font
759 setlocale(LC_CTYPE, "en_US");
765 // List all allowed options
766 core::map<std::string, ValueSpec> allowed_options;
767 allowed_options.insert("help", ValueSpec(VALUETYPE_FLAG,
768 "Show allowed options"));
769 allowed_options.insert("config", ValueSpec(VALUETYPE_STRING,
770 "Load configuration from specified file"));
771 allowed_options.insert("port", ValueSpec(VALUETYPE_STRING,
772 "Set network port (UDP)"));
773 allowed_options.insert("disable-unittests", ValueSpec(VALUETYPE_FLAG,
774 "Disable unit tests"));
775 allowed_options.insert("enable-unittests", ValueSpec(VALUETYPE_FLAG,
776 "Enable unit tests"));
777 allowed_options.insert("map-dir", ValueSpec(VALUETYPE_STRING,
778 "Same as --world (deprecated)"));
779 allowed_options.insert("world", ValueSpec(VALUETYPE_STRING,
781 allowed_options.insert("verbose", ValueSpec(VALUETYPE_FLAG,
782 "Print more information to console"));
783 allowed_options.insert("logfile", ValueSpec(VALUETYPE_STRING,
784 "Set logfile path (debug.txt)"));
785 allowed_options.insert("gameid", ValueSpec(VALUETYPE_STRING,
786 "Set gameid (\"--gameid list\" prints available ones)"));
788 allowed_options.insert("speedtests", ValueSpec(VALUETYPE_FLAG,
790 allowed_options.insert("address", ValueSpec(VALUETYPE_STRING,
791 "Address to connect to. ('' = local game)"));
792 allowed_options.insert("random-input", ValueSpec(VALUETYPE_FLAG,
793 "Enable random user input, for testing"));
794 allowed_options.insert("server", ValueSpec(VALUETYPE_FLAG,
795 "Run dedicated server"));
796 allowed_options.insert("name", ValueSpec(VALUETYPE_STRING,
798 allowed_options.insert("password", ValueSpec(VALUETYPE_STRING,
800 allowed_options.insert("go", ValueSpec(VALUETYPE_FLAG,
801 "Disable main menu"));
806 bool ret = cmd_args.parseCommandLine(argc, argv, allowed_options);
808 if(ret == false || cmd_args.getFlag("help"))
810 dstream<<"Allowed options:"<<std::endl;
811 for(core::map<std::string, ValueSpec>::Iterator
812 i = allowed_options.getIterator();
813 i.atEnd() == false; i++)
815 std::ostringstream os1(std::ios::binary);
816 os1<<" --"<<i.getNode()->getKey();
817 if(i.getNode()->getValue().type == VALUETYPE_FLAG)
821 dstream<<padStringRight(os1.str(), 24);
823 if(i.getNode()->getValue().help != NULL)
824 dstream<<i.getNode()->getValue().help;
828 return cmd_args.getFlag("help") ? 0 : 1;
832 Low-level initialization
835 // In certain cases, output info level on stderr
836 if(cmd_args.getFlag("verbose") || cmd_args.getFlag("speedtests"))
837 log_add_output(&main_stderr_log_out, LMT_INFO);
839 porting::signal_handler_init();
840 bool &kill = *porting::signal_handler_killstatus();
842 porting::initializePaths();
844 // Create user data directory
845 fs::CreateDir(porting::path_user);
847 init_gettext((porting::path_share+DIR_DELIM+".."+DIR_DELIM+"locale").c_str());
849 // Initialize debug streams
851 std::string logfile = DEBUGFILE;
853 std::string logfile = porting::path_user+DIR_DELIM+DEBUGFILE;
855 if(cmd_args.exists("logfile"))
856 logfile = cmd_args.get("logfile");
857 bool disable_stderr = false;
858 debugstreams_init(disable_stderr, logfile.c_str());
859 // Initialize debug stacks
862 DSTACK(__FUNCTION_NAME);
864 dstream<<"logfile = "<<logfile<<std::endl;
865 dstream<<"path_share = "<<porting::path_share<<std::endl;
866 dstream<<"path_user = "<<porting::path_user<<std::endl;
869 BEGIN_DEBUG_EXCEPTION_HANDLER
871 // List gameids if requested
872 if(cmd_args.exists("gameid") && cmd_args.get("gameid") == "list")
874 std::set<std::string> gameids = getAvailableGameIds();
875 for(std::set<std::string>::const_iterator i = gameids.begin();
876 i != gameids.end(); i++)
877 dstream<<(*i)<<std::endl;
881 // Print startup message
882 actionstream<<PROJECT_NAME<<
883 " with SER_FMT_VER_HIGHEST="<<(int)SER_FMT_VER_HIGHEST
891 // Initialize default settings
892 set_default_settings(g_settings);
894 // Initialize sockets
896 atexit(sockets_cleanup);
902 // Path of configuration file in use
903 std::string configpath = "";
905 if(cmd_args.exists("config"))
907 bool r = g_settings->readConfigFile(cmd_args.get("config").c_str());
910 errorstream<<"Could not read configuration from \""
911 <<cmd_args.get("config")<<"\""<<std::endl;
914 configpath = cmd_args.get("config");
918 core::array<std::string> filenames;
919 filenames.push_back(porting::path_user +
920 DIR_DELIM + "minetest.conf");
921 // Legacy configuration file location
922 filenames.push_back(porting::path_user +
923 DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
925 // Try also from a lower level (to aid having the same configuration
926 // for many RUN_IN_PLACE installs)
927 filenames.push_back(porting::path_user +
928 DIR_DELIM + ".." + DIR_DELIM + ".." + DIR_DELIM + "minetest.conf");
931 for(u32 i=0; i<filenames.size(); i++)
933 bool r = g_settings->readConfigFile(filenames[i].c_str());
936 configpath = filenames[i];
941 // If no path found, use the first one (menu creates the file)
943 configpath = filenames[0];
946 // Initialize random seed
954 if((ENABLE_TESTS && cmd_args.getFlag("disable-unittests") == false)
955 || cmd_args.getFlag("enable-unittests") == true)
966 if(cmd_args.exists("port"))
967 port = cmd_args.getU16("port");
968 else if(g_settings->exists("port"))
969 port = g_settings->getU16("port");
974 std::string world_path = porting::path_user + DIR_DELIM + "server" + DIR_DELIM + "worlds" + DIR_DELIM + "world";
975 if(cmd_args.exists("world"))
976 world_path = cmd_args.get("world");
977 else if(cmd_args.exists("map-dir"))
978 world_path = cmd_args.get("map-dir");
979 else if(g_settings->exists("map-dir"))
980 world_path = g_settings->get("map-dir");
982 // No map-dir option was specified.
983 // Check if the world is found from the default directory, and if
984 // not, see if the legacy world directory exists.
985 std::string legacy_world_path = porting::path_user+DIR_DELIM+".."+DIR_DELIM+"world";
986 if(!fs::PathExists(world_path) && fs::PathExists(legacy_world_path)){
987 errorstream<<"Warning: Using legacy world directory \""
988 <<legacy_world_path<<"\""<<std::endl;
989 world_path = legacy_world_path;
994 std::string gameid = "";
995 if(cmd_args.exists("gameid"))
996 gameid = cmd_args.get("gameid");
997 std::string world_gameid = getWorldGameId(world_path);
998 if(world_gameid == ""){
1000 world_gameid = gameid;
1002 world_gameid = "mesetint";
1006 gameid = world_gameid;
1007 else if(world_gameid != ""){
1008 if(world_gameid != gameid){
1009 errorstream<<"World gameid mismatch"<<std::endl;
1014 errorstream<<"No gameid supplied or detected"<<std::endl;
1018 infostream<<"Using gameid \""<<gameid<<"\""<<std::endl;
1020 SubgameSpec gamespec = findSubgame(gameid);
1021 if(!gamespec.isValid()){
1022 errorstream<<"Game \""<<gameid<<"\" not found"<<std::endl;
1026 // Run dedicated server if asked to or no other option
1028 bool run_dedicated_server = true;
1030 bool run_dedicated_server = cmd_args.getFlag("server");
1032 if(run_dedicated_server)
1034 DSTACK("Dedicated server branch");
1036 // Create time getter if built with Irrlicht
1038 g_timegetter = new SimpleTimeGetter();
1042 Server server(world_path, configpath, gamespec);
1046 dedicated_server_loop(server, kill);
1051 #ifndef SERVER // Exclude from dedicated server build
1057 std::string address = g_settings->get("address");
1058 if(cmd_args.exists("address"))
1059 address = cmd_args.get("address");
1061 std::string playername = g_settings->get("name");
1062 if(cmd_args.exists("name"))
1063 playername = cmd_args.get("name");
1065 bool skip_main_menu = cmd_args.getFlag("go");
1068 Device initialization
1071 // Resolution selection
1073 bool fullscreen = false;
1074 u16 screenW = g_settings->getU16("screenW");
1075 u16 screenH = g_settings->getU16("screenH");
1079 video::E_DRIVER_TYPE driverType;
1081 std::string driverstring = g_settings->get("video_driver");
1083 if(driverstring == "null")
1084 driverType = video::EDT_NULL;
1085 else if(driverstring == "software")
1086 driverType = video::EDT_SOFTWARE;
1087 else if(driverstring == "burningsvideo")
1088 driverType = video::EDT_BURNINGSVIDEO;
1089 else if(driverstring == "direct3d8")
1090 driverType = video::EDT_DIRECT3D8;
1091 else if(driverstring == "direct3d9")
1092 driverType = video::EDT_DIRECT3D9;
1093 else if(driverstring == "opengl")
1094 driverType = video::EDT_OPENGL;
1097 errorstream<<"WARNING: Invalid video_driver specified; defaulting "
1098 "to opengl"<<std::endl;
1099 driverType = video::EDT_OPENGL;
1103 Create device and exit if creation failed
1106 MyEventReceiver receiver;
1108 IrrlichtDevice *device;
1109 device = createDevice(driverType,
1110 core::dimension2d<u32>(screenW, screenH),
1111 16, fullscreen, false, false, &receiver);
1114 return 1; // could not create selected driver.
1117 Continue initialization
1120 video::IVideoDriver* driver = device->getVideoDriver();
1122 // Disable mipmaps (because some of them look ugly)
1123 driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
1126 This changes the minimum allowed number of vertices in a VBO.
1129 //driver->setMinHardwareBufferVertexCount(50);
1131 // Create time getter
1132 g_timegetter = new IrrlichtTimeGetter(device);
1134 // Create game callback for menus
1135 g_gamecallback = new MainGameCallback(device);
1138 Speed tests (done after irrlicht is loaded to get timer)
1140 if(cmd_args.getFlag("speedtests"))
1142 dstream<<"Running speed tests"<<std::endl;
1147 device->setResizable(true);
1149 bool random_input = g_settings->getBool("random_input")
1150 || cmd_args.getFlag("random-input");
1151 InputHandler *input = NULL;
1153 input = new RandomInputHandler();
1155 input = new RealInputHandler(device, &receiver);
1157 scene::ISceneManager* smgr = device->getSceneManager();
1159 guienv = device->getGUIEnvironment();
1160 gui::IGUISkin* skin = guienv->getSkin();
1161 gui::IGUIFont* font = guienv->getFont(getTexturePath("fontlucida.png").c_str());
1163 skin->setFont(font);
1165 errorstream<<"WARNING: Font file was not found."
1166 " Using default font."<<std::endl;
1167 // If font was not found, this will get us one
1168 font = skin->getFont();
1171 u32 text_height = font->getDimension(L"Hello, world!").Height;
1172 infostream<<"text_height="<<text_height<<std::endl;
1174 //skin->setColor(gui::EGDC_BUTTON_TEXT, video::SColor(255,0,0,0));
1175 skin->setColor(gui::EGDC_BUTTON_TEXT, video::SColor(255,255,255,255));
1176 //skin->setColor(gui::EGDC_3D_HIGH_LIGHT, video::SColor(0,0,0,0));
1177 //skin->setColor(gui::EGDC_3D_SHADOW, video::SColor(0,0,0,0));
1178 skin->setColor(gui::EGDC_3D_HIGH_LIGHT, video::SColor(255,0,0,0));
1179 skin->setColor(gui::EGDC_3D_SHADOW, video::SColor(255,0,0,0));
1185 ChatBackend chat_backend;
1188 If an error occurs, this is set to something and the
1189 menu-game loop is restarted. It is then displayed before
1192 std::wstring error_message = L"";
1194 // The password entered during the menu screen,
1195 std::string password;
1200 while(device->run() && kill == false)
1202 // Set the window caption
1203 device->setWindowCaption(L"Minetest [Main Menu]");
1205 // This is used for catching disconnects
1210 Clear everything from the GUIEnvironment
1215 We need some kind of a root node to be able to add
1216 custom gui elements directly on the screen.
1217 Otherwise they won't be automatically drawn.
1219 guiroot = guienv->addStaticText(L"",
1220 core::rect<s32>(0, 0, 10000, 10000));
1223 Out-of-game menu loop.
1225 Loop quits when menu returns proper parameters.
1227 while(kill == false)
1229 // Cursor can be non-visible when coming from the game
1230 device->getCursorControl()->setVisible(true);
1231 // Some stuff are left to scene manager when coming from the game
1234 // Reset or hide the debug gui texts
1235 /*guitext->setText(L"Minetest-c55");
1236 guitext2->setVisible(false);
1237 guitext_info->setVisible(false);
1238 guitext_chat->setVisible(false);*/
1240 // Initialize menu data
1241 MainMenuData menudata;
1242 menudata.address = narrow_to_wide(address);
1243 menudata.name = narrow_to_wide(playername);
1244 menudata.port = narrow_to_wide(itos(port));
1245 menudata.fancy_trees = g_settings->getBool("new_style_leaves");
1246 menudata.smooth_lighting = g_settings->getBool("smooth_lighting");
1247 menudata.clouds_3d = g_settings->getBool("enable_3d_clouds");
1248 menudata.opaque_water = g_settings->getBool("opaque_water");
1249 menudata.creative_mode = g_settings->getBool("creative_mode");
1250 menudata.enable_damage = g_settings->getBool("enable_damage");
1251 if(cmd_args.exists("password"))
1252 menudata.password = narrow_to_wide(cmd_args.get("password"));
1254 if(skip_main_menu == false)
1257 new GUIMainMenu(guienv, guiroot, -1,
1258 &g_menumgr, &menudata, g_gamecallback);
1259 menu->allowFocusRemoval(true);
1261 if(error_message != L"")
1263 verbosestream<<"error_message = "
1264 <<wide_to_narrow(error_message)<<std::endl;
1266 GUIMessageMenu *menu2 =
1267 new GUIMessageMenu(guienv, guiroot, -1,
1268 &g_menumgr, error_message.c_str());
1270 error_message = L"";
1273 video::IVideoDriver* driver = device->getVideoDriver();
1275 infostream<<"Created main menu"<<std::endl;
1277 while(device->run() && kill == false)
1279 if(menu->getStatus() == true)
1282 //driver->beginScene(true, true, video::SColor(255,0,0,0));
1283 driver->beginScene(true, true, video::SColor(255,128,128,128));
1285 drawMenuBackground(driver);
1291 // On some computers framerate doesn't seem to be
1292 // automatically limited
1296 // Break out of menu-game loop to shut down cleanly
1297 if(device->run() == false || kill == true)
1300 infostream<<"Dropping main menu"<<std::endl;
1304 // Delete map if requested
1305 if(menudata.delete_map)
1307 bool r = fs::RecursiveDeleteContent(world_path);
1309 error_message = L"Delete failed";
1314 playername = wide_to_narrow(menudata.name);
1315 password = translatePassword(playername, menudata.password);
1316 //infostream<<"Main: password hash: '"<<password<<"'"<<std::endl;
1318 address = wide_to_narrow(menudata.address);
1319 int newport = stoi(wide_to_narrow(menudata.port));
1323 g_settings->set("new_style_leaves", itos(menudata.fancy_trees));
1324 g_settings->set("smooth_lighting", itos(menudata.smooth_lighting));
1325 g_settings->set("enable_3d_clouds", itos(menudata.clouds_3d));
1326 g_settings->set("opaque_water", itos(menudata.opaque_water));
1327 g_settings->set("creative_mode", itos(menudata.creative_mode));
1328 g_settings->set("enable_damage", itos(menudata.enable_damage));
1329 g_settings->set("name", playername);
1330 g_settings->set("address", address);
1331 g_settings->set("port", itos(port));
1332 // Update configuration file
1333 if(configpath != "")
1334 g_settings->updateConfigFile(configpath.c_str());
1340 // Break out of menu-game loop to shut down cleanly
1341 if(device->run() == false || kill == true)
1365 catch(con::PeerNotFoundException &e)
1367 errorstream<<"Connection error (timed out?)"<<std::endl;
1368 error_message = L"Connection error (timed out?)";
1370 catch(SocketException &e)
1372 errorstream<<"Socket error (port already in use?)"<<std::endl;
1373 error_message = L"Socket error (port already in use?)";
1377 errorstream<<e.what()<<std::endl;
1378 error_message = narrow_to_wide(e.what()) + L"\nCheck debug.txt for details.";
1381 catch(std::exception &e)
1383 std::string narrow_message = "Some exception, what()=\"";
1384 narrow_message += e.what();
1385 narrow_message += "\"";
1386 errorstream<<narrow_message<<std::endl;
1387 error_message = narrow_to_wide(narrow_message);
1391 // If no main menu, show error and exit
1394 if(error_message != L""){
1395 verbosestream<<"error_message = "
1396 <<wide_to_narrow(error_message)<<std::endl;
1406 In the end, delete the Irrlicht device.
1412 // Update configuration file
1413 if(configpath != "")
1414 g_settings->updateConfigFile(configpath.c_str());
1416 END_DEBUG_EXCEPTION_HANDLER(errorstream)
1418 debugstreams_deinit();