RE: [fltk.general] Is it possible to alternate between 2 Fl_Windows?

35 views
Skip to first unread message

MacArthur, Ian (Selex ES, UK)

unread,
Apr 30, 2015, 4:29:34 AM4/30/15
to fltkg...@googlegroups.com
First off, please do not post incomplete examples, it makes it harder for folks to help you, and therefore less likely that they will help you.

Please post examples that are complete enough to be compileable...

> I have 2 Fl_Windows structs as shown below. Each struct has its own
> variable: splashScreen for struct SplashScreen and instructionsScreen
> for struct InstructionScreen.

Convention dictates that your "struct" types here ought to be "class" types.
Though that is not the source of your problem.

> Is it possible to "forward declare" a fltk
> type? I have been trying to do a "forward declaration" of variable
> instructionsScreen but I just cannot find how.

This is not a fltk problem, this is a programming syntax issue. Your InstructionScreen type is incompletely defined at the point that you want to use it in your SplashScreen class.

There are a number of ways to work around that - attached below a slightly hacky way that works, with minimal changes to your code.

A better way would be to separate out the class definition from the definition of the member functions; then give both class definitions at the start of your file and the method implementations later - then there should be enough information for the compiler to resolve your calls in the member functions.

Here's the working hack solution:

----------------------

#include <FL/Fl.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Double_Window.H>

// Forward declare the derived classes
class SplashScreen;
class InstructionsScreen;

InstructionsScreen *pInstSc = (InstructionsScreen*)0;

// --------------------------------------------------------------------
// Define and instantiate class for splash screen.
// --------------------------------------------------------------------
class SplashScreen : public Fl_Double_Window {
Fl_Button *btnSplash;


// Action to perform for "Go to instructions" button's callback.
void cb_btnSplashAux() {
hide();
if (pInstSc) {
// pInstSc is incomplete at this point,
// but we "know" it is derived from Fl_Double_Window
Fl_Double_Window *pWin = (Fl_Double_Window*)pInstSc;
pWin->show();
}
}

// "Go to instructions" button's static callback.
static void cb_btnSplash(Fl_Widget *but, void *data) {
SplashScreen *o = (SplashScreen*)data;
o->cb_btnSplashAux();
}

public:
SplashScreen(int W, int H, const char *L = 0) : Fl_Double_Window(W, H, L) {

btnSplash = new Fl_Button(660, 550, 120, 40, "Go to Instructions");
btnSplash->callback(cb_btnSplash, (void*)this);

}
};
SplashScreen splashScreen(800, 600, "Flip Flaps - Splash");

// --------------------------------------------------------------------
// Define and instantiate class for instructions screen.
// --------------------------------------------------------------------
class InstructionsScreen : public Fl_Double_Window {
Fl_Button *btnInstructions;

// Action to perform for "Go to splash" button's callback.
void cb_btnInstructionsAux() {
hide();
splashScreen.show();
}

// "Go to splash" button's static callback.
static void cb_btnInstructions(Fl_Widget *but, void *data) {
InstructionsScreen *o = (InstructionsScreen*)data;
o->cb_btnInstructionsAux();
}

public:
InstructionsScreen(int W, int H, const char *L = 0) : Fl_Double_Window(W, H, L) {

btnInstructions = new Fl_Button(660, 550, 120, 40, "Go to Splash");
btnInstructions->callback(cb_btnInstructions, (void*)this);

}

};

InstructionsScreen instructionsScreen(800, 600, "Flip Flaps - Instructions");

// ********************************************************************
// ********************************************************************
// Main function.
// ********************************************************************
// ********************************************************************
int main()
{
pInstSc = &instructionsScreen;
splashScreen.show();
return Fl::run();
}

/* end of file */






Selex ES Ltd
Registered Office: Sigma House, Christopher Martin Road, Basildon, Essex SS14 3EL
A company registered in England & Wales. Company no. 02426132
********************************************************************
This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender.
You should not copy it or use it for any purpose nor disclose or
distribute its contents to any other person.
********************************************************************

MacArthur, Ian (Selex ES, UK)

unread,
Apr 30, 2015, 4:39:00 AM4/30/15
to fltkg...@googlegroups.com
OK, I felt guilty about the hacky cast in the previous example, so here's the job done "better"...



#include <FL/Fl.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Double_Window.H>

// --------------------------------------------------------------------
// Define and instantiate class for splash screen.
// --------------------------------------------------------------------
class SplashScreen : public Fl_Double_Window {
Fl_Button *btnSplash;

// Action to perform for "Go to instructions" button's callback.
// implemented after instructionsScreen is declared
void cb_btnSplashAux();

// "Go to instructions" button's static callback.
static void cb_btnSplash(Fl_Widget *but, void *data) {
SplashScreen *o = (SplashScreen*)data;
o->cb_btnSplashAux();
}

public:
SplashScreen(int W, int H, const char *L = 0) : Fl_Double_Window(W, H, L) {
btnSplash = new Fl_Button(660, 550, 120, 40, "Go to Instructions");
btnSplash->callback(cb_btnSplash, (void*)this);
}
};

// --------------------------------------------------------------------
// Define and instantiate class for instructions screen.
// --------------------------------------------------------------------
class InstructionsScreen : public Fl_Double_Window {
Fl_Button *btnInstructions;

// Action to perform for "Go to splash" button's callback.
// implemented after splashScreen is declared
void cb_btnInstructionsAux();

// "Go to splash" button's static callback.
static void cb_btnInstructions(Fl_Widget *but, void *data) {
InstructionsScreen *o = (InstructionsScreen*)data;
o->cb_btnInstructionsAux();
}

public:
InstructionsScreen(int W, int H, const char *L = 0) : Fl_Double_Window(W, H, L) {

btnInstructions = new Fl_Button(660, 550, 120, 40, "Go to Splash");
btnInstructions->callback(cb_btnInstructions, (void*)this);
}
};

// Declare the window class instances
SplashScreen splashScreen(800, 600, "Flip Flaps - Splash");
InstructionsScreen instructionsScreen(800, 600, "Flip Flaps - Instructions");

// Now implement the methods that depend on the instance declarations
void SplashScreen::cb_btnSplashAux() {
hide();
instructionsScreen.show();
}

void InstructionsScreen::cb_btnInstructionsAux() {
hide();
splashScreen.show();
}

// ********************************************************************
// Main function.
// ********************************************************************
int main()
{

jorgem...@gmail.com

unread,
May 1, 2015, 12:08:35 PM5/1/15
to fltkg...@googlegroups.com, ian.ma...@selex-es.com
Thank you very much for your answer.
I notice that you are using Fl_Double_Window instead of Fl_Window. I went to documentation and it says Fl_Double_Window provides a double buffered window and there is also an additional "detail description" but I do not get the whole idea. Can you please provide a simple explanation? Also, would the code work with Fl_Window?

Best regards,
Jorge Maldonado

Ian MacArthur

unread,
May 1, 2015, 12:43:44 PM5/1/15
to fltkg...@googlegroups.com
On Fri May 01 2015 17:08:35, jorgem...@gmail.com wrote:
> Thank you very much for your answer.
> I notice that you are using Fl_Double_Window instead of Fl_Window. I went to documentation and it says Fl_Double_Window provides a double buffered window and there is also an additional "detail description" but I do not get the whole idea. Can you please provide a simple explanation?

It probably don’t make much difference with modern compositing window managers, but it used to be that a double-buffered window could *appear* smoother as there was less chance of the user seeing “tearing” artefacts due to redrawing whilst the window was visible...

I tend to default to using the double window...


> Also, would the code work with Fl_Window?

Yes, they are interchangeable.








Reply all
Reply to author
Forward
0 new messages