Okay, here we go again, this time I hope it'll be less of a headache
.
The following code draws a white rectangle instead of the blue one it should. The reason is that the RenderImage entity in Test's constructor is garbage collected.
using System;
using SFML.Graphics;
using SFML.Window;
namespace SFMLSandbox
{
class Program
{
static void Main()
{
RenderWindow app = new RenderWindow(new VideoMode(500, 500), "SFML Sandbox");
app.Closed += new EventHandler(OnClosed);
Test test = new Test();
GC.Collect();
while(app.IsOpened())
{
app.DispatchEvents();
app.Clear();
test.Draw(app);
app.Display();
}
}
static void OnClosed(object sender, EventArgs e)
{
((RenderWindow)sender).Close();
}
}
class Test
{
public Sprite sprite;
public Test()
{
RenderImage i = new RenderImage(500, 500);
i.Clear(Color.Blue);
i.Display();
sprite = new Sprite(i.Image);
}
public void Draw(RenderTarget target)
{
target.Draw(sprite);
}
}
}
My guess is it has something to do with weak references or whatever, this stuff is over my head. If I declare the RenderImage outside of the method (but still inside the class), it works as expected:
using System;
using SFML.Graphics;
using SFML.Window;
namespace SFMLSandbox
{
class Program
{
static void Main()
{
RenderWindow app = new RenderWindow(new VideoMode(500, 500), "SFML Sandbox");
app.Closed += new EventHandler(OnClosed);
Test test = new Test();
GC.Collect();
while(app.IsOpened())
{
app.DispatchEvents();
app.Clear();
test.Draw(app);
app.Display();
}
}
static void OnClosed(object sender, EventArgs e)
{
((RenderWindow)sender).Close();
}
}
class Test
{
public Sprite sprite;
RenderImage i;
public Test()
{
i = new RenderImage(500, 500);
i.Clear(Color.Blue);
i.Display();
sprite = new Sprite(i.Image);
}
public void Draw(RenderTarget target)
{
target.Draw(sprite);
}
}
}
Is this an actual bug or am I just using this wrong?
Thanks.