Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to Make a text Scrollable in a rectangle?  (Read 1059 times)

0 Members and 1 Guest are viewing this topic.

smusman

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
How to Make a text Scrollable in a rectangle?
« on: March 18, 2018, 08:30:59 am »
I am displaying multiple of lines text inside a rectangle using SFML. Now if my text lines go out of rectangle size vertically, I want to make the text be able scroll using mouse inside the rectangle.

Here is a code for an example:


#include <iostream>
#include <SFML/Graphics.hpp>
using  namespace sf;
using namespace std;

int main()
{
    sf::RenderWindow window(sf::VideoMode(700, 700), "Window");

    RectangleShape rec;
    rec.setFillColor(Color(210,210,210));
    rec.setSize(sf::Vector2f(400,400));
    rec.setPosition(Vector2f(100,100));

    Font font;
    if (!font.loadFromFile("Consolas.ttf"))
    {
        cout<<"Unable to load font"<<endl;
    }

    Text t1[20];
    int text_difference=22;
    int text_incrementer=0;
    for(int var=0;var<20;var++)
    {
    t1[var].setFont(font);
    t1[var].setString("this is a line");
    t1[var].setCharacterSize(20);
    t1[var].setStyle(Text::Bold);
    t1[var].setPosition(Vector2f(110,110+text_incrementer));
    text_incrementer+=text_difference;
    t1[var].setFillColor(Color(255,255,255));
    }



    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(rec);

        for(int i=0; i<20; i++)
        {
            window.draw(t1[i]);
        }

        window.display();
    }

    return 0;
}


 

Please help me write the code which makes the text scroll able.

Hapax

  • Hero Member
  • *****
  • Posts: 3364
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to Make a text Scrollable in a rectangle?
« Reply #1 on: March 18, 2018, 11:16:56 pm »
To scroll something, simply change its position. To scroll vertically, change it y position.

How you then clip that to the rectangle is up to you. Possibly the simplest way would be to set up a render texture that matches the rectangle's size and draw the text to there (with its y offset so that only the part you want to display will be on the texture) and then draw that render texture to the window instead of the text.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything