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

Author Topic: Blinking Functionality to the Cursor  (Read 2048 times)

0 Members and 1 Guest are viewing this topic.

tigerBellySleeper

  • Newbie
  • *
  • Posts: 1
    • View Profile
Blinking Functionality to the Cursor
« on: April 03, 2021, 06:49:13 pm »
I have created a Typing Class that displays keyboard events onto an SFML window. I am not trying to create a Cursor class that will show a blinking cursor onto the same SFML window.
My thinking is to have the addEventHandler function read the mouse input, then the actual blinking occurs in my update function. I want the blinking to occur every 500ms, I'm stuck on what else I need to make the blinking occur on the SFML window. Thanks in advance.
class Cursor : public sf::Drawable{
public:
    Cursor();
    void addEventHandler(sf::RenderWindow& window, sf::Event event);
    void update();
    virtual void draw(sf::RenderTarget& window, sf::RenderStates states) const;


private:
    sf::Cursor cursor;
    sf::Clock clock;
    bool showCursor;
    const sf::Time delay;

};
 
////////////////////Cursor.cpp//////////////////////////
#include "Cursor.h"

Cursor::Cursor(){
}

void Cursor::addEventHandler(sf::RenderWindow &window, sf::Event event) {
    if(event.type == sf::Event::MouseButtonPressed)
    {
        std::cout << "Mouse" << std::endl;
        if (cursor.loadFromSystem(sf::Cursor::Arrow)){
            window.setMouseCursor(cursor);
            window.setMouseCursorVisible(true);
        }
    }
}

void Cursor::update() {
    if(clock.getElapsedTime() >= sf::milliseconds(500))
    {
        clock.restart();
        cursor.loadFromSystem(sf::Cursor::Arrow);
        std::cout<<"Update"<<std::endl;
    }
}

void Cursor::draw(sf::RenderTarget &window, sf::RenderStates states) const {

}
 
////////////////////main.cpp//////////////////////////
#include <iostream>
#include "SFML/Graphics.hpp"
#include "Typing.h"
#include "Cursor.h"

int main() {
    sf::RenderWindow window;
    window.create(sf::VideoMode(800,600), "Typing in SFML");
    Typing typing;
    Cursor cursor;
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
            {
                window.close();
            }
            typing.addEventHandler(window, event);
            cursor.addEventHandler(window, event);
        }
        cursor.update();
        typing.update();
        window.clear();
        window.draw(typing);
        window.draw(cursor);
        window.display();
    }
    return 0;
}
 
« Last Edit: April 28, 2021, 09:47:08 am by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: Blinking Functionality to the Cursor
« Reply #1 on: April 28, 2021, 09:52:47 am »
Why would you want your mouse cursor to blink?

All you do currently is to load the arrow cursor every 500ms.
Not sure how expensive that is, but if you intend to reuse the same mouse cursor every now and then it's probably better to store them, instead of reloading every few milliseconds.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything