FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
subimagefont.cpp
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 #include <string>
24 
25 // Platform specific includes
26 #include "util/base/fife_stdint.h"
27 
28 // 3rd party library includes
29 #include <SDL.h>
30 
31 // FIFE includes
32 // These includes are split up in two parts, separated by one empty line
33 // First block: files included from the FIFE root src directory
34 // Second block: files included from the same folder
35 #include "util/base/exception.h"
36 #include "util/log/logger.h"
37 #include "util/structures/rect.h"
38 #include "util/utf8/utf8.h"
39 #include "video/image.h"
40 #include "video/imagemanager.h"
41 #include "video/renderbackend.h"
42 
43 #include "subimagefont.h"
44 
45 namespace FIFE {
46  static Logger _log(LM_GUI);
47 
48  SubImageFont::SubImageFont(const std::string& filename, const std::string& glyphs)
49  : ImageFontBase() {
50 
51  FL_LOG(_log, LMsg("guichan_image_font, loading ") << filename << " glyphs " << glyphs);
52 
53 //prock - 504
54  ImagePtr img = ImageManager::instance()->load(filename);
55  int32_t image_id = img->getHandle();
56  SDL_Surface* surface = img->getSurface();
57  m_colorkey = RenderBackend::instance()->getColorKey();
58 
59  if( !surface ) {
60  throw CannotOpenFile(filename);
61  }
62 
63  // Make sure we get 32bit RGB
64  // and copy the Pixelbuffers surface
65  SDL_Surface *tmp = SDL_CreateRGBSurface(SDL_SWSURFACE,
66  surface->w,surface->h,32,
67  RMASK, GMASK, BMASK ,NULLMASK);
68 
69  SDL_BlitSurface(surface,0,tmp,0);
70  surface = tmp;
71 
72  // Prepare the data for extracting the glyphs.
73  uint32_t *pixels = reinterpret_cast<uint32_t*>(surface->pixels);
74 
75  int32_t x, w;
76  x = 0; w=0;
77 
78  SDL_Rect src;
79 
80  src.h = surface->h;
81  src.y = 0;
82 
83  uint32_t separator = pixels[0];
84  uint32_t colorkey = SDL_MapRGB(surface->format, m_colorkey.r, m_colorkey.g, m_colorkey.b);
85 
86  // if colorkey feature is not enabled then manually find the color key in the font
87  if (!RenderBackend::instance()->isColorKeyEnabled()) {
88  while(x < surface->w && pixels[x] == separator) {
89  ++x;
90  }
91 
92  colorkey = pixels[x];
93  }
94 
95  // Disable alpha blending, so that we use color keying
96  SDL_SetAlpha(surface,0,255);
97  SDL_SetColorKey(surface,SDL_SRCCOLORKEY,colorkey);
98 
99  FL_DBG(_log, LMsg("image_font")
100  << " glyph separator is "
101  << pprint(reinterpret_cast<void*>(separator))
102  << " transparent color is "
103  << pprint(reinterpret_cast<void*>(colorkey)));
104 
105  // Finally extract all glyphs
106  std::string::const_iterator text_it = glyphs.begin();
107  while(text_it != glyphs.end()) {
108  w=0;
109  while(x < surface->w && pixels[x] == separator)
110  ++x;
111  if( x == surface->w )
112  break;
113 
114  while(x + w < surface->w && pixels[x + w] != separator)
115  ++w;
116 
117  src.x = x;
118  src.w = w;
119 
120  tmp = SDL_CreateRGBSurface(SDL_SWSURFACE,
121  w,surface->h,32,
122  RMASK, GMASK, BMASK ,NULLMASK);
123 
124  SDL_FillRect(tmp,0,colorkey);
125  SDL_BlitSurface(surface,&src,tmp,0);
126 
127  // Disable alpha blending, so that we use colorkeying
128  SDL_SetAlpha(tmp,0,255);
129  SDL_SetColorKey(tmp,SDL_SRCCOLORKEY,colorkey);
130 
131 
132  uint32_t codepoint = utf8::next(text_it, glyphs.end());
133  m_glyphs[ codepoint ].surface = tmp;
134 
135  x += w;
136  }
137 
138  // Set placeholder glyph
139  // This should actually work ith utf8.
140  if( m_glyphs.find('?') != m_glyphs.end() ) {
141  m_placeholder = m_glyphs['?'];
142  } else {
143  m_placeholder.surface = 0;
144  }
145 
146  mHeight = surface->h;
147  SDL_FreeSurface(surface);
148  }
149 
150 
151 }
152 
SubImageFont(const std::string &filename, const std::string &glyphs)