Skip to content

Commit

Permalink
Fixed offset on flips (Sprites)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime BACOUX committed Mar 11, 2015
1 parent b78816f commit 435bcda
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 61 deletions.
6 changes: 4 additions & 2 deletions example/03_Hello/Hello.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ class Hello : public lm::GameState
{
public:
Hello()
: _x(0)
, _y(0)
{
_music.loadFile("music.ogg", lm::SoundType::Music);
_jump.loadFile("jump.ogg", lm::SoundType::FX);
_music.loadFile("music.ogg", lm::Sound::Type::Music);
_jump.loadFile("jump.ogg", lm::Sound::Type::FX);
_music.play();
}

Expand Down
2 changes: 1 addition & 1 deletion include/Lums/Lums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#define LUMS_VERSION_MAJOR 2
#define LUMS_VERSION_MINOR 7
#define LUMS_VERSION_TEENY 0
#define LUMS_VERSION_PATCH 1
#define LUMS_VERSION_PATCH 2

#define LUMS_VERSION_NUMBER LUMS_STR(LUMS_VERSION_MAJOR) "." \
LUMS_STR(LUMS_VERSION_MINOR) "." \
Expand Down
27 changes: 12 additions & 15 deletions include/Lums/Sound.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,23 @@ namespace lm
Finished
};

enum class SoundType
{
Null,
FX,
Music
};

class Sound
{
public:

enum class Type
{
None,
FX,
Music
};

Sound();
void loadFile(const std::string name, SoundType type, bool resource = true);
void loadFile(const std::string& name, Type type, bool resource = true);
void play(float x = 0.0f, float y = 0.0f, float z = 0.0f);
void pause();
void stop();
ALuint playSound();
bool operator==(const Sound& rhs) const;
bool operator!=(const Sound& rhs) const;
~Sound();

private:
Expand All @@ -62,22 +60,21 @@ namespace lm
void loadFileOGG(const std::string name, bool resource);
void readOGG(ALuint& Buffer, ALsizei NbSamples);

float _x;
float _y;
float _z;
FILE* _file;
ALenum _format;
ALsizei _sampleRate;
ALsizei _read;
OggVorbis_File _stream;
SoundType _type;
Type _type;
SoundState _state;
float _x;
float _y;
float _z;
bool _run;
size_t _id;
static size_t _nbs;
std::mutex _mtx;
std::condition_variable _cv;
static SoundManager* _soundManager;
};

}
Expand Down
66 changes: 28 additions & 38 deletions src/Sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,76 +20,81 @@

using namespace lm;

static char soundManager[sizeof(SoundManager)];
static SoundManager* soundManagerPtr = nullptr;

static ALuint sourceMusic;
static ALuint sourcesFx[64];
static int nextSound = 0;

Sound::Sound()
: _file(0)
, _format(0)
, _sampleRate(0)
, _read(0)
, _type(SoundType::Null)
, _type(Type::None)
, _state(SoundState::Pending)
, _x(0)
, _y(0)
, _z(0)
, _run(true)
, _id(_nbs)
{
_nbs++;
if (!Sound::_soundManager)
Sound::_soundManager = new SoundManager();
if (!soundManagerPtr)
{
soundManagerPtr = new(soundManager) SoundManager();
alGenSources(1, &sourceMusic);
alGenSources(64, sourcesFx);
}
}

void
Sound::play(float x, float y, float z)
{
_x = x;
_y = y;
_z = z;
if (_type == SoundType::Null)
if (_type == Type::None)
{
std::cerr << "No file loaded !" << std::endl;
return ;
return;
}
_state = SoundState::Playing;
if (_type == SoundType::Music)
if (_type == Type::Music)
{
_run = true;
Sound::_soundManager->addMusic(this);
soundManagerPtr->addMusic(this);
}
else
Sound::_soundManager->addSound(this);
else
soundManagerPtr->addSound(this);
}

void
Sound::pause()
{
_state = SoundState::Paused;
if (_type == SoundType::Music)
if (_type == Type::Music)
{
Sound::_soundManager->pauseMusic();
soundManagerPtr->pauseMusic();
std::unique_lock<std::mutex> lck(_mtx);
_cv.notify_all();
}
else
Sound::_soundManager->pauseSound(*this);
soundManagerPtr->pauseSound(*this);
}

void
Sound::stop()
{
_state = SoundState::Finished;
if (_type == SoundType::Music)
if (_type == Type::Music)
{
Sound::_soundManager->stopMusic();
soundManagerPtr->stopMusic();
_run = false;
}
else
Sound::_soundManager->stopSound(*this);
soundManagerPtr->stopSound(*this);
}

ALuint
Sound::playSound()
{
if (_type == SoundType::FX)
if (_type == Type::FX)
return (playFX());
else
return (playMusic());
Expand Down Expand Up @@ -179,7 +184,7 @@ Sound::playFX()


void
Sound::loadFile(const std::string name, SoundType type, bool resource)
Sound::loadFile(const std::string& name, Type type, bool resource)
{
_type = type;
typedef void (Sound::*sndptr_t)(std::string, bool);
Expand Down Expand Up @@ -235,18 +240,6 @@ Sound::readOGG(ALuint& buffer, ALsizei nbSamples)
alBufferData(buffer, _format, &samples[0], totalRead, _sampleRate);
}

bool
Sound::operator==(const Sound& rhs) const
{
return (_id == rhs._id);
}

bool
Sound::operator!=(const Sound& rhs) const
{
return (_id != rhs._id);
}

Sound::~Sound()
{
if (!_file)
Expand All @@ -257,6 +250,3 @@ Sound::~Sound()

size_t
Sound::_nbs = 0;

SoundManager*
Sound::_soundManager = 0;
6 changes: 3 additions & 3 deletions src/SoundManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ SoundManager::pauseSound(Sound& sound)
for (it = _sounds.begin(); it != _sounds.end(); ++it)
{
Sound* s = (*it).sound;
if (sound == *s && (*it).state == SoundState::Playing)
if (&sound == s && (*it).state == SoundState::Playing)
{
alSourcePause((*it).source);
(*it).state = SoundState::Paused;
Expand All @@ -74,7 +74,7 @@ SoundManager::stopSound(Sound& sound)
for (it = _sounds.begin(); it != _sounds.end(); ++it)
{
Sound* s = (*it).sound;
if (sound == *s)
if (&sound == s)
{
alSourceStop((*it).source);
(*it).state = SoundState::Finished;
Expand Down Expand Up @@ -141,7 +141,7 @@ SoundManager::addMusic(Sound* sound)
std::chrono::milliseconds dura(400);
while (_state != SoundManagerState::Free)
std::this_thread::sleep_for(dura);
if (_music.sound && *(_music.sound) != *sound)
if (_music.sound && _music.sound != sound)
_music.sound->stop();
_state = SoundManagerState::Used;
_music = s;
Expand Down
4 changes: 2 additions & 2 deletions src/Sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ Sprite::updateTexCoord()

double dw = _w;
double dh = _h;
double x = tex.offX;
double y = tex.offY;
double x = _flipX ? 0 : tex.offX;
double y = _flipY ? 0 : tex.offY;

if (_flipX)
{
Expand Down

0 comments on commit 435bcda

Please sign in to comment.