Here is my proper Game.Cpp update function(Not sure what happened and I should have noticed it).
void Game::Update()
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed || event.key.code == sf::Keyboard::Escape )
window.close();
}
unit.Update(event);
}
Unit.cpp
#include "Unit.h"
Unit::Unit(void)
{
fileName = "Content//Myrmidon.png";
position.x = 0; position.y = 0;
origin.x = 0;
origin.y = 0;
scale.x = 1;
scale.y = 1;
rotation = 0;
animation2D.UpdateAnimationInfo(0,0,32,32,2,250,true);
}
Unit::~Unit(void)
{
}
void Unit::Update(sf::Event &_event)
{
animation2D.Update();
}
void Unit::Draw(sf::RenderWindow &_window)
{
spriteBatch.Draw(_window,fileName,position,animation2D.SourceRectangle(),origin,scale,rotation);
}
Here is my animation.cpp and spriteBatch.cpp code since you may need to look at that.
Aimation
#include "Animation2D.h"
Animation2D::Animation2D(void)
{
}
Animation2D::~Animation2D(void)
{
}
void Animation2D::Update()
{
sourceRectangle.left = currentFrame * frameWidth;
sourceRectangle.top = currentRow * frameHeight;
sourceRectangle.width = frameWidth;
sourceRectangle.height = frameHeight;
if (animate)
{
time+=1;
if (time >= interval)
{
time = 0;
currentFrame++;
if (currentFrame > maxFrames)
currentFrame = 0;
}
}
else
{
time = 0;
}
}
void Animation2D::UpdateAnimationInfo(int _currentFrame, int _currentRow, int _frameWidth, int _frameHeight, int _maxFrames, float _interval, bool _animate)
{
currentFrame = _currentFrame;
currentRow = _currentRow;
frameWidth = _frameWidth;
frameHeight = _frameHeight;
maxFrames = _maxFrames;
interval = _interval;
animate = _animate;
}
sf::IntRect Animation2D::SourceRectangle()
{
return sourceRectangle;
}
SpriteBatch
#include "SpriteBatch.h"
SpriteBatch::SpriteBatch(void)
{
}
SpriteBatch::~SpriteBatch(void)
{
}
void SpriteBatch::Draw(sf::RenderWindow &_window ,std::string _fileName, sf::Vector2f _position,sf::IntRect _sourceRect,sf::Vector2f _origin, sf::Vector2f _scale, float _rotation)
{
texture.loadFromFile(_fileName);
sprite.setTexture(texture);
sprite.setPosition(_position.x,_position.y);
sprite.setTextureRect(_sourceRect);
sprite.setOrigin(_origin.x,_origin.y);
sprite.setScale(_scale.x,_scale.y);
sprite.setRotation(_rotation);
_window.draw(sprite);
}
Thanks for putting up with my stupidity.