Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Multiple views/cameras

This thread is locked; no one can reply to it. rss feed Print
Multiple views/cameras
Endgegner
Member #15,006
March 2013
avatar

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
Member #476
June 2000
avatar

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.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Kris Asick
Member #1,424
July 2001

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.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Endgegner
Member #15,006
March 2013
avatar

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.

Go to: