Page 1 of 1

C++, SDL and Inheritance question.

Posted: Tue Jun 08, 2010 12:13 am
by epicasian
Okay, I'm stumped. I have a class named tnt_SpriteAnimation which derives from the class tnt_Sprite. In main, I create a new instance of tnt_SpriteAnimation (called test), which in the constructor, takes a file name argument, width and height. When I try to draw the sprite to the screen, I get nothing. However, if I change the instance "test" to a tnt_Sprite, I see the sprite sheet rendered to the screen. Why if the type is tnt_Sprite the sprite sheet is rendered and not rendered if the type is tnt_SpriteAnimation?

All help is greatly appreciated,
~EpicAsian

Re: C++, SDL and Inheritance question.

Posted: Tue Jun 08, 2010 1:45 am
by GroundUpEngine
Do you have virtual functions in your base class? because they could of been overwritten e.g.

Code: Select all

class tnt_Sprite {
  virtual void render() { // render, render, render };
}

class tnt_SpriteAnimation : public tnt_Sprite {
  void render() { // nothing };
}

Re: C++, SDL and Inheritance question.

Posted: Tue Jun 08, 2010 5:47 am
by Ginto8
Also, rather than naming everything tnt_*, you should look into namespaces for your library/engine.

Re: C++, SDL and Inheritance question.

Posted: Tue Jun 08, 2010 10:12 am
by epicasian
GroundUpEngine wrote:Do you have virtual functions in your base class? because they could of been overwritten e.g.

Code: Select all

class tnt_Sprite {
  virtual void render() { // render, render, render };
}

class tnt_SpriteAnimation : public tnt_Sprite {
  void render() { // nothing };
}

Thank you, GroundUpEngine, I knew I mus have missed something obvious (again). That usually happens when I'm tired :lol:.
Ginto8 wrote:Also, rather than naming everything tnt_*, you should look into namespaces for your library/engine.
I never really though of using namespaces, I'll look into it.

Thanks for all the help,
~EpicAsian

Re: C++, SDL and Inheritance question.

Posted: Tue Jun 08, 2010 4:20 pm
by GroundUpEngine
No prob :) namespaces?

Code: Select all

namespace tnt
{
  class Sprite {
    // stuff
  };
}
main.cpp

Code: Select all

using namespace tnt;
Sprite playa; // Now I can use tnt::Sprite

Re: C++, SDL and Inheritance question.

Posted: Tue Jun 08, 2010 4:39 pm
by epicasian
GroundUpEngine wrote:No prob :) namespaces?

Code: Select all

namespace tnt
{
  class Sprite {
    // stuff
  };
}
main.cpp

Code: Select all

using namespace tnt;
Sprite playa; // Now I can use tnt::Sprite
I know what namepaces are, I just never really thought about using them with the engine. :D

~EpicAsian

Re: C++, SDL and Inheritance question.

Posted: Wed Jun 09, 2010 8:51 am
by Falco Girgis
epicasian wrote:
GroundUpEngine wrote:No prob :) namespaces?

Code: Select all

namespace tnt
{
  class Sprite {
    // stuff
  };
}
main.cpp

Code: Select all

using namespace tnt;
Sprite playa; // Now I can use tnt::Sprite
I know what namepaces are, I just never really thought about using them with the engine. :D

~EpicAsian
Well you should.

The entire reason C++ introduced namespaces was because every C library appended its own little prefix to all of their global function calls. OpenGL:

Code: Select all

glSOMESHIT();
SDL:

Code: Select all

SDL_SOMESHIT()
A namespace is a much prettier, cleaner way of grouping together symbols within a single project/library/api.