Jump to content

Loading assets in the background?


Recommended Posts

I'm not sure how to go about creating a loading screen where I can load all the assets in the background.  Does Ultra support this out of the box or do I have to code some thread magic myself?

I need to be able to update a progress bar and / or a spinning 3D object in the corner of the screen without any pauses.

Link to comment
Share on other sites

If you have called World::Render once, then the rendering thread will just keep rendering as the map is loading on the main thread.

You can use a custom shader to display a spinning object, or wait for texture animation to be added, and you can make a circular object with a scrolling texture.

  • Thanks 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Tried to replicate how i made loading screen in my game but for some reason loading screen shows with big delay in this example

 

#include "UltraEngine.h"
#include "ComponentSystem.h"

using namespace UltraEngine;

shared_ptr<World> gameWorld;

shared_ptr<World> world;
shared_ptr<Interface> ui;

shared_ptr<Camera> uiCamera;
shared_ptr<Map> gameScene;

shared_ptr<Window> window;
shared_ptr<Framebuffer> framebuffer;

shared_ptr<World> loadingWorld;
shared_ptr<Interface> loadingUi;
shared_ptr<Camera> loadingCamera;
shared_ptr<Widget> loadingLabel;

shared_ptr<Widget> menuPanel;
bool isMainMenuOn = true;

bool newGameButtonCallback(const Event& ev, shared_ptr<Object> extra)
{
    ui = nullptr;
    world = nullptr;
    loadingWorld->Render(framebuffer);
    WString mapName = "Maps/start.ultra";
    gameScene = LoadMap(gameWorld, mapName);
    isMainMenuOn = false;
    return true;
}

bool exitButtonCallback(const Event& ev, shared_ptr<Object> extra)
{
    exit(0);
    return true;
}

void loadingInit() 
{
    loadingWorld = CreateWorld();
    //Loading UI
    auto font = LoadFont("Fonts/arial.ttf");
    loadingUi = CreateInterface(loadingWorld, font, framebuffer->GetSize());
    loadingUi->SetScale(1);
    loadingUi->SetRenderLayers(2);
    loadingUi->LoadColorScheme("Resources/configs/Style.json");
    //Label LOADING...
    int centerX = float(framebuffer->GetSize().x) * 0.5f;
    int centerY = float(framebuffer->GetSize().y) * 0.5f;
    float labelHeight = float(framebuffer->GetSize().y) * 0.2f;
    loadingLabel = CreateLabel("LOADING", float(framebuffer->GetSize().x) * 0.05f, centerY - labelHeight * 0.5f, float(framebuffer->GetSize().x) * 0.95f, labelHeight, loadingUi->root, LABEL_CENTER | LABEL_MIDDLE);
    loadingLabel->SetFontScale(20);
    //Create ui camera
    loadingCamera = CreateCamera(loadingWorld, PROJECTION_ORTHOGRAPHIC);
    loadingCamera->SetPosition(centerX, centerY, 0);
    loadingCamera->SetRenderLayers(2);
    loadingCamera->SetClearMode(CLEAR_DEPTH);
    loadingCamera->SetLighting(false);
}

int main(int argc, const char* argv[])
{
    RegisterComponents();
    //Load FreeImage plugin (optional)
    auto fiplugin = LoadPlugin("Plugins/FITextureLoader");
    //Get the displays
    auto displays = GetDisplays();
    //Create a window
    window = CreateWindow("Ultra Engine", 0, 0, 1280 * displays[0]->scale, 720 * displays[0]->scale, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);
    //Create a framebuffer
    framebuffer = CreateFramebuffer(window);
    //Create a world
    world = CreateWorld();

    //Load a font
    auto font = LoadFont("Fonts/arial.ttf");

    //Create user interface
    ui = CreateInterface(world, font, framebuffer->GetSize());
    ui->SetRenderLayers(2);
    ui->root->SetColor(0.2f, 0.2f, 0.2f, 1.0f);
    ui->LoadColorScheme("Resources/configs/Style.json");

    //Create ui camera
    auto uiCamera = CreateCamera(world, PROJECTION_ORTHOGRAPHIC);
    uiCamera->SetPosition(float(framebuffer->GetSize().x) * 0.5f, float(framebuffer->GetSize().y) * 0.5f, 0);
    uiCamera->SetRenderLayers(2);
    uiCamera->SetClearMode(CLEAR_DEPTH);

    auto newGameButton = CreateButton("New game", 200, 125, 200, 50, ui->root);
    ListenEvent(EVENT_WIDGETACTION, newGameButton, newGameButtonCallback);

    auto exitButton = CreateButton("Exit", 200, 200, 200, 50, ui->root);
    ListenEvent(EVENT_WIDGETACTION, exitButton, exitButtonCallback);

    gameWorld = CreateWorld();
    gameWorld->RecordStats();

    loadingInit();

    shared_ptr<World> currentWorld = world;
    shared_ptr<Interface> currentUI = ui;

    while (window->Closed() == false)
    {
        if (!isMainMenuOn) currentWorld = gameWorld;     
        while (PeekEvent())
        {
            const Event ev = WaitEvent();
            switch (ev.id)
            {
            case EVENT_WINDOWCLOSE:
                if (ev.source == window)
                {
                    exit(0);
                    break;
                }
                break;
            default:
                currentUI->ProcessEvent(ev);
                break;
            }
        }
        currentWorld->Update();
        currentWorld->Render(framebuffer);
    }
    return 0;
}

 

  • Thanks 1
Link to comment
Share on other sites

1 hour ago, SpiderPig said:

I see, thanks.  Could the application go non responsive though?

The window event system is on the main thread, so it would stop processing events while the map is loading. However, the screen buffer would continue to refresh as the rendering thread runs.

  • Like 1
  • Thanks 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...