> Well, yeah, does it work? I get a bug where half the screen
> will just turn into a random mess, and I've stepped through
> the process with a debugger, can't find anything wrong there.
Seems to: worked example atatched - sorry, it's bigger than I intended,
but appears to work fine.
Also, be advised that I'm posting via Outlook, so there's a very real
chance the file will have been munged by the time you receive it...
--------------
//
// Test copying between offscreen images
//
// fltk-config --compile dual_offscreen.cxx
//
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Box.H>
#include <FL/fl_draw.H>
#include <FL/x.H>
#include <stdio.h>
#include <math.h>
static const int trace_max = 1024;
static const int view_width = 512;
static const int view_height = 200;
static int trace_pos = 0;
static int view_pos = 0;
static int view_break = 0;
static float trace[trace_max];
static float view[view_width];
static Fl_Offscreen osc1 = NULL; // offscreen drawing buffer
static Fl_Offscreen osc2 = NULL; // offscreen drawing buffer
// A view to display the content of an offscreen
class scope_view: public Fl_Box
{
void draw();
public:
Fl_Offscreen this_osc;
scope_view(int x, int y,int w,int h, const char *l=0) :
Fl_Box(x,y,w,h,l)
{this_osc = NULL;}
};
void scope_view::draw()
{
if (this_osc) fl_copy_offscreen(x(), y(), w(), h(), this_osc, 0, 0);
} /* end of draw() method */
static scope_view *scope = 0; // for viewing osc1
static scope_view *grid = 0; // for viewing osc2
// Draw a moving sine-wave into osc1
static void offscreen_draw()
{
fl_begin_offscreen(osc1);
fl_color(FL_BLACK);
fl_rectf(0, 0, view_width, view_height);
fl_color(FL_GREEN);
fl_push_matrix();
fl_translate(0, (view_height/2));
fl_scale(view_width, view_height/2);
fl_begin_line();
for (int i = 0; i < view_width; i++) {
if(i == view_break) {
fl_end_line();
fl_begin_line();
}
fl_vertex(((float)i/(float)view_width), view[i]);
}
fl_end_line();
fl_pop_matrix();
fl_end_offscreen();
} /* end of offscreen draw function */
// copy the osc1 buffer into osc2 then draw a grid on top
// the grid colour changes dynamically, to show the updates
static void grid_draw()
{
static int icol = 56; // a random colour on the ramp
fl_begin_offscreen(osc2);
// copy osc1 onto osc2
fl_copy_offscreen(0, 0, view_width, view_height, osc1, 0, 0);
// pick a "random" colour to draw the grid with
icol++; if(icol > 255) icol = 56;
Fl_Color col = (Fl_Color)icol;
fl_color(col); // set the colour
fl_push_matrix();
fl_translate(0, (view_height/2));
fl_scale(view_width, view_height/2);
// horizontal gridlines
for(int idx = -75; idx <= 75; idx += 25) {
float dy = (float)idx / 100.0;
fl_begin_line();
fl_vertex(0.0, dy); fl_vertex(1.0, dy);
fl_end_line();
}
// vertical gridlines
for(int idx = 10; idx <= 90; idx += 10) {
float dx = (float)idx / 100.0;
fl_begin_line();
fl_vertex(dx, 1.0); fl_vertex(dx, -1.0);
fl_end_line();
}
fl_pop_matrix();
fl_end_offscreen();
} /* end of offscreen grid draw function */
static void update_offscreen(void *)
{
if (!osc1) {
// now create the offscreen buffer - you can't do this until
// *after* the main_window is mapped...
osc1 = fl_create_offscreen(view_width, view_height);
scope->this_osc = osc1;
}
for (int i = 0; i < 7; i++) {
view[view_pos] = trace[trace_pos];
trace_pos++;
if(trace_pos >= trace_max) trace_pos = 0;
view_pos++;
if(view_pos >= view_width) view_pos = 0;
}
view_break = view_pos;
if (osc1) offscreen_draw();
Fl::repeat_timeout(0.05, update_offscreen);
scope->redraw();
}
static void update_grid(void *)
{
if (!osc2) {
osc2 = fl_create_offscreen(view_width, view_height);
grid->this_osc = osc2;
}
if (osc2) grid_draw();
Fl::repeat_timeout(0.07, update_grid);
grid->redraw();
}
int main(int argc, char **argv)
{
Fl_Window *main_win = new Fl_Window(522, 415, "Scope Window");
main_win->begin();
// view the first offscreen - the scope trace
scope = new scope_view(5, 5, view_width, view_height);
// view the second offscreen, the trace with the grid overlaid
grid = new scope_view(5, 210, view_width, view_height);
main_win->end();
// now create the scope trace - a sine wave
for (int i = 0; i < trace_max; i++) {
trace[i] = -sin(((i*5*2)*M_PI/(double)trace_max));
}
for (int i = 0; i < view_width; i++) {
view[i] = trace[i];
}
trace_pos = view_width;
main_win->show(argc, argv);
// animate the windows
Fl::add_timeout(0.1, update_offscreen);
Fl::add_timeout(0.13, update_grid);
return Fl::run();
}
/* end of file */
--------------
SELEX Galileo Ltd
Registered Office: Sigma House, Christopher Martin Road, Basildon, Essex SS14
3EL
A company registered in England & Wales. Company no. 02426132
********************************************************************
This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender.
You should not copy it or use it for any purpose nor disclose or
distribute its contents to any other person.
********************************************************************
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk