Efeito Digitação com C++ e SFML

📱 Youtube Shorts, TikTok e Reels.


Efeito Digitação com C++ e SFML


Youtube Shorts


TikTok

@terminalroot

Efeito digitação com C++ e SFML Código: https://terminalroot.com.br/2025/06/efeito-digitacao-com-cpp-e-sfml.html #cpp #sfml #typing

♬ som original - terminalroot

Reels

https://www.instagram.com/p/DLOcxv1xuoq/


Código

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

int main(){
  sf::RenderWindow window(sf::VideoMode(450,800), "Typing Effect"); 

  sf::Font font;
  if(!font.loadFromFile("./arial.ttf")){
    std::cerr << "Failed to load font.\n";
    return EXIT_FAILURE;
  }

  std::wstring full_text = L"#include <iostream>\n"
  " std::cout << \"Efeito digitação!\\n\";\n"
  " std::cout << \"C++ e SFML\\n\";\n"
  " std::cout << \"Deixe o LIKE\\n\";\n"
  " std::cout << \"Se inscreva!\\n\";\n"
  " std::cout << \"@TerminalRootTV\\n\";\n"
  " return EXIT_SUCCESS;\n"
  "}\n";

  std::vector<std::wstring> words;
  std::wstring current_word;

  for(wchar_t c : full_text){
    if(c == L' ' || c == L'\n' || iswpunct(c)){
      if(!current_word.empty()){
        words.push_back(current_word);
        current_word.clear();
      }
      words.push_back(std::wstring(1, c));
    }else{
      current_word += c;
    }
  }

  if(!current_word.empty()){
    words.push_back(current_word);
  }

  std::vector<sf::Text> text_parts;
  float x = 50, y = 50;
  size_t word_index = 0, char_index = 0;
  sf::Clock clock;
  float typing_speed = 0.05f;


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


    if(word_index < words.size()){
      if(clock.getElapsedTime().asSeconds() > typing_speed){
        
        wchar_t c = words[word_index][char_index];

        sf::Text new_text;
        new_text.setFont(font);
        new_text.setCharacterSize(24);
        new_text.setFillColor(sf::Color::White);
        new_text.setString(std::wstring(1, c));
        new_text.setPosition(x, y);

        text_parts.push_back(new_text);

        if(c == L'\n'){
          x = 50;
          y += 30;
        }else{
          x += new_text.getGlobalBounds().width + 2;
        }

        char_index++;
        if(char_index >= words[word_index].size()){
          word_index++;
          char_index = 0;
        }
        clock.restart();
      }
    }

    window.clear(sf::Color::Black);

    for(const auto& target : text_parts){
      window.draw(target);
    }

    window.display();
  }

  return EXIT_SUCCESS;
}
// g++ main.cpp -lsfml-graphics -lsfml-window -lsfml-system

sfml shorts cpp


Compartilhe


Nosso canal no Youtube

Inscreva-se


Marcos Oliveira

Marcos Oliveira

Desenvolvedor de software
https://github.com/terroo


Crie Aplicativos Gráficos para Linux e Windows com C++

Aprenda C++ Moderno e crie Games, Programas CLI, GUI e TUI de forma fácil.

Saiba Mais

Receba as novidades no seu e-mail!

Após cadastro e confirmação do e-mail, enviaremos semanalmente resumos e também sempre que houver novidades por aqui para que você mantenha-se atualizado!