FIFE 2008.0
|
00001 /*************************************************************************** 00002 * Copyright (C) 2005-2008 by the FIFE team * 00003 * http://www.fifengine.de * 00004 * This file is part of FIFE. * 00005 * * 00006 * FIFE is free software; you can redistribute it and/or * 00007 * modify it under the terms of the GNU Lesser General Public * 00008 * License as published by the Free Software Foundation; either * 00009 * version 2.1 of the License, or (at your option) any later version. * 00010 * * 00011 * This library is distributed in the hope that it will be useful, * 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00014 * Lesser General Public License for more details. * 00015 * * 00016 * You should have received a copy of the GNU Lesser General Public * 00017 * License along with this library; if not, write to the * 00018 * Free Software Foundation, Inc., * 00019 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * 00020 ***************************************************************************/ 00021 00022 // Standard C++ library includes 00023 #include <string> 00024 00025 // Platform specific includes 00026 #include "util/base/fife_stdint.h" 00027 00028 // 3rd party library includes 00029 #include <SDL.h> 00030 00031 // FIFE includes 00032 // These includes are split up in two parts, separated by one empty line 00033 // First block: files included from the FIFE root src directory 00034 // Second block: files included from the same folder 00035 #include "util/base/exception.h" 00036 #include "util/log/logger.h" 00037 #include "util/structures/rect.h" 00038 #include "util/utf8/utf8.h" 00039 #include "video/image.h" 00040 #include "video/renderbackend.h" 00041 #include "video/imagepool.h" 00042 00043 #include "subimagefont.h" 00044 00045 namespace FIFE { 00046 static Logger _log(LM_GUI); 00047 00048 SubImageFont::SubImageFont(const std::string& filename, const std::string& glyphs, ImagePool& pool) 00049 : ImageFontBase(), m_pool(pool) { 00050 00051 FL_LOG(_log, LMsg("guichan_image_font, loading ") << filename << " glyphs " << glyphs); 00052 00053 int image_id = m_pool.addResourceFromFile(filename); 00054 Image& img = dynamic_cast<Image&>(m_pool.get(image_id)); 00055 SDL_Surface* surface = img.getSurface(); 00056 m_colorkey = RenderBackend::instance()->getColorKey(); 00057 00058 if( !surface ) { 00059 throw CannotOpenFile(filename); 00060 } 00061 00062 // Make sure we get 32bit RGB 00063 // and copy the Pixelbuffers surface 00064 SDL_Surface *tmp = SDL_CreateRGBSurface(SDL_SWSURFACE, 00065 surface->w,surface->h,32, 00066 RMASK, GMASK, BMASK ,NULLMASK); 00067 00068 SDL_BlitSurface(surface,0,tmp,0); 00069 surface = tmp; 00070 00071 // Prepare the data for extracting the glyphs. 00072 uint32_t *pixels = reinterpret_cast<uint32_t*>(surface->pixels); 00073 00074 int x, w; 00075 x = 0; w=0; 00076 00077 SDL_Rect src; 00078 00079 src.h = surface->h; 00080 src.y = 0; 00081 00082 uint32_t separator = pixels[0]; 00083 uint32_t colorkey = SDL_MapRGB(surface->format, m_colorkey.r, m_colorkey.g, m_colorkey.b); 00084 00085 // if colorkey feature is not enabled then manually find the color key in the font 00086 if (!RenderBackend::instance()->isColorKeyEnabled()) { 00087 while(x < surface->w && pixels[x] == separator) { 00088 ++x; 00089 } 00090 00091 colorkey = pixels[x]; 00092 } 00093 00094 // Disable alpha blending, so that we use color keying 00095 SDL_SetAlpha(surface,0,255); 00096 SDL_SetColorKey(surface,SDL_SRCCOLORKEY,colorkey); 00097 00098 FL_DBG(_log, LMsg("image_font") 00099 << " glyph separator is " 00100 << pprint(reinterpret_cast<void*>(separator)) 00101 << " transparent color is " 00102 << pprint(reinterpret_cast<void*>(colorkey))); 00103 00104 // Finally extract all glyphs 00105 std::string::const_iterator text_it = glyphs.begin(); 00106 while(text_it != glyphs.end()) { 00107 w=0; 00108 while(x < surface->w && pixels[x] == separator) 00109 ++x; 00110 if( x == surface->w ) 00111 break; 00112 00113 while(x + w < surface->w && pixels[x + w] != separator) 00114 ++w; 00115 00116 src.x = x; 00117 src.w = w; 00118 00119 tmp = SDL_CreateRGBSurface(SDL_SWSURFACE, 00120 w,surface->h,32, 00121 RMASK, GMASK, BMASK ,NULLMASK); 00122 00123 SDL_FillRect(tmp,0,colorkey); 00124 SDL_BlitSurface(surface,&src,tmp,0); 00125 00126 // Disable alpha blending, so that we use colorkeying 00127 SDL_SetAlpha(tmp,0,255); 00128 SDL_SetColorKey(tmp,SDL_SRCCOLORKEY,colorkey); 00129 00130 00131 uint32_t codepoint = utf8::next(text_it, glyphs.end()); 00132 m_glyphs[ codepoint ].surface = tmp; 00133 00134 x += w; 00135 } 00136 00137 // Set placeholder glyph 00138 // This should actually work ith utf8. 00139 if( m_glyphs.find('?') != m_glyphs.end() ) { 00140 m_placeholder = m_glyphs['?']; 00141 } else { 00142 m_placeholder.surface = 0; 00143 } 00144 00145 mHeight = surface->h; 00146 SDL_FreeSurface(surface); 00147 } 00148 00149 00150 } 00151