Skip to content
Matt Styles edited this page Apr 25, 2019 · 12 revisions

Scenes are the central hub in xygine where logic and rendering meet. A scene encapsulates the ECS, event handling and rendering in a single class. When a new State is pushed on to the stack the first thing to do should be to initialise a Scene as a member of the state, by providing it with a list of Systems, Directors and Post Processes which will be used to control the scene. Each of which can be done using the following functions:

Scene::addSystem<T>();
Scene::addDirector<T>();
Scene::addPostProcess<T>();

It is important that these are set up first, because other classes, such as entities, depend on the scene being in a ready state. For more detailed information on each of the above see their respective pages:

Updating a Scene

Once a scene has been initialised it needs to be updated regularly every frame so that the active systems and directors receive messages, events and time based updates. This is usually done by forwarding the state's function parameters to the Scene, although this can be managed manually in cases such as running a dedicated server which does use a state stack.

bool MyState::handleEvent(const sf::Event& evt)
{
	m_scene.forwardEvent(evt);
	return true;
}

void MyState::handleMessage(const xy::Message& msg)
{
	m_scene.forwardMessage(msg);
}

bool MyState::update(float dt)
{
	m_scene.update(dt);
	return true;
}

The same is applicable to drawing a scene, which inherits the sf::Drawable interface and can be drawn as any SFML drawable:

void MyScene::draw()
{
	getContext().renderWindow.draw(m_scene);
}

Multiple scenes can exist at once, so it's trivial to separate in-game rendering from UI / HUD rendering for example. This may be the case when using a post process effect which should be applied only to the game rendering, but not the user interface. In such a case the game scene would be initialised with the required post process effect, and a second scene containing UI elements initialised without. The two scenes can then be updated then drawn one after another. Constructing both scenes with the same message bus instance means that messages are easily shared between them.

void MyScene::draw()
{
	auto& window = getContext().renderWindow;
	window.draw(m_gameScene);
	window.draw(m_UIScene);
}
Clone this wiki locally