Bonjour, j'ai codé un système pour les animations en séparant mon code en trois classes, en suivant un tuto. J'ai des Frame (qui ont un intRect et un pointeur sur texture), une classe animation avec un tableau de frame, et une classe animated possédant un pointeur sur une animation, et héritant de sf::Sprite.
J'utilise également un gestionnaire de texture que j'ai codé.
Voila le main:
int main()
{
sf::RenderWindow app(sf::VideoMode(680, 680, 32), "Titan");
app.setFramerateLimit(60);
Graphic_Manager lolilol;
Animation anim;
anim.push_frame(Frame(lolilol.getTexture("nuki.png")));
anim.push_frame(Frame(lolilol.getTexture("avatar.jpg")));
Animated m_lol(anim, 0.1f);
m_lol.play();
while (app.isOpen())
{
sf::Event event;
while(app.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
app.close();
break;
default:
break;
}
}
// Remplissage de l'écran (couleur noire par défaut)
app.clear();
m_lol.anim(2.f);
app.draw(m_lol);
// Affichage de la fenêtre à l'écran
app.display();
}
return EXIT_SUCCESS;
}
Et le reste:
#include"frame.hpp"
Frame::Frame(sf::Texture& texture)
{
m_texture = &texture;
m_rect = sf::IntRect(0,0,texture.getSize().x, texture.getSize().y);
}
Frame::Frame(sf::Texture& texture, sf::IntRect rect)
{
m_texture = &texture;
m_rect = rect;
}
#ifndef FRAME_HPP_INCLUDED
#define FRAME_HPP_INCLUDED
#include <SFML/Graphics.hpp>
class Frame
{
public:
Frame(sf::Texture& texture);
Frame(sf::Texture& texture, sf::IntRect rect);
sf::IntRect m_rect;
sf::Texture* m_texture;
};
#endif // FRAME_HPP_INCLUDED
#include "animation.hpp"
Animation::Animation()
{}
void Animation::push_frame(const Frame& frame)
{
m_anim.push_back(frame);
}
size_t Animation::size()
{
return m_anim.size();
}
Frame& Animation::operator [] (size_t N)
{
return m_anim[N];
}
Frame& Animation::getFrame(size_t N)
{
return m_anim[N];
}
#ifndef ANIMATION_HPP_INCLUDED
#define ANIMATION_HPP_INCLUDED
#include "frame.hpp"
class Animation
{
public:
Animation();
void push_frame(const Frame& frame);
size_t size();
Frame& getFrame(size_t N);
Frame& operator [] (size_t N);
private:
std::vector<Frame> m_anim;
};
#endif // ANIMATION_HPP_INCLUDED
#include "animated.hpp"
#include <iostream>
Animated::Animated() : sf::Sprite()
{
m_anim = NULL;
m_time = 0;
m_elapsedTime = m_time;
m_loop = true;
m_pause = true;
currentFrame = 0;
}
Animated::Animated(Animation anim, float time) : sf::Sprite()
{
m_anim = &anim;
m_time = time;
m_elapsedTime = m_time;
m_loop = true;
m_pause = true;
currentFrame = 0;
}
void Animated::anim(float elapsedTime)
{
if(!m_pause && m_anim != NULL)
{
m_elapsedTime -= elapsedTime;
if(m_elapsedTime <= 0.f)
{
m_elapsedTime = m_time;
if(currentFrame +1 < m_anim->size())
{
currentFrame++;
}
else
{
if(m_loop)
{
currentFrame = 0;
}
else
{
stop();
}
}
setFrame(currentFrame);
}
else
{
return;
}
}
}
void Animated::setFrame(int frame)
{
if(m_anim != NULL)
{
if(frame < m_anim->size())
{
std::cout << "ok";
if((m_anim->getFrame(frame).m_texture) != NULL)
setTexture(*m_anim->getFrame(frame).m_texture);
setTextureRect(m_anim->getFrame(frame).m_rect);
currentFrame = frame;
}
}
}
void Animated::setTime(float time)
{
m_time= time;
}
void Animated::setAnimation(Animation anim)
{
m_anim = &anim;
}
void Animated::setLoop(bool loop)
{
m_loop = loop;
}
void Animated::play()
{
m_pause = false;
}
void Animated::stop()
{
m_pause = true;
currentFrame = 0;
}
void Animated::pause()
{
m_pause = true;
}
#ifndef ANIMATED_HPP_INCLUDED
#define ANIMATED_HPP_INCLUDED
#include "animation.hpp"
class Animated : public sf::Sprite
{
public:
Animated();
Animated(Animation anim, float time);
void anim(float elapsedTime);
void setTime(float time);
void setAnimation(Animation anim);
void setLoop(bool loop);
void setFrame(int frame);
void play();
void stop();
void pause();
private:
Animation* m_anim;
float m_time;
float m_elapsedTime;
bool m_loop;
bool m_pause;
int currentFrame;
};
#endif // ANIMATED_HPP_INCLUDED
sf::Texture& Graphic_Manager::getTexture(const std::string& chemin)
{
std::map<std::string, sf::Texture>::iterator trouve = m_texture.find(chemin);
if(trouve == m_texture.end())
{
if(loadTexture(chemin))
{
return m_texture[chemin];
}
else
{
return m_texture[chemin];
}
}
else
{
return m_texture[chemin];
}
}
Et ça plante au moment du setTexture(*m_anim->getFrame(frame).m_texture); dans setFrame; j'ai ecran blanc, le programme ne répond plus, windows cherche une solution. Mais je ne vois pas le problème...
Et aussi qu'est ce qui remplace getFrameTime dans la sfml 2 ?
Merci