Search code examples
c++sfml

SFML window is not responding


I am a beginner C++ programmer and I'm making a snake game and I am trying to use SFML library. I want to make my snake console application but I want to display score to SFML window. Not only that I do not know how to display score to window even though I have been searching for solution all night long. Plus my SFML window keeps on being not responsive.

I rendered the window with the following code :

sf::RenderWindow window(sf::VideoMode(200, 75), "Score");


sf::Font font;
if (!font.loadFromFile("font.tff"))
    cout << "ERROR LOADING FONT" << endl;
sf::Text text;
text.setFont(font);
text.setString("hello");
//  sf::String scoredis;

while (window.isOpen()) {

    sf::Event event;
    while (window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
            window.close();
    }

Then I tried to display score inside the game loop :

do{ 
    ...
    window.clear();
    window.draw(text);
    window.display();
    ...
}while()

Full code inside main() :

{
ShowConsoleCursor(false);//nastavimo da ne kaze kurzorja
                         //Nalozimo zvok ki ga bomo predvajali ko pojemo sadez

sf::SoundBuffer buffer;
if (!buffer.loadFromFile("sound.wav"))//v buffer vstavimo zvok
{
    cout << "ERROR LOADING SOUND" << endl;//ce je kaksna napaka se prikaze to
}
sf::Sound sound;
sound.setBuffer(buffer);//nastavimo nas zvok na to kaj je v bufferju

/*sf::Font font;
if (!font.loadFromFile("font.tff"))
    cout << "ERROR LOADING FONT" << endl;
sf::Text text;
text.setFont(font);
text.setString("hello");
//  sf::String scoredis;*/

sf::RenderWindow window(sf::VideoMode(200, 75), "Score");



while (window.isOpen()) {

    sf::Event event;
    while (window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
            window.close();
    }
}

    int score = 0;
    int weight = 0;
    srand(time(0));
    narisi(); //draw map
    setup();

    thread NitZaVnos(spremembaSmeri);   //ustvari in zažene se nova nit, ki opravlja delo funkcije spremembaSmeri

    do
    {
        if (weight>50)
        {
            changed = false;
            premik(preberiSmer());
            weight = 0;
        }
        if (fruitPickup())//Funkcija ki pogleda ce je glava kace na sadezu
        {
            genFruit();//generira novi sadez
            score += 10;//dodamo tocke
            sSize++;//povecamo velikost kace
            sound.play();//predvajamo zvok
        }

        weight++;
        if (zalet())// Pogledamo ce se glava zaleti v telo ce vrne funkcija true skocimo izben zanke
            break;
        //stringstream ss;
        //  ss << score;
        //scoredis.SetText(ss.str().c_str());

        window.clear();
        //window.draw(text);
        window.display();

        Sleep(5); //program pocaka 5 ms

    } while (!gameOver());



    gotoxy(0, visinaM + 1);
    run = false;    //nastavi se vrednost spremenljivke run na false, da se lahko stranska nit zakljuci
    NitZaVnos.join();   //stranska nit ze sedaj sinhronizira z glavnim programom
    return 0;
}

Solution

  • The window going unresponsive with SFML is related to you not polling for events anymore. Make sure your event loop is untouched within the main loop and that it still runs while the game does. It would help if you posted your full code.