Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 934


Ignore:
Timestamp:
Mar 27, 2008, 5:15:08 PM (17 years ago)
Author:
rgrieder
Message:
  • added debug output to the InputHandler
  • fixed singleton issues
  • cleaned up Main.cc (replaced WinMain by main)
Location:
code/branches/network/src/orxonox
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • code/branches/network/src/orxonox/CMakeLists.txt

    r922 r934  
    11SET( ORXONOX_SRC_FILES
    22  GraphicsEngine.cc
     3  InputEventListener.cc
    34  InputHandler.cc
    4   InputEventListener.cc
    55  Main.cc
    66  Orxonox.cc
  • code/branches/network/src/orxonox/InputHandler.cc

    r930 r934  
    2222 *      Reto Grieder
    2323 *   Co-authors:
    24  *      Some guy writing the example code from Ogre
     24 *      ...
    2525 *
    2626 */
     
    3535
    3636#include "core/CoreIncludes.h"
     37#include "core/Debug.h"
    3738#include "Orxonox.h"
    3839#include "InputEventListener.h"
     
    4243{
    4344  /**
     45    @brief The reference to the singleton
     46  */
     47  InputHandler* InputHandler::singletonRef_s = 0;
     48
     49  /**
    4450    @brief Constructor only resets the pointer values to 0.
    4551  */
    4652  InputHandler::InputHandler() :
    47       mouse_(0), keyboard_(0), inputSystem_(0),
    48       uninitialized_(true)
     53      mouse_(0), keyboard_(0), inputSystem_(0)
    4954  {
    5055    //RegisterObject(InputHandler);
     
    5661  InputHandler::~InputHandler()
    5762  {
    58     this->destroy();
    5963  }
    6064
     
    6569  InputHandler *InputHandler::getSingleton()
    6670  {
    67     static InputHandler theOnlyInstance;
    68     return &theOnlyInstance;
     71    if (!singletonRef_s)
     72      singletonRef_s = new InputHandler();
     73    return singletonRef_s;
     74    //static InputHandler theOnlyInstance;
     75    //return &theOnlyInstance;
    6976  }
    7077
     
    7683    @param windowHeight The height of the render window
    7784  */
    78   void InputHandler::initialise(size_t windowHnd, int windowWidth, int windowHeight)
    79   {
    80     if (this->uninitialized_ || !this->inputSystem_)
     85  bool InputHandler::initialise(size_t windowHnd, int windowWidth, int windowHeight)
     86  {
     87    if (!this->inputSystem_)
    8188    {
    8289      // Setup basic variables
     
    9299#endif
    93100
    94       // Create inputsystem
    95       inputSystem_ = OIS::InputManager::createInputSystem(paramList);
    96 
    97       // If possible create a buffered keyboard
    98       if (inputSystem_->numKeyboards() > 0)
     101      try
    99102      {
    100         keyboard_ = static_cast<OIS::Keyboard*>(inputSystem_->createInputObject(OIS::OISKeyboard, true));
    101         keyboard_->setEventCallback(this);
     103        // Create inputsystem
     104        inputSystem_ = OIS::InputManager::createInputSystem(paramList);
     105        //if (getSoftDebugLevel() >= ORX_DEBUG)
     106        //  orxonox::OutputHandler::getOutStream().setOutputLevel(4) << "asdfblah" << std::endl;
     107        COUT(ORX_DEBUG) << "*** InputHandler: Created OIS input system" << std::endl;
     108
     109        // If possible create a buffered keyboard
     110        if (inputSystem_->numKeyboards() > 0)
     111        {
     112          keyboard_ = static_cast<OIS::Keyboard*>(inputSystem_->createInputObject(OIS::OISKeyboard, true));
     113          keyboard_->setEventCallback(this);
     114          COUT(ORX_DEBUG) << "*** InputHandler: Created OIS mouse" << std::endl;
     115        }
     116
     117        // If possible create a buffered mouse
     118        if (inputSystem_->numMice() > 0 )
     119        {
     120          mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true));
     121          mouse_->setEventCallback(this);
     122          COUT(ORX_DEBUG) << "*** InputHandler: Created OIS keyboard" << std::endl;
     123
     124          // Set mouse region
     125          this->setWindowExtents(windowWidth, windowHeight);
     126        }
    102127      }
    103 
    104       // If possible create a buffered mouse
    105       if (inputSystem_->numMice() > 0 )
     128      catch (OIS::Exception ex)
    106129      {
    107         mouse_ = static_cast<OIS::Mouse*>(inputSystem_->createInputObject(OIS::OISMouse, true));
    108         mouse_->setEventCallback(this);
    109 
    110         // Set mouse region
    111         this->setWindowExtents(windowWidth, windowHeight);
     130        // something went wrong with the initialisation
     131        COUT(ORX_ERROR) << "Error: Failed creating an input system. Message: \"" << ex.eText << "\"" << std::endl;
     132        this->inputSystem_ = 0;
     133        return false;
    112134      }
    113 
    114       uninitialized_ = false;
    115135    }
    116136
     137    COUT(ORX_DEBUG) << "*** InputHandler: Loading key bindings..." << std::endl;
     138    // temporary solution: create event list
     139    //InputEvent[] list = this->createEventList();
    117140    // load the key bindings
    118141    InputEvent empty = {0, false, 0, 0, 0};
     
    122145    //assign 'abort' to the escape key
    123146    this->bindingsKeyPressed_[(int)OIS::KC_ESCAPE].id = 1;
     147    COUT(ORX_DEBUG) << "*** InputHandler: Loading done." << std::endl;
     148
     149    return true;
    124150  }
    125151
     
    127153    @brief Destroys all the created input devices.
    128154  */
    129   void InputHandler::destroy()
    130   {
     155  void InputHandler::destroyDevices()
     156  {
     157    COUT(ORX_DEBUG) << "*** InputHandler: Destroying InputHandler..." << std::endl;
    131158    if (this->mouse_)
    132159      this->inputSystem_->destroyInputObject(mouse_);
     
    139166    this->keyboard_      = 0;
    140167    this->inputSystem_   = 0;
    141     this->uninitialized_ = true;
     168    COUT(ORX_DEBUG) << "*** InputHandler: Destroying done." << std::endl;
     169  }
     170
     171  /**
     172    @brief Destroys the singleton.
     173  */
     174  void InputHandler::destroy()
     175  {
     176    if (singletonRef_s)
     177      delete singletonRef_s;
     178    singletonRef_s = 0;
    142179  }
    143180
  • code/branches/network/src/orxonox/InputHandler.h

    r929 r934  
    4343namespace orxonox
    4444{
     45  /**
     46    @brief Captures and distributes mouse and keyboard input.
     47    It resolves the key bindings to InputEvents which can be heard by
     48    implementing the InputEventListener interface.
     49  */
    4550  class _OrxonoxExport InputHandler
    4651        : public Tickable, public OIS::KeyListener, public OIS::MouseListener
    4752  {
    48     //friend ClassIdentifier<InputHandler>;
     53    //friend class ClassIdentifier<InputHandler>;
    4954  public:
    50     void initialise(size_t windowHnd, int windowWidth, int windowHeight);
    51     void destroy();
     55    bool initialise(size_t windowHnd, int windowWidth, int windowHeight);
     56    void destroyDevices();
    5257    void tick(float dt);
    5358    void setWindowExtents(int width, int height);
    5459
     60    // Temporary solutions. Will be removed soon!
    5561    OIS::Mouse    *getMouse()    { return this->mouse_   ; }
    5662    OIS::Keyboard *getKeyboard() { return this->keyboard_; }
    5763
    5864    static InputHandler* getSingleton();
     65    static void destroy();
    5966
    6067  private:
     
    7885    OIS::Mouse        *mouse_;          //!< OIS keyboard
    7986
    80     /**
    81       @bref Tells whether initialise has been called successfully
    82       Also true if destroy() has been called.
    83     */
    84     bool uninitialized_;
    85 
    86     //! denotes the maximum number of different keys there are in OIS.
    87     //! 256 should be ok since the highest number in the enum is 237.
     87    /** denotes the maximum number of different keys there are in OIS.
     88        256 should be ok since the highest number in the enum is 237. */
    8889    static const int numberOfKeys_ = 256;
    8990    //! Array of input events for every pressed key
     
    9293    InputEvent bindingsKeyReleased_[numberOfKeys_];
    9394
    94     //! denotes the maximum number of different buttons there are in OIS.
    95     //! 16 should be ok since the highest number in the enum is 7.
     95    /** denotes the maximum number of different buttons there are in OIS.
     96        16 should be ok since the highest number in the enum is 7. */
    9697    static const int numberOfButtons_ = 16;
    9798    //! Array of input events for every pressed key
     
    100101    InputEvent bindingsButtonReleased_[numberOfButtons_];
    101102
     103    //! Pointer to the instance of the singleton
     104    static InputHandler *singletonRef_s;
    102105  };
    103106}
  • code/branches/network/src/orxonox/Main.cc

    r918 r934  
    2727
    2828 /**
    29  @file  Main.cc
    30  @brief main file handling most of the machine specific code
     29 @file
     30 @brief Entry point of the program. Platform specific code.
    3131  */
    3232
    3333#include "OrxonoxStableHeaders.h"
    3434
    35 #include <OgrePlatform.h>
    36 #include <OgreException.h>
     35#include <exception>
    3736
    38 
     37#include "OrxonoxPlatform.h"
    3938#include "core/SignalHandler.h"
    4039#include "Orxonox.h"
    4140
    4241using namespace orxonox;
    43 #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
     42#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_APPLE
    4443#include <CoreFoundation/CoreFoundation.h>
    4544
     
    7271#endif
    7372
    74 #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 && !defined( __MINGW32__ )
    75 #ifndef WIN32_LEAN_AND_MEAN
    76 #define WIN32_LEAN_AND_MEAN
    77 #endif
    78 #include <windows.h>
    79   INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
    80   {
    81     // something like this would be less hacky
    82     // maybe one can work with trailing '\0'
    83     // or maybe use string based functions
    84     // I was unable to test it without working windows version
    85     char* cmd = strCmdLine;
    86     int argc = 2;
    87     int i;
    88     int j;
    89     for(i = 0; cmd[i] != NULL; i++)
    90     {
    91       if(cmd[i] == ' ') argc++;
    92     }
    93     char **argv = new char*[argc];
    94     for (j = 0; j < argc; j++)
    95     {
    96       argv[j] = new char[i];
    97     }
    98     j = 1;
    99     int k = 0;
    100     for(int i = 0; cmd[i] != NULL; i++)
    101     {
    102       if(cmd[i] != ' ') {
    103         argv[j][k] = cmd[i];
    104         k++;
    105       }
    106       else {
    107         argv[j][k] = '\0';
    108         k = 0;
    109         j++;
    110       }
    111     }
    112     argv[j][k] = '\0';
    113     argv[0] = "BeniXonox.exe";
    114     //char *argv[2];
    115     //argv[0] = "asdfProgram";
    116     //argv[1] =  strCmdLine;
    117     //int argc = 2;
     73int main(int argc, char **argv)
     74{
     75  try {
     76    SignalHandler::getInstance()->doCatch(argv[0], "orxonox.log");
     77    Orxonox* orx = Orxonox::getSingleton();
     78
     79#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_APPLE
     80    orx->init(argc, argv, macBundlePath());
    11881#else
    119   int main(int argc, char **argv)
    120   {
    121 #endif
    122     try {
    123       srand(time(0));  //initaialize RNG; TODO check if it works on win
    124       SignalHandler::getInstance()->doCatch(argv[0], "orxonox.log");
    125       Orxonox* orx = Orxonox::getSingleton();
    126 #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
    127       orx->init(argc, argv, macBundlePath());
    128       orx->start();
    129 #else
    130 /*
    131 for (int i = 0; i < 500; i++)
    132 {
    133 int x = rand() % 40000 - 20000;
    134 int y = rand() % 40000 - 20000;
    135 int z = rand() % 40000 - 20000;
    136 
    137 int scale = rand() % 100 + 20;
    138 
    139 int version = rand() % 6 + 1;
    140 
    141 float rotx = float(rand()) / RAND_MAX;
    142 float roty = float(rand()) / RAND_MAX;
    143 float rotz = float(rand()) / RAND_MAX;
    144 
    145 int axis = rand() % 3 + 1;
    146 
    147 if (axis == 1)
    148   rotx = 0;
    149 if (axis == 2)
    150   roty = 0;
    151 if (axis == 3)
    152   rotz = 0;
    153 
    154 int rotation = rand() % 40 + 10;
    155 
    156 //    <Model position="1000,1500,0" scale="50" mesh="ast1.mesh" rotationAxis="0,1.25,0" rotationRate="70" />
    157 std::cout << "    <Model position=\"" << x << "," << y << "," << z << "\" scale=\"" << scale << "\" mesh=\"ast" << version << ".mesh\" rotationAxis=\"" << rotx << "," << roty << "," << rotz << "\" rotationRate=\"" << rotation << "\" />" << std::endl;
    158 
    159 
    160 }
    161 */
    162       orx->init(argc, argv, "");
    163       orx->start();
    164 #endif
    165     }
    166     catch (Ogre::Exception& e) {
    167 #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 && !defined( __MINGW32__ )
    168       MessageBoxA(NULL, e.getFullDescription().c_str(),
    169             "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
    170 #else
    171       std::cerr << "Exception:\n";
    172       std::cerr << e.getFullDescription().c_str() << "\n";
    173 #endif
    174       return 1;
    175     }
    176     return 0;
    177   }
    178 
    179 #ifdef __cplusplus
    180 }
     82    orx->init(argc, argv, "");
    18183#endif
    18284
    183 
    184 /*int main(int argc, char **argv)
    185 {
    186   try
     85    orx->start();
     86    orx->destroy();
     87  }
     88  catch (std::exception &ex)
    18789  {
    188     SignalHandler::getInstance()->doCatch(argv[0], "orxonox.log");
    189     Orxonox* orx = Orxonox::getSingleton();
    190 #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
    191     orx->init(argc, argv, macBundlePath());
    192     orx->start();
    193 #else
    194     orx->init(argc, argv, "");
    195     orx->start();
    196 #endif
    197 
    198   }
    199   catch(Ogre::Exception& e)
    200   {
    201     fprintf(stderr, "An exception has occurred: %s\n",
    202             e.getFullDescription().c_str());
     90    std::cerr << "Exception:\n";
     91    std::cerr << ex.what() << "\n";
    20392    return 1;
    20493  }
     
    20796}
    20897
    209 */
     98#ifdef __cplusplus
     99}
     100#endif
  • code/branches/network/src/orxonox/Orxonox.cc

    r930 r934  
    4646//#include <exception>
    4747#include <deque>
    48 #define _CRTDBG_MAP_ALLOC
    49 #include <stdlib.h>
    50 #include <crtdbg.h>
    5148
    5249//***** ORXONOX ****
     
    8077{
    8178  /**
     79    @brief Reference to the only instance of the class.
     80  */
     81  Orxonox *Orxonox::singletonRef_s = 0;
     82
     83  /**
    8284   * create a new instance of Orxonox
    8385   */
     
    8587  {
    8688    this->ogre_ = new GraphicsEngine();
     89    this->timer_ = 0;
    8790    this->dataPath_ = "";
    8891    this->auMan_ = 0;
     
    105108      delete this->orxonoxHUD_;
    106109    Loader::close();
    107     // do not destroy the InputHandler since this is a singleton too
    108     // and might have been deleted already (after return 0; in main())
     110    InputHandler::destroy();
    109111    if (this->auMan_)
    110112      delete this->auMan_;
     
    142144  Orxonox* Orxonox::getSingleton()
    143145  {
    144     static Orxonox theOnlyInstance;
    145     return &theOnlyInstance;
     146    if (!singletonRef_s)
     147      singletonRef_s = new Orxonox();
     148    return singletonRef_s;
     149    //static Orxonox theOnlyInstance;
     150    //return &theOnlyInstance;
     151  }
     152
     153  /**
     154    @brief Destroys the Orxonox singleton.
     155  */
     156  void Orxonox::destroy()
     157  {
     158    if (singletonRef_s)
     159      delete singletonRef_s;
     160    singletonRef_s = 0;
    146161  }
    147162
     
    313328  {
    314329    inputHandler_ = InputHandler::getSingleton();
    315     inputHandler_->initialise(ogre_->getWindowHandle(),
    316           ogre_->getWindowWidth(), ogre_->getWindowHeight());
     330    if (!inputHandler_->initialise(ogre_->getWindowHandle(),
     331          ogre_->getWindowWidth(), ogre_->getWindowHeight()))
     332      abortImmediate();
    317333  }
    318334
  • code/branches/network/src/orxonox/Orxonox.h

    r929 r934  
    4444
    4545      static Orxonox* getSingleton();
     46      static void destroy();
    4647
    4748   private:
     
    8687      gameMode              mode_;
    8788      std::string           serverIp_;
     89
     90      static Orxonox *singletonRef_s;
    8891  };
    8992}
  • code/branches/network/src/orxonox/core/DebugLevel.cc

    r871 r934  
    9595
    9696        // Return a constant value while we're creating the object
    97         return 4;
     97        return 3;
    9898    }
    9999}
Note: See TracChangeset for help on using the changeset viewer.