TTF_Font Error[SOLVED]
Posted: Thu May 20, 2010 9:09 pm
So i have a textmanager class that holds a TTF_Font*Font in it. This is it's load function:
This worked perfectly in a separate project. However, when i put a TextManager inside a System class:
and try to call LoadFont, it doesnt load the font correctly. It ends up not pointing to the font.
But in my text render function, if i define the font:
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
void TextManager::LoadFont(const char*name,int size)
{
fontsize=size;
Font=TTF_OpenFont(name,size);
}
Code: Select all
class System
{
public:
TextManager Text;
}
Code: Select all
System.Text.LoadFont("comic.ttf",42); //doesnt work
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();
}
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;
}