0

I am using SDL_ttf but in mingw32, I download everything and try the following program

#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(); run(); }

Ok, so here I post all code. I also copy the font file to the current folder but it is not effective nor showing any error or warning. I compile this in mingw32 with the following command

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

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

Martin G
  • 17,357
  • 9
  • 82
  • 98
user1285419
  • 2,183
  • 7
  • 48
  • 70
  • Your code looks fine to me. It's important that you have set your videomode before `TTF_Init()`, if I remember correctly. That's the only possible mistake I can think of now. – fancyPants Apr 25 '12 at 15:08
  • Is `AIRAL.TTF` in the current working directory? – genpfault Apr 25 '12 at 15:09
  • Thanks for your reply. SDL is properly initialized with setting videomode somewhere before that code. And airal.ttf stored in the system font directory, so SDL_ttf won't load any font in the default font director? – user1285419 Apr 25 '12 at 15:10
  • Check the [doc](http://jcatki.no-ip.org/SDL_ttf/SDL_ttf_14.html). Nothing about scanning the default font directory. – genpfault Apr 25 '12 at 16:28

2 Answers2

1

It looks like your font pointer isn't being initiallized and you're passing garbage to rended_text. I would check that the font file is actually in your project folder and make sure the spelling is exact.

  • i just updated the question by posing full code ... I also try to copy the font to the current folder , it doesn't work – user1285419 Apr 26 '12 at 08:55
  • I can't find anything wrong with your code, but I would say, you should always check for your font to be null during your init function the same way you did with screen. Unitilized fonts seem to be the source of many errors. I'm sorry I can't be of more help I'm not very handy with command line compiles. – Bryce Howard Apr 26 '12 at 16:20
  • Thanks anyway. I add some code to check the screen too. But seems it is not the problem because I draw some line there but just fail to draw the text, weird – user1285419 Apr 26 '12 at 20:07
0

I had a similar issue with SDL_ttf today. Did you make sure you had all the DLL files for SDL_ttf in your Project directory? the DLL files that came with SDL_ttf.