C++, SDL and Inheritance question.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
epicasian
Chaos Rift Junior
Chaos Rift Junior
Posts: 232
Joined: Mon Feb 22, 2010 10:32 pm
Current Project: Gigazilla Engine
Favorite Gaming Platforms: Dreamcast, SNES, PS2, PC
Programming Language of Choice: C/++
Location: WoFo, KY

C++, SDL and Inheritance question.

Post 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
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: C++, SDL and Inheritance question.

Post 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 };
}
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: C++, SDL and Inheritance question.

Post by Ginto8 »

Also, rather than naming everything tnt_*, you should look into namespaces for your library/engine.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
epicasian
Chaos Rift Junior
Chaos Rift Junior
Posts: 232
Joined: Mon Feb 22, 2010 10:32 pm
Current Project: Gigazilla Engine
Favorite Gaming Platforms: Dreamcast, SNES, PS2, PC
Programming Language of Choice: C/++
Location: WoFo, KY

Re: C++, SDL and Inheritance question.

Post 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
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: C++, SDL and Inheritance question.

Post 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
User avatar
epicasian
Chaos Rift Junior
Chaos Rift Junior
Posts: 232
Joined: Mon Feb 22, 2010 10:32 pm
Current Project: Gigazilla Engine
Favorite Gaming Platforms: Dreamcast, SNES, PS2, PC
Programming Language of Choice: C/++
Location: WoFo, KY

Re: C++, SDL and Inheritance question.

Post 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
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: C++, SDL and Inheritance question.

Post 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.
Post Reply