Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] Application becomes unresponsive on focus change  (Read 1366 times)

0 Members and 1 Guest are viewing this topic.

djlynn03

  • Newbie
  • *
  • Posts: 5
    • View Profile
[SOLVED] Application becomes unresponsive on focus change
« on: March 29, 2022, 10:28:03 pm »
I am creating an application to display a slideshow of images. My application starts and runs successfully, but when I click somewhere else on my screen and change the window focus, the app becomes unresponsive. I am unsure if it is a problem with my event loop or something else.

Below is my main function and I have my event loop inside the main loop.
int WINAPI WinMain(
    _In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPSTR     lpCmdLine,
    _In_ int       nCmdShow
)
{

    EnumDisplayMonitors(NULL, NULL, MyInfoEnumProc, 0);

    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hIcon = LoadIcon(wcex.hInstance, IDI_APPLICATION);
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = szWindowClass;
    wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);


    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL,
            _T("Call to RegisterClassEx failed!"),
            _T(""),
            NULL);

        return 1;
    }

    hInst = hInstance;

    HWND hWnd = createWindow(hInst);

    DWORD Style = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS;
    HWND View1 = CreateWindow(L"STATIC", NULL, Style, 0, 0, windowWidth, windowHeight, hWnd, NULL, hInst, NULL);

    SFMLView1.create(View1);
    SFMLView1.setMouseCursorVisible(false);
    SFMLView1.setFramerateLimit(10);
    SFMLView1.setVerticalSyncEnabled(true);

    if (!hWnd)
    {
        MessageBox(NULL,
            _T("Call to CreateWindow failed!"),
            _T(""),
            NULL);

        return 1;
    }

    DEVMODE fullscreenSettings;
    bool isChangeSuccessful;

    EnumDisplaySettings(NULL, 0, &fullscreenSettings);
    fullscreenSettings.dmPelsWidth = 1080;
    fullscreenSettings.dmPelsHeight = 720;
    fullscreenSettings.dmBitsPerPel = 8;
    fullscreenSettings.dmDisplayFrequency = 30;
    fullscreenSettings.dmFields = DM_PELSWIDTH |
        DM_PELSHEIGHT |
        DM_BITSPERPEL |
        DM_DISPLAYFREQUENCY;

    SetWindowLongPtr(hWnd, GWL_EXSTYLE, WS_EX_APPWINDOW | WS_EX_TOPMOST);
    SetWindowLongPtr(hWnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);
    SetWindowPos(hWnd, HWND_BOTTOM, windowX, windowY, windowWidth, windowHeight, SWP_SHOWWINDOW); // Z-order set to the bottom so other windows can be displayed above it
    isChangeSuccessful = ChangeDisplaySettings(&fullscreenSettings, CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL;
    ShowWindow(hWnd, SW_MAXIMIZE);

    DWORD dwExStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
    dwExStyle &= ~WS_EX_APPWINDOW;
    dwExStyle |= WS_EX_TOOLWINDOW;
    SetWindowLong(hWnd, GWL_EXSTYLE, dwExStyle);

    ShowWindow(hWnd,
        nCmdShow);
    UpdateWindow(hWnd);

    std::vector<std::string> imagePaths = populateImages(hWnd);
    if (imagePaths.size() == 0) {
        exit(2);
    }

    std::cout << imagePaths.at(0) << "\n";

    //MSG msg;
    int index = 0;

    sf::Image image;
 
    sf::FileInputStream stream;
    sf::Texture texture;

    sf::Vector2f targetSize(windowWidth, windowHeight);

    std::vector<sf::Sprite> sprites;

    sf::Sprite sprite;
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);

    while (SFMLView1.isOpen()) {
        sf::Event event;
        while (SFMLView1.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                SFMLView1.close();
        }

        stream.open(imagePaths.at(index));
        texture.loadFromStream(stream);

        sf::Sprite sprite(texture);

        sprite.setScale(
            targetSize.x / sprite.getLocalBounds().width,
            targetSize.y / sprite.getLocalBounds().height
        );

        SFMLView1.clear();
        SFMLView1.draw(sprite);
        SFMLView1.display();

        if (index == imagePaths.size() - 1) {
            index = 0;
        }
        else {
            index += 1;
        }
        std::this_thread::sleep_for(std::chrono::seconds(2));
    }
    return 0;
}

Thanks
« Last Edit: March 30, 2022, 10:13:29 pm by djlynn03 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: Application becomes unresponsive on focus change
« Reply #1 on: March 30, 2022, 04:30:53 pm »
Any reason you create your own window instead of using SFML's facilities?

To me it sounds like events aren't being processed correctly.
Hard to say whether this is influenced by your custom window creation code.
If you run it through a debugger and set a break point inside the pollEvent-loop?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

djlynn03

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Application becomes unresponsive on focus change
« Reply #2 on: March 30, 2022, 07:20:12 pm »
The problem with using a debugger is that the program only crashes when I run it as an executable, and works fine when I am in Visual Studio. I do not have a reason for creating my own window, the project just sort of ended up that way. I will switch my custom window code to SFML and see if it makes a difference. It could be a problem with the SFML window not receiving the events because of the custom window.

djlynn03

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Application becomes unresponsive on focus change
« Reply #3 on: March 30, 2022, 10:03:22 pm »
Issue is fixed. I removed my custom window code and now it all works as expected.