Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 roby65   Members   -  Reputation: 154

Like
0Likes
Like

Posted 12 June 2011 - 03:48 AM

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;


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

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9110

Like
0Likes
Like

Posted 12 June 2011 - 03:51 AM

You might want to read this article.

#3 Hodgman   Moderators   -  Reputation: 23953

Like
0Likes
Like

Posted 12 June 2011 - 03:57 AM

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;

}


#4 scgames   Members   -  Reputation: 1961

Like
0Likes
Like

Posted 12 June 2011 - 09:12 AM

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

#5 phantom   Moderators   -  Reputation: 5712

Like
0Likes
Like

Posted 12 June 2011 - 09:15 AM

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.

#6 roby65   Members   -  Reputation: 154

Like
0Likes
Like

Posted 12 June 2011 - 10:59 AM

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],i);
anim.SetDelay(Delay,i);
}
newid=m_Animations.size();
m_Animations.insert(std::pair<uint, cAnimation>(newid,anim));

return newid;
}


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

EDIT: Maybe i should switch to pointers, right?

#7 phantom   Moderators   -  Reputation: 5712

Like
0Likes
Like

Posted 12 June 2011 - 12:39 PM

Really? Why do animation and texture managers need to be linked?

#8 ApochPiQ   Moderators   -  Reputation: 12343

Like
0Likes
Like

Posted 12 June 2011 - 10:45 PM

Post the complete code and indicate exactly what line is associated with the error.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS