2 // "$Id: doublebuffer.cxx 7903 2010-11-28 21:06:39Z matt $"
4 // Double-buffering test program for the Fast Light Tool Kit (FLTK).
6 // This demo shows how double buffering helps, by drawing the
7 // window in a particularily bad way.
9 // The single-buffered window will blink as it updates. The
10 // double buffered one will not. It will take just as long
11 // (or longer) to update, but often it will appear to be faster.
13 // This demo should work for both the GL and X versions of Fl,
14 // even though the double buffering mechanism is totally different.
16 // Copyright 1998-2010 by Bill Spitzak and others.
18 // This library is free software; you can redistribute it and/or
19 // modify it under the terms of the GNU Library General Public
20 // License as published by the Free Software Foundation; either
21 // version 2 of the License, or (at your option) any later version.
23 // This library is distributed in the hope that it will be useful,
24 // but WITHOUT ANY WARRANTY; without even the implied warranty of
25 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 // Library General Public License for more details.
28 // You should have received a copy of the GNU Library General Public
29 // License along with this library; if not, write to the Free Software
30 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 // Please report all bugs and problems on the following page:
35 // http://www.fltk.org/str.php
39 #include <FL/Fl_Single_Window.H>
40 #include <FL/Fl_Double_Window.H>
41 #include <FL/Fl_Box.H>
42 #include <FL/fl_draw.H>
43 #include <FL/Fl_Hor_Slider.H>
48 // this purposely draws each line 10 times to be slow:
49 void star(int w
, int h
, int n
) {
51 fl_translate(w
/2, h
/2);
53 for (int i
= 0; i
< n
; i
++) {
54 for (int j
= i
+1; j
< n
; j
++)/* for (int k=0; k<10; k++)*/ {
56 fl_vertex(cos(2*M_PI
*i
/n
+.1), sin(2*M_PI
*i
/n
+.1));
57 fl_vertex(cos(2*M_PI
*j
/n
+.1), sin(2*M_PI
*j
/n
+.1));
64 int sides
[2] = {20,20};
66 void slider_cb(Fl_Widget
* o
, long v
) {
67 sides
[v
] = int(((Fl_Slider
*)o
)->value());
68 o
->parent()->redraw();
71 void bad_draw(int w
,int h
,int which
) {
72 // for (int i=0; i<10; i++) {
73 // fl_color(7); fl_rectf(0,0,w,h); fl_color(0); star(w,h);
74 // fl_color(0); fl_rectf(0,0,w,h); fl_color(7); star(w,h);
76 fl_color(FL_BLACK
); fl_rectf(0,0,w
,h
);
77 fl_color(FL_WHITE
); star(w
,h
,sides
[which
]);
78 // for (int x=0; x<sides[which]; x++) for (int y=0; y<sides[which]; y++)
79 //fl_draw_box(FL_UP_BOX, 10*x, 10*y, 25,25, FL_GRAY);
82 class single_blink_window
: public Fl_Single_Window
{
83 void draw() {bad_draw(w(),h(),0); draw_child(*child(0));}
85 single_blink_window(int x
, int y
,int w
,int h
,const char *l
)
86 : Fl_Single_Window(x
,y
,w
,h
,l
) {resizable(this);}
89 class double_blink_window
: public Fl_Double_Window
{
90 void draw() {bad_draw(w(),h(),1); draw_child(*child(0));}
92 double_blink_window(int x
, int y
, int w
,int h
,const char *l
)
93 : Fl_Double_Window(x
,y
,w
,h
,l
) {resizable(this);}
96 int main(int argc
, char **argv
) {
97 if (!Fl::visual(FL_DOUBLE
))
98 printf("Xdbe not supported, faking double buffer with pixmaps.\n");
99 Fl_Window
w01(420,420,"Fl_Single_Window"); w01
.box(FL_FLAT_BOX
);
100 single_blink_window
w1(10,10,400,400,"Fl_Single_Window");
101 w1
.box(FL_FLAT_BOX
); w1
.color(FL_BLACK
); //w1.position(100,200);
102 Fl_Hor_Slider
slider0(20,370,360,25);
105 slider0
.value(sides
[0]);
106 slider0
.callback(slider_cb
, 0);
109 Fl_Window
w02(420,420,"Fl_Double_Window"); w02
.box(FL_FLAT_BOX
);
110 double_blink_window
w2(10,10,400,400,"Fl_Double_Window");
111 w2
.box(FL_FLAT_BOX
); w2
.color(FL_BLACK
); //w2.position(600,200);
112 Fl_Hor_Slider
slider1(20,370,360,25);
115 slider1
.value(sides
[0]);
116 slider1
.callback(slider_cb
, 1);
119 w01
.show(argc
, argv
);
127 // End of "$Id: doublebuffer.cxx 7903 2010-11-28 21:06:39Z matt $".