0

我正在使用 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[]) { init(); 跑(); }

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

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

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

4

2 回答 2

1

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

于 2012-04-25T15:37:14.047 回答
0

我今天遇到了与 SDL_ttf 类似的问题。你确定你的项目目录中有 SDL_ttf 的所有 DLL 文件吗?SDL_ttf 附带的 DLL 文件。

于 2014-07-28T04:17:59.697 回答