Sign in to follow this  
moejul

[SDL + OpenGL] Write a text

Recommended Posts

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 :
[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(...) {

}[/code]

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 :
[code]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);
}
[/code]

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

But it doesn't work, I've this error :
[center][img]http://img26.imageshack.us/img26/3527/problemetexte2.png[/img]

[left]Or when it works, I've a reshaped text : [url="http://img545.imageshack.us/img545/7010/problemetexte.png"]click here to see[/url].

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 [url="http://www.gamedev.net/topic/284259-for-reference-using-sdl_ttf-with-opengl/"]this good reference[/url], but successless. :(

For advance, thank you ! :)
[/left] [/center]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this