简体   繁体   English

我在SFML sf :: RenderWindow中只有黑屏

[英]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). 我尝试在C ++ Windows窗体项目(在这里也是新的:D)中使用SFML的优点(在这里我还是很新的)。 My idea is when an event occurs this SFML window to pop up showing an image. 我的想法是,当事件发生时,此SFML窗口弹出以显示图像。 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: 我设法使SFML窗口在事件发生时弹出,但是它仅出现在空白屏幕上,但没有显示我想要的纹理。这是涉及SFML的代码摘录,带有“ //”。 。”我将剪切不必要的代码:

BatteryAlarmDlg.h 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 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. 背后的基本思想是,如果有一个事件,并且第一个计时器(每1秒钟执行一次m_tPowerCheck),然后显示SFML窗口绘制图像,然后播放声音。 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. 每隔15毫秒(m_tWindowRefresh)检查sfml窗口是否打开并重新绘制图片-在这里我不确定是否有必要,因为这是静止图像,但最初我认为黑屏是因为我不刷新它不断。 But still - only a black screen... :\\ 但仍然-只有黑屏...:\\

Edit: The sound from SFML works fine. 编辑:SFML的声音工作正常。

It looks like you set size of a sf::RectangleShape m_sfrChangePowerMessage as sf::Vector2f(0,0) . 看起来您将sf::RectangleShape m_sfrChangePowerMessage大小设置为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)); m_sfrChangePowerMessage.setSize(sf::Vector2f(800,600));

Also you might be interested in Observer Pattern , it might help you with your application. 另外,您可能对Observer Pattern感兴趣,它可能对您的应用程序有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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