TTF text does not appear when implemented using SDL_Renderer

时间:2016-08-31 18:13:18

标签: c++ sdl-2 sdl-ttf

The following code compiles and generates BACKGROUND_TEXTURE, however fails to display text Message. When I call placeText function before BACKGROUND_TEXTURE, the graphics window appears to flicker with a white backdrop. I don't know what I have to do for the Message text to appear. By the way, Image.png appears as it is supposed to.

internal const int SCREEN_WIDTH = 1481; //1024;
internal const int SCREEN_HEIGHT = 865; //768;
internal char *imageNames[] = {(char *)"Image.png",
                               (char *)"GSAEV.bmp",
                               (char *)"RSAEV.bmp",
                               (char *)"SPerson.bmp",
                               (char *)"NPerson.bmp"};
internal Dimensions imageDims[] = {{0, 0}, {30, 40}, {30, 40}, {30, 40},
                                   {22, 30}};
internal bool
sdl2Init(SDL_Renderer **renderer)
        gWindow = SDL_CreateWindow("Ridesharing Visualization: (2016)",
                                   SDL_WINDOWPOS_UNDEFINED,
                                   SDL_WINDOWPOS_UNDEFINED,
                                   SCREEN_WIDTH, SCREEN_HEIGHT,
                                   SDL_WINDOW_SHOWN);
int imgFlags = IMG_INIT_PNG;
               *renderer = SDL_CreateRenderer(gWindow, -1,
                                               SDL_RENDERER_ACCELERATED |
                                               SDL_RENDERER_PRESENTVSYNC);
SDL_SetRenderDrawColor(vehicleState->renderer, 0xFF, 0xFF, 0xFF, 0xFF);
    SDL_RenderClear(vehicleState->renderer);
    SDL_RenderCopy(vehicleState->renderer,
                   vehicleState->images[BACKGROUND_TEXTURE].texture, 0, 0);

placeText(vehicleState->renderer);

// NOTE: places text
inline void
placeText(SDL_Renderer *renderer)
{
   // Initialize SDL_ttf library
   if ( TTF_Init() == -1 )
   {
   printf( "SDL_ttf could not initialize! SDL_TTF Error: %s\n", TTF_GetError() );
   } else {
    // Load font
    TTF_Font* Arial = TTF_OpenFont("arial.TTF", 24); //this opens a font style and sets a size

    TTF_CloseFont(Arial);

    SDL_Color Black = { 0, 0, 0, 0xFF };  
    // Write text to surface
    SDL_Surface* surfaceMessage = TTF_RenderText_Solid(Arial, "put your text here", Black); 
    // Convert surface to texture
    SDL_Texture* Message = SDL_CreateTextureFromSurface(renderer, surfaceMessage); 
    // Free Surface
    SDL_FreeSurface(surfaceMessage); // we got the texture now -> free surface

    SDL_Rect Message_rect; //create a rect
    Message_rect.x = 10;  //controls the rect's x coordinate
    Message_rect.y = 10; // controls the rect's y coordinte
    Message_rect.w = 100; // controls the width of the rect
    Message_rect.h = 100; // controls the height of the rect

    SDL_RenderCopy(renderer, Message, NULL, &Message_rect);

    SDL_RenderPresent(renderer);
//    SDL_Delay(500);

    SDL_DestroyTexture(Message);
   }
    atexit(TTF_Quit); // Ensure TTF_Quit() is called when we exit
}

0 个答案:

没有答案