Beispiel #1
0
  void StateManager::resetActiveState(void)
  {
    // Is there no currently active state to reset?
    if(!mStack.empty())
    {
      // Retrieve the currently active state
      IState* anState = mStack.back();

      // Log the resetting of an active state
      ILOG() << "StateManager::resetActiveState(" << anState->getID() << ")" << std::endl;

      // Pause the currently active state
      anState->pause();

      // Call the ReInit method to Reset the currently active state
      anState->reInit();

      // Resume the currently active state
      anState->resume();

      // Don't keep pointers around we don't need anymore
      anState = NULL;
    }
    else
    {
      // Quit the application with an error status response
      if(NULL != mApp)
      {
        mApp->quit(StatusAppStackEmpty);
      }
      return;
    }
  }
Beispiel #2
0
  void StateManager::removeActiveState(void)
  {
    // Is there no currently active state to drop?
    if(!mStack.empty())
    {
      // Retrieve the currently active state
      IState* anState = mStack.back();

      // Log the removing of an active state
      ILOG() << "StateManager::removeActiveState(" << anState->getID() << ")" << std::endl;

      // Pause the currently active state
      anState->pause();
 
      // Deinitialize the currently active state before we pop it off the stack
      anState->deInit();

      // Pop the currently active state off the stack
      mStack.pop_back();

      // Move this state to our dropped stack
      mDead.push_back(anState);

      // Don't keep pointers around we don't need anymore
      anState = NULL;
    }
    else
    {
      // Quit the application with an error status response
      if(NULL != mApp)
      {
        mApp->quit(StatusAppStackEmpty);
      }
      return;
    }

    // Is there another state to activate? then call Resume to activate it
    if(!mStack.empty())
    {
      // Has this state ever been initialized?
      if(mStack.back()->isInitComplete())
      {
        // Resume the new active state
        mStack.back()->resume();
      }
      else
      {
        // Initialize the new active state
        mStack.back()->doInit();
      }
    }
    else
    {
      // There are no states on the stack, exit the program
      if(NULL != mApp)
      {
        mApp->quit(StatusAppOK);
      }
    }
  }
Beispiel #3
0
bool StateManager::update(size_t dt)
{
    if (m_bNeedDestroy)
    {
       IState* st = m_States.top();
       st->destroy();
       delete st;
       m_States.pop();
       m_bNeedDestroy = false;
       return true;
    }

    if (m_newState != 0)
    {
        m_States.push(m_newState);
        m_newState = 0;
        return true;
    }

    if (m_States.empty())
        return false;

    if (m_bNeedInit)
    {
        m_States.top()->init();
        m_bNeedInit = false;
        return true;
    }

    m_States.top()->update(dt);
    m_States.top()->render();

    return true;
}
Beispiel #4
0
IState* 
IState::clone() {

	IState *res = IState::create();
	res->copy(this);

	return res;
}
Beispiel #5
0
void StateManager::destroy()
{
	for (size_t i = 0; i != eState_Size; ++i)
	{
		IState* s = states_[i];
		if (s)
		{
			s->destroy();
			delete s;
			states_[i] = 0;
		}
	}
}
Beispiel #6
0
  void StateManager::setActiveState(Id theStateID)
  {
    std::vector<IState*>::iterator it;

    // Find the state that matches theStateID
    for(it=mStack.begin(); it < mStack.end(); it++)
    {
      // Does this state match theStateID? then activate it as the new
      // currently active state
      if((*it)->getID() == theStateID)
      {
        // Get a pointer to soon to be currently active state
        IState* anState = *it;

        // Log the setting of a previously active state as the current active state
        ILOG() << "StateManager::setActiveState(" << anState->getID() << ")" << std::endl;

        // Erase it from the list of previously active states
        mStack.erase(it);

        // Is there a state currently running? then Pause it
        if(!mStack.empty())
        {
          // Pause the currently running state since we are changing the
          // currently active state to the one specified by theStateID
          mStack.back()->pause();
        }

        // Add the new active state
        mStack.push_back(anState);

        // Don't keep pointers we don't need around
        anState = NULL;

        // Has this state ever been initialized?
        if(mStack.back()->isInitComplete())
        {
          // Resume the new active state
          mStack.back()->resume();
        }
        else
        {
          // Initialize the new active state
          mStack.back()->doInit();
        }

        // Exit our find loop
        break;
      } // if((*it)->getID() == theStateID)
    } // for(it=mStack.begin(); it < mStack.end(); it++)
  }
Beispiel #7
0
IState* 
IState::clone() {

	IState *res = IState::create();

	res->copy(this);

	//std::map< int, int>::iterator iterInt;
	//iterInt = m_IntProps.begin();
	//for ( ; iterInt != m_IntProps.end(); ++iterInt) {
	//
	//	res->m_IntProps[iterInt->first] = iterInt->second;
	//}

	//std::map< int, bool>::iterator iterBool;
	//iterBool = m_EnableProps.begin();
	//for ( ; iterBool != m_EnableProps.end(); ++iterBool) {
	//
	//	res->m_EnableProps[iterBool->first] = iterBool->second;
	//}

	//std::map< int, float>::iterator iterFloat;
	//iterFloat = m_FloatProps.begin();
	//for ( ; iterFloat != m_FloatProps.end(); ++iterFloat) {
	//
	//	res->m_FloatProps[iterFloat->first] = iterFloat->second;
	//}

	//std::map< int, vec4>::iterator iterVec4;
	//iterVec4 = m_Float4Props.begin();
	//for ( ; iterVec4 != m_Float4Props.end(); ++iterVec4) {
	//
	//	res->m_Float4Props[iterVec4->first] = iterVec4->second;
	//}

	//std::map< int, bvec4>::iterator iterBool4;
	//iterBool4 = m_Bool4Props.begin();
	//for ( ; iterBool4 != m_Bool4Props.end(); ++iterBool4) {
	//
	//	res->m_Bool4Props[iterBool4->first] = iterBool4->second;
	//}

	//iterInt = m_EnumProps.begin();
	//for ( ; iterInt != m_EnumProps.end(); ++iterInt) {
	//
	//	res->m_EnumProps[iterInt->first] = iterInt->second;
	//}
	return res;
}
Beispiel #8
0
  StateManager::~StateManager()
  {
    ILOGM("StateManager::dtor()");

    // Drop all active states
    while(!mStack.empty())
    {
      // Retrieve the currently active state
      IState* anState = mStack.back();
 
      // Pop the currently active state off the stack
      mStack.pop_back();

      // Pause the currently active state
      anState->pause();

      // De-initialize the state
      anState->deInit();

      // Handle the cleanup before we pop it off the stack
      anState->cleanup();

      // Just delete the state now
      delete anState;

      // Don't keep pointers around we don't need
      anState = NULL;
    }

    // Delete all our dropped states
    while(!mDead.empty())
    {
      // Retrieve the currently active state
      IState* anState = mDead.back();

      // Pop the currently active state off the stack
      mDead.pop_back();

      // Pause the currently active state
      anState->pause();

      // De-initialize the state
      anState->deInit();

      // Handle the cleanup before we pop it off the stack
      anState->cleanup();

      // Just delete the state now
      delete anState;

      // Don't keep pointers around we don't need
      anState = NULL;
    }

    // Clear pointers we don't need anymore
    mApp = NULL;
  }
Beispiel #9
0
bool Manager::_popState( IProgressState* newpstate )
{
	if( !m_states->current() )
		return( false );
	if( !newpstate )
		newpstate = m_progressStateDefault;
	m_engine->resetTimer();
	m_states->current()->onExit();
	m_engine->resetTimer();
	ActorManager* am = m_actors->current();
	DELETE_OBJECT( am );
	m_actors->pop();
	ControlManager* ctm = m_controls->current();
	DELETE_OBJECT( ctm );
	m_controls->pop();
	CollisionManager* clm = m_collisions->current();
	DELETE_OBJECT( clm );
	m_collisions->pop();
	IState* s = m_states->current();
	DELETE_OBJECT( s );
	m_states->pop();
	m_assets->releaseLevel( m_states->count() + 1 );
	IState* c = m_states->current();
	if( !m_changeState && c )
	{
		if( newpstate )
		{
			m_loading = true;
			newpstate->onEnter();
			m_loading = false;
		}
		m_engine->resetTimer();
		c->onEnter( newpstate );
		m_engine->resetTimer();
		if( newpstate )
		{
			m_loading = true;
			newpstate->onExit();
			m_loading = false;
			m_assetsLoading->releaseLevel( m_states->count() + 1 );
		}
	}
	return( true );
}
Beispiel #10
0
  void StateManager::cleanup(void)
  {
    // Always call our cleanup events with our pointer when this method is called
    mCleanupEvents.doEvents();

    // Remove one of our dead states
    if(!mDead.empty())
    {
      // Retrieve the dead state
      IState* anState = mDead.back();
      assert(NULL != anState && "StateManager::handleCleanup() invalid dropped state pointer");

      // Pop the dead state off the stack
      mDead.pop_back();

      // Call the DeInit if it hasn't been called yet
      if(anState->isInitComplete())
      {
        anState->deInit();
      }

      // Handle the cleanup before we delete anState
      anState->cleanup();

      // Just delete the state now
      delete anState;

      // Don't keep pointers around we don't need
      anState = NULL;
    }

    // Make sure we still have an active state
    if(NULL == mStack.back())
    {
      // There are no states on the stack, exit the program
      if(NULL != mApp)
      {
        mApp->quit(StatusAppOK);
      }
    }
  }
Beispiel #11
0
bool Manager::_pushState( IState* newstate, IProgressState* newpstate )
{
	if( !newstate )
		return( false );
	if( !newpstate )
		newpstate = m_progressStateDefault;
	IState* c = m_states->current();
	if( m_states->push( newstate ) )
	{
		if( !m_changeState && c )
		{
			m_engine->resetTimer();
			c->onExit();
			m_engine->resetTimer();
		}
		m_actors->push( xnew ActorManager() );
		Global::use().actors = m_actors->current();
		m_controls->push( xnew ControlManager() );
		Global::use().controls = m_controls->current();
		m_collisions->push( xnew CollisionManager() );
		Global::use().collisions = m_collisions->current();
		if( newpstate )
		{
			m_loading = true;
			newpstate->onEnter();
			m_loading = false;
		}
		m_engine->resetTimer();
		newstate->onEnter( newpstate );
		m_engine->resetTimer();
		if( newpstate )
		{
			m_loading = true;
			newpstate->onExit();
			m_loading = false;
			m_assetsLoading->releaseLevel( m_states->count() );
		}
		return( true );
	}
	return( false );
}
Beispiel #12
0
    void Game::Loop(void) {
        Logger::Log() << "Loop()";
        
        // Clock used in restricting Update loop to a fixed rate
        sf::Clock update_clock;
        update_clock.Reset();
        
        // Update immediately
        Uint32 next_update = update_clock.GetElapsedTime();
        
        // Loop while IsRunning returns true
        while(IsRunning() && window.IsOpened()) {
            
            IState* currentState = stateManager.GetActiveState();
            
            // Check for corrupt state returned by our StateManager
            assert(NULL != currentState && "Game::Loop() received a bad pointer");
            
            while (update_clock.GetElapsedTime() > next_update) {
             
                sf::Event event;

                while(window.PollEvent(event)) {
                    switch (event.Type) {
                        case sf::Event::Closed: // Window closed
                            Quit(EXIT_SUCCESS);
                            break;
                        case sf::Event::GainedFocus:  // Window gained focus
                            // resume state
                            currentState->Resume();
                            break;
                        case sf::Event::LostFocus:    // Window lost focus
                            // Pause state
                            currentState->Pause();
                            break;
                        case sf::Event::Resized:      // Window resized
                            break;
                        default:
                            // Allow state to handle events
                            currentState->HandleEvents(event);
                            break;
                    }
                    
                }

                // Update current state
                currentState->UpdateFixed();

                next_update += update_rate;
            }
            
            // Draw current state
            currentState->Draw();

            window.Display();
            
            // Cleanup state
        }
        
    }
Beispiel #13
0
//------------------------------------------------------------------------------
void Engine::start()
{
  Logger::setLogLevel(Logger::L_INFO);
  
  log.info("Starting engine.");
  
  setup();
  
  // Start the game loop
  while (renderWindow_->isOpen())
  {
    IState *state = state_manager_.getActiveState();
    
    // Process events
    sf::Event event;
    while (renderWindow_->pollEvent(event))
    {
      // Close window : exit
      if (event.type == sf::Event::Closed)
        renderWindow_->close();

      state->handleEvent(event);
    }
     
    // Clear screen
    renderWindow_->clear();
    
    state->update();
    state->draw();
    
    //drawFps();
         
    // Update the window
    renderWindow_->display();
  }

}
Beispiel #14
0
  void IApp::ProcessInput(IState& theState)
  {
    // Variable for storing the current input event to be processed
    sf::Event anEvent;

#if (SFML_VERSION_MAJOR < 2)
    while(mWindow.GetEvent(anEvent))
#else
    while(mWindow.pollEvent(anEvent))
#endif
    {
      // Handle some input events and let the current state handle the rest
#if (SFML_VERSION_MAJOR < 2)
      // Switch on Event Type
      switch(anEvent.Type)
#else
      // Switch on Event Type
      switch(anEvent.type)
#endif
      {
        case sf::Event::Closed:       // Window closed
          Quit(StatusAppOK);
          break;
        case sf::Event::GainedFocus:  // Window gained focus
          theState.Resume();
          break;
        case sf::Event::LostFocus:    // Window lost focus
          theState.Pause();
          break;
        case sf::Event::Resized:      // Window resized
          break;
        default:                      // Current active state will handle
          theState.HandleEvents(anEvent);
      } // switch(anEvent.Type)
    } // while(mWindow.GetEvent(anEvent))
  }
Beispiel #15
0
/*
 ISoundEngine * StateMachine::getSoundEngine()
 {
 return m_pSndEngine;
 }
 */
irr::u32 StateMachine::run() {
	bool bQuit, bSettings = false;
	s32 lastFPS = -1;
	m_bGraphicsChanged = false;
	//load device from setting files

//#ifdef WIN32
//	SettingHandler *setup = new SettingHandler("asset/conf/Device.xml");
//	m_pDevice = setup->createDeviceFromSettings();
//	delete setup;
//#else 
	m_pDevice = createDevice(video::EDT_OPENGL,
			dimension2d<u32>(SCREEN_WIDTH, SCREEN_HEIGHT), 16, false, false,
			false, 0); //setup->createDeviceFromSettings();
//#endif

	m_pDevice->setWindowCaption(L"PinBob");
	//main loop
	do {
		m_pDevice->setWindowCaption(L"PinBob");
		initStates(m_pDevice);
		m_bGraphicsChanged = false;
		bQuit = false;

		//This loop will run until the game is quit or the graphics mode is to be re-inited
		while (m_pDevice->run() && !bQuit && !m_bGraphicsChanged) {
			//m_bDrawScene is "false" if the active state wants to do all the drawing stuff on it's own
			//if the active state wants to do all the drawing stuff,just set call" setDrawScene(false)"
			if (m_bDrawScene && m_pDevice->isWindowActive()) {
				m_pDriver->beginScene(true, true, SColor(0, 200, 200, 200));
				m_pSmgr->drawAll();
				m_pGuienv->drawAll();
				m_pDriver->endScene();
			}
			s32 fps = m_pDriver->getFPS();

			if (lastFPS != fps) {
				m_iFps = fps;
				core::stringw str = L"Dancy [";
				str += m_pDriver->getName();
				str += "] FPS:";
				str += fps;

				m_pDevice->setWindowCaption(str.c_str());
				lastFPS = fps;
			}

			u32 iRet = m_pActive ? m_pActive->update() : 0;

			//a return value of "0" from the active state shows that no state change is made
			if (iRet) {
				//the index of the state to be changed to is the return value -1 (remember: "0" means "no change")
				iRet--;
				//an out of bounds index is interpreted as "quit program" signal
				if (iRet < m_aStates.size()) {
					printf("Return state number is %d.\n", iRet);
					//get the next state
					IState *pNext = m_aStates[iRet];
					//deactivate the current state
					m_pActive->deactivate(pNext);
					//activate the next state
					pNext->activate(m_pActive);
					//save config file on each state change
					//ConfigFileManager::getSharedInstance()->writeConfig(m_pDevice,"asset/conf/SettingManager.xml");
					m_pActive = pNext;
				} else
					bQuit = true;
			}
		}
		if (m_pActive) {
			m_pActive->deactivate(NULL);
		}
		clearStates();
		//CShadowManager::getSharedInstance()->clear();
		m_pSmgr->getMeshCache()->clear();
		m_pDevice->closeDevice();
		m_pDevice->drop();
	} while (m_bGraphicsChanged);
	return 0;
}
 void attack() { state->attack(); }
 void run()    { state->run(); }
Beispiel #18
0
  void StateManager::dropActiveState(void)
  {
    // Is there no currently active state to drop?
    if(!mStack.empty())
    {
      // Retrieve the currently active state
      IState* anState = mStack.back();

      // Log the dropping of an active state
      ILOG() << "StateManager::dropActiveState(" << anState->getID() << ")" << std::endl;

      // Pause the currently active state
      anState->pause();

      // Deinit currently active state before we pop it off the stack
      // (handleCleanup() will be called by IState::doInit() method if this
      // state is ever set active again)
      anState->deInit();

      // Pop the currently active state off the stack
      mStack.pop_back();

      // Move this now inactive state to the absolute back of our stack
      mStack.insert(mStack.begin(), anState);

      // Don't keep pointers around we don't need anymore
      anState = NULL;
    }
    else
    {
      // Quit the application with an error status response
      if(NULL != mApp)
      {
        mApp->quit(StatusAppStackEmpty);
      }
      return;
    }

    // Is there another state to activate? then call Resume to activate it
    if(!mStack.empty())
    {
      // Has this state ever been initialized?
      if(mStack.back()->isInitComplete())
      {
        // Resume the new active state
        mStack.back()->resume();
      }
      else
      {
        // Initialize the new active state
        mStack.back()->doInit();
      }
    }
    else
    {
      // There are no states on the stack, exit the program
      if(NULL != mApp)
      {
        mApp->quit(StatusAppOK);
      }
    }
  }