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

Author Topic: How to control text  (Read 1414 times)

0 Members and 1 Guest are viewing this topic.

Rohan

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
How to control text
« on: May 22, 2018, 10:40:13 am »
I take text using textentered but when i dra it on screen like i press  4 it print 44444444444444444
how to solve it
if (event.type == sf::Event::TextEntered){
            int g = 0;
         if (event.text.unicode >= 48 && event.text.unicode <= 57){

               d = d + (char)(event.text.unicode);
               b.setString(d);
« Last Edit: May 22, 2018, 10:52:12 am by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10838
    • View Profile
    • development blog
    • Email
Re: How to control text
« Reply #1 on: May 22, 2018, 11:04:34 am »
Can you provide a complete and minimal example?

Also make sure to use the Help sections for asking for help and to use [code=cpp][/code] tags for posting code.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: How to control text
« Reply #2 on: May 22, 2018, 11:13:39 am »
Make sure you use events only inside the event loop.

Rohan

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: How to control text
« Reply #3 on: May 22, 2018, 11:58:51 am »
Example :
   like press 3 now see its  output on screen shot

Rohan

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: How to control text
« Reply #4 on: May 22, 2018, 12:00:38 pm »
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
using namespace std;
#include <SFML\Graphics.hpp>
#include <SFML\Audio.hpp>
#include <cmath>
#include <ctime>
#include <iostream>
#include <windows.h>
#include <fstream>
#include <sstream>

using namespace std;
using namespace sf;
int entername();
int main()
{

    sf::RenderWindow window(sf::VideoMode(1024,768), "Time Enter ");
    sf::Vector2f windowCenter = sf::Vector2f(window.getSize().x / 2.0f, window.getSize().y / 2.0f);

    sf::Font font;
    font.loadFromFile("arial.ttf");

    sf::Text hour("Enter Hours :", font);
    hour.setCharacterSize(40);
    hour.setOrigin(500,200);
    hour.setPosition(window.getSize().x / 2 + 2, window.getSize().y / 2 + 2);
    hour.setStyle(sf::Text::Bold);
    hour.setColor(sf::Color::Red);

    sf::Text minute("Enter Minutes :", font);
    minute.setCharacterSize(40);
    minute.setOrigin(500,100);
    minute.setPosition(window.getSize().x / 2 + 2, window.getSize().y / 2 + 2);
    minute.setStyle(sf::Text::Bold);
    minute.setColor(sf::Color::Red);

    sf::Texture clockBrand;
    if (!clockBrand.loadFromFile("rohan.png")){return EXIT_FAILURE;}
    sf::Sprite clockBrandSprite;
    clockBrandSprite.setTexture(clockBrand);

    sf::Texture hour1;
    if (!hour1.loadFromFile("enterhours.png")){return EXIT_FAILURE;}
    sf::Sprite hours;
    hours.setTexture(hour1);
    hours.setOrigin(240,200);
    hours.setPosition(window.getSize().x / 2 + 2, window.getSize().y / 2 + 2);

    sf::Texture minute1;
    if (!minute1.loadFromFile("enterminutes.png")){return EXIT_FAILURE;}
    sf::Sprite minutes;
    minutes.setTexture(hour1);
    minutes.setOrigin(200,100);
    minutes.setPosition(window.getSize().x / 2 + 2, window.getSize().y / 2 + 2);


    sf::Text a("", font);
    a.setCharacterSize(20);
    a.setStyle(sf::Text::Bold);
    a.setPosition(280,200);
    a.setColor(sf::Color::Black);
    sf::String c;

    sf::Text b("",font);
    b.setCharacterSize(20);
    b.setStyle(sf::Text::Bold);
    b.setPosition(325,300);
    b.setColor(sf::Color::Black);
    sf::String d;

    sf::Texture ba;
    if (!ba.loadFromFile("images.jpg")){return EXIT_FAILURE;}
    sf::Sprite bac;
    bac.setTexture(ba);
    bac.setOrigin(513,385);
    bac.setPosition(window.getSize().x / 2 + 2, window.getSize().y / 2 + 2);

    float x;
    float y;
    float z;


    while (window.isOpen()){
    sf::Event event;


 
  if ((sf::Mouse::getPosition(window).x >= 314 && sf::Mouse::getPosition(window).x <= 573) &&(sf::Mouse::getPosition(window).y >= 288 && sf::Mouse::getPosition(window).y <= 336)){
       if (event.type == sf::Event::TextEntered){
          if (event.text.unicode >= 48 && event.text.unicode <= 57){
               d = d + static_cast<char>(event.text.unicode);
              // d.insert(d.getSize(),event.text.unicode);
               b.setString(d);
               ofstream out;
               out.open("b.txt", std::ofstream::out | std::ofstream::trunc);
               out <<"."<<static_cast <char>(event.text.unicode)<<endl;
               out.close();}}}
 







    while (window.pollEvent(event)){
            if (event.type == sf::Event::Closed)
                window.close();}
                  window.draw(clockBrandSprite);
                  window.draw(hour);
                  window.draw(minute);
                  window.draw(hours);
                  window.draw(minutes);
                  window.draw(bac);
                  window.draw(a);
                  window.draw(b);
                  window.display();

                  }
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How to control text
« Reply #5 on: May 22, 2018, 12:54:59 pm »
Please make sure to read what people kindly reply to you.

Quote
Can you provide a complete and minimal example?

Quote
use [code=cpp][/code] tags for posting code

Quote
Make sure you use events only inside the event loop.

Also make sure to read the documentation and tutorials carefully.
« Last Edit: May 22, 2018, 12:57:11 pm by Laurent »
Laurent Gomila - SFML developer