51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#include "mainmenumanager.h"
+#include "gui/mainmenumanager.h"
#include "clouds.h"
#include "server.h"
#include "filesys.h"
-#include "guiMainMenu.h"
+#include "gui/guiMainMenu.h"
#include "game.h"
+#include "player.h"
#include "chat.h"
#include "gettext.h"
#include "profiler.h"
#include "serverlist.h"
-#include "guiEngine.h"
+#include "gui/guiEngine.h"
#include "fontengine.h"
#include "clientlauncher.h"
#include "version.h"
#include "renderingengine.h"
+#include "network/networkexceptions.h"
+
+#if USE_SOUND
+ #include "sound_openal.h"
+#endif
+#ifdef __ANDROID__
+ #include "porting.h"
+#endif
/* mainmenumanager.h
*/
// Passed to menus to allow disconnecting and exiting
MainGameCallback *g_gamecallback = nullptr;
+#if 0
+// This can be helpful for the next code cleanup
+static void dump_start_data(const GameStartData &data)
+{
+ std::cout <<
+ "\ndedicated " << (int)data.is_dedicated_server <<
+ "\nport " << data.socket_port <<
+ "\nworld_path " << data.world_spec.path <<
+ "\nworld game " << data.world_spec.gameid <<
+ "\ngame path " << data.game_spec.path <<
+ "\nplayer name " << data.name <<
+ "\naddress " << data.address << std::endl;
+}
+#endif
ClientLauncher::~ClientLauncher()
{
delete g_gamecallback;
delete RenderingEngine::get_instance();
+
+#if USE_SOUND
+ g_sound_manager_singleton.reset();
+#endif
}
-bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
+bool ClientLauncher::run(GameStartData &start_data, const Settings &cmd_args)
{
- init_args(game_params, cmd_args);
+ /* This function is called when a client must be started.
+ * Covered cases:
+ * - Singleplayer (address but map provided)
+ * - Join server (no map but address provided)
+ * - Local server (for main menu only)
+ */
+
+ init_args(start_data, cmd_args);
// List video modes if requested
if (list_video_modes)
return RenderingEngine::print_video_modes();
+#if USE_SOUND
+ if (g_settings->getBool("enable_sound"))
+ g_sound_manager_singleton = createSoundManagerSingleton();
+#endif
+
if (!init_engine()) {
errorstream << "Could not initialize game engine." << std::endl;
return false;
return true;
}
- video::IVideoDriver *video_driver = RenderingEngine::get_video_driver();
- if (video_driver == NULL) {
+ if (RenderingEngine::get_video_driver() == NULL) {
errorstream << "Could not initialize video driver." << std::endl;
return false;
}
- RenderingEngine::setXorgClassHint(video_driver->getExposedVideoData(), PROJECT_NAME_C);
- RenderingEngine::get_instance()->setWindowIcon();
+ RenderingEngine::get_instance()->setupTopLevelWindow(PROJECT_NAME_C);
/*
This changes the minimum allowed number of vertices in a VBO.
skin->setColor(gui::EGDC_3D_SHADOW, video::SColor(255, 0, 0, 0));
skin->setColor(gui::EGDC_HIGH_LIGHT, video::SColor(255, 70, 120, 50));
skin->setColor(gui::EGDC_HIGH_LIGHT_TEXT, video::SColor(255, 255, 255, 255));
-
+#ifdef __ANDROID__
+ float density = porting::getDisplayDensity();
+ skin->setSize(gui::EGDS_CHECK_BOX_WIDTH, (s32)(17.0f * density));
+ skin->setSize(gui::EGDS_SCROLLBAR_SIZE, (s32)(14.0f * density));
+ skin->setSize(gui::EGDS_WINDOW_BUTTON_WIDTH, (s32)(15.0f * density));
+ if (density > 1.5f) {
+ std::string sprite_path = porting::path_user + "/textures/base/pack/";
+ if (density > 3.5f)
+ sprite_path.append("checkbox_64.png");
+ else if (density > 2.0f)
+ sprite_path.append("checkbox_32.png");
+ else
+ sprite_path.append("checkbox_16.png");
+ // Texture dimensions should be a power of 2
+ gui::IGUISpriteBank *sprites = skin->getSpriteBank();
+ video::IVideoDriver *driver = RenderingEngine::get_video_driver();
+ video::ITexture *sprite_texture = driver->getTexture(sprite_path.c_str());
+ if (sprite_texture) {
+ s32 sprite_id = sprites->addTextureAsSprite(sprite_texture);
+ if (sprite_id != -1)
+ skin->setIcon(gui::EGDI_CHECK_BOX_CHECKED, sprite_id);
+ }
+ }
+#endif
g_fontengine = new FontEngine(g_settings, guienv);
FATAL_ERROR_IF(g_fontengine == NULL, "Font engine creation failed.");
if (!g_menucloudsmgr)
g_menucloudsmgr = RenderingEngine::get_scene_manager()->createNewSceneManager();
if (!g_menuclouds)
- g_menuclouds = new Clouds(g_menucloudsmgr, -1, rand(), 100);
- g_menuclouds->update(v2f(0, 0), video::SColor(255, 200, 200, 255));
+ g_menuclouds = new Clouds(g_menucloudsmgr, -1, rand());
+ g_menuclouds->setHeight(100.0f);
+ g_menuclouds->update(v3f(0, 0, 0), video::SColor(255, 240, 240, 255));
scene::ICameraSceneNode* camera;
- camera = g_menucloudsmgr->addCameraSceneNode(0,
- v3f(0, 0, 0), v3f(0, 60, 100));
+ camera = g_menucloudsmgr->addCameraSceneNode(NULL, v3f(0, 0, 0), v3f(0, 60, 100));
camera->setFarValue(10000);
/*
core::rect<s32>(0, 0, 10000, 10000));
bool game_has_run = launch_game(error_message, reconnect_requested,
- game_params, cmd_args);
+ start_data, cmd_args);
// Reset the reconnect_requested flag
reconnect_requested = false;
if (!game_has_run) {
if (skip_main_menu)
break;
- else
- continue;
+
+ continue;
}
// Break out of menu-game loop to shut down cleanly
if (!RenderingEngine::get_raw_device()->run() || *kill) {
- if (g_settings_path != "")
+ if (!g_settings_path.empty())
g_settings->updateConfigFile(g_settings_path.c_str());
break;
}
- if (current_playername.length() > PLAYERNAME_SIZE-1) {
- error_message = gettext("Player name too long.");
- playername = current_playername.substr(0, PLAYERNAME_SIZE-1);
- g_settings->set("name", playername);
- continue;
- }
-
RenderingEngine::get_video_driver()->setTextureCreationFlag(
video::ETCF_CREATE_MIP_MAPS, g_settings->getBool("mip_map"));
#ifdef HAVE_TOUCHSCREENGUI
- receiver->m_touchscreengui = new TouchScreenGUI(device, receiver);
+ receiver->m_touchscreengui = new TouchScreenGUI(RenderingEngine::get_raw_device(), receiver);
g_touchscreengui = receiver->m_touchscreengui;
#endif
the_game(
kill,
- random_input,
input,
- worldspec.path,
- current_playername,
- current_password,
- current_address,
- current_port,
+ start_data,
error_message,
chat_backend,
- &reconnect_requested,
- gamespec,
- simple_singleplayer_mode
+ &reconnect_requested
);
RenderingEngine::get_scene_manager()->clear();
return retval;
}
-void ClientLauncher::init_args(GameParams &game_params, const Settings &cmd_args)
+void ClientLauncher::init_args(GameStartData &start_data, const Settings &cmd_args)
{
-
skip_main_menu = cmd_args.getFlag("go");
- // FIXME: This is confusing (but correct)
-
- /* If world_path is set then override it unless skipping the main menu using
- * the --go command line param. Else, give preference to the address
- * supplied on the command line
- */
- address = g_settings->get("address");
- if (game_params.world_path != "" && !skip_main_menu)
- address = "";
- else if (cmd_args.exists("address"))
- address = cmd_args.get("address");
+ start_data.address = g_settings->get("address");
+ if (cmd_args.exists("address")) {
+ // Join a remote server
+ start_data.address = cmd_args.get("address");
+ start_data.world_path.clear();
+ start_data.name = g_settings->get("name");
+ }
+ if (!start_data.world_path.empty()) {
+ // Start a singleplayer instance
+ start_data.address = "";
+ }
- playername = g_settings->get("name");
if (cmd_args.exists("name"))
- playername = cmd_args.get("name");
+ start_data.name = cmd_args.get("name");
list_video_modes = cmd_args.getFlag("videomodes");
- use_freetype = g_settings->getBool("freetype");
-
random_input = g_settings->getBool("random_input")
|| cmd_args.getFlag("random-input");
}
}
bool ClientLauncher::launch_game(std::string &error_message,
- bool reconnect_requested, GameParams &game_params,
+ bool reconnect_requested, GameStartData &start_data,
const Settings &cmd_args)
{
- // Initialize menu data
- MainMenuData menudata;
- 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;
-
+ // Prepare and check the start data to launch a game
+ std::string error_message_lua = error_message;
error_message.clear();
if (cmd_args.exists("password"))
- menudata.password = cmd_args.get("password");
+ start_data.password = cmd_args.get("password");
+
+ if (cmd_args.exists("password-file")) {
+ std::ifstream passfile(cmd_args.get("password-file"));
+ if (passfile.good()) {
+ getline(passfile, start_data.password);
+ } else {
+ error_message = gettext("Provided password file "
+ "failed to open: ")
+ + cmd_args.get("password-file");
+ errorstream << error_message << std::endl;
+ return false;
+ }
+ }
// If a world was commanded, append and select it
- if (game_params.world_path != "") {
- worldspec.gameid = getWorldGameId(game_params.world_path, true);
- worldspec.name = _("[--world parameter]");
+ // This is provieded by "get_world_from_cmdline()", main.cpp
+ if (!start_data.world_path.empty()) {
+ auto &spec = start_data.world_spec;
+
+ spec.path = start_data.world_path;
+ spec.gameid = getWorldGameId(spec.path, true);
+ spec.name = _("[--world parameter]");
- if (worldspec.gameid == "") { // Create new
- worldspec.gameid = g_settings->get("default_game");
- worldspec.name += " [new]";
+ if (spec.gameid.empty()) { // Create new
+ spec.gameid = g_settings->get("default_game");
+ spec.name += " [new]";
}
- worldspec.path = game_params.world_path;
}
/* Show the GUI menu
*/
+ std::string server_name, server_description;
if (!skip_main_menu) {
+ // Initialize menu data
+ // TODO: Re-use existing structs (GameStartData)
+ MainMenuData menudata;
+ menudata.address = start_data.address;
+ menudata.name = start_data.name;
+ menudata.password = start_data.password;
+ menudata.port = itos(start_data.socket_port);
+ menudata.script_data.errormessage = error_message_lua;
+ menudata.script_data.reconnect_requested = reconnect_requested;
+
main_menu(&menudata);
// Skip further loading if there was an exit signal.
if (*porting::signal_handler_killstatus())
return false;
- address = menudata.address;
+ 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.script_data.errormessage;
+ return false;
+ }
+
int newport = stoi(menudata.port);
if (newport != 0)
- game_params.socket_port = newport;
-
- simple_singleplayer_mode = menudata.simple_singleplayer_mode;
+ start_data.socket_port = newport;
+ // Update world information using main menu data
std::vector<WorldSpec> worldspecs = getAvailableWorlds();
- if (menudata.selected_world >= 0
- && menudata.selected_world < (int)worldspecs.size()) {
+ int world_index = menudata.selected_world;
+ if (world_index >= 0 && world_index < (int)worldspecs.size()) {
g_settings->set("selected_world_path",
- worldspecs[menudata.selected_world].path);
- worldspec = worldspecs[menudata.selected_world];
+ worldspecs[world_index].path);
+ start_data.world_spec = worldspecs[world_index];
}
+
+ start_data.name = menudata.name;
+ start_data.password = menudata.password;
+ start_data.address = std::move(menudata.address);
+ server_name = menudata.servername;
+ server_description = menudata.serverdescription;
+
+ start_data.local_server = !menudata.simple_singleplayer_mode &&
+ start_data.address.empty();
+ } else {
+ start_data.local_server = !start_data.world_path.empty() &&
+ start_data.address.empty() && !start_data.name.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.script_data.errormessage;
+ if (!RenderingEngine::run())
return false;
- }
- if (menudata.name == "" && !simple_singleplayer_mode) {
+ if (!start_data.isSinglePlayer() && start_data.name.empty()) {
error_message = gettext("Please choose a name!");
+ errorstream << error_message << std::endl;
return false;
}
- playername = menudata.name;
- password = menudata.password;
-
- current_playername = playername;
- current_password = password;
- current_address = address;
- current_port = game_params.socket_port;
-
// If using simple singleplayer mode, override
- if (simple_singleplayer_mode) {
- assert(skip_main_menu == false);
- current_playername = "singleplayer";
- current_password = "";
- current_address = "";
- current_port = myrand_range(49152, 65535);
+ if (start_data.isSinglePlayer()) {
+ start_data.name = "singleplayer";
+ start_data.password = "";
+ start_data.socket_port = myrand_range(49152, 65535);
} else {
- g_settings->set("name", playername);
- if (address != "") {
+ g_settings->set("name", start_data.name);
+ if (!start_data.address.empty()) {
ServerListSpec server;
- server["name"] = menudata.servername;
- server["address"] = menudata.address;
- server["port"] = menudata.port;
- server["description"] = menudata.serverdescription;
+ server["name"] = server_name;
+ server["address"] = start_data.address;
+ server["port"] = itos(start_data.socket_port);
+ server["description"] = server_description;
ServerList::insert(server);
}
}
+ if (start_data.name.length() > PLAYERNAME_SIZE - 1) {
+ error_message = gettext("Player name too long.");
+ start_data.name.resize(PLAYERNAME_SIZE);
+ g_settings->set("name", start_data.name);
+ return false;
+ }
+
+ auto &worldspec = start_data.world_spec;
infostream << "Selected world: " << worldspec.name
<< " [" << worldspec.path << "]" << std::endl;
- if (current_address == "") { // If local game
- if (worldspec.path == "") {
+ if (start_data.address.empty()) {
+ // For singleplayer and local server
+ if (worldspec.path.empty()) {
error_message = gettext("No world selected and no address "
"provided. Nothing to do.");
errorstream << error_message << std::endl;
}
// Load gamespec for required game
- gamespec = findWorldSubgame(worldspec.path);
- if (!gamespec.isValid() && !game_params.game_spec.isValid()) {
+ start_data.game_spec = findWorldSubgame(worldspec.path);
+ if (!start_data.game_spec.isValid()) {
error_message = gettext("Could not find or load game \"")
+ worldspec.gameid + "\"";
errorstream << error_message << std::endl;
if (porting::signal_handler_killstatus())
return true;
- if (game_params.game_spec.isValid() &&
- game_params.game_spec.id != worldspec.gameid) {
- warningstream << "Overriding gamespec from \""
- << worldspec.gameid << "\" to \""
- << game_params.game_spec.id << "\"" << std::endl;
- gamespec = game_params.game_spec;
- }
-
- if (!gamespec.isValid()) {
+ if (!start_data.game_spec.isValid()) {
error_message = gettext("Invalid gamespec.");
error_message += " (world.gameid=" + worldspec.gameid + ")";
errorstream << error_message << std::endl;
}
}
+ start_data.world_path = start_data.world_spec.path;
return true;
}
video::IVideoDriver *driver = RenderingEngine::get_video_driver();
infostream << "Waiting for other menus" << std::endl;
- while (RenderingEngine::get_raw_device()->run() && *kill == false) {
+ while (RenderingEngine::get_raw_device()->run() && !*kill) {
if (!isMenuActive())
break;
driver->beginScene(true, true, video::SColor(255, 128, 128, 128));