]> git.lizzy.rs Git - minetest.git/blobdiff - src/client/clientlauncher.cpp
Clean up getTime helpers
[minetest.git] / src / client / clientlauncher.cpp
index d6327e25915123498d7ecb86a704376b6a36b213..be4e82134a4e3a93eb3ca147efe7f80d70702408 100644 (file)
@@ -32,7 +32,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "guiEngine.h"
 #include "player.h"
 #include "fontengine.h"
+#include "joystick_controller.h"
 #include "clientlauncher.h"
+#include "version.h"
 
 /* mainmenumanager.h
  */
@@ -49,22 +51,6 @@ bool noMenuActive()
 MainGameCallback *g_gamecallback = NULL;
 
 
-// Instance of the time getter
-static TimeGetter *g_timegetter = NULL;
-
-u32 getTimeMs()
-{
-       if (g_timegetter == NULL)
-               return 0;
-       return g_timegetter->getTime(PRECISION_MILLI);
-}
-
-u32 getTime(TimePrecision prec) {
-       if (g_timegetter == NULL)
-               return 0;
-       return g_timegetter->getTime(prec);
-}
-
 ClientLauncher::~ClientLauncher()
 {
        if (receiver)
@@ -89,14 +75,11 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
        if (list_video_modes)
                return print_video_modes();
 
-       if (!init_engine(game_params.log_level)) {
+       if (!init_engine()) {
                errorstream << "Could not initialize game engine." << std::endl;
                return false;
        }
 
-       // Create time getter
-       g_timegetter = new IrrlichtTimeGetter(device);
-
        // Speed tests (done after irrlicht is loaded to get timer)
        if (cmd_args.getFlag("speedtests")) {
                dstream << "Running speed tests" << std::endl;
@@ -112,6 +95,8 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
 
        porting::setXorgClassHint(video_driver->getExposedVideoData(), PROJECT_NAME_C);
 
+       porting::setWindowIcon(device);
+
        /*
                This changes the minimum allowed number of vertices in a VBO.
                Default is 500.
@@ -123,10 +108,7 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
 
        device->setResizable(true);
 
-       if (random_input)
-               input = new RandomInputHandler();
-       else
-               input = new RealInputHandler(device, receiver);
+       init_input();
 
        smgr = device->getSceneManager();
        smgr->getParameters()->setAttribute(scene::ALLOW_ZWRITE_ON_TRANSPARENT, true);
@@ -168,8 +150,9 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
        ChatBackend chat_backend;
 
        // If an error occurs, this is set to something by menu().
-       // It is then displayed before  the menu shows on the next call to menu()
+       // It is then displayed before the menu shows on the next call to menu()
        std::string error_message;
+       bool reconnect_requested = false;
 
        bool first_loop = true;
 
@@ -183,7 +166,9 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
        {
                // Set the window caption
                const wchar_t *text = wgettext("Main Menu");
-               device->setWindowCaption((narrow_to_wide(PROJECT_NAME_C) + L" [" + text + L"]").c_str());
+               device->setWindowCaption((utf8_to_wide(PROJECT_NAME_C) +
+                       L" " + utf8_to_wide(g_version_hash) +
+                       L" [" + text + L"]").c_str());
                delete[] text;
 
                try {   // This is used for catching disconnects
@@ -197,7 +182,11 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
                        */
                        guiroot = guienv->addStaticText(L"", core::rect<s32>(0, 0, 10000, 10000));
 
-                       bool game_has_run = launch_game(error_message, game_params, cmd_args);
+                       bool game_has_run = launch_game(error_message, reconnect_requested,
+                               game_params, cmd_args);
+
+                       // Reset the reconnect_requested flag
+                       reconnect_requested = false;
 
                        // If skip_main_menu, we only want to startup once
                        if (skip_main_menu && !first_loop)
@@ -233,6 +222,7 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
                        receiver->m_touchscreengui = new TouchScreenGUI(device, receiver);
                        g_touchscreengui = receiver->m_touchscreengui;
 #endif
+
                        the_game(
                                kill,
                                random_input,
@@ -245,6 +235,7 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
                                current_port,
                                error_message,
                                chat_backend,
+                               &reconnect_requested,
                                gamespec,
                                simple_singleplayer_mode
                        );
@@ -317,22 +308,52 @@ void ClientLauncher::init_args(GameParams &game_params, const Settings &cmd_args
                        || cmd_args.getFlag("random-input");
 }
 
-bool ClientLauncher::init_engine(int log_level)
+bool ClientLauncher::init_engine()
 {
        receiver = new MyEventReceiver();
-       create_engine_device(log_level);
+       create_engine_device();
        return device != NULL;
 }
 
+void ClientLauncher::init_input()
+{
+       if (random_input)
+               input = new RandomInputHandler();
+       else
+               input = new RealInputHandler(device, receiver);
+
+       if (g_settings->getBool("enable_joysticks")) {
+               irr::core::array<irr::SJoystickInfo> infos;
+               std::vector<irr::SJoystickInfo> joystick_infos;
+
+               // Make sure this is called maximum once per
+               // irrlicht device, otherwise it will give you
+               // multiple events for the same joystick.
+               if (device->activateJoysticks(infos)) {
+                       infostream << "Joystick support enabled" << std::endl;
+                       joystick_infos.reserve(infos.size());
+                       for (u32 i = 0; i < infos.size(); i++) {
+                               joystick_infos.push_back(infos[i]);
+                       }
+                       input->joystick.onJoystickConnect(joystick_infos);
+               } else {
+                       errorstream << "Could not activate joystick support." << std::endl;
+               }
+       }
+}
+
 bool ClientLauncher::launch_game(std::string &error_message,
-               GameParams &game_params, const Settings &cmd_args)
+               bool reconnect_requested, GameParams &game_params,
+               const Settings &cmd_args)
 {
        // Initialize menu data
        MainMenuData menudata;
-       menudata.address      = address;
-       menudata.name         = playername;
-       menudata.port         = itos(game_params.socket_port);
-       menudata.errormessage = error_message;
+       menudata.address                         = address;
+       menudata.name                            = playername;
+       menudata.password                        = password;
+       menudata.port                            = itos(game_params.socket_port);
+       menudata.script_data.errormessage        = error_message;
+       menudata.script_data.reconnect_requested = reconnect_requested;
 
        error_message.clear();
 
@@ -379,22 +400,21 @@ bool ClientLauncher::launch_game(std::string &error_message,
                }
        }
 
-       if (!menudata.errormessage.empty()) {
+       if (!menudata.script_data.errormessage.empty()) {
                /* The calling function will pass this back into this function upon the
                 * next iteration (if any) causing it to be displayed by the GUI
                 */
-               error_message = menudata.errormessage;
+               error_message = menudata.script_data.errormessage;
                return false;
        }
 
-       if (menudata.name == "")
-               menudata.name = std::string("Guest") + itos(myrand_range(1000, 9999));
-       else
-               playername = menudata.name;
-
-       password = translatePassword(playername, menudata.password);
+       if (menudata.name == "" && !simple_singleplayer_mode) {
+               error_message = gettext("Please choose a name!");
+               return false;
+       }
 
-       g_settings->set("name", playername);
+       playername = menudata.name;
+       password = menudata.password;
 
        current_playername = playername;
        current_password   = password;
@@ -408,13 +428,16 @@ bool ClientLauncher::launch_game(std::string &error_message,
                current_password = "";
                current_address = "";
                current_port = myrand_range(49152, 65535);
-       } else if (address != "") {
-               ServerListSpec server;
-               server["name"] = menudata.servername;
-               server["address"] = menudata.address;
-               server["port"] = menudata.port;
-               server["description"] = menudata.serverdescription;
-               ServerList::insert(server);
+       } else {
+               g_settings->set("name", playername);
+               if (address != "") {
+                       ServerListSpec server;
+                       server["name"] = menudata.servername;
+                       server["address"] = menudata.address;
+                       server["port"] = menudata.port;
+                       server["description"] = menudata.serverdescription;
+                       ServerList::insert(server);
+               }
        }
 
        infostream << "Selected world: " << worldspec.name
@@ -449,7 +472,7 @@ bool ClientLauncher::launch_game(std::string &error_message,
 
                if (game_params.game_spec.isValid() &&
                                game_params.game_spec.id != worldspec.gameid) {
-                       errorstream << "WARNING: Overriding gamespec from \""
+                       warningstream << "Overriding gamespec from \""
                                    << worldspec.gameid << "\" to \""
                                    << game_params.game_spec.id << "\"" << std::endl;
                        gamespec = game_params.game_spec;
@@ -489,25 +512,14 @@ void ClientLauncher::main_menu(MainMenuData *menudata)
 #endif
 
        /* show main menu */
-       GUIEngine mymenu(device, guiroot, &g_menumgr, smgr, menudata, *kill);
+       GUIEngine mymenu(device, &input->joystick, guiroot,
+               &g_menumgr, smgr, menudata, *kill);
 
        smgr->clear();  /* leave scene manager in a clean state */
 }
 
-bool ClientLauncher::create_engine_device(int log_level)
+bool ClientLauncher::create_engine_device()
 {
-       static const irr::ELOG_LEVEL irr_log_level[5] = {
-               ELL_NONE,
-               ELL_ERROR,
-               ELL_WARNING,
-               ELL_INFORMATION,
-#if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
-               ELL_INFORMATION
-#else
-               ELL_DEBUG
-#endif
-       };
-
        // Resolution selection
        bool fullscreen = g_settings->getBool("fullscreen");
        u16 screenW = g_settings->getU16("screenW");
@@ -518,9 +530,12 @@ bool ClientLauncher::create_engine_device(int log_level)
        u16 bits = g_settings->getU16("fullscreen_bpp");
        u16 fsaa = g_settings->getU16("fsaa");
 
+       // stereo buffer required for pageflip stereo
+       bool stereo_buffer = g_settings->get("3d_mode") == "pageflip";
+
        // Determine driver
        video::E_DRIVER_TYPE driverType = video::EDT_OPENGL;
-       std::string driverstring = g_settings->get("video_driver");
+       const std::string &driverstring = g_settings->get("video_driver");
        std::vector<video::E_DRIVER_TYPE> drivers
                = porting::getSupportedVideoDrivers();
        u32 i;
@@ -543,9 +558,11 @@ bool ClientLauncher::create_engine_device(int log_level)
        params.AntiAlias     = fsaa;
        params.Fullscreen    = fullscreen;
        params.Stencilbuffer = false;
+       params.Stereobuffer  = stereo_buffer;
        params.Vsync         = vsync;
        params.EventReceiver = receiver;
        params.HighPrecisionFPU = g_settings->getBool("high_precision_fpu");
+       params.ZBufferBits   = 24;
 #ifdef __ANDROID__
        params.PrivateData = porting::app_global;
        params.OGLES2ShaderPath = std::string(porting::path_user + DIR_DELIM +
@@ -554,13 +571,8 @@ bool ClientLauncher::create_engine_device(int log_level)
 
        device = createDeviceEx(params);
 
-       if (device) {
-               // Map our log level to irrlicht engine one.
-               ILogger* irr_logger = device->getLogger();
-               irr_logger->setLogLevel(irr_log_level[log_level]);
-
+       if (device)
                porting::initIrrlicht(device);
-       }
 
        return device != NULL;
 }
@@ -645,14 +657,14 @@ void ClientLauncher::speed_tests()
                infostream << "Around 5000/ms should do well here." << std::endl;
                TimeTaker timer("Testing mutex speed");
 
-               JMutex m;
+               Mutex m;
                u32 n = 0;
                u32 i = 0;
                do {
                        n += 10000;
                        for (; i < n; i++) {
-                               m.Lock();
-                               m.Unlock();
+                               m.lock();
+                               m.unlock();
                        }
                }
                // Do at least 10ms
@@ -690,7 +702,7 @@ bool ClientLauncher::print_video_modes()
                return false;
        }
 
-       dstream << _("Available video modes (WxHxD):") << std::endl;
+       std::cout << _("Available video modes (WxHxD):") << std::endl;
 
        video::IVideoModeList *videomode_list = nulldevice->getVideoModeList();
 
@@ -701,14 +713,14 @@ bool ClientLauncher::print_video_modes()
                for (s32 i = 0; i < videomode_count; ++i) {
                        videomode_res = videomode_list->getVideoModeResolution(i);
                        videomode_depth = videomode_list->getVideoModeDepth(i);
-                       dstream << videomode_res.Width << "x" << videomode_res.Height
+                       std::cout << videomode_res.Width << "x" << videomode_res.Height
                                << "x" << videomode_depth << std::endl;
                }
 
-               dstream << _("Active video mode (WxHxD):") << std::endl;
+               std::cout << _("Active video mode (WxHxD):") << std::endl;
                videomode_res = videomode_list->getDesktopResolution();
                videomode_depth = videomode_list->getDesktopDepth();
-               dstream << videomode_res.Width << "x" << videomode_res.Height
+               std::cout << videomode_res.Width << "x" << videomode_res.Height
                        << "x" << videomode_depth << std::endl;
 
        }