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

Author Topic: Window doesn't display anything while threading  (Read 3613 times)

0 Members and 1 Guest are viewing this topic.

nihohit

  • Newbie
  • *
  • Posts: 37
    • View Profile
Window doesn't display anything while threading
« on: March 20, 2012, 11:55:24 am »
Hey,
My program has seperate logic and a screen parts, and when I try to run them on seperate threads, the screen doesn't display anything. When run sequentially ( in a
Code: [Select]
while(true)
{
    logic.loop();
    display.loop();
}

 loop) everything works, but when done in seperate threads (as is in the code below) - the screen is consistently black.


Code: [Select]
class main
    {
        static int Main(string[] args)
        {
            City_Generator.GameBoard city = City_Generator.CityFactory.createCity(30, 20);

            Buffers.DisplayBuffer disp = new Buffers.DisplayBuffer();
            Buffers.InputBuffer input = new Buffers.InputBuffer();
            Buffers.SoundBuffer sound = new Buffers.SoundBuffer();
            Graphic_Manager.DisplayManager display = new Graphic_Manager.DisplayManager(30 * 32, 20 * 32, 32, disp, city.Img);
            Logic.GameLogic logic = new Logic.GameLogic(disp, input, sound, city, 100);
           
            Thread logicThread = new Thread(new ThreadStart(logic.run));
            Thread graphicThread = new Thread(new ThreadStart(display.run));
            logicThread.Start();
            graphicThread.Start();
            logicThread.Join();
            graphicThread.Join();

            return 1;
        }

    }

nihohit

  • Newbie
  • *
  • Posts: 37
    • View Profile
Window doesn't display anything while threading
« Reply #1 on: March 20, 2012, 11:57:46 am »
Odd thing - I now tried to run the same program, only without creating an additional thread for the graphics, but by running it from the main thread, and this has created an InvalidOperationException on a modified Collection - the error doesn't appear on seperate threads, but I'm sure that both of them run.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Window doesn't display anything while threading
« Reply #2 on: March 20, 2012, 12:03:14 pm »
Could you translate this code to pseudo-code showing which SFML classes are involved in each thread, and which functions you call on them (especially on the (Render)Window class).
Laurent Gomila - SFML developer

nihohit

  • Newbie
  • *
  • Posts: 37
    • View Profile
Window doesn't display anything while threading
« Reply #3 on: March 20, 2012, 12:27:39 pm »
sure:

logic.run:
1. while(true) logic.loop()

logic.loop():
1. process info
2. displayBuffer.updateInfo(info)

displayBuffer.updateInfo(info)
1. foreach Entity ent in info:
2. Sprite sprite = relevantSprite(ent)
3. List sprites.Add(sprite)

displayBuffer.getSprites()
1. return sprites;

graphic.run:
1. while(true) graphic.loop()

graphic.loop():
1. drawnSprites.clear()
2. Window mainWindow.clear()
3. mainWindow.draw(background) //background is a constant
4. drawnSprites = displayBuffer.getSprites()
5. foreach Sprite sprite in drawnSprites:
6. mainWindow.draw(sprite)
7. mainWindow.display();

the odd thing is that I would've expected that at least the background would've been displayed constatnly, but even that isn't shown.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Window doesn't display anything while threading
« Reply #4 on: March 20, 2012, 01:28:05 pm »
There are two important things:
1. events must be polled in the same thread where the window was created
2. Clear/Draw/Display must be called in the thread where the window was created; otherwise you have to explicitely deactivate the OpenGL context (window.SetActive(false)).
Laurent Gomila - SFML developer

nihohit

  • Newbie
  • *
  • Posts: 37
    • View Profile
Window doesn't display anything while threading
« Reply #5 on: March 20, 2012, 02:02:51 pm »
Thanks! 2 solved my problem.

When you talk about polling events, do you mean that only the thread that calls window.display() should call window.draw(sprite), or something else?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Window doesn't display anything while threading
« Reply #6 on: March 20, 2012, 02:10:17 pm »
Quote
When you talk about polling events

I mean the call to window.DispatchEvents().
Laurent Gomila - SFML developer

 

anything