View Full Version : ClanLib and Graphic Layers
jmguy02
04-01-2009, 03:56 AM
How does, or can, ClanLib deal with layers when using graphics? (e.g. Layer1 = background.jpg, Layer2 = pic1.jpg, etc...)
rombust
04-01-2009, 06:55 AM
If i'm understanding you correctly.
We layer graphics using the alpha channel of the image to control the opacity.
See ClanLib[0.8][0.9]/Examples/ShadowOfTheBeast
jmguy02
04-01-2009, 02:35 PM
Opacity adjustment isn't exactly what I was thinking of but I could sort of see where that might work to an extent. What I was thinking was more akin to layers in a graphic tool like Corel or Photoshop...
For example: if you had a background image then you had buttons on top of that and then there might be a window that could pop up above the background and the buttons. In that instance if every object had an alpha of 255 how do you know what object shows on top of what? Layers allow you to specify the stacking order. So if an object is above (higher layer) another object then it would cover it.
After thinking about adjusting opacity more.......I suppose that would work and code can just modify opacity on the fly. I might be able to create a routine to organize that in layers too using the opacity adjustment. That must be how it's done at a lower level than I'm accustom to. Thanks for your help.
jmguy02
04-01-2009, 04:49 PM
I found Alan Thorne's page where he was interested in the same application where he needed layers. Here's the link in case anyone runs across this need.
http://www.alanthorn.net/clanlib/
sphair
04-02-2009, 07:11 AM
The "layers" is basically the draw order in clanlib.
Lets say each of these functions draw the wanted elements, using CL_Sprites or whatever:
DrawBackground();
DrawButtons();
DrawWindow();
The order you call this is the order they appear on screen.
If you want a specific layer system, isn't that just a std::vector<> of for instance CL_Sprites you want to draw on that layer?
A rough sketch coded in a hurry:
class SpriteElement
{
CL_Sprite sprite;
CL_Point position;
void Draw(CL_GraphicContext &gc)
{
sprite.draw(position.x, position.y, gc);
}
}
class Layer
{
std::vector<SpriteElement *> sprites;
void Draw(CL_GraphicContext &gc)
{
for(int i=0; i<sprites.size(); ++i)
{
sprites[i]->draw(gc);
}
}
}
class LayerManager
{
std::vector<Layer> layers;
void Draw(CL_GraphicContext &gc)
{
for(int i=0; i<layers.size(); ++i)
{
layers[i]->draw(gc);
}
}
}
catch22
04-07-2009, 08:16 PM
As Sphair pointed out, you control your own drawing order.
I used SpriteWorld back in my Mac coding days, and it had an elaborated sprite layering system -- which was ultimately just a linked list of things to draw in a particular order. But SW was C only, so it was useful then. With C++, it's not really necessary.
It's very easy and manageable to do yourself. I use a "container" class which is mainly just a vector (or simple list) with some methods for updating and drawing each object in the container. Just call them in the order you want them drawn (farthest layer first), and that's it.
Now if you're talking Y-sorting (or z-sorting), it's a little different, but still not too complex.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.