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

Author Topic: Thread bug ?  (Read 2572 times)

0 Members and 1 Guest are viewing this topic.

Yuraj

  • Newbie
  • *
  • Posts: 8
  • C#
    • View Profile
    • Email
Thread bug ?
« on: May 01, 2013, 02:54:52 pm »
Hello,
there is probably bug in SFML (or only .net binding?), it can't draw things which were loaded in thread before (like text, textures etc.). I debugged this and It reach app.Draw(text); (it's loaded fully) but It can't draw it on screen.

using SFML.Graphics;
using SFML.Window;
using System;
using System.Threading;

namespace SFMLThreads
{
    class Program
    {
        static void Main(string[] args)
        {
            RenderWindow app = new RenderWindow(new VideoMode(800,600), "SFML Threads Test");
            app.SetFramerateLimit(60);
            app.Closed += (s,e) => app.Close();

            //Create and start thread
            Text text = null;
            new Thread(() =>
                {
                    text = new Text("test", new Font("C:\\Windows\\Fonts\\Verdana.ttf"), 25);
                }).Start();

            while (app.IsOpen())
            {
                app.DispatchEvents();

                app.Clear();
                if (text != null)
                {
                    app.Draw(text);
                }
                app.Display();
            }
        }
    }
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Thread bug ?
« Reply #1 on: May 01, 2013, 02:57:24 pm »
Please read http://www.sfml-dev.org/tutorials/2.0/graphics-draw.php ('Drawing from threads' section).
Laurent Gomila - SFML developer

Yuraj

  • Newbie
  • *
  • Posts: 8
  • C#
    • View Profile
    • Email
Re: Thread bug ?
« Reply #2 on: May 01, 2013, 03:00:25 pm »
Ehm, thanks  :) I thought It was only C++ (threads) related tutorial.

Yuraj

  • Newbie
  • *
  • Posts: 8
  • C#
    • View Profile
    • Email
Re: Thread bug ?
« Reply #3 on: May 01, 2013, 03:16:26 pm »
By the way, is it better to add all Draw logic into thread?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Thread bug ?
« Reply #4 on: May 01, 2013, 03:20:36 pm »
There's no absolute "best solution". It depends on what you do. But use threads only if you need to, don't forget that they have overhead and add complexity to your code.
Laurent Gomila - SFML developer