Skip to content

Object Renderers

sppmacd edited this page Aug 25, 2021 · 3 revisions

THIS IS DEPRECATED!

In this tutorial you will learn how to use SceneObject renderers as an alternative way to render objects.

The SceneObject may have assigned the ObjectRenderer. It's an object which code can be shared between many objects. You must have exactly one renderer per SceneObject.

Textured Renderer

The basic object renderer is a TexturedRenderer2D. It renders an object using specific texture. You can create and assign this renderer using this code:

auto renderer = make<EGE::TexturedRenderer2D>(*sceneObject);
renderer->setTextureName("bird.png");
sceneObject->setRenderer(renderer);

This code creates a renderer, sets texture name for renderer (setTextureName) and assigns a renderer to scene object (setRenderer). In this example we use a bird.png texture located in default location, res:
TODO: image
If we use a texture, we need also to load it when game starts. For this purpose, use GUIResourceManager::registerTexture():

auto resourceManager = make<EGE::GUIResourceManager>();
// ...
resourceManager->registerTexture("bird.png");

If you run the code, you notice that the bird is very small and is not centered on screen. The scale we will discuss in later section, Adding Camera. To center the bird, you can just call renderer->center():

auto renderer = make<EGE::TexturedRenderer2D>(*sceneObject);
renderer->setTextureName("bird.png");
renderer->center();
sceneObject->setRenderer(renderer);

Custom Renderer

In many ways, the one type of object will be rendered in one way. But if we want to share some renderer code, we can create our custom ObjectRenderer:

class MyObjectRenderer : public EGE::ObjectRenderer
{
public:
    MyObjectRenderer(EGE::SceneObject2D& sceneObject)
    : EGE::ObjectRenderer(sceneObject) {}

    virtual void render(EGE::Renderer& renderer) const override;
};
void MyObjectRenderer::render(EGE::Renderer& renderer) const
{
    EGE::SceneObject2D& sceneObject = (EGE::SceneObject2D&)m_sceneObject;
    renderer.renderRectangle(sceneObject.getPosition().x - 5, sceneObject.getPosition().y - 5, 10, 10, sf::Color::Green, sf::Color::Red);
}

This code makes our renderer render a small rectangle with green fill and red outline at center of object. Also, in constructor we force using 2D SceneObjects as only them works with this renderer. To use your renderer with object, again use setRenderer():

We can also use textures. Just get a texture from ResourceManager using ResourceManager::getTexture(). (...)

void MyObjectRenderer::render(EGE::Renderer& renderer) const
{
    EGE::SceneObject2D& sceneObject = (EGE::SceneObject2D&)m_sceneObject;
    auto texture = sceneObject.getOwner()->getLoop()->getResourceManager()->getTexture("bird.png");
    renderer.renderTexturedRectangle(sceneObject.getPosition().x - 5, sceneObject.getPosition().y - 5, 10, 10, texture->getTexture());
}

The strange chain of calls (sceneObject.getOwner()->getLoop()->getResourceManager()->getTexture("bird.png");) means something like this:

Get Texture with name "bird.png" from Resource Manager of Game Loop of Scene to which the Scene Object is added

We must call getTexture() on texture because the texture may be an atlas texture, for which you will need to give atlas name of specified texture (but about that later).

If you want, you can read more about EGE::Renderer here.

Clone this wiki locally