pingus-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Pingus-CVS] CVS: Games/Pingus/src/input controller.cxx,1.15,1.16 contro


From: torangan
Subject: [Pingus-CVS] CVS: Games/Pingus/src/input controller.cxx,1.15,1.16 controller.hxx,1.8,1.9
Date: 17 Aug 2002 17:20:53 -0000

Update of /usr/local/cvsroot/Games/Pingus/src/input
In directory dark:/tmp/cvs-serv27715

Modified Files:
        controller.cxx controller.hxx 
Log Message:
changed vector<pair<ButtonName, Button*> > into a map


Index: controller.cxx
===================================================================
RCS file: /usr/local/cvsroot/Games/Pingus/src/input/controller.cxx,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -d -r1.15 -r1.16
--- controller.cxx      16 Aug 2002 18:13:36 -0000      1.15
+++ controller.cxx      17 Aug 2002 17:20:51 -0000      1.16
@@ -17,6 +17,7 @@
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
+#include "../debug.hxx"
 #include "../xml_helper.hxx"
 #include "../pingus_error.hxx"
 #include "axis.hxx"
@@ -75,22 +76,22 @@
          scroller = 
ScrollerFactory::create(XMLhelper::skip_blank(cur->children));
          
        else if ( ! strcmp(reinterpret_cast<const char*>(cur->name), 
"primary-button"))
-         buttons.push_back(ButtonEntry(primary, 
ButtonFactory::create(XMLhelper::skip_blank(cur->children))));
+         buttons[primary] = 
ButtonFactory::create(XMLhelper::skip_blank(cur->children));
          
        else if ( ! strcmp(reinterpret_cast<const char*>(cur->name), 
"secondary-button"))
-         buttons.push_back(ButtonEntry(secondary, 
ButtonFactory::create(XMLhelper::skip_blank(cur->children))));
+         buttons[secondary] = 
ButtonFactory::create(XMLhelper::skip_blank(cur->children));
          
        else if ( ! strcmp(reinterpret_cast<const char*>(cur->name), 
"pause-button"))
-         buttons.push_back(ButtonEntry(pause, 
ButtonFactory::create(XMLhelper::skip_blank(cur->children))));
+         buttons[pause] = 
ButtonFactory::create(XMLhelper::skip_blank(cur->children));
 
        else if ( ! strcmp(reinterpret_cast<const char*>(cur->name), 
"fast-forward-button"))
-         buttons.push_back(ButtonEntry(fast_forward, 
ButtonFactory::create(XMLhelper::skip_blank(cur->children))));
+         buttons[fast_forward] = 
ButtonFactory::create(XMLhelper::skip_blank(cur->children));
        
        else if ( ! strcmp(reinterpret_cast<const char*>(cur->name), 
"armageddon-button"))
-         buttons.push_back(ButtonEntry(armageddon, 
ButtonFactory::create(XMLhelper::skip_blank(cur->children))));
+         buttons[armageddon] = 
ButtonFactory::create(XMLhelper::skip_blank(cur->children));
        
        else if ( ! strcmp(reinterpret_cast<const char*>(cur->name), 
"escape-button"))
-         buttons.push_back(ButtonEntry(escape, 
ButtonFactory::create(XMLhelper::skip_blank(cur->children))));
+         buttons[escape] = 
ButtonFactory::create(XMLhelper::skip_blank(cur->children));
        
        else if ( ! strcmp(reinterpret_cast<const char*>(cur->name), 
"action-buttons"))
          create_action_buttons(XMLhelper::skip_blank(cur->children));
@@ -105,7 +106,10 @@
       }
 
     if (!standard_pointer)
-      standard_pointer = new DummyPointer;
+      {
+        standard_pointer = new DummyPointer;
+       pwarn << "Controller: No standard pointer - inserting dummy" << 
std::endl;
+      }
     else
       {
         std_pointer_x = standard_pointer->get_x_pos();
@@ -113,32 +117,57 @@
       }
 
     if (!scroller)
-      scroller = new DummyScroller;
+      {
+        scroller = new DummyScroller;
+       pwarn << "Controller: No scroller - inserting dummy" << std::endl;
+      }
       
     if (!action_axis)
-      action_axis = new DummyAxis;
+      {
+        action_axis = new DummyAxis;
+       pwarn << "Controller: No action axis button - inserting dummy" << 
std::endl;
+      }
       
-    if (!find_button(primary))
-      buttons.push_back(ButtonEntry(primary, new DummyButton));
+    if (!buttons.count(primary))
+      {
+        buttons[primary] = new DummyButton;
+       pwarn << "Controller: No primary button - inserting dummy" << std::endl;
+      }
       
-    if (!find_button(secondary))
-      buttons.push_back(ButtonEntry(secondary, new DummyButton));
+    if (!buttons.count(secondary))
+      {
+        buttons[secondary] = new DummyButton;
+       pwarn << "Controller: No secondary button - inserting dummy" << 
std::endl;
+      }
 
-    if (!find_button(pause))
-      buttons.push_back(ButtonEntry(pause, new DummyButton));
+    if (!buttons.count(pause))
+      {
+        buttons[pause] = new DummyButton;
+       pwarn << "Controller: No pause button - inserting dummy" << std::endl;
+      }
 
-    if (!find_button(fast_forward))
-      buttons.push_back(ButtonEntry(fast_forward, new DummyButton));
+    if (!buttons.count(fast_forward))
+      {
+        buttons[fast_forward] = new DummyButton;
+       pwarn << "Controller: No fast_forward button - inserting dummy" << 
std::endl;
+      }
       
-    if (!find_button(armageddon))
-      buttons.push_back(ButtonEntry(armageddon, new DummyButton));
+    if (!buttons.count(armageddon))
+      {
+        buttons[armageddon] = new DummyButton;
+       pwarn << "Controller: No armageddon button - inserting dummy" << 
std::endl;
+      }
 
-    if (!find_button(escape))
-      buttons.push_back(ButtonEntry(escape, new DummyButton));
+    if (!buttons.count(escape))
+      {
+        buttons[escape] = new DummyButton;
+       pwarn << "Controller: No escape button - inserting dummy" << std::endl;
+      }
 
     button_states.resize(buttons.size());
-    for (unsigned int i=0; i < buttons.size(); i++)
-      button_states[i] = buttons[i].second->is_pressed();
+    std::map<ButtonName, Button*>::const_iterator it = buttons.begin();
+    for (unsigned int i=0; i < buttons.size(); ++i, ++it)
+      button_states[i] = it->second->is_pressed();
   }
 
   void
@@ -155,8 +184,7 @@
          }
          
        if ( ! strcmp(reinterpret_cast<const char*>(cur->name), 
"action-button"))
-         buttons.push_back(ButtonEntry(static_cast<ButtonName>(action_1 + 
count),
-                                       
ButtonFactory::create(XMLhelper::skip_blank(cur->children))));
+         buttons[static_cast<ButtonName>(action_1 + count)] = 
ButtonFactory::create(XMLhelper::skip_blank(cur->children));
        else
          PingusError::raise(std::string("Wrong Element in Controller Config 
(action-buttons): ") + reinterpret_cast<const char*>(cur->name));
        
@@ -177,9 +205,9 @@
     standard_pointer->update(delta);
     action_axis     ->update(delta);
 
-    for (unsigned int i=0; i < buttons.size(); i++)
-      buttons[i].second->update(delta);
-           
+    for (std::map<ButtonName, Button*>::iterator it = buttons.begin(); it != 
buttons.end(); ++it)
+      it->second->update(delta);
+
     if (std_pointer_x != standard_pointer->get_x_pos() || std_pointer_y != 
standard_pointer->get_y_pos())
       {
        std_pointer_x = standard_pointer->get_x_pos();
@@ -191,14 +219,15 @@
     if (scroller->get_x_delta() || scroller->get_y_delta())
       events.push_back(new ScrollEvent(scroller->get_x_delta(), 
scroller->get_y_delta()));
     
-    for (unsigned int i=0; i < buttons.size(); i++)
-      if (buttons[i].second->is_pressed() != button_states[i])
+    std::map<ButtonName, Button*>::const_iterator it = buttons.begin();
+    for (unsigned int i=0; i < buttons.size(); ++i, ++it)
+      if (it->second->is_pressed() != button_states[i])
         {
-         button_states[i] = buttons[i].second->is_pressed();
+         button_states[i] = it->second->is_pressed();
          if (button_states[i])
-           events.push_back(new ButtonEvent(buttons[i].first, pressed));
+           events.push_back(new ButtonEvent(it->first, pressed));
          else
-           events.push_back(new ButtonEvent(buttons[i].first, released));
+           events.push_back(new ButtonEvent(it->first, released));
        }
     
     const float& temp = action_axis->get_pos();
@@ -207,26 +236,14 @@
   }
 
   const Button*
-  Controller::get_button (ButtonName name) const
-  {
-    const Button* const & temp = find_button(name);
-    if (temp)
-      return temp;
-      
-    PingusError::raise("Invalid Button requested");
-    
-    return 0; // never reached
-  }
-
-  const Button*
-  Controller::find_button (ButtonName name) const
+  Controller::get_button (ButtonName name)
   {
-    for (unsigned int i=0; i < buttons.size(); ++i)
-      if (buttons[i].first == name)
-        return buttons[i].second;
+    if (buttons.count(name))
+      return buttons[name];
 
-    return 0;  
+    return 0;
   }
+
 }
 
 /* EOF */

Index: controller.hxx
===================================================================
RCS file: /usr/local/cvsroot/Games/Pingus/src/input/controller.hxx,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- controller.hxx      16 Aug 2002 15:14:00 -0000      1.8
+++ controller.hxx      17 Aug 2002 17:20:51 -0000      1.9
@@ -20,10 +20,10 @@
 #ifndef HEADER_PINGUS_INPUT_CONTROLLER_HXX
 #define HEADER_PINGUS_INPUT_CONTROLLER_HXX
 
+#include <map>
 #include <list>
 #include <string>
 #include <vector>
-#include <utility>
 #include "../libxmlfwd.hxx"
 #include "button_event.hxx"
 
@@ -35,8 +35,6 @@
   class Pointer;
   class Scroller;
 
-  typedef std::pair<ButtonName, Button*> ButtonEntry;
-  
   class Controller {
 
     private:
@@ -44,8 +42,7 @@
       Pointer*  standard_pointer;
       Scroller* scroller;
     
-      std::vector<std::pair<ButtonName, Button*> > buttons;
-                  
+      std::map<ButtonName, Button*> buttons;
       
       std::list<Event*> events;
 
@@ -62,15 +59,14 @@
       const Scroller* get_scroller    () const { return scroller;         }
       const Axis    * get_action_axis () const { return action_axis;      }
 
-      const Button* get_button (ButtonName name) const;
+      /// returns the requested button or 0 if it doesn't exist (e.g. 
undefined action button)
+      const Button* get_button (ButtonName name);
       
       void update(float delta);
 
     private:
       void create_action_buttons (xmlNodePtr cur);
       
-      const Button* find_button (ButtonName name) const;
-         
   };
 }
 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]