Hello SFML-Community!
I'm working on a small game with a friend of mine to learn C++ combined with SFML. (I've already experience with various programming languages, so I'm not a beginner!
)
Now, we want to develop a small map editor for our game. Because I've advanced skills in C# and Windows Forms, I chose this language, esspecially to concentrate on the code besides the GUI-stuff.
Before I start with the design, I wanted to try the SFML.NET binding, so I can use it also in Windows Forms applications. I got the RenderWindow to draw it stuff into an User Control, derived from a panel. I added this panel to my form and it would draw the sprite correctly - but there's still a problem: the panel flickers a lot!
I've already searched the forum and changed the styles of my panel (UserPaint, OptimizedDoubleBuffer, AllPaintingInWmPaint); the control is still flickering.
Here's the code of my User Control:
using System;
using System.Windows.Forms;
namespace Editor.UserControls
{
public partial class sfmlPanel : Panel
{
// Timer for refreshing the panel
System.Windows.Forms.Timer timer = new Timer();
// SFML-stuff
SFML.Graphics.RenderWindow sfml_RWnd;
SFML.Graphics.Image image;
SFML.Graphics.Sprite sprite;
// Constructor
public sfmlPanel()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
UpdateStyles();
// Raise the timer_tick-event after 10ms
timer.Interval = 10;
// Adds the timer_tick-event
timer.Tick += new EventHandler(timer_Tick);
// Activate the timer
timer.Enabled = true;
// Set the handle for the RenderWindow to the panel
sfml_RWnd = new SFML.Graphics.RenderWindow(this.Handle);
}
void timer_Tick(object sender, EventArgs e)
{
// Refresh the panel
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
sfml_RWnd.Clear(SFML.Graphics.Color.Blue);
sfml_RWnd.Draw(sprite);
sfml_RWnd.Display();
}
public void AddSprite(SFML.Graphics.Sprite srcSprite)
{
sprite = srcSprite;
sprite.Position = new SFML.Graphics.Vector2(20, 20);
}
}
}
And here's the code to include the panel to my form:
...
UserControls.sfmlPanel panMap;
SFML.Graphics.Image sfml_Image;
SFML.Graphics.Sprite sfml_Sprite;
...
// Constructor
public frmMain()
{
...
panMap = new UserControls.sfmlPanel();
panMap.Top = 0;
panMap.Left = 0;
panMap.Width = 200;
panMap.Height = 200;
sfml_Image = new SFML.Graphics.Image("tile_01.png");
sfml_Sprite = new SFML.Graphics.Sprite(sfml_Image);
sfml_Sprite.Position = new SFML.Graphics.Vector2(20, 20);
panMap.AddSprite(sfml_Sprite);
this.Controls.Add(panMap);
...
}
Maybe I forgot just a small stupid detail and you can help me?
Thanks in advance!
Best regards
PS: I'm sorry for my English, but I'm naturally speaking German!