Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F102489
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
11 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/animatedsprite.cpp b/animatedsprite.cpp
index 5f2fd09..8bba801 100644
--- a/animatedsprite.cpp
+++ b/animatedsprite.cpp
@@ -1,188 +1,191 @@
////////////////////////////////////////////////////////////
//
// Copyright (C) 2014 Maximilian Wagenbach (aka. Foaly) (foaly.f@web.de)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
+// This file has been created using Maximilian Wagenbach (aka. Foaly) sources
+// further modificated. Thank you Foaly.
+
#include "animatedsprite.h"
AnimatedSprite::AnimatedSprite(sf::Time frameTime, bool paused, bool looped) :
m_animation(NULL), m_frameTime(frameTime), m_currentFrame(0), m_isPaused(paused), m_isLooped(looped), m_texture(NULL)
{
}
void AnimatedSprite::setAnimation(const Animation& animation)
{
m_animation = &animation;
m_texture = m_animation->getSpriteSheet();
m_currentFrame = 0;
setFrame(m_currentFrame);
}
void AnimatedSprite::setFrameTime(sf::Time time)
{
m_frameTime = time;
}
void AnimatedSprite::play()
{
m_isPaused = false;
}
void AnimatedSprite::play(const Animation& animation)
{
if (getAnimation() != &animation)
setAnimation(animation);
play();
}
void AnimatedSprite::pause()
{
m_isPaused = true;
}
void AnimatedSprite::stop()
{
m_isPaused = true;
m_currentFrame = 0;
setFrame(m_currentFrame);
}
void AnimatedSprite::setLooped(bool looped)
{
m_isLooped = looped;
}
void AnimatedSprite::setColor(const sf::Color& color)
{
// Update the vertices' color
m_vertices[0].color = color;
m_vertices[1].color = color;
m_vertices[2].color = color;
m_vertices[3].color = color;
}
const Animation* AnimatedSprite::getAnimation() const
{
return m_animation;
}
sf::FloatRect AnimatedSprite::getLocalBounds() const
{
sf::IntRect rect = m_animation->getFrame(m_currentFrame);
float width = static_cast<float>(std::abs(rect.width));
float height = static_cast<float>(std::abs(rect.height));
return sf::FloatRect(0.f, 0.f, width, height);
}
sf::FloatRect AnimatedSprite::getGlobalBounds() const
{
return getTransform().transformRect(getLocalBounds());
}
bool AnimatedSprite::isLooped() const
{
return m_isLooped;
}
bool AnimatedSprite::isPlaying() const
{
return !m_isPaused;
}
sf::Time AnimatedSprite::getFrameTime() const
{
return m_frameTime;
}
void AnimatedSprite::setFrame(std::size_t newFrame, bool resetTime)
{
if (m_animation)
{
//calculate new vertex positions and texture coordiantes
sf::IntRect rect = m_animation->getFrame(newFrame);
m_vertices[0].position = sf::Vector2f(0.f, 0.f);
m_vertices[1].position = sf::Vector2f(0.f, static_cast<float>(rect.height));
m_vertices[2].position = sf::Vector2f(static_cast<float>(rect.width), static_cast<float>(rect.height));
m_vertices[3].position = sf::Vector2f(static_cast<float>(rect.width), 0.f);
float left = static_cast<float>(rect.left) + 0.0001f;
float right = left + static_cast<float>(rect.width);
float top = static_cast<float>(rect.top);
float bottom = top + static_cast<float>(rect.height);
m_vertices[0].texCoords = sf::Vector2f(left, top);
m_vertices[1].texCoords = sf::Vector2f(left, bottom);
m_vertices[2].texCoords = sf::Vector2f(right, bottom);
m_vertices[3].texCoords = sf::Vector2f(right, top);
}
if (resetTime)
m_currentTime = sf::Time::Zero;
}
void AnimatedSprite::update(sf::Time deltaTime)
{
// if not paused and we have a valid animation
if (!m_isPaused && m_animation)
{
// add delta time
m_currentTime += deltaTime;
// if current time is bigger then the frame time advance one frame
if (m_currentTime >= m_frameTime)
{
// reset time, but keep the remainder
m_currentTime = sf::microseconds(m_currentTime.asMicroseconds() % m_frameTime.asMicroseconds());
// get next Frame index
if (m_currentFrame == 0)
m_currentFrame++;
else
{
// animation has ended
m_currentFrame = 0; // reset to start
if (!m_isLooped)
{
m_isPaused = true;
}
}
// set the current frame, not reseting the time
setFrame(m_currentFrame, false);
}
}
}
void AnimatedSprite::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
if (m_animation && m_texture)
{
states.transform *= getTransform();
states.texture = m_texture;
target.draw(m_vertices, 4, sf::Quads, states);
}
}
diff --git a/animatedsprite.h b/animatedsprite.h
index c0f886b..3929a3a 100644
--- a/animatedsprite.h
+++ b/animatedsprite.h
@@ -1,72 +1,75 @@
////////////////////////////////////////////////////////////
//
// Copyright (C) 2014 Maximilian Wagenbach (aka. Foaly) (foaly.f@web.de)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
+// This file has been created using Maximilian Wagenbach (aka. Foaly) sources
+// further modificated. Thank you Foaly.
+
#ifndef ANIMATEDSPRITE_INCLUDE
#define ANIMATEDSPRITE_INCLUDE
#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/System/Time.hpp>
#include <SFML/Graphics/Drawable.hpp>
#include <SFML/Graphics/Transformable.hpp>
#include <SFML/System/Vector2.hpp>
#include "animation.h"
class AnimatedSprite : public sf::Drawable, public sf::Transformable
{
public:
explicit AnimatedSprite(sf::Time frameTime = sf::seconds(0.2f), bool paused = false, bool looped = true);
void update(sf::Time deltaTime);
void setAnimation(const Animation& animation);
void setFrameTime(sf::Time time);
void play();
void play(const Animation& animation);
void pause();
void stop();
void setLooped(bool looped);
void setColor(const sf::Color& color);
const Animation* getAnimation() const;
sf::FloatRect getLocalBounds() const;
sf::FloatRect getGlobalBounds() const;
bool isLooped() const;
bool isPlaying() const;
sf::Time getFrameTime() const;
void setFrame(std::size_t newFrame, bool resetTime = true);
const Animation* m_animation;
private:
sf::Time m_frameTime;
sf::Time m_currentTime;
std::size_t m_currentFrame;
bool m_isPaused;
bool m_isLooped;
const sf::Texture* m_texture;
sf::Vertex m_vertices[4];
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
};
#endif // ANIMATEDSPRITE_INCLUDE
diff --git a/animation.cpp b/animation.cpp
index 28f33cb..638580a 100644
--- a/animation.cpp
+++ b/animation.cpp
@@ -1,54 +1,57 @@
////////////////////////////////////////////////////////////
//
// Copyright (C) 2014 Maximilian Wagenbach (aka. Foaly) (foaly.f@web.de)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
+// This file has been created using Maximilian Wagenbach (aka. Foaly) sources
+// further modificated. Thank you Foaly.
+
#include "animation.h"
Animation::Animation() : m_texture(NULL)
{
}
void Animation::addFrame(sf::IntRect rect)
{
m_frames.push_back(rect);
}
void Animation::setSpriteSheet(const sf::Texture& texture)
{
m_texture = &texture;
}
const sf::Texture* Animation::getSpriteSheet() const
{
return m_texture;
}
std::size_t Animation::getSize() const
{
return m_frames.size();
}
const sf::IntRect& Animation::getFrame(std::size_t n) const
{
return m_frames[n];
}
diff --git a/animation.h b/animation.h
index f9ec7f2..317c098 100644
--- a/animation.h
+++ b/animation.h
@@ -1,47 +1,50 @@
////////////////////////////////////////////////////////////
//
// Copyright (C) 2014 Maximilian Wagenbach (aka. Foaly) (foaly.f@web.de)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
+// This file has been created using Maximilian Wagenbach (aka. Foaly) sources
+// further modificated. Thank you Foaly.
+
#ifndef ANIMATION_INCLUDE
#define ANIMATION_INCLUDE
#include <vector>
#include <SFML/Graphics/Rect.hpp>
#include <SFML/Graphics/Texture.hpp>
class Animation
{
public:
Animation();
void addFrame(sf::IntRect rect);
void setSpriteSheet(const sf::Texture& texture);
const sf::Texture* getSpriteSheet() const;
std::size_t getSize() const;
const sf::IntRect& getFrame(std::size_t n) const;
private:
std::vector<sf::IntRect> m_frames;
const sf::Texture* m_texture;
};
#endif // ANIMATION_INCLUDE
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Mon, Feb 2, 9:02 PM (2 d, 4 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55561
Default Alt Text
(11 KB)
Attached To
Mode
R82 deerportal
Attached
Detach File
Event Timeline
Log In to Comment