Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5] 2D Screen Layers using OpenGL orthographic

This thread is locked; no one can reply to it. rss feed Print
[A5] 2D Screen Layers using OpenGL orthographic
Renato Riolino
Member #12,870
May 2011

Hi,

I want to make a library for use in my 2D games to abstract the notion of screen layers. Eg:

Put bitmap1 (10x10) on screen position 0x0
Put bitmap2 (5x5) above bitmap1 on position 0x0

If the bitmap1 changes, I need to redraw it and after redraw bitmap2 too.

Since A5 runs on top of OpenGL I thought about using OpenGL Z axis orthographic as a layer system.

I'd just put the bitmaps on their X and Y position but each bitmap on its own unique Z position and the camera showing them all. If a bitmap on a lower Z axis changes, OpenGL would do all the hard working on calculating and redrawing everyone above it.

Anyone has done it already? Or is there a better way to achieve the same effect?

Thanks

Chris Katko
Member #1,881
January 2002
avatar

I'm not sure if you'd really need to do something like that, but all you need to do is: sort all drawing operations by their z-depth before drawing them.

Unless you're doing something truly far out there, there's no reason you can't update the entire screen every frame. Graphics are 10,000 times faster than they ever were in the 90's. Saving screen real-estate with dirty rectangles just isn't important anymore for games and completely falls apart and explodes if you're doing lots of transparent/blending effects.

Am I misunderstanding what you're trying to accomplish?

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Renato Riolino
Member #12,870
May 2011

Hi Chris,

Since A5 is hardware accelerated I have improved my games with much more animations. The game runs at full hd with a lot of transparency animations (one PNG with alpha for each frame) and drawing all the visible ones on every screen refresh is to slow (1 fps or less).

Doing some simple checks like redrawing just the area where the animation changed improved a lot, but I had to do all this hard work by myself (if the bitmap that changed has transparency, I have first to redraw the bitmap below and if the bitmap below have transparence too I have to do the same with the below and so on...).

Thats why I thought of using OpenGL for it. I would have just to put every bitmap on its own Z-depth position and OpenGL would do all the drawing hardware accelarated.
But I don't know if this is how Allegro/OpenGL works at all, so this may be unduable.

I have zero experience with 3d programming.

Thanks

Arthur Kalliokoski
Second in Command
February 2005
avatar

if the bitmap that changed has transparency, I have first to redraw the bitmap below and if the bitmap below have transparence too I have to do the same with the below and so on

Just draw all the opaque things first, then draw the transparent bitmaps with al_map_rgba() with the a (alpha) set to maybe 0.5 and it'll allow you to see the others below it automagically.

They all watch too much MSNBC... they get ideas.

Renato Riolino
Member #12,870
May 2011

Arthur,

Doing this way is very slow in my case (because I have lots of high resolution bitmaps). I'm trying to improve speed by redrawing only what changed and that's why I thought of using OpenGL z-axis as a "layer" system.

For example:

Imagine 3 rectangle bitmaps of 100x100 pixels (B1, B2 and B3). B1 and B2 are opaque while B3 have at least one pixel transparent (see the image).

609088

B1 is on 50,0,0 (x,y,z), B2 is on 50,100,3 and B3 is on 0,50,2.

If B1 changes, after redrawing it I have to redraw B3 where it intersects with B1 (50,0 to 100,50 of B3).

If B2 changes, I don't need to redraw B3 because besides they are intersecting, B3 is behind B2 and B2 is opaque.

But if B3 changes, I need first to redraw B1 (0,50 to 50,100) then B3 and finally readraw B2 (0,0 to 50,50).

English is not my primary language so I don't know if I'm been clear to explain, but this is the kind optimization I'm trying to do.

Of course on the above example is just easier to redraw those 3 bitmaps entirely just on the correct order, but in a real game I have dozens or hundreds of bitmaps moving and changing not always in sync and a lot of them with alpha. Redrawing everything is to damn slow.

[]'s

Renato

Arthur Kalliokoski
Second in Command
February 2005
avatar

I have dozens or hundreds

So what. Are you loading the bitmaps after setting the screen mode, so they textures are in the video card instead of regular RAM?

They all watch too much MSNBC... they get ideas.

Renato Riolino
Member #12,870
May 2011

Yes, I am. Most of the bitmaps are loaded on the video ram and others that are not used all the time are loaded from disk on the fly when needed, because they don't fit on video ram.

At the moment I have 2 giga of PNGs.

Arthur Kalliokoski
Second in Command
February 2005
avatar

I have 2 giga of PNGs

I'd say you need to step back and reconsider a better way to make this game.

They all watch too much MSNBC... they get ideas.

Renato Riolino
Member #12,870
May 2011

Doing the kind of optimization I explained on the example worked great for me with A4. Just porting that library to A5 with some minor adjust improved a lot the overall performance.

My game is running at 1920x1080 with 30 fps most of the time.

So I think I don't need to reconsider it.

I was just hoping that the 3d engine of allegro could already do this kind of optimization automatically for me and then instead of optimizing my A4 library for the Allegro 5 I could just write a new one to bypass all the hard work to the opengl (positioning the bitmaps on differents Z axis to simulate the layers).

Well, I think it can't be done then I'll finish the porting.

Thank you

Chris Katko
Member #1,881
January 2002
avatar

At the moment I have 2 giga of PNGs.

How much of those are you shooting to the screen at a given moment? Have you considered how much memory bandwidth 1920x1080x32 @ 30 FPS with NO redrawing costs?

Is this a game, or a research experiment? Because if this is a "game", to a developer, that's one of those red flags that shoot up that tells us you might be doing something really backwards.

Please give us more detail of what your game is, and what your trying to accomplish. Perhaps a screenshot?

allegro could already do this kind of optimization automatically for me

It does a lot of things automatically when you use it the way it's intended!

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Thomas Fjellstrom
Member #476
June 2000
avatar

Are you trying to double-buffer like is suggested with Allegro 4? Because that tends to make things slower with Allegro 5.

Basically, you should be able to just blast all of your drawing at the screen, every frame, and still have a ton of headroom left over.

If you have to load new graphics every frame, that could be a potential bottleneck, I'd suggest against that :(

--
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

Go to: