From 966efd3dd2fef4ef9095b3b9358f0442720189b6 Mon Sep 17 00:00:00 2001 From: Antidote Date: Wed, 26 Feb 2014 21:47:39 -0800 Subject: [PATCH] * Finish refactor --- include/Sprite.hpp | 98 +++++++++++++++--- include/SpriteFile.hpp | 96 +++++++++++++++-- include/SpriteFrame.hpp | 158 ++++++++-------------------- include/SpritePart.hpp | 186 +++++++++++++++++++++++++++++---- libzelda.pro | 7 ++ src/Sprite.cpp | 216 +++++++++++++++++++++++++++++++++------ src/SpriteFile.cpp | 156 ++++++++++++++++++++++++++-- src/SpriteFileReader.cpp | 76 +++++++++++--- src/SpriteFileWriter.cpp | 52 +++++++--- src/SpriteFrame.cpp | 122 ++++++++-------------- src/SpritePart.cpp | 174 +++++++++++++++++++++---------- 11 files changed, 974 insertions(+), 367 deletions(-) diff --git a/include/Sprite.hpp b/include/Sprite.hpp index 6a50966..d6756f0 100644 --- a/include/Sprite.hpp +++ b/include/Sprite.hpp @@ -1,9 +1,15 @@ #ifndef SSPRITE_HPP #define SSPRITE_HPP -#include +#ifndef LIBZELDA_USE_QT #include #include +#else +#include +#include +#include +#include +#endif #include 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 ids); +#ifndef LIBZELDA_USE_QT std::vector stateIds() const; +#else + QList 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 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 frames); +#else + void setFrames(QList frames); +#endif + Uint32 frameCount() const; - std::vector parts() const; +#ifndef LIBZELDA_USE_QT + std::vector frames() const; +#else + QList frames() const; +#endif SpriteFile* container() const; -private: - SpriteFile* m_root; - std::string m_name; - Vector2Df m_position; - std::vector m_stateIds; //!< Stores the texture id's for each state. - std::vector m_parts; - Uint32 m_currentState; + 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 m_stateIds; //!< Stores the texture id's for each state. + std::vector m_frames; +#else + QString m_name; + QPoint m_position; + QList m_stateIds; + QList 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 diff --git a/include/SpriteFile.hpp b/include/SpriteFile.hpp index dc79d7b..12b94a3 100644 --- a/include/SpriteFile.hpp +++ b/include/SpriteFile.hpp @@ -1,8 +1,17 @@ #ifndef SSPRITEFILE_HPP #define SSPRITEFILE_HPP +#ifndef LIBZELDA_USE_QT #include #include +#else +#include +#include +#include +#include +#include +#endif + #include #include @@ -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 textures() const; +#else + QList textures() const; +#endif Uint32 textureCount() const; /*! * \brief setTextures * \param textures */ + +#ifndef LIBZELDA_USE_QT void setTextures(std::vector textures); +#else + void setTextures(QList 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 sprites); - +#else + void setSprites(QMap sprites); +#endif +#ifndef LIBZELDA_USE_QT Sprite* sprite(const std::string& name); - std::unordered_map sprites() const; +#else + Sprite* sprite(const QString& name); + QMap sprites() const; +#endif + Uint32 spriteCount() const; -private: - std::vector m_textures; +#ifdef LIBZELDA_USE_QT +signals: + void originChanged(QPoint); + void sizeChanged(QSize); +#endif + private: + #ifndef LIBZELDA_USE_QT + std::vector m_textures; Vector2Di m_size; Vector2Df m_origin; std::unordered_map m_sprites; +#else + QList m_textures; + QSize m_size; + QPoint m_origin; + QMap 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 diff --git a/include/SpriteFrame.hpp b/include/SpriteFrame.hpp index 9d05f57..dd33564 100644 --- a/include/SpriteFrame.hpp +++ b/include/SpriteFrame.hpp @@ -4,149 +4,81 @@ #include +#ifndef LIBZELDA_USE_QT #include +#else +#include +#include +#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 parts); + std::vector parts() const; +#else + void setParts(QList parts); + QList 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; - float m_frameTime; - bool m_flippedH; - bool m_flippedV; + Sprite* m_root; + float m_frameTime; +#ifndef LIBZELDA_USE_QT + std::vector m_parts; +#else + QList m_parts; +#endif }; } // Sakura } // zelda -#endif // SSpRITEFRAME_HPP +#ifdef LIBZELDA_USE_QT +Q_DECLARE_METATYPE(zelda::Sakura::SpriteFrame*); +#endif + +#endif // SSPRITEFRAME_HPP diff --git a/include/SpritePart.hpp b/include/SpritePart.hpp index c2d1296..a0f467a 100644 --- a/include/SpritePart.hpp +++ b/include/SpritePart.hpp @@ -2,7 +2,15 @@ #define SSPRITEPART_HPP #include -#include +#ifndef LIBZELDA_USE_QT +# include +#else +# include +# include +# include +# include +#endif + #include #include @@ -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 frames); - SpriteFrame* currentFrame(); - int currentFrameID(); - std::vector 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 m_frames; }; } } +#ifdef LIBZELDA_USE_QT +Q_DECLARE_METATYPE(zelda::Sakura::SpritePart*) +#endif -#endif // SSpRITEPART_HPP +#endif // SSPRITEPART_HPP diff --git a/libzelda.pro b/libzelda.pro index 8e3e34e..45f57ff 100644 --- a/libzelda.pro +++ b/libzelda.pro @@ -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 diff --git a/src/Sprite.cpp b/src/Sprite.cpp index 45f06bd..879bdf1 100644 --- a/src/Sprite.cpp +++ b/src/Sprite.cpp @@ -1,51 +1,94 @@ #include "Sprite.hpp" -#include "SpritePart.hpp" +#include "SpriteFrame.hpp" #include "SpriteFile.hpp" #include +#ifdef LIBZELDA_USE_QT +#include +#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 ids) if (ids.size() == 0) return; +#ifndef LIBZELDA_USE_QT m_stateIds = ids; +#else + m_stateIds = QList::fromVector(QVector::fromStdVector(ids)); +#endif } +#ifndef LIBZELDA_USE_QT std::vector Sprite::stateIds() const +#else +QList Sprite::stateIds() const +#endif { return m_stateIds; } @@ -87,50 +138,153 @@ Uint32 Sprite::stateCount() const void Sprite::setCurrentState(const Uint32 id) { - m_currentState = id; -} - -void Sprite::addPart(SpritePart* part) -{ - for (SpritePart* tmp : m_parts) - { - if (tmp == part) - return; - } - m_parts.push_back(part); -} - -void Sprite::setParts(std::vector parts) -{ - if (parts.size() == 0) + if (id >= m_stateIds.size()) return; - if (m_parts.size() > 0) + m_currentState = id; +#ifdef LIBZELDA_USE_QT + emit stateChanged(id); +#endif +} + +Uint32 Sprite::currentState() const +{ + return m_currentState; +} + +bool Sprite::addFrame(SpriteFrame* part) +{ + if (m_frames.size() > 65536) + return false; + for (SpriteFrame* tmp : m_frames) { - for (SpritePart* part : m_parts) - { - delete part; - part = NULL; - } - m_parts.clear(); + if (tmp == part) + return false; } - m_parts = parts; + m_frames.push_back(part); + return true; } -Uint32 Sprite::partCount() const +bool Sprite::removeFrame(SpriteFrame* frame) { - return m_parts.size(); +#ifndef LIBZELDA_USE_QT + std::vector::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; } -std::vector Sprite::parts() const +void Sprite::setFrame(Uint32 id) { - return m_parts; + if (id > m_frames.size()) + return; +} + +#ifndef LIBZELDA_USE_QT +void Sprite::setFrames(std::vector frames) +{ + if (frames.size() == 0) + return; + if (m_frames.size() > 0) + { + for (SpriteFrame* frame : m_frames) + { + delete frame; + frame = NULL; + } + m_frames.clear(); + } + m_frames = frames; +} +#else +void Sprite::setFrames(QList frames) +{ + m_frames.clear(); + m_frames = frames; +} +#endif + + +Uint32 Sprite::frameCount() const +{ + return m_frames.size(); +} + +#ifndef LIBZELDA_USE_QT +std::vector Sprite::frames() const +#else +QList Sprite::frames() const +#endif +{ + 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; +} } } diff --git a/src/SpriteFile.cpp b/src/SpriteFile.cpp index a7a9073..209f4bd 100644 --- a/src/SpriteFile.cpp +++ b/src/SpriteFile.cpp @@ -1,15 +1,15 @@ #include "SpriteFile.hpp" #include "Sprite.hpp" +#include "utility.hpp" #include - 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 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 SpriteFile::textures() const +#else +QList 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::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 sprites) { if (sprites.size() == 0) @@ -140,16 +240,41 @@ void SpriteFile::setSprites(std::unordered_map sprites) m_sprites = sprites; } +#else +void SpriteFile::setSprites(QMap 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 SpriteFile::sprites() const +#else +QMap 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 textures) { if (textures.size() == 0) @@ -176,5 +302,15 @@ void SpriteFile::setTextures(std::vector textures) m_textures = textures; } +#else +void SpriteFile::setTextures(QList textures) +{ + if (textures.size() == 0) + return; + + m_textures.clear(); + m_textures = textures; +} +#endif } // Sakura } // zelda diff --git a/src/SpriteFileReader.cpp b/src/SpriteFileReader.cpp index 4016ea6..c5a0620 100644 --- a/src/SpriteFileReader.cpp +++ b/src/SpriteFileReader.cpp @@ -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 textures; +#else + QList 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 sprites; +#else + QMap 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 parts; - for (Uint8 j = 0; j < partCount; j++) +#ifndef LIBZELDA_USE_QT + std::vector frames; +#else + QList frames; +#endif + + for (Uint32 k = 0; k < frameCount; k++) { - Sakura::SpritePart* part = new Sakura::SpritePart(sprite); - part->setName(base::readString()); - part->setCollision(base::readBool()); + Sakura::SpriteFrame* frame = new Sakura::SpriteFrame(sprite); + frame->setFrameTime(base::readFloat()); + Uint16 partCount = base::readUInt16(); - Uint32 frameCount = base::readUInt32(); - std::vector frames; - for (Uint32 k = 0; k < frameCount; k++) +#ifndef LIBZELDA_USE_QT + std::vector parts; +#else + QList 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)); + parts.push_back(part); } - 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"); } diff --git a/src/SpriteFileWriter.cpp b/src/SpriteFileWriter.cpp index f2c709c..3faeda9 100644 --- a/src/SpriteFileWriter.cpp +++ b/src/SpriteFileWriter.cpp @@ -42,34 +42,52 @@ void SpriteFileWriter::writeFile(Sakura::SpriteFile* file) base::writeBool(texture->Preload); } +#ifndef LIBZELDA_USE_QT for (std::pair 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->frameTime()); + base::writeUInt16(frame->partCount()); + for (Sakura::SpritePart* part: frame->parts()) { - 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()); +#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()); } } } diff --git a/src/SpriteFrame.cpp b/src/SpriteFrame.cpp index 8f07d1a..ecf49d9 100644 --- a/src/SpriteFrame.cpp +++ b/src/SpriteFrame.cpp @@ -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 parts) +#else +void SpriteFrame::setParts(QList parts) +#endif +{ + m_parts = parts; +} + +#ifndef LIBZELDA_USE_QT +std::vector SpriteFrame::parts() const +#else +QList 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 diff --git a/src/SpritePart.cpp b/src/SpritePart.cpp index 2353e37..6cfc877 100644 --- a/src/SpritePart.cpp +++ b/src/SpritePart.cpp @@ -1,40 +1,51 @@ #include "SpritePart.hpp" -#include "SpriteFrame.hpp" +#include "SpritePart.hpp" #include "Sprite.hpp" +#include 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) - { - m_frameIndex--; - m_currentFrame = m_frames[m_frameIndex]; - } + return m_offset; } -SpriteFrame* SpritePart::frame(int id) +void SpritePart::setTextureOffset(float x, float y) { - if (id < 0 || id >= (int)m_frames.size()) - return NULL; - - return m_frames[id]; +#ifndef LIBZELDA_USE_QT + setTextureOffset(Vector2Df(x, y)); +#else + setTextureOffset(QPoint(x, y)); +#endif } -void SpritePart::setFrames(std::vector frames) +#ifndef LIBZELDA_USE_QT +void SpritePart::setTextureOffset(const Vector2Df& offset) +#else +void SpritePart::setTextureOffset(const QPoint& offset) +#endif { - if (frames.size() == 0) - return; - - if (m_frames.size() > 0) - { - for (SpriteFrame* frame : m_frames) - { - delete frame; - frame = NULL; - } - m_frames.clear(); - } - - if (!m_currentFrame) - m_currentFrame = frames[0]; - - m_frames = frames; + m_textureOffset = offset; +#ifdef LIBZELDA_USE_QT + emit textureOffsetChanged(offset); +#endif } -SpriteFrame* SpritePart::currentFrame() +#ifndef LIBZELDA_USE_QT +Vector2Df SpritePart::textureOffset() const +#else +QPoint SpritePart::textureOffset() const +#endif { - return m_currentFrame; + return m_textureOffset; } -int SpritePart::currentFrameID() +void SpritePart::setSize(Uint32 width, Uint32 height) { - return m_frameIndex; +#ifndef LIBZELDA_USE_QT + setSize(Vector2Df(width, height)); +#else + setSize(QSize(width, height)); +#endif } -std::vector SpritePart::frames() const +#ifndef LIBZELDA_USE_QT +void SpritePart::setSize(const Vector2Di& size) +#else +void SpritePart::setSize(const QSize& size) +#endif { - return m_frames; + m_size = size; +#ifdef LIBZELDA_USE_QT + emit sizeChanged(size); +#endif } -Uint32 SpritePart::frameCount() const +#ifndef LIBZELDA_USE_QT +Vector2Di SpritePart::size() const +#else +QSize SpritePart::size() const +#endif { - return m_frames.size(); + return m_size; } +void SpritePart::setFlippedHorizontally(const bool val) +{ + m_flippedH = val; +#ifdef LIBZELDA_USE_QT + emit orientationChanged(val, flippedVertically()); +#endif +} + +bool SpritePart::flippedHorizontally() const +{ + return m_flippedH; +} + +void SpritePart::setFlippedVertically(const bool val) +{ + 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; +} } }