Skip to content

Instantly share code, notes, and snippets.

@trlewis
Created February 20, 2016 07:25
Show Gist options
  • Save trlewis/dbb709ecc88667714123 to your computer and use it in GitHub Desktop.
Save trlewis/dbb709ecc88667714123 to your computer and use it in GitHub Desktop.
SFML .Net with WPF
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="MyGrid">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<WindowsFormsHost x:Name="FormHost" Grid.Row="0">
</WindowsFormsHost>
<Button Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Bottom" Content="awdawd"
Margin="4" Padding="8 2"/>
</Grid>
</Window>
namespace WpfApplication1
{
using System;
using SFML.Graphics;
using SFML.Window;
using SFML.System;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : System.Windows.Window
{
private RenderWindow _renderWindow;
private CircleShape _circle;
public MainWindow()
{
InitializeComponent();
this._circle = new CircleShape();
this._circle.Radius = 20;
this._circle.FillColor = new Color(255, 0, 255, 255);
this._circle.Position = new Vector2f(2, 2);
//need to use this to prevent base.OnPaint and base.OnPaintBackground from erasing contents
var mysurf = new MyDrawingSurface();
this.FormHost.Child = mysurf;
SetdoubleBuffered(mysurf); //same results whether or not i do this.
var context = new ContextSettings { DepthBits = 24 };
this._renderWindow = new RenderWindow(mysurf.Handle);
this._renderWindow.MouseButtonPressed += _renderWindow_MouseButtonPressed;
var timer = new System.Windows.Threading.DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 0, 0, 1000 / 60);
timer.Tick += timer_Tick;
timer.Start();
}
public static void SetdoubleBuffered(System.Windows.Forms.Control c)
{
System.Reflection.PropertyInfo aProp =
typeof(System.Windows.Forms.Control).GetProperty(
"DoubleBuffered", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
aProp.SetValue(c, true, null);
}
void _renderWindow_MouseButtonPressed(object sender, MouseButtonEventArgs e)
{
//e.X and e.Y are actually what they should be. top-left of the visible area is 0,0
this._circle.Position = new Vector2f(e.X, e.Y);
}
void timer_Tick(object sender, EventArgs e)
{
this._renderWindow.DispatchEvents();
Random rand = new Random();
var clearColor = new Color((byte)rand.Next(255), (byte)rand.Next(255), (byte)rand.Next(255));
this._renderWindow.Draw(this._circle);
this._renderWindow.Clear(clearColor); //this works, it flashes all sorts of colors
//trying several types of things to draw in the hopes that one will work. so far no luck.
var rect = new RectangleShape();
rect.Position = new Vector2f(-10, -10);
rect.Size = new Vector2f(100, 200);
rect.FillColor = Color.Blue;
Vertex[] line = new Vertex[]
{
new Vertex(new Vector2f(0,0)) {Color = Color.White},
new Vertex(new Vector2f(50,100)) {Color = Color.Red}
};
//tried drawing before clearing, after calling Display(), none worked.
this._renderWindow.Draw(line, PrimitiveType.Lines);
this._renderWindow.Draw(rect);
this._renderWindow.Draw(this._circle);
this._renderWindow.Display();
}
public class MyDrawingSurface : System.Windows.Forms.Control
{
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
//base.OnPaint(e);
}
protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs pevent)
{
//base.OnPaintBackground(pevent);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment