Skip to content

Commit

Permalink
Ogg Vorbis support.
Browse files Browse the repository at this point in the history
  • Loading branch information
rohit-n committed Oct 10, 2015
1 parent 064f1ef commit 8a89bbd
Show file tree
Hide file tree
Showing 15 changed files with 5,566 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ clonepoint
*.o
*.sav
data/fonts
data/music/
data/sprites/*.png
data/sounds/
data/tilesets/
72 changes: 72 additions & 0 deletions audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ along with Clonepoint. If not, see <http://www.gnu.org/licenses/>.
*/

#include "audio.h"
#include "file.h"
#include "global.h"
#include "stb_vorbis.c"

AudioManager::AudioManager()
{
Expand All @@ -39,6 +41,10 @@ AudioManager::AudioManager()
_loadedSources.push_back(src);
}

_musicSource = 0;
_musicBuffer = 0;
alGenSources(1, &_musicSource);

loadWAV("./data/sounds/alarm.wav");
loadWAV("./data/sounds/door_open.wav");
loadWAV("./data/sounds/door_close.wav");
Expand Down Expand Up @@ -70,6 +76,13 @@ AudioManager::~AudioManager()

_loadedSources.clear();

alDeleteSources(1, &_musicSource);

if (_musicBuffer != 0)
{
alDeleteBuffers(1, &_musicBuffer);
}

std::map<std::string, ALuint>::iterator it;

for (it = _loadedBuffers.begin(); it != _loadedBuffers.end(); it++)
Expand All @@ -94,6 +107,33 @@ void AudioManager::playSound(std::string filename)
alSourcePlay(src);
}

void AudioManager::playMusic(std::string filename)
{
if (_musicBuffer != 0)
{
alDeleteBuffers(1, &_musicBuffer);
_musicBuffer = 0;
}

_musicBuffer = bufferFromOGG("./data/music/" + filename);

if (_musicBuffer == 0)
{
return;
}

alSourcei(_musicSource, AL_BUFFER, _musicBuffer);
alSource3f(_musicSource, AL_POSITION, 0, 0, 0);
alSourcePlay(_musicSource);
}

void AudioManager::stopMusic()
{
alDeleteBuffers(1, &_musicBuffer);
_musicBuffer = 0;
alSourceStop(_musicSource);
}

void AudioManager::playSound3D(const char* filename, float x, float y, float z)
{
ALuint src = getNextAvailableSource();
Expand Down Expand Up @@ -150,6 +190,37 @@ void AudioManager::loadWAV(std::string filename)
}
}

ALuint AudioManager::bufferFromOGG(std::string filename)
{
long size;
char* file_buffer = file_read(filename.c_str(), &size);
if (!file_buffer)
{
return 0;
}

stb_vorbis* ogg = stb_vorbis_open_memory((const unsigned char*)file_buffer, (int)size, NULL, NULL);
if (!ogg)
{
return 0;
}

ALuint ret;

stb_vorbis_info info = stb_vorbis_get_info( ogg );
ALenum format = ( ( info.channels == 1 ) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16);

int const length_samples = stb_vorbis_stream_length_in_samples(ogg) * info.channels;
std::vector<ALshort> ogg_buffer(length_samples);
alGenBuffers(1, &ret);
stb_vorbis_get_samples_short_interleaved(ogg, info.channels, ogg_buffer.data(), length_samples);
alBufferData(ret, format, ogg_buffer.data(), (length_samples * sizeof( ALshort)), info.sample_rate);
stb_vorbis_close(ogg);
delete [] file_buffer;

return ret;
}

ALuint AudioManager::getBuffer(std::string filename)
{
std::map<std::string, ALuint>::iterator it = _loadedBuffers.find(filename);
Expand Down Expand Up @@ -186,4 +257,5 @@ void AudioManager::setVolume(float volume)
{
alSourcef(_loadedSources[i], AL_GAIN, volume);
}
alSourcef(_musicSource, AL_GAIN, volume);
}
5 changes: 5 additions & 0 deletions audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,19 @@ class AudioManager
AudioManager();
~AudioManager();
void playSound(std::string filename);
void playMusic(std::string filename);
void stopMusic();
void playSound3D(const char* filename, float x, float y, float z);
void loadWAV(std::string filename);
ALuint bufferFromOGG(std::string filename);
ALuint getBuffer(std::string filename);
ALuint getNextAvailableSource();
void setVolume(float volume);
private:
std::map<std::string, ALuint> _loadedBuffers;
std::vector<ALuint> _loadedSources;
ALuint _musicSource;
ALuint _musicBuffer;
ALCdevice* _device;
ALCcontext* _context;
};
Expand Down
2 changes: 1 addition & 1 deletion bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ eBinding BindingsManager::getBindingFromKey(SDL_Keycode key)

void BindingsManager::loadBindingsFromConfig(const char* filename)
{
char* text = file_read(filename);
char* text = file_read(filename, NULL);
char* delim = (char*)" =\t\n\r";
char* token = strtok(text, delim);
eBinding binding;
Expand Down
2 changes: 1 addition & 1 deletion config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void ConfigManager::setValue(std::string key, std::string value)

void ConfigManager::loadConfig(const char* filename)
{
char* text = file_read(filename);
char* text = file_read(filename, NULL);
char* delim = (char*)" =\t\n\r";
char* token = strtok(text, delim);
char* key;
Expand Down
2 changes: 1 addition & 1 deletion creditsstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ CreditsState::CreditsState(StateManager* sm) : MenuState(sm)
Gunpoint created by Tom Francis (www.pentadact.com)\n\
\n\n\
Libraries Used:\n\
stb_image and stb_ttf by Sean Barrett (www.nothings.org)\n\
stb_image, stb_ttf, stb_vorbis by Sean Barrett (www.nothings.org)\n\
TinyXML (www.grinninglizard.com/tinyxml/)\n\
SDL2 (www.libsdl.org)\n\
OpenAL Soft (kcat.strangesoft.net/openal.html)\n\
Expand Down
4 changes: 2 additions & 2 deletions draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1718,8 +1718,8 @@ GLuint Renderer::compileShaders(const char* vert_filename, const char* frag_file
GLuint vert_shader = glCreateShaderObject(GL_VERTEX_SHADER);
GLuint frag_shader = glCreateShaderObject(GL_FRAGMENT_SHADER);

char* vertString = file_read(vert_filename);
char* fragString = file_read(frag_filename);
char* vertString = file_read(vert_filename, NULL);
char* fragString = file_read(frag_filename, NULL);

addShader(program, vertString, vert_shader);
addShader(program, fragString, frag_shader);
Expand Down
51 changes: 28 additions & 23 deletions file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,33 @@

#include "file.h"

char* file_read(const char* filename)
char* file_read(const char* filename, long* size_ret)
{
FILE* input = fopen(filename, "rb");
if(input == NULL) return NULL;

if(fseek(input, 0, SEEK_END) == -1) return NULL;
long size = ftell(input);
if(size == -1) return NULL;
if(fseek(input, 0, SEEK_SET) == -1) return NULL;

char* content = new char[(size_t)size+1];
if(content == NULL)
return NULL;

fread(content, 1, (size_t)size, input);
if(ferror(input))
{
delete [] content;
return NULL;
}

fclose(input);
content[size] = '\0';
return content;
FILE* input = fopen(filename, "rb");

if (size_ret) *size_ret = 0;

if(input == NULL) return NULL;

if(fseek(input, 0, SEEK_END) == -1) return NULL;
long size = ftell(input);
if(size == -1) return NULL;
if(fseek(input, 0, SEEK_SET) == -1) return NULL;

char* content = new char[(size_t)size+1];
if(content == NULL)
return NULL;

fread(content, 1, (size_t)size, input);
if(ferror(input))
{
delete [] content;
return NULL;
}

if (size_ret) *size_ret = size;

fclose(input);
content[size] = '\0';
return content;
}
2 changes: 1 addition & 1 deletion file.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
#include <cstdlib>

//This function is public domain, taken from https://gitorious.org/wikibooks-opengl
char* file_read(const char* filename);
char* file_read(const char* filename, long* size_ret);
1 change: 1 addition & 0 deletions levelendstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void LevelEndState::handleButton(Button* button)
{
if (button == _OKButton)
{
Locator::getAudio()->stopMusic();
_manager->switchToState(MAINMENU_SCREEN);
_manager->destroyScene();
}
Expand Down
1 change: 1 addition & 0 deletions pausestate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ void PauseState::handleButton(Button* button)

if (button == _exitButton)
{
Locator::getAudio()->stopMusic();
_manager->switchToState(MAINMENU_SCREEN);
_manager->destroyScene();
}
Expand Down
11 changes: 9 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Default Controls:

Credits:
Tom Francis for creating Gunpoint.
Sean Barrett for stb_image and stb_ttf (public domain).
Sean Barrett for stb_image, stb_ttf, and stb_vorbis (public domain).
TinyXML and SDL2 are under the Zlib license.
OpenAL Soft is under the LGPL license.

Expand Down Expand Up @@ -123,4 +123,11 @@ Credits:
6. Mouse cursor
- By qubodup
- http://opengameart.org/content/simple-light-graysacle-cursors-16x16
- CC0 http://creativecommons.org/publicdomain/zero/1.0/
- CC0 http://creativecommons.org/publicdomain/zero/1.0/

Music
1. groove_grove.ogg
- By Kevin MacLeod
- incompetech.com
- CC BY 3.0 http://creativecommons.org/licenses/by/3.0/
- Converted from MP3 to OGG
2 changes: 1 addition & 1 deletion static_sprites.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ unsigned int StaticSpriteManager::getPlayerSpriteIndex(PlayerStaticSprites pss)

void StaticSpriteManager::loadSpriteIndicesFromFile(std::string filename)
{
char* text = file_read(filename.c_str());
char* text = file_read(filename.c_str(), NULL);
char* delim = (char*)" =\t\n\r";
char* token = strtok(text, delim);
std::string spriteName;
Expand Down
Loading

0 comments on commit 8a89bbd

Please sign in to comment.