Skip to content

Commit

Permalink
📐 Major formatting cleanup; audio+datatypes dirs
Browse files Browse the repository at this point in the history
Tabs->spaces, removed old comment-outs, unified header blocks, removed header guards in favor of #pragma once
Used tools: ReSharper C++; VisualStudio "untabify selected lines", manual adjustments
  • Loading branch information
ohlidalp committed Nov 21, 2016
1 parent 054716d commit 01ac30c
Show file tree
Hide file tree
Showing 11 changed files with 2,484 additions and 2,026 deletions.
195 changes: 102 additions & 93 deletions source/main/audio/Sound.cpp
@@ -1,160 +1,169 @@
/*
This source file is part of Rigs of Rods
Copyright 2005-2012 Pierre-Michel Ricordel
Copyright 2007-2012 Thomas Fischer
This source file is part of Rigs of Rods
Copyright 2005-2012 Pierre-Michel Ricordel
Copyright 2007-2012 Thomas Fischer
For more information, see http://www.rigsofrods.org/
For more information, see http://www.rigsofrods.org/
Rigs of Rods is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3, as
published by the Free Software Foundation.
Rigs of Rods is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3, as
published by the Free Software Foundation.
Rigs of Rods is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
Rigs of Rods is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU General Public License
along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
*/

#ifdef USE_OPENAL

#include "Sound.h"
#include "SoundManager.h"

using namespace Ogre;

Sound::Sound(ALuint buffer, SoundManager *soundManager, int sourceIndex) :
buffer(buffer)
, sound_manager(soundManager)
, source_index(sourceIndex)
, audibility(0.0f)
, gain(0.0f)
, pitch(1.0f)
, position(Vector3::ZERO)
, velocity(Vector3::ZERO)
, enabled(true)
, loop(false)
, should_play(false)
, hardware_index(-1)
Sound::Sound(ALuint buffer, SoundManager* soundManager, int sourceIndex) :
buffer(buffer)
, sound_manager(soundManager)
, source_index(sourceIndex)
, audibility(0.0f)
, gain(0.0f)
, pitch(1.0f)
, position(Vector3::ZERO)
, velocity(Vector3::ZERO)
, enabled(true)
, loop(false)
, should_play(false)
, hardware_index(-1)
{
}

void Sound::computeAudibility(Vector3 pos)
{
// disable sound?
if (!enabled)
{
audibility = 0.0f;
return;
}

// first check if the sound is finished!
if (!loop && should_play && hardware_index != -1)
{
int value = 0;
alGetSourcei((ALuint)sound_manager->getHardwareSource(hardware_index), AL_SOURCE_STATE, &value);
if (value != AL_PLAYING)
{
should_play = false;
}
}

// should it play at all?
if (!should_play || gain == 0.0f)
{
audibility = 0.0f;
return;
}

float distance = (pos - position).length();

if (distance > sound_manager->MAX_DISTANCE)
{
audibility = 0.0f;
} else if (distance < sound_manager->REFERENCE_DISTANCE)
{
audibility = gain;
} else
{
audibility = gain * (sound_manager->REFERENCE_DISTANCE / (sound_manager->REFERENCE_DISTANCE + (sound_manager->ROLLOFF_FACTOR * (distance - sound_manager->REFERENCE_DISTANCE))));
}
// disable sound?
if (!enabled)
{
audibility = 0.0f;
return;
}

// first check if the sound is finished!
if (!loop && should_play && hardware_index != -1)
{
int value = 0;
alGetSourcei((ALuint)sound_manager->getHardwareSource(hardware_index), AL_SOURCE_STATE, &value);
if (value != AL_PLAYING)
{
should_play = false;
}
}

// should it play at all?
if (!should_play || gain == 0.0f)
{
audibility = 0.0f;
return;
}

float distance = (pos - position).length();

if (distance > sound_manager->MAX_DISTANCE)
{
audibility = 0.0f;
}
else if (distance < sound_manager->REFERENCE_DISTANCE)
{
audibility = gain;
}
else
{
audibility = gain * (sound_manager->REFERENCE_DISTANCE / (sound_manager->REFERENCE_DISTANCE + (sound_manager->ROLLOFF_FACTOR * (distance - sound_manager->REFERENCE_DISTANCE))));
}
}

bool Sound::isPlaying()
{
if (hardware_index != -1)
{
int value = 0;
alGetSourcei((ALuint)sound_manager->getHardwareSource(hardware_index), AL_SOURCE_STATE, &value);
return (value == AL_PLAYING);
}
return false;
if (hardware_index != -1)
{
int value = 0;
alGetSourcei((ALuint)sound_manager->getHardwareSource(hardware_index), AL_SOURCE_STATE, &value);
return (value == AL_PLAYING);
}
return false;
}

void Sound::setEnabled(bool e)
{
if (e == this->enabled) return;
if (e == this->enabled)
return;

this->enabled = e;
sound_manager->recomputeSource(source_index, REASON_PLAY, 0.0f, NULL);
this->enabled = e;
sound_manager->recomputeSource(source_index, REASON_PLAY, 0.0f, NULL);
}

bool Sound::getEnabled()
{
return enabled;
return enabled;
}

void Sound::play()
{
should_play = true;
sound_manager->recomputeSource(source_index, REASON_PLAY, 0.0f, NULL);
should_play = true;
sound_manager->recomputeSource(source_index, REASON_PLAY, 0.0f, NULL);
}

void Sound::stop()
{
should_play = false;
sound_manager->recomputeSource(source_index, REASON_STOP, 0.0f, NULL);
should_play = false;
sound_manager->recomputeSource(source_index, REASON_STOP, 0.0f, NULL);
}

void Sound::setGain(float gain)
{
if (gain == this->gain) return;
if (gain == this->gain)
return;

this->gain = gain;
sound_manager->recomputeSource(source_index, REASON_GAIN, gain, NULL);
this->gain = gain;
sound_manager->recomputeSource(source_index, REASON_GAIN, gain, NULL);
}

void Sound::setLoop(bool loop)
{
if (loop == this->loop) return;
if (loop == this->loop)
return;

this->loop = loop;
sound_manager->recomputeSource(source_index, REASON_LOOP, (loop) ? 1.0f : 0.0f, NULL);
this->loop = loop;
sound_manager->recomputeSource(source_index, REASON_LOOP, (loop) ? 1.0f : 0.0f, NULL);
}

void Sound::setPitch(float pitch)
{
if (pitch == this->pitch) return;
if (pitch == this->pitch)
return;

this->pitch = pitch;
sound_manager->recomputeSource(source_index, REASON_PTCH, pitch, NULL);
this->pitch = pitch;
sound_manager->recomputeSource(source_index, REASON_PTCH, pitch, NULL);
}

void Sound::setPosition(Ogre::Vector3 pos)
{
if (pos == this->position) return;
if (pos == this->position)
return;

this->position = pos;
sound_manager->recomputeSource(source_index, REASON_POSN, 0.0f, &pos);
this->position = pos;
sound_manager->recomputeSource(source_index, REASON_POSN, 0.0f, &pos);
}

void Sound::setVelocity(Ogre::Vector3 vel)
{
if (vel == this->velocity) return;
if (vel == this->velocity)
return;

this->velocity = vel;
sound_manager->recomputeSource(source_index, REASON_VLCT, 0.0f, &vel);
this->velocity = vel;
sound_manager->recomputeSource(source_index, REASON_VLCT, 0.0f, &vel);
}

#endif // USE_OPENAL
107 changes: 57 additions & 50 deletions source/main/audio/Sound.h
@@ -1,78 +1,85 @@
/*
This source file is part of Rigs of Rods
Copyright 2005-2012 Pierre-Michel Ricordel
Copyright 2007-2012 Thomas Fischer
This source file is part of Rigs of Rods
Copyright 2005-2012 Pierre-Michel Ricordel
Copyright 2007-2012 Thomas Fischer
For more information, see http://www.rigsofrods.org/
For more information, see http://www.rigsofrods.org/
Rigs of Rods is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3, as
published by the Free Software Foundation.
Rigs of Rods is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3, as
published by the Free Software Foundation.
Rigs of Rods is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
Rigs of Rods is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU General Public License
along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
*/

#ifdef USE_OPENAL

#pragma once
#ifndef __Sound_H_
#define __Sound_H_

#include "RoRPrerequisites.h"

#ifdef __APPLE__
#include <OpenAL/al.h>
#else
#include <AL/al.h>
#include <AL/al.h>
#endif // __APPLE__

class Sound : public ZeroedMemoryAllocator
{
friend class SoundManager;
friend class SoundManager;

public:
Sound(ALuint buffer, SoundManager* soundManager, int sourceIndex);
Sound(ALuint buffer, SoundManager* soundManager, int sourceIndex);

void setPitch(float pitch);
void setGain(float gain);
void setPosition(Ogre::Vector3 pos);
void setVelocity(Ogre::Vector3 vel);
void setLoop(bool loop);
void setEnabled(bool e);
void play();
void stop();

bool getEnabled();
bool isPlaying();

enum RecomputeSource
{
REASON_PLAY,
REASON_STOP,
REASON_GAIN,
REASON_LOOP,
REASON_PTCH,
REASON_POSN,
REASON_VLCT
};

private:
void computeAudibility(Ogre::Vector3 pos);

void setPitch(float pitch);
void setGain(float gain);
void setPosition(Ogre::Vector3 pos);
void setVelocity(Ogre::Vector3 vel);
void setLoop(bool loop);
void setEnabled(bool e);
void play();
void stop();
float audibility;
float gain;
float pitch;
bool loop;
bool enabled;
bool should_play;

bool getEnabled();
bool isPlaying();
// this value is changed dynamically, depending on whether the input is played or not.
int hardware_index;
ALuint buffer;

enum RecomputeSource { REASON_PLAY, REASON_STOP, REASON_GAIN, REASON_LOOP, REASON_PTCH, REASON_POSN, REASON_VLCT };
Ogre::Vector3 position;
Ogre::Vector3 velocity;

private:
void computeAudibility(Ogre::Vector3 pos);

float audibility;
float gain;
float pitch;
bool loop;
bool enabled;
bool should_play;

// this value is changed dynamically, depending on whether the input is played or not.
int hardware_index;
ALuint buffer;

Ogre::Vector3 position;
Ogre::Vector3 velocity;

SoundManager* sound_manager;
// must not be changed during the lifetime of this object
int source_index;
SoundManager* sound_manager;
// must not be changed during the lifetime of this object
int source_index;
};

#endif // __Sound_H_
#endif // USE_OPENAL

0 comments on commit 01ac30c

Please sign in to comment.