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

Author Topic: sf::string, newlines and getline  (Read 3376 times)

0 Members and 1 Guest are viewing this topic.

mongrol

  • Newbie
  • *
  • Posts: 29
    • View Profile
sf::string, newlines and getline
« on: May 14, 2010, 01:03:51 pm »
Hi,
I'm parsing some map files that include a paragraph of descriptive text. I then wish to display this using sf::String and the usual Text processing. In other parts of my game I join strings together like this;

Code: [Select]

str="      "+selectedCreature->name;
str+="\n      "+tool.stringFromInt(selectedCreature->xp);
str+="\n      "+tool.stringFromInt(selectedCreature->strength);


This works fine and the newlines work as intended. In my map file I have some text with the \n's already in it like this;

Code: [Select]

There's been a report of some strange noises coming from a \n
local farm. I request you to investigate. Send a couple of \n
men out there and sort those peasants out.


I read those lines in using getline joining the lines together into one string like this;

Code: [Select]

introText+=line;
getline(mapfile,line);


When I send this string to my rendering it prints the \n's like they've been escaped. introText is a std::string object.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
sf::string, newlines and getline
« Reply #1 on: May 14, 2010, 03:49:35 pm »
The \n should be in your code, not in your original text where new lines are already there.

This should work
Code: [Select]

There's been a report of some strange noises coming from a
local farm. I request you to investigate. Send a couple of
men out there and sort those peasants out.

Code: [Select]
introText += line + "\n";
getline(mapfile, line);
Laurent Gomila - SFML developer

mongrol

  • Newbie
  • *
  • Posts: 29
    • View Profile
sf::string, newlines and getline
« Reply #2 on: May 15, 2010, 05:24:23 am »
Yeah, silly me. Ta.

ActionBoy

  • Newbie
  • *
  • Posts: 36
    • View Profile
sf::string, newlines and getline
« Reply #3 on: May 26, 2010, 11:00:08 pm »
Sorry to highjack the topic, but I wonder is it possible to load a line with getline that contains \n in the text file and make SFML render it as a new line?

For Example:

Hi I have a dog.\n Whats your name?

and have it rendered as

Hi I have a dog.
Whats your name?

The reason is I have some old gamefiles from the DOS days that looks like this so I dont want to edit them(replace \n a new line).

Edit: Just read what I wrote again and it sounds dumb, but I still like to know.

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
sf::string, newlines and getline
« Reply #4 on: May 27, 2010, 08:33:22 am »
There is a difference : The files from mongrol were containing a string like this 'hello\nworld', with the \n as two characters '\' and 'n'. The SFML was writing each character.

Your files (from DOS) are certainly containing something like this 'hello'\n'world' meaning the character '\n' is an escaped character (byte) of value 0x10 (10 in hexa => 16 in decimal, n is the 16th character).
So this is very different, but, the SFML is not going to add a 'new line and return' to your sf::Text (or sf::String depends on 1.6 or 2.0). You'll have to do it yoursef ;)

Ehm, I don't know if what I said is clear, if not you can ask again ;)
Mindiell
----

ActionBoy

  • Newbie
  • *
  • Posts: 36
    • View Profile
sf::string, newlines and getline
« Reply #5 on: May 27, 2010, 09:24:13 pm »
Tnx. That makes perfect sense.

 

anything