Incomprehensible bug

This is a discussion on Incomprehensible bug within the C++ Programming forums, part of the General Programming Boards category; Hey guys, i have the following code: Code: bool QueryNewGame(ConfigFile &pConfig, sf::RenderWindow *pApp) { pApp->Clear(); sf::Font GUIFont; if( !GUIFont.LoadFromFile(pConfig.read<std::string>("gui_font"))) return ...

  1. #1
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964

    Incomprehensible bug

    Hey guys, i have the following code:

    Code:
    bool QueryNewGame(ConfigFile &pConfig, sf::RenderWindow *pApp)
    {
        pApp->Clear();
        sf::Font GUIFont;
        if( !GUIFont.LoadFromFile(pConfig.read<std::string>("gui_font"))) return false;
    
        sf::String Str("", GUIFont, pConfig.read<int>("gui_text_size"));
        sf::Event Event;
        time_t StartTime = time(NULL);
        int EndTime = pConfig.read<int>("game_continue_timer");
    
        while( difftime(time(NULL), StartTime ) < EndTime )
        {
            int ElapsedTime = difftime(time(NULL), StartTime);
            std::stringstream SS;
            SS << "PRESS SPACE TO RESTART THE GAME, PRESS ESCAPE TO EXIT.\n" << ElapsedTime;
            Str.SetText(SS.str().c_str());
            Str.SetColor(sf::Color(255, 0, 0));
            SS.str("");
            Str.Move(pApp->GetWidth()/10, pApp->GetHeight()/2);
            sf::Event Event;
            pApp->GetEvent(Event);
    
                //Window closed
                if(Event.Type == sf::Event::Closed)
                    return false;
    
                //Escape pressed
                if( (Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape) )
                    return false;
    
                if( (Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Space) )
                    return true;
    
            pApp->Draw(Str);
            pApp->Display();
        }
        return false;
    }
    Which *should* output the text string to the screen, along with the elapsed time in seconds. For some reason the elapsed time that reaches the screen is always 0. I have run this through GDB and the ElapsedTime variable gets incremented properly, with the amount of seconds since the start of the function. GDB won't let me look into the stringstream however, for some reason it's just an "unknown type" so i suspect that is where the problem is, am i using this stringstream all wrong?
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't recognize the sf namespace, although it's clear that it's the GUI. I can check the standard stuff, and that works fine:
    Code:
    #include <iostream>
    #include <sstream>
    #include <unistd.h> //I'm using sleep to simulate whatever it is that you're trying to do
    #include <ctime>
    int main(void) {
        time_t StartTime = time(0);
        int EndTime = 15;
        while (difftime(time(0), StartTime) < EndTime) {
            int ElapsedTime = difftime(time(0), StartTime);
            std::stringstream SS;
            SS << "PRESS SPACE TO RESTART\n" << ElapsedTime;
            std::cout << SS.str().c_str() << std::endl;
            sleep(1);
        }
        return 0;
    }
    That's compilable, and it works wonderfully here. Perhaps you can try adding your sf:: code one line at a time to this and see what happens.

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    What GUI/toolkit is this for?
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    I'm using SFML in the above code, i figured that it was my use of the std lib that was off, but compiling and running tabstops code yields proper results so the problem must be with SFML. I'll take it to their forums then, thanks for the clarification.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    It boggles my mind that they used sf instead of sfml as the namespace name.

  6. #6
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Memloop View Post
    It boggles my mind that they used sf instead of sfml as the namespace name.
    Why? Is there a name conflict with something important that i've missed?
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  7. #7
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Normally people only use an abbreviation if the name is long. And sfml is not that long.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Disappearing Bug
    By el-sid in forum C++ Programming
    Replies: 4
    Last Post: 08-16-2009, 01:12 PM
  2. compiler bug or brain bug?
    By Lesshardtofind in forum C++ Programming
    Replies: 10
    Last Post: 07-14-2009, 04:16 AM
  3. gaks bug?
    By Yarin in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-31-2008, 03:47 PM
  4. Debugging a rare / unreproducible bug..
    By g4j31a5 in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 08-05-2008, 01:56 PM
  5. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 08:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21