我在SFML sf :: RenderWindow

时间:2016-12-20 00:21:14

标签: c++ forms screen sfml

我尝试在c ++ windows窗体项目中使用SFML(我还是新手)的优点(这里也是新的:D)。我的想法是当一个事件发生时,此弹出的SFML窗口显示一个图像。当事件发生时,我设法让SFML窗口弹出,但它只显示空白屏幕,但没有显示我想要的纹理......以下是涉及SFML的代码的摘录,带有“//。 “我会削减不必要的代码:

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;

    // ...
};

在上面的代码我只是声明成员。

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);
}

背后的基本思想是,如果有一个事件和第一个计时器(每1秒m_tPowerCheck)用完,则显示SFML窗口绘制图像然后播放声音。 在每15毫秒(m_tWindowRefresh)检查sfml窗口是否打开并重绘图片 - 这里我不确定是否有必要因为这是一张静止图像但最初我认为黑屏是因为我不刷新它不断。但仍然 - 只有黑屏......:\

编辑:SFML的声音效果很好。

1 个答案:

答案 0 :(得分:1)

您似乎将sf::RectangleShape m_sfrChangePowerMessage的大小设置为sf::Vector2f(0,0)。 这是因为你打电话:

m_sfrChangePowerMessage.setSize
        (
            sf::Vector2f(m_sfwChangePowerMessage.getSize().x, m_sfwChangePowerMessage.getSize().y)
        );
在创建窗口之前

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

我的示例解决方案是替换

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

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

您也可能对Observer Pattern感兴趣,它可能对您的申请有所帮助。