Hi,
I think I got somewhere using the offscreen. I managed to draw a black window 
with a red square in the middle by standard drawing and by offscreen drawing, 
you can comment in/out the function call doing it.
Moreover, I also managed to create the offscreen outside the draw() in the 
derived window, but then you need to precede the fl_create_offscreen by 
Fl::flush( ); aWin->make_current( );
Alternatively you still can create it within the draw() function, but I dislike 
that as it is an action to be taken once, so it's ugly to have draw() check all 
the time for an initialized pointer ... at least, in my opinion.

Hopefully this example will be useful also for other people trying to learn the 
offscreen usage ... or could anybody simplify/clarify it further?

I have two questions:

a) including <FL/x.H> is ugly for cross-platform programming, can I include an 
header automatically selecting x.h or win32.h or any platform related header 
which is needed?

b) so far the drawing is done through fl_rectf, would it work the same way if 
full widgets are drawn?

Regards,
Fabio

==================== Example program ================================

//offscreen-test.cpp by Fabio Bracci <[email protected]>
#include <iostream>
#include <string>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#include <FL/fl_draw.H>
#include <FL/x.H>
#include <FL/Fl_Double_Window.H>


Fl_Offscreen offscr = NULL;
int offscrW = 400, offscrH = 400;

void drawRectangles( int winW, int winH ) {
  std::cout << "drawRectangles( int winW, int winH ) START" << std::endl;
  int x = 0, y = 0;

  std::cout << "aWin x=" << x << " y=" << y << " w=" << winW << " h=" << winH 
<< std::endl;

  fl_color( FL_BLACK );
  fl_rectf( x, y, x + winW, y + winH );
  std::cout << "screen filled in black" << std::endl;

  fl_color( FL_BLUE );
  fl_font( FL_TIMES, 15 );
  fl_draw( "test", 10, 10 );

  int margin = 100;

  int redRectX = x + margin;
  int redRectY = y + margin;
  int redRectW = winW - 2 * margin;
  int redRectH = winH - 2 * margin;

  std::cout << "RECTANGLE x=" << redRectX << " y=" << redRectY << " w=" << 
redRectW << " h=" << redRectH << std::endl;

  fl_color( FL_RED );
  fl_rectf( redRectX, redRectY, redRectW, redRectH );
  std::cout << "screen filled in red" << std::endl;

  std::cout << "drawRectangles( int winW, int winH ) END" << std::endl;
}

void drawOffscreen( Fl_Double_Window *aWin ) {
  std::cout << "drawOffscreen( Fl_Double_Window *aWin ) START" << std::endl;
  std::cout << "*aWin is " << (aWin->shown( ) ? "shown" : "not shown") << 
std::endl;

  fl_begin_offscreen( offscr );
  std::cout << "Fl_Offscreen OPENED" << std::endl;

  drawRectangles( aWin->w( ), aWin->h( ) );

  fl_end_offscreen( );
  std::cout << "Fl_Offscreen CLOSED" << std::endl;

  int srcX = 0, srcY = 0, destX = 0, destY = 0;

  fl_copy_offscreen( destX, destY, offscrW, offscrH, offscr, srcX, srcY );
  std::cout << "Fl_Offscreen COPIED" << std::endl;

  std::cout << "drawOffscreen( Fl_Double_Window *aWin ) START" << std::endl;
}

class MyWin : public Fl_Double_Window {
protected:

  void draw( ) {
    std::cout << "MyWin::draw()" << std::endl;

    //    if ( !offscr ) {
    //      offscr = fl_create_offscreen( offscrW, offscrH );
    //      std::cout << "Fl_Offscreen CREATED" << std::endl;
    //    }
    Fl_Double_Window::draw( );

    drawOffscreen( this );
    // ALTERNATIVELY COMMENT OUT THE ABOVE LINE OR THE LINE BELOW TO DRAW 
WITHOUT OR WITH OFFSCREEN
    //        drawRectangles( w( ), h( ) );
  }

public:

  MyWin( int a, int b, char const *label = "Fl_Offscreen test" )
  :
  Fl_Double_Window( a, b, label ) { }
};

int main( ) {

  Fl_Double_Window *aWin = new MyWin( 400, 400, "Fl_Offscreen test" );
  aWin->resizable( );
  aWin->show( );
  std::cout << "Fl_Double_Window started" << std::endl;

  // ALTERNATIVELY COMMENT OUT THE OFFSCREEN SETUP BELOW OR IN MyWin::Draw()
  Fl::flush( );
  aWin->make_current( );
  offscr = fl_create_offscreen( offscrW, offscrH );
  std::cout << "Fl_Offscreen created" << std::endl;

  return Fl::run( );
}

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

Reply via email to