[Add] Sound effects.

[Add] Player walking sounds.
This commit is contained in:
Tamir Atias 2012-04-12 19:50:19 +03:00
parent f2d6345a61
commit 7aa8d9adb2
10 changed files with 100 additions and 2 deletions

View File

@ -83,6 +83,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\..\src\Actor\NPC.h" />
<ClInclude Include="..\..\src\Actor\Player.h" />
<ClInclude Include="..\..\src\Collision\AABB.h" />
<ClInclude Include="..\..\src\Global\Constants.h" />
@ -101,6 +102,7 @@
<ClInclude Include="..\..\src\Math\Timer.h" />
<ClInclude Include="..\..\src\Math\Vec2.h" />
<ClInclude Include="..\..\Src\Sound\Music.h" />
<ClInclude Include="..\..\Src\Sound\SoundEffect.h" />
<ClInclude Include="..\..\src\Sprite\Sprite.h" />
<ClInclude Include="..\..\src\System\Debug.h" />
<ClInclude Include="..\..\src\System\ResourceManager.h" />
@ -122,6 +124,7 @@
<ClInclude Include="..\..\src\TMXParser\TmxUtil.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\src\Actor\NPC.cpp" />
<ClCompile Include="..\..\src\Actor\Player.cpp" />
<ClCompile Include="..\..\src\Collision\AABB.cpp" />
<ClCompile Include="..\..\src\Global\Constants.cpp" />
@ -136,6 +139,7 @@
<ClCompile Include="..\..\src\Math\Timer.cpp" />
<ClCompile Include="..\..\src\Math\Vec2.cpp" />
<ClCompile Include="..\..\Src\Sound\Music.cpp" />
<ClCompile Include="..\..\Src\Sound\SoundEffect.cpp" />
<ClCompile Include="..\..\src\Sprite\Sprite.cpp" />
<ClCompile Include="..\..\src\System\Debug.cpp" />
<ClCompile Include="..\..\src\Texture\Texture.cpp" />

View File

@ -153,6 +153,12 @@
<ClInclude Include="..\..\Src\Sound\Music.h">
<Filter>Sound</Filter>
</ClInclude>
<ClInclude Include="..\..\Src\Sound\SoundEffect.h">
<Filter>Sound</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Actor\NPC.h">
<Filter>Actor</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\src\Main\main.cpp">
@ -242,5 +248,11 @@
<ClCompile Include="..\..\Src\Sound\Music.cpp">
<Filter>Sound</Filter>
</ClCompile>
<ClCompile Include="..\..\Src\Sound\SoundEffect.cpp">
<Filter>Sound</Filter>
</ClCompile>
<ClCompile Include="..\..\src\Actor\NPC.cpp">
<Filter>Actor</Filter>
</ClCompile>
</ItemGroup>
</Project>

BIN
Data/SFX/step_cloth1.wav Normal file

Binary file not shown.

BIN
Data/SFX/step_cloth2.wav Normal file

Binary file not shown.

BIN
Data/SFX/step_cloth3.wav Normal file

Binary file not shown.

BIN
Data/SFX/step_cloth4.wav Normal file

Binary file not shown.

View File

@ -1,4 +1,7 @@
#include <stdlib.h>
#include "Player.h"
#include "../Sound/SoundEffect.h"
Player::Player(void) {
PLAYER_SPEED = 15;
@ -17,6 +20,12 @@ Player::Player(void) {
_environmentCollisionBound = new AABB();
_environmentCollisionBound->SetMin(_collisionBound->GetMin().x, _collisionBound->GetMax().y - 50.0f);
_environmentCollisionBound->SetMax(_collisionBound->GetMax().x, _collisionBound->GetMax().y);
_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");
_stepSFX[3] = sfxManager.Load("../Data/SFX/step_cloth4.wav");
_lastStepSFXPlayed = -1;
}
Player::~Player(void) {
@ -68,8 +77,8 @@ bool Player::GetInBlueCollision(void) {
}
void Player::ProcessEvents(void) {
x = _player->GetX();
y = _player->GetY();
float oldX = x = _player->GetX();
float oldY = y = _player->GetY();
if(KeyStillDown(SDLK_w)) {
y -= PLAYER_SPEED;
_player->SetY(y);
@ -86,6 +95,18 @@ void Player::ProcessEvents(void) {
x += PLAYER_SPEED;
_player->SetX(x);
}
if(x != oldX || y != oldY) {
if(!SoundEffect::IsPlaying(1)) {
int sfxIndex;
do {
sfxIndex = rand() % 4;
} while(sfxIndex == _lastStepSFXPlayed);
SoundEffect::Play(_stepSFX[sfxIndex], 1, 0);
_lastStepSFXPlayed = sfxIndex;
}
}
}
int Player::GetWidth(void) { return _player->GetWidth(); }

View File

@ -6,6 +6,7 @@
#include "../IO/Input.h"
class Sprite;
class SoundEffect;
// We will derive from an Actor class at some point.
class Player {
@ -66,4 +67,7 @@ private:
AABB* _collisionBound;
AABB* _environmentCollisionBound;
SoundEffect* _stepSFX[4];
int _lastStepSFXPlayed;
};

33
src/Sound/SoundEffect.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <SDL/SDL_mixer.h>
#include "SoundEffect.h"
ResourceManager<SoundEffect> sfxManager;
SoundEffect::SoundEffect() {
_chunk = NULL;
}
SoundEffect::~SoundEffect() {
if(_chunk) {
Mix_FreeChunk(_chunk);
_chunk = NULL;
}
}
bool SoundEffect::Load(const std::string& filename) {
_chunk = Mix_LoadWAV(filename.c_str());
return _chunk != NULL;
}
void SoundEffect::Play(SoundEffect* effect, int channel, int loops) {
Mix_PlayChannel(channel, effect->_chunk, loops);
}
void SoundEffect::Stop(int channel) {
Mix_HaltChannel(channel);
}
bool SoundEffect::IsPlaying(int channel) {
return Mix_Playing(channel);
}

24
src/Sound/SoundEffect.h Normal file
View File

@ -0,0 +1,24 @@
#pragma once
#include "../System/ResourceManager.h"
struct Mix_Chunk;
class SoundEffect : public Resource {
template<class T> friend class ResourceManager;
public:
SoundEffect();
~SoundEffect();
bool Load(const std::string& filename);
static void Play(SoundEffect* effect, int channel, int loops);
static void Stop(int channel);
static bool IsPlaying(int channel);
private:
Mix_Chunk* _chunk;
};
extern ResourceManager<SoundEffect> sfxManager;