Skip to content

Commit

Permalink
tiny loadinglayer speedups
Browse files Browse the repository at this point in the history
  • Loading branch information
dankmeme01 committed Aug 4, 2024
1 parent 5f8d71f commit 78a084d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ if (BLAZE_DEBUG)
target_compile_definitions(${PROJECT_NAME} PRIVATE BLAZE_DEBUG=1)
endif()

CPMAddPackage("gh:dankmeme01/asp2#bf4bc5e")
CPMAddPackage("gh:dankmeme01/asp2#7024af9")
CPMAddPackage(
NAME Boost
VERSION 1.84.0
Expand Down
54 changes: 35 additions & 19 deletions src/hooks/LoadingLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,22 +370,29 @@ class $modify(MyLoadingLayer, LoadingLayer) {
bool init(bool fromReload) {
m_fields->startedLoadingGame = std::chrono::high_resolution_clock::now();

if (!CCLayer::init()) return false;

BLAZE_TIMER_START("(LoadingLayer::init) Initial setup");

if (!CCLayer::init()) return false;

this->m_fromRefresh = fromReload;
CCDirector::get()->m_bDisplayStats = true;
CCTexture2D::setDefaultAlphaPixelFormat(cocos2d::kCCTexture2DPixelFormat_Default);

// load the launchsheet and bg in another thread, as fmod setup takes a little bit, and image loading can be parallelized.
asp::Thread<> sheetThread;

std::optional<MTTextureInitTask> launchSheetInitTask;
std::optional<MTTextureInitTask> bgInitTask;
asp::Channel<MTTextureInitTask> initTasks;
sheetThread.setLoopFunction([&](asp::StopToken<>& stopToken) {
launchSheetInitTask = asyncLoadImage("GJ_LaunchSheet.png", "GJ_LaunchSheet.plist");
bgInitTask = asyncLoadImage("game_bg_01_001.png", nullptr);
#define ASYNC_IMG(name) if (auto _t = asyncLoadImage(name, nullptr)) { initTasks.push(std::move(_t.value())); }
#define ASYNC_SHEET(name, plist) if (auto _t = asyncLoadImage(name, plist)) { initTasks.push(std::move(_t.value())); }

ASYNC_SHEET("GJ_LaunchSheet.png", "GJ_LaunchSheet.plist");
ASYNC_IMG("game_bg_01_001.png");
ASYNC_IMG("slidergroove.png");
ASYNC_IMG("sliderBar.png");

#undef ASYNC_IMG

stopToken.stop();
});

Expand All @@ -397,24 +404,33 @@ class $modify(MyLoadingLayer, LoadingLayer) {
FMODAudioEngine::get()->setup();
}

auto* gm = GameManager::get();
if (gm->m_switchModes) {
gm->m_switchModes = false;
GameLevelManager::get()->getLevelSaveData();
}

// Continue loading launchsheet
BLAZE_TIMER_STEP("Launchsheet loading");
sheetThread.join();

if (launchSheetInitTask) {
addTexture(launchSheetInitTask->img, launchSheetInitTask->sheetName);
auto* sfcache = CCSpriteFrameCache::get();
sfcache->addSpriteFramesWithFile("GJ_LaunchSheet.plist");
}
auto* sfcache = CCSpriteFrameCache::get();

if (bgInitTask) {
addTexture(bgInitTask->img, bgInitTask->sheetName);
}
while (true) {
if (initTasks.empty()) {
if (!sheetThread.isStopped()) {
std::this_thread::yield();
continue;
} else if (initTasks.empty()) {
break;
}
}

auto* gm = GameManager::get();
if (gm->m_switchModes) {
gm->m_switchModes = false;
GameLevelManager::get()->getLevelSaveData();
auto iTask = initTasks.popNow();

addTexture(iTask.img, iTask.sheetName);
if (iTask.plistToLoad) {
sfcache->addSpriteFramesWithFile("GJ_LaunchSheet.plist");
}
}

BLAZE_TIMER_STEP("LoadingLayer UI");
Expand Down

0 comments on commit 78a084d

Please sign in to comment.