D:/Programmation/Cpp/SFML/src/SFML/Graphics/String.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00026 // Headers
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     // Requesting the font will make the font manager create it
00056     sf_private::sfFontManager::GetInstance().GetBitmapFont(FontName, static_cast<unsigned int>(CharSize));
00057 }
00058 
00059 
00063 void sfString::Render(sfRenderWindow& Window)
00064 {
00065     // No text, no rendering :)
00066     if (Text == "")
00067         return;
00068 
00069     // Get bitmap font from font manager
00070     const sf_private::sfFontManager::Font& BitmapFont = sf_private::sfFontManager::GetInstance().GetBitmapFont(Font, static_cast<unsigned int>(Size));
00071 
00072     // Setup alpha-blending
00073     sfGLCheck(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
00074 
00075     // Set color
00076     sfGLCheck(glColor4ub(Color.Red, Color.Green, Color.Blue, Color.Alpha));
00077 
00078     // Set transformations
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     // Set font texture
00084     BitmapFont.Image.Bind();
00085 
00086     float X = 0;
00087     float Y = 0;
00088 
00089     // Draw the sprite
00090     glBegin(GL_QUADS);
00091     for (std::size_t i = 0; i < Text.size(); ++i)
00092     {
00093         // Get current character
00094         unsigned char c = static_cast<unsigned char>(Text[i]);
00095 
00096         // Get dimensions of current character from font description
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         // Handle special characters...
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         // Draw a textured quad for current character
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         // Advance to next character
00118         X += AdvanceX;
00119     }
00120     glEnd();
00121 }
00122 
00123 
00127 void sfString::GetSize(float& Width, float& Height) const
00128 {
00129     // Get bitmap font from font manager
00130     const sf_private::sfFontManager::Font& BitmapFont = sf_private::sfFontManager::GetInstance().GetBitmapFont(Font, static_cast<unsigned int>(Size));
00131 
00132     // Reset values
00133     float CurWidth = 0;
00134     Width  = 0;
00135     Height = Size;
00136 
00137     // Go through each character
00138     for (std::size_t i = 0; i < Text.size(); ++i)
00139     {
00140         // Get current character
00141         unsigned char c = static_cast<unsigned char>(Text[i]);
00142 
00143         // Get dimensions of current character from font description
00144         const float Advance = BitmapFont.Advance[c] * Size / BitmapFont.CharSize;
00145 
00146         // Handle special characters...
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         // Advance to next character
00161         CurWidth += Advance;
00162     }
00163 
00164     // In case there was no '\n' in the string
00165     if (Width < 1)
00166         Width = CurWidth;
00167 }

Generated for SFML by  doxygen 1.5.2