Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Animated Sprite SFML.NET  (Read 4359 times)

0 Members and 1 Guest are viewing this topic.

Muchaszewski

  • Newbie
  • *
  • Posts: 1
    • View Profile
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 :)
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 main
            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.
« Last Edit: November 06, 2013, 04:23:13 pm by Muchaszewski »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Animated Sprite SFML.NET
« Reply #1 on: November 06, 2013, 04:55:28 pm »
This looks good, but you do need to make some changes/cleanup.

  • Remove unused 'using' statements - at the moment you are using only 3/8 of your statements
  • Remove empty constructors
  • Use properties - you are not writing C++ here
  • Where is Draw.Display defined?
  • Don't use outdated Hungarian (m_xxx) prefixes
  • Clearly define what functions do, you have setAnimation doing two different things with an overload and setFrame is more like an update function
  • You are using an int for time. Is this MS, SEC, MIN, or what? Consider using a Timespan or better yet NetEXT's version of the SFML time classes (see my signature)
  • In your OnDraw() call you are creating a new RenderStates with the exact same properties of the original. The point to this is...?

These are a few of the things I noticed, also if you don't know I have ported most of Thor to .NET in my NetEXT project. The entire animation module is already ported  ;)
« Last Edit: November 06, 2013, 04:57:41 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

 

anything