I've got a problem that may just be caused by the wrong way of thinking.
But anyways, I need some help with it:
I made a system for drawing text to the screen by printing char after char from subrects of one sf::Sprite.
When the first character is drawn, i move the position of my sprite by the width of a char and draw the next one.
This is done in a "for"-loop which cycles through every character of a string that is passed.
My problem occurs when calling this function: The loop doesn't finish, even if the string ends.
It just keeps going on, which results in the characters getting drawn over and over again while moving out of the screen, as their position is updated every frame.
So, do you guys have any idea on how correct the loop?
Or is the error maybe a completely different one?
So, here is the relevant code:
main.cpp
#include "menu.hpp"
int main()
{
Menu Menu;
while (Window.IsOpened())
{
Window.Clear(sf::Color(255, 204, 153));
Menu.DrawCharacters("ABC", Window);
Window.Display();
}
return 0;
}
menu.cpp
void Menu::DrawCharacters(string Text, sf::RenderWindow &Window)
{
for(unsigned int x = 0; x != Text.length(); x++)
{
if(Text[x] == 'A') { CharIndexX = 0; CharIndexY = 0; }
if(Text[x] == 'B') { CharIndexX = 1; CharIndexY = 0; }
if(Text[x] == 'C') { CharIndexX = 2; CharIndexY = 0; }
CharacterSprite.SetSubRect(sf::IntRect(CharIndexX * CharWidth, CharIndexY * CharHeight, CharIndexX * CharWidth + CharWidth, CharIndexY * CharHeight + CharHeight));
CharacterSprite.SetPosition(CharPositionX + CharWidth * CharPositionXIndex, CharPositionY);
CharPositionXIndex++;
Window.Draw(CharacterSprite);
}
}
void Menu::Initialize();
{
CharacterImage.LoadFromFile("images/characterset.png");
CharacterImage.SetSmooth(false);
CharacterSprite.SetImage(CharacterImage);
CharWidth = 12;
CharHeight = 24;
CharIndexX = 0;
CharIndexY = 0;
CharPositionX = 100;
CharPositionY = 100;
CharPositionXIndex = 1;
}
menu.hpp
#ifndef MENU_HPP
#define MENU_HPP
class Menu
{
public:
void Initialize();
void DrawCharacters(string Text, sf::RenderWindow &Window);
int CharWidth;
int CharHeight;
int CharIndexX;
int CharIndexY;
int CharPositionX;
int CharPositionY;
int CharPositionXIndex;
sf::Image CharacterImage;
sf::Sprite CharacterSprite;
};
#endif