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.

256 lines
6.5KB

  1. //
  2. // "$Id: fullscreen.cxx 8033 2010-12-15 12:11:16Z AlbrechtS $"
  3. //
  4. // Fullscreen test program for the Fast Light Tool Kit (FLTK).
  5. //
  6. // This demo shows how to do many of the window manipulations that
  7. // are popular on SGI programs, even though X does not really like
  8. // them. You can toggle the border on/off, change the visual to
  9. // switch between single/double buffer, and make the window take
  10. // over the screen.
  11. //
  12. // Normally the program makes a single window with a child GL window.
  13. // This simulates a program where the 3D display is surrounded by
  14. // control knobs. Running the program with an argument will
  15. // make it make a seperate GL window from the controls window. This
  16. // simulates a (older?) style program where the graphics display is
  17. // a different window than the controls.
  18. //
  19. // This program reports how many times it redraws the window to
  20. // stdout, so you can see how much time it is wasting. It appears
  21. // to be impossible to prevent X from sending redundant resize
  22. // events, so there are extra redraws. But the way I have the
  23. // code arranged here seems to be keeping that to a minimu.
  24. //
  25. // Apparently unavoidable bugs:
  26. //
  27. // Turning the border on causes an unnecessary redraw.
  28. //
  29. // Turning off full screen when the border is on causes an unnecessary
  30. // resize and redraw when the program turns the border on.
  31. //
  32. // If it is a seperate window, turning double buffering on and off
  33. // will cause the window to raise, deiconize, and possibly move. You
  34. // can avoid this by making the Fl_Gl_Window a child of a normal
  35. // window.
  36. //
  37. // Copyright 1998-2010 by Bill Spitzak and others.
  38. //
  39. // This library is free software; you can redistribute it and/or
  40. // modify it under the terms of the GNU Library General Public
  41. // License as published by the Free Software Foundation; either
  42. // version 2 of the License, or (at your option) any later version.
  43. //
  44. // This library is distributed in the hope that it will be useful,
  45. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  46. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  47. // Library General Public License for more details.
  48. //
  49. // You should have received a copy of the GNU Library General Public
  50. // License along with this library; if not, write to the Free Software
  51. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  52. // USA.
  53. //
  54. // Please report all bugs and problems on the following page:
  55. //
  56. // http://www.fltk.org/str.php
  57. //
  58. #include <config.h>
  59. #include <FL/Fl.H>
  60. #include <FL/Fl_Single_Window.H>
  61. #include <FL/Fl_Hor_Slider.H>
  62. #include <FL/Fl_Toggle_Light_Button.H>
  63. #include <FL/math.h>
  64. #include <stdio.h>
  65. #if HAVE_GL
  66. #include <FL/gl.h>
  67. #include <FL/Fl_Gl_Window.H>
  68. class shape_window : public Fl_Gl_Window {
  69. void draw();
  70. public:
  71. int sides;
  72. shape_window(int x,int y,int w,int h,const char *l=0);
  73. };
  74. shape_window::shape_window(int x,int y,int w,int h,const char *l) :
  75. Fl_Gl_Window(x,y,w,h,l) {
  76. sides = 3;
  77. }
  78. void shape_window::draw() {
  79. printf("drawing size %d %d\n",w(),h());
  80. if (!valid()) {
  81. valid(1);
  82. // printf("init\n");
  83. glLoadIdentity();
  84. glViewport(0,0,w(),h());
  85. }
  86. glClear(GL_COLOR_BUFFER_BIT);
  87. glColor3f(.5,.6,.7);
  88. glBegin(GL_POLYGON);
  89. for (int j = 0; j < sides; j ++) {
  90. double ang = j*2*M_PI/sides;
  91. glVertex3f(cos(ang),sin(ang),0);
  92. }
  93. glEnd();
  94. }
  95. #else
  96. #include <FL/fl_draw.H>
  97. class shape_window : public Fl_Window {
  98. void draw();
  99. public:
  100. int sides;
  101. shape_window(int x,int y,int w,int h,const char *l=0);
  102. };
  103. shape_window::shape_window(int x,int y,int w,int h,const char *l) :
  104. Fl_Window(x,y,w,h,l) {
  105. sides = 3;
  106. }
  107. void shape_window::draw() {
  108. fl_color(0);
  109. fl_rectf(0,0,w(),h());
  110. fl_font(0,20);
  111. fl_color(7);
  112. fl_draw("This requires GL",0,0,w(),h(),FL_ALIGN_CENTER);
  113. }
  114. #endif
  115. void sides_cb(Fl_Widget *o, void *p) {
  116. shape_window *sw = (shape_window *)p;
  117. sw->sides = int(((Fl_Slider *)o)->value());
  118. sw->redraw();
  119. }
  120. #if HAVE_GL
  121. void double_cb(Fl_Widget *o, void *p) {
  122. shape_window *sw = (shape_window *)p;
  123. int d = ((Fl_Button *)o)->value();
  124. sw->mode(d ? Fl_Mode(FL_DOUBLE|FL_RGB) : FL_RGB);
  125. }
  126. #else
  127. void double_cb(Fl_Widget *, void *) {}
  128. #endif
  129. void border_cb(Fl_Widget *o, void *p) {
  130. Fl_Window *w = (Fl_Window *)p;
  131. int d = ((Fl_Button *)o)->value();
  132. w->border(d);
  133. #if defined(WIN32) || defined(__APPLE__)
  134. int wx = w->x(), wy = w->y();
  135. w->hide(); w->show();
  136. w->position(wx, wy);
  137. #endif
  138. }
  139. int px,py,pw,ph;
  140. Fl_Button *border_button;
  141. void fullscreen_cb(Fl_Widget *o, void *p) {
  142. Fl_Window *w = (Fl_Window *)p;
  143. int d = ((Fl_Button *)o)->value();
  144. if (d) {
  145. px = w->x();
  146. py = w->y();
  147. pw = w->w();
  148. ph = w->h();
  149. #ifndef WIN32//necessary because fullscreen removes border
  150. border_button->value(0);
  151. border_button->do_callback();
  152. #endif
  153. w->fullscreen();
  154. } else {
  155. w->fullscreen_off(px,py,pw,ph);
  156. }
  157. }
  158. #include <stdlib.h>
  159. void exit_cb(Fl_Widget *, void *) {
  160. exit(0);
  161. }
  162. #define NUMB 5
  163. int twowindow = 0;
  164. int initfull = 0;
  165. int arg(int, char **argv, int &i) {
  166. if (argv[i][1] == '2') {twowindow = 1; i++; return 1;}
  167. if (argv[i][1] == 'f') {initfull = 1; i++; return 1;}
  168. return 0;
  169. }
  170. int main(int argc, char **argv) {
  171. int i=0;
  172. if (Fl::args(argc,argv,i,arg) < argc)
  173. Fl::fatal("Options are:\n -2 = 2 windows\n -f = startup fullscreen\n%s",Fl::help);
  174. Fl_Single_Window window(300,300+30*NUMB); window.end();
  175. shape_window sw(10,10,window.w()-20,window.h()-30*NUMB-20);
  176. #if HAVE_GL
  177. sw.mode(FL_RGB);
  178. #endif
  179. Fl_Window *w;
  180. if (twowindow) { // make it's own window
  181. sw.resizable(&sw);
  182. w = &sw;
  183. window.set_modal(); // makes controls stay on top when fullscreen pushed
  184. argc--;
  185. sw.show();
  186. } else { // otherwise make a subwindow
  187. window.add(sw);
  188. window.resizable(&sw);
  189. w = &window;
  190. }
  191. window.begin();
  192. int y = window.h()-30*NUMB-5;
  193. Fl_Hor_Slider slider(50,y,window.w()-60,30,"Sides:");
  194. slider.align(FL_ALIGN_LEFT);
  195. slider.callback(sides_cb,&sw);
  196. slider.value(sw.sides);
  197. slider.step(1);
  198. slider.bounds(3,40);
  199. y+=30;
  200. Fl_Toggle_Light_Button b1(50,y,window.w()-60,30,"Double Buffered");
  201. b1.callback(double_cb,&sw);
  202. y+=30;
  203. Fl_Toggle_Light_Button b2(50,y,window.w()-60,30,"Border");
  204. b2.callback(border_cb,w);
  205. b2.set();
  206. border_button = &b2;
  207. y+=30;
  208. Fl_Toggle_Light_Button b3(50,y,window.w()-60,30,"FullScreen");
  209. b3.callback(fullscreen_cb,w);
  210. y+=30;
  211. Fl_Button eb(50,y,window.w()-60,30,"Exit");
  212. eb.callback(exit_cb);
  213. y+=30;
  214. if (initfull) {b3.set(); b3.do_callback();}
  215. window.end();
  216. window.show(argc,argv);
  217. return Fl::run();
  218. }
  219. //
  220. // End of "$Id: fullscreen.cxx 8033 2010-12-15 12:11:16Z AlbrechtS $".
  221. //