Skip to main content
GameDev.net gamedev.net
🔒 Locked

Variables not working correctly

Started by coderWalker Aug 29, 2011 at 3:44 PM 4 replies 1.4k views
Original Post
coderWalker
coderWalker
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.
thisFont = TTF_OpenFont( fontPath, size);
thisPath = fontPath;


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


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);
}
}
}
If this post was helpful please +1 or like it !

Webstrand
SiCrane
SiCrane
Well if TTF_OpenFont() is returning null, then the obvious thing to try would be to check what TTF_GetError() says the error is.
Ectara
Ectara
Where are you calling it? Is the module calling it using the up to date headers? It would seem that the calling module is passing parameters in the wrong order, or the wrong number of parameters. If it mistakenly passed zero for fontPath, that would explain why the TTF function returns NULL.
coderWalker
coderWalker

Well if TTF_OpenFont() is returning null, then the obvious thing to try would be to check what TTF_GetError() says the error is.


The only thing is, inside the function the arguents are correct such as,
char* fontPath = "resources/main.ttf"
cahr* thisPath =

This is after thisPath = fontPath; which is just a copy of a pointer.


Are there no problems in the code?
So this has to be a linker problem?

If this post was helpful please +1 or like it !

Webstrand
Ectara
Ectara
Where are you calling it? Is the module calling it using the up to dateheaders?

What happens if you place the pointer copy before the function call? Logically, it shouldn't make any difference, so if the result changes, I'd guess memory corruption. Have you tried a memory debugger like Valgrind or its Windows equivalent?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.