0

I'm trying to use SDL_ttf to render text on OpenGL, but every time I try to get the pixels from a SDL_Surface, it throws the following error:

Unhandled exception at 0x1000a590 in L2DF.exe:
0xC0000005: Access violation reading location 0x05b8f9e8.

I've placed a breakpoint where the error occurs, the SDL_Surface pointer doesn't seen to be NULL, and all the data (like width and height) are correct.

Here's the code:

void put_string(std::string text, bool filter=false)
{
    SDL_Color col = {255, 255, 255};
    SDL_Surface *txt = TTF_RenderText_Solid(sdl_font, text.c_str(), col);

    if (txt == NULL) return;

    GLuint tx;
    glGenTextures(1, &tx);
    glBindTexture(GL_TEXTURE_2D, tx);

    if (filter)
    {
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    }
    else
    {
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    }

    // Here is where the error occurs...
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, txt->w, txt->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, txt->pixels);
    // If I replace txt->pixels by NULL, the error is gone...

    glBegin(GL_QUADS);
    glTexCoord2d(0, 0); glVertex2d(0, 0);
    glTexCoord2d(1, 0); glVertex2d(txt->w, 0);
    glTexCoord2d(1, 1); glVertex2d(txt->w, txt->h);
    glTexCoord2d(0, 1); glVertex2d(0, txt->h);
    glEnd();

    glBindTexture(GL_TEXTURE_2D, 0);
    glDeleteTextures(1, &tx);

    SDL_FreeSurface(txt);
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
dcubix
  • 187
  • 2
  • 10

0 Answers0