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

Author Topic: App freezes when maximizing window  (Read 1680 times)

0 Members and 1 Guest are viewing this topic.

Thrusty

  • Newbie
  • *
  • Posts: 16
    • View Profile
App freezes when maximizing window
« on: March 31, 2011, 04:39:27 pm »
Hi there.

I'm experimenting with SFML as I wish to change my project over to this from SDL.

I've created a little test program to see if I can get everything working how I want it. The problem is when I press the windows maximize window button. My hunch is that this is causing the main thread to freeze. Just not sure how to compensate for this.

Here's my code:

(CodeBlocks; C++ compiler options: -static-gcc, -mwindows)

Code: [Select]
#include <SFML/Graphics.hpp>

sf::RenderWindow* win_ptr = 0;
std::deque< sf::Event > event_buffer;
sf::Mutex buffer_mutex;

void event_thread_func(void*)
{
    win_ptr = new sf::RenderWindow(sf::VideoMode(640,480,32), "SFML");
    win_ptr->SetActive(false);
    sf::Event event;

    while (win_ptr->IsOpened())
    {
        while (win_ptr->GetEvent(event))
        {
            buffer_mutex.Lock();

            event_buffer.push_back(event);

            buffer_mutex.Unlock();
        }

        sf::Sleep(0.01f);
    }
}

int main()
{
    bool sleep = false;

    sf::Clock Clock;
    sf::String Hello;
    Hello.SetText(L"The quick brown fox jumps over the lazy old dog.");
    Hello.SetColor(sf::Color(127, 63, 0));
    Hello.SetPosition(100.f, 100.f);
    Hello.SetRotation(15.f);
    Hello.SetSize(14);

    sf::Thread event_thread(event_thread_func);
    event_thread.Launch();

    while (win_ptr == 0)
    {
        sf::Sleep(0.01f);
    }
    win_ptr->SetActive(true);

    win_ptr->Clear();
    win_ptr->Display();

    while (win_ptr->IsOpened())
    {
        {
            buffer_mutex.Lock();
            if (!event_buffer.empty())
            {
                if (event_buffer.front().Type == sf::Event::Closed)
                    break;

                event_buffer.pop_front();
            }
            else
                sleep = true;
            buffer_mutex.Unlock();
        }
        if (sleep)
            sf::Sleep(0.01f);

        if (1.f / win_ptr->GetFrameTime() > 1000 / 60)
        {
            Hello.SetRotation(Clock.GetElapsedTime()*100);
            win_ptr->Clear();
            win_ptr->Draw(Hello);
            win_ptr->Display();
        }
    }

    win_ptr->Close();
    return 0;
}

Thrusty

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: App freezes when maximizing window
« Reply #1 on: March 31, 2011, 05:29:33 pm »
Never mind. Fixed it by using my own timing method:

Code: [Select]
   while (win_ptr->IsOpened())
    {
        begin = Clock.GetElapsedTime();

        //input

        if (counter > .029f)
        {
            counter = 0;
            win_ptr->Clear();
            win_ptr->Draw(Sprite);
            win_ptr->Display();
        }

        counter += Clock.GetElapsedTime() - begin;
    }