Skip to content

Commit

Permalink
Moved AppLoad functions into AppCommon
Browse files Browse the repository at this point in the history
- Moved AsyncWorker to modules/utils
- Moved LogWindow to modules/utils
  • Loading branch information
hudrima1 committed Jun 30, 2024
1 parent e013226 commit 348979f
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 218 deletions.
2 changes: 0 additions & 2 deletions apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ function(sl_add_app)
${SL_PROJECT_ROOT}/apps/source/App.h
${SL_PROJECT_ROOT}/apps/source/CVCapture.h
${SL_PROJECT_ROOT}/apps/source/AppCommon.h
${SL_PROJECT_ROOT}/apps/source/AppLoad.h
${SL_PROJECT_ROOT}/apps/source/SLScene.h
${SL_PROJECT_ROOT}/apps/source/SLInterface.h
${SL_PROJECT_ROOT}/apps/source/Scene.h
Expand All @@ -54,7 +53,6 @@ function(sl_add_app)
file(GLOB COMMON_SOURCES
${SL_PROJECT_ROOT}/apps/source/CVCapture.cpp
${SL_PROJECT_ROOT}/apps/source/AppCommon.cpp
${SL_PROJECT_ROOT}/apps/source/AppLoad.cpp
${SL_PROJECT_ROOT}/apps/source/SLInterface.cpp
${SL_PROJECT_ROOT}/apps/source/SLProjectScene.cpp
${APP_SOURCES}
Expand Down
126 changes: 124 additions & 2 deletions apps/source/AppCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
* \remarks Please use clangformat to format the code. See more code style on
* https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
*/

#include <SL.h>
#include <App.h>
#include <AppCommon.h>
#include <SL.h>
#include <SLAssetManager.h>
#include <SLScene.h>
#include <SLSceneView.h>
Expand Down Expand Up @@ -156,6 +156,128 @@ void AppCommon::registerCoreAssetsLoad()
IOK_font);
}
//-----------------------------------------------------------------------------
/*!
* Deletes the current scene and creates a new one identified by the sceneID.
* All assets get registered for async loading while Imgui shows a progress
* spinner in the UI. After the parallel loading the scene gets assembled back
* in the main thread.
* \param sv Pointer to the sceneview
* \param sceneID Scene identifier defined in SLEnum
*/
void AppCommon::switchScene(SLSceneView* sv, SLSceneID sceneID)
{
SLAssetManager*& am = AppCommon::assetManager;
SLAssetLoader*& al = AppCommon::assetLoader;

SLfloat startLoadMS = GlobalTimer::timeMS();

//////////////////////
// Delete old scene //
//////////////////////

if (App::config.onBeforeSceneDelete)
App::config.onBeforeSceneDelete(sv, AppCommon::scene);

if (AppCommon::scene)
{
delete AppCommon::scene;
AppCommon::scene = nullptr;
}

// Deactivate in general the device sensors
AppCommon::devRot.init();
AppCommon::devLoc.init();

// reset existing sceneviews
for (auto* sceneView : AppCommon::sceneViews)
sceneView->unInit();

// Clear all data in the asset manager
am->clear();

////////////////////
// Init new scene //
////////////////////

AppCommon::sceneID = sceneID;
SLScene* s = App::config.onNewScene(sceneID);
SL_LOG("Scene name : %s (SceneID: %d)",
s->name().c_str(),
AppCommon::sceneID);

// Initialize all preloaded stuff from SLScene
s->init(am);

#ifndef SL_EMSCRIPTEN
s->initOculus(AppCommon::dataPath + "shaders/");
#endif

// Reset the global SLGLState state
SLGLState::instance()->initAll();

///////////////////////////////
// Prepare for async loading //
///////////////////////////////

// Register assets on the loader that have to be loaded before assembly.
al->scene(s);
s->registerAssetsToLoad(*al);

// `onDone` is a wrapper around `onDoneLoading` that will be called
// in the main thread after loading.
auto onDone = std::bind(onDoneLoading, sv, s, startLoadMS);

// Start loading assets asynchronously.
al->loadAssetsAsync(onDone);

if (App::config.onBeforeSceneLoad)
App::config.onBeforeSceneLoad(sv, s);
}
//-----------------------------------------------------------------------------
void AppCommon::onDoneLoading(SLSceneView* sv, SLScene* s, SLfloat startLoadMS)
{
SLAssetManager* am = AppCommon::assetManager;
SLAssetLoader* al = AppCommon::assetLoader;

// Register a task to assemble the scene.
al->addLoadTask(std::bind(&SLScene::assemble, s, am, sv));

// `onDone` is a wrapper around `onDoneAssembling` that will be called
// in the main thread after loading.
auto onDone = std::bind(onDoneAssembling, sv, s, startLoadMS);

// Start assembling the scene asynchronously.
al->loadAssetsAsync(onDone);

if (App::config.onBeforeSceneAssembly)
App::config.onBeforeSceneAssembly(sv, s);
}
//-----------------------------------------------------------------------------
void AppCommon::onDoneAssembling(SLSceneView* sv, SLScene* s, SLfloat startLoadMS)
{
/* Assign the scene to the sceneview. The sceneview exists and is used
* before it knows its scene. This is new since we do async loading and
* show a spinner in the sceneview. */
sv->scene(s);
sv->postSceneLoad();

// Make sure the scene view has a camera
if (!sv->camera())
sv->camera(sv->sceneViewCamera());

// call onInitialize on all scene views to init the scenegraph and stats
for (auto* sceneView : AppCommon::sceneViews)
if (sceneView != nullptr)
sceneView->onInitialize();

AppCommon::scene = s;

if (App::config.onAfterSceneAssembly)
App::config.onAfterSceneAssembly(sv, s);

s->loadTimeMS(GlobalTimer::timeMS() - startLoadMS);
}
//-----------------------------------------------------------------------------
//! Calls the destructor of the single scene instance.
/*! Destroys all data by calling the destructor of the single scene instance.
All other date gets destroyed from there. This function gets called by the
Expand Down
8 changes: 6 additions & 2 deletions apps/source/AppCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
*/

#ifndef SLAPPLICATION_H
#define SLAPPLICATION_H
#ifndef SLAPPCOMMON_H
#define SLAPPCOMMON_H

#include <atomic>
#include <mutex>
Expand Down Expand Up @@ -67,6 +67,7 @@ class AppCommon

static void createApp(SLstring appName);
static void registerCoreAssetsLoad();
static void switchScene(SLSceneView* sv, SLSceneID sceneID);
static void deleteApp();

static SLstring name; //!< Application name
Expand Down Expand Up @@ -118,6 +119,9 @@ class AppCommon
static const string PROFILE_FTP_DIR; //!< ftp directory for profiles upload

private:
static void onDoneLoading(SLSceneView* sv, SLScene* s, SLfloat startLoadMS);
static void onDoneAssembling(SLSceneView* sv, SLScene* s, SLfloat startLoadMS);

static string _jobProgressMsg; //!< Text message to show during progress
static atomic<int> _jobProgressNum; //!< Integer value to show progress
static atomic<int> _jobProgressMax; //!< Max. integer progress value
Expand Down
142 changes: 0 additions & 142 deletions apps/source/AppLoad.cpp

This file was deleted.

30 changes: 0 additions & 30 deletions apps/source/AppLoad.h

This file was deleted.

31 changes: 0 additions & 31 deletions apps/source/AsyncWorker.h

This file was deleted.

Loading

0 comments on commit 348979f

Please sign in to comment.