Page 1 of 1

TTF_Font Error[SOLVED]

Posted: Thu May 20, 2010 9:09 pm
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;
}

Re: TTF_Font Error

Posted: Fri May 21, 2010 3:22 pm
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...

Re: TTF_Font Error

Posted: Fri May 21, 2010 3:32 pm
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.

Re: TTF_Font Error

Posted: Fri May 21, 2010 3:38 pm
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?

Re: TTF_Font Error

Posted: Fri May 21, 2010 3:41 pm
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).

Re: TTF_Font Error

Posted: Fri May 21, 2010 4:02 pm
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.

Re: TTF_Font Error

Posted: Fri May 21, 2010 4:08 pm
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.