up vote 0 down vote favorite
share [fb]

I've been trying to create an SMFL program which can update graphics even while the window is being resized.

I created a thread which runs a function that collects events, and initially creates the render window.

The event gathering function runs properly, and the program seems to respond to events as it's supposed to; I can't get anything to display, however. Even a simple Clear(sf::Color) command does not display.

Please note that I have already checked this in a debugger, and can confirm the the draw commands are executed, they just don't do anything to the screen.

Update:

I tried adding a Clear(sf::Color::White); command inside the thread function, and it worked. However, I cannot update graphics from within the thread function, as that would be highly impractical and inconvenient.

Here's the code:

System.cpp

#include "system.h"

Sys::System::System::System(const sf::VideoMode& rVM, const std::string& rSTR) :
    vm(rVM),
    vtitle(rSTR),
    win(0),
    gInst(*this)
{
}

void Sys::System::System::run()
{
    for(;;)
    {
        eventQueueMutex.Lock();
        while (!eventQueue.empty())
        {
            if (gInst.menu.onEvent(eventQueue.front()) == 0) {}

            if (eventQueue.front().Type == sf::Event::Closed)
                return;

            eventQueue.pop();
        }
        eventQueueMutex.Unlock();

        win->Clear();
        gInst.menu.draw();
        win->Display();
    }
}

void Sys::System::start()
{
    // Initialize system

    System s(sf::VideoMode(1024, 600, 32), "Test 2");

    // Load standard resources

    s.mainFont.LoadFromMemory(Sys::EmbeddedFiles::getStandardFontPointer(), Sys::EmbeddedFiles::getStandardFontSize(), 18);

    // Start event gatherer thread (this also creates the render window)

    sf::Thread thread(&threadEventFunction, &s);
    thread.Launch();

    for(;;)
    {
        s.gathererSignalMutex.Lock();
        if (s.win) break;
        s.gathererSignalMutex.Unlock();
    }
    // No need to unlock the mutex here (it never gets used again)



    // Attempting to access the render window before this point will
    // result in a segmentation violation

    // Change graphics settings (win now points to heap space)

    s.win->SetFramerateLimit(120);

    // Create GUI menus and menu entries

    s.uiInit();

    // Start running the editor program

    s.run();

    // Wait for the gatherer thread to end

    s.win->Close();
    thread.Wait();
}

Ui_input.cpp

#include "system.h"



void Sys::System::threadEventFunction(void* arg)
{
    System::System& rS = *static_cast<System::System*>(arg);

    rS.gathererSignalMutex.Lock();
    rS.win = new sf::RenderWindow(rS.vm, rS.vtitle);
    rS.gathererSignalMutex.Unlock();

    sf::RenderWindow& rWin = *rS.win;

    sf::Event event;

    while (rWin.IsOpened())
    {
        while (rWin.GetEvent(event))
        {
            rS.eventQueueMutex.Lock();
            rS.eventQueue.push(event);
            rS.eventQueueMutex.Unlock();
        }

        sf::Sleep(0.01f);
    }
}
link|improve this question

50% accept rate
feedback

1 Answer

Try debugging the code and stepping through it to see if it's getting stuck somewhere, or you could move the Clear call to earlier and earlier positions until it starts doing something... I can't see anything horrendously wrong with the code, and it would be hard to tell you what's wrong without debugging.

link|improve this answer
Draw, Clear commands fail to produce any effect on the window, regardless of where I place them. This is very unusual. – Truncheon Dec 31 '11 at 9:46
I would recommend stepping through the code with a debugger, or at the very least writing output to a console window to mark what code has been executed. I feel like there's something that's preventing any of your drawing code to be reached. – Robert Rouhani Dec 31 '11 at 9:55
See the update in my question. – Truncheon Jan 1 at 2:37
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.