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

Author Topic: Sprite animation gone wrong  (Read 2319 times)

0 Members and 1 Guest are viewing this topic.

KoniGTA

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Sprite animation gone wrong
« on: December 24, 2016, 08:48:42 pm »
After implementing a few bugs in the program i finally got it running with no errors.However there's a problem with the images loading up
Here's the main part:
#include <SFML\Graphics.hpp>
#include <iostream>
#include"Animatrix.h"
int main()
{
   sf::RenderWindow window(sf::VideoMode(1360, 720), "Larger SFML", sf::Style::Default);
   sf::Texture texture;
   texture.loadFromFile("bahamut.png");
   texture.setRepeated(false);
   Animatrix first(&texture,sf::Vector2u(4,4), 0.3f);
   float fraps = 0.0;
   while (window.isOpen())
   {   
      sf::Clock fps;
      sf::Event event;
      while (window.pollEvent(event))
      {
         switch (event.type)
         {
         case sf::Event::Closed:
            std::cout << std::endl << "I wont let u close!MUAHAHAHAHAHA!!";
            return 0;
            break;
         case sf::Event::Resized:
            std::cout << "New height" << event.size.height << " New width" << event.size.width << std::endl;
            break;
         case sf::Event::TextEntered:
            printf("You have pressed =%c \n", event.text.unicode);
            break;
         }
         
      }
      fraps = fps.restart().asSeconds();
      first.Update(fraps);
      window.clear(sf::Color::Red);
      window.draw(first.show());
      window.display();
   }
   
   return 0;
}

Here's the header of class animatrix:
#pragma once
#include "SFML\Graphics.hpp"
class Animatrix
{
public:
   Animatrix(sf::Texture* texture,sf::Vector2u num,float stime);
   sf::Vector2u currentimage;
   sf::Vector2u size;
   float stime, fps;
   float totaltime;
   sf::Sprite show();
   void Update(float fps);
   sf::Sprite sprite;
   sf::Vector2u count;
   int j;
   ~Animatrix();
};


Here's the definition of Animatrix.h:

#include "Animatrix.h"
#include"SFML\Graphics.hpp"
#include<iostream>



Animatrix::Animatrix(sf::Texture* texture,sf::Vector2u num,float stime)
{
    sprite.setTexture(*texture);
   count.x = num.x;
   count.y = num.y;
   size.x= texture->getSize().x / count.x ;
   size.y = texture->getSize().y / count.y ;
   std::cout << std::endl << texture->getSize().x;
   currentimage.x = 0;
   currentimage.y = 0;
   totaltime = 0.0;
   this->stime = stime;
   j = 0;
   std::cout << std::endl << "size.x="<< size.x;
}

void Animatrix::Update(float fps)
{
   this->fps = fps;
   totaltime = totaltime + this->fps;
   
   if( totaltime >= stime )
   {
   totaltime = totaltime - stime;
      if (j == 0)
      {
         if (currentimage.x < count.x)
         {
            currentimage.x++;
         }
         if(currentimage.x==count.x-1)
         {
            j++;
         }
      }
      else
         
      {
         std::cout <<std::endl<< "yo";
         j++;
            currentimage.x--;
            if (currentimage.x == 0)
            {
               j = 0;
            }
      }
   }
   
}
sf::Sprite Animatrix::show()
{
   sf::Vector2u tempos;
   tempos.x = currentimage.x*size.x;
   tempos.y = currentimage.y*size.y;
   std::cout <<std::endl<< "tempos.x= " << tempos.x;
   sf::Vector2u newpos;
   newpos.x = size.x + tempos.x;
   newpos.y = size.y + tempos.y;
   std::cout << std::endl << "newpos.x= " << newpos.x;
   sprite.setTextureRect(sf::IntRect(tempos.x, tempos.y, size.x + tempos.x, size.y + tempos.y));
   return sprite;
}

Animatrix::~Animatrix()
{
}

The images load up as given in attachment.The original image is the bahamut.png.The first up in the animation is bahamut1.png which seems like the obvious one to come.Then when it switches the sprites the sprite displayed is as in bahamut2.png.Same goes for the next time as in bahamut3.png.However in the final step after which it should run in reverse order the final sprite displays properly as in only the last texture is displayed i.e the 4th one only in bahamut.png.And then once it starts reversing the same follows again.


TLDR:1st and last image load up properly.2nd and 3rd image load in pairs as shown in bahamut2 and bahamut3.png
Please help I'm new to SFML!Don't go scolding me !

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Sprite animation gone wrong
« Reply #1 on: December 24, 2016, 11:10:03 pm »
Quote
sprite.setTextureRect(sf::IntRect(tempos.x, tempos.y, size.x + tempos.x, size.y + tempos.y));
Rect (T rectLeft, T rectTop, T rectWidth, T rectHeight)
Don't add tempos to your size.

KoniGTA

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Sprite animation gone wrong
« Reply #2 on: December 25, 2016, 09:10:20 am »
Could you please elaborate?And the coordinates that i get after adding are the correct ones.I checked using cout.Can you please explain why the error happens if i add them?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Sprite animation gone wrong
« Reply #3 on: December 25, 2016, 04:27:28 pm »
sf::IntRect(x, y, width, height)

x and y are the coordinates of the top left corner of your sprite.
0 and 0 for the first animation of the first row.
around 38 (150 / 4) and 0 for the second animation of the first row
around 76 and 0 for the third, etc.

width and height are not coordinates of the bottom right corner of your sprite, they are a size and since all the frames of your animations are the same size, it shouldn't vary and should be around 38 and 38.
If you add tempos to your size, you get a larger size then expected and that's why you see more than 1 frame at the same time. (it "works" for the first animation of the first row because tempos is 0)

KoniGTA

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Sprite animation gone wrong
« Reply #4 on: December 27, 2016, 02:58:58 pm »
TY