Hi there, I've spent some days trying to find a solution for the following problem (in Visual Cpp 10/SFML 2.1), but couldn't find anything helpful on the net. I think its only a small line of code that is needed here.
What the program does: After each click on the red recangle, the int variable "myInt" (Start value = 1) is incremented by one. Every such operation is being recorded on the console. So far, so good up to here.
What I need now, is some line of code (I've tried every possible combination, but no luck :-) ) to force the variable to "refresh" itself by showing its new value on the screen. But I don't know how to do that. Has it something to do with the template? (I've been reading a cpp book for some weeks now, but templates aren't covered there really (and if at all, only briefly)) Any help apperciated!
#include <SFML/Graphics.hpp>
#include <string>
#include <sstream>
#include <iostream>
template <typename T>
std::string toString(T arg)
{
std::stringstream ss;
ss << arg;
return ss.str();
}
int main()
{
int myInt = 1;
sf::RenderWindow mMainWindow(sf::VideoMode(500, 300), "Map");
sf::RectangleShape rectangle;
rectangle.setSize(sf::Vector2f(70, 70));
rectangle.setFillColor(sf::Color::Red);
rectangle.setPosition(30, 30);
sf::Font myFont;
myFont.loadFromFile("menufont.otf");
sf::Text myText(toString<int>(myInt), myFont);
myText.setPosition(100,100);
myText.setCharacterSize(50);
myText.setColor(sf::Color(250,0,0,255));
while (mMainWindow.isOpen())
{
sf::Event event;
while (mMainWindow.pollEvent(event))
{
if (event.type == sf::Event::Closed)
mMainWindow.close();
}
if (rectangle.getGlobalBounds().contains(mMainWindow.mapPixelToCoords(sf::Mouse::getPosition(mMainWindow))))
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
myInt++;
std::cout << myInt;
// code for updating myInt Variable.
}
mMainWindow.clear();
mMainWindow.setFramerateLimit(30);
mMainWindow.draw(myText);
mMainWindow.draw(rectangle);
mMainWindow.display();
}
return 0;
}