1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include <SDL.h>
#include <functional>
#include "Rendering/GL/myGL.h"
#include "LoadScreen.h"
#include "Game.h"
#include "GlobalUnsynced.h"
#include "Game/Players/Player.h"
#include "Game/Players/PlayerHandler.h"
#include "Game/UI/MouseHandler.h"
#include "ExternalAI/SkirmishAIHandler.h"
#include "Lua/LuaIntro.h"
#include "Lua/LuaMenu.h"
#include "Map/MapInfo.h"
#include "Rendering/Fonts/glFont.h"
#include "Rendering/GlobalRendering.h"
#include "Rendering/Textures/NamedTextures.h"
#include "Sim/Misc/TeamHandler.h"
#include "Sim/Path/IPathManager.h"
#include "System/Config/ConfigHandler.h"
#include "System/Exceptions.h"
#include "System/Sync/FPUCheck.h"
#include "System/Log/ILog.h"
#include "Net/Protocol/NetProtocol.h"
#include "System/Matrix44f.h"
#include "System/SafeUtil.h"
#include "System/FileSystem/FileHandler.h"
#include "System/Platform/Watchdog.h"
#include "System/Platform/Threading.h"
#include "System/Sound/ISound.h"
#include "System/Sound/ISoundChannels.h"
#if !defined(HEADLESS) && !defined(NO_SOUND)
#include "System/Sound/OpenAL/EFX.h"
#include "System/Sound/OpenAL/EFXPresets.h"
#endif
#include <vector>
CONFIG(int, LoadingMT).defaultValue(0).safemodeValue(0);
CLoadScreen* CLoadScreen::singleton = nullptr;
CLoadScreen::CLoadScreen(std::string&& _mapFileName, std::string&& _modFileName, ILoadSaveHandler* _saveFile)
: saveFile(_saveFile)
, mapFileName(std::move(_mapFileName))
, modFileName(std::move(_modFileName))
, mtLoading(true)
, lastDrawTime(0)
{
}
CLoadScreen::~CLoadScreen()
{
// Kill() must have been called first, such that the loading
// thread can not access singleton while its dtor is running
assert(!gameLoadThread.joinable());
if (clientNet != nullptr)
clientNet->KeepUpdating(false);
if (netHeartbeatThread.joinable())
netHeartbeatThread.join();
if (!gu->globalQuit) {
activeController = game;
if (luaMenu != nullptr)
luaMenu->ActivateGame();
}
if (activeController == this)
activeController = nullptr;
}
bool CLoadScreen::Init()
{
activeController = this;
// hide the cursor until we are ingame
SDL_ShowCursor(SDL_DISABLE);
// When calling this function, mod archives have to be loaded
// and gu->myPlayerNum has to be set.
skirmishAIHandler.LoadPreGame();
#ifdef HEADLESS
mtLoading = false;
#else
const int mtCfg = configHandler->GetInt("LoadingMT");
// user override
mtLoading = (mtCfg > 0);
// runtime detect. disable for intel/mesa drivers, they crash at multithreaded OpenGL (date: Nov. 2011)
mtLoading |= (mtCfg < 0) && !globalRendering->haveMesa && !globalRendering->haveIntel;
#endif
// Create a thread during the loading that pings the host/server, so it knows that this client is still alive/loading
clientNet->KeepUpdating(true);
netHeartbeatThread = std::move(spring::thread(Threading::CreateNewThread(std::bind(&CNetProtocol::UpdateLoop, clientNet))));
game = new CGame(mapFileName, modFileName, saveFile);
if ((CglFont::threadSafety = mtLoading)) {
try {
// create the game-loading thread; rebinds primary context to hidden window
gameLoadThread = std::move(COffscreenGLThread(std::bind(&CGame::Load, game, mapFileName)));
while (!Watchdog::HasThread(WDT_LOAD));
} catch (const opengl_error& gle) {
LOG_L(L_WARNING, "[LoadScreen::%s] offscreen GL context creation failed (error: \"%s\")", __func__, gle.what());
mtLoading = false;
CglFont::threadSafety = false;
}
}
// LuaIntro must be loaded and killed in the same thread (main)
// and bound context (secondary) that CLoadScreen::Draw runs in
//
// note that it has access to gl.LoadFont (which creates a user
// data wrapping a local font) but also to gl.*Text (which uses
// the global font), the latter will cause problems in GL4
CLuaIntro::LoadFreeHandler();
if (mtLoading)
return true;
LOG("[LoadScreen::%s] single-threaded", __func__);
game->Load(mapFileName);
return false;
}
void CLoadScreen::Kill()
{
if (mtLoading && !gameLoadThread.joinable())
return;
if (luaIntro != nullptr)
luaIntro->Shutdown();
CLuaIntro::FreeHandler();
// at this point, gameLoadThread running CGame::Load
// has finished and deregistered itself from WatchDog
gameLoadThread.join();
CglFont::threadSafety = false;
}
/******************************************************************************/
static void FinishedLoading()
{
if (gu->globalQuit)
return;
// send our playername to the server to indicate we finished loading
const CPlayer* p = playerHandler.Player(gu->myPlayerNum);
clientNet->Send(CBaseNetProtocol::Get().SendPlayerName(gu->myPlayerNum, p->name));
#ifdef SYNCCHECK
clientNet->Send(CBaseNetProtocol::Get().SendPathCheckSum(gu->myPlayerNum, pathManager->GetPathCheckSum()));
#endif
mouse->ShowMouse();
#if !defined(HEADLESS) && !defined(NO_SOUND)
// NB: sound is initialized at this point, but EFX support is *not* guaranteed
efx.CommitEffects(mapInfo->efxprops);
#endif
}
void CLoadScreen::CreateDeleteInstance(std::string&& mapFileName, std::string&& modFileName, ILoadSaveHandler* saveFile)
{
if (CreateInstance(std::move(mapFileName), std::move(modFileName), saveFile))
return;
// not mtLoading, Game::Load has completed and we can go
DeleteInstance();
FinishedLoading();
}
bool CLoadScreen::CreateInstance(std::string&& mapFileName, std::string&& modFileName, ILoadSaveHandler* saveFile)
{
assert(singleton == nullptr);
singleton = new CLoadScreen(std::move(mapFileName), std::move(modFileName), saveFile);
// returns true when mtLoading, false otherwise
return (singleton->Init());
}
void CLoadScreen::DeleteInstance()
{
if (singleton == nullptr)
return;
singleton->Kill();
spring::SafeDelete(singleton);
}
/******************************************************************************/
void CLoadScreen::ResizeEvent()
{
if (luaIntro != nullptr)
luaIntro->ViewResize();
}
int CLoadScreen::KeyPressed(int k, bool isRepeat)
{
//FIXME add mouse events
if (luaIntro != nullptr)
luaIntro->KeyPress(k, isRepeat);
return 0;
}
int CLoadScreen::KeyReleased(int k)
{
if (luaIntro != nullptr)
luaIntro->KeyRelease(k);
return 0;
}
bool CLoadScreen::Update()
{
if (luaIntro != nullptr) {
// keep checking this while we are the active controller
std::lock_guard<spring::recursive_mutex> lck(mutex);
for (const auto& pair: loadMessages) {
good_fpu_control_registers(pair.first.c_str());
luaIntro->LoadProgress(pair.first, pair.second);
}
loadMessages.clear();
}
if (game->IsDoneLoading()) {
CLoadScreen::DeleteInstance();
FinishedLoading();
return true;
}
// without this call the window manager would think the window is unresponsive and thus ask for hard kill
if (!mtLoading)
SDL_PollEvent(nullptr);
return true;
}
bool CLoadScreen::Draw()
{
// limit FPS via sleep to not lock a singlethreaded CPU from loading the game
if (mtLoading) {
const spring_time now = spring_gettime();
const unsigned diffTime = spring_tomsecs(now - lastDrawTime);
constexpr unsigned wantedFPS = 50;
constexpr unsigned minFrameTime = 1000 / wantedFPS;
if (diffTime < minFrameTime)
spring_sleep(spring_msecs(minFrameTime - diffTime));
lastDrawTime = now;
}
// let LuaMenu keep the lobby connection alive
if (luaMenu != nullptr)
luaMenu->Update();
if (luaIntro != nullptr) {
luaIntro->Update();
luaIntro->DrawGenesis();
ClearScreen();
luaIntro->DrawLoadScreen();
}
if (!mtLoading)
globalRendering->SwapBuffers(true, false);
return true;
}
/******************************************************************************/
/******************************************************************************/
void CLoadScreen::SetLoadMessage(const std::string& text, bool replaceLast)
{
Watchdog::ClearTimer(WDT_LOAD);
std::lock_guard<spring::recursive_mutex> lck(mutex);
loadMessages.emplace_back(text, replaceLast);
LOG("[LoadScreen::%s] text=\"%s\"", __func__, text.c_str());
LOG_CLEANUP();
// be paranoid about FPU state for the loading thread since some
// external library might reset it (main thread state is checked
// in ::Update)
good_fpu_control_registers(text.c_str());
if (mtLoading)
return;
Update();
Draw();
}
|