[Fix] Animation was farked!

[Fix] Strings are now allocated from the heap.
This commit is contained in:
Tamir Atias 2012-04-17 03:58:42 +03:00
parent d4ede47353
commit e35cb64246
36 changed files with 216 additions and 149 deletions

View File

@ -86,6 +86,8 @@
<ClInclude Include="..\..\src\Actor\Actor.h" /> <ClInclude Include="..\..\src\Actor\Actor.h" />
<ClInclude Include="..\..\src\Actor\NPC.h" /> <ClInclude Include="..\..\src\Actor\NPC.h" />
<ClInclude Include="..\..\src\Actor\Player.h" /> <ClInclude Include="..\..\src\Actor\Player.h" />
<ClInclude Include="..\..\src\Animation\AnimatingSprite.h" />
<ClInclude Include="..\..\src\Animation\AnimationSequence.h" />
<ClInclude Include="..\..\src\Collision\AABB.h" /> <ClInclude Include="..\..\src\Collision\AABB.h" />
<ClInclude Include="..\..\src\Font\Font.h" /> <ClInclude Include="..\..\src\Font\Font.h" />
<ClInclude Include="..\..\src\Global\Constants.h" /> <ClInclude Include="..\..\src\Global\Constants.h" />
@ -131,6 +133,8 @@
<ClCompile Include="..\..\src\Actor\Actor.cpp" /> <ClCompile Include="..\..\src\Actor\Actor.cpp" />
<ClCompile Include="..\..\src\Actor\NPC.cpp" /> <ClCompile Include="..\..\src\Actor\NPC.cpp" />
<ClCompile Include="..\..\src\Actor\Player.cpp" /> <ClCompile Include="..\..\src\Actor\Player.cpp" />
<ClCompile Include="..\..\src\Animation\AnimatingSprite.cpp" />
<ClCompile Include="..\..\src\Animation\AnimationSequence.cpp" />
<ClCompile Include="..\..\src\Collision\AABB.cpp" /> <ClCompile Include="..\..\src\Collision\AABB.cpp" />
<ClCompile Include="..\..\src\Font\Font.cpp" /> <ClCompile Include="..\..\src\Font\Font.cpp" />
<ClCompile Include="..\..\src\Global\Constants.cpp" /> <ClCompile Include="..\..\src\Global\Constants.cpp" />

View File

@ -43,6 +43,9 @@
<Filter Include="Font"> <Filter Include="Font">
<UniqueIdentifier>{be4a8635-8a7a-45ba-8c95-06e95cdfb8e3}</UniqueIdentifier> <UniqueIdentifier>{be4a8635-8a7a-45ba-8c95-06e95cdfb8e3}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="Animation">
<UniqueIdentifier>{2826112d-5f07-4ee2-b4b4-2be9289c6811}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\src\Main\Game.h"> <ClInclude Include="..\..\src\Main\Game.h">
@ -174,6 +177,12 @@
<ClInclude Include="..\..\src\System\String.h"> <ClInclude Include="..\..\src\System\String.h">
<Filter>System</Filter> <Filter>System</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\Animation\AnimatingSprite.h">
<Filter>Animation</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Animation\AnimationSequence.h">
<Filter>Animation</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\src\Main\main.cpp"> <ClCompile Include="..\..\src\Main\main.cpp">
@ -281,5 +290,11 @@
<ClCompile Include="..\..\src\System\String.cpp"> <ClCompile Include="..\..\src\System\String.cpp">
<Filter>System</Filter> <Filter>System</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\Animation\AnimationSequence.cpp">
<Filter>Animation</Filter>
</ClCompile>
<ClCompile Include="..\..\src\Animation\AnimatingSprite.cpp">
<Filter>Animation</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 KiB

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 684 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 625 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 684 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 636 B

View File

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 845 B

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 755 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 717 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 755 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 787 B

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 771 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 729 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 771 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 786 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.8 KiB

View File

@ -4,8 +4,6 @@
#include "../Sound/SoundEffect.h" #include "../Sound/SoundEffect.h"
Actor::Actor(void) { Actor::Actor(void) {
_actor = new Sprite();
_stepSFX[0] = sfxManager.Load("../Data/SFX/step_cloth1.wav"); _stepSFX[0] = sfxManager.Load("../Data/SFX/step_cloth1.wav");
_stepSFX[1] = sfxManager.Load("../Data/SFX/step_cloth2.wav"); _stepSFX[1] = sfxManager.Load("../Data/SFX/step_cloth2.wav");
_stepSFX[2] = sfxManager.Load("../Data/SFX/step_cloth3.wav"); _stepSFX[2] = sfxManager.Load("../Data/SFX/step_cloth3.wav");
@ -13,12 +11,15 @@ Actor::Actor(void) {
_lastStepSFXPlayed = -1; _lastStepSFXPlayed = -1;
_velocity = 4.0f; _velocity = 4.0f;
_direction = Front; _direction = FRONT;
_preventMovement = NONE; _preventMovement = NONE;
x = 0.0f;
y = 0.0f;
_actorLeft = new AnimatingSprite(); _actorLeft = new AnimatingSprite();
_actorRight = new AnimatingSprite(); _actorRight = new AnimatingSprite();
_actorFront = new AnimatingSprite(); _actorFront = new AnimatingSprite();
_actorBack = new AnimatingSprite(); _actorBack = new AnimatingSprite();
} }
@ -29,44 +30,37 @@ Actor::~Actor(void) {
_stepSFX[i] = NULL; _stepSFX[i] = NULL;
} }
} }
delete _actor;
delete _actorLeft; delete _actorLeft;
delete _actorRight; delete _actorRight;
delete _actorFront; delete _actorFront;
delete _actorBack; delete _actorBack;
} }
void Actor::LoadSprite(const char* filename) { void Actor::LoadSprites(const String& basename) {
_actor->LoadSprite(filename); String frontFilename = String("../Data/Img/") + basename + "/Front/" + basename + "_F";
w = _actor->GetWidth(); String leftFilename = String("../Data/Img/") + basename + "/Left/" + basename + "_L";
h = _actor->GetHeight(); 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) { void Actor::Update(float dt) {
float oldX = x = _actor->GetX(); GetAnimation()->Update(dt);
float oldY = y = _actor->GetY();
float oldX = x;
if(_direction == LEFT) { float oldY = y;
_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. // We should check for collisions now.
Move(dt); Move(dt);
if(x != oldX || y != oldY) { if(x != oldX || y != oldY) {
GetAnimation()->SetCurrentAnimation(1);
if(!SoundEffect::IsPlaying(1)) { if(!SoundEffect::IsPlaying(1)) {
int sfxIndex; int sfxIndex;
do { do {
@ -78,12 +72,39 @@ void Actor::Update(float dt) {
_lastStepSFXPlayed = sfxIndex; _lastStepSFXPlayed = sfxIndex;
} }
} }
else {
GetAnimation()->SetCurrentAnimation(0);
}
} }
void Actor::Render(void) { void Actor::Render(void) {
_actor->Draw(); GetAnimation()->Render(x, y);
} }
void Actor::Render(float x, float 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();
} }

View File

@ -11,7 +11,7 @@ class Actor {
public: public:
enum Facing { enum Facing {
Front, FRONT,
BACK, BACK,
LEFT, LEFT,
RIGHT, RIGHT,
@ -26,7 +26,7 @@ public:
Actor(void); Actor(void);
~Actor(void); ~Actor(void);
void LoadSprite(const char* filename); void LoadSprites(const String& basename);
virtual void Update(float dt); virtual void Update(float dt);
virtual void Render(void); virtual void Render(void);
@ -34,8 +34,8 @@ public:
float GetX(void) { return x; } float GetX(void) { return x; }
float GetY(void) { return y; } float GetY(void) { return y; }
float GetWidth(void) { return w; } float GetWidth(void);
float GetHeight(void) { return h; } float GetHeight(void);
void SetXY(float xArg, float yArg) { x = xArg; y = yArg; } void SetXY(float xArg, float yArg) { x = xArg; y = yArg; }
@ -45,9 +45,10 @@ public:
protected: protected:
virtual void Move(float dt) = 0; virtual void Move(float dt) = 0;
AnimatingSprite* GetAnimation(void);
float _velocity; float _velocity;
Sprite* _actor;
AnimatingSprite* _actorLeft; AnimatingSprite* _actorLeft;
AnimatingSprite* _actorRight; AnimatingSprite* _actorRight;
AnimatingSprite* _actorFront; AnimatingSprite* _actorFront;
@ -60,9 +61,8 @@ protected:
float y; float y;
private: private:
float w;
float h;
SoundEffect* _stepSFX[4]; SoundEffect* _stepSFX[4];
int _lastStepSFXPlayed; int _lastStepSFXPlayed;
String _walkAnimationID;
}; };

View File

@ -1,7 +1,6 @@
#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) {
@ -12,7 +11,7 @@ void NPC::Update(float dt) {
} }
void NPC::Render(void) { void NPC::Render(void) {
Actor::Render(105, 125); Actor::Render();
} }
void NPC::Move(float dt) { void NPC::Move(float dt) {

View File

@ -2,10 +2,7 @@
#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/Front/Player_F", 2, _velocity); _direction = Actor::RIGHT;
//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);
} }
Player::~Player(void) { Player::~Player(void) {
@ -20,21 +17,21 @@ void Player::Render(void) {
} }
void Player::Move(float dt) { 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)) { if(KeyStillDown(SDLK_a) || KeyStillDown(SDLK_LEFT)) {
x -= _velocity * 60 * dt; x -= _velocity * 60 * dt;
_actor->SetX(x); _direction = Actor::LEFT;
} }
if(KeyStillDown(SDLK_d) || KeyStillDown(SDLK_RIGHT)) { if(KeyStillDown(SDLK_d) || KeyStillDown(SDLK_RIGHT)) {
x += _velocity * 60 * dt; 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)) { if(KeyDown(SDLK_LSHIFT)) {

View File

@ -8,6 +8,7 @@
AnimatingSprite::AnimatingSprite(void) { AnimatingSprite::AnimatingSprite(void) {
_spriteCounter = 0; _spriteCounter = 0;
_timer = 0; _timer = 0;
_currentFrame = 1;
} }
AnimatingSprite::~AnimatingSprite(void) { AnimatingSprite::~AnimatingSprite(void) {
@ -33,32 +34,21 @@ void AnimatingSprite::Update(float dt) {
* the _currentFrame is set to the next valid frame. * the _currentFrame is set to the next valid frame.
*/ */
_timer += dt;
if(_sequence) { if(_sequence) {
_timer += dt;
if(_timer > _animationSpeed) { if(_timer > _animationSpeed) {
_timer = 0; _timer = 0;
_currentFrame++; _currentFrame++;
if(_currentFrame > _sequence->GetAnimation(_currentAnimation)->frameEnd) { Animation* curAnim = _sequence->GetAnimation(_currentAnimation);
if(_sequence->GetAnimation(_currentAnimation)->_loopTo != "") { if(_currentFrame > curAnim->frameEnd) {
SetCurrentAnimation(_sequence->GetAnimation(_currentAnimation)->_loopTo); _currentFrame = curAnim->_loopTo;
} else {
_currentFrame = _sequence->GetAnimation(_currentAnimation)->frameBegin;
}
}
}
} else {
if(_timer > _animationSpeed) {
_timer = 0;
_currentFrame = 0;
if(_currentFrame > _numberOfFrames) {
_currentFrame = 1;
} }
} }
} }
} }
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++) { for(int i = 0; i < frames; i++) {
String tempFilename; String tempFilename;
tempFilename = ""; tempFilename = "";
@ -74,19 +64,16 @@ void AnimatingSprite::LoadAnimatingSprite(const char* id, const char* filename,
_sprites[_spriteCounter]->LoadSprite((const char*)tempFilename); _sprites[_spriteCounter]->LoadSprite((const char*)tempFilename);
_spriteCounter++; _spriteCounter++;
} }
_id = id;
_numberOfFrames = frames; _numberOfFrames = frames;
_animationSpeed = animationSpeed; _animationSpeed = animationSpeed;
_sequence = new AnimationSequence(sequence); _sequence = new AnimationSequence(sequence);
SetCurrentAnimation(0); SetCurrentAnimation(0);
} }
void AnimatingSprite::SetCurrentAnimation(const char* animation) { void AnimatingSprite::SetCurrentAnimation(const String& animation) {
_currentAnimation = _sequence->GetAnimation(animation)->_animationID; _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; _currentAnimation = _sequence->GetAnimation(index)->_animationID;
_currentFrame = _sequence->GetAnimation(index)->frameBegin;
} }

View File

@ -8,28 +8,26 @@ public:
AnimatingSprite(void); AnimatingSprite(void);
~AnimatingSprite(void); ~AnimatingSprite(void);
void SetCurrentAnimation(const char* filename); void SetCurrentAnimation(const String& filename);
void SetCurrentAnimation(int index); void SetCurrentAnimation(int index);
const char* GetCurrentAnimation(void); const char* GetCurrentAnimation(void);
int GetCurrentFrame(void) { return _currentFrame; } int GetCurrentFrame(void) { return _currentFrame; }
int GetTotalFrames(void) { return _sequence->GetAnimation(_currentAnimation)->frameEnd; } int GetTotalFrames(void) { return _sequence->GetAnimation(_currentAnimation)->frameEnd; }
Sprite* GetCurrentFrameSprite(void) { return _sprites[_currentFrame - 1]; }
void Update(float dt); void Update(float dt);
void Render(void); void Render(void);
void Render(float x, float y); void Render(float x, float y);
void LoadAnimatingSprite(const char* id, const char* filename, const char* sequence, int frames, float animationSpeed); void LoadAnimatingSprite(const char* filename, const char* sequence, int frames, float animationSpeed);
const char* GetID(void) { return _id; }
private: private:
Sprite* _sprites[MAX_ANIM_FRAMES]; Sprite* _sprites[MAX_ANIM_FRAMES];
int _spriteCounter; int _spriteCounter;
AnimationSequence* _sequence; AnimationSequence* _sequence;
const char* _id;
float _animationSpeed; float _animationSpeed;
float _timer; float _timer;
int _currentFrame; int _currentFrame;

View File

@ -18,12 +18,13 @@
*/ */
AnimationSequence::AnimationSequence(void) { AnimationSequence::AnimationSequence(void) {
memset(_animations, 0, sizeof(_animations));
} }
AnimationSequence::AnimationSequence(const char* filename) { AnimationSequence::AnimationSequence(const char* filename) {
_numberOfFrames = 0; _numberOfFrames = 0;
_sequenceID = filename; _sequenceID = filename;
memset(_animations, 0, sizeof(_animations));
ReadFile(); ReadFile();
} }
@ -38,28 +39,30 @@ void AnimationSequence::ReadFile(void) {
// animation array. // animation array.
if(_file.Exists(_sequenceID)) { if(_file.Exists(_sequenceID)) {
String name; String name;
String loop;
char* temp; char* temp;
_file.OpenFile(_sequenceID, "r"); _file.OpenFile(_sequenceID, "rb");
_file.ReadBuffer(temp); _file.ReadBuffer(temp);
_file.CloseFile(); _file.CloseFile();
int counter = 0; int counter = 0;
_numberOfFrames = atoi(Scan(temp, counter)); {
String scanResult = Scan(temp, counter);
_numberOfFrames = atoi(scanResult.GetPointer());
}
for(int index = 0; index < _numberOfFrames; index++) { for(int index = 0; index < _numberOfFrames; index++) {
name = ""; name = "";
int startFrame = 0; int startFrame = 0;
int endFrame = 0; int endFrame = 0;
loop = ""; int loop = 1;
name = Scan(temp, counter); name = Scan(temp, counter);
startFrame = atoi(Scan(temp, counter)); startFrame = atoi(Scan(temp, counter));
endFrame = atoi(Scan(temp, counter)); endFrame = atoi(Scan(temp, counter));
if(temp[counter - 1] == SPACE) { if(temp[counter - 1] == SPACE) {
loop = Scan(temp, counter); loop = atoi(Scan(temp, counter));
} }
_animations[index] = new Animation(); _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; String temp;
temp = ""; temp = "";
bool terminate = false; bool terminate = false;
@ -94,16 +97,16 @@ const char* AnimationSequence::Scan(char* source, int &counter) {
return temp; return temp;
} }
AnimationSequence::Animation* AnimationSequence::GetAnimation(const char* filename) { Animation* AnimationSequence::GetAnimation(const String& filename) {
for(int i = 0; i < _numberOfFrames; i++) { for(int i = 0; i < _numberOfFrames; i++) {
if(strcmp(filename, _animations[i]->_animationID) == 0) { if(filename == _animations[i]->_animationID) {
return _animations[i]; return _animations[i];
} }
} }
return 0; return 0;
} }
AnimationSequence::Animation* AnimationSequence::GetAnimation(int index) { Animation* AnimationSequence::GetAnimation(int index) {
if(index < _numberOfFrames) { if(index < _numberOfFrames) {
return _animations[index]; return _animations[index];
} }

View File

@ -10,14 +10,14 @@
#define ENDOFLINE 59 #define ENDOFLINE 59
#define SPACE 32 #define SPACE 32
class AnimationSequence { struct Animation {
struct Animation { String _animationID;
const char* _animationID; int frameBegin;
int frameBegin; int frameEnd;
int frameEnd; int _loopTo;
String _loopTo; };
};
class AnimationSequence {
public: public:
AnimationSequence(void); AnimationSequence(void);
AnimationSequence(const char* filename); AnimationSequence(const char* filename);
@ -25,10 +25,10 @@ public:
void ReadFile(void); void ReadFile(void);
Animation* GetAnimation(int index); Animation* GetAnimation(int index);
Animation* GetAnimation(const char* filename); Animation* GetAnimation(const String& filename);
private: private:
const char* Scan(char* source, int& counter); String Scan(char* source, int& counter);
const char* _sequenceID; const char* _sequenceID;
int _numberOfFrames; int _numberOfFrames;

View File

@ -43,6 +43,9 @@ bool Game::Init(void) {
_level->Load("../Data/Map/Ugly.tmx"); _level->Load("../Data/Map/Ugly.tmx");
_level->PlayBGM(); _level->PlayBGM();
_player->LoadSprites("Player");
_NPC->LoadSprites("Player");
// Return success. // Return success.
return true; return true;
} }

View File

@ -29,5 +29,5 @@ void SoundEffect::Stop(int channel) {
} }
bool SoundEffect::IsPlaying(int channel) { bool SoundEffect::IsPlaying(int channel) {
return Mix_Playing(channel); return Mix_Playing(channel) == 1;
} }

View File

@ -24,8 +24,8 @@ void Sprite::Draw() const {
} }
void Sprite::Draw(float x, float y) { void Sprite::Draw(float x, float y) {
DrawRegion(Rect(0.0f, 0.0f, (float)texture->GetWidth(), (float)texture->GetHeight()));
SetPosition(Vec2(x, y)); SetPosition(Vec2(x, y));
DrawRegion(Rect(0.0f, 0.0f, (float)texture->GetWidth(), (float)texture->GetHeight()));
} }
void Sprite::DrawRegion(const Rect& src) const { void Sprite::DrawRegion(const Rect& src) const {

View File

@ -14,7 +14,7 @@ FileReader::~FileReader(void) {
bool FileReader::Exists(const char* filename) { bool FileReader::Exists(const char* filename) {
// Check to see if _filename is existent in memory, // Check to see if _filename is existent in memory,
_file = fopen(filename, "r"); _file = fopen(filename, "rb");
if(_file) { if(_file) {
// Close the file we have. // Close the file we have.
@ -64,7 +64,7 @@ void FileReader::Write(const int buffer) {
} }
void FileReader::Read(const int &value) { void FileReader::Read(const int &value) {
if((_file) && (_accessType == "r")) { if((_file) && (_accessType == "rb")) {
fscanf(_file, "%i", &value); fscanf(_file, "%i", &value);
} else { } else {
// _filename does not exist or we have the wrong accessType. // _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) { void FileReader::ReadBuffer(char* &buffer) {
if((_file) && (_accessType == "r")) { if((_file) && (_accessType == "rb")) {
size_t size = 0; size_t size = 0;
fseek(_file, 0, SEEK_END); fseek(_file, 0, SEEK_END);
size = ftell(_file); size = ftell(_file);
rewind(_file); rewind(_file);
buffer = (char*)malloc(sizeof(char)* size); buffer = (char*)malloc(sizeof(char)* size + sizeof(char));
buffer[size] = 0;
if(buffer != NULL) { if(buffer != NULL) {
fread(buffer, 1, size, _file); fread(buffer, 1, size, _file);

View File

@ -1,6 +1,3 @@
#ifdef _WIN32
#define "windows.h"
#endif
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
@ -9,85 +6,110 @@
#define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS
String::String(void) { String::String(void) {
_string = new char[1];
_string[0] = 0;
_length = 0;
} }
String::~String(void) { String::~String(void) {
if(_string) {
delete[] _string;
_string = NULL;
_length = 0;
}
} }
String::String(char letter) { String::String(char letter) {
// Char to string conversion. // Char to string conversion.
char temp[1]; _string = new char[2];
temp[0] = letter; _string[0] = letter;
strcpy(_string, temp); _string[1] = 0;
_length = 1;
} }
String::String(char* text) { String::String(const char* text) {
// char* or char array. _length = strlen(text);
strcpy(_string, text); _string = new char[_length + 1];
memcpy(_string, text, _length);
_string[_length] = 0;
} }
String::String(String& text) { String::String(const String& text) {
// Copy the String. _length = text._length;
strcpy(_string, text.GetPointer()); _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 a pointer to the memory address of the string.
return _string; return _string;
} }
int String::Length(void) { int String::Length(void) {
// Return the length of the string. // Return the length of the string.
return strlen(_string); return _length;
} }
void String::Format(const char* format, ...) { 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_list vlist;
va_start(vlist, format); va_start(vlist, format);
#ifdef _WIN32 #ifdef _WIN32
vsprintf_s(&temp[0], 256, format, vlist); vsprintf_s(_string, _length, format, vlist);
#else #else
vsnprintf(&temp[0], 256, format, vlist); vsnprintf(_string, _length, format, vlist);
#endif #endif
va_end(vlist); 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. char* oldString = _string;
assert(strlen(_string) + 1 < MAX_STRING_LEN); int oldLength = _length;
char temp[1] = ""; _length = oldLength + 1;
temp[0] = value; _string = new char[_length + 1];
strncat(_string, temp, 1); _string[_length] = 0;
memcpy(_string, oldString, oldLength);
_string[oldLength] = value;
delete[] oldString;
} }
void String::Concatenate(const char* value) { void String::Concatenate(const char* value) {
// Concatenate a char* or array to the end of the string. char* oldString = _string;
assert(strlen(_string) + strlen(value) < MAX_STRING_LEN); int oldLength = _length;
strcat(_string, value); 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) { void String::Concatenate(const String& value) {
assert(strlen(_string) + strlen(value) < MAX_STRING_LEN); Concatenate(value.GetPointer());
strcat(_string, value.GetPointer());
} }
// Operator overloads, can't be bothered to comment // Operator overloads, can't be bothered to comment
// them, use your brain. :) // them, use your brain. :)
String& String::operator=(const char* value) { String& String::operator=(const char* value) {
assert(strlen(value) < MAX_STRING_LEN); _length = strlen(value);
strcpy(_string, value); _string = new char[_length + 1];
_string[_length] = 0;
memcpy(_string, value, _length);
return *this; return *this;
} }
String& String::operator=(String& value) { String& String::operator=(const String& value) {
assert(strlen(value) < MAX_STRING_LEN); *this = value.GetPointer();
strcpy(_string, value.GetPointer());
return *this; return *this;
} }
@ -98,7 +120,7 @@ bool String::operator==(const char* value) const {
return false; return false;
} }
bool String::operator==(String& value) const { bool String::operator==(const String& value) const {
if(strcmp(_string, value.GetPointer()) == 0) { if(strcmp(_string, value.GetPointer()) == 0) {
return true; return true;
} }
@ -119,6 +141,12 @@ bool String::operator!=(const char* value) const {
return false; return false;
} }
String String::operator+(const String& value) const {
String copy(*this);
copy.Concatenate(value);
return copy;
}
String::operator const char*() const { String::operator const char*() const {
return _string; return _string;
} }

View File

@ -1,18 +1,17 @@
#pragma once #pragma once
#define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS
#define MAX_STRING_LEN 255
class String { class String {
public: public:
String(void); String(void);
String(char letter); String(char letter);
String(char text[]); String(const char* text);
String(String& text); String(const String& text);
~String(void); ~String(void);
const char* GetPointer(void); const char* GetPointer(void) const;
void Concatenate(const char* value); void Concatenate(const char* value);
void Concatenate(String& value); void Concatenate(const String& value);
void Concatenate(char value); void Concatenate(char value);
int Length(void); int Length(void);
@ -20,15 +19,18 @@ public:
// Operator overloads. // Operator overloads.
String& operator=(const char* value); String& operator=(const char* value);
String& operator=(String& value); String& operator=(const String& value);
bool operator==(const char* value) const; bool operator==(const char* value) const;
bool operator==(String& value) const; bool operator==(const String& value) const;
bool operator!=(String& value) const; bool operator!=(String& value) const;
bool operator!=(const char* value) const; bool operator!=(const char* value) const;
String operator+(const String& value) const;
operator const char*() const; operator const char*() const;
private: private:
char _string[MAX_STRING_LEN]; char* _string;
int _length;
}; };