InputHandler.cpp

Go to the documentation of this file.
00001 
00005 /* Copyright © 2009, 2010 James Legg.
00006     This program is free software: you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation, either version 3 of the License, or
00009     (at your option) any later version.
00010 */
00011 #include "InputHandler.h"
00012 
00013 #include "InputDeviceKeyboard.h"
00014 #include "InputDeviceJoystick.h"
00015 
00016 #include <Debug.h>
00017 
00018 namespace Engine
00019 {
00020 
00021 InputHandler::InputHandler()
00022     :   scene(0)
00023     ,   keyboard(0)
00024 {
00025 }
00026 
00027 void InputHandler::init()
00028 {
00029     // don't initalise twice.
00030     if (keyboard) return;
00031     
00032     // Add devices which are permanetly attached.
00033     keyboard = (InputDeviceKeyboard*)-1;
00034     keyboard = new InputDeviceKeyboard;
00035     
00036     // Add all joysticks found.
00037     SDL_InitSubSystem(SDL_INIT_JOYSTICK);
00038     int number_of_joysticks = SDL_NumJoysticks();
00039     DEBUG_MESSAGE("Using " << number_of_joysticks << " joysticks. ");
00040     joysticks.reserve(number_of_joysticks);
00041     for (int joystick_index = 0; joystick_index < number_of_joysticks;
00042          joystick_index++)
00043     {
00044         add_device(*(new InputDeviceJoystick(joystick_index)));
00045         DEBUG_MESSAGE("Using joystick \"" << SDL_JoystickName(joystick_index));
00046     }
00047     
00049 }
00050 
00051 InputHandler::~InputHandler()
00052 {
00053     delete keyboard;
00054     for (std::vector<InputDeviceJoystick *>::iterator it = joysticks.begin();
00055          it != joysticks.end();
00056          it++)
00057     {
00058         delete *it;
00059     }
00060     joysticks.clear();
00061 }
00062 
00063 InputHandler & InputHandler::get_instance()
00064 {
00065     static InputHandler input_handler;
00066     input_handler.init();
00067     return input_handler;
00068 }
00069 
00070 InputHandler::iterator InputHandler::add_device(InputDevice & device)
00071 {
00072     if (scene)
00073     {
00074         device.set_scene(*scene);
00075     }
00076     return devices.insert(&device).first;
00077 }
00078 
00079 void InputHandler::remove_device(InputHandler::iterator it)
00080 {
00081     devices.erase(it);
00082 }
00083 
00084 void InputHandler::remove_device(InputDevice & device)
00085 {
00086     devices.erase(&device);
00087 }
00088 
00089 void InputHandler::set_scene(Scene & scene_in)
00090 {
00091     scene = &scene_in;
00092     for (iterator it = devices.begin(); it != devices.end(); it++)
00093     {
00094         (*it)->set_scene(*scene);
00095     }
00096 }
00097 
00098 void InputHandler::poll()
00099 {
00100     // poll all devices that need polling for events.
00101     for (iterator it = devices.begin(); it != devices.end(); it++)
00102     {
00103         (*it)->poll();
00104     }
00105 }
00106 
00107 InputHandler::iterator InputHandler::begin()
00108 {
00109     return devices.begin();
00110 }
00111 
00112 InputHandler::iterator InputHandler::end()
00113 {
00114     return devices.end();
00115 }
00116 
00117 std::size_t InputHandler::get_number_of_devices() const
00118 {
00119     return devices.size();
00120 }
00121 
00122 
00123 }

Get Racer at SourceForge.net. Fast, secure and Free Open Source software downloads

Generated at Mon Sep 6 00:41:12 2010 by Doxygen version 1.4.7 for Racer version svn335.