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

Author Topic: Scrolling Text..  (Read 8250 times)

0 Members and 1 Guest are viewing this topic.

rahul8590

  • Newbie
  • *
  • Posts: 25
    • View Profile
Scrolling Text..
« on: March 16, 2010, 08:54:37 pm »
Well i have been using SFML for past 3-4 days and have completely fallen in love with it , its awesome .. :)

Well i have uploaded an image and also , text (arial) .
Now , i want the text to be scrolling , how do i do that .. ?

I am attaching the code in here :
Code: [Select]

#include <SFML/Graphics.hpp>
#include<iostream>

 int main()
 {
     // Create the main window
     sf::RenderWindow App(sf::VideoMode(703, 493), "Rahul.Reincarnated ");

     // Load a sprite to display
     sf::Image Image;
     if (!Image.LoadFromFile("image1.jpg"))
         return EXIT_FAILURE;
     sf::Sprite Sprite(Image);

     // Create a graphical string to display
     sf::Font Arial;
     if (!Arial.LoadFromFile("arial.ttf"))
         return EXIT_FAILURE;
     sf::String Text("\t \t Welcome to OpenGL Project \n \t \t \t \t \t \t \t \t \t \t \t By Rahul ", Arial, 50);

    while (App.IsOpened())
     {
         // Process events
         sf::Event Event;
         while (App.GetEvent(Event))
         {
             // Close window : exit
             if (Event.Type == sf::Event::Closed)
                 App.Close();

            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                App.Close();

         }

         // Clear screen
         App.Clear();

         // Draw the sprite
         App.Draw(Sprite);

         // Draw the string
         App.Draw(Text);

         // Update the window
         App.Display();
     }

    std::cout<<" thank you for using the program ";
    getchar();

     return EXIT_SUCCESS;
 }



Now , if i want the welcome text to be entering into the window (scroll) is there any way to do that.. ?

Haze

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Github Profile
Scrolling Text..
« Reply #1 on: March 16, 2010, 09:15:58 pm »
I'm not sure of what you mean by "scrolling text", is that just moving the text in a vertical way?
This can be simply done by adding 3 lines of code:

Code: [Select]

#include <SFML/Graphics.hpp>
#include<iostream>

int main()
{
    // Create the main window
    sf::RenderWindow App(sf::VideoMode(703, 493), "Rahul.Reincarnated ");

    // Load a sprite to display
    sf::Image Image;
    if (!Image.LoadFromFile("image1.jpg"))
     return EXIT_FAILURE;
    sf::Sprite Sprite(Image);

    // Create a graphical string to display
    sf::Font Arial;
    if (!Arial.LoadFromFile("arial.ttf"))
     return EXIT_FAILURE;
    sf::String Text("\t \t Welcome to OpenGL Project \n \t \t \t \t \t \t \t \t \t \t \t By Rahul ", Arial, 50);

    const int SCROLL_SPEED = 20; // 20 pixels per second
    Text.SetY(-Text.GetRect().GetHeight()); // text position is just above the screen
   
    while (App.IsOpened())
    {
         // Process events
         sf::Event Event;
         while (App.GetEvent(Event))
         {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                 App.Close();

            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                App.Close();
         }
         // increase Y axis, move down
         Text.Move(0, App.GetFrameTime() * SCROLL_SPEED);
         
         // Clear screen
         App.Clear();

         // Draw the sprite
         App.Draw(Sprite);

         // Draw the string
         App.Draw(Text);

         // Update the window
         App.Display();
     }

    std::cout<<" thank you for using the program ";
    getchar();

    return EXIT_SUCCESS;
}

rahul8590

  • Newbie
  • *
  • Posts: 25
    • View Profile
Scrolling Text..
« Reply #2 on: March 17, 2010, 04:40:54 pm »
Voila ... ! It worked ..

But The point is , i want it to scroll it continuously , then wat do i do .
I tried putting that text scroll inside  App.GetEvent

Code: [Select]

Text.Move( 0 ,App.GetFrameTime() * SCROLL_SPEED) ;


and i observed the scroll was in compliance to my mouse action , i mean if my mouse was moving then only the text was scrolling down .

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Scrolling Text..
« Reply #3 on: March 17, 2010, 04:48:49 pm »
Quote
But The point is , i want it to scroll it continuously

It should, with the code above. The text is moved slightly at every iteration of the main loop.

Quote
and i observed the scroll was in compliance to my mouse action , i mean if my mouse was moving then only the text was scrolling down .

This is because your text will move everytime there is an event, and MouseMove is one of them ;)
Laurent Gomila - SFML developer

rahul8590

  • Newbie
  • *
  • Posts: 25
    • View Profile
Scrolling Text..
« Reply #4 on: March 17, 2010, 06:29:49 pm »
Quote from: Laurent
Quote
But The point is , i want it to scroll it continuously

It should, with the code above. The text is moved slightly at every iteration of the main loop.


Well its not , its just happening once . The test is vertically scrolling and coming down and later the same process is not happening again.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Scrolling Text..
« Reply #5 on: March 17, 2010, 07:31:36 pm »
Ah, so that's what you meant. You just have to reset the Y position of the text when it is greater than the window height.
Laurent Gomila - SFML developer

rahul8590

  • Newbie
  • *
  • Posts: 25
    • View Profile
Scrolling Text..
« Reply #6 on: March 18, 2010, 11:17:09 pm »
Well i tried doing that in various ways . but i am getting errors .

intially i i wrote

Code: [Select]

while (Text.Position() ! = 100 )
Text.Move( 0 ,App.GetFrameTime() * SCROLL_SPEED) ;



I mean , i wanted the scrolling to take place till a certain position

but how do i get to know the position ? I was going through the documentation , but could understand much from it.

model76

  • Full Member
  • ***
  • Posts: 231
    • View Profile
Scrolling Text..
« Reply #7 on: March 19, 2010, 12:16:25 am »
Quote from: "rahul8590"
Well i tried doing that in various ways . but i am getting errors .

intially i i wrote

Code: [Select]

while (Text.Position() ! = 100 )
Text.Move( 0 ,App.GetFrameTime() * SCROLL_SPEED) ;


That would give you some problems, as
a) Position() is not a member of sf::Drawable,
and b) the y-position will likely not be exactly 100 at the start of the loop, so the loop will never run,
anc c) if that while loop is inside your main loop, even if correctly done, it will just move the text, and never show it on the screen.

In your main loop, try something like this instead:
Code: [Select]
Text.Move( 0.f, App.GetFrameTime() * SCROLL_SPEED );

if( Text.GetPosition().y > END_POSITION )
    Text.SetPosition( 0.f, START_POSITION );
That is if you want the text to scroll down. If you want it to scroll up, you will of course need to modify the if statement.

rahul8590

  • Newbie
  • *
  • Posts: 25
    • View Profile
Scrolling Text..
« Reply #8 on: March 20, 2010, 07:34:47 pm »
awesome... it worked  :lol: