简体   繁体   中英

I get just black screen in an SFML sf::RenderWindow

I try to use the advantages of SFML (I am still new here) in an c++ windows forms project (also new here :D). My idea is when an event occurs this SFML window to pop up showing an image. I've managed to make the SFML window to pop up when the event happens but it appears only with a blank screen but not showing the texture I want... Here are the excerpts with code where SFML is involved, with "// .." I will cut the unnecessary code:

BatteryAlarmDlg.h

class CBatteryAlarmDlg : public CDialog
{
    // ... 
    protected:
    // ...

    // timers
    Timer m_tPowerCheck; // the struct Timer has just two members - int id and int duration
    Timer m_tWindowRefresh;

    // sounds
    sf::SoundBuffer m_sfsbWarningMessageBuffer;
    sf::Sound m_sfsWarningMessageSound;
    std::string m_sPathToWarningMessageSoundFile;

    // sfml window management support
    sf::RenderWindow m_sfwChangePowerMessage;
    sf::Texture m_sftChangePowerMessage;
    sf::RectangleShape m_sfrChangePowerMessage;
    bool m_bIsWindowAlive;
    sf::Event m_sfeEvent;

    // ...
};

on the code above I just declare the members.

BatteryAlarmDlg.cpp

// ...
BOOL CBatteryAlarmDlg::OnInitDialog()
{
    // ...
    /// init timers ///
    m_tPowerCheck = Timer(42, 1000); // a timer with id == 42 and with a duration == 1,000 ms
    SetTimer(m_tPowerCheck.id, m_tPowerCheck.duration, nullptr);
    m_tWindowRefresh = Timer(1, 15);
    SetTimer(m_tWindowRefresh.id, m_tWindowRefresh.duration, nullptr);

    /// init sounds ///
    m_sPathToWarningMessageSoundFile = "warning-message-2.wav";
    if (!m_sfsbWarningMessageBuffer.loadFromFile(m_sPathToWarningMessageSoundFile))
    {
        MessageBox("Error loading of warning message sound file!");
    }
    m_sfsWarningMessageSound.setBuffer(m_sfsbWarningMessageBuffer);

    /// init messages windows ///
    m_sfwChangePowerMessage.setFramerateLimit(60);
    if (!m_sftChangePowerMessage.loadFromFile("ChagePowerMessage.png"))
    {
        MessageBox("Error loading of warning message image file!");
    }
    m_sfrChangePowerMessage.setSize
        (
            sf::Vector2f(m_sfwChangePowerMessage.getSize().x, m_sfwChangePowerMessage.getSize().y)
        );

    m_sfrChangePowerMessage.setPosition(0, 0);
    m_sfrChangePowerMessage.setTexture(&m_sftChangePowerMessage);
    m_bIsWindowAlive = false;

    return TRUE;  // return TRUE  unless you set the focus to a control
}
    // ...
void CBatteryAlarmDlg::OnTimer(UINT_PTR nIDEvent)
{
    if (nIDEvent == m_tPowerCheck.id)
    {

        if (GetSystemPowerStatus(&m_spsPower) == 0)
        {
            // message = "Error: Could not get the system power status!";
            MessageBox("Error: Could not get the system power status!");
        }       
        if (m_spsPower.ACLineStatus == 0 & m_iPowerChange == 1)
        {
            if (!m_sfwChangePowerMessage.isOpen())
            {
                m_sfwChangePowerMessage.create(sf::VideoMode(800, 600), "Battery allarm : System Power Changed!");
            }
            m_sfwChangePowerMessage.clear();
            m_sfwChangePowerMessage.draw(m_sfrChangePowerMessage);
            m_sfwChangePowerMessage.display();
            m_iPowerChange = 0;
            m_sfsWarningMessageSound.play();
            m_bIsWindowAlive = true;
        }

        if (m_spsPower.ACLineStatus == 1 & m_iPowerChange == 0)
        {
            m_iPowerChange = 1;
        }

    }


    if (nIDEvent = m_tWindowRefresh.id)
    {
        if (m_bIsWindowAlive)
        {
            while (m_sfwChangePowerMessage.pollEvent(m_sfeEvent))
            {
                if
                    (
                        m_sfeEvent.type == sf::Event::Closed ||
                        (m_sfeEvent.type == sf::Event::KeyPressed && m_sfeEvent.key.code == sf::Keyboard::Escape)
                    )
                {
                    m_bIsWindowAlive = false;
                }
            }

            m_sfwChangePowerMessage.clear();
            m_sfwChangePowerMessage.draw(m_sfrChangePowerMessage);

            m_sfwChangePowerMessage.display();
        }

        if(!m_bIsWindowAlive && m_sfwChangePowerMessage.isOpen())
        {
            m_sfwChangePowerMessage.close();
        }
    }
    CDialog::OnTimer(nIDEvent);
}

The basic idea behind is if there is an event and first timer (m_tPowerCheck on every 1 second) is run out then show the SFML window drawing the image and then play the sound. On every 15 miliseconds (m_tWindowRefresh) check if the sfml window is open and redraw the picture - here I am not certain that it is necessary because this is a still image but initially I thought that the black screen is because I don't refresh it constantly. But still - only a black screen... :\\

Edit: The sound from SFML works fine.

It looks like you set size of a sf::RectangleShape m_sfrChangePowerMessage as sf::Vector2f(0,0) . It happens because you call:

m_sfrChangePowerMessage.setSize
        (
            sf::Vector2f(m_sfwChangePowerMessage.getSize().x, m_sfwChangePowerMessage.getSize().y)
        );

before the window gets created:

m_sfwChangePowerMessage.create(sf::VideoMode(800, 600), "Battery allarm : System Power Changed!");

My example solution is to replace

m_sfrChangePowerMessage.setSize
        (
            sf::Vector2f(m_sfwChangePowerMessage.getSize().x, m_sfwChangePowerMessage.getSize().y)
        );

to m_sfrChangePowerMessage.setSize(sf::Vector2f(800,600));

Also you might be interested in Observer Pattern , it might help you with your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM