On 16 Feb 2010, at 20:12, Dufour wrote:
>
> Thanks a lot for the answer !
>
> I will use the "offset drawing" method. It's a part of the tutorial  
> I don't quite understand. Do you have some examples of how you  
> would do that?

Below is a (rather long and poorly commented) example; it creates an  
large off-screen area, then draws randomly into that surface's area.

A smaller, resizeable, viewport is provided that displays a subset of  
the offscreen area.
By clicking and dragging you can move the view around the offscreen  
area as it is updated.

I think that demonstrates most of the techniques you will need.
----------------------------------------------
/ 
************************************************************************ 
*****/
//   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; // offscreen area dimensions
/ 
************************************************************************ 
*****/
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 */

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




_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to