On 08.06.2011 10:23, Paul R wrote:
> ... I have tinkered with some code and things are clearer now, but i still
> have some questions.
>
> For the sake of argument lets say i want to fill a window area with
> randomly sized& placed and randomly coloured rectangles.
>
> I found that i can 'pass' arguments to the draw() function by using its
> constructor XYWH and a global value for colour (which is a really poor
> hack i would like to find a better solution for)
> something like this altered version of Gregs x drawing example is the
> best i can come up with so far, but this just seems all wrong,
Except that your code is incomplete (missing #include's) and buggy
(maybe edited before posting?), nothing is wrong with your idea.
See below for a working example code.
> What would be the best way of repeatedly creating rectangles?
That depends... See below.
> Also the colour assignment seems to be redundant as when the GUI redraws
> It just sets all the rectangles to whatever the current value is,
> Or did i just imagine that cant rememeber was last night
>
> int gColour;
You are using a global variable, but this is not used be the c'tor,
and hence it doesn't _change_ each widget's internals. Instead it is
used later when *drawing* all widgets, and hence they all use the same
color. But there's a better way: you can either add the color argument
to your derived constructor and store it in a private variable for
later (not shown below, left as an exercise ;-) ), or you can use the
builtin widget method color(Fl_Color) to store it in the widget's
existing variable. This is used here:
/* example program to draw "random" rectangles
use fltk-config --compile DrawBox.cxx to compile
*/
#include <FL/Fl_Double_Window.H>
#include <FL/Fl.H>
#include <FL/fl_draw.H>
class DrawBox : public Fl_Widget {
public:
DrawBox(int X, int Y, int W, int H, const char*L=0)
: Fl_Widget(X,Y,W,H,L) {
}
void draw() {
fl_rectf(x(), y(), w(), h(), color());
}
};
int main()
{
Fl_Double_Window win(200,200);
int x = 10, y = 10, w = 20, h = 20;
Fl_Color gColour;
gColour = 2;
//loop start
for (int i=0; i<40; i++) {
DrawBox* draw_box = new DrawBox(x, y, w, h);
draw_box->color(gColour);
gColour = (gColour + 37) % 19;
x = ( x + 2*y + 13*(i+1)) % (win.w()-40) + 10;
y = ( y + 3*x + 23*(i+1)) % (win.h()-40) + 10;
}
win.end();
win.resizable(win);
win.show();
return(Fl::run());
}
/* end of file */
Notes:
- I added win.end(). You should use it, even if it is not really
needed here.
- I added win.resizable(win) to show resizing behavior.
- I added a pseudo-random loop by multiplying with some prime numbers
and using modulo calculation for the window bounds.
As for what would be the "best solution": In the shown example code
you don't have access to individual box widgets, unless you're using
the window's children() method or save the pointers to the widgets
in an own array. If you want to change individual boxes (i.e. their
sizes and/or colors) during the runtime of your program (as written
in your later message), then you would probably use internal storage
of your own design to better access the individual boxes. If this is
what you intend to do, then it would probably be a better approach
to define your own class (e.g. MyCanvas) and add an own structure
with information of your boxes in this widget. MyCanvas's draw()
method would then call all fl_rectf() for all included boxes in one
loop. This would probably be faster than using individual widgets.
As an example that has been discussed here a long time ago, a user
wanted to implement a graphical piano keyboard with individual
buttons for each piano key. The alternative solution would be to
create a keyboard widget and use one draw() method to draw all the
black and white keys (and the background) at once. Event handling
(handle()) for pushing the keyboard keys would then have to find
the individual keys by doing some trivial calculation, but this
would be much faster than letting FLTK check 50-70 single buttons.
I suggest to do some experiments with both approaches and to decide
what you think to be the best solution for the particular problem.
Albrecht
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk