Example #1
0
/**
 * Resets the status of all the Surface child elements,
 * like unpressing buttons.
 */
void State::resetAll()
{
	for (std::vector<Surface*>::iterator i = _surfaces.begin(); i != _surfaces.end(); ++i)
	{
		InteractiveSurface *s = dynamic_cast<InteractiveSurface*>(*i);
		if (s != 0)
		{
			s->unpress(this);
		}
	}
}
Example #2
0
/**
 * The state machine takes care of passing all the events from SDL to the
 * active state, running any code within and blitting all the states and
 * cursor to the screen. This is run indefinitely until the game quits.
 */
void Game::run()
{
	while (!_quit)
	{
		// Clean up states
		while (!_deleted.empty())
		{
			delete _deleted.back();
			_deleted.pop_back();
		}		

		// Initialize active state
		if (!_init)
		{
			_states.back()->init();
			_init = true;

			// Unpress buttons
			for (std::vector<Surface*>::iterator i = _states.back()->getSurfaces()->begin(); i < _states.back()->getSurfaces()->end(); i++)
			{
				InteractiveSurface *s = dynamic_cast<InteractiveSurface*>(*i);
				if (s != 0)
				{
					s->unpress(_states.back());
				}
			}

			// Refresh mouse position
			SDL_Event ev;
			int x, y;
			SDL_GetMouseState(&x, &y);
			ev.type = SDL_MOUSEMOTION;
			ev.motion.x = x;
			ev.motion.y = y;
			Action action = Action(&ev, _screen->getXScale(), _screen->getYScale());
			_states.back()->handle(&action);
		}

		// Process events
		while (SDL_PollEvent(&_event))
		{
			if (_event.type == SDL_QUIT)
			{
				_quit = true;
			}
			else
			{
				Action action = Action(&_event, _screen->getXScale(), _screen->getYScale());
				_screen->handle(&action);
				_cursor->handle(&action);
				_fpsCounter->handle(&action);
				_states.back()->handle(&action);
			}
		}
		
		// Process logic
		_fpsCounter->think();
		_states.back()->think();
		
		// Process rendering
		if (_init)
		{
			_screen->clear();
			std::list<State*>::iterator i = _states.end();
			do
			{
				i--;
			}
			while(i != _states.begin() && !(*i)->isScreen());

			for (; i != _states.end(); i++)
			{
				(*i)->blit();
			}
			_fpsCounter->blit(_screen->getSurface());
			_cursor->blit(_screen->getSurface());
		}
		_screen->flip();

		SDL_Delay(0);
	}
}