<SOLVED> Inheritance and Pointers

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
Maevik
Chaos Rift Junior
Chaos Rift Junior
Posts: 230
Joined: Mon Mar 02, 2009 3:22 pm
Current Project: www.keedepictions.com/Pewpew/
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: Long Beach, CA

<SOLVED> Inheritance and Pointers

Post by Maevik »

I'm and encountering an exception anytime I try to access an SDL_Surface* from a derived class. I have a console class:

Code: Select all

class Console: public Object
{      ////////////Only displaying members which may be relevant///////////////////
	SDL_Surface* Frame;
	vector<SDL_Surface*> textLines;
	TTF_Font* font;

public:
	Console(void);
	void buildFrame();
	SDL_Surface* getImage();
};
void Console::buildFrame()  //Called by constructor.  Made it a function so I could use it later when, say, the resolution was changed
{
	SDL_Rect temp;
	Uint32 color;

	// Build the background
	temp.h = SDL_GetVideoSurface()->h;
	temp.w = SDL_GetVideoSurface()->w;
	temp.x = 0;
	temp.w = 0;
	color = SDL_MapRGB( Frame->format , CONSOLE_BACKGROUND.r , CONSOLE_BACKGROUND.g , CONSOLE_BACKGROUND.b );
	SDL_FillRect( Frame , &temp , color );

	// Build the outline
	temp.w = 4;
	color = SDL_MapRGB( Frame->format , CONSOLE_FRAME.r , CONSOLE_FRAME.g , CONSOLE_FRAME.b );
	SDL_FillRect( Frame , &temp , color );
	
	temp.x = Frame->w - temp.w;
	SDL_FillRect( Frame , &temp , color );

	temp.w = Frame->w;
	temp.x = 0;
	temp.h = 4;
	temp.y = Frame->h - 4 - 4 - 14;
	SDL_FillRect( Frame , &temp , color );

	temp.y = Frame->h - 4;
	SDL_FillRect( Frame , &temp , color );

	Object::image = Frame;
	Object::rect.x = 0;
	Object::rect.y = 0;
	Object::rect.w = Frame->w;
	Object::rect.h = Frame->h;
	
}
SDL_Surface* Console::getImage()
{
	if( Frame != NULL )
		return Frame;
	else
		return NULL;
}
And then I have the base class Object:

Code: Select all

class Object
{
	string imageName;
	bool solid;
	SDL_Rect collisionRect;

protected:
	SDL_Rect rect;
	SDL_Surface* image;			/*	Only used for Objects who cannot pre load their
									images, IE: The Console							*/

public:
	Object(void);
	Object( int x , int y , const string& imageName = "" , int w = 0 , int h = 0 );
	Object( int x , int y , int w = 0 , int h = 0 );
	~Object(void);
	void calculateMidpoint();
	string* getImageName();
	void setImageName( const string& newImageName );
	SDL_Rect* getRect() ;
	SDL_Surface* getSurface() ;
};
Basically, anytime my program tries to access myConsoleInstance->Frame , myConsoleInstance->image , myConsoleInstance->getRect()->x or anything like that I get an exception. I'm not sure what I'm doing wrong. Any help would be appreciated :D
Last edited by Maevik on Mon Mar 01, 2010 1:14 pm, edited 1 time in total.
My love is like a Haddoken, it's downright fierce!
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Inheritance and Pointers

Post by short »

Well..... this may help... but you can make your own decisions about whether or not making your classes friends is a good idea:
(note: I have not tested this, I'm to tired atm to even tell you if they both have to be friends of each other or just one to the other. Good luck, maybe in the morning I'll be able to help more :)
Maevik wrote:

Code: Select all

class Console: public Object
{      ////////////Only displaying members which may be relevant///////////////////
	SDL_Surface* Frame;
	vector<SDL_Surface*> textLines;
	TTF_Font* font;

public:
	Console(void);
	void buildFrame();
	SDL_Surface* getImage();
        friend class Object;
                   .....
And then I have the base class Object:

Code: Select all

class Object
{
	string imageName;
	bool solid;
	SDL_Rect collisionRect;
        friend class Console;
                ..........
There is a much better solution but atm I'm to tired, sorry :(
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
Maevik
Chaos Rift Junior
Chaos Rift Junior
Posts: 230
Joined: Mon Mar 02, 2009 3:22 pm
Current Project: www.keedepictions.com/Pewpew/
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: Long Beach, CA

Re: Inheritance and Pointers

Post by Maevik »

Ok, this seems to not be an issue with what I thought it was at all. Apparently SDL didn't like me creating FillRects on empty/null surfaces. I guess you can only draw them to the screen/main surface? Hooray for SDL_GetVideoSurface()
My love is like a Haddoken, it's downright fierce!
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: <SOLVED> Inheritance and Pointers

Post by short »

lol the simplest solutions are the best :)
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
Post Reply