• Advertisement
Sign in to follow this  

[C++]2D Game engine, global objects problem

This topic is 2414 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys,

i'm making my own 2d engine using sdl & opengl, but i need some "suggestions"...

I have everything declared using classes, but some classes needs other classes, for example cAnimation needs cTextureManager, how to accomplish this?
In practice, i need to have some classess accessible from others, but i can't understand how to make this.
I tried doing something like this:



class cTextureManager
{
[.....]
} g_TextureManager;

[/QUOTE]

but naturally it gives error "class cTextureManager g_TextureManager" (?g_TextureManager@@3VcTextureManager@@A) already defined in CTextureManager.obj"

Can somebody explain me how to do that?

Roby

Share this post


Link to post
Share on other sites
Advertisement
Don't use globals; pass references.int main(...)
{
cTextureManager textureManager;
cAnimationManager( textureManager );
...
}

class cAnimationManager
{
public:
cAnimationManager( cTextureManager& tm ) : m_textureManager(tm) {}
private:
cTextureManager& m_textureManager;
}

Share this post


Link to post
Share on other sites
Also, consider dropping the 'c' prefix for your class names (it doesn't really add any useful information, and just clutters the code, IMO).

Share this post


Link to post
Share on other sites
Also consider deeply WHY the animation manager needs access to the texture manager and if you can decouple it in anyway; apply this logic to all systems where you think there should be a link between them, chances are in many many cases you won't really need a link.

Share this post


Link to post
Share on other sites
Thanks for the replies!
I tried to think how not to link together animation and texture manager but they are indeed linked and i cannot do in other ways.

Using the first method i changed my code, but now i'm blocked again, and i can't understand how to fix this error:


uint cAnimationManager::CreateAnimation( std::vector< uint > Frames, Uint32 Delay)
{
int newid=0;
cAnimation anim(m_TextureManager);
for(int i=0;i<Frames.size();i++)
{
anim.SetTexture(Frames,i);
anim.SetDelay(Delay,i);
}
newid=m_Animations.size();
m_Animations.insert(std::pair<uint, cAnimation>(newid,anim));

return newid;
}
[/QUOTE]

I get a "no appropriate default constructor avaible" error, how to fix that?

EDIT: Maybe i should switch to pointers, right?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement