My First Class


mac-10

Still Fresh
Joined
Oct 27, 2006
Messages
56
Age
45
Website
Visit site
Hi there

I am having some trouble with a class I am trying to write.
I am new to C++ and have followed some tutorials on the web about
classes but I have run into a problem. Please be gentle I am new to C++
And last time I posted some code I got torn a new one.

I was trying to make a class to load in and display fonts.
Displaying them at different sizes and colours.

The class seems to work up until the point I try and write it to the screen surface.
As you can see I am using the drawtext function that I borrowed from guyforks sdl examples ;)

Does the problem have something to do with the pointer to the screen surface?

Or

Am I not grasping OOP


Any help or advice on this would be great.

CODE

class fontMaker{
private:
TTF_Font* font;
public:
void LoadFont(char*,int);
void drawText(SDL_Surface*, char*, int, int, int, int, int);
void renderText();
};

void fontMaker::LoadFont(char* font_path,int size){
font = TTF_OpenFont(font_path, size);
}

void fontMaker::drawText(SDL_Surface* screen, char* string, int x, int y, int fR, int fG, int fB)
{
SDL_Color foregroundColor = { fR, fG, fB };
SDL_Surface* textSurface = TTF_RenderText_Blended(font,string, foregroundColor);
SDL_Rect textLocation = { x, y, 0, 0 };
SDL_BlitSurface(textSurface, NULL, screen, &textLocation);
SDL_FreeSurface(textSurface);
}

fontMaker my_font;
my_font.LoadFont("AGITPM__.TTF",12);
my_font.drawText(screen,"Hello I am the font class",10,10,255,255,255);




thanks

Mac
 
I am speaking from Java here, but when you first declare fontMaker my_font; shouldn't you allocate it some memory by the 'new' keyword or something similarly before calling its methods? Like fontMaker my_font = new fontMaker();

C++ may be different though, so take this with a grain of salt.
 
QUOTE
C++ may be different though, so take this with a grain of salt.


C++ is different :)

fontMaker my_font;

Creates the object on the stack.

Most obvious potential problem - check TTF_OpenFont isn't returning NULL (same goes for TTF_RenderText_Blended)
 
mac-10 said:
Does the problem have something to do with the pointer to the screen surface?

Hard to say considering that you have not shown the code where you define "screen", nor said exactly what happened when you write on the screen. Maybe it is just that something went wrong at the initialization, so your screen pointer points nowhere.
 
Last edited by a moderator:
parkydr you were spot on the TTF_OpenFont was null because
I am stupid and didn't get my paths right.

funny things is I've been playing around with this for about a day on and off
it didn’t occur to me to check the paths I just assumed it was something a lot more sinister.

feels like I have wasted everyone’s time I promise next time I'll have a problem worth posting

I am sorry

thanks again

Mac
 
Don't worry, I had a similar problem that took too long to work out, when I had simply forgotton to put the font file on the SD card :rolleyes:

Oh, and the time i wondered why none of my images were showing on the gp2x...

the solution is normaly the obvious!

mac-10 said:
parkydr you were spot on the TTF_OpenFont was null because
I am stupid and didn't get my paths right.

funny things is I've been playing around with this for about a day on and off
it didn’t occur to me to check the paths I just assumed it was something a lot more sinister.

feels like I have wasted everyone’s time I promise next time I'll have a problem worth posting

I am sorry

thanks again

Mac
 
Last edited by a moderator:
Only if you declare it a pointer, you need the new.
CODE
fontMaker *my_font = NULL;
my_font = new fontMaker;


My class for this would look somewhat like this:
CODE
class fontMaker{
private:
TTF_Font* font;
public:
fontMaker() //i was missing the constructor here
{
font = NULL;
}

void LoadFont(char* font_path, int size)
{
if( font != NULL ) TTF_CloseFont(font); //if a font was alread loaded, unload it
font = TTF_OpenFont(font_path, size);
}

void drawText(SDL_Surface* screen, char* string, int x, int y, int fR, int fG, int fB)
{
SDL_Color foregroundColor = { fR, fG, fB };
SDL_Surface* textSurface = TTF_RenderText_Blended(font,string, foregroundColor);
SDL_Rect textLocation = { x, y, 0, 0 };
SDL_BlitSurface(textSurface, NULL, screen, &textLocation);
SDL_FreeSurface(textSurface);
}
};


Please comment on if you think its better to put the functions of the class outside or inside of it...
 
Quiest said:
Please comment on if you think its better to put the functions of the class outside or inside of it...

Inside if you intend to inline them and they are short, outside if not. It is always favourable to put code in separate source files because of file dependency during compiling.

If you change a header file, the compiler will have to recompile any source file that #includes it. If you make a change in source file, then the compiler has to only compile that one file.

To the OP, it might be worth reading either of both of these free eBooks:

http://www.steveheller.com/cppad/Output/dialogTOC.html
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html
 
Last edited by a moderator:
Back
Top