KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gr_glut.h
Go to the documentation of this file.
1 /* $Id: gr_glut.h 18327 2014-12-02 04:27:31Z ksimek $ */
2 /* =========================================================================== *
3  |
4  | Copyright (c) 1994-2010 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Kyle Simek
18  * =========================================================================== */
19 
20 #ifndef KJB_GR_CPP_GLUT_H
21 #define KJB_GR_CPP_GLUT_H
22 
23 #ifdef KJB_HAVE_GLUT
24 
25 #include <l_cpp/l_exception.h>
26 #include <string>
27 #include <vector>
28 
29 #ifdef KJB_HAVE_BST_THREAD
30 #include <boost/thread.hpp>
31 #endif
32 
33 
34 namespace boost {
35 
36 template <class R>
37 class function0;
38 
39 template <class R, class T1, class T2>
40 class function2;
41 
42 template <class R, class T1, class T2, class T3>
43 class function3;
44 
45 template <class R, class T1, class T2, class T3, class T4>
46 class function4;
47 
48 //class mutex;
49 } // namespace boost
58 namespace kjb
59 {
60 namespace opengl
61 {
62 
63 class Glut
64 {
65 // TODO:
66 // menu handling
67 // idle callback
68  public:
69  static unsigned int get_display_mode()
70  {
71  return display_mode_;
72  }
82  static void set_display_mode(unsigned int display_mode);
83 
84  static void set_init_window_size(int w, int h);
85 
86  static void set_init_window_position(int x, int y);
87 
88  static void init();
89 
90  static void init(int argc, char** argv);
91 
92  static bool is_initialized();
93 
94  static bool is_running();
95 
103  static void push_current_window();
104 
110  static void pop_current_window();
111 
112 
113  static void set_idle_callback(boost::function0<void> cb);
114 
115  static void add_timer_callback(size_t msec, boost::function0<void> cb);
116 
117  static void clear_idle_callback();
118 
128  static void enable_multithreading();
129 
130  static boost::thread::id thread_id();
131 
139  static void post_redisplay_t(int wnd_handle);
140 
141 
149  static void push_task_t(boost::function0<void> task);
150 
151 
152 #ifdef KJB_HAVE_BST_THREAD
153 
166  static boost::mutex* mutex;
167 #endif
168 
174  static void task_pump_t();
175 
179  static void task_pump();
180 
184 
185  static void disable_warnings();
186 
187  static bool warnings_enabled();
188 
194  static void test_initialized(const std::string& obj_name);
195 
196 private:
203  static void idle();
204 
205  static void timer(int i);
206 
214  static void idle_t();
215 private:
216 
217  static unsigned int display_mode_;
218  static bool initialized_;
219  static bool warnings_;
220 
221  static int init_h_;
222  static int init_w_;
223  static int init_x_;
224  static int init_y_;
225 
226  static boost::function0<void>* idle_callback_;
227  static std::vector<boost::function0<void> > timer_callbacks_;
228  static std::vector<size_t> timer_gaps_;
229 
230  static bool multithreading_enabled_;
231  static std::vector<bool> redisplay_flags_;
232  static std::vector<int> redisplay_wnds_;
233 
234  static std::vector<boost::function0<void> > tasks_;
235 
236 
237 
238  static int cur_wnd_stack_;
239 
240 #ifdef KJB_HAVE_BST_THREAD
241  static boost::thread::id* thread_id_;
242 #endif
243 
244 };
245 
246 
250 class Glut_window
251 {
252 // TODO:
253 // menu handling
254 // timers?
255 // stacking
256 // set_active
257 // visibility callback
258 // entry_callback
259 public:
263  Glut_window( const std::string& title);
264 
265  Glut_window(
266  int width,
267  int height,
268  const std::string& title = DEFAULT_WINDOW_TITLE
269  );
270 
271  Glut_window(
272  int width,
273  int height,
274  int pos_x,
275  int pos_y,
276  const std::string& title
277  );
278 
279  ~Glut_window();
280 
288  void set_size(int width, int height);
289 
291  int get_width() const;
292 
294  int get_height() const;
295 
303  void set_position(int x, int y);
304 
306  int get_handle() const;
307 
319  void set_current() const;
320 
333  void redisplay() const;
334 
335  /*
336  * A thread-safe version of redisplay().
337  *
338  * This makes no direct calls to glut or opengl, but enqueues
339  * a redisplay event with the Glut class, which is processed
340  * at the next idle event.
341  *
342  */
343  void redisplay_t() const;
344 
355  bool is_thread_safe() const;
356 
357 
358 
359  // ////////////////////
360  // CALLBACK SETTERS //
361  // ////////////////////
366  void set_display_callback(boost::function0<void> cb);
367 
368  const boost::function0<void>& get_display_callback() const;
369 
376  static void set_idle_callback(boost::function0<void> cb);
381  void set_reshape_callback(boost::function2<void, int, int> cb);
386  void set_keyboard_callback(boost::function3<void, unsigned char, int, int> cb);
391  void set_mouse_callback(boost::function4<void, int, int, int, int> cb);
396  void set_motion_callback(boost::function2<void, int, int> cb);
401  void set_passive_motion_callback(boost::function2<void, int, int> cb);
406  void set_special_callback(boost::function3<void, int, int, int> cb);
407 
408 
409 
410  // /////////////////////
411  // CALLBACK CLEARERS //
412  // /////////////////////
413 
414  /* Stop handling display events.
415  *
416  * @note this actually sets a no-op function to handle display
417  * events, as setting no display callback will cause glut to
418  * crash in most implementations.
419  * @warning Not thread safe
420  */
421  void clear_display_callback() ;
422 
427  static void clear_idle_callback();
428 
434  void clear_reshape_callback();
435 
440  void clear_keyboard_callback();
441 
447  void clear_mouse_callback();
448 
454  void clear_motion_callback();
455 
461  void clear_passive_motion_callback();
462 
468  void clear_special_callback();
469 
475  void clear_callbacks();
476 
477  void init();
478 
479 private:
480  // prevent copying
481  Glut_window(const Glut_window&){}
482  Glut_window& operator=(const Glut_window&) { return *this; }
483 
484 
485  // /////////////
486  // CALLBACKs //
487  // //////////////////////////////////////////////////////////////
488  // These are intentionally not virtual, to encourage users to
489  // pass their callback functions into this object using the
490  // set_<callback>() methods, and to discourage
491  // subclassing Glut_window, which should be unnecessary.
492  //
493  // If the no-subclassing rule is too much trouble, we can add virtual
494  // to these later.
495  //
496  // Also, these are not callable directly, because only glut should
497  // be calling these, by way of the glut_window_<callback>_callback()
498  // global functions, which are friends.
499  //
500  // You can change these to be public if needed, but think twice
501  // before changing them and ask if you really need to call these
502  // directly, or if you should redesign your code to keep event
503  // handlers separate from functions that do work.
504  // //////////////////////////////////////////////////////////////
505 
507  void display_callback() const;
508 
513  void reshape_callback(int w, int h);
514 
515  void keyboard_callback(unsigned char k, int x, int y) const;
516 
517  void mouse_callback(int button, int state, int x, int y) const;
518 
519  void motion_callback(int x, int y) const;
520 
521  void passive_motion_callback(int x, int y) const;
522 
523  void special_callback(int k, int x, int y) const;
524 
525  // ////////////////////////////////
526  // CALLBACK FRIEND DECLARATIONS //
527  // ////////////////////////////////
528  friend void glut_window_display_callback();
529  friend void glut_window_reshape_callback(int, int);
530  friend void glut_window_keyboard_callback(unsigned char, int, int);
531  friend void glut_window_mouse_callback(int, int, int, int);
532  friend void glut_window_motion_callback(int, int);
533  friend void glut_window_passive_motion_callback(int, int);
534  friend void glut_window_special_callback(int, int, int);
535 
536 private:
537  std::string title_;
538  mutable int width_;
539  mutable int height_;
540  int x_;
541  int y_;
542  int handle_;
543 
544 
545  boost::function0<void>* display_callback_;
546  boost::function2<void, int, int>* reshape_callback_;
547  boost::function3<void, unsigned char, int, int>* keyboard_callback_;
548  boost::function4<void, int, int, int, int>* mouse_callback_;
549  boost::function2<void, int, int>* motion_callback_;
550  boost::function2<void, int, int>* passive_motion_callback_;
551  boost::function3<void, int, int, int>* special_callback_;
552 
553  static const char* DEFAULT_WINDOW_TITLE;
554 };
555 
556 
557 } // opengl
558 } // kjb
559 #endif /* KJB_HAVE_GLUT */
560 
561 #endif
for k
Definition: APPgetLargeConnectedEdges.m:61
height
Definition: APPgetLargeConnectedEdges.m:33
x
Definition: APPgetLargeConnectedEdges.m:100
end get the endpoints of the long edges and an image of the long edges for id
Definition: APPgetLargeConnectedEdges.m:96
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
Support for error handling exception classes in libKJB.