>
> On 3 Feb 2009, at 18:23, Fabio Bracci wrote:
> >
> > I'm trying to set up a small example program to play with the
> > offscreen.
>
>
> OK - below is my reworked offscreen demo.
> Sorry to all that it ended up being so long, but I got a bit carried
> away.
>
> Anyway, what I am attempting to show is how to create and draw to an
> fl_offscreen context. The demo also shows how to do incremental
> drawing (which fltk in general does not allow) by use of the
> offscreen context to draw into.
> Also, I was going to add some scrollbars to move around the view, but
> then decided to make the view draggable instead - scrollbars wold
> work in basically the same way (updating the view offset) but I
> thought this was cooler.
> The actual drawing I am doing is rather simplistic - but I quite like
> the abstract effect it produces!
> Anyway, hope this is of some help to you,
> --
> Ian
>
> /
> ************************************************************************
> *****/
> // fltk-config --compile offscreen-test.cxx
>
> /* Standard headers */
> #include <stdlib.h>
> /* Fltk headers */
> #include <Fl/Fl.h>
> #include <FL/Fl_Double_Window.H>
> #include <FL/x.H>
> #include <Fl/Fl_Box.H>
> #include <Fl/fl_draw.h>
>
> static Fl_Double_Window *main_window = 0;
> static Fl_Offscreen oscr = 0;
> static int os_w = 0, os_h = 0;
> /
> ************************************************************************
> *****/
> class oscr_box : public Fl_Box {
> void draw();
> int handle(int event);
> protected:
> int x1, y1;
> int xoff, yoff;
> int drag_state;
> int page_x, page_y;
> public:
> oscr_box(int x, int y, int w, int h);
> };
> /
> ************************************************************************
> *****/
> oscr_box::oscr_box(int x, int y, int w, int h) : Fl_Box(x,y,w,h){
> /* Constructor */
> x1 = y1 = 0;
> xoff = yoff = 0;
> drag_state = 0;
> page_x = page_y = 750;
> } // Constructor
> /
> ************************************************************************
> *****/
> void oscr_box::draw() {
> int wd = w();
> int ht = h();
> int xo = x();
> int yo = y();
>
> fl_color(fl_gray_ramp(19));
> fl_rectf(xo, yo, wd, ht);
>
> if(oscr) { // offscreen exists
> fl_copy_offscreen(xo, yo, wd, ht, oscr, page_x, page_y);
> }
> else { // create offscreen
> main_window->make_current(); // seem to need a context to base
> the
> offscreen on...
> os_w = 2000; os_h = 2000;
> oscr = fl_create_offscreen(os_w, os_h);
> }
> } // draw method
> /
> ************************************************************************
> *****/
> int oscr_box::handle(int ev) {
> int ret = Fl_Box::handle(ev);
> // handle dragging of visible page area - if a valid context exists
> // all sorts of editor stuff needs to be added here... later...
> if(oscr) {
> int x_max = os_w;
> int y_max = os_h;
>
> switch(ev) {
> case FL_ENTER:
> main_window->cursor(FL_CURSOR_HAND);
> ret = 1;
> break;
>
> case FL_LEAVE:
> main_window->cursor(FL_CURSOR_DEFAULT);
> ret = 1;
> break;
>
> case FL_PUSH:
> x1 = Fl::event_x_root();
> y1 = Fl::event_y_root();
> drag_state = 1; // drag
> ret = 1;
> break;
>
> case FL_DRAG:
> if(drag_state == 1) { // dragging page
> int x2 = Fl::event_x_root();
> int y2 = Fl::event_y_root();
> xoff = x1 - x2;
> yoff = y1 - y2;
> x1 = x2; y1 = y2;
> page_x += xoff;
> page_y += yoff;
> // check the page bounds
> if(page_x < -w()) page_x = -w();
> else if(page_x > x_max) page_x = x_max;
> if(page_y < -h()) page_y = -h();
> else if(page_y > y_max) page_y = y_max;
> redraw();
> }
> ret = 1;
> break;
>
> case FL_RELEASE:
> drag_state = 0;
> ret = 1;
> break;
>
> default:
> break;
> }
> }
> return ret;
> } // handle
> /
> ************************************************************************
> *****/
> static oscr_box *os_box = 0; // a widget to view the offscreen with
>
> /
> ************************************************************************
> *****/
> static void oscr_drawing (void) {
> Fl_Color col;
> static int icol = 56;
> static int ox = 1000, oy = 1000;
> static int iters = 3456;
>
> if(!oscr) return;
>
> fl_begin_offscreen(oscr); /* Open the offscreen context for drawing */
>
> if(iters > 1024) {
> fl_color(FL_WHITE);
> fl_rectf(0, 0, os_w, os_h);
> iters = 0;
> }
> iters++;
>
> icol++; if(icol > 255) icol = 56;
> col = (Fl_Color)icol;
> fl_color(col); // set the colour
>
> int rnd = rand();
> double drx = (double)os_w * (double)rnd / (double)RAND_MAX;
> rnd = rand();
> double dry = (double)os_h * (double)rnd / (double)RAND_MAX;
> rnd = rand();
> double drt = 4.0 * (double)rnd / (double)RAND_MAX;
>
> int ex = (int)drx;
> int ey = (int)dry;
> fl_line_style(FL_SOLID, (int)drt);
> fl_line(ox, oy, ex, ey);
> ox = ex; oy = ey;
>
> fl_end_offscreen(); // close the offscreen context
> } // oscr_drawing
> /
> ************************************************************************
> *****/
> static void oscr_anim(void *) {
> if(oscr) { // offscreen exists, draw something
> oscr_drawing();
> }
> os_box->redraw();
> Fl::repeat_timeout(0.1, oscr_anim);
> } // oscr_anim
> /
> ************************************************************************
> *****/
> int main (int argc, char **argv) {
> main_window = new Fl_Double_Window(400, 300, "Offscreen demo");
> main_window->begin();
> os_box = new oscr_box(5, 5, 390, 290);
> main_window->end();
> main_window->resizable(os_box);
>
> main_window->show(argc, argv);
>
> Fl::add_timeout(0.1, oscr_anim);
> srand(time(NULL));
>
> return Fl::run();
> } // main
> /
> ************************************************************************
> *****/
> /* end of file */
>
>
>
>
I know this example is just to illustrate the question our friend had( to
understand the use of offscreen methods), but I was wondering if I wanted to do
exactly the same example you did, if I just create the main screen as
Fl_Double_Window as you did would solve all the double buffering problems, and
I wouldn't need to care about making a offscreen draw method, because the son
object would draw in the main screen's draw method which is a offscreen draw.
Am I right or do I need to have all my objects in the screen doing double
buffering even if my parent window is already Fl_Double_Window?
J. Marcelo Auler
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk