2014-05-16 74 views
0

我目前一直在試圖弄清楚在屏幕上顯示文本。但問題是,我似乎無法在屏幕上獲得任何東西。我已經看過很多關於這個問題的教程,我相信我的問題在於實際的屏幕「blitting」。在我看過的大多數教程中,他們都使用了「BlitSurface」方法。雖然自從我使用SDL2.0.3以來,我認爲這確實不行。我試圖做一些在屏幕上獲取圖像的基本類,但它只是保持空白。任何建議和/或解決方案?無法使用SDL_TTF在屏幕上顯示文本?

Game.ccp

#include "Game.h" 
using namespace std; 

bool Game::onInit(char*title, int width, int height, int fullscreen) 
{ 
    SDL_Init(SDL_INIT_EVERYTHING); 
    gWindow = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, fullscreen); 
    if (gWindow == NULL) 
    { 
     cout << "Failed to create window \n"; 
    } 
    else 
     cout << "Created window! \n"; 
    gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED); 
    if (gRenderer == NULL) 
    { 
     cout << "Failed to create renderer! \n"; 
    } 
    else 
    { 
     cout << "Created renderer! \n"; 
     gRunning = true; 
    } 
    TTF_Init(); 

    //Calling the Text.h function init. 
    text.init(0, 0, 24, "bin/font/Pixel.ttf", "HELLO", gRenderer, { 255, 255, 255 }); 



    return gRunning; 

} 


void Game::onHandleEvents() 
{ 
    SDL_Event Event; 
    if (SDL_PollEvent(&Event) != 0) 
    { 
     if (Event.type == SDL_QUIT) 
     { 
      gRunning = false; 
     } 

     switch (Event.key.keysym.sym) 
     { 
     case SDLK_ESCAPE: 
     { 
      gRunning = false; 
     } 

     } 
    } 
} 


void Game::onUpdate() 
{ 

} 

void Game::onRender() 
{ 
    SDL_RenderClear(gRenderer); 

    //Calling Text.h draw function. 
    text.draw(gRenderer); 
    SDL_RenderPresent(gRenderer); 
} 

void Game::onClean() 
{ 
    SDL_DestroyRenderer(gRenderer); 
    SDL_DestroyWindow(gWindow); 

    gWindow = NULL; 
    gRenderer = NULL; 

    TTF_Quit(); 
    SDL_Quit(); 
} 

Text.h

#pragma once 
#include <SDL.h> 
#include <SDL_ttf.h> 
#include <string> 

class Text 
{ 
private: 
    TTF_Font*font; 
    SDL_Surface*surfaceMessage = NULL; 
    SDL_Texture*Message; 
    std::string text; 
    int x, y; 
    SDL_Color color; 
    SDL_Rect src; 
    SDL_Rect dst; 

public: 
    bool init(int X, int Y, int fontsize, char FontFile[], char message[],SDL_Renderer*renderer ,SDL_Color c) 
    { 
     x = X; 
     y = Y; 
     text = message; 
     font = TTF_OpenFont(FontFile, fontsize); 
     if (font != NULL) 
     { 
      surfaceMessage = TTF_RenderText_Solid(font, message, color); 

      Message = SDL_CreateTextureFromSurface(renderer, surfaceMessage); 
     } 


     return Message; 
    } 

    void draw(SDL_Renderer*renderer) 
    { 
     src = { 0, 0, surfaceMessage->w, surfaceMessage->h }; 
     dst = { 0, 0, surfaceMessage->w, surfaceMessage->h }; 

     SDL_RenderCopy(renderer, Message, &src, &dst); 
    } 

}; 
+1

您確定SDL能夠找到字體嗎?並且請告訴我們您在Game.cpp中調用函數的位置 – olevegard

+0

是SDL能夠找到字體。我在我稱之爲功能的地方添加了評論。 – NeoSanguine

+0

在你的init函數中,它看起來像你沒有設置顏色成員等於你的SDL_Color c參數。還可以嘗試測試surfaceMessage是否爲null,如果它將TTF_GetError()發送到輸出流以找出發生了什麼問題。 – eziegl

回答

0

的問題是,我忘了把SDL_ttf.dll到我的System32中。 現在該方法工作得很好。儘管它仍然無法在game.ccp中調用該方法。 :/