summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorennewalker <ennewalker@c06c8d41-db1a-0410-9941-cceddc491573>2008-07-24 00:31:57 +0000
committerennewalker <ennewalker@c06c8d41-db1a-0410-9941-cceddc491573>2008-07-24 00:31:57 +0000
commitc2457cbc9315371f712321e7d0263883151eb196 (patch)
treebbd1189f7a484bdcaaccd2d38e1f32552bc43958
parent01b9f925de89c9c68dc845da151011414e57cc92 (diff)
downloadcrawl-ref-c2457cbc9315371f712321e7d0263883151eb196.tar.gz
crawl-ref-c2457cbc9315371f712321e7d0263883151eb196.zip
More configurable fonts for Tiles.
git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@6654 c06c8d41-db1a-0410-9941-cceddc491573
-rw-r--r--crawl-ref/settings/tiles_options.txt14
-rw-r--r--crawl-ref/source/dat/tiles/Vera.ttfbin0 -> 65932 bytes
-rw-r--r--crawl-ref/source/externs.h10
-rw-r--r--crawl-ref/source/files.cc6
-rw-r--r--crawl-ref/source/initfile.cc37
-rw-r--r--crawl-ref/source/tilefont.cc49
-rw-r--r--crawl-ref/source/tilefont.h7
-rw-r--r--crawl-ref/source/tilesdl.cc83
-rw-r--r--crawl-ref/source/tilesdl.h12
9 files changed, 167 insertions, 51 deletions
diff --git a/crawl-ref/settings/tiles_options.txt b/crawl-ref/settings/tiles_options.txt
index 629b192a65..e06c866997 100644
--- a/crawl-ref/settings/tiles_options.txt
+++ b/crawl-ref/settings/tiles_options.txt
@@ -5,7 +5,7 @@ tile_show_items = !?/%=([)X}+\_.
# tile_title_screen = false
-# tile_player_col = white
+# tile_player_col = white
# tile_monster_col = red
# tile_neutral_col = red
# tile_friendly_col = lightred
@@ -24,3 +24,15 @@ tile_show_items = !?/%=([)X}+\_.
# tile_lava_col = grey
# tile_excluded_col = darkcyan
# tile_excl_centre = darkblue
+# tile_window_col = yellow
+
+# tile_font_crt_file = VeraMono.ttf
+# tile_font_crt_size = 15
+# tile_font_stat_file = VeraMono.ttf
+# tile_font_stat_size = 16
+# tile_font_msg_file = VeraMono.ttf
+# tile_font_msg_size = 14
+# tile_font_tip_file = VeraMono.ttf
+# tile_font_tip_size = 15
+# tile_font_lbl_file = Vera.ttf
+# tile_font_lbl_size = 14
diff --git a/crawl-ref/source/dat/tiles/Vera.ttf b/crawl-ref/source/dat/tiles/Vera.ttf
new file mode 100644
index 0000000000..58cd6b5e61
--- /dev/null
+++ b/crawl-ref/source/dat/tiles/Vera.ttf
Binary files differ
diff --git a/crawl-ref/source/externs.h b/crawl-ref/source/externs.h
index 9953838a19..ceac4828d0 100644
--- a/crawl-ref/source/externs.h
+++ b/crawl-ref/source/externs.h
@@ -1882,6 +1882,16 @@ public:
char tile_excluded_col;
char tile_excl_centre_col;
char tile_window_col;
+ std::string tile_font_crt_file;
+ int tile_font_crt_size;
+ std::string tile_font_msg_file;
+ int tile_font_msg_size;
+ std::string tile_font_stat_file;
+ int tile_font_stat_size;
+ std::string tile_font_lbl_file;
+ int tile_font_lbl_size;
+ std::string tile_font_tip_file;
+ int tile_font_tip_size;
#endif
#ifdef WIN32TILES
bool use_dos_char;
diff --git a/crawl-ref/source/files.cc b/crawl-ref/source/files.cc
index f0f7c041c2..a82fc65208 100644
--- a/crawl-ref/source/files.cc
+++ b/crawl-ref/source/files.cc
@@ -521,11 +521,17 @@ std::string datafile_path(std::string basename,
const std::string prefixes[] = {
std::string("dat") + FILE_SEPARATOR,
+#ifdef USE_TILE
+ std::string("dat/tiles") + FILE_SEPARATOR,
+#endif
std::string("docs") + FILE_SEPARATOR,
std::string("settings") + FILE_SEPARATOR,
#ifndef DATA_DIR_PATH
std::string("..") + FILE_SEPARATOR + "docs" + FILE_SEPARATOR,
std::string("..") + FILE_SEPARATOR + "dat" + FILE_SEPARATOR,
+#ifdef USE_TILE
+ std::string("..") + FILE_SEPARATOR + "dat/tiles" + FILE_SEPARATOR,
+#endif
std::string("..") + FILE_SEPARATOR + "settings" + FILE_SEPARATOR,
std::string("..") + FILE_SEPARATOR,
#endif
diff --git a/crawl-ref/source/initfile.cc b/crawl-ref/source/initfile.cc
index b07a088383..0b1594d947 100644
--- a/crawl-ref/source/initfile.cc
+++ b/crawl-ref/source/initfile.cc
@@ -851,6 +851,18 @@ void game_options::reset_options()
tile_excluded_col = MAP_DKCYAN;
tile_excl_centre_col = MAP_DKBLUE;
tile_window_col = MAP_YELLOW;
+
+ // font selection
+ tile_font_crt_file = "VeraMono.ttf";
+ tile_font_crt_size = 15;
+ tile_font_stat_file = "VeraMono.ttf";
+ tile_font_stat_size = 16;
+ tile_font_msg_file = "VeraMono.ttf";
+ tile_font_msg_size = 14;
+ tile_font_tip_file = "VeraMono.ttf";
+ tile_font_tip_size = 15;
+ tile_font_lbl_file = "Vera.ttf";
+ tile_font_lbl_size = 14;
#endif
#ifdef WIN32TILES
@@ -2943,6 +2955,31 @@ void game_options::read_option_line(const std::string &str, bool runscript)
tile_window_col =
_str_to_tile_colour(field);
}
+ else if (key == "tile_font_crt_file")
+ {
+ tile_font_crt_file = field;
+ }
+ else INT_OPTION(tile_font_crt_size, 1, INT_MAX);
+ else if (key == "tile_font_msg_file")
+ {
+ tile_font_msg_file = field;
+ }
+ else INT_OPTION(tile_font_msg_size, 1, INT_MAX);
+ else if (key == "tile_font_stat_file")
+ {
+ tile_font_stat_file = field;
+ }
+ else INT_OPTION(tile_font_stat_size, 1, INT_MAX);
+ else if (key == "tile_font_tip_file")
+ {
+ tile_font_tip_file = field;
+ }
+ else INT_OPTION(tile_font_tip_size, 1, INT_MAX);
+ else if (key == "tile_font_lbl_file")
+ {
+ tile_font_lbl_file = field;
+ }
+ else INT_OPTION(tile_font_lbl_size, 1, INT_MAX);
#endif
#ifdef WIN32TILES
diff --git a/crawl-ref/source/tilefont.cc b/crawl-ref/source/tilefont.cc
index 8faf6051d8..fec21c1d18 100644
--- a/crawl-ref/source/tilefont.cc
+++ b/crawl-ref/source/tilefont.cc
@@ -5,8 +5,10 @@
* Modified for Crawl Reference by $Author: ennewalker $ on $Date: 2008-03-07 $
*/
+#include "AppHdr.h"
#include "tilefont.h"
#include "defines.h"
+#include "files.h"
#include <SDL.h>
#include <SDL_opengl.h>
@@ -57,16 +59,26 @@ bool FTFont::load_font(const char *font_name, unsigned int font_size)
fprintf(stderr, "Failed to initialize freetype library.\n");
return false;
}
+
+ // TODO enne - need to find a cross-platform way to also
+ // attempt to locate system fonts by name...
+ std::string font_path = datafile_path(font_name, false, true);
+ if (font_path.c_str()[0] == 0)
+ {
+ fprintf(stderr, "Could not find font '%s'\n", font_name);
+ return false;
+ }
- error = FT_New_Face(library, font_name, 0, &face);
+ error = FT_New_Face(library, font_path.c_str(), 0, &face);
if (error == FT_Err_Unknown_File_Format)
{
- fprintf(stderr, "Unknown font format for file '%s'\n", font_name);
+ fprintf(stderr, "Unknown font format for file '%s'\n",
+ font_path.c_str());
return false;
}
else if (error)
{
- fprintf(stderr, "Invalid font from file '%s'\n", font_name);
+ fprintf(stderr, "Invalid font from file '%s'\n", font_path.c_str());
}
error = FT_Set_Pixel_Sizes(face, font_size, font_size);
@@ -75,8 +87,9 @@ bool FTFont::load_font(const char *font_name, unsigned int font_size)
// Get maximum advance
m_max_advance = coord_def(0,0);
int ascender = face->ascender >> 6;
- int min_y = 100000; // TODO enne - fix me
+ int min_y = 100000;
int max_y = 0;
+ int max_width = 0;
m_min_offset = 0;
m_glyphs = new GlyphInfo[256];
for (unsigned int c = 0; c < 256; c++)
@@ -100,18 +113,17 @@ bool FTFont::load_font(const char *font_name, unsigned int font_size)
m_max_advance.x = std::max(m_max_advance.x, advance);
+ max_width = std::max(max_width, bmp->width);
min_y = std::min(min_y, ascender - face->glyph->bitmap_top);
max_y = std::max(max_y, ascender + bmp->rows - face->glyph->bitmap_top);
m_glyphs[c].offset = face->glyph->bitmap_left;
m_glyphs[c].advance = advance;
+ m_glyphs[c].width = bmp->width;
- m_min_offset = std::min(m_min_offset, m_glyphs[c].offset);
+ m_min_offset = std::min((char)m_min_offset, m_glyphs[c].offset);
}
- // TEMP enne - this seems to be broken on OSX - it returns 8, when it should be 2?
- m_max_advance.x = 10;
-
// The ascender and text height given by FreeType2 is ridiculously large
// (e.g. 37 pixels high for 14 pixel font). Use min and max bounding
// heights on the characters we care about to get better values for the
@@ -119,11 +131,13 @@ bool FTFont::load_font(const char *font_name, unsigned int font_size)
m_max_advance.y = max_y - min_y;
ascender -= min_y;
+ int max_height = m_max_advance.y;
+
// Grow character size to power of 2
coord_def charsz(1,1);
- while (charsz.x < m_max_advance.x)
+ while (charsz.x < max_width)
charsz.x *= 2;
- while (charsz.y < m_max_advance.y)
+ while (charsz.y < max_height)
charsz.y *= 2;
// Fill out texture to be (16*charsz.x) X (16*charsz.y) X (32-bit)
@@ -159,10 +173,6 @@ bool FTFont::load_font(const char *font_name, unsigned int font_size)
ASSERT(bmp->pixel_mode == FT_PIXEL_MODE_GRAY);
ASSERT(bmp->num_grays == 256);
- ASSERT(bmp->width + face->glyph->bitmap_left <= m_max_advance.x);
- ASSERT(bmp->rows <= m_max_advance.y);
- ASSERT(vert_offset >= 0);
- ASSERT(vert_offset + bmp->rows <= m_max_advance.y);
// Horizontal offset stored in m_glyphs and handled when drawing
unsigned int offset_x = (c % 16) * charsz.x;
@@ -225,15 +235,16 @@ void FTFont::render_textblock(unsigned int x_pos, unsigned int y_pos,
for (unsigned int x = 0; x < width; x++)
{
unsigned char c = chars[i];
- unsigned int this_adv = m_glyphs[c].advance + 1;
adv.x += m_glyphs[c].offset;
if (m_glyphs[c].renderable)
{
+ int this_width = m_glyphs[c].width;
+
unsigned char col = colours[i];
float tex_x = (float)(c % 16) / 16.0f;
float tex_y = (float)(c / 16) / 16.0f;
- float tex_x2 = tex_x + (float)this_adv / (float)m_tex.width();
+ float tex_x2 = tex_x + (float)this_width / (float)m_tex.width();
float tex_y2 = tex_y + texcoord_dy;
FontVertLayout v;
@@ -257,7 +268,7 @@ void FTFont::render_textblock(unsigned int x_pos, unsigned int y_pos,
v.a = 255;
verts.push_back(v);
- v.pos_x = adv.x + this_adv;
+ v.pos_x = adv.x + this_width;
v.pos_y = adv.y + m_max_advance.y;
v.tex_x = tex_x2;
v.tex_y = tex_y2;
@@ -267,7 +278,7 @@ void FTFont::render_textblock(unsigned int x_pos, unsigned int y_pos,
v.a = 255;
verts.push_back(v);
- v.pos_x = adv.x + this_adv;
+ v.pos_x = adv.x + this_width;
v.pos_y = adv.y;
v.tex_x = tex_x2;
v.tex_y = tex_y;
@@ -279,7 +290,7 @@ void FTFont::render_textblock(unsigned int x_pos, unsigned int y_pos,
}
i++;
- adv.x += this_adv - m_glyphs[c].offset - 1;
+ adv.x += m_glyphs[c].advance - m_glyphs[c].offset;
}
adv.x = 0;
diff --git a/crawl-ref/source/tilefont.h b/crawl-ref/source/tilefont.h
index 5d8939a9a9..20ed52f5d4 100644
--- a/crawl-ref/source/tilefont.h
+++ b/crawl-ref/source/tilefont.h
@@ -50,10 +50,13 @@ protected:
struct GlyphInfo
{
// offset before drawing glyph; can be negative
- int offset;
+ char offset;
// per-glyph horizontal advance
- int advance;
+ char advance;
+
+ // per-glyph width
+ char width;
bool renderable;
};
diff --git a/crawl-ref/source/tilesdl.cc b/crawl-ref/source/tilesdl.cc
index 7a375ea046..fda5f26320 100644
--- a/crawl-ref/source/tilesdl.cc
+++ b/crawl-ref/source/tilesdl.cc
@@ -78,7 +78,6 @@ TilesFramework::TilesFramework() :
m_context(NULL),
m_fullscreen(false),
m_active_layer(LAYER_CRT),
- m_font(NULL),
m_buttons_held(0),
m_key_mod(0),
m_mouse(-1, -1),
@@ -113,6 +112,12 @@ void TilesFramework::shutdown()
m_layers[i].m_regions.clear();
}
+ for (unsigned int i = 0; i < m_fonts.size(); i++)
+ {
+ delete m_fonts[i].font;
+ m_fonts[i].font = NULL;
+ }
+
SDL_Quit();
}
@@ -157,25 +162,34 @@ bool TilesFramework::initialise()
if (!m_image.load_textures())
return false;
- // TODO enne - make this configurable (and fall back on default if fails)
- // TODO enne - use Crawl's dat file loading utilities
- const char *font_file = "dat/tiles/VeraMono.ttf";
- const int font_size = 14;
- if (!load_font(font_file, font_size))
- return false;
-
- ASSERT(m_font);
-
// TODO enne - grab these from options
unsigned int map_pixsz = 4;
- // TODO enne - different font for tooltip and for dungeon tags
- m_region_tile = new DungeonRegion(&m_image, m_font, TILE_X, TILE_Y);
+ int crt_font = load_font(Options.tile_font_crt_file.c_str(),
+ Options.tile_font_crt_size);
+ int msg_font = load_font(Options.tile_font_msg_file.c_str(),
+ Options.tile_font_msg_size);
+ int stat_font = load_font(Options.tile_font_stat_file.c_str(),
+ Options.tile_font_stat_size);
+ m_tip_font = load_font(Options.tile_font_tip_file.c_str(),
+ Options.tile_font_tip_size);
+ int lbl_font = load_font(Options.tile_font_lbl_file.c_str(),
+ Options.tile_font_lbl_size);
+
+ if (crt_font == -1 || msg_font == -1 || stat_font == -1
+ || m_tip_font == -1 || lbl_font == -1)
+ {
+ return false;
+ }
+
+ m_region_tile = new DungeonRegion(&m_image, m_fonts[lbl_font].font,
+ TILE_X, TILE_Y);
m_region_map = new MapRegion(map_pixsz);
m_region_self_inv = new InventoryRegion(&m_image, TILE_X, TILE_Y);
- m_region_msg = new MessageRegion(m_font);
- m_region_stat = new StatRegion(m_font);
- m_region_crt = new CRTRegion(m_font);
+
+ m_region_msg = new MessageRegion(m_fonts[msg_font].font);
+ m_region_stat = new StatRegion(m_fonts[stat_font].font);
+ m_region_crt = new CRTRegion(m_fonts[crt_font].font);
m_region_menu_inv = new InventoryRegion(&m_image, TILE_X, TILE_Y);
m_layers[LAYER_NORMAL].m_regions.push_back(m_region_tile);
@@ -196,20 +210,34 @@ bool TilesFramework::initialise()
return true;
}
-bool TilesFramework::load_font(const char *font_file, int font_size)
+int TilesFramework::load_font(const char *font_file, int font_size,
+ bool default_on_fail)
{
- m_font = new FTFont();
+ FTFont *font = new FTFont();
+
+ for (unsigned int i = 0; i < m_fonts.size(); i++)
+ {
+ font_info &finfo = m_fonts[i];
+ if (finfo.name == font_file && finfo.size == font_size)
+ return i;
+ }
- if (!m_font->load_font(font_file, font_size))
+ if (!font->load_font(font_file, font_size))
{
- delete m_font;
- m_font = NULL;
-
- printf("Failed to open font '%s'\n", font_file);
- return false;
+ delete font;
+ if (default_on_fail)
+ return (load_font("VeraMono.ttf", 12, false));
+ else
+ return -1;
}
- return true;
+ font_info finfo;
+ finfo.name = font_file;
+ finfo.size = font_size;
+ finfo.font = font;
+ m_fonts.push_back(finfo);
+
+ return (m_fonts.size() - 1);
}
void TilesFramework::load_dungeon(unsigned int *tileb, int gx, int gy)
@@ -701,9 +729,10 @@ void TilesFramework::redraw()
if (!m_tooltip.empty())
{
const coord_def min_pos(0, 0);
- m_font->render_string(m_mouse.x, m_mouse.y - 2, m_tooltip.c_str(),
- min_pos, m_windowsz, WHITE, false, 150,
- BLUE, 5);
+ FTFont *font = m_fonts[m_tip_font].font;
+ font->render_string(m_mouse.x, m_mouse.y - 2, m_tooltip.c_str(),
+ min_pos, m_windowsz, WHITE, false, 150,
+ BLUE, 5);
}
SDL_GL_SwapBuffers();
diff --git a/crawl-ref/source/tilesdl.h b/crawl-ref/source/tilesdl.h
index 5f0032ebec..9653db6968 100644
--- a/crawl-ref/source/tilesdl.h
+++ b/crawl-ref/source/tilesdl.h
@@ -118,7 +118,8 @@ public:
const coord_def &get_cursor() const;
protected:
- bool load_font(const char *font_file, int font_size);
+ int load_font(const char *font_file, int font_size,
+ bool default_on_fail = true);
int handle_mouse(MouseEvent &event);
// screen pixel dimensions
@@ -157,7 +158,14 @@ protected:
CRTRegion *m_region_crt;
InventoryRegion *m_region_menu_inv;
- FTFont *m_font;
+ struct font_info
+ {
+ std::string name;
+ int size;
+ FTFont *font;
+ };
+ std::vector<font_info> m_fonts;
+ int m_tip_font;
void do_layout();