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 !