Reputation: 21
I'm new to SFML and I'm trying to make simple game, but I have a problem:
I have Text class that handles my Score, here is the .h file
#pragma once
#include <SFML\Graphics.hpp>
#include <string>
#include <iostream>
class Text
{
public:
Text();
Text(std::string, std::string, sf::Vector2f position, int);
~Text();
void ModifyScoreBy(int value);
void Update();
void Draw(sf::RenderWindow &window);
static int getScore();
private:
sf::Text text;
sf::Font* font;
std::string pathToFont;
std::string textString;
static int numberScore;
};
and the .cpp file:
#include "Text.h"
int Text::numberScore = 0;
Text::Text()
{
}
Text::Text(std::string pathToFolder, std::string Text, sf::Vector2f position, int numberScore)
{
font = new sf::Font;
if (!font->loadFromFile(pathToFolder))
system("pause");
textString = Text;
this->numberScore = numberScore;
textString += std::to_string(numberScore);
text.setString(textString);
text.setFont(*font);
text.setPosition(position);
}
Text::~Text()
{
}
int Text::getScore()
{
return numberScore;
}
void Text::ModifyScoreBy(int value)
{
numberScore += value;
}
void Text::Update()
{
std::cout <<"Score: " << numberScore << std::endl;
textString = "Score: " + std::to_string(numberScore);
}
void Text::Draw(sf::RenderWindow &window)
{
window.draw(text);
}
the stuff with iostream is for testing purposes.
the sf::Text and the sf::Font are initialized as it should but in the Update() function it doesn't really update the sf::Text it stays Score: 0 when it is supposed to be Score: 10 or Score: 20 etc.
std::cout <<"Score: " << numberScore << std::endl; in the Update() function is working OK and is printing the right result.
I can't figure out what the problem is so I need help.
Thanks in advance.
Upvotes: 0
Views: 1188
Reputation: 56
Maybe you should add text.setString(textString);
at the end of Update() (right after modifying textString
).
Also, numberScore
should probably not be a static variable and font
is never freed.
Upvotes: 0