gpt4 book ai didi

c++ - SDL_ttf 未正确运行

转载 作者:行者123 更新时间:2023-11-28 08:05:51 28 4
gpt4 key购买 nike

我正在使用 SDL_ttf,但在 mingw32 中,我下载所有内容并尝试以下程序

#include <SDL.h>
#include "SDL_ttf.h"
#include <iostream>

using namespace std;

const int window_width = 600;
const int window_height = 600;

TTF_Font *font;
SDL_Surface *screen;

void init(void)
{
SDL_putenv("SDL_VIDEO_CENTERED=center");
if (TTF_Init() == -1)
{
cerr << "Unable to initialize SDL_tff: " << TTF_GetError() << endl;
exit(1);
}
font = TTF_OpenFont("arial.ttf", 12);

if (SDL_Init(SDL_INIT_EVERYTHING) < 0) exit(2);
if ( (screen = SDL_SetVideoMode(window_width, window_height, 8, SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL) exit(3);
}

void drawText(SDL_Surface *screen, int x, int y, string text)
{
SDL_Color foregroundColor = { 255, 255, 255 };
SDL_Color backgroundColor = { 0, 0, 255 };
SDL_Surface *resulting_text;
SDL_Rect rect = { x, y, 0, 0 };
resulting_text = TTF_RenderText_Solid(font, text.c_str(), foregroundColor);
cout << SDL_GetError();
SDL_BlitSurface(resulting_text, NULL, screen, &rect);
SDL_FreeSurface(resulting_text);
}

void run()
{
SDL_Event event;
bool running = true;
while (running)
{
SDL_LockSurface(screen);
char s[256];
drawText(screen, 20, 20, "text ................");
SDL_UpdateRect(screen, 0, 0, window_width, window_height);
SDL_UnlockSurface(screen);
while (SDL_PollEvent(&event))
{
if (event.type == SDL_KEYDOWN)
{
SDLKey _key = event.key.keysym.sym;
if ((_key == 27) || (_key == 'q') || (_key == 'Q'))
{
running = false;
}
}
}
}
}

int main(int argc, char* argv[]) { 在里面(); 运行();

好的,所以我在这里发布所有代码。我也将字体文件复制到当前文件夹,但它无效,也没有显示任何错误或警告。我使用以下命令在 mingw32 中编译它

g++ SDLTestFont.cpp -o TF.exe -I"d:\SDL\include" -lmingw32 -lSDLmain

lSDL SDL_ttf.lib -mwindows -O3 -w -static

最佳答案

看起来你的字体指针没有被初始化,你正在将垃圾传递给 rended_text。我会检查字体文件是否确实在您的项目文件夹中,并确保拼写准确无误。

关于c++ - SDL_ttf 未正确运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10318025/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com