Adonthell  0.4
win_ttf.cc
1 /*
2  $Id: win_ttf.cc,v 1.3 2006/09/03 20:48:08 ksterker Exp $
3 
4  (C) Copyright 2004 Kai Sterker
5  Part of the Adonthell Project http://adonthell.linuxgames.com
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details
13 */
14 
15 #include <ctype.h>
16 #include "win_ttf.h"
17 
18 // number of references to font file
19 u_int32 win_ttf::refcount = 0;
20 // font file used
21 TTF_Font *win_ttf::ttf = NULL;
22 
23 // ctor
24 win_ttf::win_ttf (const char *color, const string & file) : win_font ()
25 {
26  switch (color[0])
27  {
28  case 'r': // red
29  Color.r = 255; Color.g = 173; Color.b = 123;
30  break;
31  case 'b': // blue
32  Color.r = 139; Color.g = 185; Color.b = 238;
33  break;
34  case 'g': // green
35  Color.r = 205; Color.g = 254; Color.b = 148;
36  break;
37  case 'y': // yellow
38  Color.r = 255; Color.g = 238; Color.b = 123;
39  break;
40  case 'v': // violet
41  Color.r = 222; Color.g = 133; Color.b = 230;
42  break;
43  default: // white and all the rest
44  Color.r = 255; Color.g = 255; Color.b = 255;
45  break;
46  }
47 
48  refcount++;
49 
50  if (load (file))
51  {
52  u_int16 real_height_ = TTF_FontAscent (ttf);
53  height_ = screen::dbl_mode () ? (real_height_+3) >> 1 : real_height_+1;
54  cursor = &operator[]('_');
55  length_ = cursor->length ();
56  }
57  // fallback to old win_font implementation
58  else win_font::load ((char *) color);
59 }
60 
61 win_ttf::~win_ttf ()
62 {
63  refcount--;
64  cursor = NULL;
65  if (refcount == 0 && ttf != NULL)
66  TTF_CloseFont (ttf);
67 }
68 
69 bool win_ttf::load (const string & file)
70 {
71  string path;
72 
73  // size of font
74  u_int32 size = screen::dbl_mode () ? 22 : 12;
75 
76  // load font only once
77  if (ttf != NULL) return true;
78 
79  // absolute or relative font file from config?
80  if (file != "" && file[0] == '/')
81  {
82  path = file;
83  }
84  else
85  {
86  // path where is the file
87  path = WIN_DIRECTORY;
88 
89  // add win font directory path
90  path += WIN_FONT_DIRECTORY;
91 
92  // font name from config file
93  path += file == "" ? "avatar.ttf" : file;
94  }
95 
96  // try to load font specified in cfg file
97  ttf = TTF_OpenFont (path.c_str (), size);
98 
99  if (ttf == NULL)
100  {
101  printf ("*** error loading font '%s':\n %s\n", path.c_str (), TTF_GetError ());
102  return false;
103  }
104 
105  // make sure our font doesn't exceed a pixel size of 24/13
106  while (TTF_FontAscent (ttf) > (screen::dbl_mode () ? 24 : 13)) {
107  TTF_CloseFont (ttf);
108  TTF_OpenFont (path.c_str (), --size);
109  }
110 
111  return true;
112 }
113 
114 bool win_ttf::in_table(u_int16 tmp)
115 {
116  if (win_font::in_table (tmp) == true) return true;
117 
118  // try to create font
119  if (tmp > 0x80 || isprint (tmp)) {
120  operator[](tmp);
121  return true;
122  }
123  return false;
124 }
125 
126 image & win_ttf::operator[](u_int16 glyph)
127 {
128  static u_int16 unichar[2] = { 0, 0 };
129  unichar[0] = glyph;
130 
131  static SDL_Color bg = { 0x00, 0x00, 0x00, 0 };
132  static SDL_Color white = { 0xff, 0xff, 0xff, 0 };
133  if (win_font::in_table (glyph)) return *(glyphs[glyph]);
134  if (ttf == NULL) return *(glyphs[' ']);
135 
136  SDL_Surface *s = TTF_RenderUNICODE_Shaded (ttf, unichar, Color, bg);
137  if (s == NULL) return *(glyphs[' ']);
138 
139  image tmp (s, bg);
140  image *glph = new image (tmp.length(), height_, false);
141  glph->fillrect (0, 0, tmp.length()+1, height_+1, screen::trans_col(), NULL);
142 
143  s = TTF_RenderUNICODE_Solid (ttf, unichar, bg);
144  if (s != NULL)
145  {
146  image shadow (s, white);
147  shadow.draw (1, 1+height_-shadow.height(), 0, 0, shadow.length(), shadow.height(), NULL, glph);
148  }
149  else
150  {
151  fprintf (stderr, "%s\n", TTF_GetError ());
152  }
153 
154  tmp.draw (0, height_-tmp.height(), 0, 0, tmp.length(), tmp.height(), NULL, glph);
155  glyphs[glyph] = glph;
156 
157  return *glph;
158 }
159 
#define u_int16
16 bits long unsigned integer
Definition: types.h:32
void fillrect(s_int16 x, s_int16 y, u_int16 l, u_int16 h, u_int32 col, drawing_area *da_opt=NULL)
Fills an area of the surface with a given color.
Definition: surface.cc:260
Image manipulation class.
Definition: image.h:41
#define u_int32
32 bits long unsigned integer
Definition: types.h:35
A* pathfinding algorithm implementation class.
Definition: path.h:48
static u_int32 trans_col()
Returns the translucent color in screen&#39;s depth format.
Definition: screen.h:89