Advertisement

[SDL + OpenGL] Write a text

Started by April 11, 2011 03:43 PM
-1 comments, last by moejul 13 years, 7 months ago
Hi,

At first, sorry for my English, but i speak French... I do my best. ;)

I'm making a game for a school's work and I've got a problem when I write the name of the player.

I want to write some text with SDL_ttf library on a grid drawed with OpenGL. The game will not use the 3D, so i don't need DEPTH_BUFFER.

Here is a basic code :
#include <iostream>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <string>
#define _USE_MATH_DEFINES
#include <math.h>

#define LARGEUR_FENETRE 700
#define HAUTEUR_FENETRE 480
#define Nlignes 8
#define Ncolonnes 8

using namespace std;

void ecrire(...);

int main(int argc, char *argv[]) {
bool continuer = true;
SDL_Surface *fenetre = NULL;
SDL_Event evenement;
SDL_Color couleur_txt = {0, 0, 0};

SDL_Init(SDL_INIT_VIDEO); // Initialisaion de SDL en mode vidéo.
TTF_Init();
fenetre = SDL_SetVideoMode(LARGEUR_FENETRE, HAUTEUR_FENETRE, 32, SDL_OPENGL); // Création de la fenêtre pour OpenGL.
SDL_WM_SetCaption("Jeu Othello-Reversi", NULL); // Titre de la fenêtre.
glClearColor(0.85, 1, 0.9, 0); // Couleur de fond
glEnable(GL_BLEND); // Permet d'utiliser l'opacité
glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Pour l'opacité
do {
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
gluOrtho2D(0, LARGEUR_FENETRE, HAUTEUR_FENETRE, 0);

glBegin(GL_QUADS);
glColor4ub(16, 116, 21, 255);
glVertex2d(40, 40); // Cadre de la grille
glVertex2d(40, 440);
glVertex2d(440, 440);
glVertex2d(440, 40);
glEnd();

glBegin(GL_LINES);
glColor4ub(9, 62, 12, 255);
for (int i = 40; i <= 440; i++) { // Lignes horizontales de la grille
glVertex2d(40, i);
glVertex2d(440, i);
i += 49;
}
for (int i = 40; i <= 440; i++) { // Lignes verticales de la grille
glVertex2d(i, 40);
glVertex2d(i, 440);
i += 49;
}
glEnd();

ecrire(...);

SDL_PollEvent(&evenement); // Gestion des événements
switch (evenement.type) {
case SDL_QUIT:
continuer = false;
break;
}

SDL_GL_SwapBuffers();
} while(continuer);

TTF_Quit();
SDL_FreeSurface(fenetre);
SDL_Quit();

return EXIT_SUCCESS;
}

void ecrire(...) {

}


What do I have to write in the "ecrire" function (écrire = to write) ? Are the declarations in the main function correct ([font="Courier New"]glEnable, glBlendFunc[/font]) ? I'd like to keep the line [font="Courier New"]gluOrtho2D(0, LARGEUR_FENETRE, HAUTEUR_FENETRE, 0);[/font], if possible.

I maked a try :
void ecrire(string texte, string nom_police, SDL_Color couleur, int pos_x, int pos_y, int taille) {
SDL_Rect position;
TTF_Font *police;
police = TTF_OpenFont(nom_police.c_str(), taille);
position.x = pos_x;
position.y = pos_y;
SDL_Surface *message = TTF_RenderText_Blended(police, texte.c_str(), couleur);
GLuint texture = 0;
int largeur_texture, hauteur_texture;

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

// Il faut que les dimensions de la texture soient de taille 2^n (32, 64, 128,...).
largeur_texture = (int)(pow(2, ceil(log((float)message->w) / log(2.0))) + 0.5);
hauteur_texture = (int)(pow(2, ceil(log((float)message->h) / log(2.0))) + 0.5);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, largeur_texture, hauteur_texture, 0, GL_RGBA, GL_UNSIGNED_BYTE, message->pixels);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex2d(position.x, position.y);
glTexCoord2i(1, 0); glVertex2d(position.x+message->w, position.y);
glTexCoord2i(1, 1); glVertex2d(position.x+message->w, position.y+message->h);
glTexCoord2i(0, 1); glVertex2d(position.x, position.y+message->h);
glEnd();

glDeleteTextures(1, &texture);
SDL_FreeSurface(message);
TTF_CloseFont(police);
}


With this line in the main :
ecrire("Hello", "verdana.ttf", couleur_txt, 50, 100, 50);

But it doesn't work, I've this error :

problemetexte2.png

Or when it works, I've a reshaped text : click here to see.

Where is the problem ? If you don't find it, could you say what I've to put in the ecrire function ? I tried to start with this good reference, but successless. :(

For advance, thank you ! :)

This topic is closed to new replies.

Advertisement