Unable to draw a box

43 views
Skip to first unread message

jorgem...@gmail.com

unread,
Apr 21, 2015, 4:31:12 PM4/21/15
to fltkg...@googlegroups.com
Hello,

I am totally new to fltk and I am trying to run a very simple code that I include below. I am running FLTK for Windows in Visual Studio 2013 and my goal is to draw a box. When I run the code I can see a window and a button but the box is not shown. I will very much appreciate your feedback.

#include "stdafx.h"
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_JPEG_Image.H>
#include <FL/Fl_Box.H>
#include <FL/fl_draw.H>


int main(){
Fl_Window win(400, 300, 600, 400, "Flip Flaps");
Fl_Button* Button = new Fl_Button(500, 320, 80, 30);

fl_draw_box(FL_FLAT_BOX, 10, 10, 500, 30, fl_rgb_color(255, 0, 0));

win.show();
return Fl::run();
}

Respectfully,
Jorge Maldonado

Ian MacArthur

unread,
Apr 21, 2015, 4:46:48 PM4/21/15
to fltkg...@googlegroups.com
On Tue Apr 21 2015 03:10:22, jorgem...@gmail.com wrote:
> Hello,
>
> I am totally new to fltk and I am trying to run a very simple code that I include below. I am running FLTK for Windows in Visual Studio 2013 and my goal is to draw a box. When I run the code I can see a window and a button but the box is not shown. I will very much appreciate your feedback.


You want to start of taking a look at the docs, and looking at the simple examples in the test folder, to see how things work.

Also, you probably want to looks at Erco’s cheat sheet:

http://seriss.com/people/erco/fltk/

And take it from there.





Greg Ercolano

unread,
Apr 21, 2015, 5:04:46 PM4/21/15
to fltkg...@googlegroups.com
On 04/21/15 13:46, Ian MacArthur wrote:
> On Tue Apr 21 2015 03:10:22, jorgem...@gmail.com wrote:
>> Hello,
>>
>> I am totally new to fltk and I am trying to run a very simple code that I include below. I am running FLTK for Windows in Visual Studio 2013 and my goal is to draw a box. When I run the code I can see a window and a button but the box is not shown. I will very much appreciate your feedback.
>
> You want to start of taking a look at the docs,

Yes, specifically the section entitled "Drawing things in FLTK".

To find the docs, go to the fltk.org main page, click "Documentation",
and find the version you're running (hopefully FLTK 1.3.3), click
the HTML link, and look for the above section.

In particular, read those first few paragraphs, as they pertain
to your use of fl_draw_box() outside of a derived class's draw() method.

To draw shapes, you'll need to derive your own class (in your case,
you probably want to derive from Fl_Window if you want to draw anywhere
in the window) and create a draw() method for your class, and put
your fl_draw_box() code there, e.g.

class YourWindow : public Fl_Window {
public:
YourWindow(int W,int H,const char *L=0) : Fl_Window(W,H,L) { }
void draw() { // class's draw() method -- the only place where drawing can be done
Fl_Window::draw(); // First: let window draw itself (the bg, child widgets, buttons..)
fl_draw_box(FL_FLAT_BOX, 10, 10, 500, 30, fl_rgb_color(255, 0, 0)); // ..second, draw your box
}
};
..
int main() {
YourWindow win(600,400,"Flip Flaps"); // use "YourWindow" instead of "Fl_Window"
..
}

jorgem...@gmail.com

unread,
Apr 27, 2015, 9:24:43 PM4/27/15
to fltkg...@googlegroups.com, erco_...@seriss.com
Hi,

I have been all day long trying to make the code work with a callback without success. Such a callback must be executed in response to a button click where I update some data in functions UpdateFlipsCount() and FlipPancakes(2) and finally the window (pancakesScreen in this case) must be redrawn. Your example defines the window in the "main" function so the callback does not see it. I tried defining pancakesWindow as a global variable but I need such a window to be available at a "later time" and not as soon as the code runs. Is it possible to pass the window as a parameter to a callback so the redraw() method can be executed?

I will very much appreciate your support, this issue seems to be the last one to finish my final college project.

(I wonder if this is a FLTK or C++ question but, if I ask it in a C++ forum, maybe it will be less possible that someone knows about FLTK).


void cb_btnFlip2(Fl_Widget*, void*)
{
UpdateFlipsCount();
FlipPancakes(2);
pancakesScreen.redraw();
}


Best regards,
Jorge Maldonado

Greg Ercolano

unread,
Apr 27, 2015, 9:49:08 PM4/27/15
to fltkg...@googlegroups.com
On 04/27/15 18:24, jorgem...@gmail.com wrote:
[..]Your example defines the window in the "main" function so the callback does not see it.

    To avoid a global, pass a pointer to your window through the callback.
    So for instance:

static void cb_btnFlip2(Fl_Widget*, void *data) {
        pancakesScreen *pswin = (pancakesScreen*)data;
pswin->UpdateFlipsCount();
pswin->FlipPancakes(2);
pswin->redraw();
}

int main(..) {
    pancakesScreen pswin(..);
    ..
    somebutton->callback(cb_btnFlip2, (void*)&pswin);
}

    The FLTK videos cover this use of void*; remember you can pass
    a pointer to anything.. often the 'userdata' option for callbacks is used
    to pass a pointer to the object that the callback needs access to.



Reply all
Reply to author
Forward
0 new messages