SDL_ttf无法正常运行

时间:2012-04-25 14:40:49

标签: 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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

相关问题