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

Author Topic: Sprite Sheets in SFML  (Read 5062 times)

0 Members and 1 Guest are viewing this topic.

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Sprite Sheets in SFML
« on: April 24, 2014, 10:18:56 pm »
Been hunting the forums for this and I know it has been done but can't find it.  I know it is similar to what is done with tile mapping but not sure what to use for the sheet and selecting the part of the animation.  :(
I have many ideas but need the help of others to find way to make use of them.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Sprite Sheets in SFML
« Reply #1 on: April 24, 2014, 10:59:03 pm »
Ehh, creating a sprite sheet is extremely simple. Just use gimp, photoshop or any other image editor to create a large image with all your small sprite images on it. Then, when setting a texture on a sprite just give the appropriate rectangle where the image you want is found in your big texture.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
AW: Sprite Sheets in SFML
« Reply #2 on: April 25, 2014, 08:01:21 am »
Create a sprite sheet, i.e. collection of multiple sprites, in your favorite image editor.
Then use setTextureRect on an sf::Sprite to "select" the part you want display.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Sprite Sheets in SFML
« Reply #3 on: April 25, 2014, 01:29:08 pm »
or SFML.Graphics.Sprite.SetTextureRect(...) in SFML.NET  :)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: Sprite Sheets in SFML
« Reply #4 on: April 25, 2014, 04:32:27 pm »
lol I think I'm doing it the hard way. :D


    public class SFMLAnimatedTexture
    {
        private List<Texture> animationlist;
        private Image sheetimage;
        private int activepaslide, delay,framecount, columns, framewidth, frameheigth;
       

        public SFMLAnimatedTexture(ref Image sheet, int animationdelay, int numberofcolumns, int framew, int frameh)
        {
            sheetimage = sheet;
            delay = animationdelay;
            columns = numberofcolumns;
            framewidth = framew;
            frameheigth = frameh;

            framecount = 0;
            activepaslide = 0;

            BreakupSpriteSheet(ref sheetimage, framewidth, frameheigth, columns);
        }

        public Texture SelectAnimationSlide(int slide)
        {

            if (slide >= 0 && slide < animationlist.Count())
            {
                return animationlist[slide];
            }
            else
            {
                return null;
            }
        }
       
        public void UpdateAnimation(ref Texture usertexture, int animationdirection)
        {
            framecount++;
            if (framecount > delay)
            {
                framecount = 0;
                activepaslide += animationdirection;
                if(activepaslide < 0)
                {
                    activepaslide = animationlist.Count() - 1;
                }
                else if (activepaslide > animationlist.Count() - 1)
                {
                    activepaslide = 0;
                }
                usertexture = SelectAnimationSlide(activepaslide);
            }
           
        }

        private void BreakupSpriteSheet(ref Image sfmlimage, int slidewidth, int slideheigth, int cols)
        {
            for (int i = 0; i < cols; i++)
            {
                int xloc = (i % cols) * slidewidth,
                    yloc = (i / cols) * slideheigth;

                animationlist.Add(new Texture(sfmlimage, new IntRect(xloc, yloc, slidewidth, slideheigth)));
            }
        }

    }
 

:P  Went for breaking it up into a list but I guess it wouldn't hurt to have a few different ways of doing it. :)
I have many ideas but need the help of others to find way to make use of them.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Sprite Sheets in SFML
« Reply #5 on: April 25, 2014, 04:37:02 pm »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Sprite Sheets in SFML
« Reply #6 on: April 25, 2014, 06:55:09 pm »
...and the equivalent functionality in NetEXT, Thor's C# port by zsbzsb :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: