Hello,
thanks for the tips, I did a quick modification doing the redrawing of all
rectangle within one container in the draw() function but it's even slower.
I did also play with the return values from the handler in the original program
but could not notice any differenze.
Are there any other ways to make it faster (I can't use opengl since the
graphic card does not support it).
Thanks,
Jens
Here is the program (below is the output from the profiling).
----------------------------------------------------------------
#include <FL/Fl.H>
#include <FL/fl_draw.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Scroll.H>
#include <FL/Fl_Button.H>
#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include "eventnames.h"
#define MAXSIZE (32768) // zoom limit is ok for the moment
using namespace std;
Fl_Double_Window *win;
Fl_Scroll *scroll;
int divider;
int diex = 9408000, diey = 9408000; // real world chip size
int gzoom = 1;
// click on rectangle changes color to green
class Draw : public Fl_Widget {
private:
int x_;
int y_;
int w_;
int h_;
int c_;
int z_;
public:
static int zoom;
static int components;
Draw(int x, int y, int w, int h, const char *l=0) : Fl_Widget(x, y ,w ,h, l)
{
x_ = x;
y_ = y;
w_ = w;
h_ = h;
c_ = FL_WHITE;
components++;
}
const int x0(void) const { return x_; };
const int y0(void) const { return y_; };
const int w0(void) const { return w_; };
const int h0(void) const { return h_; };
void x0(const int x) { x_ = x; }
void y0(const int y) { y_ = y; }
void w0(const int w) { w_ = w; }
void h0(const int h) { h_ = h; }
protected:
void draw() {
fl_color(c_);
fl_rectf(x(), y(), w(), h());
fl_color(FL_BLACK);
fl_rect(x(), y(), w(), h());
}
int handle(int e) {
if((e == FL_PUSH) && (Fl::event_button() == FL_LEFT_MOUSE)) {
Fl::redraw();
cout << Fl::event_inside(x(), y(), w(), h()) <<endl;
if(Fl::event_inside(x(), y(), w(), h())) {
c_ = FL_GREEN;
}
}
return(Fl_Widget::handle(e));
}
};
int Draw::components = 0;
class Button : public Fl_Button {
public:
Button(int x, int y, int w, int h, const char *l=0) : Fl_Button(x, y ,w ,h,
l) {
}
private:
int handle(int e) {
switch(e) {
case FL_SHORTCUT:
if ((Fl::event_key() == 'i') || (Fl::event_key() == 'o') ||
(Fl::event_key() == 'f')) {
return(Fl_Button::handle(e));
}
else {
return(0);
}
break;
case FL_PUSH:
return(Fl_Button::handle(e));
break;
case FL_RELEASE:
return(Fl_Button::handle(e));
break;
default:
return(0);
break;
}
}
};
Draw *draw;
vector <Draw*> draw_Vector;
static void zoomincb(Fl_Widget *wi, void *userdata) {
int x,y,w,h,dx,dy;
const int zoom=2;
if ((diex/divider*gzoom*zoom < MAXSIZE) && (diey/divider*gzoom*zoom <
MAXSIZE)) {
cout << "diex: " << diex/divider*gzoom*zoom << endl;
gzoom = gzoom * zoom;
cout << "diex: " << diex/divider*gzoom << endl;
for(unsigned int i=0;i < draw_Vector.size(); i++) {
draw = draw_Vector.at(i);
draw->x0(draw->x0()*zoom);
draw->y0(draw->y0()*zoom);
draw->w0(draw->w0()*zoom);
draw->h0(draw->h0()*zoom);
draw->resize(draw->x0()/divider,draw->y0()/divider,draw->w0()/divider,draw->h0()/divider);
}
w = scroll->w()/2;
h = scroll->h()/2;
x = scroll->xposition() + gzoom * w;
y = scroll->yposition() + gzoom * h;
dx = x - gzoom * w;
dy = y - gzoom * h;
x = scroll->xposition();
y = scroll->yposition();
scroll->position(x*zoom+dx+w,y*zoom+dy+h);
Fl::redraw();
}
Fl::wait(1);
}
static void zoomoutcb(Fl_Widget *wi, void *userdata) {
int x,y,w,h,dx,dy;
const int zoom=2;
if(gzoom>=zoom) {
for(unsigned int i=0;i < draw_Vector.size(); i++) {
draw = draw_Vector.at(i);
draw->x0(draw->x0()/zoom);
draw->y0(draw->y0()/zoom);
draw->w0(draw->w0()/zoom);
draw->h0(draw->h0()/zoom);
draw->resize(draw->x0()/divider,draw->y0()/divider,draw->w0()/divider,draw->h0()/divider);
}
gzoom = gzoom / zoom;
w = scroll->w()/2;
h = scroll->h()/2;
x = scroll->xposition() + gzoom * w;
y = scroll->yposition() + gzoom * h;
dx = x - gzoom * w;
dy = y - gzoom * h;
x = scroll->xposition();
y = scroll->yposition();
scroll->position(x/zoom+dx-w/zoom,y/zoom+dy-h/zoom);
Fl::redraw();
Fl::wait(1);
}
}
static void zoomfitcb(Fl_Widget *w, void *userdata) {
const int zoom=1;
scroll->hscrollbar.value(0,scroll->w(),0,scroll->w());
scroll->scrollbar.value(0,scroll->h(),0,scroll->h());
if((float)(scroll->w()/scroll->h())>(float)(diex/diey)) {
divider = diey/scroll->h();
}
else {
divider = diex/scroll->w();
}
for(unsigned int i=0;i < draw_Vector.size(); i++) {
draw = draw_Vector.at(i);
if(gzoom!=1) {
draw->x0(draw->x0()/gzoom);
draw->y0(draw->y0()/gzoom);
draw->w0(draw->w0()/gzoom);
draw->h0(draw->h0()/gzoom);
}
draw->resize(draw->x0()*zoom/divider,draw->y0()*zoom/divider,draw->w0()*zoom/divider,draw->h0()*zoom/divider);
}
gzoom = 1;
Fl::redraw();
Fl::wait(1);
}
int main() {
win = new Fl_Double_Window(800,800);
scroll = new Fl_Scroll(0,0,win->w(),win->h()-50);
int x=0,y=0,w=0,h=0;
for(x=0;x<diex;x=x+w) {
w = (1+(int) (10.0*rand()/(RAND_MAX+1.0))) * diex/6000; // 6000 ~ 1.2 Mio
components, 600 ~ 11000 components
for(y=0;y<diey;y=y+h) {
h = (1+(int) (10.0*rand()/(RAND_MAX+1.0))) * diey/6000; // 6000 ~ 1.2 Mio
components, 600 ~ 11000 components
draw_Vector.push_back(new Draw((x), (y), (w), (h)));
}
}
zoomfitcb(0,0);
cout << "Draw::components: " << Draw::components << endl;
scroll->end();
win->resizable(scroll);
Fl_Box box(0,win->h()-50,win->w(),50);
Button zoomin(0,win->h()-40,100,35,"Zoom &in");
zoomin.shortcut('i');
zoomin.callback(zoomincb);
Button zoomout(120,win->h()-40,100,35,"Zoom &out");
zoomout.shortcut('o');
zoomout.callback(zoomoutcb);
Button zoomfit(4*120,win->h()-40,100,35,"Zoom &Fit");
zoomfit.callback(zoomfitcb);
win->end();
win->show();
return(Fl::run());
}
--------------------------------------------------------------
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls s/call s/call name
21.65 62.32 62.32 189 0.33 0.48 Fl_Group::handle(int)
16.64 110.23 47.90 73631997 0.00 0.00 send(Fl_Widget*, int)
9.09 136.39 26.17 61561102 0.00 0.00 Fl::event_inside(Fl_Widget
const*)
7.41 157.74 21.34 80874063 0.00 0.00 Draw::handle(int)
4.93 171.94 14.20 6 2.37 10.83 zoomincb(Fl_Widget*, void*)
4.88 185.99 14.05 15692052 0.00 0.00
Fl_Group::draw_child(Fl_Widget&) const
2.69 193.74 7.75 80874063 0.00 0.00 Fl_Widget::handle(int)
2.69 201.48 7.74 7 1.11 1.27 Fl_Scroll::position(int,
int)
2.28 208.05 6.57 13 0.51 0.51 Fl_Scroll::draw()
> [..]
> > As the code goes:
> > a) Handle your selection of a square in a single handle() function in a
> > global container like a derived group that contains all squares and
> > always return 1. That will avoid fltk from having to dispatch 100000+
> > handle calls to each square. On a pick, that function should select and
> > set a redraw on the proper square directly.
> [..]
>
> I think (though I'm far from certain), that this could also
> be accomplished by returning 1 from your Draw::handle()
> in all cases, rather than using Fl_Widget::handle() for
> the default case.
>
>
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk