Visual c++ 无法使用SDL\U TTF在屏幕上获取文本?

Visual c++ 无法使用SDL\U TTF在屏幕上获取文本?,visual-c++,sdl,truetype,game-development,sdl-2,Visual C++,Sdl,Truetype,Game Development,Sdl 2,我目前一直在试图弄清楚如何在屏幕上显示文本。但问题是,我似乎无法在屏幕上看到任何东西。我已经看过很多关于这个问题的教程,我相信我的问题在于实际的“闪电”到屏幕上。在我看过的大多数教程中,它们都使用了“BlitSurface”方法。尽管我使用的是SDL2.0.3,但我认为这并不能真正起作用。我试图做一些简单的课程,让图像出现在屏幕上,但它只是保持空白。有什么建议和/或解决方案吗 Game.ccp #include "Game.h" using namespace std; bool Gam

我目前一直在试图弄清楚如何在屏幕上显示文本。但问题是,我似乎无法在屏幕上看到任何东西。我已经看过很多关于这个问题的教程,我相信我的问题在于实际的“闪电”到屏幕上。在我看过的大多数教程中,它们都使用了“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();
}
#包括“Game.h”
使用名称空间std;
bool Game::onInit(字符*标题、整数宽度、整数高度、整数全屏)
{
SDL_Init(SDL_Init_EVERYTHING);
gWindow=SDL_CreateWindow(标题、SDL_WINDOWPOS_未定义、SDL_WINDOWPOS_未定义、宽度、高度、全屏);
if(gWindow==NULL)
{
couth};
SDL_渲染复制(渲染器、消息、src和dst);
}
};

问题是我忘了将SDL\u ttf.dll放入我的系统32。
现在这个方法工作得很好。尽管它仍然无法调用game.ccp中的方法:/

你确定SDL能找到字体吗?请告诉我们您在游戏中调用函数的位置。cppYes SDL能够找到字体。我在调用函数的地方添加了注释。在init函数中,似乎没有将color成员设置为与SDL_color c参数相等。还可以尝试测试surfaceMessage是否为null,是否为send TTF_GetError(),并将其发送到输出流,以找出哪里出了问题。
#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);
    }

};