Implement basic ttf rendering, use in example

This commit is contained in:
Dmitry Marakasov 2014-12-28 06:15:46 +03:00
parent ffdeb9465d
commit f1398b54df
3 changed files with 88 additions and 8 deletions

View File

@ -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);
}
}

View File

@ -26,11 +26,7 @@
#include <SDL2/SDL_ttf.h>
//#include <SDL2pp/Config.hh>
//#include <SDL2pp/Optional.hh>
//#include <SDL2pp/Rect.hh>
//struct SDL_PixelFormat;
#include <SDL2pp/Surface.hh>
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);
};
}

View File

@ -28,6 +28,7 @@
#include <SDL2pp/Font.hh>
#include <SDL2pp/Window.hh>
#include <SDL2pp/Renderer.hh>
#include <SDL2pp/Texture.hh>
#include <SDL2pp/Exception.hh>
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();