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

Author Topic: Large delays with window visuals updating despite timer not reporting any gaps  (Read 2955 times)

0 Members and 1 Guest are viewing this topic.

3eechan

  • Newbie
  • *
  • Posts: 1
    • View Profile
I'm very new to SFML so hopefully my problem is a very simple one. I currently have (relatively) few objects being drawn to my RenderWindow every loop iteration, although at seemingly random intervals there will be a noticeable delay with the next frame being drawn. Despite these very noticeable delays (2+ seconds) I have a statement in my code that will output the time between the current frame and the last one should it take more than 1 second. This statement does not output anything even when I can obviously see there has been one such delay. I am losing my mind trying to figure out where I screwed up. Any help will be very appreciated.

It is a simple typing program that measures WPM. The current word is displayed, alongside the next word to be typed.

Here is the code from my main function:
#include "game.hpp"


int main(void)
{
    //game window
    sf::RenderWindow gameWindow(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "the game", sf::Style::Close);

    //application icon
    sf::Image appIcon;
    appIcon.loadFromFile("appIcon.png");
    gameWindow.setIcon(appIcon.getSize().x, appIcon.getSize().y, appIcon.getPixelsPtr());

    //local mouse position (i.e. position relative to game window)
    sf::Vector2i localMousePosition;

    //game object
    Game currentGame;
    currentGame.populateWordList();

    //initialize music
    currentGame.initMainMusic();
    currentGame.changeMainMusicState(1);

    //MAIN MENU CHECK
    bool playButtonClick = false;

    //WORD SPELL CHECK
    bool fullWordTyped = true;

    //USER'S TYPED LETTERS
    std::string typedCharacterString;
    int typedCharStrSize = 60;

    //CURRENT WORD TO DISPLAY
    std::string currentWord;
    //NEXT WORD TO DISPLAY
    std::string nextWord = currentGame.getRandomWord();

    //DATA FOR TIME
    bool resetTimer = true;
    sf::Clock timer;

    sf::Time currentFrameTime = timer.getElapsedTime();
    sf::Time lastFrameTime;


    //SRAND INITIALIZATION (I know, I know)
    std::srand(std::time(0));

   
    /*RUN THE PROGRAM AS LONG
    AS THE WINDOW IS OPEN*/

    while (gameWindow.isOpen())
    {
       
       
        sf::Event event;

        //EVENT LOOP FOR MAIN MENU
        while (gameWindow.pollEvent(event))
        {
            //if window close request ==> close window
            if (event.type == sf::Event::Closed)
            {
                gameWindow.close();
            }

            /*draw the main menu as long as:

            1. the play button has not been clicked
            2. the exit button has not been clicked*/

            else if (!playButtonClick)
            {
                if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left
                    && currentGame.isMouseOnPlayButton(gameWindow))
                {
                    playButtonClick = true;
                }

                else if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left
                        && currentGame.isMouseOnExitButton(gameWindow))
                {
                    gameWindow.close();
                }
            }

            else if (event.type == sf::Event::TextEntered)
            {
               
                if (typedCharStrSize > 50)
                    typedCharStrSize--;

                else
                    typedCharStrSize = 60;

                if (event.text.unicode == 8 && !typedCharacterString.empty())
                {
                    typedCharacterString.pop_back();
                    break;
                }

                else if (event.text.unicode == 32 && typedCharacterString == currentWord)
                {
                    currentGame.incrementWordsTyped();
                    currentGame.incrementTotalCharactersTyped(typedCharacterString.size());
                   
                       
                    typedCharacterString.clear();
                    fullWordTyped = true;
                    break;
                }
                   
                else if (event.text.unicode > 31 && event.text.unicode < 127)
                {
                    typedCharacterString.push_back(static_cast<char>(event.text.unicode));
                    break;
                }
            }
        }

        //clear previous frame
        gameWindow.clear(sf::Color::Black);

        if (!playButtonClick)
            currentGame.mainMenu(gameWindow);

        if (playButtonClick)
        {
            sf::Time timePeriod = timer.getElapsedTime();
            currentGame.setWPM((currentGame.getTotalCharactersTyped() / 5) / (timePeriod.asSeconds() / 60));
            currentGame.drawBackground(gameWindow);
            currentGame.drawWPM(gameWindow);
            currentGame.drawString(currentWord, gameWindow, WINDOW_WIDTH/2.2, WINDOW_HEIGHT/4);

            if (currentGame.getTotalWordsTyped() < 49)
                currentGame.drawString(nextWord, gameWindow, WINDOW_WIDTH/1.4, WINDOW_HEIGHT/4, 30);
           
            currentGame.drawString(typedCharacterString, gameWindow, WINDOW_WIDTH/2, WINDOW_HEIGHT/3, typedCharStrSize, sf::Text::Style::Underlined);
            timePeriod = timer.getElapsedTime();
            currentGame.drawString(std::to_string((int)timePeriod.asSeconds()), gameWindow, WINDOW_WIDTH/3, WINDOW_HEIGHT/1.1);

            if (currentGame.isMainMusicPlaying())
            {
                currentGame.changeMainMusicState();
                currentGame.initGameMusic();
                currentGame.changeGameMusicState(1);
            }
               
            if (resetTimer)
            {
                timer.restart();
                resetTimer = false;
            }
               
            if (fullWordTyped)
            {                    
                currentWord = nextWord;
                do
                    {
                       
                        nextWord = currentGame.getRandomWord();
                    } while (currentWord == nextWord || currentWord == " " || currentWord.empty() || nextWord.empty()
                            ||  (currentWord.size() + nextWord.size()) > 22);
                   
                fullWordTyped = false;
            }

            if (currentGame.getTotalWordsTyped() >= 50)
            {
                currentGame.updateBestWPM();
                resetTimer = true;
                playButtonClick = false;
            }
        }
       
        //draw to the screen
        gameWindow.display();
        lastFrameTime = currentFrameTime;
        currentFrameTime = timer.getElapsedTime();
        if (currentFrameTime.asSeconds() - lastFrameTime.asSeconds() > 1)
        {
            std::cout << "\nThis frame took " << currentFrameTime.asSeconds() - lastFrameTime.asSeconds() << " seconds\n";
        }
       


       
    }
   
    return 0;
}
 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
One thing I can see is that you are using the timer's elapsed time to measure the frame but you may also have reset it previously.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*