Merge branch 'char16_t'

This commit is contained in:
Dmitry Marakasov 2015-06-10 00:08:53 +03:00
commit 93dc374280
3 changed files with 124 additions and 22 deletions

View File

@ -19,6 +19,8 @@
3. This notice may not be removed or altered from any source distribution.
*/
#include <vector>
#include <SDL2/SDL_ttf.h>
#include <SDL2pp/Font.hh>
@ -178,6 +180,12 @@ Point Font::GetSizeUNICODE(const Uint16* text) const {
return Point(w, h);
}
Point Font::GetSizeUNICODE(const std::u16string& text) const {
std::vector<Uint16> uint16_text(text.length() + 1);
std::copy(text.begin(), text.end(), uint16_text.begin());
return GetSizeUNICODE(uint16_text.data());
}
Surface Font::RenderText_Solid(const std::string& text, SDL_Color fg) {
SDL_Surface* surface = TTF_RenderText_Solid(font_, text.c_str(), fg);
if (surface == nullptr)
@ -199,6 +207,12 @@ Surface Font::RenderUNICODE_Solid(const Uint16* text, SDL_Color fg) {
return Surface(surface);
}
Surface Font::RenderUNICODE_Solid(const std::u16string& text, SDL_Color fg) {
std::vector<Uint16> uint16_text(text.length() + 1);
std::copy(text.begin(), text.end(), uint16_text.begin());
return Font::RenderUNICODE_Solid(uint16_text.data(), fg);
}
Surface Font::RenderGlyph_Solid(Uint16 ch, SDL_Color fg) {
SDL_Surface* surface = TTF_RenderGlyph_Solid(font_, ch, fg);
if (surface == nullptr)
@ -227,6 +241,12 @@ Surface Font::RenderUNICODE_Shaded(const Uint16* text, SDL_Color fg, SDL_Color b
return Surface(surface);
}
Surface Font::RenderUNICODE_Shaded(const std::u16string& text, SDL_Color fg, SDL_Color bg) {
std::vector<Uint16> uint16_text(text.length() + 1);
std::copy(text.begin(), text.end(), uint16_text.begin());
return Font::RenderUNICODE_Shaded(uint16_text.data(), fg, bg);
}
Surface Font::RenderGlyph_Shaded(Uint16 ch, SDL_Color fg, SDL_Color bg) {
SDL_Surface* surface = TTF_RenderGlyph_Shaded(font_, ch, fg, bg);
if (surface == nullptr)
@ -255,6 +275,12 @@ Surface Font::RenderUNICODE_Blended(const Uint16* text, SDL_Color fg) {
return Surface(surface);
}
Surface Font::RenderUNICODE_Blended(const std::u16string& text, SDL_Color fg) {
std::vector<Uint16> uint16_text(text.length() + 1);
std::copy(text.begin(), text.end(), uint16_text.begin());
return Font::RenderUNICODE_Blended(uint16_text.data(), fg);
}
Surface Font::RenderGlyph_Blended(Uint16 ch, SDL_Color fg) {
SDL_Surface* surface = TTF_RenderGlyph_Blended(font_, ch, fg);
if (surface == nullptr)

View File

@ -518,6 +518,24 @@ public:
////////////////////////////////////////////////////////////
Point GetSizeUNICODE(const Uint16* text) const;
////////////////////////////////////////////////////////////
/// \brief Calculate the resulting surface size of the UNICODE encoded text rendered using font
///
/// \param[in] text UNICODE null terminated string to size up
///
/// \returns Point representing dimensions of the rendered text
///
/// \throws SDL2pp::Exception
///
/// No actual rendering is done, however correct kerning is done
/// to get the actual width. The height returned in h is the same
/// as you can get using GetHeight()
///
/// \see https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html#SEC41
///
////////////////////////////////////////////////////////////
Point GetSizeUNICODE(const std::u16string& text) const;
////////////////////////////////////////////////////////////
/// \brief Render LATIN1 text using solid mode
///
@ -563,6 +581,21 @@ public:
////////////////////////////////////////////////////////////
Surface RenderUNICODE_Solid(const Uint16* text, SDL_Color fg);
////////////////////////////////////////////////////////////
/// \brief Render UNICODE encoded text using solid mode
///
/// \param[in] text UNICODE encoded string to render
/// \param[in] fg Color to render the text in
///
/// \returns Surface containing rendered text
///
/// \throws SDL2pp::Exception
///
/// \see https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html#SEC45
///
////////////////////////////////////////////////////////////
Surface RenderUNICODE_Solid(const std::u16string& text, SDL_Color fg);
////////////////////////////////////////////////////////////
/// \brief Render the glyph for UNICODE character using solid mode
///
@ -626,6 +659,22 @@ public:
////////////////////////////////////////////////////////////
Surface RenderUNICODE_Shaded(const Uint16* text, SDL_Color fg, SDL_Color bg);
////////////////////////////////////////////////////////////
/// \brief Render UNICODE encoded text using shaded mode
///
/// \param[in] text UNICODE encoded string to render
/// \param[in] fg Color to render the text in
/// \param[in] bg Color to render the background box in
///
/// \returns Surface containing rendered text
///
/// \throws SDL2pp::Exception
///
/// \see https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html#SEC49
///
////////////////////////////////////////////////////////////
Surface RenderUNICODE_Shaded(const std::u16string& text, SDL_Color fg, SDL_Color bg);
////////////////////////////////////////////////////////////
/// \brief Render the glyph for UNICODE character using shaded mode
///
@ -667,7 +716,7 @@ public:
///
/// \throws SDL2pp::Exception
///
/// \see https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html#SEC48
/// \see https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html#SEC52
///
////////////////////////////////////////////////////////////
Surface RenderUTF8_Blended(const std::string& text, SDL_Color fg);
@ -682,11 +731,26 @@ public:
///
/// \throws SDL2pp::Exception
///
/// \see https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html#SEC49
/// \see https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html#SEC53
///
////////////////////////////////////////////////////////////
Surface RenderUNICODE_Blended(const Uint16* text, SDL_Color fg);
////////////////////////////////////////////////////////////
/// \brief Render UNICODE encoded text using blended mode
///
/// \param[in] text UNICODE encoded string to render
/// \param[in] fg Color to render the text in
///
/// \returns Surface containing rendered text
///
/// \throws SDL2pp::Exception
///
/// \see https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html#SEC53
///
////////////////////////////////////////////////////////////
Surface RenderUNICODE_Blended(const std::u16string& text, SDL_Color fg);
////////////////////////////////////////////////////////////
/// \brief Render the glyph for UNICODE character using blended mode
///
@ -697,7 +761,7 @@ public:
///
/// \throws SDL2pp::Exception
///
/// \see https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html#SEC50
/// \see https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html#SEC54
///
////////////////////////////////////////////////////////////
Surface RenderGlyph_Blended(Uint16 ch, SDL_Color fg);

View File

@ -20,6 +20,9 @@
*/
#include <iostream>
#include <vector>
#include <SDL2/SDL_stdinc.h>
#include <SDL2pp/SDL.hh>
#include <SDL2pp/SDLTTF.hh>
@ -38,17 +41,32 @@ int main() try {
Font font(TESTDATA_DIR "/Vera.ttf", 30);
Surface solid = font.RenderText_Solid("Hello, world! (solid mode)", SDL_Color{255, 255, 255, 255});
Surface shaded = font.RenderText_Shaded("Hello, world! (shaded mode)", SDL_Color{255, 255, 255, 255}, SDL_Color{127, 127, 127, 255});
Surface blended = font.RenderText_Blended("Hello, world! (blended mode)", SDL_Color{255, 255, 255, 255});
std::vector<Texture> textures;
textures.emplace_back(render,
font.RenderText_Solid("Hello, world! (solid mode)", SDL_Color{255, 255, 255, 255})
);
textures.emplace_back(render,
font.RenderText_Shaded("Hello, world! (shaded mode)", SDL_Color{255, 255, 255, 255}, SDL_Color{127, 127, 127, 255})
);
textures.emplace_back(render,
font.RenderText_Blended("Hello, world! (blended mode)", SDL_Color{255, 255, 255, 255})
);
font.SetOutline(1);
Surface outline = font.RenderText_Blended("Hello, world! (blended + outline)", SDL_Color{255, 255, 255, 255});
Texture solid_tex(render, solid);
Texture shaded_tex(render, shaded);
Texture blended_tex(render, blended);
Texture outline_tex(render, outline);
textures.emplace_back(render,
font.RenderText_Blended("Hello, world! (blended + outline)", SDL_Color{255, 255, 255, 255})
);
font.SetOutline(0);
textures.emplace_back(render,
font.RenderUTF8_Blended(u8"Hello, world! «¼½¾» (UTF-8 support)", SDL_Color{255, 255, 255, 255})
);
textures.emplace_back(render,
font.RenderUNICODE_Blended(u"Hello, world! «¼½¾» (UTF-16 support)", SDL_Color{255, 255, 255, 255})
);
while (1) {
// Process input
@ -61,18 +79,12 @@ int main() try {
render.SetDrawColor(0, 63, 63);
render.Clear();
// Render 3 strings
// Render all strings
int h = 0;
render.Copy(solid_tex, NullOpt, Rect(0, h, solid.GetWidth(), solid.GetHeight()));
h += solid.Get()->h;
render.Copy(shaded_tex, NullOpt, Rect(0, h, shaded.GetWidth(), shaded.GetHeight()));
h += shaded.Get()->h;
render.Copy(blended_tex, NullOpt, Rect(0, h, blended.GetWidth(), blended.GetHeight()));
h += blended.Get()->h;
render.Copy(outline_tex, NullOpt, Rect(0, h, outline.GetWidth(), outline.GetHeight()));
for (auto& texture: textures) {
render.Copy(texture, NullOpt, Rect(0, h, texture.GetWidth(), texture.GetHeight()));
h += texture.GetHeight();
}
render.Present();