PageRenderTime 37ms CodeModel.GetById 15ms app.highlight 17ms RepoModel.GetById 2ms app.codeStats 0ms

/editor/src/frame_listener.cpp

http://pmaz3.googlecode.com/
C++ | 102 lines | 67 code | 18 blank | 17 comment | 13 complexity | e2171f93ca094d9020ac268028f5d3b7 MD5 | raw file
  1/** 
  2    @file frame_listener.cpp 
  3    @brief Frame Listener class functions
  4*/
  5#include "frame_listener.hpp"
  6
  7pm3::FRAME_LISTENER::FRAME_LISTENER() {
  8	m_time = 0.0;
  9
 10    m_mouse_state = 0;
 11}
 12
 13pm3::FRAME_LISTENER::~FRAME_LISTENER() {
 14}
 15
 16bool pm3::FRAME_LISTENER::frameEnded( const Ogre::FrameEvent &r_event ) {
 17    pm3::INTERFACE *intf = pm3::INTERFACE::getSingletonPtr();
 18    float worst = 0, avg = 0, best = 0;
 19    std::string str;
 20
 21    if (intf && intf->get_render_window()) {
 22        // Update FPS on the overlay
 23        worst = intf->get_render_window()->getWorstFPS();
 24        avg = intf->get_render_window()->getAverageFPS();
 25        best = intf->get_render_window()->getBestFPS();
 26
 27        str = "FPS: " + pm3::util::to_str( worst )  + " .. " +
 28                        pm3::util::to_str( avg ) + " .. " +
 29                        pm3::util::to_str( best );
 30        pm3::hud::TEXT_OVERLAY::getSingleton().set_text( "FPS", str );
 31    }
 32
 33	return true;
 34}
 35
 36bool pm3::FRAME_LISTENER::frameStarted( const Ogre::FrameEvent &r_event ) {
 37    pm3::PHYSICS *physics = pm3::PHYSICS::getSingletonPtr();
 38    pm3::PIXELWORLD *world = pm3::PIXELWORLD::getSingletonPtr();
 39    int num_movement_keys = 0;
 40    float mouse_sensitivity = 0;
 41    Ogre::Vector2 mouse_delta;
 42
 43    // Keyboard
 44    {
 45        // Go forward
 46        if (m_keymap [Qt::Key_W]) {
 47            world->m_player.move( Ogre::Vector3( 0.0f, 0.0f, -r_event.timeSinceLastFrame ) );
 48            num_movement_keys++;
 49        }
 50        // Go backwards
 51        if (m_keymap [Qt::Key_S]) {
 52            world->m_player.move( Ogre::Vector3( 0.0f, 0.0f, r_event.timeSinceLastFrame ) );
 53            num_movement_keys++;
 54        }
 55        // Strafe left
 56        if (m_keymap [Qt::Key_A]) {
 57            world->m_player.move( Ogre::Vector3( -r_event.timeSinceLastFrame, 0.0f, 0.0f ) );
 58            num_movement_keys++;
 59        }
 60        // Strafe right
 61        if (m_keymap [Qt::Key_D]) {
 62            world->m_player.move( Ogre::Vector3( r_event.timeSinceLastFrame, 0.0f, 0.0f ) );
 63            num_movement_keys++;
 64        }
 65
 66        // No movement? Stop the player then
 67        if (num_movement_keys == 0)
 68            world->m_player.stop();
 69    }
 70
 71    // Mouse
 72    {
 73        mouse_sensitivity = pm3::PIXELWORLD::getSingleton().m_player.get_mouse_sensitivity();
 74
 75        mouse_delta = m_mouse_new_pos - m_mouse_old_pos;
 76        mouse_delta *= -r_event.timeSinceLastFrame * mouse_sensitivity;
 77
 78        if (( m_mouse_state & Qt::RightButton ) != 0) {
 79            world->m_player.angle_rotate( Ogre::Degree( mouse_delta.x ), 
 80                                          Ogre::Degree( mouse_delta.y ) );
 81        }
 82    }
 83
 84    // Update physics
 85    if (physics && physics->get_physics_world()) {
 86        // Update bullet world
 87        //! \note The third argument is fixed timestep, which should be small enough to allow for enough iterations during one frame
 88        physics->get_physics_world()->stepSimulation( r_event.timeSinceLastFrame, 10, 1.0f / 200.0f );
 89        physics->get_physics_world()->debugDrawWorld();
 90
 91        physics->get_physics_debug()->step();
 92    }
 93
 94    // Update world
 95    if (world)
 96        world->update();
 97
 98    // Update the camera debug overlay text
 99    pm3::hud::TEXT_OVERLAY::getSingleton().set_text( "Camera", pm3::PIXELWORLD::getSingleton().m_player.get_pos_str() );
100
101	return true;
102}