glh_glut_replay.h
Go to the documentation of this file.
00001 /*
00002     glh - is a platform-indepenedent C++ OpenGL helper library 
00003 
00004 
00005     Copyright (c) 2000 Cass Everitt
00006         Copyright (c) 2000 NVIDIA Corporation
00007     All rights reserved.
00008 
00009     Redistribution and use in source and binary forms, with or
00010         without modification, are permitted provided that the following
00011         conditions are met:
00012 
00013      * Redistributions of source code must retain the above
00014            copyright notice, this list of conditions and the following
00015            disclaimer.
00016 
00017      * Redistributions in binary form must reproduce the above
00018            copyright notice, this list of conditions and the following
00019            disclaimer in the documentation and/or other materials
00020            provided with the distribution.
00021 
00022      * The names of contributors to this software may not be used
00023            to endorse or promote products derived from this software
00024            without specific prior written permission. 
00025 
00026        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027            ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028            LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00029            FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00030            REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00031            INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00032            BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00033            LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00034            CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00035            LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00036            ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
00037            POSSIBILITY OF SUCH DAMAGE. 
00038 
00039 
00040     Cass Everitt - cass@r3.nu
00041 */
00042 
00043 #ifndef GLH_GLUT_REPLAY_H
00044 #define GLH_GLUT_REPLAY_H
00045 
00046 // a facility for recording and playing back glut events
00047 
00048 #include <list>
00049 #include <glh/glh_glut.h>
00050 
00051 namespace glh
00052 {
00053 
00054 
00055   struct glut_event
00056   {
00057           enum event_type
00058           {
00059                   DISPLAY, IDLE, KEYBOARD, MENU_STATUS, MOTION, MOUSE,
00060                   PASSIVE_MOTION, RESHAPE, SPECIAL, TIMER, VISIBILITY
00061           };
00062           glut_event(event_type t) : type(t) {}
00063           virtual ~glut_event() {}
00064           virtual void dispatch() = 0;
00065           const event_type type;
00066   };
00067 
00068   struct glut_display_event : public glut_event
00069   {
00070           glut_display_event() : glut_event(DISPLAY) {}
00071           virtual void dispatch() { glut_display_function(); }
00072   };
00073 
00074   struct glut_idle_event : public glut_event
00075   {
00076           glut_idle_event() : glut_event(IDLE) {}
00077           virtual void dispatch() { glut_idle_function(); }
00078   };
00079 
00080   struct glut_keyboard_event : public glut_event
00081   {
00082           glut_keyboard_event(unsigned char key, int xpos, int ypos)
00083                   :     glut_event(KEYBOARD), k(key), x(xpos), y(ypos)
00084           {}
00085           virtual void dispatch() { glut_keyboard_function(k,x,y); }
00086           unsigned char k;
00087           int x, y;
00088   };
00089 
00090   struct glut_menu_status_event : public glut_event
00091   {
00092           glut_menu_status_event(int status, int xpos, int ypos)
00093                   :     glut_event(MENU_STATUS), s(status), x(xpos), y(ypos)
00094           {}
00095           virtual void dispatch() { glut_menu_status_function(s,x,y); }
00096           int s, x, y;
00097   };
00098   
00099   struct glut_motion_event : public glut_event
00100   {
00101           glut_motion_event(int xpos, int ypos)
00102                   :     glut_event(MOTION), x(xpos), y(ypos)
00103           {}
00104           virtual void dispatch() { glut_motion_function(x,y); }
00105           int x, y;
00106   };
00107 
00108   struct glut_mouse_event : public glut_event
00109   {
00110           glut_mouse_event(int button, int state, int xpos, int ypos)
00111                   :     glut_event(MOUSE), b(button), s(state), x(xpos), y(ypos)
00112           {}
00113           virtual void dispatch() { glut_mouse_function(b,s,x,y); }
00114           int b, s, x, y;
00115   };
00116 
00117   struct glut_passive_motion_event : public glut_event
00118   {
00119           glut_passive_motion_event(int xpos, int ypos)
00120                   :     glut_event(PASSIVE_MOTION), x(xpos), y(ypos)
00121           {}
00122           virtual void dispatch() { glut_passive_motion_function(x,y); }
00123           int x, y;
00124   };
00125 
00126   struct glut_reshape_event : public glut_event
00127   {
00128           glut_reshape_event(int width, int height)
00129                   :     glut_event(RESHAPE), w(width), h(height)
00130           {}
00131           virtual void dispatch() { glut_reshape_function(w,h); }
00132           int w, h;
00133   };
00134 
00135   struct glut_special_event : public glut_event
00136   {
00137           glut_special_event(int key, int xpos, int ypos)
00138                   :     glut_event(SPECIAL), k(key), x(xpos), y(ypos) 
00139           {}
00140           virtual void dispatch() { glut_special_function(k,x,y); }
00141           int k, x, y;
00142   };
00143 
00144   struct glut_timer_event : public glut_event
00145   {
00146           glut_timer_event(int value)
00147                   :     glut_event(TIMER), v(value) 
00148           {}
00149           virtual void dispatch() { glut_timer_function(v); }
00150           int v;
00151   };
00152 
00153   struct glut_visibility_event : public glut_event
00154   {
00155           glut_visibility_event(int visibility)
00156                   :     glut_event(VISIBILITY), v(visibility)
00157           {}
00158           virtual void dispatch() { glut_visibility_function(v); }
00159           int v;
00160   };
00161 
00162 
00163 
00164   struct glut_replay : public glut_interactor
00165   {
00166         enum recorder_mode
00167         {
00168                 RECORD, PLAY, STOP
00169         };
00170 
00171         glut_replay()
00172         {
00173                 it = event_list.end();
00174                 mode = STOP;
00175                 paused = false;
00176         }
00177 
00178         virtual ~glut_replay()
00179         { erase(); }
00180 
00181         virtual void display()
00182         {
00183                 if(enabled && RECORD == mode && ! paused)
00184                         event_list.push_back(new glut_display_event());
00185         }
00186         virtual void idle()
00187         {
00188                 if(enabled && RECORD == mode && ! paused)
00189                         event_list.push_back(new glut_idle_event());
00190         }
00191         virtual void keyboard(unsigned char key, int x, int y)
00192         {
00193                 if(enabled && RECORD == mode && ! paused)
00194                         event_list.push_back(new glut_keyboard_event(key,x,y));
00195         }
00196         virtual void menu_status(int status, int x, int y) 
00197         {
00198                 if(enabled && RECORD == mode && ! paused)
00199                         event_list.push_back(new glut_menu_status_event(status,x,y));
00200         }
00201         virtual void motion(int x, int y) 
00202         {
00203                 if(enabled && RECORD == mode && ! paused)
00204                         event_list.push_back(new glut_motion_event(x,y));
00205         }
00206         virtual void mouse(int button, int state, int x, int y) 
00207         {
00208                 if(enabled && RECORD == mode && ! paused)
00209                         event_list.push_back(new glut_mouse_event(button,state,x,y));
00210         }
00211         virtual void passive_motion(int x, int y) 
00212         {
00213                 if(enabled && RECORD == mode && ! paused)
00214                         event_list.push_back(new glut_passive_motion_event(x,y));
00215         }
00216         virtual void reshape(int w, int h) 
00217         {
00218                 if(enabled && RECORD == mode && ! paused)
00219                         event_list.push_back(new glut_reshape_event(w,h));
00220         }
00221         virtual void special(int  key, int x, int y) 
00222         {
00223                 if(enabled)
00224                 {
00225                         if     (key == GLUT_KEY_F6)
00226                         { mode = RECORD; glut_event_processed(); erase(); }
00227                         else if(key == GLUT_KEY_F7)
00228                         { mode = PLAY; glut_event_processed(); glutPostRedisplay(); }
00229                         else if(key == GLUT_KEY_F8)
00230                         { mode = STOP; glut_event_processed(); }
00231                         else if(key == GLUT_KEY_F9)
00232                         { paused = !paused; glut_event_processed(); glutPostRedisplay(); }
00233                         else if(RECORD == mode)
00234                         { event_list.push_back(new glut_special_event(key,x,y)); }
00235                 }
00236         }
00237         virtual void timer(int value) 
00238         {
00239                 if(enabled && RECORD == mode && ! paused)
00240                         event_list.push_back(new glut_timer_event(value));
00241         }
00242         virtual void visibility(int v) 
00243         {
00244                 if(enabled && RECORD == mode && ! paused)
00245                         event_list.push_back(new glut_visibility_event(v));
00246         }
00247 
00248     
00249         // other methods
00250 
00251         bool playing() { return mode == PLAY; }
00252 
00253         void dispatch_accumulated_events()
00254         {
00255                 if(mode == PLAY && ! paused)
00256                 {
00257                         while(it != event_list.end() && (*it)->type != glut_event::DISPLAY)
00258                         {
00259                                 (*it)->dispatch();
00260                                 it++;
00261                         }
00262                         if(it == event_list.end())
00263                         {
00264                                 mode = STOP;
00265                                 it = event_list.begin();
00266                         }
00267                         else if ((*it)->type == glut_event::DISPLAY)
00268                         {
00269                                 it++;
00270                         }
00271                 }
00272         }
00273 
00274         void erase()
00275         { 
00276                 while(event_list.begin() != event_list.end())
00277                 {
00278                         glut_event * e = event_list.back();
00279                         event_list.pop_back();
00280                         delete e;
00281                 }
00282         }
00283 
00284         std::list<glut_event *> event_list;
00285         std::list<glut_event *>::iterator it;
00286         recorder_mode mode;
00287         bool paused;
00288   };
00289 
00290 }
00291 
00292 #endif


nao_openni
Author(s): Bener SUAY
autogenerated on Mon Jan 6 2014 11:27:50