Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5388 in orxonox.OLD for trunk/src


Ignore:
Timestamp:
Oct 16, 2005, 2:05:26 AM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: implemented a t-Stack, for dynamic stacks, and integrated it into the Shell.

Location:
trunk/src/lib
Files:
7 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/event/event_def.h

    r5291 r5388  
    4040typedef enum elState
    4141  {
    42     ES_GAME,           //!< the state during the game plays
    43     ES_GAME_MENU,      //!< state when the menu is called during game
    44     ES_MENU,           //!< orxonox menu state
    45     ES_SHELL,          //!< if we are in shell Modus
     42    ES_NULL         = -1,
     43    ES_GAME         = 0,       //!< the state during the game plays
     44    ES_GAME_MENU    = 1,       //!< state when the menu is called during game
     45    ES_MENU         = 2,       //!< orxonox menu state
     46    ES_SHELL        = 3,       //!< if we are in shell Modus
    4647
    47     ES_ALL,            //!< you want to register events for all states
     48    ES_ALL          = 4,       //!< you want to register events for all states
    4849
    49     ES_NUMBER       //!< the number of states
     50    ES_NUMBER       = 5,       //!< the number of states
    5051  };
    5152
  • trunk/src/lib/event/event_handler.cc

    r5371 r5388  
    2626#include "class_list.h"
    2727
     28#include "t_stack.h"
     29
    2830using namespace std;
    2931
     
    4749  this->state = ES_GAME;
    4850  this->keyMapper = NULL;
     51  this->stateStack = NULL;
    4952}
    5053
     
    7275    }
    7376  }
     77  delete this->stateStack;
    7478  delete this->keyMapper;
    7579
     
    8791void EventHandler::init(IniParser* iniParser)
    8892{
    89   this->keyMapper = new KeyMapper();
    90   this->keyMapper->loadKeyBindings(iniParser);
    91 }
     93  if (this->keyMapper == NULL)
     94  {
     95    this->keyMapper = new KeyMapper();
     96    this->keyMapper->loadKeyBindings(iniParser);
     97  }
     98  if (this->stateStack == NULL)
     99    this->stateStack = new tStack<short>;
     100}
     101
     102/**
     103 * pushes the current State in the State-stack, and selects state
     104 * @param state the new State to set
     105 */
     106void EventHandler::pushState(elState state)
     107{
     108  if (likely(state != ES_NULL && state != ES_ALL && this->stateStack != NULL))
     109  {
     110    this->stateStack->push(this->state);
     111    this->setState(state);
     112  }
     113  else
     114  {
     115    PRINTF(2)("unable to push State\n");
     116  }
     117}
     118
     119/**
     120 * this removes the topmost stack-entry and select the underlying one
     121 * @returns the next stack-entry
     122 */
     123elState EventHandler::popState()
     124{
     125  if (unlikely(this->stateStack == NULL))
     126    return ES_NULL;
     127  elState state = (elState)this->stateStack->pop();
     128  if (state == ES_NULL)
     129  {
     130    PRINTF(2)("No more states availiable. (unable to pop state)\n");
     131    return ES_NULL;
     132  }
     133  else
     134  {
     135    this->setState(state);
     136    return state;
     137  }
     138}
     139
    92140
    93141/**
  • trunk/src/lib/event/event_handler.h

    r5309 r5388  
    1414// FORWARD DECLARATION
    1515class EventListener;
    16 template <class T> class tList;
     16template<class T> class tList;
     17template<class T> class tStack;
    1718class IniParser;
    1819
     
    3031  /** @returns the current state */
    3132  inline elState getState() const { return this->state; };
     33
     34  void pushState(elState state);
     35  elState popState();
    3236
    3337  void subscribe(EventListener* el, elState state, int eventType);
     
    5054  static EventHandler*       singletonRef;                    //!< the singleton reference
    5155
    52   EventListener*             listeners[ES_NUMBER][EV_NUMBER]; //!< a list of registered listeners
    53   elState                    state;                           //!< the state of the event handlder
    54   KeyMapper*                 keyMapper;                       //!< reference to the key mapper
     56  EventListener*             listeners[ES_NUMBER][EV_NUMBER]; //!< a list of registered listeners.
     57  elState                    state;                           //!< the state of the event handlder.
     58  tStack<short>*             stateStack;                      //!< a stack for the States we are in.
     59  KeyMapper*                 keyMapper;                       //!< reference to the key mapper.
     60
    5561};
    5662
  • trunk/src/lib/gui/gl_gui/glgui_handler.cc

    r5366 r5388  
    1717
    1818#include "glgui_handler.h"
     19#include "event_handler.h"
    1920
    2021using namespace std;
     
    2829   this->setName("GLGuiHandler");
    2930
    30    /* If you make a new class, what is most probably the case when you write this file
    31       don't forget to:
    32        1. Add the new file new_class.cc to the ./src/Makefile.am
    33        2. Add the class identifier to ./src/class_id.h eg. CL_NEW_CLASS
    34    !!!!!!!!!! IMPORTANT FOR SINGLETON !!!!!!!!!!!!!!!!
    35         3. SingleTon MUST be CL_NEW_CLASS = 0x00000fxx
    36 
    37       Advanced Topics:
    38       - if you want to let your object be managed via the ObjectManager make sure to read
    39         the object_manager.h header comments. You will use this most certanly only if you
    40         make many objects of your class, like a weapon bullet.
    41    */
    4231}
    4332
     
    5443  GLGuiHandler::singletonRef = NULL;
    5544}
     45
     46void GLGuiHandler::activate()
     47{
     48//  EventHandler::getInstance()->setState(ES_MENU);
     49
     50}
     51
     52void GLGuiHandler::deactivate()
     53{
     54
     55}
     56
     57
     58void GLGuiHandler::process(const Event &event)
     59{
     60
     61
     62
     63}
  • trunk/src/lib/gui/gl_gui/glgui_handler.h

    r5366 r5388  
    77#define _GLGUI_HANDLER_H
    88
    9 #include "base_object.h"
     9#include "event_listener.h"
    1010
    1111// FORWARD DEFINITION
    1212
    1313//! A default singleton class.
    14 class GLGuiHandler : public BaseObject {
     14class GLGuiHandler : public EventListener {
    1515
    1616 public:
     
    1919  inline static GLGuiHandler* getInstance(void) { if (!singletonRef) singletonRef = new GLGuiHandler();  return singletonRef; };
    2020
     21  void activate();
     22  void deactivate();
     23
     24
     25  virtual void process(const Event &event);
     26
    2127 private:
    2228  GLGuiHandler(void);
    2329  static GLGuiHandler* singletonRef;
     30
     31
     32  bool                 isActive;
    2433};
    2534
  • trunk/src/lib/shell/shell.cc

    r5383 r5388  
    125125  if (this->bActive == true)
    126126    PRINTF(3)("The shell is already active\n");
     127
     128  printf("ACT\n");
    127129  this->bActive = true;
    128130
    129   EventHandler::getInstance()->setState(ES_SHELL);
     131  EventHandler::getInstance()->pushState(ES_SHELL);
    130132  this->setRelCoorSoft2D(0, 0, 1, 5);
    131133
     
    149151  this->bActive = false;
    150152
    151   EventHandler::getInstance()->setState(ES_GAME);
     153  EventHandler::getInstance()->popState();
     154
    152155  this->setRelCoorSoft2D(0, -(int)this->shellHeight, 1, 5);
    153156
     
    445448    if (event.type == SDLK_BACKQUOTE)
    446449    {
    447       if (EventHandler::getInstance()->getState() == ES_GAME)
     450      if (this->bActive == false)
    448451        this->activate();
    449452      else
  • trunk/src/lib/util/array.h

    r5321 r5388  
    2424#define _ARRAY_H
    2525#include "debug.h"
     26
     27using namespace std;
    2628
    2729//! Array Class that handles dynamic-type arrays.
  • trunk/src/lib/util/t_stack.h

    r5382 r5388  
    1515
    1616/*!
    17   @file array.h
    18   @brief Contains the Array Class that handles arrays of classes.
     17  @file t_stack.h
     18  @brief Contains the tStack Class that handles stacks of classes.
    1919  this class creates a Array of a semi-Dynamic length.
    2020  beware, that after finalizing the array may not be resized again.
    2121*/
    2222
    23 #ifndef _ARRAY_H
    24 #define _ARRAY_H
    25 #include "debug.h"
     23#ifndef _T_STACK_H
     24#define _T_STACK_H
    2625
    27 //! Array Class that handles dynamic-type arrays.
    28 template<class T> class Array
     26using namespace std;
     27
     28//! Stack Class that handles dynamic-type Stacks.
     29template<class T>
     30     class tStack
    2931{
    3032  public:
    31     Array ();
    32     ~Array();
     33    tStack();
     34    ~tStack();
    3335
    34     void finalizeArray ();
    35     void addEntry (T entry);
    36     void addEntry(T entry0, T entry1, T entry2);
     36    void push(T entry);
     37    T pop();
     38    T getTop();
     39    /** @returns the Size of the Stack (0 if empty) */
     40    unsigned int getSize() { return this->entryCount; };
    3741
    38     /** @returns The array */
    39     inline T* getArray () const { return this->array; };
    40     inline const T getEntry(unsigned int number) const;
    41     /** * @returns The Count of entries in the Array*/
    42     inline unsigned int getCount()const { return this->entryCount; };
    43     inline int getIndex(T* entry) const;
    44     inline bool isFinalized() const { return this->finalized; }
    4542    void debug() const ;
    4643
    4744  private:
    48     //! One entry of the Array
    49     struct Entry
     45    //! One entry of the Stack
     46    struct tStackEntry
    5047    {
    51       T            value;          //!< The value of this Entry.
    52       Entry*       next;           //!< Pointer to the Next entry.
     48      T              value;          //!< The value of this Entry.
     49      tStackEntry*   next;           //!< Pointer to the Next entry.
    5350    };
    5451
    55     T*            array;           //!< The array that will be produced when finalizing the Array.
    56     unsigned int  entryCount;      //!< The count of Entries in this Array.
    57     bool          finalized;       //!< If this variable is set to true, the Array can not be changed anymore. true if finalized, false else (initially).
    58     Entry*        firstEntry;      //!< Pointer to the first Entry of this Array
    59     Entry*        currentEntry;    //!< Pointer to the current Entry of this Array. The one Entry we are working with.
     52
     53    unsigned int     entryCount;      //!< The count of Entries in this Array.
     54    tStackEntry*     topEntry;      //!< Pointer to the first Entry of this Array
    6055};
    6156
    62 
    6357/**
    64  *  creates a new Array
    65 */
     58 * creates and initializes a Stack
     59 */
    6660template<class T>
    67 Array<T>::Array ()
     61    tStack<T>::tStack()
    6862{
    69   PRINTF(5)("crating new Array\n");
    70   this->firstEntry = new Entry;
    71   this->firstEntry->next =NULL;
    72   this->currentEntry = this->firstEntry;
    73   this->finalized = false;
    74   this->entryCount = 0; //0 means one entry
    75 }
    76 
    77 template<class T>
    78     const T Array<T>::getEntry(unsigned int number) const
    79 {
    80   if (this->finalized && number < this->entryCount)
    81     return this->array[number];
     63  this->topEntry = NULL;
     64  this->entryCount = 0;
    8265}
    8366
    8467/**
    85  *  deletes an Array.
    86    It does this by first deleting all the array-entries, and then delete the array[] itself
    87 */
    88 template<class T>
    89 Array<T>::~Array()
    90 {
    91   PRINTF(5)("deleting array\n");
    92   if (!this->finalized)
    93   {
    94     Entry* walker = this->firstEntry;
    95     Entry* previous;
    96     while (walker)
    97     {
    98       previous = walker;
    99       walker = walker->next;
    100       delete previous;
    101     }
    102   }
    103   if (this->finalized)
    104     delete[] this->array;
    105 }
    106 
    107 /**
    108  *  finalizes an array.
    109    This Function creates the array, and makes it ready to be sent to the application.
    110 */
    111 template<class T>
    112 void Array<T>::finalizeArray ()
    113 {
    114   if (this->finalized)
    115     return;
    116   PRINTF(5)("Finalizing array. Length: %i\n", entryCount);
    117   if (!(this->array = new T [this->entryCount]))
    118     PRINTF(1)("could not allocate %i data Blocks\n", this->entryCount);
    119   Entry* walker = this->firstEntry;
    120   for (int i=0; i < this->entryCount; i++)
    121   {
    122     this->array[i] = walker->value;
    123     walker = walker->next;
    124   }
    125   walker = this->firstEntry;
    126   Entry* previous;
    127   while (walker)
    128   {
    129     previous = walker;
    130     walker = walker->next;
    131     delete previous;
    132   }
    133   this->firstEntry = NULL;
    134   this->finalized = true;
    135 }
    136 
    137 /**
    138  *  adds a new Entry to the Array
    139  * @param entry Entry to add.
    140 */
    141 template<class T>
    142 void Array<T>::addEntry (T entry)
    143 {
    144   if (!this->finalized)
    145   {
    146     PRINTF(5)("adding new Entry to Array: %f\n", entry);
    147 
    148     this->currentEntry->value = entry;
    149     this->currentEntry->next = new Entry;
    150     this->currentEntry = currentEntry->next;
    151     this->currentEntry->next = NULL;
    152     ++this->entryCount;
    153   }
    154   else
    155     PRINTF(2)("adding failed, because array has already been finalized\n");
    156 }
    157 
    158 /**
    159  *  Adds 3 entries at once (convenience)
    160 */
    161 template<class T>
    162 void Array<T>::addEntry (T entry0, T entry1, T entry2)
    163 {
    164   this->addEntry(entry0);
    165   this->addEntry(entry1);
    166   this->addEntry(entry2);
    167 }
    168 
    169 
    170 /**
    171  *  gets back the index of the entry in the array. value check
    172  * @param entry: the entry to look up
    173  * @returns the index in the array, -1 if not found
     68 * delocates alocated memory from a Stack.
     69 * This does not delete the entries of the Stack
    17470 */
    17571template<class T>
    176 int Array<T>::getIndex(T* entry) const
     72    tStack<T>::~tStack()
    17773{
    178   if( unlikely(this->finalized == false))
    179     return -1;
    180 
    181   for(int i = 0; i < this->entryCount; ++i)
     74  tStackEntry* delEntry;
     75  while (this->topEntry != NULL)
    18276  {
    183     if( unlikely(*entry == this->array[i]))
    184       return i;
     77    delEntry = this->topEntry;
     78    this->topEntry = topEntry->next;
     79    delete delEntry;
    18580  }
    18681}
     
    18883
    18984/**
    190  *  Simple debug info about the Array
    191 */
     85 * pushes one Entry into the Stack.
     86 * @param entry the Entry to push into the Stack
     87 */
    19288template<class T>
    193 void Array<T>::debug () const
     89    void tStack<T>::push(T entry)
    19490{
    195   PRINT(0)("entryCount=%i, address=%p\n", this->entryCount, this->array);
     91  tStackEntry* newEntry = new tStackEntry;
     92  newEntry->value = entry;
     93  newEntry->next = this->topEntry;
     94  this->topEntry = newEntry;
     95
     96  this->entryCount++;
    19697}
    19798
    198 #endif
     99
     100/**
     101 * pops up the topmost enrty of the Stack, and moves the pointer to the next Stack-entry.
     102 * @returns the top-most enrty.
     103 */
     104template<class T>
     105    T tStack<T>::pop()
     106{
     107  if (this->topEntry == NULL)
     108    return 0;
     109
     110  tStackEntry* retEntry = this->topEntry;
     111  T retVal = retEntry->value;
     112  this->topEntry = this->topEntry->next;
     113  delete retEntry;
     114  this->entryCount--;
     115  return retVal;
     116}
     117
     118/**
     119 * @returns the topMost entry of the Stack
     120 */
     121template<class T>
     122    T tStack<T>::getTop()
     123{
     124  if (this->topEntry != NULL)
     125    return this->topEntry->value;
     126  else
     127    return NULL;
     128}
     129
     130#endif /* _T_STACK_H */
Note: See TracChangeset for help on using the changeset viewer.