diff --git a/Makefile b/Makefile index b6876fd..4d97bd7 100644 --- a/Makefile +++ b/Makefile @@ -5,14 +5,14 @@ all: menu %.oo: %.cpp g++ -Wall -g -c `sdl-config --cflags` -o $@ $< -menu.oo: menu.cpp menu.hh utils.hh Makefile +menu.oo: menu.cpp menu.hh utils.hh font.hh Makefile utils.oo: utils.cpp utils.hh Makefile -main.oo: menu.hh utils.hh Makefile +main.oo: menu.hh utils.hh sdl_ttf_font.hh Makefile menu: $(OBJS) g++ `sdl-config --libs` -lSDL -lSDL_image -lSDL_ttf -o $@ $+ clean: - rm -f *.oo menu + rm -f *.oo menu *~ diff --git a/main.cpp b/main.cpp index 9aa912d..229809a 100644 --- a/main.cpp +++ b/main.cpp @@ -80,7 +80,7 @@ static void init(void) TTF_Init(); - fnt = read_and_alloc_font("font.ttf", 18); + fnt = read_and_alloc_font("themes/default/font.ttf", 18); g_background = IMG_Load("themes/default/background.png"); diff --git a/sdl_ttf_font.hh b/sdl_ttf_font.hh new file mode 100644 index 0000000..a7ce83b --- /dev/null +++ b/sdl_ttf_font.hh @@ -0,0 +1,63 @@ +#include +#include + +#include "font.hh" +#include "utils.hh" + +#ifndef __SDL_TTF_FONT_HH__ +#define __SDL_TTF_FONT_HH__ + +class Font_TTF : public Font +{ +public: + Font_TTF(TTF_Font *font, + int r, int g, int b) + { + this->clr = (SDL_Color){r, g, b}; + this->font = font; + } + + ~Font_TTF() + { + free(this->font); + } + + int getHeight(const char *str) + { + int tw, th; + + TTF_SizeText(this->font, str, &tw, &th); + + return th; + } + + int getWidth(const char *str) + { + int tw, th; + + TTF_SizeText(this->font, str, &tw, &th); + + return tw; + } + + void draw(SDL_Surface *where, const char *msg, + int x, int y, int w, int h) + { + SDL_Surface *p; + SDL_Rect dst; + + p = TTF_RenderText_Blended(this->font, msg, this->clr); + panic_if(!p, "%s\n", TTF_GetError()); + + dst = (SDL_Rect){x, y, w, h}; + + SDL_BlitSurface(p, NULL, where, &dst); + SDL_FreeSurface(p); + } + +protected: + TTF_Font *font; + SDL_Color clr; +}; + +#endif