SFML community forums

Bindings - other languages => DotNet => Topic started by: Recoil on July 01, 2019, 04:00:30 pm

Title: Multi-threading SFML Render Window
Post by: Recoil on July 01, 2019, 04:00:30 pm
I have a Render Window working that is used to test objects and text drawn to it.  That Render Window is in a class called Engine, that handles all the initializations, program loop, render window events, and disposing.

My Sub Main() initializes an Engine instance, and runs the Engines loop.  When I move or resize the Render Window the program freezes until I let the mouse go.  So now I need to multi-thread the SFML's already multi-threaded render window.  However, when putting the Engine's instance on a separate thread to initialize and run the program loop, I run into the same thing of freezing while I am moving or resizing the window.  This make sense as pretty much nothing has changed, and only forwarded the issue to a new thread, yet I am not sure where to handle the freezing issue at so when I move or resize the screen the program keeps cycling.

I'm not sure how good a code example will work in this instance, but it is VB code so as close to pseudo code as possible...

Main Module:
Module Test

    Public Enum State

        Initializing
        Running
        Stopped

    End Enum

    Private m_Engine As Engine
    Private EngineThread As Threading.Thread

    Sub Main()

        EngineThread = New Threading.Thread(AddressOf EngineStart)

        EngineThread.Start()

    End Sub

    Private Sub EngineStart()

        m_Engine = New Engine(True)

        m_Engine.Initialize()

        Do While m_Engine.EngineState = Engine.State.Running

            m_Engine.Run()

        Loop

        m_Engine.DestroyEngine()

    End Sub

End Module
 

Engine Class:
    Public Sub New()
    End Sub

    Public Sub Initialize()

        SetState()

        ResetClock()

        SetContextSettings()

        CreateNewRenderWindow()

    End Sub

    Public Sub Run()

        CalculateTime()             ' process timing stuff

        PrepareScreens()            ' set view, dispatch events, clear

        DrawStuffToScreens()        ' entities, gui, text

        PresentScreens()            ' display

    End Sub
 

The Render Window is already handling any window events, keyboard input, mouse actions, and joystick events by default, so I am not sure where, or how, to allow the program to continue to process while I am moving or resizing the window.  Is this just a default of using the SFML RenderWindow?
Title: Re: Multi-threading SFML Render Window
Post by: Hapax on July 01, 2019, 07:12:33 pm
If you need to update the window during resizing/moving, you will need to create the window manually or maybe using another library that covers that.
You can still add SFML into the window after creation.
Title: Re: Multi-threading SFML Render Window
Post by: Recoil on July 01, 2019, 07:32:51 pm
Thanks Hapax!

I'm not sure I need to do anything TBH.  But when I move the window and the UI info text updates, I get a spike in cycles per second from the timer, and a dip in FPS, but only for a brief second or two.  I'm not sure if that means the loop is catching and not processing, or simply processing but not updating the graphics.

I can't step through the debugger while I drag the window around to check.  Is there any way to actually tell?
Title: Re: Multi-threading SFML Render Window
Post by: Hapax on July 04, 2019, 10:15:53 pm
The spike/drop is happening because time is continuing whilst the program's execution is not. Then, when it regains control, it has a lot of time since the previous frame. How you deal with this time is up to you.
You can process it all as one big bit of time but that is subject to skipping certain parts throughout the update.
You can break it down into smaller chunks and process those as separate updates; this is a good method overall but is subject to large in-game time passing instantly when the execution is delayed.
Using that latter method, you can set a maximum allowed amount of time to be accumulated and clamp it to that range. So, for example, if you have set a maximum of 1 second of time then 30 seconds pass since last update, it would process only a second of it (probably broken into tiny chunks/steps) but discard the other 29 seconds.
Title: Re: Multi-threading SFML Render Window
Post by: Recoil on July 10, 2019, 03:42:35 pm
Thanks again Hapax!

It's been a few days because I have been trying to get justification working right on my control labels.  I finally got time to work up a test utilizing the TimeStep class you helped me with previously.

It contains a function OutOfSync that returns to allow the program to process all movements.  This part works until the window is moved or resized then halts processing until I release the window, then it continues the movements being done from where they stopped at.

This is not an overly huge issue as on a single player game it is like pausing it, and a multiplayer game the positions would be updated from the server anyways as soon as it got a chance.

I think I was looking at this the wrong way to be honest, because it would be the server portion that would pause if moving or resizing the window that would cause an interference.  While this is not likely to happen, it was something I though may be an issue at some point with updating the clients.  I may have to look at a different setup if using a server to host games with.
Title: Re: Multi-threading SFML Render Window
Post by: Recoil on July 15, 2019, 07:43:13 pm
After taking some time to mull this over, it doesn't matter what I do the form events are going to overtake any threading that I setup...

However, trying another route on customizing a form I found that if I set the form style to None and handled all my screen placement and resizing on my own, while it still bogs down a little, it still performs the operations.

My code probably will not help anyone else, but these links I went off of probably will:

Moving Borderless Windows (https://en.sfml-dev.org/forums/index.php?topic=14391.0)
How to make the entire window draggable? (https://en.sfml-dev.org/forums/index.php?topic=23834.0)