Robb Main wrote:
In the code below, the PNG image I'm loading is not displayed immediately. I've 
been unable to overcome the delay before it is displayed, although I'm told the 
image should be ready to display immediately after loading (i.e., the 
constructor returns). What am I doing wrong?

Thanks,
Robb.

As Matthias suggested, you should separate the splash screen code and the main window code - this would make your code much easier and better to understand.

Please read his suggestions ...

However, your code works almost okay, but ...

        Fl_Window *pWin = new myWindow(0, 0, 640, 480);
        // wait while splash is displayed
        /*
                There is a noticeable delay before the PNG image appears, 
because the
                picture is NOT rendered by the first itteration of the loop 
below.
                The picture is rendered when Fl::check() is called.
                I tried numerous different things (see constructor above), in 
order to
                "flush" the PNG image to the display immediately, but nothing 
worked.
        */
        for (int i=0; i<3; i++ ) {
                usleep( 1000000 );
                Fl::check();
        }

you wrote it yourself. The splash screen will only be displayed, when you call Fl::check(), but this is called at the end of your loop after usleep(). Change this (move Fl::check() at the beginning of the loop, or call it once before the loop, and there will be no visible delay.

        // Now show the main window - this will remove the splash screen
        pWin->show();
        return Fl::run();
}
------------------------
<end code>

Although I would recommend to separate the two windows, like Matthias said, I'll append a slimline version of your code, just to show you another approach that closely matches your way.

Albrecht

// compile with fltk-config --use-images --compile splash.cxx

#include <FL/Fl.H>
#include <FL/Fl_Shared_Image.H>
#include <FL/Fl_Single_Window.H>
#include <FL/Fl_PNG_Image.H>
#include <FL/Fl_Button.H>

// for usleep (Sleep) used in main()
#ifdef WIN32
#include <windows.h>      // usleep doesn't work for Windows (?)
#else
#include <unistd.h>
#endif // WIN32

class splashWindow : public Fl_Window {
    Fl_PNG_Image *pBGImage;
protected:
    virtual void draw() {
        Fl_Window::draw();              // Draw window widget first
        if ( pBGImage )
            pBGImage->draw(10,10);   // draw PNG image over the above
    }
public:
    // Constructor
splashWindow(int X, int Y, int W, int H, char *pszTitle, char *pszBGImage) : Fl_Window(X,Y,W,H) {

        pBGImage = new Fl_PNG_Image( pszBGImage );
        // resize(X,Y,pBGImage->w()+20,pBGImage->h()+20); // make it as big as 
the image
        border(0);
        end();
    }
};

class myWindow : public Fl_Window {
    splashWindow *pSplashWin;
public:
    void show() {
        Fl_Window::show();                  // Show main window widget first
        if (pSplashWin) {
            // Cleanup
            // ** remove( pSplashWin );     // no need to remove splash window
            delete( pSplashWin );           // deallocate it
            pSplashWin = 0;
        }
    } // show()

// Constructor
    myWindow(int X, int Y, int W, int H) : Fl_Window(X,Y,W,H) {
        pSplashWin = 0;

        // Add a button to the main screen, so we know when it's displayed.
        Fl_Button *btn1 = new Fl_Button(40, 40, 200, 200, "MAIN WINDOW" );
        end();

        // Create *Independent* Splash Screen window *after* end()
        pSplashWin = new splashWindow( X, Y, W, H, "My Splash", 
"./splash-bg3.png" );
        pSplashWin->show();
    }
};

int main(int argc, char ** argv) {

    fl_register_images();
    Fl::scheme("plastic");
    Fl_Window *pWin = new myWindow(200, 200, 640, 480);
    // wait while splash is displayed
    /*
        There is a noticeable delay before the PNG image appears, because the
        picture is NOT rendered by the first itteration of the loop below.
        The picture is rendered when Fl::check() is called.
        I tried numerous different things (see constructor above), in order to
        "flush" the PNG image to the display immediately, but nothing worked.
    */
    for (int i=0; i<9; i++ ) {
        Fl::check();            // call this first
#ifdef WIN32
            Sleep ( 1000 );
#else
            usleep( 1000000 );
#endif
    }
    // Now show the main window - this will remove the splash screen
    pWin->show(argc,argv);
    return Fl::run();
}
//
// compile with fltk-config --use-images --compile splash.cxx
//
#include <FL/Fl.H>
#include <FL/Fl_Shared_Image.H>
#include <FL/Fl_Single_Window.H>
#include <FL/Fl_PNG_Image.H>
#include <FL/Fl_Button.H>

// for usleep (Sleep) used in main()
#ifdef WIN32
#include <windows.h>    // usleep doesn't work for Windows (?)
#else
#include <unistd.h>
#endif // WIN32

class splashWindow : public Fl_Window {
    Fl_PNG_Image *pBGImage;
protected:
    virtual void draw() {
        Fl_Window::draw();              // Draw window widget first
        if ( pBGImage )
            pBGImage->draw(10,10);      // draw PNG image over the above
    }
public:
    // Constructor
    splashWindow(int X, int Y, int W, int H, char *pszTitle, char *pszBGImage) 
: Fl_Window(X,Y,W,H) {

        pBGImage = new Fl_PNG_Image( pszBGImage );

        // resize(X,Y,pBGImage->w()+20,pBGImage->h()+20); // make it as big as 
the image

        border(0);      // this would be useless for a child window
        end();
    }
};

class myWindow : public Fl_Window {
    splashWindow *pSplashWin;
public:
    void show() {
        Fl_Window::show();                  // Show main window widget first
        if (pSplashWin) {
            // Cleanup
            // ** remove( pSplashWin );     // no need to remove splash window
            delete( pSplashWin );           // deallocate it
            pSplashWin = 0;
        }
    } // show()

// Constructor
    myWindow(int X, int Y, int W, int H) : Fl_Window(X,Y,W,H) {
        pSplashWin = 0;

        // Add a button to the main screen, so we know when it's displayed.
        Fl_Button *btn1 = new Fl_Button(40, 40, 200, 200, "MAIN WINDOW" );
        end();

        // Create *Independent* Splash Screen window *after* end()
        pSplashWin = new splashWindow( X, Y, W, H, "My Splash", 
"./splash-bg3.png" );
        pSplashWin->show();
    }
};

int main(int argc, char ** argv) {

    fl_register_images();
    Fl::scheme("plastic");
    Fl_Window *pWin = new myWindow(200, 200, 640, 480);
    // wait while splash is displayed
    /*
        There is a noticeable delay before the PNG image appears, because the
        picture is NOT rendered by the first itteration of the loop below.
        The picture is rendered when Fl::check() is called.
        I tried numerous different things (see constructor above), in order to
        "flush" the PNG image to the display immediately, but nothing worked.
    */
    for (int i=0; i<3; i++ ) {
        Fl::check();            // call this first
#ifdef WIN32
            Sleep ( 1000 );
#else
            usleep( 1000000 );
#endif
    }
    // Now show the main window - this will remove the splash screen
    pWin->show(argc,argv);
    return Fl::run();
}
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to