SFML issues??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<iostream>
#include <SFML/Graphics.hpp>
using namespace std;
 
int main()
{   // Make a window that is 800 by 200 pixels
    // And has the title "Hello from SFML"
    sf::RenderWindow window(sf::VideoMode(800, 200), "Hello from SFML");
    /** loading an image **/
    sf::Texture imageSource;
   if(!imageSource.loadFromFile("background.png"))
      return EXIT_FAILURE;
  sf::Sprite imageSprite;
  imageSprite.setTexture(imageSource);
 
     
    sf::Text message;
 
    // We need to choose a font
    sf::Font font;
    font.loadFromFile("font.ttf");
 
    // Set the font to our message
    message.setFont(font);
 
    // Assign the actual message
 
     string name;
   for(int i=0;i<5;i++)
   {

    cout<< "enter word: ";
   cin>>name;

    message.setString(name);
    message.setString(name);
 
     
    message.setCharacterSize(20);
 
    // Choose a color
    message.setFillColor(sf::Color::White);
 }
    // This "while" loop goes round and round- perhaps forever
    while (window.isOpen())
    {
         
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                // Someone closed the window- bye
                window.close();
 
        }
 
       
       window.draw(imageSprite);

 

        window.draw(message);
 
         
 
        // Show everything we just drew
        window.display();
    }// This is the end of the "while" loop
 
    return 0;
}



i'd like to display multiple names, but i don't know how to that ( the names will be entered using the consol)
Last edited on
What you can try is to store the names in a vector first.
For each name in the vector create a sf::Text,
set the name and position and call window.draw for the text.
There is also this way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <SFML/Graphics.hpp>
#include <vector>

typedef std::vector<std::string> StringVector;

std::string join(const StringVector& v, char sep)
{
  std::string retval;

  if(v.empty())
    return retval;
  
  for (size_t i = 0; i < v.size() - 1; ++i)
    retval += v[i] + sep;

  retval += v[v.size() - 1];

  return retval;
}

int main()
{
  sf::RenderWindow window(sf::VideoMode(600, 400), "SFML works!");
  sf::Text message;
  sf::Font font;
  font.loadFromFile("Verdana.ttf");
  message.setFont(font);
  message.setCharacterSize(30);
  message.setColor(sf::Color::Red);
  message.setPosition(100, 100);

  std::vector<std::string> names = { "Anna", "Lisa", "Debbie" };
  std::string s = join(names, '\n');
  message.setString(s);

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

  return 0;
}


Output:
http://pasteboard.co/nWjOtnBD7.jpg
@Thomas1965 Thank you so much for your help :) it means so much!

Have a great day!
I need your opinio about a game, the game should have an edit box for the user to enter informtion, What's best to use SFML or SDL 2 or others?
They're both fine, just stick with what you already know better.

You shouldn't use SFML like this, you're supposed to clear, draw, and display, repeatedly.

1
2
3
window.clear();
// Draw your stuff
window.display();


If you don't refresh your display problems occur once you try to change it obviously, or if you were to resize your window etc.

That join function isn't what I'd use, it's more complicated than it needs to be.

1
2
3
4
5
6
7
// Pre c++11
std::string result = "";
for( size_t i = 0; i < names.size(); ++i)
{
    result += names[i];
}
return result;


1
2
3
4
5
6
7
// c++11
std::string result = "";
for(std::string & val : names)
{
    result += val;
}
return result;
That join function isn't what I'd use, it's more complicated than it needs to be.

Neither of the variants you supplied do the same job as the join function you're denigrating.
Thanks a lot to all!
Topic archived. No new replies allowed.