diff --git a/font.hh b/font.hh index 1d1b239..3065b54 100644 --- a/font.hh +++ b/font.hh @@ -1,6 +1,8 @@ #ifndef __FONT_HH__ #define __FONT_HH__ +#include + class Font { public: @@ -22,9 +24,8 @@ public: return this->getWidth(buf); } - virtual void draw(int x, int y, int w, int h) = 0; - - virtual void draw(int x, int y, int w, int h, int r, int g, int b) = 0; + virtual void draw(SDL_Surface *where, const char *msg, + int x, int y, int w, int h) = 0; }; #endif /* __FONT_HH__ */ diff --git a/main.cpp b/main.cpp index dadd116..2506aee 100644 --- a/main.cpp +++ b/main.cpp @@ -2,12 +2,13 @@ #include #include "menu.hh" +#include "sdl_ttf_font.hh" #include "utils.hh" class PrintMenu : public Menu { public: - PrintMenu(TTF_Font *font) : Menu(font) + PrintMenu(Font *font) : Menu(font) { } @@ -76,7 +77,7 @@ static void init(void) TTF_Init(); - fnt = read_and_alloc_font("font.ttf"); + fnt = read_and_alloc_font("font.ttf", 18); bg_left = IMG_Load("bg_left.png"); bg_right = IMG_Load("bg_right.png"); @@ -84,7 +85,7 @@ static void init(void) panic_if( !bg_left || !bg_right || !bg_middle, "bg loading failed\n"); - g_menu = new PrintMenu(fnt); + g_menu = new PrintMenu(new Font_TTF(fnt, 255, 255, 255)); g_menu->setText(main_menu_messages); g_menu->setSelectedBackground(bg_left, bg_middle, bg_right, NULL, NULL, NULL); diff --git a/menu.cpp b/menu.cpp index d2ada2b..db24698 100644 --- a/menu.cpp +++ b/menu.cpp @@ -13,6 +13,7 @@ #include #include "menu.hh" +#include "font.hh" #include "utils.hh" #define IS_SUBMENU(p_msg) ( (p_msg)[0] == '^' ) @@ -20,16 +21,12 @@ void Menu::printText(SDL_Surface *where, const char *msg, SDL_Color clr, int x, int y, int w, int h) { - SDL_Surface *font_surf; - SDL_Rect dst = {x, y, 0, 0}; - char buf[255]; + char *buf; unsigned int i; - int tw, th; + int tw; - TTF_SizeText(this->font, msg, &tw, &th); - - memset(buf, 0, sizeof(buf)); - strncpy(buf, msg, 254); + buf = strdup(msg); + tw = this->font->getWidth(buf); /* Crop text */ if (x + tw > w) @@ -56,12 +53,8 @@ void Menu::printText(SDL_Surface *where, const char *msg, SDL_Color clr, buf[i] = ' '; } - font_surf = TTF_RenderText_Blended(this->font, buf, clr); - panic_if (!font_surf, - "%s\n", TTF_GetError()); - - SDL_BlitSurface(font_surf, NULL, where, &dst); - SDL_FreeSurface(font_surf); + this->font->draw(where, buf, x, y, w, h); + free(buf); } void Menu::highlightBackground(SDL_Surface *where, @@ -74,7 +67,7 @@ void Menu::highlightBackground(SDL_Surface *where, if (!bg_left || !bg_middle || !bg_right) return; - int font_height = TTF_FontHeight(this->font); + int font_height = this->font->getHeight("X"); int bg_y_start = y + font_height / 2 - bg_left->h / 2; int bg_x_start = x - bg_left->w / 3; @@ -102,7 +95,7 @@ void Menu::highlightBackground(SDL_Surface *where, void Menu::draw(SDL_Surface *where, int x, int y, int w, int h) { - int font_height = TTF_FontHeight(this->font); + int font_height = this->font->getHeight("X"); int line_height = (font_height + font_height / 4); int x_start = x; int entries_visible = h / line_height - 2; @@ -139,7 +132,9 @@ void Menu::draw(SDL_Surface *where, int x, int y, int w, int h) /* Draw the background for the selected entry */ if (this->cur_sel == i) { int tw, th; - TTF_SizeText(this->font, msg, &tw, &th); + + tw = this->font->getWidth(msg); + th = this->font->getHeight(msg); this->highlightBackground(where, this->text_bg_left, this->text_bg_middle, this->text_bg_right, @@ -151,7 +146,7 @@ void Menu::draw(SDL_Surface *where, int x, int y, int w, int h) submenu_t *p_submenu = this->findSubmenu(i); int n_pipe = 0; int total_chars = 0; - int tw, th, tw_first, th_first; + int tw, th, tw_first; int n_chars; char *p; int n; @@ -175,11 +170,12 @@ void Menu::draw(SDL_Surface *where, int x, int y, int w, int h) p = (char*)xmalloc(total_chars + 1); strncpy(p, msg, n + 1); - TTF_SizeText(this->font, p, &tw_first, &th_first); + tw_first = this->font->getWidth(p); memset(p, 0, total_chars + 1); strncpy(p, msg + n, n_chars - 1); - TTF_SizeText(this->font, p, &tw, &th); + tw = this->font->getWidth(p); + th = this->font->getHeight(p); this->highlightBackground(where, this->submenu_bg_left, this->submenu_bg_middle, this->submenu_bg_right, @@ -407,7 +403,7 @@ void Menu::setText(const char **messages, int *submenu_defaults) this->selectOne(0); } -Menu::Menu(TTF_Font *font) +Menu::Menu(Font *font) { this->setTextColor((SDL_Color){0xff,0xff,0xff,0}); this->font = font; diff --git a/menu.hh b/menu.hh index 85fa950..6488626 100644 --- a/menu.hh +++ b/menu.hh @@ -16,6 +16,8 @@ #include #include +#include "font.hh" + enum { EVENT_NONE = 0, KEY_UP = 1, @@ -42,7 +44,7 @@ typedef int event_t; class Menu { public: - Menu(TTF_Font *font); + Menu(Font *font); void setTextColor(SDL_Color clr); @@ -97,7 +99,7 @@ protected: const char *title; const char **pp_msgs; - TTF_Font *font; + Font *font; SDL_Color text_color; SDL_Surface *text_bg_left; diff --git a/utils.cpp b/utils.cpp index cf3c40f..6b8cee8 100644 --- a/utils.cpp +++ b/utils.cpp @@ -6,7 +6,7 @@ #include "utils.hh" -TTF_Font *read_and_alloc_font(const char *path) +TTF_Font *read_and_alloc_font(const char *path, int pt_size) { TTF_Font *out; SDL_RWops *rw; @@ -28,7 +28,7 @@ TTF_Font *read_and_alloc_font(const char *path) fprintf(stderr, "Could not create RW: %s\n", SDL_GetError()); exit(1); } - out = TTF_OpenFontRW(rw, 1, 20); + out = TTF_OpenFontRW(rw, 1, pt_size); if (!out) { fprintf(stderr, "TTF: Unable to create font %s (%s)\n", diff --git a/utils.hh b/utils.hh index 8964571..ce4fae5 100644 --- a/utils.hh +++ b/utils.hh @@ -39,6 +39,6 @@ static inline void *xmalloc(size_t sz) panic_if(r < 0 || r >= (int)(size), "snprintf failed for %s with %d\n", fmt, r); \ } while(0) -TTF_Font *read_and_alloc_font(const char *path); +TTF_Font *read_and_alloc_font(const char *path, int pt_size); #endif /* __UTILS_H__ */