From 8bf972d3163958cf4e0ed1f5fa82d2756a859aa3 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sun, 21 May 2023 13:20:50 +0200 Subject: [PATCH] add the tag --- includes/font.h | 2 +- src/credits.c | 2 +- src/font.c | 33 +++++++++++++++++++++++---------- src/tag.c | 18 +++++++++++++++--- 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/includes/font.h b/includes/font.h index 8cc8c4a..0feba85 100644 --- a/includes/font.h +++ b/includes/font.h @@ -20,7 +20,7 @@ int initFont(TTF_Font **, char *, int); * Renvoie 2 en cas de problème de surface avec la SDL * * Renvoie 0 en cas de succès */ -int writeText(GLuint *, TTF_Font *, const char *, SDL_Color); +int writeText(GLuint *, TTF_Font *, const char *, SDL_Color, GLboolean); // Libère une police de la mémoire void freeFont(TTF_Font *); diff --git a/src/credits.c b/src/credits.c index 83053a2..a5f579f 100644 --- a/src/credits.c +++ b/src/credits.c @@ -54,7 +54,7 @@ static void init(void) { "- Audio : beepbox\n" "- Talent : aucun\n" "- Librairies : GL4D, SDL2 et extensions", - (SDL_Color){255, 255, 255, 255})) { + (SDL_Color){255, 255, 255, 255}, GL_FALSE)) { exit(errStatus); } diff --git a/src/font.c b/src/font.c index 62d5258..7e762ec 100644 --- a/src/font.c +++ b/src/font.c @@ -15,14 +15,7 @@ int initFont(TTF_Font **font, char *filename, int size) { } int writeText(GLuint *_textTexId, TTF_Font *font, const char *text, - SDL_Color color) { - 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); - + SDL_Color color, GLboolean keep_previous) { SDL_Surface *d, *s; if (!(d = TTF_RenderUTF8_Blended_Wrapped(font, text, color, 0))) { freeFont(font); @@ -30,16 +23,36 @@ int writeText(GLuint *_textTexId, TTF_Font *font, const char *text, return 1; } - if (!(s = SDL_CreateRGBSurface(0, d->w, d->h, 32, R_MASK, G_MASK, B_MASK, + 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, A_MASK))) { freeFont(font); fprintf(stderr, "Erreur SDL : %s\n", SDL_GetError()); return 2; } + + if (keep_previous) { + glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, s->pixels); + } + SDL_BlitSurface(d, NULL, s, NULL); SDL_FreeSurface(d); - glBindTexture(GL_TEXTURE_2D, *_textTexId); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, s->w, s->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, s->pixels); diff --git a/src/tag.c b/src/tag.c index 99d99c2..abbd28b 100644 --- a/src/tag.c +++ b/src/tag.c @@ -38,6 +38,16 @@ static void init(void) { glGenTextures(1, &_texId); load_img("images/wall.png", _texId); + TTF_Font *font = NULL; + if (initFont(&font, "fonts/Instrument.ttf", 100)) { + exit(3); + } + if (writeText(&_texId, font, "macron = loser", + (SDL_Color){255, 255, 255, 255}, GL_FALSE)) { + exit(3); + } + freeFont(font); + gl4duGenMatrix(GL_FLOAT, matrix_proj); gl4duGenMatrix(GL_FLOAT, matrix_model); gl4duGenMatrix(GL_FLOAT, matrix_view); @@ -51,6 +61,7 @@ static void draw(void) { GLfloat lum_pos[] = {-2.0f * (float)cos(angle * (GLfloat)M_PI / (5.0f * 180.0f)), 0.2f, 0.0f, 1.0f}; + const GLfloat distance = 2; // distance from the scene glEnable(GL_DEPTH_TEST); glClearColor(0.2f, 0.2f, 0.2f, 1); @@ -62,13 +73,14 @@ static void draw(void) { gl4duFrustumf(-1, 1, -ratio, ratio, 2, 1000); bindAndLoadf(matrix_view); - const GLfloat distance = 2; // distance from the scene gl4duLookAtf(0, distance, distance, 0, 0, 0, 0, 1, 0); // gl4duRotatef(-90, 0, 1, 0); // rotation camera bindAndLoadf(matrix_model); - gl4duRotatef(40, 1, 0, 0); - gl4duRotatef(40, 0, 0, 1); + gl4duRotatef(45, 1, 0, 0); + gl4duRotatef(180, 0, 1, 0); + gl4duRotatef(20, 0, 0, 1); + gl4duScalef(-1, 1, 1); angle += dt * 90.0f; gl4duSendMatrices();