* Finish refactor

This commit is contained in:
Antidote 2014-02-26 21:47:39 -08:00
parent 9c64071bc0
commit 966efd3dd2
11 changed files with 974 additions and 367 deletions

View File

@ -1,9 +1,15 @@
#ifndef SSPRITE_HPP
#define SSPRITE_HPP
#include <unordered_map>
#ifndef LIBZELDA_USE_QT
#include <vector>
#include <string>
#else
#include <QObject>
#include <QPoint>
#include <QString>
#include <QList>
#endif
#include <Types.hpp>
namespace zelda
@ -11,20 +17,37 @@ namespace zelda
namespace Sakura
{
class SpriteFile;
class SpritePart;
class SpriteFrame;
#ifndef LIBZELDA_USE_QT
class Sprite
{
#else
class Sprite : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(qreal currentState READ currentState WRITE setCurrentState)
Q_PROPERTY(qreal stateCount READ stateCount CONSTANT)
#endif
public:
Sprite(SpriteFile* root);
Sprite(SpriteFile* root, const std::string& name);
virtual ~Sprite();
virtual void setPosition(const float x, const float y);
#ifndef LIBZELDA_USE_QT
virtual void setPosition(const Vector2Df& pos);
Vector2Df position() const;
virtual Vector2Df position() const;
void setName(const std::string& name);
std::string name() const;
#else
virtual void setPosition(const QPoint& pos);
virtual QPoint position() const;
void setName(const QString& name);
QString name() const;
#endif
void addStateId(int id);
@ -35,30 +58,73 @@ public:
*/
int stateId(int index) const;
void setStateIds(std::vector<int> ids);
#ifndef LIBZELDA_USE_QT
std::vector<int> stateIds() const;
#else
QList<int> stateIds() const;
#endif
Uint32 stateCount() const;
void setCurrentState(const Uint32 id);
void setCurrentState(Uint32 id);
Uint32 currentState() const;
void addPart(SpritePart* part);
SpritePart* part(const std::string& name);
void setParts(std::vector<SpritePart*> parts);
Uint32 partCount() const;
bool addFrame(SpriteFrame* Frame);
bool removeFrame(SpriteFrame* Frame);
SpriteFrame* Frame(Uint32 id);
void setFrame(Uint32 id);
#ifndef LIBZELDA_USE_QT
void setFrames(std::vector<SpriteFrame*> frames);
#else
void setFrames(QList<SpriteFrame*> frames);
#endif
Uint32 frameCount() const;
std::vector<SpritePart*> parts() const;
#ifndef LIBZELDA_USE_QT
std::vector<SpriteFrame*> frames() const;
#else
QList<SpriteFrame*> frames() const;
#endif
SpriteFile* container() const;
void setCurrentFrame(SpriteFrame* frame);
void setCurrentFrame(Uint32 id);
SpriteFrame* currentFrame() const;
void advanceFrame();
void retreatFrame();
void setRoot(SpriteFile* root);
SpriteFile* root() const;
#ifdef LIBZELDA_USE_QT
signals:
void frameChanged(SpriteFrame* frame);
void nameChanged(QString);
void stateChanged(quint32);
#endif
private:
SpriteFile* m_root;
#ifndef LIBZELDA_USE_QT
std::string m_name;
Vector2Df m_position;
std::vector<int> m_stateIds; //!< Stores the texture id's for each state.
std::vector<SpritePart*> m_parts;
std::vector<SpriteFrame*> m_frames;
#else
QString m_name;
QPoint m_position;
QList<int> m_stateIds;
QList<SpriteFrame*> m_frames;
#endif
Uint32 m_currentState;
Uint32 m_currentFrame;
};
} // Sakura
} // zelda
#ifdef LIBZELDA_USE_QT
Q_DECLARE_METATYPE(Uint32)
Q_DECLARE_METATYPE(zelda::Sakura::Sprite*)
#endif
#endif // SSPRITE_HPP

View File

@ -1,8 +1,17 @@
#ifndef SSPRITEFILE_HPP
#define SSPRITEFILE_HPP
#ifndef LIBZELDA_USE_QT
#include <vector>
#include <unordered_map>
#else
#include <QObject>
#include <QMap>
#include <QList>
#include <QSize>
#include <QPoint>
#endif
#include <string>
#include <Types.hpp>
@ -17,8 +26,14 @@ struct STexture
};
class Sprite;
#ifndef LIBZELDA_USE_QT
class SpriteFile
{
#else
class SpriteFile : public QObject
{
Q_OBJECT
#endif
public:
/*!
* \brief Major
@ -69,7 +84,19 @@ public:
* \param size
* \param origin
*/
#ifndef LIBZELDA_USE_QT
SpriteFile(const Vector2Di& size, const Vector2Df& origin);
#else
SpriteFile(const QSize& size, const QPoint& origin);
#endif
~SpriteFile();
#ifndef LIBZELDA_USE_QT
public:
#else
public slots:
#endif
/*!
* \brief setSize
@ -82,13 +109,21 @@ public:
* \brief setSize
* \param size
*/
#ifndef LIBZELDA_USE_QT
void setSize(const Vector2Di& size);
#else
void setSize(const QSize& size);
#endif
/*!
* \brief size
* \return
*/
#ifndef LIBZELDA_USE_QT
Vector2Di size() const;
#else
QSize size() const;
#endif
/*!
* \brief width
@ -113,13 +148,22 @@ public:
* \brief setOrigin
* \param origin
*/
void setOrigin(const Vector2Df& origin);
#ifndef LIBZELDA_USE_QT
void setOrigin(const Vector2Di& origin);
#else
void setOrigin(const QPoint& origin);
#endif
/*!
* \brief origin
* \return
*/
#ifndef LIBZELDA_USE_QT
Vector2Df origin() const;
#else
QPoint origin() const;
#endif
/*!
* \brief originX
@ -137,7 +181,7 @@ public:
* \brief addTexture
* \param texture
*/
void addTexture(STexture* texture);
bool addTexture(STexture* texture);
/*!
* \brief removeTexture
@ -152,30 +196,70 @@ public:
*/
STexture* texture(Uint32 id);
#ifndef LIBZELDA_USE_QT
std::vector<STexture*> textures() const;
#else
QList<STexture*> textures() const;
#endif
Uint32 textureCount() const;
/*!
* \brief setTextures
* \param textures
*/
#ifndef LIBZELDA_USE_QT
void setTextures(std::vector<STexture*> textures);
#else
void setTextures(QList<STexture*> textures);
#endif
void addSprite(Sprite* sprite);
#ifndef LIBZELDA_USE_QT
void removeSprite(const std::string& name);
#else
void removeSprite(const QString& name);
#endif
void removeSprite(Sprite* sprite);
#ifndef LIBZELDA_USE_QT
void setSprites(std::unordered_map<std::string, Sprite*> sprites);
#else
void setSprites(QMap<QString, Sprite*> sprites);
#endif
#ifndef LIBZELDA_USE_QT
Sprite* sprite(const std::string& name);
std::unordered_map<std::string, Sprite*> sprites() const;
#else
Sprite* sprite(const QString& name);
QMap<QString, Sprite*> sprites() const;
#endif
Uint32 spriteCount() const;
#ifdef LIBZELDA_USE_QT
signals:
void originChanged(QPoint);
void sizeChanged(QSize);
#endif
private:
#ifndef LIBZELDA_USE_QT
std::vector<STexture*> m_textures;
Vector2Di m_size;
Vector2Df m_origin;
std::unordered_map<std::string, Sprite*> m_sprites;
#else
QList<STexture*> m_textures;
QSize m_size;
QPoint m_origin;
QMap<QString, Sprite*> m_sprites;
#endif
};
} // Sakura
} // Zelda
#ifdef LIBZELDA_USE_QT
Q_DECLARE_METATYPE(zelda::Sakura::SpriteFile*)
Q_DECLARE_METATYPE(zelda::Sakura::STexture*)
#endif
#endif // SSPRITE_HPP

View File

@ -4,149 +4,81 @@
#include <Types.hpp>
#ifndef LIBZELDA_USE_QT
#include <vector>
#else
#include <QObject>
#include <QList>
#endif
namespace zelda
{
namespace Sakura
{
class Sprite;
class SpritePart;
#ifndef LIBZELDA_USE_QT
class SpriteFrame
{
#else
class SpriteFrame : public QObject
{
Q_OBJECT
Q_PROPERTY(qreal frameTime READ frameTime WRITE setFrameTime)
#endif
public:
/*!
* \brief SSpriteFrame
*/
SpriteFrame();
/*!
* \brief SSpriteFrame
* \param offX
* \param offY
* \param texX
* \param texY
* \param width
* \param height
* \param frameTime
* \param flippedH
* \param flippedV
*/
SpriteFrame(float offX, float offY, float texX, float texY, Uint32 width, Uint32 height, float frameTime, bool flippedH = false, bool flippedV = false);
/*!
* \brief SSpriteFrame
* \param frameOff
* \param texOff
* \param size
* \param frameTime
* \param flippedH
* \param flippedV
*/
SpriteFrame(const Vector2Df& frameOff, const Vector2Df& texOff, const Vector2Di& size, float frameTime, bool flippedH = false, bool flippedV = false);
/*!
* \brief setOffset
* \param x
* \param y
*/
void setOffset(float x, float y);
/*!
* \brief setOffset
* \param offset
*/
void setOffset(const Vector2Df& offset);
/*!
* \brief offset
* \return
*/
Vector2Df offset() const;
/*!
* \brief setTextureOffset
* \param x
* \param y
*/
void setTextureOffset(float x, float y);
/*!
* \brief setTextureOffset
* \param texOff
*/
void setTextureOffset(const Vector2Df& texOff);
/*!
* \brief textureOffset
* \return
*/
Vector2Df textureOffset() const;
/*!
* \brief setSize
* \param width
* \param height
*/
void setSize(Uint32 width, Uint32 height);
/*!
* \brief setSize
* \param size
*/
void setSize(const Vector2Di& size);
/*!
* \brief size
* \return
*/
Vector2Di size() const;
/*!
* \brief setFlippedHorizontally
* \param val
*/
void setFlippedHorizontally(const bool val);
/*!
* \brief flippedHorizontally
* \return
*/
bool flippedHorizontally() const;
/*!
* \brief setFlippedVertically
* \param val
*/
void setFlippedVertically(const bool val);
/*!
* \brief flippedVertically
* \return
*/
bool flippedVertically() const;
SpriteFrame(Sprite* root);
/*!
* \brief setFrameTime
* \param frameTime
*/
void setFrameTime(float frameTime);
/*!
* \brief frameTime
* \return
*/
float frameTime() const;
#ifndef LIBZELDA_USE_QT
void setParts(std::vector<SpritePart*> parts);
std::vector<SpritePart*> parts() const;
#else
void setParts(QList<SpritePart*> parts);
QList<SpritePart*> parts() const;
#endif
Uint32 partCount() const;
void setRoot(Sprite* root);
Sprite* root() const;
#ifdef LIBZELDA_USE_QT
signals:
void frameTimeChanged(float);
#endif
private:
Vector2Df m_offset;
Vector2Df m_textureOffset;
Vector2Di m_size;
Sprite* m_root;
float m_frameTime;
bool m_flippedH;
bool m_flippedV;
#ifndef LIBZELDA_USE_QT
std::vector<SpritePart*> m_parts;
#else
QList<SpritePart*> m_parts;
#endif
};
} // Sakura
} // zelda
#endif // SSpRITEFRAME_HPP
#ifdef LIBZELDA_USE_QT
Q_DECLARE_METATYPE(zelda::Sakura::SpriteFrame*);
#endif
#endif // SSPRITEFRAME_HPP

View File

@ -2,7 +2,15 @@
#define SSPRITEPART_HPP
#include <Types.hpp>
#ifndef LIBZELDA_USE_QT
# include <vector>
#else
# include <QObject>
# include <QPoint>
# include <QSize>
# include <QString>
#endif
#include <string>
#include <Types.hpp>
@ -10,46 +18,184 @@ namespace zelda
{
namespace Sakura
{
class Sprite;
class SpriteFrame;
#ifndef LIBZELDA_USE_QT
class SpritePart
{
#else
class SpritePart : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(bool hasCollision READ hasCollision WRITE setCollision)
Q_PROPERTY(bool flippedHorizontally READ flippedHorizontally WRITE setFlippedHorizontally)
Q_PROPERTY(bool flippedVertically READ flippedVertically WRITE setFlippedVertically)
Q_PROPERTY(QPoint offset READ offset WRITE setOffset)
Q_PROPERTY(QPoint textureOffset READ textureOffset WRITE setTextureOffset)
Q_PROPERTY(QSize size READ size WRITE setSize)
#endif
public:
SpritePart(Sprite* root);
SpritePart(Sprite* root, const std::string& name, bool hasCollision = false);
SpritePart(SpriteFrame* root);
SpritePart(SpriteFrame* root, const std::string& name, bool hasCollision = false);
virtual ~SpritePart();
#ifndef LIBZELDA_USE_QT
void setName(const std::string& name);
std::string name() const;
#else
void setName(const QString& name);
QString name() const;
#endif
void setCollision(bool col);
bool hasCollision() const;
void addFrame(SpriteFrame* frame);
void advanceFrame();
void retreatFrame();
SpriteFrame* frame(int id);
void setFrames(std::vector<SpriteFrame*> frames);
SpriteFrame* currentFrame();
int currentFrameID();
std::vector<SpriteFrame*> frames() const;
Uint32 frameCount() const;
/*!
* \brief setOffset
* \param x
* \param y
*/
void setOffset(float x, float y);
/*!
* \brief setOffset
* \param offset
*/
#ifndef LIBZELDA_USE_QT
void setOffset(const Vector2Df& offset);
#else
void setOffset(const QPoint& offset);
#endif
/*!
* \brief offset
* \return
*/
#ifndef LIBZELDA_USE_QT
Vector2Df offset() const;
#else
QPoint offset() const;
#endif
/*!
* \brief setTextureOffset
* \param x
* \param y
*/
void setTextureOffset(float x, float y);
/*!
* \brief setTextureOffset
* \param texOff
*/
#ifndef LIBZELDA_USE_QT
void setTextureOffset(const Vector2Df& offset);
#else
void setTextureOffset(const QPoint& offset);
#endif
/*!
* \brief textureOffset
* \return
*/
#ifndef LIBZELDA_USE_QT
Vector2Df textureOffset() const;
#else
QPoint textureOffset() const;
#endif
/*!
* \brief setSize
* \param width
* \param height
*/
void setSize(Uint32 width, Uint32 height);
/*!
* \brief setSize
* \param size
*/
#ifndef LIBZELDA_USE_QT
void setSize(const Vector2Di& size);
#else
void setSize(const QSize& size);
#endif
/*!
* \brief size
* \return
*/
#ifndef LIBZELDA_USE_QT
Vector2Di size() const;
#else
QSize size() const;
#endif
/*!
* \brief setFlippedHorizontally
* \param val
*/
void setFlippedHorizontally(const bool val);
/*!
* \brief flippedHorizontally
* \return
*/
bool flippedHorizontally() const;
/*!
* \brief setFlippedVertically
* \param val
*/
void setFlippedVertically(const bool val);
/*!
* \brief flippedVertically
* \return
*/
bool flippedVertically() const;
void setRoot(SpriteFrame* root);
SpriteFrame* root() const;
#ifdef LIBZELDA_USE_QT
signals:
void nameChanged(QString);
void orientationChanged(bool,bool);
void offsetChanged(QPoint);
void textureOffsetChanged(QPoint);
void sizeChanged(QSize);
void collisionChanged(bool);
#endif
void setRoot(Sprite* root);
private:
Sprite* m_root;
SpriteFrame* m_root;
#ifndef LIBZELDA_USE_QT
std::string m_name;
#else
QString m_name;
#endif
bool m_hasCollision;
SpriteFrame* m_currentFrame;
#ifndef LIBZELDA_USE_QT
Vector2Df m_offset;
Vector2Df m_textureOffset;
Vector2Di m_size;
#else
QPoint m_offset;
QPoint m_textureOffset;
QSize m_size;
#endif
bool m_flippedH;
bool m_flippedV;
Uint32 m_frameIndex;
// The collection of frames for this part
std::vector<SpriteFrame*> m_frames;
};
}
}
#ifdef LIBZELDA_USE_QT
Q_DECLARE_METATYPE(zelda::Sakura::SpritePart*)
#endif
#endif // SSpRITEPART_HPP
#endif // SSPRITEPART_HPP

View File

@ -2,6 +2,13 @@ CONFIG += staticlib
TEMPLATE= lib
DESTDIR = ./lib
# Uncomment this if you wish to use Qt with libZelda
DEFINES += LIBZELDA_USE_QT
contains(DEFINES, LIBZELDA_USE_QT){
QT += core
}
CONFIG(debug, debug|release){
DEFINES += DEBUG
TARGET=zelda-d

View File

@ -1,51 +1,94 @@
#include "Sprite.hpp"
#include "SpritePart.hpp"
#include "SpriteFrame.hpp"
#include "SpriteFile.hpp"
#include <algorithm>
#ifdef LIBZELDA_USE_QT
#include <QVector>
#endif
namespace zelda
{
namespace Sakura
{
Sprite::Sprite(SpriteFile* root)
: m_root(root),
m_currentState(0)
m_currentState(0),
m_currentFrame(0)
{
}
Sprite::Sprite(SpriteFile* root, const std::string& name)
: m_root(root),
m_name(name),
m_currentState(0)
{
#ifdef LIBZELDA_USE_QT
m_name = QString::fromStdString(name);
#else
m_name = name;
#endif
}
Sprite::~Sprite()
{
#ifndef LIBZELDA_USE_QT
for (SpriteFrame* frame : m_frames)
#else
foreach(SpriteFrame* frame, m_frames)
#endif
{
delete frame;
frame = NULL;
}
m_frames.clear();
}
void Sprite::setPosition(const float x, const float y)
{
#ifndef LIBZELDA_USE_QT
setPosition(Vector2Df(x, y));
#else
setPosition(QPoint(x, y));
#endif
}
#ifndef LIBZELDA_USE_QT
void Sprite::setPosition(const Vector2Df& pos)
#else
void Sprite::setPosition(const QPoint& pos)
#endif
{
m_position = pos;
}
#ifndef LIBZELDA_USE_QT
Vector2Df Sprite::position() const
#else
QPoint Sprite::position() const
#endif
{
return m_position;
}
#ifndef LIBZELDA_USE_QT
void Sprite::setName(const std::string& name)
#else
void Sprite::setName(const QString& name)
#endif
{
m_name = name;
#ifdef LIBZELDA_USE_QT
emit nameChanged(name);
#endif
}
#ifndef LIBZELDA_USE_QT
std::string Sprite::name() const
#else
QString Sprite::name() const
#endif
{
return m_name;
}
@ -72,10 +115,18 @@ void Sprite::setStateIds(std::vector<int> ids)
if (ids.size() == 0)
return;
#ifndef LIBZELDA_USE_QT
m_stateIds = ids;
#else
m_stateIds = QList<int>::fromVector(QVector<int>::fromStdVector(ids));
#endif
}
#ifndef LIBZELDA_USE_QT
std::vector<int> Sprite::stateIds() const
#else
QList<int> Sprite::stateIds() const
#endif
{
return m_stateIds;
}
@ -87,50 +138,153 @@ Uint32 Sprite::stateCount() const
void Sprite::setCurrentState(const Uint32 id)
{
if (id >= m_stateIds.size())
return;
m_currentState = id;
#ifdef LIBZELDA_USE_QT
emit stateChanged(id);
#endif
}
void Sprite::addPart(SpritePart* part)
Uint32 Sprite::currentState() const
{
for (SpritePart* tmp : m_parts)
return m_currentState;
}
bool Sprite::addFrame(SpriteFrame* part)
{
if (m_frames.size() > 65536)
return false;
for (SpriteFrame* tmp : m_frames)
{
if (tmp == part)
return false;
}
m_frames.push_back(part);
return true;
}
bool Sprite::removeFrame(SpriteFrame* frame)
{
#ifndef LIBZELDA_USE_QT
std::vector<SpriteFrame*>::iterator iter = std::find(m_frames.begin(), m_frames.end(), frame);
if (iter != m_frames.end())
{
m_frames.erase(iter);
return true;
}
#else
if (m_frames.removeOne(frame))
return true;
#endif
return false;
}
void Sprite::setFrame(Uint32 id)
{
if (id > m_frames.size())
return;
}
m_parts.push_back(part);
}
void Sprite::setParts(std::vector<SpritePart*> parts)
#ifndef LIBZELDA_USE_QT
void Sprite::setFrames(std::vector<SpriteFrame*> frames)
{
if (parts.size() == 0)
if (frames.size() == 0)
return;
if (m_parts.size() > 0)
if (m_frames.size() > 0)
{
for (SpritePart* part : m_parts)
for (SpriteFrame* frame : m_frames)
{
delete part;
part = NULL;
delete frame;
frame = NULL;
}
m_parts.clear();
m_frames.clear();
}
m_frames = frames;
}
#else
void Sprite::setFrames(QList<SpriteFrame*> frames)
{
m_frames.clear();
m_frames = frames;
}
#endif
Uint32 Sprite::frameCount() const
{
return m_frames.size();
}
m_parts = parts;
}
Uint32 Sprite::partCount() const
#ifndef LIBZELDA_USE_QT
std::vector<SpritePart*> Sprite::frames() const
#else
QList<SpriteFrame*> Sprite::frames() const
#endif
{
return m_parts.size();
}
std::vector<SpritePart*> Sprite::parts() const
{
return m_parts;
return m_frames;
}
SpriteFile* Sprite::container() const
{
return m_root;
}
void Sprite::setCurrentFrame(SpriteFrame* frame)
{
Uint32 id = 0;
for (SpriteFrame* tmpFrame : m_frames)
{
if (tmpFrame == frame)
{
setCurrentFrame(id);
return;
}
id++;
}
}
void Sprite::setCurrentFrame(Uint32 id)
{
if (id >= m_frames.size())
return;
m_currentFrame = id;
#ifdef LIBZELDA_USE_QT
emit frameChanged(currentFrame());
#endif
}
SpriteFrame* Sprite::currentFrame() const
{
return m_frames[m_currentFrame];
}
void Sprite::advanceFrame()
{
m_currentFrame++;
if (m_currentFrame >= m_frames.size())
m_currentFrame = m_frames.size() - 1;
}
void Sprite::retreatFrame()
{
if (m_currentFrame == 0)
return;
m_currentFrame--;
}
void Sprite::setRoot(SpriteFile* root)
{
m_root = root;
}
SpriteFile* Sprite::root() const
{
return m_root;
}
}
}

View File

@ -1,15 +1,15 @@
#include "SpriteFile.hpp"
#include "Sprite.hpp"
#include "utility.hpp"
#include <iostream>
namespace zelda
{
namespace Sakura
{
const Uint32 SpriteFile::Major = 1;
const Uint32 SpriteFile::Minor = 0;
const Uint32 SpriteFile::Revision = 1;
const Uint32 SpriteFile::Revision = 2;
const Uint32 SpriteFile::Build = 0;
const Uint32 SpriteFile::Version = Major | (Minor << 8) | (Revision << 16) | (Build << 24);
@ -17,75 +17,141 @@ const Uint32 SpriteFile::Magic = 'S' | ('P' << 8) | ('R' << 16) | ('S' << 24);
SpriteFile::SpriteFile()
: m_size(Vector2Di(1, 1))
{
}
SpriteFile::SpriteFile(Uint32 width, Uint32 height, float originX, float originY)
: m_size(Vector2Di(width, height)),
m_origin(Vector2Df(originX, originY))
: m_size(width, height),
m_origin(originX, originY)
{
}
#ifndef LIBZELDA_USE_QT
SpriteFile::SpriteFile(const Vector2Di& size, const Vector2Df& origin)
#else
SpriteFile::SpriteFile(const QSize& size, const QPoint& origin)
#endif
: m_size(size),
m_origin(origin)
{
}
SpriteFile::~SpriteFile()
{
#ifndef LIBZELDA_USE_QT
for (std::pair<std::string, Sprite*> sprite : m_sprites)
{
delete sprite.second;
sprite.second = NULL;
}
#endif
m_sprites.clear();
}
void SpriteFile::setSize(Uint32 width, Uint32 height)
{
#ifndef LIBZELDA_USE_QT
setSize(Vector2Di(width, height));
#else
setSize(QSize(width, height));
#endif
}
#ifndef LIBZELDA_USE_QT
void SpriteFile::setSize(const Vector2Di& size)
#else
void SpriteFile::setSize(const QSize& size)
#endif
{
m_size = size;
#ifdef LIBZELDA_USE_QT
emit sizeChanged(size);
#endif
}
#ifndef LIBZELDA_USE_QT
Vector2Di SpriteFile::size() const
#else
QSize SpriteFile::size() const
#endif
{
return m_size;
}
Uint32 SpriteFile::width() const
{
#ifndef LIBZELDA_USE_QT
return m_size.x;
#else
return m_size.width();
#endif
}
Uint32 SpriteFile::height() const
{
#ifndef LIBZELDA_USE_QT
return m_size.y;
#else
return m_size.height();
#endif
}
void SpriteFile::setOrigin(const float x, const float y)
{
#ifndef LIBZELDA_USE_QT
setOrigin(Vector2Df(x, y));
#else
setOrigin(QPoint(x, y));
#endif
}
#ifndef LIBZELDA_USE_QT
void SpriteFile::setOrigin(const Vector2Df& origin)
#else
void SpriteFile::setOrigin(const QPoint& origin)
#endif
{
m_origin = origin;
#ifdef LIBZELDA_USE_QT
emit originChanged(origin);
#endif
}
#ifndef LIBZELDA_USE_QT
Vector2Df SpriteFile::origin() const
#else
QPoint SpriteFile::origin() const
#endif
{
return m_origin;
}
float SpriteFile::originX() const
{
#ifndef LIBZELDA_USE_QT
return m_origin.x;
#else
return m_origin.x();
#endif
}
float SpriteFile::originY() const
{
#ifndef LIBZELDA_USE_QT
return m_origin.y;
#else
return m_origin.y();
#endif
}
void SpriteFile::addTexture(STexture* texture)
bool SpriteFile::addTexture(STexture* texture)
{
if (m_textures.size() >= 65536)
return false;
m_textures.push_back(texture);
return true;
}
void SpriteFile::removeTexture(int id)
@ -106,7 +172,11 @@ STexture* SpriteFile::texture(Uint32 id)
return m_textures[id];
}
#ifndef LIBZELDA_USE_QT
std::vector<STexture*> SpriteFile::textures() const
#else
QList<STexture*> SpriteFile::textures() const
#endif
{
return m_textures;
}
@ -118,12 +188,42 @@ Uint32 SpriteFile::textureCount() const
void SpriteFile::addSprite(Sprite* sprite)
{
if (m_sprites.find(sprite->name()) != m_sprites.end())
#ifndef LIBZELDA_USE_QT
std::string name(sprite->name());
zelda::utility::tolower(name);
if (m_sprites.find(name) != m_sprites.end())
return;
#else
QString name = sprite->name().toLower();
if (m_sprites.contains(name))
return;
#endif
m_sprites[sprite->name()] = sprite;
m_sprites[name] = sprite;
}
#ifndef LIBZELDA_USE_QT
void SpriteFile::removeSprite(const std::string& name)
{
std::string tmpName(name);
zelda::utility::tolower(tmpName);
std::unordered_map<std::string, Sprite*>::iterator iterator = m_sprites.find(tmpName);
if (iterator != m_sprites.end())
m_sprites.erase(iterator);
}
#else
void SpriteFile::removeSprite(const QString& name)
{
m_sprites.remove(name.toLower());
}
#endif
void SpriteFile::removeSprite(Sprite* sprite)
{
removeSprite(sprite->name());
}
#ifndef LIBZELDA_USE_QT
void SpriteFile::setSprites(std::unordered_map<std::string, Sprite*> sprites)
{
if (sprites.size() == 0)
@ -140,16 +240,41 @@ void SpriteFile::setSprites(std::unordered_map<std::string, Sprite*> sprites)
m_sprites = sprites;
}
#else
void SpriteFile::setSprites(QMap<QString, Sprite *> sprites)
{
if (sprites.size() == 0)
return;
m_sprites.clear();
m_sprites = sprites;
}
#endif
#ifndef LIBZELDA_USE_QT
Sprite* SpriteFile::sprite(const std::string& name)
{
if (m_sprites.find(name) == m_sprites.end())
std::string nameLow(name);
zelda::utility::tolower(nameLow);
if (m_sprites.find(nameLow) == m_sprites.end())
return NULL;
return m_sprites[name];
return m_sprites[nameLow];
}
#else
Sprite* SpriteFile::sprite(const QString& name)
{
if (!m_sprites.contains(name.toLower()))
return NULL;
return m_sprites[name.toLower()];
}
#endif
#ifndef LIBZELDA_USE_QT
std::unordered_map<std::string, Sprite*> SpriteFile::sprites() const
#else
QMap<QString, Sprite*> SpriteFile::sprites() const
#endif
{
return m_sprites;
}
@ -159,6 +284,7 @@ Uint32 SpriteFile::spriteCount() const
return m_sprites.size();
}
#ifndef LIBZELDA_USE_QT
void SpriteFile::setTextures(std::vector<STexture*> textures)
{
if (textures.size() == 0)
@ -176,5 +302,15 @@ void SpriteFile::setTextures(std::vector<STexture*> textures)
m_textures = textures;
}
#else
void SpriteFile::setTextures(QList<STexture *> textures)
{
if (textures.size() == 0)
return;
m_textures.clear();
m_textures = textures;
}
#endif
} // Sakura
} // zelda

View File

@ -5,6 +5,7 @@
#include "SpriteFrame.hpp"
#include "InvalidOperationException.hpp"
#include "IOException.hpp"
#include "utility.hpp"
namespace zelda
{
@ -63,7 +64,11 @@ Sakura::SpriteFile* SpriteFileReader::readFile()
// it will be slow as hell, so we store them in a vector locally
// then give that vector the the container, this bypasses the de-reference
// for each texture
#ifndef LIBZELDA_USE_QT
std::vector<Sakura::STexture*> textures;
#else
QList<Sakura::STexture*> textures;
#endif
for (Uint16 i = 0; i < textureCount; i++)
{
@ -81,13 +86,22 @@ Sakura::SpriteFile* SpriteFileReader::readFile()
// Normally this isn't a problem, but someone may decide to copy and paste a sprite
// and forget to change the name, that needs to be handled, but it's outside the scope
// of this reader.
#ifndef LIBZELDA_USE_QT
std::unordered_map <std::string, Sakura::Sprite*> sprites;
#else
QMap<QString, Sakura::Sprite*> sprites;
#endif
for (Uint16 i = 0; i < spriteCount; i++)
{
Sakura::Sprite* sprite = new Sakura::Sprite(ret);
sprite->setName(base::readString());
Uint16 partCount = base::readUInt16();
#ifndef LIBZELDA_USE_QT
std::string name = base::readString();
#else
QString name = QString::fromStdString(base::readString());
#endif
sprite->setName(name);
Uint16 frameCount = base::readUInt16();
Uint16 stateCount = base::readUInt16();
// Each state id corresponds to a texture held in the parent class
@ -105,37 +119,67 @@ Sakura::SpriteFile* SpriteFileReader::readFile()
// and the storage footprint, while Sakura supports packs and zips
// it's still a bad idea to have a metric ton of texture resources
// littering the place
std::vector<Sakura::SpritePart*> parts;
for (Uint8 j = 0; j < partCount; j++)
{
Sakura::SpritePart* part = new Sakura::SpritePart(sprite);
part->setName(base::readString());
part->setCollision(base::readBool());
Uint32 frameCount = base::readUInt32();
#ifndef LIBZELDA_USE_QT
std::vector<Sakura::SpriteFrame*> frames;
#else
QList<Sakura::SpriteFrame*> frames;
#endif
for (Uint32 k = 0; k < frameCount; k++)
{
Sakura::SpriteFrame* frame = new Sakura::SpriteFrame(sprite);
frame->setFrameTime(base::readFloat());
Uint16 partCount = base::readUInt16();
#ifndef LIBZELDA_USE_QT
std::vector<Sakura::SpritePart*> parts;
#else
QList<Sakura::SpritePart*> parts;
#endif
for (Uint8 j = 0; j < partCount; j++)
{
Sakura::SpritePart* part = new Sakura::SpritePart(frame);
#ifndef LIBZELDA_USE_QT
std::string name = base::readString();
#else
QString name = QString::fromStdString(base::readString());
#endif
part->setName(name);
part->setCollision(base::readBool());
float xOff = base::readFloat();
float yOff = base::readFloat();
part->setOffset(xOff, yOff);
float texXOff = base::readFloat();
float texYOff = base::readFloat();
part->setTextureOffset(texXOff, texYOff);
Uint32 width = base::readUInt32();
Uint32 height = base::readUInt32();
float frameTime = base::readFloat();
part->setSize(width, height);
bool flippedH = base::readBool();
part->setFlippedHorizontally(flippedH);
bool flippedV = base::readBool();
part->setFlippedVertically(flippedV);
frames.push_back(new Sakura::SpriteFrame(xOff, yOff, texXOff, texYOff, width, height, frameTime, flippedH, flippedV));
}
part->setFrames(frames);
parts.push_back(part);
}
frame->setParts(parts);
frames.push_back(frame);
}
sprite->setParts(parts);
sprite->setFrames(frames);
#ifndef LIBZELDA_USE_QT
if (sprite->name() != std::string())
sprites[sprite->name()] = sprite;
{
std::string nameLow(sprite->name());
zelda::utility::tolower(nameLow);
sprites[nameLow] = sprite;
}
#else
if (!sprite->name().isEmpty())
sprites[sprite->name().toLower()] = sprite;
#endif
else
throw zelda::error::IOException("SSpriteFileReader::readFile -> Sprite names cannot be empty");
}

View File

@ -42,34 +42,52 @@ void SpriteFileWriter::writeFile(Sakura::SpriteFile* file)
base::writeBool(texture->Preload);
}
#ifndef LIBZELDA_USE_QT
for (std::pair<std::string, Sakura::Sprite*> spritePair : file->sprites())
{
Sakura::Sprite* sprite = spritePair.second;
base::writeString(sprite->name());
base::writeUInt16(sprite->partCount());
#else
foreach(Sakura::Sprite* sprite, file->sprites().values())
{
base::writeString(sprite->name().toStdString());
#endif
base::writeUInt16(sprite->frameCount());
base::writeUInt16(sprite->stateCount());
for (int id : sprite->stateIds())
base::writeUInt16(id);
for (Sakura::SpritePart* part : sprite->parts())
for (Sakura::SpriteFrame* frame : sprite->frames())
{
base::writeString(part->name());
base::writeBool(part->hasCollision());
base::writeUInt32(part->frameCount());
for (Sakura::SpriteFrame* frame : part->frames())
{
base::writeFloat(frame->offset().x);
base::writeFloat(frame->offset().y);
base::writeFloat(frame->textureOffset().x);
base::writeFloat(frame->textureOffset().y);
base::writeUInt32(frame->size().x);
base::writeUInt32(frame->size().y);
base::writeFloat(frame->frameTime());
base::writeBool(frame->flippedHorizontally());
base::writeBool(frame->flippedVertically());
base::writeUInt16(frame->partCount());
for (Sakura::SpritePart* part: frame->parts())
{
#ifndef LIBZELDA_USE_QT
base::writeString(part->name());
#else
base::writeString(part->name().toStdString());
#endif
base::writeBool(part->hasCollision());
#ifndef LIBZELDA_USE_QT
base::writeFloat(part->offset().x);
base::writeFloat(part->offset().y);
base::writeFloat(part->textureOffset().x);
base::writeFloat(part->textureOffset().y);
base::writeUInt32(part->size().x);
base::writeUInt32(part->size().y);
#else
base::writeFloat(part->offset().x());
base::writeFloat(part->offset().y());
base::writeFloat(part->textureOffset().x());
base::writeFloat(part->textureOffset().y());
base::writeUInt32(part->size().width());
base::writeUInt32(part->size().height());
#endif
base::writeBool(part->flippedHorizontally());
base::writeBool(part->flippedVertically());
}
}
}

View File

@ -1,5 +1,6 @@
#include "SpriteFrame.hpp"
#include "SpritePart.hpp"
#include "Sprite.hpp"
namespace zelda
{
@ -10,94 +11,40 @@ SpriteFrame::SpriteFrame()
{
}
SpriteFrame::SpriteFrame(float offX, float offY, float texX, float texY, Uint32 width, Uint32 height, float frameTime, bool flippedH, bool flippedV)
: m_offset(Vector2Df(offX, offY)),
m_textureOffset(Vector2Df(texX, texY)),
m_size(Vector2Di(width, height)),
m_frameTime(frameTime),
m_flippedH(flippedH),
m_flippedV(flippedV)
SpriteFrame::SpriteFrame(Sprite* root)
: m_root(root)
{
}
SpriteFrame::SpriteFrame(const Vector2Df& frameOff, const Vector2Df& texOff, const Vector2Di& size, float frameTime, bool flippedH, bool flippedV)
: m_offset(frameOff),
m_textureOffset(texOff),
m_size(size),
m_frameTime(frameTime),
m_flippedH(flippedH),
m_flippedV(flippedV)
{
}
void SpriteFrame::setOffset(float x, float y)
{
setOffset(Vector2Df(x, y));
}
void SpriteFrame::setOffset(const Vector2Df& offset)
{
m_offset = offset;
}
Vector2Df SpriteFrame::offset() const
{
return m_offset;
}
void SpriteFrame::setTextureOffset(float x, float y)
{
setTextureOffset(Vector2Df(x, y));
}
void SpriteFrame::setTextureOffset(const Vector2Df& texOff)
{
m_textureOffset = texOff;
}
Vector2Df SpriteFrame::textureOffset() const
{
return m_textureOffset;
}
void SpriteFrame::setSize(Uint32 width, Uint32 height)
{
setSize(Vector2Di(width, height));
}
void SpriteFrame::setSize(const Vector2Di& size)
{
m_size = size;
}
Vector2Di SpriteFrame::size() const
{
return m_size;
}
void SpriteFrame::setFlippedHorizontally(const bool val)
{
m_flippedH = val;
}
bool SpriteFrame::flippedHorizontally() const
{
return m_flippedH;
}
void SpriteFrame::setFlippedVertically(const bool val)
{
m_flippedV = val;
}
bool SpriteFrame::flippedVertically() const
{
return m_flippedV;
}
void SpriteFrame::setFrameTime(float frameTime)
{
m_frameTime = frameTime;
#ifdef LIBZELDA_USE_QT
emit frameTimeChanged(frameTime);
#endif
}
#ifndef LIBZELDA_USE_QT
void SpriteFrame::setParts(std::vector<SpritePart*> parts)
#else
void SpriteFrame::setParts(QList<SpritePart*> parts)
#endif
{
m_parts = parts;
}
#ifndef LIBZELDA_USE_QT
std::vector<SpritePart*> SpriteFrame::parts() const
#else
QList<SpritePart*> SpriteFrame::parts() const
#endif
{
return m_parts;
}
Uint32 SpriteFrame::partCount() const
{
return m_parts.size();
}
float SpriteFrame::frameTime() const
@ -105,5 +52,16 @@ float SpriteFrame::frameTime() const
return m_frameTime;
}
void SpriteFrame::setRoot(Sprite* root)
{
root->removeFrame(this);
m_root = root;
}
Sprite* SpriteFrame::root() const
{
return m_root;
}
} // Sakura
} // zelda

View File

@ -1,40 +1,51 @@
#include "SpritePart.hpp"
#include "SpriteFrame.hpp"
#include "SpritePart.hpp"
#include "Sprite.hpp"
#include <algorithm>
namespace zelda
{
namespace Sakura
{
SpritePart::SpritePart(Sprite* root)
SpritePart::SpritePart(SpriteFrame* root)
: m_root(root),
m_hasCollision(false),
m_currentFrame(NULL),
m_frameIndex(0)
m_hasCollision(false)
{
}
SpritePart::SpritePart(Sprite* root, const std::string& name, bool hasCollision)
SpritePart::SpritePart(SpriteFrame* root, const std::string& name, bool hasCollision)
: m_root(root),
m_name(name),
m_hasCollision(hasCollision),
m_currentFrame(NULL),
m_frameIndex(0)
m_hasCollision(hasCollision)
{
#ifdef LIBZELDA_USE_QT
m_name = QString::fromStdString(name);
#else
m_name = name;
#endif
}
SpritePart::~SpritePart()
{
}
#ifndef LIBZELDA_USE_QT
void SpritePart::setName(const std::string& name)
#else
void SpritePart::setName(const QString& name)
#endif
{
m_name = name;
#ifdef LIBZELDA_USE_QT
emit nameChanged(name);
#endif
}
#ifndef LIBZELDA_USE_QT
std::string SpritePart::name() const
#else
QString SpritePart::name() const
#endif
{
return m_name;
}
@ -42,6 +53,9 @@ std::string SpritePart::name() const
void SpritePart::setCollision(bool col)
{
m_hasCollision = col;
#ifdef LIBZELDA_USE_QT
emit collisionChanged(col);
#endif
}
bool SpritePart::hasCollision() const
@ -49,77 +63,125 @@ bool SpritePart::hasCollision() const
return m_hasCollision;
}
void SpritePart::addFrame(SpriteFrame* frame)
void SpritePart::setOffset(float x, float y)
{
m_frames.push_back(frame);
#ifndef LIBZELDA_USE_QT
setOffset(Vector2Df(x, y));
#else
setOffset(QPoint(x, y));
#endif
}
void SpritePart::advanceFrame()
#ifndef LIBZELDA_USE_QT
void SpritePart::setOffset(const Vector2Df& offset)
#else
void SpritePart::setOffset(const QPoint& offset)
#endif
{
if (m_frameIndex < m_frames.size() - 1)
{
m_frameIndex++;
m_currentFrame = m_frames[m_frameIndex];
}
m_offset = offset;
#ifdef LIBZELDA_USE_QT
emit offsetChanged(offset);
#endif
}
void SpritePart::retreatFrame()
#ifndef LIBZELDA_USE_QT
Vector2Df SpritePart::offset() const
#else
QPoint SpritePart::offset() const
#endif
{
if (m_frameIndex > 0)
return m_offset;
}
void SpritePart::setTextureOffset(float x, float y)
{
m_frameIndex--;
m_currentFrame = m_frames[m_frameIndex];
}
#ifndef LIBZELDA_USE_QT
setTextureOffset(Vector2Df(x, y));
#else
setTextureOffset(QPoint(x, y));
#endif
}
SpriteFrame* SpritePart::frame(int id)
#ifndef LIBZELDA_USE_QT
void SpritePart::setTextureOffset(const Vector2Df& offset)
#else
void SpritePart::setTextureOffset(const QPoint& offset)
#endif
{
if (id < 0 || id >= (int)m_frames.size())
return NULL;
return m_frames[id];
m_textureOffset = offset;
#ifdef LIBZELDA_USE_QT
emit textureOffsetChanged(offset);
#endif
}
void SpritePart::setFrames(std::vector<SpriteFrame*> frames)
#ifndef LIBZELDA_USE_QT
Vector2Df SpritePart::textureOffset() const
#else
QPoint SpritePart::textureOffset() const
#endif
{
if (frames.size() == 0)
return;
return m_textureOffset;
}
if (m_frames.size() > 0)
void SpritePart::setSize(Uint32 width, Uint32 height)
{
for (SpriteFrame* frame : m_frames)
#ifndef LIBZELDA_USE_QT
setSize(Vector2Df(width, height));
#else
setSize(QSize(width, height));
#endif
}
#ifndef LIBZELDA_USE_QT
void SpritePart::setSize(const Vector2Di& size)
#else
void SpritePart::setSize(const QSize& size)
#endif
{
delete frame;
frame = NULL;
}
m_frames.clear();
m_size = size;
#ifdef LIBZELDA_USE_QT
emit sizeChanged(size);
#endif
}
if (!m_currentFrame)
m_currentFrame = frames[0];
m_frames = frames;
}
SpriteFrame* SpritePart::currentFrame()
#ifndef LIBZELDA_USE_QT
Vector2Di SpritePart::size() const
#else
QSize SpritePart::size() const
#endif
{
return m_currentFrame;
return m_size;
}
int SpritePart::currentFrameID()
void SpritePart::setFlippedHorizontally(const bool val)
{
return m_frameIndex;
m_flippedH = val;
#ifdef LIBZELDA_USE_QT
emit orientationChanged(val, flippedVertically());
#endif
}
std::vector<SpriteFrame*> SpritePart::frames() const
bool SpritePart::flippedHorizontally() const
{
return m_frames;
return m_flippedH;
}
Uint32 SpritePart::frameCount() const
void SpritePart::setFlippedVertically(const bool val)
{
return m_frames.size();
m_flippedV = val;
#ifdef LIBZELDA_USE_QT
emit orientationChanged(flippedHorizontally(), val);
#endif
}
bool SpritePart::flippedVertically() const
{
return m_flippedV;
}
SpriteFrame* SpritePart::root() const
{
return m_root;
}
}
}