trouble with inheritance and static class members

closed account (zwA4jE8b)
Hey guys, I am at the limits of my knowledge of classes and inheritance and could use some help.

I have a hud (heads up display) class that I would like player one and the npc characters to inherit so a life bar can be displayed. The relevent code is below.

I made the life_bar_status variable static because they are either all on or all off. player_one's life bar never shows up, but the npc's do.

http://www.mediafire.com/?qkaah71i3wd7e (link to the full source, in src.zip)


in hud.h
1
2
3
4
5
6
7
8
9
10
11
12
13
class char_hud
{
	protected:
		SDL_Rect gen_life_bar;
		float life_bar_ratio;
		Uint32 color;

	public:
		static bool life_bar_status;
		char_hud();
		void update(int x, int y, int health);
		void show(SDL_Surface* screen);
};


in hud.cpp
1
2
3
4
5
6
7
8
9
10
11
12
bool char_hud::life_bar_status;

char_hud::char_hud(){gen_life_bar.h = LIFE_BAR_HEIGHT;}

void char_hud::update(int x, int y, int health)
{
	gen_life_bar.x = x;
	gen_life_bar.y = y - 10;
	gen_life_bar.w = health * life_bar_ratio;
}

void char_hud::show(SDL_Surface* screen){SDL_FillRect(screen, &gen_life_bar, color);}


in objects.h
1
2
3
4
5
6
7
class game_objects: public char_hud
{
	protected:
..............

class player_one: public game_objects
{


in objects.cpp
1
2
3
4
5
6
7
8
9
10
11
(in objects constructor)
this->life_bar_ratio = this->frame_width / this->health;
this->color = 0x00FF00;

(in objects update func)
if(life_bar_status)
		this->char_hud::update(position.x_pos, position.y_pos, health);

(in objects show func)
if(life_bar_status)
		this->char_hud::show(screen);


in gamengine.cpp
1
2
3
4
5
6
7
(in the init func)
char_hud::life_bar_status = false;
spawn_character('p', 'r');

(in the input func)
if(event.key.keysym.sym == SDLK_l)
				char_hud::life_bar_status = !life_bar_status;


I understand if no one wants to go through all my code to debug it, but if you I appreciate it.

And any advice about anything wrong about what I am doing will be great.
Last edited on
I can't debug it now because I have to go to work (and I'd have to get SDL), but I'll try to give it a spin when I get home if I remember. I don't immediately see why the player's hud wouldn't be drawing. I can only suspect that it IS drawing, but just at the wrong coordinates (so maybe it's being drawn offscreen or something?)


What I did notice, though, is that you are using inheritance incorrectly.

Public inheritance forms an "is a" relationship. For example, class Cat might inherit from class Animal because a Cat "is a" Animal. player_one is not a char_hud, so it should not be inheriting from it.

If you want a "has a" relationship (ie: player_one "has a" char_hud), then you want composition:

1
2
3
4
5
6
7
8
9
10
11
12
class player_one
{
private:
  char_hud  myhud;

public:
  void Display()
  {
    // ..
    myhud.Display();
  }
};



Also, if you find yourself naming things like "player_one", "player_two", etc, you're probably doing it wrong. All players should use the same code, so a class just named "player" would probably be more appropriate.
closed account (zwA4jE8b)
I changed it so

1
2
3
4
5
6
7
8
9
10
class player_one: public game_objects
{
	float y_old;
	_shadow shadow;
	char_hud osd;

class npc: public game_objects
{
	_shadow shadow;
	char_hud osd;


and no longer inherit char_hud. but player_one's still does not show up.

am i setting life_bar_status correctly in gamengine.cpp. It works for the npc's just not player one.
Check the math that does the positioning of the bar.
closed account (zwA4jE8b)
did you notice something weird?

in hud.cpp
1
2
3
4
5
6
7
8
char_hud::char_hud(){gen_life_bar.h = LIFE_BAR_HEIGHT;}

void char_hud::update(int x, int y, int health)
{
	gen_life_bar.x = x;
	gen_life_bar.y = y - 10;
	gen_life_bar.w = health * life_bar_ratio;
}


in objects.cpp
1
2
if(osd.life_bar_status)
	osd.update(position.x_pos, position.y_pos, health);


the osd.update is the same for both npc and player class.

I used to have everything in char_hud simply as part of the objects class but I wanted to separate it. it worked fine when the life bar was hard coded into the npc and player class.
Last edited on
Maybe they're stacked on top of each other?
closed account (zwA4jE8b)
no cuz all the enemies are correctly above the each separate enemy.

Im pretty sure it has to do with me not properly using the static bool.
Last edited on
Im pretty sure it has to do with me not properly using the static bool.


That's very unlikely.
closed account (zwA4jE8b)
hmm... i need to get a book. I reverted back to my old method of making the life bars apart of the objects themselves. I will learn more about classes and such then try again.
Topic archived. No new replies allowed.