From f1398b54df5119e6390e49d8244145b23cc575f4 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Sun, 28 Dec 2014 06:15:46 +0300 Subject: [PATCH] Implement basic ttf rendering, use in example --- SDL2pp/Font.cc | 21 ++++++++++++++++++++ SDL2pp/Font.hh | 52 ++++++++++++++++++++++++++++++++++++++++++++----- examples/ttf.cc | 23 +++++++++++++++++++--- 3 files changed, 88 insertions(+), 8 deletions(-) diff --git a/SDL2pp/Font.cc b/SDL2pp/Font.cc index 95da1ba..cd80d67 100644 --- a/SDL2pp/Font.cc +++ b/SDL2pp/Font.cc @@ -60,4 +60,25 @@ TTF_Font* Font::Get() const { return font_; } +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) + throw Exception("TTF_RenderText_Solid failed"); + return Surface(surface); +} + +Surface Font::RenderText_Shaded(const std::string& text, SDL_Color fg, SDL_Color bg) { + SDL_Surface* surface = TTF_RenderText_Shaded(font_, text.c_str(), fg, bg); + if (surface == nullptr) + throw Exception("TTF_RenderText_Shaded failed"); + return Surface(surface); +} + +Surface Font::RenderText_Blended(const std::string& text, SDL_Color fg) { + SDL_Surface* surface = TTF_RenderText_Blended(font_, text.c_str(), fg); + if (surface == nullptr) + throw Exception("TTF_RenderText_Blended failed"); + return Surface(surface); +} + } diff --git a/SDL2pp/Font.hh b/SDL2pp/Font.hh index fd7eeff..b117dcd 100644 --- a/SDL2pp/Font.hh +++ b/SDL2pp/Font.hh @@ -26,11 +26,7 @@ #include -//#include -//#include -//#include - -//struct SDL_PixelFormat; +#include namespace SDL2pp { @@ -118,6 +114,52 @@ public: /// //////////////////////////////////////////////////////////// TTF_Font* Get() const; + + //////////////////////////////////////////////////////////// + /// \brief Render LATIN1 text using solid mode + /// + /// \param text LATIN1 string to render + /// \param 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#SEC43 + /// + //////////////////////////////////////////////////////////// + Surface RenderText_Solid(const std::string& text, SDL_Color fg); + + //////////////////////////////////////////////////////////// + /// \brief Render LATIN1 text using shaded mode + /// + /// \param text LATIN1 string to render + /// \param fg Color to render the text in + /// \param 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#SEC47 + /// + //////////////////////////////////////////////////////////// + Surface RenderText_Shaded(const std::string& text, SDL_Color fg, SDL_Color bg); + + //////////////////////////////////////////////////////////// + /// \brief Render LATIN1 text using blended mode + /// + /// \param text LATIN1 string to render + /// \param 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#SEC51 + /// + //////////////////////////////////////////////////////////// + Surface RenderText_Blended(const std::string& text, SDL_Color fg); }; } diff --git a/examples/ttf.cc b/examples/ttf.cc index bf8dadd..1947510 100644 --- a/examples/ttf.cc +++ b/examples/ttf.cc @@ -28,6 +28,7 @@ #include #include #include +#include #include using namespace SDL2pp; @@ -38,7 +39,15 @@ int Run() { Window window("libSDL2pp demo: font", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE); Renderer render(window, -1, SDL_RENDERER_ACCELERATED); - Font font(TESTDATA_DIR "/Vera.ttf", 10); + Font font(TESTDATA_DIR "/Vera.ttf", 20); + + Surface solid = font.RenderText_Solid("Hello, world!", SDL_Color({255, 255, 255, 255})); + Surface shaded = font.RenderText_Shaded("Hello, world!", SDL_Color({255, 255, 255, 255}), SDL_Color({127, 127, 127, 255})); + Surface blended = font.RenderText_Blended("Hello, world!", SDL_Color({255, 255, 255, 255})); + + Texture solid_tex(render, solid); + Texture shaded_tex(render, shaded); + Texture blended_tex(render, blended); while (1) { // Process input @@ -48,10 +57,18 @@ int Run() { return 0; // Clear screen - render.SetDrawColor(255, 255, 255); + render.SetDrawColor(0, 0, 0); render.Clear(); - // Simple copy + // Render 3 strings + int h = 0; + render.Copy(solid_tex, NullOpt, Rect(0, 0, solid.Get()->w, solid.Get()->h)); + h += solid.Get()->h; + + render.Copy(shaded_tex, NullOpt, Rect(0, 20, shaded.Get()->w, shaded.Get()->h)); + h += shaded.Get()->h; + + render.Copy(blended_tex, NullOpt, Rect(0, 40, blended.Get()->w, blended.Get()->h)); render.Present();