我使用SDL_ttf但在mingw32的,我下載的一切,並嘗試下面的程序SDL_ttf不正確運行
#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主(INT ARGC,CHAR *的argv []){ 的init() ; run(); }
好了,所以我在這裏發佈的所有代碼。我也將字體文件複製到當前文件夾,但它不起作用,也不顯示任何錯誤或警告。我用下面的命令編譯此在的mingw32
g++ SDLTestFont.cpp -o TF.exe -I"d:\SDL\include" -lmingw32 -lSDLmain
LSDL SDL_ttf.lib -mwindows -O3 -w -static
你的代碼對我來說看起來很好。如果我沒有記錯,在TTF_Init()之前設置你的視頻模式是很重要的。這是我現在可以想到的唯一可能的錯誤。 – fancyPants 2012-04-25 15:08:25
在當前工作目錄中是否有AIRAL.TTF? – genpfault 2012-04-25 15:09:00
感謝您的回覆。在該代碼之前的某個地方設置videomode,SDL被正確初始化。而airal.ttf存放在系統字體目錄下,所以SDL_ttf不會在默認字體導向器中加載任何字體? – user1285419 2012-04-25 15:10:42