c++ - SDL_ttf 未正确运行

标签 c++ sdl sdl-ttf

我正在使用 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/

相关文章:

c++ - Winsock - 在 C++ 中从 Java 客户端读取整数

c++ - `::operator new` 是做什么的?

audio - SDL_Audio_Spec中的回调函数是否每秒调用SDLAudio_Spec.freq次?

c - SDL 左控件 keydown 事件在释放键时触发

c++ - SDL 在屏幕上显示分数

c++ - SDL_TTF 和 SDL 2 不能一起工作。给出未处理的异常

c++ - 按列排序后如何从 QTableWidget::item 中获取正确的行(Qt BUG?)?

c++ - 这是对 std::bind 的错误使用还是编译器错误?

image - "Undefined Reference To ' IMG_Load '"代码块和 SDL_Image 错误

opengl - 在OpenGL中使用SDL_ttf