diff --git a/src/font.c b/src/font.c index 7e762ec..2994ae7 100644 --- a/src/font.c +++ b/src/font.c @@ -17,29 +17,15 @@ int initFont(TTF_Font **font, char *filename, int size) { int writeText(GLuint *_textTexId, TTF_Font *font, const char *text, SDL_Color color, GLboolean keep_previous) { SDL_Surface *d, *s; + // Create text if (!(d = TTF_RenderUTF8_Blended_Wrapped(font, text, color, 0))) { freeFont(font); fprintf(stderr, "Erreur TTF : TTF_RenderUTF8_Blended_Wrapped\n"); return 1; } - GLint w, h; - if (keep_previous) { - glBindTexture(GL_TEXTURE_2D, *_textTexId); - - glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w); - glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h); - } else { - // Aucune texture existante déjà créer - glGenTextures(1, _textTexId); - assert(_textTexId); - glBindTexture(GL_TEXTURE_2D, *_textTexId); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - w = d->w, h = d->h; - } - if (!(s = SDL_CreateRGBSurface(0, w, h, 32, R_MASK, G_MASK, B_MASK, + // Surface finale + if (!(s = SDL_CreateRGBSurface(0, d->w, d->h, 32, R_MASK, G_MASK, B_MASK, A_MASK))) { freeFont(font); fprintf(stderr, "Erreur SDL : %s\n", SDL_GetError()); @@ -47,18 +33,50 @@ int writeText(GLuint *_textTexId, TTF_Font *font, const char *text, } if (keep_previous) { - glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, s->pixels); + // On garde la texture déjà présente + assert(_textTexId); + glBindTexture(GL_TEXTURE_2D, *_textTexId); + + GLint w, h; + glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w); + glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h); + + // Surface qui va garder les données de la texture + SDL_Surface *old; + if (!(old = SDL_CreateRGBSurface(0, w, h, 32, R_MASK, G_MASK, B_MASK, + A_MASK))) { + freeFont(font); + fprintf(stderr, "Erreur SDL : %s\n", SDL_GetError()); + return 2; + } + + // Copie des données + glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, old->pixels); + + // Scale les données sur la même taille que la surface finale + SDL_BlitScaled(old, NULL, s, NULL); + + SDL_FreeSurface(old); + } else { + // Aucune texture existante, donc on la créée + glGenTextures(1, _textTexId); + assert(_textTexId); + glBindTexture(GL_TEXTURE_2D, *_textTexId); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); } + // Copie le texte sur la surface finale SDL_BlitSurface(d, NULL, s, NULL); SDL_FreeSurface(d); + // Copie la surface dans la texture glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, s->w, s->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, s->pixels); SDL_FreeSurface(s); glBindTexture(GL_TEXTURE_2D, 0); - return 0; }