[Change] Added a positional sprite method.

[Add] Added some more Animation. Not far off done now.
This commit is contained in:
Rtch90 2012-04-15 14:45:31 +01:00
parent 44f6fd070c
commit cda3b3d086
8 changed files with 127 additions and 4 deletions

View File

@ -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 +=

View File

@ -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);
}

View File

@ -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; }

View File

@ -11,7 +11,7 @@ void NPC::Update(float dt) {
}
void NPC::Render(void) {
Actor::Render();
Actor::Render(105, 125);
}
void NPC::Move(float dt) {

View File

@ -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) {
}

View File

@ -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;
};

View File

@ -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),

View File

@ -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);