Multiple views/cameras
Endgegner

Hello everybody,

I'm quite new to Allegro and just registered to the forums.

Here I come with my first question: In my 2D platformer game I would like to have multiple cameras (e.g. for split screens). I already got this working, but I'm not sure if my solution is good, so I'm asking for your advice.

I implemented the cameras like this (simplified):

class Camera {
    Vector2 position, origin, scale;
    float rotation;
    ALLEGRO_TRANSFORM transform;
    ALLEGRO_BITMAP *bitmap; // the render target for the camera
};

So basically each camera has its own bitmap where it renders to. All these camera bitmaps are rendered to the display backbuffer in a final step.

Is this a good approach? One minor drawback of this approach is that I have to resize/recreate all camera bitmaps (almost) whenever the display resolution changes. I also have to recreate the bitmaps in order to change the size (width/height) of a camera. Is there any better way of doing this?

Looking forward for your answers.

Thomas Fjellstrom

You could turn those bitmaps into subbitmaps, and skip the final `al_draw_bitmap`s.

Probably have lower overhead. Not sure you'll notice a change.

Kris Asick
Endgegner said:

One minor drawback of this approach is that I have to resize/recreate all camera bitmaps (almost) whenever the display resolution changes. I also have to recreate the bitmaps in order to change the size (width/height) of a camera.

One thought would be to have a couple XY values for the bounding box of each camera, then when it comes time to render a camera, simply render to the backbuffer after making a call to al_set_clipping_rectangle(). That way, you only have to recreate the backbuffer on each display change and not every camera's bitmaps, since they would no longer require one.

Endgegner

Thank you both for your input!

One thought would be to have a couple XY values for the bounding box of each camera, then when it comes time to render a camera, simply render to the backbuffer after making a call to al_set_clipping_rectangle(). That way, you only have to recreate the backbuffer on each display change and not every camera's bitmaps, since they would no longer require one.

This works great, thanks. I have tried that before, but unfortunately I forgot to reset the clipping rectangle to the target bitmap size and this caused some rendering glitches. That was a rookie mistake, I guess. :)

Just for the record, here is the solution:

These are the modified camera members:

Vector2 position, origin, scale;
float rotation;
ALLEGRO_TRANSFORM transform;
IntRectangle clipping;

And this is the rendering code:

IntRectangle clipping;

al_get_clipping_rectangle(&clipping.x, &clipping.y, &clipping.width, &clipping.height):
al_clear_to_color(al_map_rgb(0, 0, 0));

for (auto camera : cameras)
{
    al_use_transform(&camera.transform);
    al_set_clipping_rectangle(camera.clipping.x, camera.clipping.y,
                              camera.clipping.width, camera.clipping.height);
    // Draw the bitmaps ...
}

al_set_clipping_rectangle(clipping.x, clipping.y, clipping.width, clipping.height):

Problem solved.

Thread #612235. Printed from Allegro.cc