[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

@ -12,7 +12,14 @@ Actor::Actor(void) {
_stepSFX[3] = sfxManager.Load("../Data/SFX/step_cloth4.wav");
_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) {
@ -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;
float _animationSpeed;
float _timer;
int _currentFrame;
int _numberOfFrames;
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

@ -18,85 +18,85 @@ using namespace std;
Debug *Debug::logger = NULL;
Debug::Debug(bool logToFile) {
time_t timestamp;
if(logToFile) {
_logFile.open("../Bin/Debug.log", ios::out);
if(!_logFile.is_open()) {
// We can not open our log.
cerr << "Warning: Can not open Debug.log to write, continueing without logging\n";
time_t timestamp;
if(logToFile) {
_logFile.open("../Bin/Debug.log", ios::out);
if(!_logFile.is_open()) {
// We can not open our log.
cerr << "Warning: Can not open Debug.log to write, continueing without logging\n";
} else {
// Log File is open, let us give it a nice time stamp.
timestamp = time(NULL);
_logFile << "Log Started: " << ctime(&timestamp) << endl;
}
}
} else {
// Log File is open, let us give it a nice time stamp.
timestamp = time(NULL);
_logFile << "Log Started: " << ctime(&timestamp) << endl;
}
}
}
Debug::~Debug(void) {
time_t timestamp;
time_t timestamp;
// We only need to close the log if it is open.
if(_logFile.is_open()) {
// Give it a closing timestamp.
timestamp = time(NULL);
_logFile << endl << "Log Closed: " << ctime(&timestamp) << endl;
// We only need to close the log if it is open.
if(_logFile.is_open()) {
// Give it a closing timestamp.
timestamp = time(NULL);
_logFile << endl << "Log Closed: " << ctime(&timestamp) << endl;
// Close the log file.
_logFile.close();
}
// Close the log file.
_logFile.close();
}
}
void Debug::message(std::string msg) {
if(_logFile.is_open()) {
_logFile << msg << endl;
}
cerr << msg << endl << endl;
if(_logFile.is_open()) {
_logFile << msg << endl;
}
cerr << msg << endl << endl;
}
void Debug::message(const char *msg, ...) {
va_list vargList; // This is to handlle the variable arguments
va_list vargList; // This is to handlle the variable arguments
char outBuf[1024];
unsigned short outLen;
char outBuf[1024];
unsigned short outLen;
// This takes the arguments and puts them into the character array.
va_start(vargList, msg);
// This takes the arguments and puts them into the character array.
va_start(vargList, msg);
#if defined WIN32
outLen = _vsnprintf(outBuf, sizeof(outBuf), msg, vargList);
#ifdef WIN32
outLen = _vsnprintf(outBuf, sizeof(outBuf), msg, vargList);
#else
outLen = vsnprintf(outBuf, sizeof(outBuf), msg, vargList);
outLen = vsnprintf(outBuf, sizeof(outBuf), msg, vargList);
#endif
va_end(vargList);
va_end(vargList);
if(outLen >= sizeof(outBuf)) {
outLen = sizeof(outBuf);
}
if(outLen >= sizeof(outBuf)) {
outLen = sizeof(outBuf);
}
if(_logFile.is_open()) {
_logFile << outBuf << endl;
}
if(_logFile.is_open()) {
_logFile << outBuf << endl;
}
cerr << outBuf << endl;
cerr << outBuf << endl;
}
bool Debug::openLog(bool logToFile) {
// Make sure the logger has not already been initialized.
if(logger != NULL) {
logger->message("Warning: Multiple calls to openLog().");
return false;
}
logger = new Debug(logToFile);
return true;
// Make sure the logger has not already been initialized.
if(logger != NULL) {
logger->message("Warning: Multiple calls to openLog().");
return false;
}
logger = new Debug(logToFile);
return true;
}
void Debug::closeLog(void) {
if(logger == NULL) {
cerr << "Warning: Call to closeLog() with NULL logger pointer." << endl;
return;
}
delete logger;
logger = NULL;
if(logger == NULL) {
cerr << "Warning: Call to closeLog() with NULL logger pointer." << endl;
return;
}
delete logger;
logger = NULL;
}

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

@ -34,7 +34,7 @@ int BuildTexture(const char* filename, GLuint* texID, GLint param, bool genMips)
// Load the image, check for errors, if it isn't found, quit.
textureImage = IMG_Load(filename);
if(!textureImage) {
Debug::logger->message("Warning: could not load %s", filename);
return false;
@ -70,18 +70,18 @@ int BuildTexture(const char* filename, GLuint* texID, GLint param, bool genMips)
SDL_FreeSurface(textureImage);
return false;
}
// Create the texture.
glGenTextures(1, texID);
// Typical texture generation using data from the bitmap.
BindTexture(*texID);
// Setup filtering.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, param);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, param);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if(genMips) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
// Generate the textures and mipmaps.
@ -107,52 +107,52 @@ int LoadTGAFile(const char* filename, TGAFILE* tgaFile) {
int colorMode; // 4 for RGBA or 3 for RGB.
long imgIndex; // counter variable.
unsigned char colorSwap; // Swap variable.
// Open the TGA file.
filePtr = fopen(filename, "rb");
if(!filePtr)
return 0;
// Read the first two bytes of garbage.
fread(&ucharBad, sizeof(unsigned char), 1, filePtr);
fread(&ucharBad, sizeof(unsigned char), 1, filePtr);
// Read in the image type.
fread(&tgaFile->textureTypeCode, sizeof(unsigned char), 1, filePtr);
// The texture type should be either 2(color) or 3(greyscale).
if((tgaFile->textureTypeCode != 2) && (tgaFile->textureTypeCode != 3)) {
fclose(filePtr);
return 0;
}
// Read 13 bytes of garbage data.
fread(&sintBad, sizeof(short int), 1, filePtr);
fread(&sintBad, sizeof(short int), 1, filePtr);
fread(&ucharBad, sizeof(unsigned char), 1, filePtr);
fread(&sintBad, sizeof(short int), 1, filePtr);
fread(&sintBad, sizeof(short int), 1, filePtr);
// Read image dimensions.
fread(&tgaFile->textureWidth, sizeof(short int), 1, filePtr);
fread(&tgaFile->textureHeight, sizeof(short int), 1, filePtr);
// Read image bit depth.
fread(&tgaFile->bitCount, sizeof(unsigned char), 1, filePtr);
// Read 1 byte of garbage data.
fread(&ucharBad, sizeof(unsigned char), 1, filePtr);
// colorMode -> 3 = BGR, 4 = BGRA
colorMode = tgaFile->bitCount / 8;
imageSize = tgaFile->textureWidth * tgaFile->textureHeight * colorMode;
// Allocate memory for image data.
tgaFile->textureData = (unsigned char*)malloc(sizeof(unsigned char)*imageSize);
// Read in the image data.
fread(tgaFile->textureData, sizeof(unsigned char), imageSize, filePtr);
// Change BGR to RGB so OpenGL can read the data.
for(imgIndex = 0; imgIndex < imageSize; imgIndex += colorMode) {
colorSwap = tgaFile->textureData[imgIndex];
@ -174,58 +174,58 @@ int WriteTGAFile(const char* filename, short int width, short int height, unsign
unsigned char bitDepth;
long imageSize;
FILE* filePtr;
// Create a file for writing to binary mode.
filePtr = fopen(filename, "wb");
if(!filePtr) {
fclose(filePtr);
return 0;
}
imageType = 2; // RGB, uncompressed.
bitDepth = 24; // 24-bitdepth.
colorMode = 3; // RGB color mode.
byteSkip = 0;
shortSkip = 0;
// Write 2 bytes of data.
fwrite(&byteSkip, sizeof(unsigned char), 1, filePtr);
fwrite(&byteSkip, sizeof(unsigned char), 1, filePtr);
// Write image type.
fwrite(&imageType, sizeof(unsigned char), 1, filePtr);
fwrite(&shortSkip, sizeof(short int), 1, filePtr);
fwrite(&shortSkip, sizeof(short int), 1, filePtr);
fwrite(&byteSkip, sizeof(unsigned char), 1, filePtr);
fwrite(&shortSkip, sizeof(short int), 1, filePtr);
fwrite(&shortSkip, sizeof(short int), 1, filePtr);
// Write image dimensions.
fwrite(&width, sizeof(short int), 1, filePtr);
fwrite(&height, sizeof(short int), 1, filePtr);
fwrite(&bitDepth, sizeof(unsigned char), 1, filePtr);
// Write 1 byte of data
fwrite(&byteSkip, sizeof(unsigned char), 1, filePtr);
// Calculate the image size.
imageSize = width * height * colorMode;
// Change the image data from RGB to BGR
for(imgIndex = 0; imgIndex < imageSize; imgIndex += colorMode) {
colorSwap = imageData[imgIndex];
imageData[imgIndex] = imageData[imgIndex + 2];
imageData[imgIndex + 2] = colorSwap;
}
// Write the image data.
fwrite(imageData, sizeof(unsigned char), imageSize, filePtr);
// Close the file.
fclose(filePtr);
return 1;
}
@ -257,3 +257,4 @@ bool Texture::Load(const std::string& filename) {
}
return false;
}