You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.1KB

  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. #include <FL/Fl.H>
  38. #include <FL/Fl_Single_Window.H>
  39. #include <FL/Fl_Double_Window.H>
  40. #include <FL/Fl_Box.H>
  41. #include <FL/fl_draw.H>
  42. #include <FL/Fl_Hor_Slider.H>
  43. #include <stdlib.h>
  44. #include <FL/math.h>
  45. #include <stdio.h>
  46. // this purposely draws each line 10 times to be slow:
  47. void star(int w, int h, int n) {
  48. fl_push_matrix();
  49. fl_translate(w/2, h/2);
  50. fl_scale(w/2, h/2);
  51. for (int i = 0; i < n; i++) {
  52. for (int j = i+1; j < n; j++)/* for (int k=0; k<10; k++)*/ {
  53. fl_begin_line();
  54. fl_vertex(cos(2*M_PI*i/n+.1), sin(2*M_PI*i/n+.1));
  55. fl_vertex(cos(2*M_PI*j/n+.1), sin(2*M_PI*j/n+.1));
  56. fl_end_line();
  57. }
  58. }
  59. fl_pop_matrix();
  60. }
  61. int sides[2] = {20,20};
  62. void slider_cb(Fl_Widget* o, long v) {
  63. sides[v] = int(((Fl_Slider*)o)->value());
  64. o->parent()->redraw();
  65. }
  66. void bad_draw(int w,int h,int which) {
  67. // for (int i=0; i<10; i++) {
  68. // fl_color(7); fl_rectf(0,0,w,h); fl_color(0); star(w,h);
  69. // fl_color(0); fl_rectf(0,0,w,h); fl_color(7); star(w,h);
  70. // }
  71. fl_color(FL_BLACK); fl_rectf(0,0,w,h);
  72. fl_color(FL_WHITE); star(w,h,sides[which]);
  73. // for (int x=0; x<sides[which]; x++) for (int y=0; y<sides[which]; y++)
  74. //fl_draw_box(FL_UP_BOX, 10*x, 10*y, 25,25, FL_GRAY);
  75. }
  76. class single_blink_window : public Fl_Single_Window {
  77. void draw() {bad_draw(w(),h(),0); draw_child(*child(0));}
  78. public:
  79. single_blink_window(int x, int y,int w,int h,const char *l)
  80. : Fl_Single_Window(x,y,w,h,l) {resizable(this);}
  81. };
  82. class double_blink_window : public Fl_Double_Window {
  83. void draw() {bad_draw(w(),h(),1); draw_child(*child(0));}
  84. public:
  85. double_blink_window(int x, int y, int w,int h,const char *l)
  86. : Fl_Double_Window(x,y,w,h,l) {resizable(this);}
  87. };
  88. int main(int argc, char **argv) {
  89. if (!Fl::visual(FL_DOUBLE))
  90. printf("Xdbe not supported, faking double buffer with pixmaps.\n");
  91. Fl_Window w01(420,420,"Fl_Single_Window"); w01.box(FL_FLAT_BOX);
  92. single_blink_window w1(10,10,400,400,"Fl_Single_Window");
  93. w1.box(FL_FLAT_BOX); w1.color(FL_BLACK); //w1.position(100,200);
  94. Fl_Hor_Slider slider0(20,370,360,25);
  95. slider0.range(2,30);
  96. slider0.step(1);
  97. slider0.value(sides[0]);
  98. slider0.callback(slider_cb, 0);
  99. w1.end();
  100. w01.end();
  101. Fl_Window w02(420,420,"Fl_Double_Window"); w02.box(FL_FLAT_BOX);
  102. double_blink_window w2(10,10,400,400,"Fl_Double_Window");
  103. w2.box(FL_FLAT_BOX); w2.color(FL_BLACK); //w2.position(600,200);
  104. Fl_Hor_Slider slider1(20,370,360,25);
  105. slider1.range(2,30);
  106. slider1.step(1);
  107. slider1.value(sides[0]);
  108. slider1.callback(slider_cb, 1);
  109. w2.end();
  110. w02.end();
  111. w01.show(argc, argv);
  112. w1.show();
  113. w02.show();
  114. w2.show();
  115. return Fl::run();
  116. }
  117. //
  118. // End of "$Id: doublebuffer.cxx 7903 2010-11-28 21:06:39Z matt $".
  119. //