MachineIntelligenceCore:Visualization
WindowManager.cpp
Go to the documentation of this file.
1 
25 
26 #include <logger/Log.hpp>
27 
28 #include <exception>
29 
30 namespace mic {
31  namespace opengl {
32  namespace visualization {
33 
34 
35  // Init application instance - as NULL.
36  boost::atomic<WindowManager*> WindowManager::instance_(NULL);
37 
38 
39  // Initilize singleton instantiation mutex.
41 
43  // Try to load the instance - first check.
44  WindowManager* tmp = instance_.load(boost::memory_order_consume);
45  // If instance does not exist.
46  if (!tmp) {
47  // Enter critical section.
48  boost::mutex::scoped_lock guard(instantiation_mutex);
49  // Try to load the instance - second check.
50  tmp = instance_.load(boost::memory_order_consume);
51  // If still does not exist - create new instance.
52  if (!tmp) {
53  tmp = new WindowManager;
54  instance_.store(tmp, boost::memory_order_release);
55  }//: if
56  // Exit critical section.
57  }//: if
58  // Return instance.
59  return tmp;
60  }
61 
63  LOG(LTRACE) << "WindowManager::registerWindow";
64  if (window_ != NULL) {
65  LOG(LDEBUG) << "Registering window " << window_->getId();
66  window_registry.insert(id_win_pair_t(window_->getId(), window_));
67  }//: if
68  }
69 
71  LOG(LTRACE) << "WindowManager::findWindow";
72  // Find window with given id.
73  id_win_it_t it = window_registry.find(id_);
74  if (it != window_registry.end())
75  return it->second;
76  else {
77  LOG(LWARNING) << "Window " << id_ << " not found";
78  return NULL;
79  }//: else
80  }
81 
82  void WindowManager::displayHandler(void) {
83  LOG(LTRACE) << "Display handler of " << glutGetWindow() << " window";
84 
85  // If opengl "finished" - throw exception to break the GLUT loop!
86  if (APP_STATE->Quit()) {
87  //LOG(LERROR) << "ESC pressed - throwing an exception!";
88  throw std::exception();
89  }
90 
91  //printf("Display handler of %d window!\n", glutGetWindow());
92  Window* w = VGL_MANAGER->findWindow(glutGetWindow());
93  if (w != NULL) {
94  w->displayHandler();
95  }//: if
96  }
97 
98  void WindowManager::mouseHandler(int button, int state, int x, int y) {
99  LOG(LTRACE) << "Mouse handler of " << glutGetWindow() << " window";
100  Window* w = VGL_MANAGER->findWindow(glutGetWindow());
101  if (w != NULL) {
102  w->mouseHandler(button, state, x, y);
103  }//: if
104  }
105 
106  void WindowManager::reshapeHandler(int width_, int height_) {
107  LOG(LTRACE) << "Reshape handler of " << glutGetWindow() << " window";
108  Window* w = VGL_MANAGER->findWindow(glutGetWindow());
109  if (w != NULL) {
110  w->reshapeHandler(width_, height_);
111  }//: if
112  }
113 
114  void WindowManager::keyboardHandler(unsigned char key, int x, int y) {
115  LOG(LTRACE) << "Keyboard handler of " << glutGetWindow() << " window";
116  Window* w = VGL_MANAGER->findWindow(glutGetWindow());
117  if (w != NULL) {
118  w->keyboardHandler(key);
119  }//: if
120  }
121 
122  void WindowManager::idle(void) {
123  LOG(LTRACE) << "WindowManager::idle";
125 
126  for (id_win_it_t it = wm->window_registry.begin(); it != wm->window_registry.end(); it++) {
127  // Activate window.
128  glutSetWindow(it->first);
129  // Update its content.
130  glutPostRedisplay();
131  }//: end
132 
133  }
134 
135  void WindowManager::terminateWindows(void) {
136  LOG(LTRACE) << "WindowManager::terminateWindows";
137  /*WindowManager* wm = VGL_MANAGER;
138 
139  for (id_win_it_t it = wm->window_registry.begin(); it != wm->window_registry.end(); it++) {
140  LOG(LSTATUS) << "Window " << it->first << " being destroyed...";
141  // Activate window.
142  glutSetWindow(it->first);
143  // Destroy window.
144  glutDestroyWindow(it->first);
145  }//: end
146 
147  LOG(LERROR) << "WindowManager::terminateWindows - done destroying windows";*/
148 
149  //boost::mutex::scoped_lock unlock(APP_STATE->dataSynchronizationMutex());
150  }
151 
152  WindowManager::WindowManager() {
153  //exit_signal = false;
154  }
155 
156  WindowManager::~WindowManager() {
157  // TODO Auto-generated destructor stub
158  }
159 
160  void WindowManager::initializeGLUT(int argc, char *argv[]) {
161  LOG(LTRACE) << "WindowManager::initializeGLUT";
162  // Initialize GLUT.
163  glutInit(&argc, argv);
164 
165  // Attach the idle handler function.
166  glutIdleFunc(VGL_MANAGER->idle);
167 
168  }
169 
170  void WindowManager::startVisualizationLoop() {
171  LOG(LTRACE) << "WindowManager::startVisualizationLoop";
172  // Change application state.
173  APP_STATE->startUsingOpenGL();
174  try
175  {
176  // Run main OpenGL loop (sadly, it must be executed in the same i.e. MAIN program thread).
177  LOG(LTRACE) << "glutMainLoop()";
178  glutMainLoop();
179  }
180  catch(...)
181  {
182  LOG(LTRACE) << "Exiting glutMainLoop";
183  }
184  // Change application state.
185  APP_STATE->stopUsingOpenGL();
186  }
187 
188  } /* namespace visualization */
189  } /* namespace opengl */
190 } /* namespace mic */
std::map< unsigned int, mic::opengl::visualization::Window * >::iterator id_win_it_t
Type used in iterating/searching for windows in registry.
std::pair< unsigned int, mic::opengl::visualization::Window * > id_win_pair_t
Type used in adding windows to registry.
void registerWindow(mic::opengl::visualization::Window *window_)
std::map< unsigned int, mic::opengl::visualization::Window * > window_registry
unsigned int getId() const
Definition: Window.cpp:77
#define VGL_MANAGER
Macro returning OpenGL window manager instance.
virtual void mouseHandler(int button, int state, int x, int y)
Definition: Window.hpp:92
Contains declaration of parent class of all OpenGL-based windows.
mic::opengl::visualization::Window * findWindow(unsigned int id_)
virtual void reshapeHandler(int width_, int height_)
Definition: Window.cpp:85
static boost::atomic< WindowManager * > instance_
Class responsible for management of all OpenGL windows - defined in the form of a singleton...
Parent class of all OpenGL-based windows (abstract).
Definition: Window.hpp:51
virtual void displayHandler(void)=0
Declaration of WindowManager class along with a bunch of helpful types and macros.