Exemple #1
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
        }
        
    }
Exemple #2
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))
  }