TTF_Font Error[SOLVED]

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
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

TTF_Font Error[SOLVED]

Post by mv2112 »

So i have a textmanager class that holds a TTF_Font*Font in it. This is it's load function:

Code: Select all

void TextManager::LoadFont(const char*name,int size)
{
	fontsize=size;
	Font=TTF_OpenFont(name,size);
}
This worked perfectly in a separate project. However, when i put a TextManager inside a System class:

Code: Select all

class System
{
public:
TextManager Text;
}
and try to call LoadFont, it doesnt load the font correctly. It ends up not pointing to the font.

Code: Select all

System.Text.LoadFont("comic.ttf",42); //doesnt work
But in my text render function, if i define the font:

Code: Select all

void TextManager::PrintText(std::string text,SDL_Surface*dest,int x,int y,int max)
{
	Font=TTF_OpenFont("comic.ttf",42); //THIS WORKS
	if(max==-1)
	{
		max=maxlength;
	}
	std::string raw=text;
	SDL_Rect Rect;
	Rect.x=x;
	Rect.y=y-fontsize;
	std::string temp="";
	std::vector<std::string> buffer;
	if(text.size()==0)
	{
		return;
	}
	if(raw.size()>max)
	{
		for(int i=0;i<raw.size();i++)
		{
			temp+=raw[i];
			if((i%max==0&&i!=0)||i+1==raw.size())
			{
				if(temp[0]==' ')
				{
					temp.erase(temp.begin());
				}
				buffer.push_back(temp);
				temp.clear();
			}
		}
	}
	else
	{
		buffer.push_back(raw);
	}
	for(int i=0;i<buffer.size();i++)
	{		
		SDL_Surface*tempor=TTF_RenderText_Solid(Font,buffer[i].c_str(),Color);
		if(tempor==NULL)
		{
			printf("Error");
			getchar();
			return;
		}
		textbuffer.push_back(tempor);
	}
	for(int i=0;i<textbuffer.size();i++)
	{
		Rect.w=textbuffer[i]->w;
		Rect.h=textbuffer[i]->h;
		Rect.y+=fontsize;
		SDL_BlitSurface(textbuffer[i],NULL,dest,&Rect);
	}
	for(int i=0;i<textbuffer.size();i++)
	{
		SDL_FreeSurface(textbuffer[i]);
	}
	textbuffer.clear();
}
It works perfectly. I don't get it. I've already initialized SDL and SDL_ttf before calling LoadFont. When the TextManager was by itself,not in the system class, it worked perfectly. Anyone know why the LoadFont function doesnt work?

Here is my main

Code: Select all

int main(int argv,char*args[])
{
	Engine System;
	System.Init(640,480,32,SDL_SWSURFACE); //THIS INITS SDL AND SDL_TTF AND SETS THE VIDEO MODE
	System.Text.SetMaxLength(25); //THIS JUST SETS A INTEGER IN THE CLASS TO 25, AND IT WORKS
	System.Text.LoadFont("comic.ttf",42); //THIS DOESNT WORK
	System.NewParticleSystem();
	System.ParticleSystem[0]->Set(SDL_MapRGB(System.Window->GetFormat(),0,0,255),320,240,10000,500,2,2,2,5);
	System.ParticleSystem[0]->SetGravity(false);
	System.ParticleSystem[0]->SetType(SHOOT);
	float x=180;
	float y=180;
	System.ParticleSystem[0]->SetAngle(x,y);
	System.ParticleSystem[0]->Start();
	while(System.In.Quit())
	{
		AngleWrap(1,&x,0,360);
		AngleWrap(1,&y,0,360);
		System.ParticleSystem[0]->SetAngle(x,y);
		System.In.Poll();
		System.Window->Clear();
		System.ParticleSystem[0]->Run(System.Window->GetScreen());
		System.Text.PrintText("Test",System.Window->GetScreen(),0,0); //THIS IS IMPORTANT
		System.Window->Render();
	}
	System.End();
	return 0;
}
Last edited by mv2112 on Fri May 21, 2010 4:08 pm, edited 1 time in total.
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: TTF_Font Error

Post by mv2112 »

I've fixed my problem now there is another, i've got this problem at return 0 in main.

Code: Select all

Unhandled exception at 0x10487b3f in mvEngine3.0.exe: 0xC0000005: Access violation writing location 0xabababab.
Ugh...
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: TTF_Font Error

Post by dandymcgee »

mv2112 wrote:I've fixed my problem now there is another, i've got this problem at return 0 in main.

Code: Select all

Unhandled exception at 0x10487b3f in mvEngine3.0.exe: 0xC0000005: Access violation writing location 0xabababab.
Ugh...
That would be a magic number. The IDE is telling you something.

http://en.wikipedia.org/wiki/Magic_numb ... ramming%29
Wikipedia wrote: ABABABAB Used by Microsoft's HeapAlloc() to mark "no man's land" guard bytes after allocated heap memory
You're messing with memory off the deep end of the allocated heap.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: TTF_Font Error

Post by mv2112 »

dandymcgee wrote:
mv2112 wrote:I've fixed my problem now there is another, i've got this problem at return 0 in main.

Code: Select all

Unhandled exception at 0x10487b3f in mvEngine3.0.exe: 0xC0000005: Access violation writing location 0xabababab.
Ugh...
That would be a magic number. The IDE is telling you something.

http://en.wikipedia.org/wiki/Magic_numb ... ramming%29
Wikipedia wrote: ABABABAB Used by Microsoft's HeapAlloc() to mark "no man's land" guard bytes after allocated heap memory
You're messing with memory off the deep end of the allocated heap.
So....
I've messed up a pointer somewhere. I hate pointers.
But why would i be getting this error at return 0?
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: TTF_Font Error

Post by dandymcgee »

mv2112 wrote:
dandymcgee wrote:
mv2112 wrote:I've fixed my problem now there is another, i've got this problem at return 0 in main.

Code: Select all

Unhandled exception at 0x10487b3f in mvEngine3.0.exe: 0xC0000005: Access violation writing location 0xabababab.
Ugh...
That would be a magic number. The IDE is telling you something.

http://en.wikipedia.org/wiki/Magic_numb ... ramming%29
Wikipedia wrote: ABABABAB Used by Microsoft's HeapAlloc() to mark "no man's land" guard bytes after allocated heap memory
You're messing with memory off the deep end of the allocated heap.
So....
I've messed up a pointer somewhere. I hate pointers.
But why would i be getting this error at return 0?
Perhaps you have an object that doesn't go out of scope until the main function goes out of scope (at return 0)?
And perhaps this object's destructor attempts to delete memory it does not own?

Of course without any code this is all just speculation (I recommend you take a gander both at ~System() and destructors called from within it).
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: TTF_Font Error

Post by mv2112 »

Ugh, something is wrong with an std::vector<ParticleSystem*>. I cant access it with the debugger. I think that's whats causing the problem.
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: TTF_Font Error

Post by mv2112 »

mv2112 wrote:Ugh, something is wrong with an std::vector<ParticleSystem*>. I cant access it with the debugger. I think that's whats causing the problem.
Yup, when i changed std::vector<ParticleSystem*> to ParticleSystem* PS[100], it worked.
Post Reply