[Add] Added Animating sprites. -- Bit buggy right now.

This commit is contained in:
Rtch90 2012-04-15 22:15:23 +01:00
parent cda3b3d086
commit 42ecaaefd2
24 changed files with 206 additions and 114 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -0,0 +1,3 @@
2;
Idle 1 1;
Walk 2 4;

0
Data/Img/Player/Player_F Normal file
View File

BIN
Data/Img/Reniesta.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

View File

@ -13,6 +13,13 @@ Actor::Actor(void) {
_lastStepSFXPlayed = -1; _lastStepSFXPlayed = -1;
_velocity = 4.0f; _velocity = 4.0f;
_direction = Front;
_preventMovement = NONE;
_actorLeft = new AnimatingSprite();
_actorRight = new AnimatingSprite();
_actorFront = new AnimatingSprite();
_actorBack = new AnimatingSprite();
} }
Actor::~Actor(void) { Actor::~Actor(void) {
@ -23,6 +30,10 @@ Actor::~Actor(void) {
} }
} }
delete _actor; delete _actor;
delete _actorLeft;
delete _actorRight;
delete _actorFront;
delete _actorBack;
} }
void Actor::LoadSprite(const char* filename) { void Actor::LoadSprite(const char* filename) {
@ -35,6 +46,24 @@ void Actor::Update(float dt) {
float oldX = x = _actor->GetX(); float oldX = x = _actor->GetX();
float oldY = y = _actor->GetY(); float oldY = y = _actor->GetY();
if(_direction == LEFT) {
_actorLeft->Update(dt);
}
else if(_direction == RIGHT) {
_actorRight->Update(dt);
}
else if(_direction == Front) {
_actorFront->Update(dt);
}
else if(_direction == BACK) {
_actorBack->Update(dt);
}
// We should check for collisions now.
Move(dt); Move(dt);
if(x != oldX || y != oldY) { if(x != oldX || y != oldY) {

View File

@ -2,12 +2,27 @@
#include <SDL/SDL.h> #include <SDL/SDL.h>
#include "../Sprite/Sprite.h" #include "../Sprite/Sprite.h"
#include "../Animation/AnimatingSprite.h"
#include "../Math/Vec2.h" #include "../Math/Vec2.h"
class SoundEffect; class SoundEffect;
class Actor { class Actor {
public: public:
enum Facing {
Front,
BACK,
LEFT,
RIGHT,
NONE
};
enum Status {
WALKING,
HURT
};
Actor(void); Actor(void);
~Actor(void); ~Actor(void);
@ -24,21 +39,22 @@ public:
void SetXY(float xArg, float yArg) { x = xArg; y = yArg; } void SetXY(float xArg, float yArg) { x = xArg; y = yArg; }
int GetDirection(void) { return _direction; } Facing GetDirection(void) { return _direction; }
void SetDirection(int direction) { _direction = direction; } void SetDirection(Facing direction) { _direction = direction; }
protected: protected:
virtual void Move(float dt) = 0; virtual void Move(float dt) = 0;
float _velocity; float _velocity;
int _direction;
static const int ANIM_LEFT_FOOT = 0;
static const int ANIM_NO_FOOT = 1;
static const int ANIM_RIGHT_FOOT = 2;
static const int ANIM_ATTACK = 3;
Sprite* _actor; Sprite* _actor;
AnimatingSprite* _actorLeft;
AnimatingSprite* _actorRight;
AnimatingSprite* _actorFront;
AnimatingSprite* _actorBack;
Facing _direction;
Facing _preventMovement;
float x; float x;
float y; float y;
@ -47,8 +63,6 @@ private:
float w; float w;
float h; float h;
Vec2 _spriteVector[4][4];
SoundEffect* _stepSFX[4]; SoundEffect* _stepSFX[4];
int _lastStepSFXPlayed; int _lastStepSFXPlayed;
}; };

View File

@ -1,6 +1,7 @@
#include "NPC.h" #include "NPC.h"
NPC::NPC(void) : Actor() { NPC::NPC(void) : Actor() {
LoadSprite("../Data/Img/Player.png");
} }
NPC::~NPC(void) { NPC::~NPC(void) {

View File

@ -2,6 +2,10 @@
#include "../IO/Input.h" #include "../IO/Input.h"
Player::Player(void) : Actor() { Player::Player(void) : Actor() {
Actor::_actorFront->LoadAnimatingSprite("Player_f", "../Data/Img/Player/Front/Player_F", "../Data/Img/Player/Player_F", 4, _velocity);
//Actor::_actorFront->LoadAnimatingSprite("Player_b", "../Data/Img/Player/Front/Player_B", "../Data/Img/Player/Player_B", 4, _velocity);
//Actor::_actorFront->LoadAnimatingSprite("Player_L", "../Data/Img/Player/Front/Player_L", "../Data/Img/Player/Player_L", 4, _velocity);
//Actor::_actorFront->LoadAnimatingSprite("Player_R", "../Data/Img/Player/Front/Player_R", "../Data/Img/Player/Player_R", 4, _velocity);
} }
Player::~Player(void) { Player::~Player(void) {

View File

@ -59,13 +59,34 @@ void AnimatingSprite::Update(float dt) {
void AnimatingSprite::LoadAnimatingSprite(const char* id, const char* filename, const char* sequence, int frames, float animationSpeed) { void AnimatingSprite::LoadAnimatingSprite(const char* id, const char* filename, const char* sequence, int frames, float animationSpeed) {
for(int i = 0; i < frames; i++) {
String tempFilename;
tempFilename = "";
if(i < 10) {
tempFilename.Format("%s.00%i.png", filename, i);
}
else if(i < 100) {
tempFilename.Format("%s.0%i.png", filename, i);
} else {
tempFilename.Format("%s.%i.png", filename, i);
}
_sprites[_spriteCounter] = new Sprite();
_sprites[_spriteCounter]->LoadSprite((const char*)tempFilename);
_spriteCounter++;
}
_id = id;
_numberOfFrames = frames;
_animationSpeed = animationSpeed;
_sequence = new AnimationSequence(sequence);
SetCurrentAnimation(0);
} }
void AnimatingSprite::SetCurrentAnimation(const char* filename) { void AnimatingSprite::SetCurrentAnimation(const char* animation) {
_currentAnimation = _sequence->GetAnimation(animation)->_animationID;
_currentFrame = _sequence->GetAnimation(animation)->frameBegin;
} }
void AnimatingSprite::SetCurrentAnimation(int index) { void AnimatingSprite::SetCurrentAnimation(int index) {
_currentAnimation = _sequence->GetAnimation(index)->_animationID;
_currentFrame = _sequence->GetAnimation(index)->frameBegin;
} }

View File

@ -29,11 +29,11 @@ private:
int _spriteCounter; int _spriteCounter;
AnimationSequence* _sequence; AnimationSequence* _sequence;
String _id; const char* _id;
float _animationSpeed; float _animationSpeed;
float _timer; float _timer;
int _currentFrame; int _currentFrame;
int _numberOfFrames; int _numberOfFrames;
String _currentAnimation; const char* _currentAnimation;
}; };

View File

@ -24,7 +24,6 @@ AnimationSequence::AnimationSequence(void) {
AnimationSequence::AnimationSequence(const char* filename) { AnimationSequence::AnimationSequence(const char* filename) {
_numberOfFrames = 0; _numberOfFrames = 0;
_sequenceID = filename; _sequenceID = filename;
_sequenceID.Concatenate(".txt");
ReadFile(); ReadFile();
} }
@ -70,7 +69,7 @@ void AnimationSequence::ReadFile(void) {
_animations[index]->_loopTo = loop; _animations[index]->_loopTo = loop;
} }
} else { } else {
//Debug::logger->message("%s does not exist.", _sequenceID); Debug::logger->message("%s does not exist.", _sequenceID);
assert(false); assert(false);
} }
} }

View File

@ -12,7 +12,7 @@
class AnimationSequence { class AnimationSequence {
struct Animation { struct Animation {
String _animationID; const char* _animationID;
int frameBegin; int frameBegin;
int frameEnd; int frameEnd;
String _loopTo; String _loopTo;
@ -30,7 +30,7 @@ public:
private: private:
const char* Scan(char* source, int& counter); const char* Scan(char* source, int& counter);
String _sequenceID; const char* _sequenceID;
int _numberOfFrames; int _numberOfFrames;
FileReader _file; FileReader _file;
Animation* _animations[MAX_FRAMES]; Animation* _animations[MAX_FRAMES];

View File

@ -20,9 +20,6 @@ Game::Game(void) {
_NPC = new NPC(); _NPC = new NPC();
_level = new Level(); _level = new Level();
_player->LoadSprite("../Data/Img/Player.png");
_NPC->LoadSprite("../Data/Img/Player.png");
_NPC->SetXY(30.0f, 30.0f); _NPC->SetXY(30.0f, 30.0f);
_testFont = new Font(); _testFont = new Font();

View File

@ -101,4 +101,3 @@ void Sprite::SetTexture(Texture* texture) {
this->texture = texture; this->texture = texture;
this->size = Vec2((float)texture->GetWidth(), (float)texture->GetHeight()); this->size = Vec2((float)texture->GetWidth(), (float)texture->GetHeight());
} }

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <string> #include <string>
#include <cstdarg>
#include "../System/String.h"
#include "../Math/Vec2.h" #include "../Math/Vec2.h"
#include "../Math/Rect.h" #include "../Math/Rect.h"

View File

@ -63,7 +63,7 @@ void Debug::message(const char *msg, ...) {
// This takes the arguments and puts them into the character array. // This takes the arguments and puts them into the character array.
va_start(vargList, msg); va_start(vargList, msg);
#if defined WIN32 #ifdef WIN32
outLen = _vsnprintf(outBuf, sizeof(outBuf), msg, vargList); outLen = _vsnprintf(outBuf, sizeof(outBuf), msg, vargList);
#else #else
outLen = vsnprintf(outBuf, sizeof(outBuf), msg, vargList); outLen = vsnprintf(outBuf, sizeof(outBuf), msg, vargList);

View File

@ -3,6 +3,8 @@
#include <string> #include <string>
#include <map> #include <map>
#include "../System/String.h"
class Resource { class Resource {
public: public:
virtual bool Load(const std::string& filename) = 0; virtual bool Load(const std::string& filename) = 0;

View File

@ -1,7 +1,10 @@
#ifdef _WIN32
#define "windows.h"
#endif
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <stdarg.h>
#include "String.h" #include "String.h"
#define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS
@ -40,6 +43,21 @@ int String::Length(void) {
return strlen(_string); return strlen(_string);
} }
void String::Format(const char* format, ...) {
char temp[256];
va_list vlist;
va_start(vlist, format);
#ifdef _WIN32
vsprintf_s(&temp[0], 256, format, vlist);
#else
vsnprintf(&temp[0], 256, format, vlist);
#endif
va_end(vlist);
memcpy(_string, temp, strlen(temp)+1);
}
void String::Concatenate(char value) { void String::Concatenate(char value) {
// Concatenate a char on the end of a string. // Concatenate a char on the end of a string.
assert(strlen(_string) + 1 < MAX_STRING_LEN); assert(strlen(_string) + 1 < MAX_STRING_LEN);

View File

@ -16,6 +16,8 @@ public:
void Concatenate(char value); void Concatenate(char value);
int Length(void); int Length(void);
void Format(const char* format, ...);
// Operator overloads. // Operator overloads.
String& operator=(const char* value); String& operator=(const char* value);
String& operator=(String& value); String& operator=(String& value);

View File

@ -257,3 +257,4 @@ bool Texture::Load(const std::string& filename) {
} }
return false; return false;
} }