Permalink
Browse files

add MixerImpl based on SDL2_mixer

  • Loading branch information...
1 parent ed95426 commit d91f76e84b48ce24e0ba5343e9306e5aa3574b67 @cyxx committed Jun 24, 2015
Showing with 200 additions and 68 deletions.
  1. +4 −3 Makefile
  2. +1 −1 avi_player.cpp
  3. +3 −3 dialogue.cpp
  4. +12 −8 file.cpp
  5. +5 −4 file.h
  6. +132 −8 mixer.cpp
  7. +2 −0 mixer.h
  8. +37 −37 resource.cpp
  9. +4 −4 saveload.cpp
View
@@ -5,11 +5,12 @@
# BERMUDA_VORBIS : enable playback of digital soundtracks (22 khz mono .ogg files)
#DEFINES = -DBERMUDA_WIN32 -DBERMUDA_VORBIS
-DEFINES = -DBERMUDA_POSIX -DBERMUDA_VORBIS
-VORBIS_LIBS = -lvorbisfile -lvorbis -logg
+#DEFINES = -DBERMUDA_POSIX -DBERMUDA_VORBIS
+#VORBIS_LIBS = -lvorbisfile -lvorbis -logg
+DEFINES = -DBERMUDA_POSIX
SDL_CFLAGS = `sdl2-config --cflags`
-SDL_LIBS = `sdl2-config --libs`
+SDL_LIBS = `sdl2-config --libs` -lSDL2_mixer
CXX = g++
CXXFLAGS = -g -O -Wall $(SDL_CFLAGS) $(DEFINES)
View
@@ -79,7 +79,7 @@ bool AVI_Demuxer::readHeader() {
bool readHdrLoop = true;
while (readHdrLoop) {
_f->read(tag, 4);
- int len = _f->readUint32_tLE();
+ int len = _f->readUint32LE();
assert((len & 1) == 0);
if (memcmp(tag, "LIST", 4) == 0) {
_f->read(tag, 4);
View
@@ -250,14 +250,14 @@ void Game::loadDialogueSprite(int spr) {
break;
}
FileHolder fp(_fs, spriteFile);
- int tag = fp->readUint16_tLE();
+ int tag = fp->readUint16LE();
if (tag != 0x3553) {
error("Invalid spr format %X", tag);
}
- int count = fp->readUint16_tLE();
+ int count = fp->readUint16LE();
assert(count <= 105);
for (int i = 0; i < count; ++i) {
- int size = fp->readUint16_tLE();
+ int size = fp->readUint16LE();
_dialogueSpriteDataTable[spr][i] = (uint8_t *)malloc(size + 10);
if (_dialogueSpriteDataTable[spr][i]) {
fp->read(_dialogueSpriteDataTable[spr][i], size + 10);
View
@@ -93,21 +93,25 @@ struct File_stdio : File_impl {
File_impl *FileImpl_create() { return new File_stdio; }
File_impl *FileImpl_create(uint32_t offset, uint32_t size) { return new File_stdio(offset, size); }
-File::File() {
+File::File()
+ : _path(0) {
_impl = FileImpl_create();
}
File::File(File_impl *impl)
- : _impl(impl) {
+ : _path(0), _impl(impl) {
}
File::~File() {
+ free(_path);
_impl->close();
delete _impl;
}
bool File::open(const char *path, const char *mode) {
_impl->close();
+ free(_path);
+ _path = strdup(path);
return _impl->open(path, mode);
}
@@ -141,13 +145,13 @@ uint8_t File::readByte() {
return b;
}
-uint16_t File::readUint16_tLE() {
+uint16_t File::readUint16LE() {
uint8_t b[2];
read(b, sizeof(b));
return READ_LE_UINT16(b);
}
-uint32_t File::readUint32_tLE() {
+uint32_t File::readUint32LE() {
uint8_t b[4];
read(b, sizeof(b));
return READ_LE_UINT32(b);
@@ -161,14 +165,14 @@ void File::writeByte(uint8_t b) {
write(&b, 1);
}
-void File::writeUint16_tLE(uint16_t n) {
+void File::writeUint16LE(uint16_t n) {
writeByte(n & 0xFF);
writeByte(n >> 8);
}
-void File::writeUint32_tLE(uint32_t n) {
- writeUint16_tLE(n & 0xFFFF);
- writeUint16_tLE(n >> 16);
+void File::writeUint32LE(uint32_t n) {
+ writeUint16LE(n & 0xFFFF);
+ writeUint16LE(n >> 16);
}
struct MemoryMappedFile_impl {
View
@@ -26,13 +26,14 @@ struct File {
void seek(int offs, int origin = SEEK_SET);
uint32_t read(void *ptr, uint32_t len);
uint8_t readByte();
- uint16_t readUint16_tLE();
- uint32_t readUint32_tLE();
+ uint16_t readUint16LE();
+ uint32_t readUint32LE();
void write(void *ptr, uint32_t size);
void writeByte(uint8_t b);
- void writeUint16_tLE(uint16_t n);
- void writeUint32_tLE(uint32_t n);
+ void writeUint16LE(uint16_t n);
+ void writeUint32LE(uint32_t n);
+ char *_path;
File_impl *_impl;
};
View
@@ -3,6 +3,8 @@
* Copyright (C) 2007-2011 Gregory Montoir
*/
+#ifdef MIXER_SOFTWARE
+
#include "file.h"
#include "mixer.h"
#include "systemstub.h"
@@ -51,13 +53,13 @@ struct MixerChannel_Wav : MixerChannel {
f->seek(8); // skip RIFF header
f->read(buf, 8);
if (memcmp(buf, "WAVEfmt ", 8) == 0) {
- f->readUint32_tLE(); // fmtLength
- int compression = f->readUint16_tLE();
- int channels = f->readUint16_tLE();
- int sampleRate = f->readUint32_tLE();
- f->readUint32_tLE(); // averageBytesPerSec
- f->readUint16_tLE(); // blockAlign
- _bitsPerSample = f->readUint16_tLE();
+ f->readUint32LE(); // fmtLength
+ int compression = f->readUint16LE();
+ int channels = f->readUint16LE();
+ int sampleRate = f->readUint32LE();
+ f->readUint32LE(); // averageBytesPerSec
+ f->readUint16LE(); // blockAlign
+ _bitsPerSample = f->readUint16LE();
if (compression != 1 ||
(channels != 1 && channels != 2) ||
(sampleRate != 11025 && sampleRate != 22050 && sampleRate != 44100) ||
@@ -69,7 +71,7 @@ struct MixerChannel_Wav : MixerChannel {
_bufReadStep = (sampleRate << _fracStepBits) / mixerSampleRate;
f->read(buf, 4);
if (memcmp(buf, "data", 4) == 0) {
- _bufSize = f->readUint32_tLE();
+ _bufSize = f->readUint32LE();
_buf = (uint8_t *)malloc(_bufSize);
if (_buf) {
f->read(_buf, _bufSize);
@@ -359,3 +361,125 @@ void Mixer::unbindChannel(int channel) {
_channels[channel] = 0;
}
}
+#else
+
+#include <SDL.h>
+#include <SDL_mixer.h>
+#include "file.h"
+#include "mixer.h"
+#include "util.h"
+
+struct MixerImpl {
+
+ static const int kMixFreq = 44100;
+ static const int kMixBufSize = 4096;
+ static const int kChannels = 4;
+
+ Mix_Chunk *_sounds[kChannels];
+ Mix_Music *_music;
+
+ virtual ~MixerImpl() {
+ }
+
+ virtual void open() {
+ memset(_sounds, 0, sizeof(_sounds));
+ _music = 0;
+
+ Mix_Init(MIX_INIT_OGG | MIX_INIT_FLUIDSYNTH);
+ if (Mix_OpenAudio(kMixFreq, AUDIO_S16SYS, 2, kMixBufSize) < 0) {
+ warning("Mix_OpenAudio failed: %s", Mix_GetError());
+ }
+ Mix_AllocateChannels(kChannels);
+ }
+ virtual void close() {
+ stopAll();
+ Mix_CloseAudio();
+ Mix_Quit();
+ }
+
+ virtual void playSoundWav(const char *path, int *id) {
+ debug(DBG_MIXER, "MixerImpl::playSoundWav()");
+ Mix_Chunk *chunk = Mix_LoadWAV(path);
+ if (chunk) {
+ *id = Mix_PlayChannel(-1, chunk, 0);
+ } else {
+ *id = -1;
+ }
+ }
+ virtual void playSoundMusic(const char *path, int *id) {
+ debug(DBG_MIXER, "MixerImpl::playSoundMusic()");
+ playMusic(path);
+ *id = -1;
+ }
+ virtual bool isPlaying(int id) {
+ return Mix_Playing(id) != 0;
+ }
+ virtual void stopSound(int id) {
+ debug(DBG_MIXER, "MixerImpl::stopSound()");
+ Mix_HaltChannel(id);
+ // Mix_FreeChunk
+ }
+
+ void playMusic(const char *path) {
+ stopMusic();
+ _music = Mix_LoadMUS(path);
+ if (_music) {
+ Mix_VolumeMusic(MIX_MAX_VOLUME / 2);
+ Mix_PlayMusic(_music, 0);
+ } else {
+ warning("Failed to load music '%s', %s", path, Mix_GetError());
+ }
+ }
+ void stopMusic() {
+ Mix_HaltMusic();
+ Mix_FreeMusic(_music);
+ _music = 0;
+ }
+
+ virtual void stopAll() {
+ debug(DBG_MIXER, "MixerImpl::stopAll()");
+ for (int i = 0; i < kChannels; ++i) {
+ stopSound(i);
+ }
+ stopMusic();
+ }
+};
+
+Mixer::Mixer(SystemStub *stub)
+ : _stub(stub) {
+ _impl = new MixerImpl;
+}
+
+Mixer::~Mixer() {
+ delete _impl;
+}
+
+void Mixer::open() {
+ _impl->open();
+}
+
+void Mixer::close() {
+ _impl->close();
+}
+
+void Mixer::playSoundWav(File *f, int *id) {
+ _impl->playSoundWav(f->_path, id);
+}
+
+void Mixer::playSoundVorbis(File *f, int *id) {
+ _impl->playSoundMusic(f->_path, id);
+}
+
+bool Mixer::isSoundPlaying(int id) {
+ return _impl->isPlaying(id);
+}
+
+void Mixer::stopSound(int id) {
+ _impl->stopSound(id);
+}
+
+void Mixer::stopAll() {
+ _impl->stopAll();
+}
+
+#endif
View
@@ -9,6 +9,7 @@
#include "intern.h"
struct File;
+struct MixerImpl;
struct SystemStub;
struct MixerChannel {
@@ -49,6 +50,7 @@ struct Mixer {
int _channelIdSeed;
bool _open;
MixerChannel *_channels[kMaxChannels];
+ MixerImpl *_impl;
};
#endif // MIXER_H__
Oops, something went wrong.

0 comments on commit d91f76e

Please sign in to comment.