A possible solution for apps with a slow setup(). What do you think?


#1

Hi,
my application needs a lot of initialization stuff to be done in the setup() method (like loading graphics and even downloading something on the fly from a server). All of that takes 2-3 seconds, and in the meanwhile nothing happens from user’s perspective.

This is enough to make people nervous and even double click a second time on app icon, and should of course be avoided. After playing for a while with splash screens I couldn’t find anything satisfactory, they made the code heavily platform dependent and did not solve the problem after all.

So I tried a much simpler way that (for me at least) does the trick. Since I am not an expert at all in OF I am asking for comments and may be possible drawbacks. The idea is to delay setup() after a few frames have been drawn, so that the window can appear with a message to the user. Since the message is static it does not matter if the framerate is extremely low during the initialization.

here is a little snippet:

in ofApp.h:

void first_setup();
int framesDrawn;
ofTrueTypeFont messageFont; // this is only needed if you want to show a message while loading

and in ofApp.cpp:

const int SETUP_FRAMES = 2;

void ofApp::setup()
{
    ofSetVerticalSync(true);
    ofSetFrameRate(60);

    messageFont.load("font/F25_Executive.otf", 32, true, true);
    
    framesDrawn = 0;
}

void ofApp::first_setup()
{
    // do all heavy initialization stuff here
}

void ofApp::update()
{
    if(framesDrawn <= SETUP_FRAMES) return;

    // regular update() code goes here
}

void ofApp::draw()
{
    ofBackground(80);
    if(framesDrawn < SETUP_FRAMES)
    {
        string line = "Application is loading. Please wait.";
    
        // draw a message on screen, centered on X and Y

        int lineH = messageFont.getLineHeight();
        int y = (ofGetHeight()-lineH)/2 + messageFont.getAscenderHeight();
    
        int lineW = messageFont.stringWidth(line);
        int x = (ofGetWidth()-lineW)/2;
        
        messageFont.drawString(line, x, y);
    
        framesDrawn++;
        return;
    }
    else if(framesDrawn == SETUP_FRAMES)
    {
       first_setup();
       framesDrawn++;
    }

    // here goes all draw() code
}

#2

I just used this in a similar situation, its a nice feature from elliotwoods


#3

cool, before reinventing the wheel I searched for a solution to my splash screen problem but for some reason I missed this one.