1 | // |
---|
2 | // "$Id: doublebuffer.cxx 7903 2010-11-28 21:06:39Z matt $" |
---|
3 | // |
---|
4 | // Double-buffering test program for the Fast Light Tool Kit (FLTK). |
---|
5 | // |
---|
6 | // This demo shows how double buffering helps, by drawing the |
---|
7 | // window in a particularily bad way. |
---|
8 | // |
---|
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. |
---|
12 | // |
---|
13 | // This demo should work for both the GL and X versions of Fl, |
---|
14 | // even though the double buffering mechanism is totally different. |
---|
15 | // |
---|
16 | // Copyright 1998-2010 by Bill Spitzak and others. |
---|
17 | // |
---|
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. |
---|
22 | // |
---|
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. |
---|
27 | // |
---|
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 |
---|
31 | // USA. |
---|
32 | // |
---|
33 | // Please report all bugs and problems on the following page: |
---|
34 | // |
---|
35 | // http://www.fltk.org/str.php |
---|
36 | // |
---|
37 | |
---|
38 | #include <FL/Fl.H> |
---|
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> |
---|
44 | #include <stdlib.h> |
---|
45 | #include <FL/math.h> |
---|
46 | #include <stdio.h> |
---|
47 | |
---|
48 | // this purposely draws each line 10 times to be slow: |
---|
49 | void star(int w, int h, int n) { |
---|
50 | fl_push_matrix(); |
---|
51 | fl_translate(w/2, h/2); |
---|
52 | fl_scale(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++)*/ { |
---|
55 | fl_begin_line(); |
---|
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)); |
---|
58 | fl_end_line(); |
---|
59 | } |
---|
60 | } |
---|
61 | fl_pop_matrix(); |
---|
62 | } |
---|
63 | |
---|
64 | int sides[2] = {20,20}; |
---|
65 | |
---|
66 | void slider_cb(Fl_Widget* o, long v) { |
---|
67 | sides[v] = int(((Fl_Slider*)o)->value()); |
---|
68 | o->parent()->redraw(); |
---|
69 | } |
---|
70 | |
---|
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); |
---|
75 | // } |
---|
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); |
---|
80 | } |
---|
81 | |
---|
82 | class single_blink_window : public Fl_Single_Window { |
---|
83 | void draw() {bad_draw(w(),h(),0); draw_child(*child(0));} |
---|
84 | public: |
---|
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);} |
---|
87 | }; |
---|
88 | |
---|
89 | class double_blink_window : public Fl_Double_Window { |
---|
90 | void draw() {bad_draw(w(),h(),1); draw_child(*child(0));} |
---|
91 | public: |
---|
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);} |
---|
94 | }; |
---|
95 | |
---|
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); |
---|
103 | slider0.range(2,30); |
---|
104 | slider0.step(1); |
---|
105 | slider0.value(sides[0]); |
---|
106 | slider0.callback(slider_cb, 0); |
---|
107 | w1.end(); |
---|
108 | w01.end(); |
---|
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); |
---|
113 | slider1.range(2,30); |
---|
114 | slider1.step(1); |
---|
115 | slider1.value(sides[0]); |
---|
116 | slider1.callback(slider_cb, 1); |
---|
117 | w2.end(); |
---|
118 | w02.end(); |
---|
119 | w01.show(argc, argv); |
---|
120 | w1.show(); |
---|
121 | w02.show(); |
---|
122 | w2.show(); |
---|
123 | return Fl::run(); |
---|
124 | } |
---|
125 | |
---|
126 | // |
---|
127 | // End of "$Id: doublebuffer.cxx 7903 2010-11-28 21:06:39Z matt $". |
---|
128 | // |
---|