Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

SFML i zamrożony (aktualny) czas

Ostatnio zmodyfikowano 2019-06-29 00:16
Autor Wiadomość
nanoant20
Temat założony przez niniejszego użytkownika
SFML i zamrożony (aktualny) czas
» 2019-06-28 23:45:44
Przy pomocy SFML i biblioteki do obsługi czasu 2.2. Typ danych: struct tm
wyświetlam aktualny czas.
Niby wszystko ok, ale problem plega na tym, że czas się zamraża, po 5s.
Gdy po okienku przesunie sie wskaźnik, znów zegarek działa na kilka sekund.
Co może byc tego przyczyną?

C/C++
#include <iostream>
#include <cstring>
#include <sstream>
#include <ctime>

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>

using namespace std;
using namespace sf;

int main()
{
    /*
        flag sf::Style:: None | Titlebar | Resize | Close | Fullscreen
        default flag sf::Style:: Resize | Close Resize | Close
        mixed flag sf::Style::Titlebar |  sf::Style::Close
        */
    //sf::RenderWindow my_window(sf::VideoMode(333, 220, 32), "screen size", sf::Style::Fullscreen );
    //sf::RenderWindow my_window(sf::VideoMode(333, 220, 32), "screen size", sf::Style::Resize );
    //sf::RenderWindow my_window(sf::VideoMode(333, 220, 32), "screen size", sf::Style::Titlebar |  sf::Style::Close );
    //sf::RenderWindow my_window(sf::VideoMode(333, 220, 32), "screen size", sf::Style::None);
   
    int screen_size = sf::VideoMode::getDesktopMode().width;
   
    //Create the main window
    sf::RenderWindow my_window;
    if( screen_size >= 1024 )
    {
        my_window.create( sf::VideoMode( 666, 444, 32 ), "Zegarek", sf::Style::Titlebar | sf::Style::Close );
        my_window.setFramerateLimit( 60 );
    }
    if( screen_size == 800 )
    {
        my_window.create( sf::VideoMode( 555, 333, 32 ), "Zegarek", sf::Style::None );
        my_window.setFramerateLimit( 60 );
    }
   
    my_window.setTitle( "Aktualny czas" );
   
    // kolor.
    //sf::Color bgColor(sf::Color::Green);
    sf::Color bgColor( 30, 30, 30 );
   
    // Create a graphical text to display
    sf::Font font;
    if( !font.loadFromFile( "rec/font/DELPHIN_.TTF" ) )
         return EXIT_FAILURE;
   
    //Create static text
    sf::Text godz( "Godzina: ", font, 28 );
    godz.setPosition( 20, 20 );
   
    // Create a graphical dynamic text to display
    sf::Text text_dynamic_godz;
    text_dynamic_godz.setPosition( 130, 15 );
    text_dynamic_godz.setFont( font ); //select the font
    text_dynamic_godz.setCharacterSize( 33 ); // in pixels, not points!
    text_dynamic_godz.setFillColor( sf::Color::Red );
    //text_dynamic_x.setColor(sf::Color(255, 255, 255, 255));
   
    //Create static text
    sf::Text minuta( "Minut: ", font, 28 );
    minuta.setPosition( 20, 60 );
   
    // Create a graphical dynamic text to display
    sf::Text text_dynamic_minuta;
    text_dynamic_minuta.setPosition( 130, 55 );
    text_dynamic_minuta.setFont( font ); //select the font
    text_dynamic_minuta.setCharacterSize( 33 ); // in pixels, not points!
    text_dynamic_minuta.setFillColor( sf::Color::Red );
    //text_dynamic_x.setColor(sf::Color(255, 255, 255, 255));
   
    //Create static text
    sf::Text sekunda( "Sekund: ", font, 28 );
    sekunda.setPosition( 20, 100 );
   
    // Create a graphical dynamic text to display
    sf::Text text_dynamic_sekunda;
    text_dynamic_sekunda.setPosition( 130, 95 );
    text_dynamic_sekunda.setFont( font ); //select the font
    text_dynamic_sekunda.setCharacterSize( 33 ); // in pixels, not points!
    text_dynamic_sekunda.setFillColor( sf::Color::Red );
    //text_dynamic_x.setColor(sf::Color(255, 255, 255, 255));
   
    //stringstream into variables
    std::stringstream g, m, s;
   
    while( my_window.isOpen() )
    {
       
        sf::Event windowEvent;
        while( my_window.pollEvent( windowEvent ) )
        {
            if( windowEvent.type == sf::Event::Closed )
            {
                my_window.close();
            }
            if( windowEvent.type == sf::Event::KeyPressed && windowEvent.key.code == sf::Keyboard::Escape )
            {
                my_window.close();
            }
           
           
            // Get system time
            std::time_t czas = std::time( NULL );
           
            struct tm * ptr = localtime( & czas );
           
            // Clear screen
            //my_window.clear();
            //my_window.clear(sf::Color::Black);
            my_window.clear( bgColor );
           
            // Draw the string
            my_window.draw( godz );
            my_window.draw( minuta );
            my_window.draw( sekunda );
           
            g.str( std::string() ); //clear
            g << ptr->tm_hour;
            text_dynamic_godz.setString( g.str().c_str() );
            my_window.draw( text_dynamic_godz );
           
            m.str( std::string() ); //clear
            m << ptr->tm_min;
            text_dynamic_minuta.setString( m.str().c_str() );
            my_window.draw( text_dynamic_minuta );
           
            s.str( std::string() ); //clear string
            s << ptr->tm_sec;
            text_dynamic_sekunda.setString( s.str().c_str() );
            my_window.draw( text_dynamic_sekunda );
           
        }
        //display na samym koncu petli
        my_window.display();
       
    }
   
    return 0;
}

żeby program przetestować należy
zmienić ścieżkę do czcionki na swoją
if( !font.loadFromFile( "rec/font/DELPHIN_.TTF" ) )
P-174852
pekfos
» 2019-06-28 23:51:16
Wyświetlasz rzeczy w pętli zdarzeń.
P-174853
nanoant20
Temat założony przez niniejszego użytkownika
» 2019-06-29 00:16:22
Dzięki serdeczne. Zadziałało
Klamra zamykająca } przed // Get system time

           
P-174854
« 1 »
  Strona 1 z 1