Skip to content

Commit

Permalink
Enable rendering text as a texture.
Browse files Browse the repository at this point in the history
  • Loading branch information
lipk committed Jun 17, 2014
1 parent 976e439 commit c59414b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/text.cpp
Expand Up @@ -29,6 +29,10 @@
#include <cassert>
#include <cstring>

#if SDL_VERSION_ATLEAST(2,0,0)
#include "video.hpp"
#endif

namespace font {

namespace {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
19 changes: 19 additions & 0 deletions src/text.hpp
Expand Up @@ -25,6 +25,10 @@

#include <string>

#if SDL_VERSION_ATLEAST(2,0,0)
#include "sdl/texture.hpp"
#endif

struct language_def;

namespace gui2 {
Expand Down Expand Up @@ -72,6 +76,16 @@ class ttext
*/
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;

Expand Down Expand Up @@ -208,6 +222,11 @@ class ttext
/** 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_;

Expand Down

0 comments on commit c59414b

Please sign in to comment.