[asl]manage layer

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
mataanjin
Posts: 19
Joined: Tue May 25, 2010 3:07 am

[asl]manage layer

Post by mataanjin »

what is the best way to manage layers?

i have layer, such as:
MainLayer
OptionLayer
GameLayer
and so on.

my current code is

Code: Select all

	Layer_ID layerID;
	layerID = UTAMA;

	while(true)
	{
			switch(layerID)
		{
		case UTAMA:
			{
			MenuUtama *layer = new MenuUtama();
			layer->init(device, soundEngine);
			layerID = layer->update();
			delete layer;
			}
			break;
		case BUY:
			{
			Buy *layer = new Buy();
			layer->init(device, soundEngine, player);
			layerID = layer->update();
			delete layer;
			}
			break;
		case TIME_TRIAL:
			{
			TimeTrial *layer = new TimeTrial();
			layer->init(device, soundEngine, player);
			layerID = layer->update();
			delete layer;
			}
			break;
		case GAME:
			{
			Game *layer = new Game(device, soundEngine, player);
			layer->init();
			layerID = layer->update();
			delete layer;
			}
			break;
		default:
			//layerID = UTAMA;
			break;
		}

		// Quit when layer id is QUIT
		if (layerID == QUIT)
			break;
	}

	device->drop();
that's compiled fine but i'm unable to debug it, seems like a memory leak or something.
on release i can run it.

each layer->update() will have an infinite loop, until the EventReceiver change the layer id.

the update for my MainLayer is like this:

Code: Select all

	u32 last = device->getTimer()->getTime();

	while(true)
	{
		device->run();

		u32 now = device->getTimer()->getTime();
		if(now - last >= 8)
		{
			driver->beginScene(true, true, BGColor);

			env->drawAll();

			driver->endScene();

			if( input->getLayer() != layerID )
				return input->getLayer();
		}
	}
so how to do it the right way?
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

Code: Select all

if( input->getLayer() != layerID ) 
            return input->getLayer(); 
the return thing is really suspicious in main loop.

as for:
what is the best way to manage layers?
you can use this way as long as you know what you're doing. Also you could build a state(layer) manager which stores the layers, and updates the current layer.

Search for State manager or state machine in forums and you will find a few ways on how to do this.
Working on game: Marrbles (Currently stopped).
mataanjin
Posts: 19
Joined: Tue May 25, 2010 3:07 am

Post by mataanjin »

serengeor wrote:

Code: Select all

if( input->getLayer() != layerID ) 
            return input->getLayer(); 
the return thing is really suspicious in main loop.

as for:
what is the best way to manage layers?
you can use this way as long as you know what you're doing. Also you could build a state(layer) manager which stores the layers, and updates the current layer.

Search for State manager or state machine in forums and you will find a few ways on how to do this.
the return is of Layer_ID type,
so when i press button play, it will return PLAY
something like that.

i don't know why, but it seems the loop is not stopped at

Code: Select all

layerID = layer->update();
and wait until the layer is changed(returned)

instead it continued down.


i'll see using a state machine later.
TQ
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

if that if succeeds then the app would quit with a non 0 exit code, unless getLayer returns 0.

You should consider using some other way on implementing layers. You could make a abstract base layer class, which you could use to store your derived layers into some manager class. That manager would be responsible for changing, updating, adding/registering those layers.

A base layer class for example could look like:

Code: Select all

class ILayer
{
public:
     ILayer(LayerManager* manager):m_layerManager(manager){}
     virtual void onInit()=0;
     virtual void onUpdate()=0;
     virtual void onExit()=0;
protected:
    ///...
    LayerManager* m_layerManager;
}
Then you could derive from it your specific layers:

Code: Select all

class GameLayer: public ILayer
{
public:
     GameLayer(LayerManager* manager):ILayer(manager){}
     virtual void onInit(){printf("Initializing game state\n");};
     virtual void onUpdate(){printf("Updating game state\n");}
     virtual void onExit(){printf("Exiting from game state\n");};
protected:
    ///...
}
as for the LayerManager:

Code: Select all

class LayerManager
{
public:
    bool addLayer(const std::string & name, ILayer * layer){///add the layer to std::map, return false if layer is already there?(though may be unneeded as there won't be many layers.)}
    void switchCurrentLayer(const std::string & name){///find a layer with the name and change it with a current layer, call onExit on current layer and onInit on the new layer
///also check if current layer is valid before calling onExit }
    void updateCurrentLayer(){///update the current layer here}
    
protected:
    ILayer * m_currentLayer; ///current layer set.
    std::map<std::string, ILayer*> m_layers;/// store all added layers here.
};
You will of course have to modify this, but it should give you a start, if you think this method is good for you.
Working on game: Marrbles (Currently stopped).
Post Reply