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

Author Topic: std::string crashes  (Read 2149 times)

0 Members and 1 Guest are viewing this topic.

led

  • Newbie
  • *
  • Posts: 28
    • View Profile
std::string crashes
« on: January 06, 2010, 01:29:03 am »
This is more like a C++ problem then SFML, but like i readed in a post that i need to get the Full String before seting it into SetText()...
I marked the line with a arrow.
Code: [Select]

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

using namespace sf;

int main()
{
RenderWindow Window(VideoMode(800, 600), "testing");

//Variaveis
Image Imagem1;
Image Imagem2;
Sprite Sprite;
Font font;
String Text;
std::string texto;

//Condições
if(!Window.IsOpened())
return 1;
if(!Imagem1.LoadFromFile("img\\Hud\\hud.png"))
return 1;
if(!Imagem2.LoadFromFile("img\\Teste\\1.png"))
return 1;
if(!font.LoadFromFile("arial.ttf"))
return 1;

//Atribuições
Text.SetFont(font);
Text.SetSize(50);
Text.SetColor(Color::White);
Text.SetPosition(0,200);
Sprite.SetImage(Imagem1);

//Main loop
while (Window.IsOpened())
{
Event evento;
----->texto = evento.MouseMove.X + "," + evento.MouseMove.Y;

while (Window.GetEvent(evento))
{
if (evento.Type== Event::Closed)
return 0;
if ((evento.Type== Event::MouseButtonPressed ) && (evento.MouseButton.Button == Mouse::Left))
Text.SetText(texto);
}

Window.Clear();
Window.Draw(Sprite);
Window.Draw(Text);
Window.Display();
}
}


Heres the crash: http://i.imagehost.org/0589/sfml1.jpg
while (!idle)
{
   DoSomething();
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
std::string crashes
« Reply #1 on: January 06, 2010, 08:24:29 am »
You can't concatenate directly integers and literal strings to build your std::string. Take a look at std::ostringstream for string formatting.
Code: [Select]
#include <sstream>
...

std::ostringstream oss;
oss << evento.MouseMove.X << "," << evento.MouseMove.Y;
texto = oss.str();
Laurent Gomila - SFML developer

led

  • Newbie
  • *
  • Posts: 28
    • View Profile
std::string crashes
« Reply #2 on: January 06, 2010, 01:58:07 pm »
Thanks Laurent, it works now! :)
while (!idle)
{
   DoSomething();
}