2010-08-30 29 views
-1

J'ai eu beaucoup de mal à essayer de rendre du texte en SDL avec TTF dans une fenêtre SDL. Je suis un peu nouveau en C++ et aimerais que cela soit expliqué d'une manière un peu «débutant». (avec un exemple de code, si possible?)SDL + SDL_TTF: Comment rendre le texte en SDL + SDL_TTF? (C++)

Merci!

+0

Qu'avez-vous essayé? Qu'avez-vous (code-sage)? Qu'est-ce qui n'a pas fonctionné? – genpfault

+0

Enfer je ne comprends même plus mon propre code. Mais à peu près ceci: SDL_Color fontcolor = {fgR, fgG, fgB, fgA}; SDL_Color fontbgcolor = {bgR, bgG, bgB, bgA}; SDL_Surface * text; if (qualité == solide) result_text = TTF_RenderText_Solid (police de caractères, texte, police de caractères); else if (qualité == ombré) result_text = TTF_RenderText_Shaded (police de caractères, texte, police de caractères, police de caractères); else if (qualité == blended) result_text = TTF_RenderText_Blended (police de caractères, texte, police de caractères); retourner le texte; – Lemmons

+0

Voir http://lazyfoo.net/SDL_tutorials/lesson07/index.php – Adam

Répondre

2

Voici un exemple simple. Assurez-vous que vous avez arial.ttf, ou votre police TrueType de choix dans votre répertoire. Vous aurez besoin de lier avec -lSDL -lSDL_ttf.

#include "SDL.h" 
#include "SDL_ttf.h" 

SDL_Surface* screen; 
SDL_Surface* fontSurface; 
SDL_Color fColor; 
SDL_Rect fontRect; 

SDL_Event Event; 

TTF_Font* font; 

//Initialize the font, set to white 
void fontInit(){ 
     TTF_Init(); 
     font = TTF_OpenFont("arial.ttf", 12); 
     fColor.r = 255; 
     fColor.g = 255; 
     fColor.b = 255; 
} 

//Print the designated string at the specified coordinates 
void printF(char *c, int x, int y){ 
     fontSurface = TTF_RenderText_Solid(font, c, fColor); 
     fontRect.x = x; 
     fontRect.y = y; 
     SDL_BlitSurface(fontSurface, NULL, screen, &fontRect); 
     SDL_Flip(screen); 
} 

int main(int argc, char** argv) 
{ 
    // Initialize the SDL library with the Video subsystem 
    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE); 

    //Create the screen 
    screen = SDL_SetVideoMode(320, 480, 0, SDL_SWSURFACE); 

    //Initialize fonts 
    fontInit(); 

    //Print to center of screen 
    printF("Hello World", screen->w/2 - 11*3, screen->h/2); 

    do { 
     // Process the events 
     while (SDL_PollEvent(&Event)) { 
      switch (Event.type) { 

       case SDL_KEYDOWN: 
        switch (Event.key.keysym.sym) { 
        // Escape forces us to quit the app 
         case SDLK_ESCAPE: 
          Event.type = SDL_QUIT; 
         break; 

         default: 
         break; 
        } 
       break; 

      default: 
      break; 
     } 
    } 
    SDL_Delay(10); 
    } while (Event.type != SDL_QUIT); 

    // Cleanup 
    SDL_Quit(); 

    return 0; 
}