diff --git a/Bin/VC10/VC10.vcxproj b/Bin/VC10/VC10.vcxproj
index 8f8f235..3336f69 100644
--- a/Bin/VC10/VC10.vcxproj
+++ b/Bin/VC10/VC10.vcxproj
@@ -86,6 +86,8 @@
+
+
@@ -131,6 +133,8 @@
+
+
diff --git a/Bin/VC10/VC10.vcxproj.filters b/Bin/VC10/VC10.vcxproj.filters
index 29f0bce..52683a1 100644
--- a/Bin/VC10/VC10.vcxproj.filters
+++ b/Bin/VC10/VC10.vcxproj.filters
@@ -43,6 +43,9 @@
{be4a8635-8a7a-45ba-8c95-06e95cdfb8e3}
+
+ {2826112d-5f07-4ee2-b4b4-2be9289c6811}
+
@@ -174,6 +177,12 @@
System
+
+ Animation
+
+
+ Animation
+
@@ -281,5 +290,11 @@
System
+
+ Animation
+
+
+ Animation
+
\ No newline at end of file
diff --git a/Data/Img/Player.png b/Data/Img/Player.png
deleted file mode 100644
index af300da..0000000
Binary files a/Data/Img/Player.png and /dev/null differ
diff --git a/Data/Img/Player/Front/Player_F.txt b/Data/Img/Player/Back/Player_B
similarity index 59%
rename from Data/Img/Player/Front/Player_F.txt
rename to Data/Img/Player/Back/Player_B
index 144d0a4..a2b15b9 100644
--- a/Data/Img/Player/Front/Player_F.txt
+++ b/Data/Img/Player/Back/Player_B
@@ -1,3 +1,3 @@
2;
Idle 1 1;
-Walk 2 4;
\ No newline at end of file
+Walk 1 4;
\ No newline at end of file
diff --git a/Data/Img/Player/Back/Player_B.000.png b/Data/Img/Player/Back/Player_B.000.png
new file mode 100644
index 0000000..e59e6f9
Binary files /dev/null and b/Data/Img/Player/Back/Player_B.000.png differ
diff --git a/Data/Img/Player/Back/Player_B.001.png b/Data/Img/Player/Back/Player_B.001.png
new file mode 100644
index 0000000..0f4b749
Binary files /dev/null and b/Data/Img/Player/Back/Player_B.001.png differ
diff --git a/Data/Img/Player/Back/Player_B.002.png b/Data/Img/Player/Back/Player_B.002.png
new file mode 100644
index 0000000..e59e6f9
Binary files /dev/null and b/Data/Img/Player/Back/Player_B.002.png differ
diff --git a/Data/Img/Player/Back/Player_B.003.png b/Data/Img/Player/Back/Player_B.003.png
new file mode 100644
index 0000000..2be6dbe
Binary files /dev/null and b/Data/Img/Player/Back/Player_B.003.png differ
diff --git a/Data/Img/Player/Front/Player_F b/Data/Img/Player/Front/Player_F
index e69de29..a2b15b9 100644
--- a/Data/Img/Player/Front/Player_F
+++ b/Data/Img/Player/Front/Player_F
@@ -0,0 +1,3 @@
+2;
+Idle 1 1;
+Walk 1 4;
\ No newline at end of file
diff --git a/Data/Img/Player/Front/Player_F.000.png b/Data/Img/Player/Front/Player_F.000.png
index 5f3b659..b63e012 100644
Binary files a/Data/Img/Player/Front/Player_F.000.png and b/Data/Img/Player/Front/Player_F.000.png differ
diff --git a/Data/Img/Player/Left/Player_L b/Data/Img/Player/Left/Player_L
new file mode 100644
index 0000000..a2b15b9
--- /dev/null
+++ b/Data/Img/Player/Left/Player_L
@@ -0,0 +1,3 @@
+2;
+Idle 1 1;
+Walk 1 4;
\ No newline at end of file
diff --git a/Data/Img/Player/Left/Player_L.000.png b/Data/Img/Player/Left/Player_L.000.png
new file mode 100644
index 0000000..66ef33e
Binary files /dev/null and b/Data/Img/Player/Left/Player_L.000.png differ
diff --git a/Data/Img/Player/Left/Player_L.001.png b/Data/Img/Player/Left/Player_L.001.png
new file mode 100644
index 0000000..cca4749
Binary files /dev/null and b/Data/Img/Player/Left/Player_L.001.png differ
diff --git a/Data/Img/Player/Left/Player_L.002.png b/Data/Img/Player/Left/Player_L.002.png
new file mode 100644
index 0000000..66ef33e
Binary files /dev/null and b/Data/Img/Player/Left/Player_L.002.png differ
diff --git a/Data/Img/Player/Left/Player_L.003.png b/Data/Img/Player/Left/Player_L.003.png
new file mode 100644
index 0000000..a460aba
Binary files /dev/null and b/Data/Img/Player/Left/Player_L.003.png differ
diff --git a/Data/Img/Player/Player_F b/Data/Img/Player/Player_F
deleted file mode 100644
index e69de29..0000000
diff --git a/Data/Img/Player/Right/Player_R b/Data/Img/Player/Right/Player_R
new file mode 100644
index 0000000..a2b15b9
--- /dev/null
+++ b/Data/Img/Player/Right/Player_R
@@ -0,0 +1,3 @@
+2;
+Idle 1 1;
+Walk 1 4;
\ No newline at end of file
diff --git a/Data/Img/Player/Right/Player_R.000.png b/Data/Img/Player/Right/Player_R.000.png
new file mode 100644
index 0000000..08d6330
Binary files /dev/null and b/Data/Img/Player/Right/Player_R.000.png differ
diff --git a/Data/Img/Player/Right/Player_R.001.png b/Data/Img/Player/Right/Player_R.001.png
new file mode 100644
index 0000000..e37dec1
Binary files /dev/null and b/Data/Img/Player/Right/Player_R.001.png differ
diff --git a/Data/Img/Player/Right/Player_R.002.png b/Data/Img/Player/Right/Player_R.002.png
new file mode 100644
index 0000000..08d6330
Binary files /dev/null and b/Data/Img/Player/Right/Player_R.002.png differ
diff --git a/Data/Img/Player/Right/Player_R.003.png b/Data/Img/Player/Right/Player_R.003.png
new file mode 100644
index 0000000..e4c2eff
Binary files /dev/null and b/Data/Img/Player/Right/Player_R.003.png differ
diff --git a/Data/Img/Reniesta.png b/Data/Img/Reniesta.png
deleted file mode 100644
index 1f31089..0000000
Binary files a/Data/Img/Reniesta.png and /dev/null differ
diff --git a/src/Actor/Actor.cpp b/src/Actor/Actor.cpp
index fc8c751..6c32dec 100644
--- a/src/Actor/Actor.cpp
+++ b/src/Actor/Actor.cpp
@@ -4,8 +4,6 @@
#include "../Sound/SoundEffect.h"
Actor::Actor(void) {
- _actor = new Sprite();
-
_stepSFX[0] = sfxManager.Load("../Data/SFX/step_cloth1.wav");
_stepSFX[1] = sfxManager.Load("../Data/SFX/step_cloth2.wav");
_stepSFX[2] = sfxManager.Load("../Data/SFX/step_cloth3.wav");
@@ -13,12 +11,15 @@ Actor::Actor(void) {
_lastStepSFXPlayed = -1;
_velocity = 4.0f;
- _direction = Front;
+ _direction = FRONT;
_preventMovement = NONE;
+ x = 0.0f;
+ y = 0.0f;
+
_actorLeft = new AnimatingSprite();
_actorRight = new AnimatingSprite();
- _actorFront = new AnimatingSprite();
+ _actorFront = new AnimatingSprite();
_actorBack = new AnimatingSprite();
}
@@ -29,44 +30,37 @@ Actor::~Actor(void) {
_stepSFX[i] = NULL;
}
}
- delete _actor;
delete _actorLeft;
delete _actorRight;
delete _actorFront;
delete _actorBack;
}
-void Actor::LoadSprite(const char* filename) {
- _actor->LoadSprite(filename);
- w = _actor->GetWidth();
- h = _actor->GetHeight();
+void Actor::LoadSprites(const String& basename) {
+ String frontFilename = String("../Data/Img/") + basename + "/Front/" + basename + "_F";
+ String leftFilename = String("../Data/Img/") + basename + "/Left/" + basename + "_L";
+ String rightFilename = String("../Data/Img/") + basename + "/Right/" + basename + "_R";
+ String backFilename = String("../Data/Img/") + basename + "/Back/" + basename + "_B";
+
+ _actorFront->LoadAnimatingSprite(frontFilename, frontFilename, 4, 1.0f / _velocity);
+ _actorLeft->LoadAnimatingSprite(leftFilename, leftFilename, 4, 1.0f / _velocity);
+ _actorRight->LoadAnimatingSprite(rightFilename, rightFilename, 4, 1.0f / _velocity);
+ _actorBack->LoadAnimatingSprite(backFilename, backFilename, 4, 1.0f / _velocity);
}
void Actor::Update(float dt) {
- float oldX = x = _actor->GetX();
- float oldY = y = _actor->GetY();
+ GetAnimation()->Update(dt);
-
- 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);
- }
+ float oldX = x;
+ float oldY = y;
// We should check for collisions now.
-
-
Move(dt);
if(x != oldX || y != oldY) {
+ GetAnimation()->SetCurrentAnimation(1);
+
if(!SoundEffect::IsPlaying(1)) {
int sfxIndex;
do {
@@ -78,12 +72,39 @@ void Actor::Update(float dt) {
_lastStepSFXPlayed = sfxIndex;
}
}
+ else {
+ GetAnimation()->SetCurrentAnimation(0);
+ }
}
void Actor::Render(void) {
- _actor->Draw();
+ GetAnimation()->Render(x, y);
}
void Actor::Render(float x, float y) {
- _actor->Draw(x, y);
+ GetAnimation()->Render(x, y);
+}
+
+AnimatingSprite* Actor::GetAnimation(void) {
+ if(_direction == LEFT) {
+ return _actorLeft;
+ }
+ else if(_direction == RIGHT) {
+ return _actorRight;
+ }
+ else if(_direction == FRONT) {
+ return _actorFront;
+ }
+ else if(_direction == BACK) {
+ return _actorBack;
+ }
+ return NULL;
+}
+
+float Actor::GetWidth(void) {
+ return GetAnimation()->GetCurrentFrameSprite()->GetWidth();
+}
+
+float Actor::GetHeight(void) {
+ return GetAnimation()->GetCurrentFrameSprite()->GetHeight();
}
diff --git a/src/Actor/Actor.h b/src/Actor/Actor.h
index a5bfad5..228c0fd 100644
--- a/src/Actor/Actor.h
+++ b/src/Actor/Actor.h
@@ -11,7 +11,7 @@ class Actor {
public:
enum Facing {
- Front,
+ FRONT,
BACK,
LEFT,
RIGHT,
@@ -26,7 +26,7 @@ public:
Actor(void);
~Actor(void);
- void LoadSprite(const char* filename);
+ void LoadSprites(const String& basename);
virtual void Update(float dt);
virtual void Render(void);
@@ -34,8 +34,8 @@ public:
float GetX(void) { return x; }
float GetY(void) { return y; }
- float GetWidth(void) { return w; }
- float GetHeight(void) { return h; }
+ float GetWidth(void);
+ float GetHeight(void);
void SetXY(float xArg, float yArg) { x = xArg; y = yArg; }
@@ -45,9 +45,10 @@ public:
protected:
virtual void Move(float dt) = 0;
+ AnimatingSprite* GetAnimation(void);
+
float _velocity;
- Sprite* _actor;
AnimatingSprite* _actorLeft;
AnimatingSprite* _actorRight;
AnimatingSprite* _actorFront;
@@ -60,9 +61,8 @@ protected:
float y;
private:
- float w;
- float h;
-
SoundEffect* _stepSFX[4];
int _lastStepSFXPlayed;
+
+ String _walkAnimationID;
};
diff --git a/src/Actor/NPC.cpp b/src/Actor/NPC.cpp
index 3b86515..c344ab1 100644
--- a/src/Actor/NPC.cpp
+++ b/src/Actor/NPC.cpp
@@ -1,7 +1,6 @@
#include "NPC.h"
NPC::NPC(void) : Actor() {
- LoadSprite("../Data/Img/Player.png");
}
NPC::~NPC(void) {
@@ -12,7 +11,7 @@ void NPC::Update(float dt) {
}
void NPC::Render(void) {
- Actor::Render(105, 125);
+ Actor::Render();
}
void NPC::Move(float dt) {
diff --git a/src/Actor/Player.cpp b/src/Actor/Player.cpp
index 1265756..11a16c5 100644
--- a/src/Actor/Player.cpp
+++ b/src/Actor/Player.cpp
@@ -2,10 +2,7 @@
#include "../IO/Input.h"
Player::Player(void) : Actor() {
- Actor::_actorFront->LoadAnimatingSprite("Player_F", "../Data/Img/Player/Front/Player_F", "../Data/Img/Player/Front/Player_F", 2, _velocity);
- //Actor::_actorFront->LoadAnimatingSprite("Player_b", "../Data/Img/Player/Front/Player_B", "../Data/Img/Player/Front/Player_B", 4, _velocity);
- //Actor::_actorFront->LoadAnimatingSprite("Player_L", "../Data/Img/Player/Front/Player_L", "../Data/Img/Player/Front/Player_L", 4, _velocity);
- //Actor::_actorFront->LoadAnimatingSprite("Player_R", "../Data/Img/Player/Front/Player_R", "../Data/Img/Player/Front/Player_R", 4, _velocity);
+ _direction = Actor::RIGHT;
}
Player::~Player(void) {
@@ -20,21 +17,21 @@ void Player::Render(void) {
}
void Player::Move(float dt) {
- if(KeyStillDown(SDLK_w) || KeyStillDown(SDLK_UP)) {
- y -= _velocity * 60 * dt;
- _actor->SetY(y);
- }
- if(KeyStillDown(SDLK_s) || KeyStillDown(SDLK_DOWN)) {
- y += _velocity * 60 * dt;
- _actor->SetY(y);
- }
if(KeyStillDown(SDLK_a) || KeyStillDown(SDLK_LEFT)) {
x -= _velocity * 60 * dt;
- _actor->SetX(x);
+ _direction = Actor::LEFT;
}
if(KeyStillDown(SDLK_d) || KeyStillDown(SDLK_RIGHT)) {
x += _velocity * 60 * dt;
- _actor->SetX(x);
+ _direction = Actor::RIGHT;
+ }
+ if(KeyStillDown(SDLK_w) || KeyStillDown(SDLK_UP)) {
+ y -= _velocity * 60 * dt;
+ _direction = Actor::BACK;
+ }
+ if(KeyStillDown(SDLK_s) || KeyStillDown(SDLK_DOWN)) {
+ y += _velocity * 60 * dt;
+ _direction = Actor::FRONT;
}
if(KeyDown(SDLK_LSHIFT)) {
diff --git a/src/Animation/AnimatingSprite.cpp b/src/Animation/AnimatingSprite.cpp
index bf4733f..ee384aa 100644
--- a/src/Animation/AnimatingSprite.cpp
+++ b/src/Animation/AnimatingSprite.cpp
@@ -8,6 +8,7 @@
AnimatingSprite::AnimatingSprite(void) {
_spriteCounter = 0;
_timer = 0;
+ _currentFrame = 1;
}
AnimatingSprite::~AnimatingSprite(void) {
@@ -33,32 +34,21 @@ void AnimatingSprite::Update(float dt) {
* the _currentFrame is set to the next valid frame.
*/
- _timer += dt;
if(_sequence) {
+ _timer += dt;
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;
+ Animation* curAnim = _sequence->GetAnimation(_currentAnimation);
+ if(_currentFrame > curAnim->frameEnd) {
+ _currentFrame = curAnim->_loopTo;
}
}
}
}
-void AnimatingSprite::LoadAnimatingSprite(const char* id, const char* filename, const char* sequence, int frames, float animationSpeed) {
+void AnimatingSprite::LoadAnimatingSprite(const char* filename, const char* sequence, int frames, float animationSpeed) {
for(int i = 0; i < frames; i++) {
String tempFilename;
tempFilename = "";
@@ -74,19 +64,16 @@ void AnimatingSprite::LoadAnimatingSprite(const char* id, const char* filename,
_sprites[_spriteCounter]->LoadSprite((const char*)tempFilename);
_spriteCounter++;
}
- _id = id;
_numberOfFrames = frames;
_animationSpeed = animationSpeed;
_sequence = new AnimationSequence(sequence);
SetCurrentAnimation(0);
}
-void AnimatingSprite::SetCurrentAnimation(const char* animation) {
+void AnimatingSprite::SetCurrentAnimation(const String& 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 b14805b..d55cb8e 100644
--- a/src/Animation/AnimatingSprite.h
+++ b/src/Animation/AnimatingSprite.h
@@ -8,28 +8,26 @@ public:
AnimatingSprite(void);
~AnimatingSprite(void);
- void SetCurrentAnimation(const char* filename);
+ void SetCurrentAnimation(const String& filename);
void SetCurrentAnimation(int index);
const char* GetCurrentAnimation(void);
int GetCurrentFrame(void) { return _currentFrame; }
int GetTotalFrames(void) { return _sequence->GetAnimation(_currentAnimation)->frameEnd; }
+ Sprite* GetCurrentFrameSprite(void) { return _sprites[_currentFrame - 1]; }
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; }
+ void LoadAnimatingSprite(const char* filename, const char* sequence, int frames, float animationSpeed);
private:
Sprite* _sprites[MAX_ANIM_FRAMES];
int _spriteCounter;
AnimationSequence* _sequence;
- const char* _id;
float _animationSpeed;
float _timer;
int _currentFrame;
diff --git a/src/Animation/AnimationSequence.cpp b/src/Animation/AnimationSequence.cpp
index dfc0882..1067168 100644
--- a/src/Animation/AnimationSequence.cpp
+++ b/src/Animation/AnimationSequence.cpp
@@ -18,12 +18,13 @@
*/
AnimationSequence::AnimationSequence(void) {
-
+ memset(_animations, 0, sizeof(_animations));
}
AnimationSequence::AnimationSequence(const char* filename) {
_numberOfFrames = 0;
_sequenceID = filename;
+ memset(_animations, 0, sizeof(_animations));
ReadFile();
}
@@ -38,28 +39,30 @@ void AnimationSequence::ReadFile(void) {
// animation array.
if(_file.Exists(_sequenceID)) {
String name;
- String loop;
char* temp;
- _file.OpenFile(_sequenceID, "r");
+ _file.OpenFile(_sequenceID, "rb");
_file.ReadBuffer(temp);
_file.CloseFile();
int counter = 0;
- _numberOfFrames = atoi(Scan(temp, counter));
+ {
+ String scanResult = Scan(temp, counter);
+ _numberOfFrames = atoi(scanResult.GetPointer());
+ }
for(int index = 0; index < _numberOfFrames; index++) {
name = "";
int startFrame = 0;
int endFrame = 0;
- loop = "";
+ int loop = 1;
name = Scan(temp, counter);
startFrame = atoi(Scan(temp, counter));
endFrame = atoi(Scan(temp, counter));
if(temp[counter - 1] == SPACE) {
- loop = Scan(temp, counter);
+ loop = atoi(Scan(temp, counter));
}
_animations[index] = new Animation();
@@ -74,7 +77,7 @@ void AnimationSequence::ReadFile(void) {
}
}
-const char* AnimationSequence::Scan(char* source, int &counter) {
+String AnimationSequence::Scan(char* source, int &counter) {
String temp;
temp = "";
bool terminate = false;
@@ -94,16 +97,16 @@ const char* AnimationSequence::Scan(char* source, int &counter) {
return temp;
}
-AnimationSequence::Animation* AnimationSequence::GetAnimation(const char* filename) {
+Animation* AnimationSequence::GetAnimation(const String& filename) {
for(int i = 0; i < _numberOfFrames; i++) {
- if(strcmp(filename, _animations[i]->_animationID) == 0) {
+ if(filename == _animations[i]->_animationID) {
return _animations[i];
}
}
return 0;
}
-AnimationSequence::Animation* AnimationSequence::GetAnimation(int index) {
+Animation* AnimationSequence::GetAnimation(int index) {
if(index < _numberOfFrames) {
return _animations[index];
}
diff --git a/src/Animation/AnimationSequence.h b/src/Animation/AnimationSequence.h
index d1c05b1..9077bb4 100644
--- a/src/Animation/AnimationSequence.h
+++ b/src/Animation/AnimationSequence.h
@@ -10,14 +10,14 @@
#define ENDOFLINE 59
#define SPACE 32
-class AnimationSequence {
- struct Animation {
- const char* _animationID;
- int frameBegin;
- int frameEnd;
- String _loopTo;
- };
+struct Animation {
+ String _animationID;
+ int frameBegin;
+ int frameEnd;
+ int _loopTo;
+};
+class AnimationSequence {
public:
AnimationSequence(void);
AnimationSequence(const char* filename);
@@ -25,10 +25,10 @@ public:
void ReadFile(void);
Animation* GetAnimation(int index);
- Animation* GetAnimation(const char* filename);
+ Animation* GetAnimation(const String& filename);
private:
- const char* Scan(char* source, int& counter);
+ String Scan(char* source, int& counter);
const char* _sequenceID;
int _numberOfFrames;
diff --git a/src/Main/Game.cpp b/src/Main/Game.cpp
index c11ec37..09b69b0 100644
--- a/src/Main/Game.cpp
+++ b/src/Main/Game.cpp
@@ -43,6 +43,9 @@ bool Game::Init(void) {
_level->Load("../Data/Map/Ugly.tmx");
_level->PlayBGM();
+ _player->LoadSprites("Player");
+ _NPC->LoadSprites("Player");
+
// Return success.
return true;
}
diff --git a/src/Sound/SoundEffect.cpp b/src/Sound/SoundEffect.cpp
index 7647ddd..32d443c 100644
--- a/src/Sound/SoundEffect.cpp
+++ b/src/Sound/SoundEffect.cpp
@@ -29,5 +29,5 @@ void SoundEffect::Stop(int channel) {
}
bool SoundEffect::IsPlaying(int channel) {
- return Mix_Playing(channel);
+ return Mix_Playing(channel) == 1;
}
\ No newline at end of file
diff --git a/src/Sprite/Sprite.cpp b/src/Sprite/Sprite.cpp
index 85c7ba6..c84a766 100644
--- a/src/Sprite/Sprite.cpp
+++ b/src/Sprite/Sprite.cpp
@@ -24,8 +24,8 @@ void Sprite::Draw() const {
}
void Sprite::Draw(float x, float y) {
- DrawRegion(Rect(0.0f, 0.0f, (float)texture->GetWidth(), (float)texture->GetHeight()));
SetPosition(Vec2(x, y));
+ DrawRegion(Rect(0.0f, 0.0f, (float)texture->GetWidth(), (float)texture->GetHeight()));
}
void Sprite::DrawRegion(const Rect& src) const {
diff --git a/src/System/FileReader.cpp b/src/System/FileReader.cpp
index 8c12d62..d0c5277 100644
--- a/src/System/FileReader.cpp
+++ b/src/System/FileReader.cpp
@@ -14,7 +14,7 @@ FileReader::~FileReader(void) {
bool FileReader::Exists(const char* filename) {
// Check to see if _filename is existent in memory,
- _file = fopen(filename, "r");
+ _file = fopen(filename, "rb");
if(_file) {
// Close the file we have.
@@ -64,7 +64,7 @@ void FileReader::Write(const int buffer) {
}
void FileReader::Read(const int &value) {
- if((_file) && (_accessType == "r")) {
+ if((_file) && (_accessType == "rb")) {
fscanf(_file, "%i", &value);
} else {
// _filename does not exist or we have the wrong accessType.
@@ -85,13 +85,14 @@ void FileReader::WriteBuffer(const char* buffer, int count) {
}
void FileReader::ReadBuffer(char* &buffer) {
- if((_file) && (_accessType == "r")) {
+ if((_file) && (_accessType == "rb")) {
size_t size = 0;
fseek(_file, 0, SEEK_END);
size = ftell(_file);
rewind(_file);
- buffer = (char*)malloc(sizeof(char)* size);
+ buffer = (char*)malloc(sizeof(char)* size + sizeof(char));
+ buffer[size] = 0;
if(buffer != NULL) {
fread(buffer, 1, size, _file);
diff --git a/src/System/String.cpp b/src/System/String.cpp
index 3bc38ae..6a626d3 100644
--- a/src/System/String.cpp
+++ b/src/System/String.cpp
@@ -1,6 +1,3 @@
-#ifdef _WIN32
-#define "windows.h"
-#endif
#include
#include
#include
@@ -9,85 +6,110 @@
#define _CRT_SECURE_NO_WARNINGS
String::String(void) {
-
+ _string = new char[1];
+ _string[0] = 0;
+ _length = 0;
}
String::~String(void) {
-
+ if(_string) {
+ delete[] _string;
+ _string = NULL;
+ _length = 0;
+ }
}
String::String(char letter) {
// Char to string conversion.
- char temp[1];
- temp[0] = letter;
- strcpy(_string, temp);
+ _string = new char[2];
+ _string[0] = letter;
+ _string[1] = 0;
+ _length = 1;
}
-String::String(char* text) {
- // char* or char array.
- strcpy(_string, text);
+String::String(const char* text) {
+ _length = strlen(text);
+ _string = new char[_length + 1];
+ memcpy(_string, text, _length);
+ _string[_length] = 0;
}
-String::String(String& text) {
- // Copy the String.
- strcpy(_string, text.GetPointer());
+String::String(const String& text) {
+ _length = text._length;
+ _string = new char[_length + 1];
+ memcpy(_string, text._string, _length);
+ _string[_length] = 0;
}
-const char* String::GetPointer(void) {
+const char* String::GetPointer(void) const {
// Return a pointer to the memory address of the string.
return _string;
}
int String::Length(void) {
// Return the length of the string.
- return strlen(_string);
+ return _length;
}
void String::Format(const char* format, ...) {
- char temp[256];
+ if(_string) {
+ delete[] _string;
+ }
+
+ _length = 4095;
+ _string = new char[_length + 1];
+ memset(_string, 0, _length + 1);
+
va_list vlist;
va_start(vlist, format);
#ifdef _WIN32
- vsprintf_s(&temp[0], 256, format, vlist);
+ vsprintf_s(_string, _length, format, vlist);
#else
- vsnprintf(&temp[0], 256, format, vlist);
+ vsnprintf(_string, _length, 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);
- char temp[1] = "";
- temp[0] = value;
- strncat(_string, temp, 1);
+ char* oldString = _string;
+ int oldLength = _length;
+ _length = oldLength + 1;
+ _string = new char[_length + 1];
+ _string[_length] = 0;
+ memcpy(_string, oldString, oldLength);
+ _string[oldLength] = value;
+ delete[] oldString;
}
void String::Concatenate(const char* value) {
- // Concatenate a char* or array to the end of the string.
- assert(strlen(_string) + strlen(value) < MAX_STRING_LEN);
- strcat(_string, value);
+ char* oldString = _string;
+ int oldLength = _length;
+ int valueLength = strlen(value);
+ _length = oldLength + valueLength;
+ _string = new char[_length + 1];
+ _string[_length] = 0;
+ memcpy(_string, oldString, oldLength);
+ memcpy(_string + oldLength, value, valueLength);
+ delete[] oldString;
}
-void String::Concatenate(String& value) {
- assert(strlen(_string) + strlen(value) < MAX_STRING_LEN);
- strcat(_string, value.GetPointer());
+void String::Concatenate(const String& value) {
+ Concatenate(value.GetPointer());
}
// Operator overloads, can't be bothered to comment
// them, use your brain. :)
String& String::operator=(const char* value) {
- assert(strlen(value) < MAX_STRING_LEN);
- strcpy(_string, value);
+ _length = strlen(value);
+ _string = new char[_length + 1];
+ _string[_length] = 0;
+ memcpy(_string, value, _length);
return *this;
}
-String& String::operator=(String& value) {
- assert(strlen(value) < MAX_STRING_LEN);
- strcpy(_string, value.GetPointer());
+String& String::operator=(const String& value) {
+ *this = value.GetPointer();
return *this;
}
@@ -98,7 +120,7 @@ bool String::operator==(const char* value) const {
return false;
}
-bool String::operator==(String& value) const {
+bool String::operator==(const String& value) const {
if(strcmp(_string, value.GetPointer()) == 0) {
return true;
}
@@ -119,6 +141,12 @@ bool String::operator!=(const char* value) const {
return false;
}
+String String::operator+(const String& value) const {
+ String copy(*this);
+ copy.Concatenate(value);
+ return copy;
+}
+
String::operator const char*() const {
return _string;
}
diff --git a/src/System/String.h b/src/System/String.h
index 6d723ab..2465e0b 100644
--- a/src/System/String.h
+++ b/src/System/String.h
@@ -1,18 +1,17 @@
#pragma once
#define _CRT_SECURE_NO_WARNINGS
-#define MAX_STRING_LEN 255
class String {
public:
String(void);
String(char letter);
- String(char text[]);
- String(String& text);
+ String(const char* text);
+ String(const String& text);
~String(void);
- const char* GetPointer(void);
+ const char* GetPointer(void) const;
void Concatenate(const char* value);
- void Concatenate(String& value);
+ void Concatenate(const String& value);
void Concatenate(char value);
int Length(void);
@@ -20,15 +19,18 @@ public:
// Operator overloads.
String& operator=(const char* value);
- String& operator=(String& value);
+ String& operator=(const String& value);
bool operator==(const char* value) const;
- bool operator==(String& value) const;
+ bool operator==(const String& value) const;
bool operator!=(String& value) const;
bool operator!=(const char* value) const;
+ String operator+(const String& value) const;
+
operator const char*() const;
private:
- char _string[MAX_STRING_LEN];
+ char* _string;
+ int _length;
};