1
SFML wiki / Animated Sprite SFML.NET
« on: November 06, 2013, 04:19:21 pm »
Hi.
I write in C# and i feel like wiki is a bit empty about SFML.NET so i wrote for my game AnimationSprite and Animation classes. I change it a bit but after all with few modifications should be great for wiki
I write in C# and i feel like wiki is a bit empty about SFML.NET so i wrote for my game AnimationSprite and Animation classes. I change it a bit but after all with few modifications should be great for wiki
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SFML;
using SFML.Graphics;
using SFML.Window;
namespace Draw
{
class Animation
{
List<IntRect> m_frames = new List<IntRect>();
static Texture m_texture;
public Animation()
{
}
public void addFrame(IntRect rect)
{
m_frames.Add(rect);
}
public void setSpriteSheet(Texture texture)
{
m_texture = texture;
}
public Texture getSpriteSheet()
{
return m_texture;
}
public int getSize()
{
return m_frames.Count;
}
public IntRect getFrame(int n)
{
return m_frames[n];
}
}
class AnimatedSprite : Draw.Display
{
Animation m_animation;
Vector2f m_position;
int m_currentTime;
int m_frameTime;
int m_currentFrame;
bool m_animation_direction;
bool m_isPaused;
bool m_isLooped;
Texture m_texture;
Sprite m_sprite;
public AnimatedSprite(int frameTime, bool paused, bool looped, bool animdir = false)
{
m_frameTime = frameTime;
m_currentFrame = 0;
m_isPaused = paused;
m_isLooped = looped;
m_animation_direction = animdir; //False = normal animation (farward)
}
public void setAnimation(Animation animation)
{
m_animation = animation;
m_texture = m_animation.getSpriteSheet();
m_sprite = new Sprite(m_texture);
m_currentFrame = 0;
}
public void setFrameTime(int time)
{
m_frameTime = time;
}
public void play()
{
m_isPaused = false;
}
public void pause()
{
m_isPaused = true;
}
public void stop()
{
m_isPaused = true;
m_currentFrame = 0;
}
public void setLooped(bool looped)
{
m_isLooped = looped;
}
public Animation getAnimation()
{
return m_animation;
}
public bool isLooped()
{
return m_isLooped;
}
public bool isPlaying()
{
return !m_isPaused;
}
public int getFrameTime()
{
return m_frameTime;
}
/// <summary>
///
/// </summary>
/// <param name="time">How many do you want to add to m_currentTime</param>
/// <param name="change_direciton">Do you want to change direction after getiin to one end of animation</param>
public void setFrame(int time, bool change_direciton = false)
{
if (!m_isPaused) //If not paused
{
m_currentTime += time; //Add time
if (m_currentTime >= m_frameTime) //if time is bigger then time of one frame
{
if (m_animation_direction == true)
{
if (m_currentFrame > 0) //if current frame is in bounds
{
m_currentFrame--; //Next frame
setAnimation(); //Animate
}
else //if current frame is out of bound
{
if (m_isLooped == false)
{
pause();
}
if (change_direciton == true)
{
m_animation_direction = false;
}
else
{
m_currentFrame = m_animation.getSize() - 1;
}
}
}
else
{
if (m_currentFrame < m_animation.getSize()-1) //if current frame is in bounds
{
m_currentFrame++; //Next frame
setAnimation(); //Animate
}
else //if current frame is out of bound
{
if (m_isLooped == false)
{
pause();
}
if (change_direciton == true)
{
m_animation_direction = true;
}
else
{
m_currentFrame = 0;
}
}
}
m_currentTime = 0;
}
}
}
public void setPosition(Vector2f pos)
{
m_position = pos;
}
public void setAnimation()
{
IntRect rect = m_animation.getFrame(m_currentFrame);
m_sprite.Position = m_position;
m_sprite.TextureRect = rect;
}
protected override void OnDraw(RenderTarget target, RenderStates states)
{
states = new RenderStates(states);
target.Draw(m_sprite,states);
}
}
}
Basic code in mainusing System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SFML;
using SFML.Graphics;
using SFML.Window;
namespace Draw
{
class Animation
{
List<IntRect> m_frames = new List<IntRect>();
static Texture m_texture;
public Animation()
{
}
public void addFrame(IntRect rect)
{
m_frames.Add(rect);
}
public void setSpriteSheet(Texture texture)
{
m_texture = texture;
}
public Texture getSpriteSheet()
{
return m_texture;
}
public int getSize()
{
return m_frames.Count;
}
public IntRect getFrame(int n)
{
return m_frames[n];
}
}
class AnimatedSprite : Draw.Display
{
Animation m_animation;
Vector2f m_position;
int m_currentTime;
int m_frameTime;
int m_currentFrame;
bool m_animation_direction;
bool m_isPaused;
bool m_isLooped;
Texture m_texture;
Sprite m_sprite;
public AnimatedSprite(int frameTime, bool paused, bool looped, bool animdir = false)
{
m_frameTime = frameTime;
m_currentFrame = 0;
m_isPaused = paused;
m_isLooped = looped;
m_animation_direction = animdir; //False = normal animation (farward)
}
public void setAnimation(Animation animation)
{
m_animation = animation;
m_texture = m_animation.getSpriteSheet();
m_sprite = new Sprite(m_texture);
m_currentFrame = 0;
}
public void setFrameTime(int time)
{
m_frameTime = time;
}
public void play()
{
m_isPaused = false;
}
public void pause()
{
m_isPaused = true;
}
public void stop()
{
m_isPaused = true;
m_currentFrame = 0;
}
public void setLooped(bool looped)
{
m_isLooped = looped;
}
public Animation getAnimation()
{
return m_animation;
}
public bool isLooped()
{
return m_isLooped;
}
public bool isPlaying()
{
return !m_isPaused;
}
public int getFrameTime()
{
return m_frameTime;
}
/// <summary>
///
/// </summary>
/// <param name="time">How many do you want to add to m_currentTime</param>
/// <param name="change_direciton">Do you want to change direction after getiin to one end of animation</param>
public void setFrame(int time, bool change_direciton = false)
{
if (!m_isPaused) //If not paused
{
m_currentTime += time; //Add time
if (m_currentTime >= m_frameTime) //if time is bigger then time of one frame
{
if (m_animation_direction == true)
{
if (m_currentFrame > 0) //if current frame is in bounds
{
m_currentFrame--; //Next frame
setAnimation(); //Animate
}
else //if current frame is out of bound
{
if (m_isLooped == false)
{
pause();
}
if (change_direciton == true)
{
m_animation_direction = false;
}
else
{
m_currentFrame = m_animation.getSize() - 1;
}
}
}
else
{
if (m_currentFrame < m_animation.getSize()-1) //if current frame is in bounds
{
m_currentFrame++; //Next frame
setAnimation(); //Animate
}
else //if current frame is out of bound
{
if (m_isLooped == false)
{
pause();
}
if (change_direciton == true)
{
m_animation_direction = true;
}
else
{
m_currentFrame = 0;
}
}
}
m_currentTime = 0;
}
}
}
public void setPosition(Vector2f pos)
{
m_position = pos;
}
public void setAnimation()
{
IntRect rect = m_animation.getFrame(m_currentFrame);
m_sprite.Position = m_position;
m_sprite.TextureRect = rect;
}
protected override void OnDraw(RenderTarget target, RenderStates states)
{
states = new RenderStates(states);
target.Draw(m_sprite,states);
}
}
}
Draw.Animation animation = new Draw.Animation();
animation.setSpriteSheet(texture.TextureName("Door"));
for (int i = 0; i < 9;i++ )
animation.addFrame(new IntRect(6*i, 0, 6, 50));
Draw.AnimatedSprite animspr = new Draw.AnimatedSprite(2, false, false);
animspr.setPosition(new Vector2f(42,42));
animspr.setAnimation(animation);
animspr.setFrame(1, true); //Update/Draw
window.Draw(animspr); //Draw
Regards Tom.
animation.setSpriteSheet(texture.TextureName("Door"));
for (int i = 0; i < 9;i++ )
animation.addFrame(new IntRect(6*i, 0, 6, 50));
Draw.AnimatedSprite animspr = new Draw.AnimatedSprite(2, false, false);
animspr.setPosition(new Vector2f(42,42));
animspr.setAnimation(animation);
animspr.setFrame(1, true); //Update/Draw
window.Draw(animspr); //Draw