1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
void RenderText(TTF_Font *font, const GLubyte R, const GLubyte G, const GLubyte B,const double X, const double Y, char *texte);
int main(int argc, char** argv)
{
SDL_Init(SDL_INIT_VIDEO);
TTF_Init();
TTF_Font *font = TTF_OpenFont("police.ttf",20);
SDL_Event event;
int quit = 0;
SDL_SetVideoMode(500,500, 24,SDL_OPENGL);
glClearColor(0.5,0.5,0.5,0); //Couleur du fond
while(quit == 0)
{
SDL_PollEvent(&event);
switch(event.type)
{
case SDL_QUIT:
quit = 1;
break;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
RenderText(font,255,0,255,-1,0,"test");
SDL_GL_SwapBuffers();
SDL_Delay(33);
}
TTF_Quit();
SDL_Quit();
return 0;
}
void RenderText(TTF_Font *font, const GLubyte R, const GLubyte G, const GLubyte B,
const double X, const double Y, char *texte)
{
/*Create some variables.*/
SDL_Color color = {R, G, B};
SDL_Surface *Message = TTF_RenderText_Blended(font, texte, color);
GLuint Texture = 0;
SDL_Surface *tmp = SDL_CreateRGBSurface(0,Message->w,Message->h,32,0,0,0,0);
SDL_BlitSurface(Message,NULL,tmp,NULL);
/*Generate an OpenGL 2D texture from the SDL_Surface*.*/
glGenTextures(1, &Texture);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Texture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 4, Message->w, Message->h, 0, GL_RGBA,
GL_UNSIGNED_BYTE, tmp->pixels);
glColor3f(1,1,1);
glLoadIdentity();
glTranslated(X, Y, 0);
glBegin(GL_QUADS);
glTexCoord2d(0, 0); glVertex2d(0, 0);
glTexCoord2d(1, 0); glVertex2d(0.5, 0);
glTexCoord2d(1, 1); glVertex2d(0.5, 0.5);
glTexCoord2d(0, 1); glVertex2d(0, 0.5);
glEnd();
glDisable(GL_TEXTURE_2D);
/*Clean up.*/
//SDL_SaveBMP(Message,"rendu.bmp");
glDeleteTextures(1, &Texture);
SDL_FreeSurface(Message);
SDL_FreeSurface(tmp);
} |
Partager