From c59414b121d0d4c7fbba0b08ba6304df2d2b5607 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boldizs=C3=A1r=20Lipka?= Date: Tue, 17 Jun 2014 14:11:44 +0200 Subject: [PATCH] Enable rendering text as a texture. --- src/text.cpp | 16 ++++++++++++++++ src/text.hpp | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/text.cpp b/src/text.cpp index b95d0c19c66..cb2ba282444 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -29,6 +29,10 @@ #include #include +#if SDL_VERSION_ATLEAST(2,0,0) +#include "video.hpp" +#endif + namespace font { namespace { @@ -155,6 +159,14 @@ surface ttext::render() const return surface_; } +#if SDL_VERSION_ATLEAST(2,0,0) +sdl::ttexture ttext::render_as_texture() const +{ + rerender(); + return texture_; +} +#endif + int ttext::get_width() const { return get_size().x; @@ -684,6 +696,10 @@ void ttext::rerender(const bool force) const surface_.assign(SDL_CreateRGBSurfaceFrom( surface_buffer_, width, height, 32, stride, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000)); +#if SDL_VERSION_ATLEAST(2,0,0) + texture_ = CVideo::get_window()->create_texture + (SDL_TEXTUREACCESS_STATIC, surface_); +#endif cairo_destroy(cr); cairo_surface_destroy(cairo_surface); } diff --git a/src/text.hpp b/src/text.hpp index 488c1f4f2d1..81a2abc93d2 100644 --- a/src/text.hpp +++ b/src/text.hpp @@ -25,6 +25,10 @@ #include +#if SDL_VERSION_ATLEAST(2,0,0) +#include "sdl/texture.hpp" +#endif + struct language_def; namespace gui2 { @@ -72,6 +76,16 @@ public: */ surface render() const; +#if SDL_VERSION_ATLEAST(2,0,0) + /** + * Returns the rendered text as a texture. + * + * Before rendering it tests whether a redraw is needed and if so it first + * redraws the texture before returning it. + */ + sdl::ttexture render_as_texture() const; +#endif + /** Returns the width needed for the text. */ int get_width() const; @@ -208,6 +222,11 @@ private: /** The surface to render upon used as a cache. */ mutable surface surface_; +#if SDL_VERSION_ATLEAST(2,0,0) + /** The texture to render upon used as a cache. */ + mutable sdl::ttexture texture_; +#endif + /** The text to draw (stored as UTF-8). */ std::string text_;