Code: Select all
void TextSystem::Print(std::string input,int x,int y,int width,int height,int r,int g,int b,SDL_Surface * buffer)
{
if(input.size()<=0)
{
return;
}
SDL_Rect Rect;Rect.x=x;Rect.y=y; //for blitting
SDL_Color Col={r,g,b};
int w=0;
int h=0;
//Font is TTF_Font * Font
SDL_Surface * TEMP=TTF_RenderText_Solid(Font,input.c_str(),Col); //render text, this is were error is
w=TEMP->w/width;
h=TEMP->h/height;
SDL_Surface * ret=shrinkSurface(TEMP,w,h); //resize to width and height
Rect.w=ret->w;Rect.h=ret->h;
SDL_BlitSurface(ret,NULL,buffer,&Rect); //blit
SDL_FreeSurface(TEMP); //free surfaces
SDL_FreeSurface(ret);
}
Code: Select all
int TTF_SizeUNICODE(TTF_Font *font, const Uint16 *text, int *w, int *h)
{
int status;
const Uint16 *ch;
int swapped;
int x, z;
int minx, maxx;
int miny, maxy;
c_glyph *glyph;
FT_Error error;
FT_Long use_kerning;
FT_UInt prev_index = 0;
/* Initialize everything to 0 */
if ( ! TTF_initialized ) {
TTF_SetError( "Library not initialized" );
return -1;
}
status = 0;
minx = maxx = 0;
miny = maxy = 0;
swapped = TTF_byteswapped;
/* check kerning */
use_kerning = FT_HAS_KERNING( font->face ); //crash points to here
/* Load each character and sum it's bounding box */
x= 0;
for ( ch=text; *ch; ++ch ) { //and here
Uint16 c = *ch;
if ( c == UNICODE_BOM_NATIVE ) {
swapped = 0;
if ( text == ch ) {
++text;
}
continue;
}
if ( c == UNICODE_BOM_SWAPPED ) {
swapped = 1;
if ( text == ch ) {
++text;
}
continue;
}
if ( swapped ) {
c = SDL_Swap16(c);
}
error = Find_Glyph(font, c, CACHED_METRICS);
if ( error ) {
return -1;
}
glyph = font->current;
/* handle kerning */
if ( use_kerning && prev_index && glyph->index ) {
FT_Vector delta;
FT_Get_Kerning( font->face, prev_index, glyph->index, ft_kerning_default, &delta );
x += delta.x >> 6;
}
#if 0
if ( (ch == text) && (glyph->minx < 0) ) {
/* Fixes the texture wrapping bug when the first letter
* has a negative minx value or horibearing value. The entire
* bounding box must be adjusted to be bigger so the entire
* letter can fit without any texture corruption or wrapping.
*
* Effects: First enlarges bounding box.
* Second, xstart has to start ahead of its normal spot in the
* negative direction of the negative minx value.
* (pushes everything to the right).
*
* This will make the memory copy of the glyph bitmap data
* work out correctly.
* */
z -= glyph->minx;
}
#endif
z = x + glyph->minx;
if ( minx > z ) {
minx = z;
}
if ( font->style & TTF_STYLE_BOLD ) {
x += font->glyph_overhang;
}
if ( glyph->advance > glyph->maxx ) {
z = x + glyph->advance;
} else {
z = x + glyph->maxx;
}
if ( maxx < z ) {
maxx = z;
}
x += glyph->advance;
if ( glyph->miny < miny ) {
miny = glyph->miny;
}
if ( glyph->maxy > maxy ) {
maxy = glyph->maxy;
}
prev_index = glyph->index;
}
/* Fill the bounds rectangle */
if ( w ) {
*w = (maxx - minx);
}
if ( h ) {
#if 0 /* This is correct, but breaks many applications */
*h = (maxy - miny);
#else
*h = font->height;
#endif
}
return status;
}
Here is the text log code just for GP:
Code: Select all
void TextSystem::Render(SDL_Surface * buffer)
{
for(int i=0;i<Text.size();i++) //Text is std::vector<std::string>
{
Surfaces.push_back(TTF_RenderText_Solid(Font,Text[i].c_str(),Colors[i])); //colors is std::vector<SDL_Color>
}
for(int i=0;i<Surfaces.size();i++)
{
SDL_Rect * Rect=new SDL_Rect;
Rect->x=Position[i]->x;Rect->y=Position[i]->y;Rect->w=Surfaces[i]->w;Rect->h=Surfaces[i]->h;
//Positions is std::vector<vector2d<int>> and Surfaces is std::vector<SDL_Surface*>
SDL_BlitSurface(Surfaces[i],NULL,buffer,Rect);
delete Rect;
}
for(int i=0;i<Surfaces.size();i++)
{
delete Position[i];
SDL_FreeSurface(Surfaces[i]);
}
Position.clear();
Surfaces.clear();
Text.clear();
Colors.clear();
}