diff --git a/Data/Img/Player.png b/Data/Img/Player.png index 2a83207..af300da 100644 Binary files a/Data/Img/Player.png and b/Data/Img/Player.png differ diff --git a/Data/Img/Player/Front/Player_F.000.png b/Data/Img/Player/Front/Player_F.000.png new file mode 100644 index 0000000..5f3b659 Binary files /dev/null and b/Data/Img/Player/Front/Player_F.000.png differ diff --git a/Data/Img/Player/Front/Player_F.001.png b/Data/Img/Player/Front/Player_F.001.png new file mode 100644 index 0000000..1548e8a Binary files /dev/null and b/Data/Img/Player/Front/Player_F.001.png differ diff --git a/Data/Img/Player/Front/Player_F.002.png b/Data/Img/Player/Front/Player_F.002.png new file mode 100644 index 0000000..a223c70 Binary files /dev/null and b/Data/Img/Player/Front/Player_F.002.png differ diff --git a/Data/Img/Player/Front/Player_F.003.png b/Data/Img/Player/Front/Player_F.003.png new file mode 100644 index 0000000..7c40355 Binary files /dev/null and b/Data/Img/Player/Front/Player_F.003.png differ diff --git a/Data/Img/Player/Front/Player_F.txt b/Data/Img/Player/Front/Player_F.txt new file mode 100644 index 0000000..144d0a4 --- /dev/null +++ b/Data/Img/Player/Front/Player_F.txt @@ -0,0 +1,3 @@ +2; +Idle 1 1; +Walk 2 4; \ No newline at end of file diff --git a/Data/Img/Player/Player_F b/Data/Img/Player/Player_F new file mode 100644 index 0000000..e69de29 diff --git a/Data/Img/Reniesta.png b/Data/Img/Reniesta.png new file mode 100644 index 0000000..1f31089 Binary files /dev/null and b/Data/Img/Reniesta.png differ diff --git a/src/Actor/Actor.cpp b/src/Actor/Actor.cpp index d6b7165..fc8c751 100644 --- a/src/Actor/Actor.cpp +++ b/src/Actor/Actor.cpp @@ -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) { diff --git a/src/Actor/Actor.h b/src/Actor/Actor.h index 274670b..a5bfad5 100644 --- a/src/Actor/Actor.h +++ b/src/Actor/Actor.h @@ -2,12 +2,27 @@ #include #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; }; diff --git a/src/Actor/NPC.cpp b/src/Actor/NPC.cpp index 32da851..3b86515 100644 --- a/src/Actor/NPC.cpp +++ b/src/Actor/NPC.cpp @@ -1,6 +1,7 @@ #include "NPC.h" NPC::NPC(void) : Actor() { + LoadSprite("../Data/Img/Player.png"); } NPC::~NPC(void) { diff --git a/src/Actor/Player.cpp b/src/Actor/Player.cpp index 9d69817..d10be73 100644 --- a/src/Actor/Player.cpp +++ b/src/Actor/Player.cpp @@ -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) { diff --git a/src/Animation/AnimatingSprite.cpp b/src/Animation/AnimatingSprite.cpp index 7a8554a..bf4733f 100644 --- a/src/Animation/AnimatingSprite.cpp +++ b/src/Animation/AnimatingSprite.cpp @@ -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; } diff --git a/src/Animation/AnimatingSprite.h b/src/Animation/AnimatingSprite.h index 1d068a0..b14805b 100644 --- a/src/Animation/AnimatingSprite.h +++ b/src/Animation/AnimatingSprite.h @@ -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; }; diff --git a/src/Animation/AnimationSequence.cpp b/src/Animation/AnimationSequence.cpp index 573c1ec..dfc0882 100644 --- a/src/Animation/AnimationSequence.cpp +++ b/src/Animation/AnimationSequence.cpp @@ -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); } } diff --git a/src/Animation/AnimationSequence.h b/src/Animation/AnimationSequence.h index fc8de86..d1c05b1 100644 --- a/src/Animation/AnimationSequence.h +++ b/src/Animation/AnimationSequence.h @@ -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]; diff --git a/src/Main/Game.cpp b/src/Main/Game.cpp index 7e21ba5..c11ec37 100644 --- a/src/Main/Game.cpp +++ b/src/Main/Game.cpp @@ -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(); diff --git a/src/Sprite/Sprite.cpp b/src/Sprite/Sprite.cpp index 595cbf7..85c7ba6 100644 --- a/src/Sprite/Sprite.cpp +++ b/src/Sprite/Sprite.cpp @@ -101,4 +101,3 @@ void Sprite::SetTexture(Texture* texture) { this->texture = texture; this->size = Vec2((float)texture->GetWidth(), (float)texture->GetHeight()); } - diff --git a/src/Sprite/Sprite.h b/src/Sprite/Sprite.h index 1729c63..1e971cb 100644 --- a/src/Sprite/Sprite.h +++ b/src/Sprite/Sprite.h @@ -1,5 +1,7 @@ #pragma once #include +#include +#include "../System/String.h" #include "../Math/Vec2.h" #include "../Math/Rect.h" diff --git a/src/System/Debug.cpp b/src/System/Debug.cpp index bc8e673..d743bbc 100644 --- a/src/System/Debug.cpp +++ b/src/System/Debug.cpp @@ -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(×tamp) << endl; - } - } + } else { + // Log File is open, let us give it a nice time stamp. + timestamp = time(NULL); + _logFile << "Log Started: " << ctime(×tamp) << 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(×tamp) << 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(×tamp) << 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; } diff --git a/src/System/ResourceManager.h b/src/System/ResourceManager.h index 6724613..a5b706e 100644 --- a/src/System/ResourceManager.h +++ b/src/System/ResourceManager.h @@ -3,6 +3,8 @@ #include #include +#include "../System/String.h" + class Resource { public: virtual bool Load(const std::string& filename) = 0; diff --git a/src/System/String.cpp b/src/System/String.cpp index 187e1ef..3bc38ae 100644 --- a/src/System/String.cpp +++ b/src/System/String.cpp @@ -1,7 +1,10 @@ +#ifdef _WIN32 +#define "windows.h" +#endif #include #include #include - +#include #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); diff --git a/src/System/String.h b/src/System/String.h index 276f8f6..6d723ab 100644 --- a/src/System/String.h +++ b/src/System/String.h @@ -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); diff --git a/src/Texture/Texture.cpp b/src/Texture/Texture.cpp index 0842157..1a669e1 100644 --- a/src/Texture/Texture.cpp +++ b/src/Texture/Texture.cpp @@ -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; } +