[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;
_velocity = 4.0f;
_direction = Front;
_preventMovement = NONE;
_actorLeft = new AnimatingSprite();
_actorRight = new AnimatingSprite();
_actorFront = new AnimatingSprite();
_actorBack = new AnimatingSprite();
}
Actor::~Actor(void) {
@ -23,6 +30,10 @@ Actor::~Actor(void) {
}
}
delete _actor;
delete _actorLeft;
delete _actorRight;
delete _actorFront;
delete _actorBack;
}
void Actor::LoadSprite(const char* filename) {
@ -35,6 +46,24 @@ void Actor::Update(float dt) {
float oldX = x = _actor->GetX();
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);
if(x != oldX || y != oldY) {

View File

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

View File

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

View File

@ -2,6 +2,10 @@
#include "../IO/Input.h"
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) {

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) {
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) {
_currentAnimation = _sequence->GetAnimation(index)->_animationID;
_currentFrame = _sequence->GetAnimation(index)->frameBegin;
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,7 @@
#pragma once
#include <string>
#include <cstdarg>
#include "../System/String.h"
#include "../Math/Vec2.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.
va_start(vargList, msg);
#if defined WIN32
#ifdef WIN32
outLen = _vsnprintf(outBuf, sizeof(outBuf), msg, vargList);
#else
outLen = vsnprintf(outBuf, sizeof(outBuf), msg, vargList);

View File

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

View File

@ -1,7 +1,10 @@
#ifdef _WIN32
#define "windows.h"
#endif
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdarg.h>
#include "String.h"
#define _CRT_SECURE_NO_WARNINGS
@ -40,6 +43,21 @@ int String::Length(void) {
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) {
// Concatenate a char on the end of a string.
assert(strlen(_string) + 1 < MAX_STRING_LEN);

View File

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

View File

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