From 4a9135c8c0b2ab2c6f41b04895c1a27cf9bd1bdf Mon Sep 17 00:00:00 2001 From: Benau Date: Sat, 15 Feb 2020 16:38:49 +0800 Subject: [PATCH] Avoid pushing and using screen in no graphics --- src/guiengine/abstract_state_manager.cpp | 6 ++ src/guiengine/abstract_state_manager.hpp | 6 ++ src/main.cpp | 8 ++- src/network/protocols/client_lobby.cpp | 79 ++++++++++++++------- src/network/protocols/connect_to_server.cpp | 4 +- src/network/protocols/lobby_protocol.cpp | 44 ++++++++++++ src/network/protocols/lobby_protocol.hpp | 2 + src/network/protocols/server_lobby.cpp | 6 +- src/states_screens/race_result_gui.cpp | 25 ------- src/states_screens/race_result_gui.hpp | 5 +- src/states_screens/state_manager.cpp | 4 +- 11 files changed, 125 insertions(+), 64 deletions(-) diff --git a/src/guiengine/abstract_state_manager.cpp b/src/guiengine/abstract_state_manager.cpp index a5cf469ec..b7e495864 100644 --- a/src/guiengine/abstract_state_manager.cpp +++ b/src/guiengine/abstract_state_manager.cpp @@ -53,6 +53,12 @@ void AbstractStateManager::enterGameState() assert(!ModalDialog::isADialogActive()); assert(!ScreenKeyboard::isActive()); + if (GUIEngine::isNoGraphics()) + { + setGameState(GAME); + return; + } + if (getCurrentScreen() != NULL) getCurrentScreen()->tearDown(); m_menu_stack.clear(); m_menu_stack.emplace_back(RACE_STATE_NAME, (Screen*)NULL); diff --git a/src/guiengine/abstract_state_manager.hpp b/src/guiengine/abstract_state_manager.hpp index 39b194903..bc6c41c46 100644 --- a/src/guiengine/abstract_state_manager.hpp +++ b/src/guiengine/abstract_state_manager.hpp @@ -110,6 +110,12 @@ namespace GUIEngine */ void resetAndSetStack(Screen* screens[]); + /** + * \brief Used in no graphics STK to enter menu screen (when server is + * idle state) + */ + void enterMenuState() { setGameState(MENU); } + /** * \brief call to make the state manager enter game mode. * Causes the menu stack to be cleared; all widgets shown on screen diff --git a/src/main.cpp b/src/main.cpp index 56a7c548e..08252deac 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1408,7 +1408,10 @@ int handleCmdLine(bool has_server_config, bool has_parent_process) else NetworkConfig::get()->setIsLAN(); STKHost::create(); - NetworkingLobby::getInstance()->setJoinedServer(server); + if (!GUIEngine::isNoGraphics()) + NetworkingLobby::getInstance()->setJoinedServer(server); + else if (NetworkConfig::get()->isClient()) + std::make_shared(server)->requestStart(); } if (NetworkConfig::get()->isServer()) @@ -2253,7 +2256,8 @@ int main(int argc, char *argv[]) if (STKHost::existHost()) { - NetworkingLobby::getInstance()->push(); + if (!GUIEngine::isNoGraphics()) + NetworkingLobby::getInstance()->push(); } else if (!UserConfigParams::m_no_start_screen) { diff --git a/src/network/protocols/client_lobby.cpp b/src/network/protocols/client_lobby.cpp index aa01b5e32..42238cf01 100644 --- a/src/network/protocols/client_lobby.cpp +++ b/src/network/protocols/client_lobby.cpp @@ -24,6 +24,7 @@ #include "config/user_config.hpp" #include "config/player_manager.hpp" #include "graphics/camera.hpp" +#include "guiengine/engine.hpp" #include "guiengine/modaldialog.hpp" #include "guiengine/message_queue.hpp" #include "guiengine/screen_keyboard.hpp" @@ -57,7 +58,6 @@ #include "states_screens/dialogs/addons_pack.hpp" #include "states_screens/online/networking_lobby.hpp" #include "states_screens/online/network_kart_selection.hpp" -#include "states_screens/race_result_gui.hpp" #include "states_screens/online/tracks_screen.hpp" #include "tracks/track.hpp" #include "tracks/track_manager.hpp" @@ -134,7 +134,8 @@ void ClientLobby::setup() m_auto_back_to_lobby_time = std::numeric_limits::max(); m_start_live_game_time = std::numeric_limits::max(); m_received_server_result = false; - TracksScreen::getInstance()->resetVote(); + if (!GUIEngine::isNoGraphics()) + TracksScreen::getInstance()->resetVote(); LobbyProtocol::setup(); m_state.store(NONE); } // setup @@ -264,7 +265,8 @@ void ClientLobby::addAllPlayers(Event* event) PeerVote winner_vote(data); m_game_setup->setRace(winner_vote); - TracksScreen::getInstance()->setResult(winner_peer_id, winner_vote); + if (!GUIEngine::isNoGraphics()) + TracksScreen::getInstance()->setResult(winner_peer_id, winner_vote); std::shared_ptr peer = event->getPeerSP(); peer->cleanPlayerProfiles(); @@ -547,8 +549,11 @@ void ClientLobby::receivePlayerVote(Event* event) vote.m_track_name.c_str()); } addVote(host_id, vote); - TracksScreen::getInstance()->addVote(host_id, vote); - TracksScreen::getInstance()->updatePlayerVotes(); + if (!GUIEngine::isNoGraphics()) + { + TracksScreen::getInstance()->addVote(host_id, vote); + TracksScreen::getInstance()->updatePlayerVotes(); + } } // receivePlayerVote //----------------------------------------------------------------------------- @@ -590,8 +595,11 @@ void ClientLobby::disconnectedPlayer(Event* event) // Use the friend icon to avoid an error-like message MessageQueue::add(MessageQueue::MT_FRIEND, msg); } - TracksScreen::getInstance()->removeVote(host_id); - TracksScreen::getInstance()->updatePlayerVotes(); + if (!GUIEngine::isNoGraphics()) + { + TracksScreen::getInstance()->removeVote(host_id); + TracksScreen::getInstance()->updatePlayerVotes(); + } } // disconnectedPlayer //----------------------------------------------------------------------------- @@ -640,7 +648,8 @@ void ClientLobby::connectionAccepted(Event* event) float auto_start_timer = data.getFloat(); int state_frequency_in_server = data.getUInt32(); NetworkConfig::get()->setStateFrequency(state_frequency_in_server); - if (auto_start_timer != std::numeric_limits::max()) + if (!GUIEngine::isNoGraphics() && + auto_start_timer != std::numeric_limits::max()) NetworkingLobby::getInstance()->setStartingTimerTo(auto_start_timer); m_server_enabled_chat = data.getUInt8() == 1; if (NetworkConfig::get()->getServerCapabilities().find("report_player") != @@ -731,8 +740,11 @@ void ClientLobby::handleServerInfo(Event* event) // Auto start info unsigned min_players = data.getUInt8(); float start_timeout = data.getFloat(); - NetworkingLobby::getInstance()->initAutoStartTimer(grand_prix_started, - min_players, start_timeout, max_player); + if (!GUIEngine::isNoGraphics()) + { + NetworkingLobby::getInstance()->initAutoStartTimer(grand_prix_started, + min_players, start_timeout, max_player); + } // MOTD core::stringw motd; @@ -745,10 +757,12 @@ void ClientLobby::handleServerInfo(Event* event) if (total_lines[total_lines.size() - 1] == L'\n') total_lines.erase(total_lines.size() - 1); - NetworkingLobby::getInstance()->addMoreServerInfo(total_lines); + if (!GUIEngine::isNoGraphics()) + NetworkingLobby::getInstance()->addMoreServerInfo(total_lines); bool server_config = data.getUInt8() == 1; - NetworkingLobby::getInstance()->toggleServerConfigButton(server_config); + if (!GUIEngine::isNoGraphics()) + NetworkingLobby::getInstance()->toggleServerConfigButton(server_config); m_server_live_joinable = data.getUInt8() == 1; } // handleServerInfo @@ -821,7 +835,8 @@ void ClientLobby::updatePlayerList(Event* event) SFXManager::get()->quickSound("energy_bar_full"); m_total_players = total_players; - NetworkingLobby::getInstance()->updatePlayers(); + if (!GUIEngine::isNoGraphics()) + NetworkingLobby::getInstance()->updatePlayers(); } // updatePlayerList //----------------------------------------------------------------------------- @@ -863,6 +878,8 @@ void ClientLobby::handleChat(Event* event) core::stringw message; event->data().decodeString16(&message); Log::info("ClientLobby", "%s", StringUtils::wideToUtf8(message).c_str()); + if (GUIEngine::isNoGraphics()) + return; if (message.size() > 0) { if (GUIEngine::getCurrentScreen() == NetworkingLobby::getInstance()) @@ -1003,10 +1020,14 @@ void ClientLobby::startSelection(Event* event) // In case the user opened a user info dialog GUIEngine::ModalDialog::dismiss(); GUIEngine::ScreenKeyboard::dismiss(); - NetworkKartSelectionScreen* screen = - NetworkKartSelectionScreen::getInstance(); - screen->setAvailableKartsFromServer(m_available_karts); - screen->setLiveJoin(false); + + if (!GUIEngine::isNoGraphics()) + { + NetworkKartSelectionScreen* screen = + NetworkKartSelectionScreen::getInstance(); + screen->setAvailableKartsFromServer(m_available_karts); + screen->setLiveJoin(false); + } // In case of auto-connect or continue a grand prix, use random karts // (or previous kart) from server and go to track selection if ((NetworkConfig::get()->isAutoConnect() || skip_kart_screen) && @@ -1019,17 +1040,23 @@ void ClientLobby::startSelection(Event* event) ->createActivePlayer(std::get<1>(p), std::get<0>(p)); } input_manager->getDeviceManager()->setAssignMode(ASSIGN); - TracksScreen::getInstance()->setQuitServer(); - TracksScreen::getInstance()->setNetworkTracks(); - TracksScreen::getInstance()->push(); + if (!GUIEngine::isNoGraphics()) + { + TracksScreen::getInstance()->setQuitServer(); + TracksScreen::getInstance()->setNetworkTracks(); + TracksScreen::getInstance()->push(); + } } - else + else if (!GUIEngine::isNoGraphics()) { - screen->push(); + NetworkKartSelectionScreen::getInstance()->push(); } - TracksScreen *ts = TracksScreen::getInstance(); - ts->resetVote(); + if (!GUIEngine::isNoGraphics()) + { + TracksScreen *ts = TracksScreen::getInstance(); + ts->resetVote(); + } m_state.store(SELECTING_ASSETS); Log::info("ClientLobby", "Selection starts now"); } // startSelection @@ -1137,10 +1164,10 @@ void ClientLobby::backToLobby(Event *event) { auto lock = gp->acquireWorldDeletingMutex(); ProtocolManager::lock()->findAndTerminate(PROTOCOL_CONTROLLER_EVENTS); - RaceResultGUI::getInstance()->backToLobby(); + exitGameState(); } else - RaceResultGUI::getInstance()->backToLobby(); + exitGameState(); NetworkString &data = event->data(); core::stringw msg; diff --git a/src/network/protocols/connect_to_server.cpp b/src/network/protocols/connect_to_server.cpp index 0aaf7e322..0ff356cfe 100644 --- a/src/network/protocols/connect_to_server.cpp +++ b/src/network/protocols/connect_to_server.cpp @@ -21,6 +21,7 @@ #include "config/user_config.hpp" #include "io/file_manager.hpp" #include "io/xml_node.hpp" +#include "guiengine/engine.hpp" #include "network/crypto.hpp" #include "network/event.hpp" #include "network/network.hpp" @@ -356,7 +357,8 @@ void ConnectToServer::update(int ticks) { // Make sure lobby display the quick play server name assert(m_server); - NetworkingLobby::getInstance()->setJoinedServer(m_server); + if (!GUIEngine::isNoGraphics()) + NetworkingLobby::getInstance()->setJoinedServer(m_server); break; } case DONE: diff --git a/src/network/protocols/lobby_protocol.cpp b/src/network/protocols/lobby_protocol.cpp index db73de877..f058e9179 100644 --- a/src/network/protocols/lobby_protocol.cpp +++ b/src/network/protocols/lobby_protocol.cpp @@ -21,6 +21,9 @@ #include "input/input_manager.hpp" #include "input/device_manager.hpp" +#include "guiengine/engine.hpp" +#include "guiengine/message_queue.hpp" +#include "guiengine/screen_keyboard.hpp" #include "graphics/render_info.hpp" #include "karts/abstract_kart.hpp" #include "karts/controller/controller.hpp" @@ -33,9 +36,13 @@ #include "network/protocols/game_events_protocol.hpp" #include "network/race_event_manager.hpp" #include "race/race_manager.hpp" +#include "states_screens/online/networking_lobby.hpp" +#include "states_screens/race_result_gui.hpp" #include "states_screens/state_manager.hpp" #include "tracks/track_manager.hpp" +#include "utils/string_utils.hpp" #include "utils/time.hpp" +#include "utils/translation.hpp" std::weak_ptr LobbyProtocol::m_lobby; @@ -251,3 +258,40 @@ Track* LobbyProtocol::getPlayingTrack() const ul.unlock(); return track_manager->getTrack(track_ident); } // getPlayingTrack + +//----------------------------------------------------------------------------- +void LobbyProtocol::exitGameState() +{ + bool create_gp_msg = false; + if (race_manager->getMajorMode() == RaceManager::MAJOR_MODE_GRAND_PRIX && + race_manager->getTrackNumber() == race_manager->getNumOfTracks() - 1) + { + create_gp_msg = true; + } + + race_manager->clearNetworkGrandPrixResult(); + race_manager->exitRace(); + race_manager->setAIKartOverride(""); + + if (GUIEngine::isNoGraphics()) + { + // No screen is ever created when no graphics is on + StateManager::get()->enterMenuState(); + return; + } + + GUIEngine::ModalDialog::dismiss(); + GUIEngine::ScreenKeyboard::dismiss(); + RaceResultGUI::getInstance()->cleanupGPProgress(); + if (create_gp_msg) + { + core::stringw msg = _("Network grand prix has been finished."); + MessageQueue::add(MessageQueue::MT_ACHIEVEMENT, msg); + } + + if (GUIEngine::getCurrentScreen() != NetworkingLobby::getInstance()) + { + StateManager::get()->resetAndSetStack( + NetworkConfig::get()->getResetScreens(true/*lobby*/).data()); + } +} // exitGameState diff --git a/src/network/protocols/lobby_protocol.hpp b/src/network/protocols/lobby_protocol.hpp index c357b661d..6a1523aa6 100644 --- a/src/network/protocols/lobby_protocol.hpp +++ b/src/network/protocols/lobby_protocol.hpp @@ -149,6 +149,8 @@ protected: // ------------------------------------------------------------------------ void addLiveJoiningKart(int kart_id, const RemoteKartInfo& rki, int live_join_util_ticks) const; + // ------------------------------------------------------------------------ + void exitGameState(); public: /** Creates either a client or server lobby protocol as a singleton. */ diff --git a/src/network/protocols/server_lobby.cpp b/src/network/protocols/server_lobby.cpp index 7bc714601..2028e8647 100644 --- a/src/network/protocols/server_lobby.cpp +++ b/src/network/protocols/server_lobby.cpp @@ -49,8 +49,6 @@ #include "online/request_manager.hpp" #include "online/xml_request.hpp" #include "race/race_manager.hpp" -#include "states_screens/online/networking_lobby.hpp" -#include "states_screens/race_result_gui.hpp" #include "tracks/check_manager.hpp" #include "tracks/track.hpp" #include "tracks/track_manager.hpp" @@ -2055,7 +2053,7 @@ void ServerLobby::update(int ticks) !GameProtocol::emptyInstance()) return; - RaceResultGUI::getInstance()->backToLobby(); + exitGameState(); m_rs_state.store(RS_ASYNC_RESET); } @@ -2152,7 +2150,7 @@ void ServerLobby::update(int ticks) return; // This will go back to lobby in server (and exit the current race) - RaceResultGUI::getInstance()->backToLobby(); + exitGameState(); // Reset for next state usage resetPeersReady(); // Set the delay before the server forces all clients to exit the race diff --git a/src/states_screens/race_result_gui.cpp b/src/states_screens/race_result_gui.cpp index cb08d095a..84a13e412 100644 --- a/src/states_screens/race_result_gui.cpp +++ b/src/states_screens/race_result_gui.cpp @@ -515,31 +515,6 @@ void RaceResultGUI::eventCallback(GUIEngine::Widget* widget, return; } // eventCallback -//----------------------------------------------------------------------------- -/** Sets up the gui to go back to the lobby. Can only be called in case of a - * networked game. - */ -void RaceResultGUI::backToLobby() -{ - if (race_manager->getMajorMode() == RaceManager::MAJOR_MODE_GRAND_PRIX && - race_manager->getTrackNumber() == race_manager->getNumOfTracks() - 1) - { - core::stringw msg = _("Network grand prix has been finished."); - MessageQueue::add(MessageQueue::MT_ACHIEVEMENT, msg); - } - race_manager->clearNetworkGrandPrixResult(); - race_manager->exitRace(); - race_manager->setAIKartOverride(""); - GUIEngine::ModalDialog::dismiss(); - GUIEngine::ScreenKeyboard::dismiss(); - cleanupGPProgress(); - if (GUIEngine::getCurrentScreen() != NetworkingLobby::getInstance()) - { - StateManager::get()->resetAndSetStack( - NetworkConfig::get()->getResetScreens(true/*lobby*/).data()); - } -} // backToLobby - //----------------------------------------------------------------------------- void RaceResultGUI::displayCTFResults() { diff --git a/src/states_screens/race_result_gui.hpp b/src/states_screens/race_result_gui.hpp index 2881f40be..f32cfeb5c 100644 --- a/src/states_screens/race_result_gui.hpp +++ b/src/states_screens/race_result_gui.hpp @@ -193,7 +193,6 @@ private: void enableGPProgress(); void addGPProgressWidget(GUIEngine::Widget* widget); void displayGPProgress(); - void cleanupGPProgress(); void displayPostRaceInfo(); void displayCTFResults(); void displaySoccerResults(); @@ -217,9 +216,6 @@ public: Input::InputType type, int playerId) OVERRIDE; void eventCallback(GUIEngine::Widget* widget, const std::string& name, const int playerID) OVERRIDE; - void backToLobby(); - - friend class GUIEngine::ScreenSingleton; /** Should not be called anymore. */ @@ -260,6 +256,7 @@ public: void setHighscore(int rank); virtual void onConfirm() OVERRIDE; + void cleanupGPProgress(); }; // RaceResultGUI #endif diff --git a/src/states_screens/state_manager.cpp b/src/states_screens/state_manager.cpp index 135ca6eae..b80eb0bec 100644 --- a/src/states_screens/state_manager.cpp +++ b/src/states_screens/state_manager.cpp @@ -152,8 +152,8 @@ void StateManager::resetActivePlayers() bool StateManager::throttleFPS() { #ifndef SERVER_ONLY - return m_game_mode != GUIEngine::GAME && - GUIEngine::getCurrentScreen()->throttleFPS(); + return m_game_mode != GUIEngine::GAME && GUIEngine::getCurrentScreen() && + GUIEngine::getCurrentScreen()->throttleFPS(); #else return true; #endif