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

Author Topic: Window freezes when not moused over  (Read 2819 times)

0 Members and 1 Guest are viewing this topic.

djlynn03

  • Newbie
  • *
  • Posts: 5
    • View Profile
Window freezes when not moused over
« on: March 09, 2022, 08:21:10 pm »
Hello everyone,
I am creating a image slideshow application, and after running for a few cycles, the slideshow stops until I move my mouse over the window. When I move my mouse over the window, sometimes it runs another cycle and sometimes it only moves to the next image. I would like it to continue running indefinitely and change images after a second.

My main function looks like this:
int WINAPI WinMain(
    _In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPSTR     lpCmdLine,
    _In_ int       nCmdShow
)
{
    AllocConsole();
    freopen("CONOUT$", "w", stdout);
    freopen("CONOUT$", "w", stderr);

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

    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::string path = "\\";
    std::vector<std::string> imagePaths;

    for (const auto& entry : fs::directory_iterator(path))
    {
        if ((lastN(entry.path().string(), 3) == std::string{ "png" } || lastN(entry.path().string(), 3) == std::string{ "jpg" } || lastN(entry.path().string(), 3) == std::string{ "bmp" })
            && entry.exists() && entry.file_size() > 0)
        {
           
            imagePaths.push_back(entry.path().string());
        }
    }
    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);

    while (1) {
        if (GetMessage(&msg, NULL, 0, 0) > 0) {
            std::cout << &msg << "\n";
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        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(1));
    }
    return (int)msg.wParam;
}
 

It appears to get hung up during my main loop. The images are all accessed correctly, because it is able to cycle through all of them a few times before it gets hung up. Any help would be greatly appreciated.


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Window freezes when not moused over
« Reply #1 on: March 10, 2022, 11:12:20 am »
Why are you writing all the window creation code yourself?

When you create a SFML window from a handle, SFML takes over the event processing loop, so you need to handle the SFML events and don't really have to write your own event handling.
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: Window freezes when not moused over
« Reply #2 on: March 10, 2022, 11:52:27 pm »
I removed the event handler and the reference to it in the main loop and it started working correctly.

 

anything