Skip to content

Commit 5c73e90

Browse files
better progress bar and show status for threaded loading
1 parent e0d7b15 commit 5c73e90

2 files changed

Lines changed: 128 additions & 17 deletions

File tree

es-app/src/SystemData.cpp

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#include "Settings.h"
1111
#include "ThemeData.h"
1212
#include "views/UIModeController.h"
13+
#include <atomic>
1314
#include <fstream>
15+
#include <mutex>
1416
#include <random>
1517
#include "utils/StringUtil.h"
1618
#include "utils/ThreadPool.h"
@@ -321,16 +323,31 @@ bool SystemData::loadConfig(Window* window)
321323
pThreadPool->queueWorkItem([] { CollectionSystemManager::get()->loadCollectionSystems(true); });
322324
}
323325

324-
int processedSystem = 0;
326+
std::atomic<int> processedSystem(0);
327+
std::vector<bool> activeSystems;
328+
std::mutex activeSystemsMutex;
329+
if (pThreadPool != NULL)
330+
activeSystems.assign(systemCount, false);
325331

326332
for (pugi::xml_node system = systemList.child("system"); system; system = system.next_sibling("system"))
327333
{
328334
if (pThreadPool != NULL)
329335
{
330-
pThreadPool->queueWorkItem([system, currentSystem, systems, &processedSystem]
336+
pThreadPool->queueWorkItem([system, currentSystem, systems, &processedSystem, &activeSystems, &activeSystemsMutex]
331337
{
338+
{
339+
std::lock_guard<std::mutex> lock(activeSystemsMutex);
340+
activeSystems[currentSystem] = true;
341+
}
342+
332343
systems[currentSystem] = loadSystem(system);
333-
processedSystem++;
344+
345+
{
346+
std::lock_guard<std::mutex> lock(activeSystemsMutex);
347+
activeSystems[currentSystem] = false;
348+
}
349+
350+
processedSystem.fetch_add(1);
334351
});
335352
}
336353
else
@@ -354,11 +371,27 @@ bool SystemData::loadConfig(Window* window)
354371
{
355372
if (window != NULL)
356373
{
357-
pThreadPool->wait([window, &processedSystem, systemCount, &systemsNames]
374+
pThreadPool->wait([window, &processedSystem, systemCount, &systemsNames, &activeSystems, &activeSystemsMutex]
358375
{
359-
int px = processedSystem - 1;
360-
if (px >= 0 && px < systemsNames.size())
361-
window->renderLoadingScreen(systemsNames.at(px), (float)px / (float)(systemCount + 1));
376+
std::string activeText;
377+
{
378+
std::lock_guard<std::mutex> lock(activeSystemsMutex);
379+
for (size_t i = 0; i < activeSystems.size(); i++)
380+
{
381+
if (!activeSystems[i])
382+
continue;
383+
384+
if (!activeText.empty())
385+
activeText += ", ";
386+
activeText += systemsNames.at(i);
387+
}
388+
}
389+
390+
if (activeText.empty())
391+
activeText = "Loading systems...";
392+
393+
const int completed = processedSystem.load();
394+
window->renderLoadingScreen(activeText, systemCount == 0 ? 0 : (float)completed / (float)systemCount);
362395
}, 10);
363396
}
364397
else
@@ -375,14 +408,14 @@ bool SystemData::loadConfig(Window* window)
375408
delete pThreadPool;
376409

377410
if (window != NULL)
378-
window->renderLoadingScreen("Favorites", systemCount == 0 ? 0 : currentSystem / systemCount);
411+
window->renderLoadingScreen("Favorites", 1.0f);
379412

380413
CollectionSystemManager::get()->updateSystemsList();
381414
}
382415
else
383416
{
384417
if (window != NULL)
385-
window->renderLoadingScreen("Favorites", systemCount == 0 ? 0 : currentSystem / systemCount);
418+
window->renderLoadingScreen("Favorites", 1.0f);
386419

387420
CollectionSystemManager::get()->loadCollectionSystems();
388421
}

es-core/src/Window.cpp

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "Scripting.h"
99
#include <algorithm>
1010
#include <iomanip>
11+
#include <sstream>
12+
#include <vector>
1113

1214
#ifdef WIN32
1315
#include <SDL_events.h>
@@ -297,15 +299,91 @@ void Window::renderLoadingScreen(std::string text, float percent, unsigned char
297299
splash.setPosition((Renderer::getScreenWidth() - splash.getSize().x()) / 2, (Renderer::getScreenHeight() - splash.getSize().y()) / 2 * 0.6f);
298300
splash.render(trans);
299301

300-
auto& font = mDefaultFonts.at(1);
301-
TextCache* cache = font->buildTextCache(text, 0, 0, 0x656565FF);
302+
std::vector<std::string> lines;
303+
if(text.find('\n') != std::string::npos)
304+
{
305+
std::stringstream ss(text);
306+
std::string line;
307+
while(std::getline(ss, line, '\n'))
308+
{
309+
if(!line.empty())
310+
lines.push_back(line);
311+
}
312+
}
313+
else if(text.find(", ") != std::string::npos)
314+
{
315+
size_t start = 0;
316+
while(start < text.size())
317+
{
318+
size_t end = text.find(", ", start);
319+
if(end == std::string::npos)
320+
end = text.size();
302321

303-
float x = Math::round((Renderer::getScreenWidth() - cache->metrics.size.x()) / 2.0f);
304-
float y = Math::round(Renderer::getScreenHeight() * 0.78f);
305-
trans = trans.translate(Vector3f(x, y, 0.0f));
306-
Renderer::setMatrix(trans);
307-
font->renderTextCache(cache);
308-
delete cache;
322+
std::string line = text.substr(start, end - start);
323+
if(!line.empty())
324+
lines.push_back(line);
325+
326+
if(end == text.size())
327+
break;
328+
start = end + 2;
329+
}
330+
}
331+
else
332+
{
333+
lines.push_back(text);
334+
}
335+
336+
if(lines.empty())
337+
lines.push_back("Loading...");
338+
339+
auto font = mDefaultFonts.at(1);
340+
if(lines.size() > 4)
341+
font = mDefaultFonts.at(0);
342+
343+
float maxTextWidth = 0.0f;
344+
for(const auto& line : lines)
345+
{
346+
TextCache* cache = font->buildTextCache(line, 0, 0, 0x656565FF);
347+
maxTextWidth = std::max(maxTextWidth, cache->metrics.size.x());
348+
delete cache;
349+
}
350+
351+
if(maxTextWidth > Renderer::getScreenWidth() * 0.9f)
352+
font = mDefaultFonts.at(0);
353+
354+
const float barTop = Renderer::getScreenHeight() - (Renderer::getScreenHeight() * 3 * 0.04f);
355+
const float textTopLimit = Renderer::getScreenHeight() * 0.58f;
356+
const float textBottomLimit = barTop - 8.0f;
357+
const float lineHeight = font->getHeight() * 1.15f;
358+
const float availableHeight = std::max(0.0f, textBottomLimit - textTopLimit);
359+
360+
int maxLines = lineHeight <= 0.0f ? 1 : (int)(availableHeight / lineHeight);
361+
if(maxLines < 1)
362+
maxLines = 1;
363+
364+
if((int)lines.size() > maxLines)
365+
{
366+
const int hidden = (int)lines.size() - maxLines + 1;
367+
lines.resize(maxLines);
368+
lines[maxLines - 1] = "+" + std::to_string(hidden) + " more";
369+
}
370+
371+
const float totalHeight = lineHeight * (float)lines.size();
372+
float startY = textBottomLimit - totalHeight;
373+
if(startY < textTopLimit)
374+
startY = textTopLimit;
375+
376+
for(size_t i = 0; i < lines.size(); i++)
377+
{
378+
TextCache* cache = font->buildTextCache(lines[i], 0, 0, 0x656565FF);
379+
float x = Math::round((Renderer::getScreenWidth() - cache->metrics.size.x()) / 2.0f);
380+
float y = Math::round(startY + (lineHeight * (float)i));
381+
Transform4x4f lineTrans = Transform4x4f::Identity();
382+
lineTrans.translate(Vector3f(x, y, 0.0f));
383+
Renderer::setMatrix(lineTrans);
384+
font->renderTextCache(cache);
385+
delete cache;
386+
}
309387

310388
Renderer::swapBuffers();
311389

0 commit comments

Comments
 (0)