In this recipe we will learn how we can create text scrolling line-by-line.
We will now create an animation with scrolling text. Perform the following steps to do so:
#include "cinder/gl/Texture.h" #include "cinder/Text.h" #include "cinder/Font.h" #include "cinder/Utilities.h"
vector<gl::Texture> mTextTextures; Vec2f mTextSize;
setup
method we need to generate textures for each line of text.setWindowSize(854, 480); string font( "Times New Roman" ); mTextSize = Vec2f::zero(); į for(int i = 0; i<5; i++) { TextLayout layout; layout.clear( ColorA(0.f,0.f,0.f, 0.f) ); layout.setFont( Font( font, 48 ) ); layout.setColor( Color( 1, 1, 1 ) ); layout.addLine( "Animating text " + toString(i) ); Surface8u rendered = layout.render( true ); gl::TexturetextTexture = gl::Texture( rendered ); textTexture.setMagFilter(GL_NICEST); textTexture.setMinFilter(GL_NICEST); mTextTextures.push_back(textTexture); mTextSize.x = math<float>::max(mTextSize.x, textTexture.getWidth()); mTextSize.y = math<float>::max(mTextSize.y, textTexture.getHeight()); }
draw
method for this example looks as follows:gl::enableAlphaBlending(); gl::clear( Color::black() ); gl::setViewport(getWindowBounds()); gl::setMatricesWindowPersp(getWindowSize()); gl::color(ColorA::white()); float time = getElapsedSeconds()*0.5f; float timeFloor = math<float>::floor( time ); inttexIdx = 1 + ( (int)timeFloor % (mTextTextures.size()-1) ); float step = time - timeFloor; gl::pushMatrices(); gl::translate(getWindowCenter() - mTextSize*0.5f); float radius = 30.f; gl::color(ColorA(1.f,1.f,1.f, 1.f-step)); gl::pushMatrices(); gl::rotate( Vec3f(90.f*step,0.f,0.f) ); gl::translate(0.f,0.f,radius); gl::draw(mTextTextures[texIdx-1], Vec2f(0.f, -mTextTextures[texIdx-1].getHeight()*0.5f) ); gl::popMatrices(); gl::color(ColorA(1.f,1.f,1.f, step)); gl::pushMatrices(); gl::rotate( Vec3f(-90.f + 90.f*step,0.f,0.f) ); gl::translate(0.f,0.f,radius); gl::draw(mTextTextures[texIdx], Vec2f(0.f, -mTextTextures[texIdx].getHeight()*0.5f) ); gl::popMatrices(); gl::popMatrices();
3.145.178.151