SDL ttf: Difference between revisions
Levidsmith (talk | contribs) No edit summary |
Levidsmith (talk | contribs) |
||
(15 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
I am attempting to use SDL_ttf for writing text to the screen. Currently, I just use bmps and pings for the menu text, but that process really isn't going to work when I want to write variable data (like FPS, level complete time, etc) to the screen. | |||
== Installing Development Files == | == Installing Development Files == | ||
Again, I am using cygwin with the gcc compiler under Windows. | |||
Download SDL_ttf-2.0.9.tar.gz from http://www.libsdl.org/projects/SDL_ttf/ | Download SDL_ttf-2.0.9.tar.gz from http://www.libsdl.org/projects/SDL_ttf/ | ||
Rename to .tar.gz if Windows changed the extension to .tar.tar | |||
mv SDL_ttf-2.0.9.tar.tar SDL_ttf-2.0.9.tar.gz | |||
Extract | |||
gzip -d SDL_ttf-2.0.9.tar.gz | |||
tar -xvf SDL_ttf-2.0.9.tar | |||
If you get a permission denied error, then chmod the tar file | |||
chmod u+rw SDL_ttf-2.0.9.tar | |||
tar -xvf SDL_ttf-2.0.9.tar | |||
Download the Freetype library at http://www.freetype.org | |||
Select "stable release" and pick the Source Forge link | |||
Download freetype-2.3.11.tar.gz | |||
Unzip | |||
gzip -d freetype-2.3.11.tar.gz | |||
chmod u+wr freetype-2.3.11.tar | |||
tar -xvf freetype-2.3.11.tar | |||
Build freetype | |||
cd freetype-2.3.11 | |||
./configure | |||
make | |||
make install | |||
Build the SDL_ttf library | |||
cd SDL_ttf-2.0.9 | |||
./configure | |||
make | |||
make install | |||
''Not sure why, but it seemed like I had to run "make install" twice... or maybe I just forgot to run it the first time.'' | |||
You can verify if it installed correctly entering the following: | |||
# ls -la /usr/local/include/SDL/SDL_ttf.h | |||
-rw-r--r-- 1 Foo None 9829 Nov 22 15:27 /usr/local/include/SDL/SDL_ttf.h | |||
# ls -la /usr/local/lib/libSDL_ttf* | |||
-rw-r--r-- 1 Foo None 85416 Nov 22 15:27 /usr/local/lib/libSDL_ttf.a | |||
-rwxr-xr-x 1 Foo None 23440 Nov 22 15:27 /usr/local/lib/libSDL_ttf.dll.a | |||
-rwxr-xr-x 1 Foo None 900 Nov 22 15:27 /usr/local/lib/libSDL_ttf.la | |||
If something went wrong, then you will get a "No such file or directory" message | |||
Write a simple test program that uses SDL_ttf. | |||
#include "SDL.h" | |||
#include "SDL_ttf.h" | |||
SDL_Surface *screen; | |||
SDL_Surface *imgText; | |||
int main (int argc, char **argv) { | |||
SDL_Init(SDL_INIT_VIDEO); | |||
SDL_Color color = { 255, 255, 0 }; | |||
SDL_Rect rect; | |||
TTF_Init(); | |||
screen = SDL_SetVideoMode(1024, 768, 0, 0); | |||
TTF_Font *font = TTF_OpenFont("Arial.ttf", 24); | |||
imgText = TTF_RenderText_Solid(font, "Hello World", color); | |||
rect.x = 200; | |||
rect.y = 200; | |||
SDL_BlitSurface(imgText, NULL, screen, &rect); | |||
SDL_Flip(screen); | |||
SDL_Delay(2000); | |||
SDL_FreeSurface(imgText); | |||
return 0; | |||
} | |||
Compile | |||
gcc test.c `sdl-config --libs --cflags` -lSDL_ttf | |||
Remember to link -lSDL_ttf on the compile command. Otherwise, you will get an error like this: | |||
/cygdrive/c/Users/GATECH~1/AppData/Local/Temp/cc1SGaYb.o:test.c:(.text+0x5a): un | |||
defined reference to `_TTF_OpenFont' | |||
/cygdrive/c/Users/GATECH~1/AppData/Local/Temp/cc1SGaYb.o:test.c:(.text+0x77): un | |||
defined reference to `_TTF_RenderText_Solid' | |||
collect2: ld returned 1 exit status | |||
Run | |||
./a | |||
You should get a screen like this: | |||
[[File:SDL ttf test.jpg|400px]] | |||
If you get a blank black window, then check the permissions on your ttf file: | |||
$ ls -la *.ttf | |||
----------+ 1 GaTechGrad None 766656 Oct 9 2006 arial.ttf | |||
If you don't have read permission, then chmod the file | |||
chmod u+r arial.ttf | |||
Actually, I found that my real problem was that I didn't call TTF_Init(). My testing showed that the permissions on the ttf file really doesn't matter. | |||
Next, I created a font.c file that where I put all of the font code. I created a "drawString(char *)" method that returns a SDL_Surface with the text passed in as a parameter. The prototype for that method is put into the globals.h file, so that the method can be called from my other files. | |||
== Distributing == | == Distributing == | ||
Download SDL_ttf-2.0.9.zip from http://www.libsdl.org/projects/SDL_ttf/ | Download SDL_ttf-2.0.9-win32.zip from http://www.libsdl.org/projects/SDL_ttf/ | ||
Extract the DLLs to the project directory | Extract the DLLs to the project directory |
Latest revision as of 21:10, 22 November 2009
I am attempting to use SDL_ttf for writing text to the screen. Currently, I just use bmps and pings for the menu text, but that process really isn't going to work when I want to write variable data (like FPS, level complete time, etc) to the screen.
Installing Development Files
Again, I am using cygwin with the gcc compiler under Windows.
Download SDL_ttf-2.0.9.tar.gz from http://www.libsdl.org/projects/SDL_ttf/
Rename to .tar.gz if Windows changed the extension to .tar.tar
mv SDL_ttf-2.0.9.tar.tar SDL_ttf-2.0.9.tar.gz
Extract
gzip -d SDL_ttf-2.0.9.tar.gz tar -xvf SDL_ttf-2.0.9.tar
If you get a permission denied error, then chmod the tar file
chmod u+rw SDL_ttf-2.0.9.tar tar -xvf SDL_ttf-2.0.9.tar
Download the Freetype library at http://www.freetype.org
Select "stable release" and pick the Source Forge link
Download freetype-2.3.11.tar.gz
Unzip
gzip -d freetype-2.3.11.tar.gz chmod u+wr freetype-2.3.11.tar tar -xvf freetype-2.3.11.tar
Build freetype
cd freetype-2.3.11 ./configure make make install
Build the SDL_ttf library
cd SDL_ttf-2.0.9 ./configure make make install
Not sure why, but it seemed like I had to run "make install" twice... or maybe I just forgot to run it the first time.
You can verify if it installed correctly entering the following:
# ls -la /usr/local/include/SDL/SDL_ttf.h -rw-r--r-- 1 Foo None 9829 Nov 22 15:27 /usr/local/include/SDL/SDL_ttf.h # ls -la /usr/local/lib/libSDL_ttf* -rw-r--r-- 1 Foo None 85416 Nov 22 15:27 /usr/local/lib/libSDL_ttf.a -rwxr-xr-x 1 Foo None 23440 Nov 22 15:27 /usr/local/lib/libSDL_ttf.dll.a -rwxr-xr-x 1 Foo None 900 Nov 22 15:27 /usr/local/lib/libSDL_ttf.la
If something went wrong, then you will get a "No such file or directory" message
Write a simple test program that uses SDL_ttf.
#include "SDL.h" #include "SDL_ttf.h" SDL_Surface *screen; SDL_Surface *imgText; int main (int argc, char **argv) { SDL_Init(SDL_INIT_VIDEO); SDL_Color color = { 255, 255, 0 }; SDL_Rect rect; TTF_Init(); screen = SDL_SetVideoMode(1024, 768, 0, 0); TTF_Font *font = TTF_OpenFont("Arial.ttf", 24); imgText = TTF_RenderText_Solid(font, "Hello World", color); rect.x = 200; rect.y = 200; SDL_BlitSurface(imgText, NULL, screen, &rect); SDL_Flip(screen); SDL_Delay(2000); SDL_FreeSurface(imgText); return 0; }
Compile
gcc test.c `sdl-config --libs --cflags` -lSDL_ttf
Remember to link -lSDL_ttf on the compile command. Otherwise, you will get an error like this:
/cygdrive/c/Users/GATECH~1/AppData/Local/Temp/cc1SGaYb.o:test.c:(.text+0x5a): un defined reference to `_TTF_OpenFont' /cygdrive/c/Users/GATECH~1/AppData/Local/Temp/cc1SGaYb.o:test.c:(.text+0x77): un defined reference to `_TTF_RenderText_Solid' collect2: ld returned 1 exit status
Run
./a
You should get a screen like this:
If you get a blank black window, then check the permissions on your ttf file:
$ ls -la *.ttf ----------+ 1 GaTechGrad None 766656 Oct 9 2006 arial.ttf
If you don't have read permission, then chmod the file
chmod u+r arial.ttf
Actually, I found that my real problem was that I didn't call TTF_Init(). My testing showed that the permissions on the ttf file really doesn't matter.
Next, I created a font.c file that where I put all of the font code. I created a "drawString(char *)" method that returns a SDL_Surface with the text passed in as a parameter. The prototype for that method is put into the globals.h file, so that the method can be called from my other files.
Distributing
Download SDL_ttf-2.0.9-win32.zip from http://www.libsdl.org/projects/SDL_ttf/
Extract the DLLs to the project directory