Mercurial > fife-parpg
comparison engine/core/video/fonts/subimagefont.cpp @ 0:4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
author | mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sun, 29 Jun 2008 18:44:17 +0000 |
parents | |
children | 90005975cdbb |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4a0efb7baf70 |
---|---|
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 modify * | |
7 * it under the terms of the GNU General Public License as published by * | |
8 * the Free Software Foundation; either version 2 of the License, or * | |
9 * (at your option) any later version. * | |
10 * * | |
11 * This program 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 * | |
14 * GNU General Public License for more details. * | |
15 * * | |
16 * You should have received a copy of the GNU General Public License * | |
17 * along with this program; if not, write to the * | |
18 * Free Software Foundation, Inc., * | |
19 * 51 Franklin St, 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 "video/image.h" | |
39 #include "video/renderbackend.h" | |
40 #include "video/imagepool.h" | |
41 | |
42 #include "subimagefont.h" | |
43 | |
44 namespace FIFE { | |
45 static Logger _log(LM_GUI); | |
46 | |
47 SubImageFont::SubImageFont(const std::string& filename, const std::string& glyphs, ImagePool& pool) | |
48 : ImageFontBase(), m_pool(pool) { | |
49 | |
50 FL_LOG(_log, LMsg("guichan_image_font, loading ") << filename << " glyphs " << glyphs); | |
51 | |
52 int image_id = m_pool.addResourceFromFile(filename); | |
53 Image& img = dynamic_cast<Image&>(m_pool.get(image_id)); | |
54 SDL_Surface* surface = img.getSurface(); | |
55 | |
56 if( !surface ) { | |
57 throw CannotOpenFile(filename); | |
58 } | |
59 | |
60 // Make sure we get 32bit RGB | |
61 // and copy the Pixelbuffers surface | |
62 SDL_Surface *tmp = SDL_CreateRGBSurface(SDL_SWSURFACE, | |
63 surface->w,surface->h,32, | |
64 RMASK, GMASK, BMASK ,NULLMASK); | |
65 | |
66 SDL_BlitSurface(surface,0,tmp,0); | |
67 surface = tmp; | |
68 | |
69 // Prepare the data for extracting the glyphs. | |
70 uint32_t *pixels = reinterpret_cast<uint32_t*>(surface->pixels); | |
71 | |
72 int x, w; | |
73 x = 0; w=0; | |
74 | |
75 SDL_Rect src; | |
76 | |
77 src.h = surface->h; | |
78 src.y = 0; | |
79 | |
80 uint32_t separator = pixels[0]; | |
81 while(x < surface->w && pixels[x] == separator) | |
82 ++x; | |
83 uint32_t colorkey = pixels[x]; | |
84 | |
85 FL_DBG(_log, LMsg("image_font") | |
86 << " glyph separator is " | |
87 << pprint(reinterpret_cast<void*>(separator)) | |
88 << " transparent color is " | |
89 << pprint(reinterpret_cast<void*>(colorkey))); | |
90 | |
91 // Disable alpha blending, so that we use colorkeying | |
92 SDL_SetAlpha(surface,0,255); | |
93 SDL_SetColorKey(surface,SDL_SRCCOLORKEY,colorkey); | |
94 | |
95 // Finally extract all glyphs | |
96 for(size_t i=0; i != glyphs.size(); ++i) { | |
97 w=0; | |
98 while(x < surface->w && pixels[x] == separator) | |
99 ++x; | |
100 if( x == surface->w ) | |
101 break; | |
102 | |
103 while(x + w < surface->w && pixels[x + w] != separator) | |
104 ++w; | |
105 | |
106 src.x = x; | |
107 src.w = w; | |
108 | |
109 tmp = SDL_CreateRGBSurface(SDL_SWSURFACE, | |
110 w,surface->h,32, | |
111 RMASK, GMASK, BMASK ,NULLMASK); | |
112 | |
113 SDL_FillRect(tmp,0,colorkey); | |
114 SDL_BlitSurface(surface,&src,tmp,0); | |
115 | |
116 // Disable alpha blending, so that we use colorkeying | |
117 SDL_SetAlpha(tmp,0,255); | |
118 SDL_SetColorKey(tmp,SDL_SRCCOLORKEY,colorkey); | |
119 | |
120 | |
121 m_glyphs[ glyphs[i] ].surface = tmp; | |
122 | |
123 x += w; | |
124 } | |
125 | |
126 // Set placeholder glyph | |
127 if( m_glyphs.find('?') != m_glyphs.end() ) { | |
128 m_placeholder = m_glyphs['?']; | |
129 } else { | |
130 m_placeholder.surface = 0; | |
131 } | |
132 | |
133 mHeight = surface->h; | |
134 SDL_FreeSurface(surface); | |
135 } | |
136 | |
137 | |
138 } | |
139 |