我无法使用 SDL2 TTF 库在窗口上显示文本

问题描述 投票:0回答:1

我做一个SDL_ttf测试代码:

#include <SDL.h>
#include <stdbool.h>
#include <iostream>
#include "SDLwindow.h"
#include <SDL_ttf.h>
#include "GraphLib.h"
#undef main

using namespace std;


int main() {

    bool running = 1;

    
    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
    TTF_Init();


    WindowSDL window1;
    TTF_Font* font = TTF_OpenFont("./acme.ttf", 10);
    SDL_Surface* textSurf = TTF_RenderText_Solid(font, "Hola mundo", {255,0,0});
    SDL_Texture* textTexture = SDL_CreateTextureFromSurface(window1.renderer, textSurf);

    SDL_FreeSurface(textSurf);
    SDL_Rect textRect;
    textRect.x = 10;
    textRect.y = 10;
    textRect.w = 400;
    textRect.h = 100;
    //TTF_CloseFont(font);

    window1.CreateWindow("Pix", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 600, SDL_WINDOW_BORDERLESS);
    window1.CreateRenderer(window1.window, -1, 0);


    window1.ChangeBackgroundColor(0xe0e0e0);

    drawLine(window1, 3, 3, 40, 50, 0x0aaf88, 0);
    drawLine(window1, 40, 50, 80, 3, 0x0aaf88, 0);
    drawLine(window1, 80, 3, 3, 3, 0x0aaf88, 0);

    SDL_RenderCopy(window1.renderer, textTexture, NULL, &textRect);
    SDL_RenderPresent(window1.renderer);


    while (running) {
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                running = 0;
            }
        }

    }

    window1.Shutdown();
    TTF_Quit();

    return 0;
}

...而且...它没有出现,这是我的屏幕:

我试着改变某些行的顺序像

TTF_Init();
等等,结果是一样的,它没有出现在屏幕上

c++ sdl-2 sdl-ttf
1个回答
0
投票

在哪里可以找到带有 SDL_ttf.h 文件的 SDL 库。在我的它失踪了。 https://github.com/libsdl-org/SDL/releases/tag/release-2.26.4

© www.soinside.com 2019 - 2024. All rights reserved.