Edit: I've posted this in graphics because I think it's not .net specific.
Hello,
I have just started working with SFML (2.0 RC) in C# with the .Net Binding. To get a feel for the whole thing I wrote a simple test application with the help of the API documentation. I'm drawing a sprite with a 250*250 test texture, which I'm moving ten pixels at a time by modifying its position via a KeyPressed Event Handler. Additionally, I've registered a MouseButtonPressed Event Handler for clicking my sprite. To do that, I'm comparing the mouse position with the FloatRect of the sprite.
public void OnMousePressed
(object sender, MouseButtonEventArgs e
) { Vector2f mousePos
= new Vector2f
(Mouse
.GetPosition(window
).X, Mouse
.GetPosition(window
).Y); switch (e
.Button) { case Mouse
.Button.Left: if(sprite
.GetGlobalBounds().Contains(mousePos
.X, mousePos
.Y)) { window
.Close(); } break; } }
That works like a charm. But, if I have moved the sprite previously, then nothing happens when clicking on the new position. Instead, the window closes when I click the old position of the sprite.
This is my OnKeyPressed method:
public void OnKeyPressed
(object sender, KeyEventArgs e
) { float x
= sprite
.Position.X; float y
= sprite
.Position.Y; switch(e
.Code) { case Keyboard
.Key.Up: y
-= 10f
; break; case Keyboard
.Key.Down: y
+= 10f
; break; case Keyboard
.Key.Left: x
-= 10f
; break; case Keyboard
.Key.Right: x
+= 10f
; break; } Vector2f newPosition
= new Vector2f
(x, y
); sprite
.Position = newPosition
; Console
.WriteLine(sprite
.GetGlobalBounds().ToString()); } It always displays the same debug info: [FloatRect] Left(0) Top(0) Width(250) Height(250).
So, have I made a mistake? How is GetGlobalBounds() calculated and why isn't it using the new position of the sprite? Does it not use the Position member at all? Thanks for any help!
Of course.
Class Program with Main:
class Program
{ static void Main
(string[] args
) { SFMLProgram program
= new SFMLProgram
(); program
.StartSFMLProgram(); } }
Class SFMLProgram:
class SFMLProgram
{ RenderWindow window
; Sprite sprite
; public void OnClose
(object sender, EventArgs e
) { window
.Close(); } public void OnKeyPressed
(object sender, KeyEventArgs e
) { float x
= sprite
.Position.X; float y
= sprite
.Position.Y; switch(e
.Code) { case Keyboard
.Key.Up: y
-= 10f
; break; case Keyboard
.Key.Down: y
+= 10f
; break; case Keyboard
.Key.Left: x
-= 10f
; break; case Keyboard
.Key.Right: x
+= 10f
; break; } Vector2f newPosition
= new Vector2f
(x, y
); sprite
.Position = newPosition
; Console
.WriteLine(sprite
.GetGlobalBounds().ToString()); } public void OnMousePressed
(object sender, MouseButtonEventArgs e
) { Vector2f mousePos
= new Vector2f
(Mouse
.GetPosition(window
).X, Mouse
.GetPosition(window
).Y); switch (e
.Button) { case Mouse
.Button.Left: Console
.WriteLine(mousePos
); if(sprite
.GetGlobalBounds().Contains(mousePos
.X, mousePos
.Y)) { window
.Close(); } break; case Mouse
.Button.Right: break; } } public void StartSFMLProgram
() { window
= new RenderWindow
(new VideoMode
(800,
600),
"Testwindow"); window
.SetVisible(true); window
.Closed += new EventHandler
(OnClose
); window
.KeyPressed += new EventHandler
<KeyEventArgs
>(OnKeyPressed
); window
.MouseButtonPressed += new EventHandler
<MouseButtonEventArgs
>(OnMousePressed
); Texture texture
= new Texture
("placeholder.gif"); sprite
= new Sprite
(texture
); // Mainloop while (window
.IsOpen()) { window
.DispatchEvents(); window
.Clear(Color
.Black); window
.Draw(sprite
); window
.Display(); } } }