Unigine::LoadingScreen Class
Header: | #include <UnigineLoadingScreen.h> |
A singleton that controls the settings of the loading screen. Demonstration of it gives UNIGINE the time to load all world nodes and resources. You can also show your own loading screen when needed.
A loading screen displays a texture that is usually divided into two parts stacked vertically — the initial and final pictures — which are gradually blended from the beginning up to the end of loading to show the progress. Blending is performed based on the alpha channel of the outro (lower) part of the texture so pseudo-animation can be created using the alpha channel: regions of the lower half with small alpha values will be shown first, regions with larger alpha values will be shown last.
See Also#
-
A set of UnigineScript API samples located in the <UnigineSDK>/data/samples/widgets/ folder:
- loading_screen_00
- loading_screen_01
- loading_screen_02
- Quick Video Guide: How To Customize Loading Screens
Example
Here's a code example on how to add your own loading screens for application and world loading.
To show your loading screen on system logic initialization before a world is loaded, define it inside the init() method of the System Logic:
#include "AppSystemLogic.h"
#include "UnigineLoadingScreen.h"
using namespace Unigine;
int AppSystemLogic::init()
{
// define new transform for loading screen texture
Math::vec4 transform = Math::vec4(1.0f, 1.0f, 0.0f, 0.0f);
// enable the loading screen
LoadingScreen::setEnabled(true);
// set transform to the loading screen texture
LoadingScreen::setTransform(transform);
Math::ivec2 winsize = WindowManager::getMainWindow()->getClientSize();
// compute the aspect ratio to show the corresponding texture
float aspect_ratio = (float)winsize.x / (float)winsize.y;
// if the aspect ratio is 4:3 show the following loading screen
// during world loading
if (aspect_ratio < 1.5f) {
LoadingScreen::setTexturePath("textures/splash_4x3.png");
}
else {
// if the aspect ratio is 16:9 show this loading screen
// during world loading
LoadingScreen::setTexturePath("textures/splash_16x9.png");
}
// set the text to be displayed on the loading screen
// with a certain displacement along the X and Y axes
LoadingScreen::setText("<xy x=\"%50\" dx=\"0\" y=\"%50\" dy=\"0\"/>LOADING_PROGRESS");
// set duration (in milliseconds) and display the loading screen on world loading
float duration = 5000.0f;
float begin = clock();
while (clock() - begin < duration)
LoadingScreen::render((clock() - begin) / duration * 100.0f);
// disable the loading screen
LoadingScreen::setEnabled(false);
return 1;
}
A loading screen defined in the init() method of the World Logic will be shown right after a world is loaded:
#include "AppWorldLogic.h"
#include "UnigineLoadingScreen.h"
using namespace Unigine;
int AppWorldLogic::init()
{
// enable the loading screen
LoadingScreen::setEnabled(true);
// set the text to be displayed on the loading screen,
// specifying a color, a font, and a certain displacement along the X and Y axes
LoadingScreen::setText("<p align=\"center\"><font size=\"20\" color=\"#FF0000FF\">CUSTOM LOADING TEXT</font></p>");
// set duration (in milliseconds) and display the loading screen on world loading
float duration = 5000.0f;
float begin = clock();
while (clock() - begin < duration)
LoadingScreen::render((clock() - begin) / duration * 100.0f);
// disable the loading screen
LoadingScreen::setEnabled(false);
return 1;
}
LoadingScreen Class
Members
getMessage() const#
Return value
CurrentgetProgress() const#
Return value
CurrentEvent<> getEventRenderEnd() const#
Usage Example
// implement the RenderEnd event handler
void renderend_event_handler()
{
Log::message("\Handling RenderEnd event\n");
}
//////////////////////////////////////////////////////////////////////////////
// 1. Multiple subscriptions can be linked to an instance of the EventConnections
// class that you can use later to remove all these subscriptions at once
//////////////////////////////////////////////////////////////////////////////
// create an instance of the EventConnections class
EventConnections renderend_event_connections;
// link to this instance when subscribing to an event (subscription to various events can be linked)
LoadingScreen::getEventRenderEnd().connect(renderend_event_connections, renderend_event_handler);
// other subscriptions are also linked to this EventConnections instance
// (e.g. you can subscribe using lambdas)
LoadingScreen::getEventRenderEnd().connect(renderend_event_connections, []() {
Log::message("\Handling RenderEnd event (lambda).\n");
}
);
// ...
// later all of these linked subscriptions can be removed with a single line
renderend_event_connections.disconnectAll();
//////////////////////////////////////////////////////////////////////////////
// 2. You can subscribe and unsubscribe via an instance of the EventConnection
// class. And toggle this particular connection off and on, when necessary.
//////////////////////////////////////////////////////////////////////////////
// create an instance of the EventConnection class
EventConnection renderend_event_connection;
// subscribe to the RenderEnd event with a handler function keeping the connection
LoadingScreen::getEventRenderEnd().connect(renderend_event_connection, renderend_event_handler);
// ...
// you can temporarily disable a particular event connection to perform certain actions
renderend_event_connection.setEnabled(false);
// ... actions to be performed
// and enable it back when necessary
renderend_event_connection.setEnabled(true);
// ...
// remove subscription to the RenderEnd event via the connection
renderend_event_connection.disconnect();
//////////////////////////////////////////////////////////////////////////////
// 3. You can add EventConnection/EventConnections instance as a member of the
// class that handles the event. In this case all linked subscriptions will be
// automatically removed when class destructor is called
//////////////////////////////////////////////////////////////////////////////
// Class handling the event
class SomeClass
{
public:
// instance of the EventConnections class as a class member
EventConnections e_connections;
// A RenderEnd event handler implemented as a class member
void event_handler()
{
Log::message("\Handling RenderEnd event\n");
// ...
}
};
SomeClass *sc = new SomeClass();
// ...
// specify a class instance in case a handler method belongs to some class
LoadingScreen::getEventRenderEnd().connect(sc->e_connections, sc, &SomeClass::event_handler);
// ...
// handler class instance is deleted with all its subscriptions removed automatically
delete sc;
//////////////////////////////////////////////////////////////////////////////
// 4. You can subscribe and unsubscribe via the handler function directly
//////////////////////////////////////////////////////////////////////////////
// subscribe to the RenderEnd event with a handler function
LoadingScreen::getEventRenderEnd().connect(renderend_event_handler);
// remove subscription to the RenderEnd event later by the handler function
LoadingScreen::getEventRenderEnd().disconnect(renderend_event_handler);
//////////////////////////////////////////////////////////////////////////////
// 5. Subscribe to an event saving an ID and unsubscribe later by this ID
//////////////////////////////////////////////////////////////////////////////
// define a connection ID to be used to unsubscribe later
EventConnectionId renderend_handler_id;
// subscribe to the RenderEnd event with a lambda handler function and keeping connection ID
renderend_handler_id = LoadingScreen::getEventRenderEnd().connect([]() {
Log::message("\Handling RenderEnd event (lambda).\n");
}
);
// remove the subscription later using the ID
LoadingScreen::getEventRenderEnd().disconnect(renderend_handler_id);
//////////////////////////////////////////////////////////////////////////////
// 6. Ignoring all RenderEnd events when necessary
//////////////////////////////////////////////////////////////////////////////
// you can temporarily disable the event to perform certain actions without triggering it
LoadingScreen::getEventRenderEnd().setEnabled(false);
// ... actions to be performed
// and enable it back when necessary
LoadingScreen::getEventRenderEnd().setEnabled(true);
Return value
Event reference.Event<> getEventRenderBegin() const#
Usage Example
// implement the RenderBegin event handler
void renderbegin_event_handler()
{
Log::message("\Handling RenderBegin event\n");
}
//////////////////////////////////////////////////////////////////////////////
// 1. Multiple subscriptions can be linked to an instance of the EventConnections
// class that you can use later to remove all these subscriptions at once
//////////////////////////////////////////////////////////////////////////////
// create an instance of the EventConnections class
EventConnections renderbegin_event_connections;
// link to this instance when subscribing to an event (subscription to various events can be linked)
LoadingScreen::getEventRenderBegin().connect(renderbegin_event_connections, renderbegin_event_handler);
// other subscriptions are also linked to this EventConnections instance
// (e.g. you can subscribe using lambdas)
LoadingScreen::getEventRenderBegin().connect(renderbegin_event_connections, []() {
Log::message("\Handling RenderBegin event (lambda).\n");
}
);
// ...
// later all of these linked subscriptions can be removed with a single line
renderbegin_event_connections.disconnectAll();
//////////////////////////////////////////////////////////////////////////////
// 2. You can subscribe and unsubscribe via an instance of the EventConnection
// class. And toggle this particular connection off and on, when necessary.
//////////////////////////////////////////////////////////////////////////////
// create an instance of the EventConnection class
EventConnection renderbegin_event_connection;
// subscribe to the RenderBegin event with a handler function keeping the connection
LoadingScreen::getEventRenderBegin().connect(renderbegin_event_connection, renderbegin_event_handler);
// ...
// you can temporarily disable a particular event connection to perform certain actions
renderbegin_event_connection.setEnabled(false);
// ... actions to be performed
// and enable it back when necessary
renderbegin_event_connection.setEnabled(true);
// ...
// remove subscription to the RenderBegin event via the connection
renderbegin_event_connection.disconnect();
//////////////////////////////////////////////////////////////////////////////
// 3. You can add EventConnection/EventConnections instance as a member of the
// class that handles the event. In this case all linked subscriptions will be
// automatically removed when class destructor is called
//////////////////////////////////////////////////////////////////////////////
// Class handling the event
class SomeClass
{
public:
// instance of the EventConnections class as a class member
EventConnections e_connections;
// A RenderBegin event handler implemented as a class member
void event_handler()
{
Log::message("\Handling RenderBegin event\n");
// ...
}
};
SomeClass *sc = new SomeClass();
// ...
// specify a class instance in case a handler method belongs to some class
LoadingScreen::getEventRenderBegin().connect(sc->e_connections, sc, &SomeClass::event_handler);
// ...
// handler class instance is deleted with all its subscriptions removed automatically
delete sc;
//////////////////////////////////////////////////////////////////////////////
// 4. You can subscribe and unsubscribe via the handler function directly
//////////////////////////////////////////////////////////////////////////////
// subscribe to the RenderBegin event with a handler function
LoadingScreen::getEventRenderBegin().connect(renderbegin_event_handler);
// remove subscription to the RenderBegin event later by the handler function
LoadingScreen::getEventRenderBegin().disconnect(renderbegin_event_handler);
//////////////////////////////////////////////////////////////////////////////
// 5. Subscribe to an event saving an ID and unsubscribe later by this ID
//////////////////////////////////////////////////////////////////////////////
// define a connection ID to be used to unsubscribe later
EventConnectionId renderbegin_handler_id;
// subscribe to the RenderBegin event with a lambda handler function and keeping connection ID
renderbegin_handler_id = LoadingScreen::getEventRenderBegin().connect([]() {
Log::message("\Handling RenderBegin event (lambda).\n");
}
);
// remove the subscription later using the ID
LoadingScreen::getEventRenderBegin().disconnect(renderbegin_handler_id);
//////////////////////////////////////////////////////////////////////////////
// 6. Ignoring all RenderBegin events when necessary
//////////////////////////////////////////////////////////////////////////////
// you can temporarily disable the event to perform certain actions without triggering it
LoadingScreen::getEventRenderBegin().setEnabled(false);
// ... actions to be performed
// and enable it back when necessary
LoadingScreen::getEventRenderBegin().setEnabled(true);
Return value
Event reference.void setEnabled ( bool enabled ) #
Specifies if manual rendering of the loading screen can be started or should be stopped.Arguments
- bool enabled - true to enable manual rendering of a custom loading screen; false to disable.
bool isEnabled ( ) const#
Returns a value indicating if manual rendering of a loading screen is allowed.Return value
true if the rendering of the loading screen is enabled; otherwise, false.void setThreshold ( int threshold ) #
Sets the amount of blur in the alpha channel when interpolating between states of the loading screen.Arguments
- int threshold - amount of blur in the [0, 255] range.
int getThreshold ( ) const#
Returns the amount of blur in the alpha channel when interpolating between states of the loading screen.Return value
amount of blur.void setTexturePath ( const char * path ) #
Sets the path to a texture for custom loading screens.Arguments
- const char * path - Path to a file with the custom loading screen texture. If NULL (0) is passed, no texture will be used.
const char * getTexturePath ( ) const#
Returns the path to the texture for loading screens.Return value
Path to the texture of the loading screens.void setImage ( const Ptr<Image> & image ) #
Sets an image for a custom loading screen.Arguments
void getImage ( const Ptr<Image> & image ) const#
Returns the current image for a custom loading screen.Arguments
void setTransform ( const Math::vec4 & transform ) #
Sets transformation of the loading screen texture.Arguments
- const
Math::vec4 & transform - Transformation of the loading screen texture:
- Texture size multiplier.
- Window size multiplier.
- Horizontal position in the [0.0f, 1.0f] range.
- Vertical position in the [0.0f, 1.0f] range.
Math::vec4 getTransform ( ) const#
Returns transformation of the loading screen texture.Return value
transformation of the texture.void setBackgroundColor ( const Math::vec4 & color ) #
Sets the background color of the loading screen.Arguments
- const Math::vec4 & color - Background color.
Math::vec4 getBackgroundColor ( ) const#
Returns the background color of loading screens.Return value
Background color.void setText ( const char * text ) #
Sets the text of the loading screen.Arguments
- const char * text - Text of the loading screen. Can be either a plain or rich text. A number of aliases is available:
- UNIGINE_COPYRIGHT — the UNIGINE copyright text.
- UNIGINE_VERSION — the current UNIGINE version.
- LOADING_PROGRESS — the loading progress going from 0 to 100.
- LOADING_WORLD — the world being loaded (if any).
const char * getText ( ) const#
Returns the text of the loading screen.Return value
Text of the loading screen.void setFontPath ( const char * path ) #
Sets the path to the font used to render the text.Arguments
- const char * path - Path to the font (True Type Font).
const char * getFontPath ( ) const#
Returns the path to the font used to render the text.Return value
Path to the font.void renderInterface ( ) #
Renders a static loading screen. Such a screen does not display any progress.void render ( ) #
Renders the loading screen in the current progress state and with the current stage message.void render ( int progress ) #
Renders a custom loading screen in a given progress state. Use this function in a loop to create a gradual change between the initial (upper opaque part) and the final states (bottom transparent part) of the loading screen texture.Arguments
- int progress - Progress of alpha blending between 2 screens stored in the texture. The value in range [0;100] sets an alpha channel threshold, according to which pixels from the initial (opaque) or final (transparent) screen in the texture are rendered. By the value of 0, the initial screen is loaded. By the value of 100, the final screen is loaded.
void render ( int progress, const char * message ) #
Renders a custom loading screen in a given progress state and prints a given message. Use this function in a loop to create a gradual change between the initial (upper opaque part) and the final states (bottom transparent part) of the loading screen texture, while printing a custom loading stage.Arguments
- int progress - Progress of alpha blending between 2 loading screens stored in the texture. The value in range [0;100] sets an alpha channel threshold, according to which pixels from the initial (opaque) or final (transparent) loading screen in the texture are rendered. By the value of 0, the initial screen is loaded. By the value of 100, the final screen is loaded.
- const char * message - message to print representing the loading stage.
void renderForce ( ) #
Renders the loading screen regardless of whether the manual rendering is allowed or not.void renderForce ( const char * message ) #
Renders the loading screen regardless of whether the manual rendering is allowed or not and prints a given message.Arguments
- const char * message - message to print that represents the loading stage.
int getProgress ( ) const#
Returns the current progress state.Return value
Progress state in the [0, 100] range.const char * getMessage ( ) const#
Returns the text message representing the current loading stage.Return value
Loading message.void setMessageLoadingWorld ( const char * world ) #
Sets a message to be displayed on world loading.Arguments
- const char * world - Message on world loading. The text aliases are also supported.
const char * getMessageLoadingWorld ( ) const#
Returns the current message displayed on world loading.Return value
Message on world loading.void setMessageShadersCompilation ( const char * compilation ) #
Sets a message to be displayed on shaders compilation.Arguments
- const char * compilation - Message on shaders compilation. The text aliases are also supported.