From aef6b00a6276c82cd97327817aba680833d15df1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 27 Feb 2018 19:22:48 +0100 Subject: [PATCH] doc: compiled code snippets for the Text namespace. Everything was wrong here. Ugh. --- doc/snippets/CMakeLists.txt | 3 +- doc/snippets/MagnumText.cpp | 100 ++++++++++++++++++++++ src/Magnum/Text/DistanceFieldGlyphCache.h | 8 +- src/Magnum/Text/GlyphCache.h | 8 +- src/Magnum/Text/Renderer.h | 41 +-------- 5 files changed, 106 insertions(+), 54 deletions(-) create mode 100644 doc/snippets/MagnumText.cpp diff --git a/doc/snippets/CMakeLists.txt b/doc/snippets/CMakeLists.txt index 81a21617c..f433f47fd 100644 --- a/doc/snippets/CMakeLists.txt +++ b/doc/snippets/CMakeLists.txt @@ -35,7 +35,8 @@ endif() add_library(snippets STATIC Magnum.cpp - MagnumShaders.cpp) + MagnumShaders.cpp + MagnumText.cpp) target_link_libraries(snippets PRIVATE Magnum) set_target_properties(snippets PROPERTIES FOLDER "Magnum/doc/snippets") diff --git a/doc/snippets/MagnumText.cpp b/doc/snippets/MagnumText.cpp new file mode 100644 index 000000000..7534ce9fe --- /dev/null +++ b/doc/snippets/MagnumText.cpp @@ -0,0 +1,100 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 + Vladimír Vondruš + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +#include "Magnum/Shaders/Vector.h" +#include "Magnum/Text/AbstractFont.h" +#include "Magnum/Text/DistanceFieldGlyphCache.h" +#include "Magnum/Text/Renderer.h" + +using namespace Magnum; +using namespace Magnum::Math::Literals; + +int main() { + +{ +/* [DistanceFieldGlyphCache-usage] */ +std::unique_ptr font; +Text::DistanceFieldGlyphCache cache{Vector2i{2048}, Vector2i{384}, 16}; +font->fillGlyphCache(cache, "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789?!:;,. "); +/* [DistanceFieldGlyphCache-usage] */ +} + +{ +/* [GlyphCache-usage] */ +std::unique_ptr font; +Text::GlyphCache cache{Vector2i{512}}; +font->fillGlyphCache(cache, "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789?!:;,. "); +/* [GlyphCache-usage] */ +} + +{ +Matrix3 projectionMatrix; +/* [Renderer-usage1] */ +/* Font instance, received from a plugin manager */ +std::unique_ptr font; + +/* Configured glyph cache */ +Text::GlyphCache cache{Vector2i{512}}; +font->fillGlyphCache(cache, "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789?!:;,. "); + +Shaders::Vector2D shader; +Buffer vertexBuffer, indexBuffer; +Mesh mesh; + +/* Render the text, centered */ +std::tie(mesh, std::ignore) = Text::Renderer2D::render(*font, cache, 0.15f, + "Hello World!", vertexBuffer, indexBuffer, BufferUsage::StaticDraw, + Text::Alignment::LineCenter); + +/* Draw the text on the screen */ +shader.setTransformationProjectionMatrix(projectionMatrix) + .setColor(0xffffff_rgbf) + .setVectorTexture(cache.texture()); +mesh.draw(shader); +/* [Renderer-usage1] */ + +/* [Renderer-usage2] */ +/* Initialize the renderer and reserve memory for enough glyphs */ +Text::Renderer2D renderer{*font, cache, 0.15f, Text::Alignment::LineCenter}; +renderer.reserve(32, BufferUsage::DynamicDraw, BufferUsage::StaticDraw); + +/* Update the text occasionally */ +renderer.render("Hello World Countdown: 10"); + +/* Draw the text on the screen */ +shader.setTransformationProjectionMatrix(projectionMatrix) + .setColor(0xffffff_rgbf) + .setVectorTexture(cache.texture()); +renderer.mesh().draw(shader); +/* [Renderer-usage2] */ +} + +} diff --git a/src/Magnum/Text/DistanceFieldGlyphCache.h b/src/Magnum/Text/DistanceFieldGlyphCache.h index 179e6e301..b49c31825 100644 --- a/src/Magnum/Text/DistanceFieldGlyphCache.h +++ b/src/Magnum/Text/DistanceFieldGlyphCache.h @@ -45,13 +45,7 @@ format is red channel only. Usage is similar to @ref GlyphCache, additionally you need to specify size of resulting distance field texture. -@code{.cpp} -Text::AbstractFont* font; -Text::GlyphCache* cache = new Text::DistanceFieldGlyphCache(Vector2i(2048), Vector2i(384)); -font->createGlyphCache(cache, "abcdefghijklmnopqrstuvwxyz" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "0123456789?!:;,. "); -@endcode +@snippet MagnumText.cpp DistanceFieldGlyphCache-usage @see @ref TextureTools::distanceField() */ diff --git a/src/Magnum/Text/GlyphCache.h b/src/Magnum/Text/GlyphCache.h index 52fde7f5f..7428f15fe 100644 --- a/src/Magnum/Text/GlyphCache.h +++ b/src/Magnum/Text/GlyphCache.h @@ -48,13 +48,7 @@ Contains font glyphs prerendered into texture atlas. Create GlyphCache object with sufficient size and then call @ref AbstractFont::createGlyphCache() to fill it with glyphs. -@code{.cpp} -Text::AbstractFont* font; -Text::GlyphCache* cache = new GlyphCache(Vector2i(512)); -font->createGlyphCache(cache, "abcdefghijklmnopqrstuvwxyz" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "0123456789?!:;,. "); -@endcode +@snippet MagnumText.cpp GlyphCache-usage See @ref Renderer for information about text rendering. @todo Some way for Font to negotiate or check internal texture format diff --git a/src/Magnum/Text/Renderer.h b/src/Magnum/Text/Renderer.h index d4b9be75f..342271342 100644 --- a/src/Magnum/Text/Renderer.h +++ b/src/Magnum/Text/Renderer.h @@ -178,27 +178,7 @@ methods, returning result either as data arrays or as fully configured mesh. The text can be then drawn as usual by configuring the shader and drawing the mesh: -@code{.cpp} -// Font instance, received from plugin manager -std::unique_ptr font; - -// Configured glyph cache -Text::GlyphCache cache; - -Shaders::Vector2D shader; -Buffer vertexBuffer, indexBuffer; -Mesh mesh; - -// Render the text, centered -std::tie(mesh, std::ignore) = Text::Renderer2D::render(*font, cache, 0.15f, - "Hello World!", vertexBuffer, indexBuffer, BufferUsage::StaticDraw, Text::Alignment::LineCenter); - -// Draw the text on the screen -shader.setTransformationProjectionMatrix(projection) - .setColor(Color3(1.0f)) - .setVectorTexture(glyphCache->texture()); -mesh.draw(shader); -@endcode +@snippet MagnumText.cpp Renderer-usage1 See @ref render(AbstractFont&, const GlyphCache&, Float, const std::string&, Alignment) and @ref render(AbstractFont&, const GlyphCache&, Float, const std::string&, Buffer&, Buffer&, BufferUsage, Alignment) @@ -208,24 +188,7 @@ While this method is sufficient for one-shot rendering of static texts, for mutable texts (e.g. FPS counters, chat messages) there is another approach that doesn't recreate everything on each text change: -@code{.cpp} -std::unique_ptr font; -Text::GlyphCache cache; -Shaders::Vector2D shader; - -// Initialize renderer and reserve memory for enough glyphs -Text::Renderer2D renderer(*font, cache, 0.15f); -renderer.reserve(32, BufferUsage::DynamicDraw, BufferUsage::StaticDraw, Text::Alignment::LineCenter); - -// Update the text occasionally -renderer.render("Hello World Countdown: 10"); - -// Draw the text on the screen -shader.setTransformationProjectionMatrix(projection) - .setColor(Color3(1.0f)) - .setVectorTexture(glyphCache->texture()); -renderer.mesh().draw(shader); -@endcode +@snippet MagnumText.cpp Renderer-usage2 @section Text-Renderer-required-opengl-functionality Required OpenGL functionality