From cda3b3d086b1d56abe7a9b019d2bd49f4f1b3883 Mon Sep 17 00:00:00 2001 From: Rtch90 Date: Sun, 15 Apr 2012 14:45:31 +0100 Subject: [PATCH] [Change] Added a positional sprite method. [Add] Added some more Animation. Not far off done now. --- LibDQt/LibDQt.pro | 6 ++- src/Actor/Actor.cpp | 4 ++ src/Actor/Actor.h | 1 + src/Actor/NPC.cpp | 2 +- src/Animation/AnimatingSprite.cpp | 71 +++++++++++++++++++++++++++++++ src/Animation/AnimatingSprite.h | 39 +++++++++++++++++ src/Sprite/Sprite.cpp | 7 ++- src/Sprite/Sprite.h | 1 + 8 files changed, 127 insertions(+), 4 deletions(-) create mode 100644 src/Animation/AnimatingSprite.cpp create mode 100644 src/Animation/AnimatingSprite.h diff --git a/LibDQt/LibDQt.pro b/LibDQt/LibDQt.pro index c8443f7..59bf44f 100644 --- a/LibDQt/LibDQt.pro +++ b/LibDQt/LibDQt.pro @@ -61,7 +61,8 @@ HEADERS += ../src/Actor/Player.h \ ../src/System/FileReader.h \ ../src/Animation/AnimationSequence.h \ ../src/System/String.h \ - ../src/Font/Font.h + ../src/Font/Font.h \ + ../src/Animation/AnimatingSprite.h SOURCES += ../src/Actor/Player.cpp \ ../src/Collision/AABB.cpp \ ../src/Global/Globals.cpp \ @@ -97,5 +98,6 @@ SOURCES += ../src/Actor/Player.cpp \ ../src/System/FileReader.cpp \ ../src/Animation/AnimationSequence.cpp \ ../src/System/String.cpp \ - ../src/Font/Font.cpp + ../src/Font/Font.cpp \ + ../src/Animation/AnimatingSprite.cpp OTHER_FILES += diff --git a/src/Actor/Actor.cpp b/src/Actor/Actor.cpp index 45f563c..d6b7165 100644 --- a/src/Actor/Actor.cpp +++ b/src/Actor/Actor.cpp @@ -54,3 +54,7 @@ void Actor::Update(float dt) { void Actor::Render(void) { _actor->Draw(); } + +void Actor::Render(float x, float y) { + _actor->Draw(x, y); +} diff --git a/src/Actor/Actor.h b/src/Actor/Actor.h index 53b4b22..274670b 100644 --- a/src/Actor/Actor.h +++ b/src/Actor/Actor.h @@ -15,6 +15,7 @@ public: virtual void Update(float dt); virtual void Render(void); + virtual void Render(float x, float y); float GetX(void) { return x; } float GetY(void) { return y; } diff --git a/src/Actor/NPC.cpp b/src/Actor/NPC.cpp index c344ab1..32da851 100644 --- a/src/Actor/NPC.cpp +++ b/src/Actor/NPC.cpp @@ -11,7 +11,7 @@ void NPC::Update(float dt) { } void NPC::Render(void) { - Actor::Render(); + Actor::Render(105, 125); } void NPC::Move(float dt) { diff --git a/src/Animation/AnimatingSprite.cpp b/src/Animation/AnimatingSprite.cpp new file mode 100644 index 0000000..7a8554a --- /dev/null +++ b/src/Animation/AnimatingSprite.cpp @@ -0,0 +1,71 @@ +#include "AnimatingSprite.h" + +// Stores and controls a number of sprites that are +// used to create an animation. Each frame is saved as an +// individual sprite, then the update method controls the frame +// that should be displayed. + +AnimatingSprite::AnimatingSprite(void) { + _spriteCounter = 0; + _timer = 0; +} + +AnimatingSprite::~AnimatingSprite(void) { + for(int i = 0; i < _numberOfFrames; i++) { + delete _sprites[i]; + } +} + +void AnimatingSprite::Render(void) { + _sprites[_currentFrame-1]->Draw(); +} + +void AnimatingSprite::Render(float x, float y) { + // Render with a position. + _sprites[_currentFrame-1]->Draw(x, y); +} + +void AnimatingSprite::Update(float dt) { + /* + * Use time to control the current animation frame. + * First off, check to see if there is an animation sequence, + * then if the dt is greate than the set animation speed, + * the _currentFrame is set to the next valid frame. + */ + + _timer += dt; + if(_sequence) { + if(_timer > _animationSpeed) { + _timer = 0; + _currentFrame++; + if(_currentFrame > _sequence->GetAnimation(_currentAnimation)->frameEnd) { + if(_sequence->GetAnimation(_currentAnimation)->_loopTo != "") { + SetCurrentAnimation(_sequence->GetAnimation(_currentAnimation)->_loopTo); + } else { + _currentFrame = _sequence->GetAnimation(_currentAnimation)->frameBegin; + } + } + } + } else { + if(_timer > _animationSpeed) { + _timer = 0; + _currentFrame = 0; + if(_currentFrame > _numberOfFrames) { + _currentFrame = 1; + } + } + } +} + + +void AnimatingSprite::LoadAnimatingSprite(const char* id, const char* filename, const char* sequence, int frames, float animationSpeed) { + +} + +void AnimatingSprite::SetCurrentAnimation(const char* filename) { + +} + +void AnimatingSprite::SetCurrentAnimation(int index) { + +} diff --git a/src/Animation/AnimatingSprite.h b/src/Animation/AnimatingSprite.h new file mode 100644 index 0000000..1d068a0 --- /dev/null +++ b/src/Animation/AnimatingSprite.h @@ -0,0 +1,39 @@ +#pragma once +#include "../Sprite/Sprite.h" +#include "AnimationSequence.h" +#define MAX_ANIM_FRAMES 128 + +class AnimatingSprite { +public: + AnimatingSprite(void); + ~AnimatingSprite(void); + + void SetCurrentAnimation(const char* filename); + void SetCurrentAnimation(int index); + const char* GetCurrentAnimation(void); + + int GetCurrentFrame(void) { return _currentFrame; } + int GetTotalFrames(void) { return _sequence->GetAnimation(_currentAnimation)->frameEnd; } + + void Update(float dt); + + void Render(void); + void Render(float x, float y); + + void LoadAnimatingSprite(const char* id, const char* filename, const char* sequence, int frames, float animationSpeed); + + const char* GetID(void) { return _id; } + +private: + Sprite* _sprites[MAX_ANIM_FRAMES]; + int _spriteCounter; + AnimationSequence* _sequence; + + String _id; + float _animationSpeed; + float _timer; + int _currentFrame; + int _numberOfFrames; + + String _currentAnimation; +}; diff --git a/src/Sprite/Sprite.cpp b/src/Sprite/Sprite.cpp index 2f7c881..595cbf7 100644 --- a/src/Sprite/Sprite.cpp +++ b/src/Sprite/Sprite.cpp @@ -23,6 +23,11 @@ void Sprite::Draw() const { DrawRegion(Rect(0.0f, 0.0f, (float)texture->GetWidth(), (float)texture->GetHeight())); } +void Sprite::Draw(float x, float y) { + DrawRegion(Rect(0.0f, 0.0f, (float)texture->GetWidth(), (float)texture->GetHeight())); + SetPosition(Vec2(x, y)); +} + void Sprite::DrawRegion(const Rect& src) const { const float uvX = src.x / (float)texture->GetWidth(); const float uvY = src.y / (float)texture->GetHeight(); @@ -36,7 +41,7 @@ void Sprite::DrawRegion(const Rect& src) const { // . . // 3---------2 - Vec2 scaledSize(src.w*scale.x, src.h*scale.y); + Vec2 scaledSize(src.w*scale.x, src.h*scale.y); Vec2 vertices[4] = { Vec2(0.0f, 0.0f), diff --git a/src/Sprite/Sprite.h b/src/Sprite/Sprite.h index 79a6ea1..1729c63 100644 --- a/src/Sprite/Sprite.h +++ b/src/Sprite/Sprite.h @@ -12,6 +12,7 @@ public: virtual void Update(float dt); virtual void Draw() const; + virtual void Draw(float x, float y); virtual void DrawRegion(const Rect& src) const; virtual bool LoadSprite(const std::string& filename);