无法使用SDL_TTF在屏幕上显示文本?

时间:2014-05-16 22:14:29

标签: visual-c++ sdl true-type-fonts game-development sdl-2

我目前一直想弄清楚在屏幕上显示文字。然而问题是,我似乎无法在屏幕上获得任何内容。我已经看了很多关于此事的教程,我相信我的问题在于实际的" blitting"到屏幕。在大多数教程中,我看过他们已经使用过" BlitSurface"方法。虽然因为我使用的是SDL2.0.3,所以我认为这不起作用。我正在尝试做一些简单的类来获取屏幕上的图像,但它只是保持空白。有什么建议和/或解决方案吗?

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 个答案:

答案 0 :(得分:0)

问题是我忘记将SDL_ttf.dll放入我的System32。 现在这个方法运行得很好。虽然它仍然无法在game.ccp中调用该方法。 :/