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

Author Topic: Rendering a Text Cursor  (Read 3988 times)

0 Members and 1 Guest are viewing this topic.

lmsmi1

  • Newbie
  • *
  • Posts: 13
  • Console Obsession
    • View Profile
Rendering a Text Cursor
« on: June 22, 2013, 02:41:27 am »
So uhm, I've just "transferred" from SDL and so far... I LOVE SFML!!! I've noticed it has all the basic functions I need for a console simulator all in one archive. I don't have to use it with external libs (like SDL & SDL_ttf for example).

So here's my question: What will I need to do in SFML to render a blinking text cursor on an imitation console window? I've already got some code here for you (that works when it's linked to the correct libs). By the way: I'm using G++ (GCC) TDM on Windows 7 (I compile apps in 32-bit mode.)

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

int main() {
    sf::RenderWindow wnd(sf::VideoMode(650, 300), "SFML Console");
    sf::Vector2u myVector(650, 300);
    wnd.setSize(myVector);

    sf::Font myFont;
    myFont.loadFromFile("theFont.ttf");

    sf::Color myClr;
    myClr.r = 0;
    myClr.g = 203;
    myClr.b = 0;

    sf::String myStr = "Hello world!";

    sf::Text myTxt;
    myTxt.setColor(myClr);
    myTxt.setString(myStr);
    myTxt.setFont(myFont);
    myTxt.setCharacterSize(12);
    myTxt.setStyle(sf::Text::Regular);
    myTxt.setPosition(0, 0);

    while(wnd.isOpen()) {
        sf::Event myEvent;

        while (wnd.pollEvent(myEvent)) {
            if (myEvent.type == sf::Event::Closed) {
                wnd.close();
            }

            if (myEvent.type == sf::Event::KeyPressed) {
                if (myEvent.key.code == sf::Keyboard::Escape) {
                    wnd.close();
                }
            }

            wnd.clear();
            wnd.draw(myTxt);
            wnd.display();
        }
    }
}
 

Thanks in advance.

~lmsmi1

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Rendering a Text Cursor
« Reply #1 on: June 22, 2013, 08:43:03 am »
You have to track mouse events (or real-time input) to know the cursor position. In sf::RenderWindow, you can hide the native cursor and draw your sprite instead.

For the blinking, you can use a sf::Clock.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Rendering a Text Cursor
« Reply #2 on: June 22, 2013, 09:26:24 am »
Position horizontally your cursor at text width + text position, and vertically at text position. (and update it when text size increases/decreases)
To know the width (or height) of an sf::Text you can use its getGlobalBounds method.

You probably already use an sf::Clock in your app to measure the time of each frame.
Make a time counter (an sf::Time variable for example) and add frame time to it each frame. Every time your counter goes over a treshold value (for example if it's over 500ms if you want a 1 sec blinking) change the visibility of your cursor (personally, I lazily change its color to sf::Transparent, but you could keep a boolean value to know whether to display it or not) and reset your counter variable.

 

anything