00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00024
00026
00028 #include <SFML/Graphics/String.hpp>
00029 #include <SFML/Graphics/FontManager.hpp>
00030 #include <SFML/Graphics/Image.hpp>
00031 #include <SFML/Graphics/OpenGL.hpp>
00032
00033
00037 sfString::sfString(const std::string& Str, const std::string& FontName, const sfColor& Col, float X, float Y, float Rot, float Si) :
00038 Text (Str),
00039 Font (FontName),
00040 Color (Col),
00041 Left (X),
00042 Top (Y),
00043 Rotation(Rot),
00044 Size (Si)
00045 {
00046
00047 }
00048
00049
00053 void sfString::PreloadFont(const std::string& FontName, float CharSize)
00054 {
00055
00056 sf_private::sfFontManager::GetInstance().GetBitmapFont(FontName, static_cast<unsigned int>(CharSize));
00057 }
00058
00059
00063 void sfString::Render(sfRenderWindow& Window)
00064 {
00065
00066 if (Text == "")
00067 return;
00068
00069
00070 const sf_private::sfFontManager::Font& BitmapFont = sf_private::sfFontManager::GetInstance().GetBitmapFont(Font, static_cast<unsigned int>(Size));
00071
00072
00073 sfGLCheck(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
00074
00075
00076 sfGLCheck(glColor4ub(Color.Red, Color.Green, Color.Blue, Color.Alpha));
00077
00078
00079 sfGLCheck(glTranslatef(Left, Top, 0));
00080 sfGLCheck(glRotatef(-Rotation, 0, 0, 1));
00081 sfGLCheck(glScalef(Size / BitmapFont.CharSize, Size / BitmapFont.CharSize, 1));
00082
00083
00084 BitmapFont.Image.Bind();
00085
00086 float X = 0;
00087 float Y = 0;
00088
00089
00090 glBegin(GL_QUADS);
00091 for (std::size_t i = 0; i < Text.size(); ++i)
00092 {
00093
00094 unsigned char c = static_cast<unsigned char>(Text[i]);
00095
00096
00097 const sfIntRect& Rect = BitmapFont.Rect[c];
00098 const sfFloatRect& Coord = BitmapFont.Coord[c];
00099 const unsigned int AdvanceX = BitmapFont.Advance[c];
00100 const unsigned int AdvanceY = BitmapFont.CharSize;
00101
00102
00103 switch (c)
00104 {
00105 case ' ' : X += AdvanceX; continue;
00106 case '\n' : Y += AdvanceY; X = 0; continue;
00107 case '\t' : X += AdvanceX * 4; continue;
00108 case '\v' : Y += AdvanceY * 4; continue;
00109 }
00110
00111
00112 glTexCoord2f(Coord.Left, Coord.Top); glVertex2f(X + Rect.Left, Y + Rect.Top);
00113 glTexCoord2f(Coord.Left, Coord.Bottom); glVertex2f(X + Rect.Left, Y + Rect.Bottom);
00114 glTexCoord2f(Coord.Right, Coord.Bottom); glVertex2f(X + Rect.Right, Y + Rect.Bottom);
00115 glTexCoord2f(Coord.Right, Coord.Top); glVertex2f(X + Rect.Right, Y + Rect.Top);
00116
00117
00118 X += AdvanceX;
00119 }
00120 glEnd();
00121 }
00122
00123
00127 void sfString::GetSize(float& Width, float& Height) const
00128 {
00129
00130 const sf_private::sfFontManager::Font& BitmapFont = sf_private::sfFontManager::GetInstance().GetBitmapFont(Font, static_cast<unsigned int>(Size));
00131
00132
00133 float CurWidth = 0;
00134 Width = 0;
00135 Height = Size;
00136
00137
00138 for (std::size_t i = 0; i < Text.size(); ++i)
00139 {
00140
00141 unsigned char c = static_cast<unsigned char>(Text[i]);
00142
00143
00144 const float Advance = BitmapFont.Advance[c] * Size / BitmapFont.CharSize;
00145
00146
00147 switch (c)
00148 {
00149 case ' ' : CurWidth += Advance; continue;
00150 case '\t' : CurWidth += Size * 4; continue;
00151 case '\v' : Height += Size * 4; continue;
00152
00153 case '\n' :
00154 Height += Size;
00155 if (CurWidth > Width) Width = CurWidth;
00156 CurWidth = 0;
00157 continue;
00158 }
00159
00160
00161 CurWidth += Advance;
00162 }
00163
00164
00165 if (Width < 1)
00166 Width = CurWidth;
00167 }