Original Post
I am trying to make a set of functions that I can reuse to draw text using OpenGL.
I just can't understand the problem.
When I call init("resources/font.ttf",0,0,0,16) it enters the fv::text::init() function.
Something is wrong with the thisFont and thisPath variables.
I pause the debugger on the first line of the fv::text::init function and check the variables.
This is what I get:
fv::text::thisFont 0x00000000 _TTF_Font *
fv::text::thisPath 0x00000000 char *
How is that possible?
I check all my passed arguments and they are set fine.
Even after the following lines execute I get the same thing.
fv::text::thisFont 0x00000000 _TTF_Font *
fv::text::thisPath 0x00000000 char *
I mean the thisPath is a copy of another char* so something's deffinetly wrong.
Please tell me what's wrong.
Thanks in advance. this has been killing me.
Walker
I just can't understand the problem.
When I call init("resources/font.ttf",0,0,0,16) it enters the fv::text::init() function.
Something is wrong with the thisFont and thisPath variables.
I pause the debugger on the first line of the fv::text::init function and check the variables.
This is what I get:
fv::text::thisFont 0x00000000 _TTF_Font *
fv::text::thisPath 0x00000000
How is that possible?
I check all my passed arguments and they are set fine.
Even after the following lines execute I get the same thing.
thisFont = TTF_OpenFont( fontPath, size);
thisPath = fontPath;fv::text::thisFont 0x00000000 _TTF_Font *
fv::text::thisPath 0x00000000
I mean the thisPath is a copy of another char* so something's deffinetly wrong.
Please tell me what's wrong.
Thanks in advance. this has been killing me.
Walker
namespace fv
{
namespace text
{
//Private
TTF_Font* thisFont;
SDL_Color thisColor;
char* thisPath;
int roundToInt(double x)
{
return (int)(x + 0.5);
}
int roundPower2(int x)
{
double logbase2 = log(static_cast<double>(x)) / log(static_cast<double>(2));
return roundToInt(pow(2,ceil(logbase2)));
}
void init(char* fontPath,const int r,const int g,const int b,const int size)
{
thisFont = TTF_OpenFont( fontPath, size);
thisPath = fontPath;
thisColor.r=r;
thisColor.b=b;
thisColor.g=g;
return;
}
void end()
{
TTF_CloseFont(thisFont);
}
void setColor(int r, int g, int b)
{
thisColor.r=r;
thisColor.b=b;
thisColor.g=g;
return;
}
void setSize(const int size)
{
TTF_CloseFont(thisFont);
thisFont = TTF_OpenFont(thisPath, size);
return;
}
void draw(int x, int y,const char *text)
{
SDL_Surface *initial;
SDL_Surface *intermediary;
//SDL_Rect rect;
int w,h;
GLuint texture;
// Use SDL_TTF to render our text
initial = TTF_RenderText_Blended(thisFont, text, thisColor);
// Convert the rendered text to a known format
w = roundPower2(initial->w);
h = roundPower2(initial->h);
intermediary = SDL_CreateRGBSurface(0, w, h, 32,
0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
SDL_BlitSurface(initial, 0, intermediary, 0);
// Tell GL about our new texture
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_BGRA,
GL_UNSIGNED_BYTE, intermediary->pixels );
// GL_NEAREST looks horrible, if scaled...
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// prepare to render our texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glColor3f(1.0f, 1.0f, 1.0f);
// Draw a quad at location
glBegin(GL_QUADS);
// Recall that the origin is in the lower-left corner
//That is why the TexCoords specify different corners
//than the Vertex coors seem to.
glTexCoord2f(0.0f, 0.0f);
glVertex2f(x, y);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(x + w, y);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(x + w, y + h);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(x , y + h);
glEnd();
// Bad things happen if we delete the texture before it finishes
glFinish();
// return the deltas in the unused w,h part of the rect
//location->w = initial->w;
//location->h = initial->h;
// Clean up
SDL_FreeSurface(initial);
SDL_FreeSurface(intermediary);
glDeleteTextures(1, &texture);
}
}
}