Permalink
Browse files

move Mixer implementations to separate .cpp

  • Loading branch information...
1 parent 2882507 commit 1994c0045eeae6ce6ea89932fa5e2d6f084af69f @cyxx committed Jul 2, 2015
Showing with 255 additions and 296 deletions.
  1. +5 −6 Makefile
  2. +1 −6 game.cpp
  3. +18 −40 mixer.h
  4. +100 −0 mixer_sdl.cpp
  5. +119 −244 mixer.cpp → mixer_soft.cpp
  6. +4 −0 systemstub.h
  7. +8 −0 systemstub_sdl.cpp
View
@@ -5,9 +5,8 @@
# 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
+DEFINES = -DBERMUDA_POSIX -DBERMUDA_VORBIS
+VORBIS_LIBS = -lvorbisfile -lvorbis -logg
SDL_CFLAGS = `sdl2-config --cflags`
SDL_LIBS = `sdl2-config --libs` -lSDL2_mixer
@@ -19,9 +18,9 @@ CXXFLAGS = -g -O -Wall $(SDL_CFLAGS) $(DEFINES)
OBJDIR = obj
SRCS = avi_player.cpp bag.cpp decoder.cpp dialogue.cpp file.cpp fs.cpp game.cpp \
- main.cpp mixer.cpp opcodes.cpp parser_dlg.cpp parser_scn.cpp random.cpp \
- resource.cpp saveload.cpp staticres.cpp str.cpp systemstub_sdl.cpp util.cpp \
- win16.cpp
+ main.cpp mixer_sdl.cpp mixer_soft.cpp opcodes.cpp parser_dlg.cpp parser_scn.cpp \
+ random.cpp resource.cpp saveload.cpp staticres.cpp str.cpp systemstub_sdl.cpp \
+ util.cpp win16.cpp
OBJS = $(SRCS:.cpp=.o)
DEPS = $(SRCS:.cpp=.d)
View
@@ -16,13 +16,12 @@ static const bool kCheatNoHit = false;
Game::Game(SystemStub *stub, const char *dataPath, const char *savePath, const char *musicPath)
: _fs(dataPath), _stub(stub), _dataPath(dataPath), _savePath(savePath), _musicPath(musicPath) {
- _mixer = new Mixer(_stub);
+ _mixer = _stub->getMixer();
_stateSlot = 1;
detectVersion();
}
Game::~Game() {
- delete _mixer;
}
void Game::detectVersion() {
@@ -111,7 +110,6 @@ void Game::mainLoop() {
_stub->init(kGameWindowTitle, kGameScreenWidth, kGameScreenHeight);
allocateTables();
loadCommonSprites();
- _mixer->open();
restart();
if (_isDemo) {
playBitmapSequenceDemo();
@@ -185,7 +183,6 @@ void Game::mainLoop() {
clearSceneData(-1);
deallocateTables();
unloadCommonSprites();
- _mixer->close();
_stub->destroy();
}
@@ -889,10 +886,8 @@ void Game::playVideo(const char *name) {
if (f.open(filePath)) {
_stub->fillRect(0, 0, kGameScreenWidth, kGameScreenHeight, 0);
_stub->updateScreen();
- _mixer->close();
AVI_Player player(_mixer, _stub);
player.play(&f);
- _mixer->open();
}
free(filePath);
}
View
@@ -9,49 +9,27 @@
#include "intern.h"
struct File;
-struct MixerImpl;
struct SystemStub;
-struct MixerChannel {
- virtual ~MixerChannel() {}
- virtual bool load(File *f, int mixerSampleRate) = 0;
- virtual int read(int16_t *dst, int samples) = 0;
- int id;
-};
-
struct Mixer {
- enum {
- kMaxChannels = 4,
- kDefaultSoundId = -1
- };
-
- Mixer(SystemStub *stub);
- ~Mixer();
-
- void open();
- void close();
-
- void startSound(File *f, int *id, MixerChannel *mc);
- void playSound(File *f, int *id);
- void playMusic(File *f, int *id);
- bool isSoundPlaying(int id);
- void stopSound(int id);
- void stopAll();
- void setMusicMix(void *param, void (*mix)(void *, uint8_t *, int));
-
- void mix(int16_t *buf, int len);
- static void mixCallback(void *param, uint8_t *buf, int len);
-
- int generateSoundId(int channel);
- int getChannelFromSoundId(int id);
- bool bindChannel(MixerChannel *mc, int *id);
- void unbindChannel(int channel);
-
- SystemStub *_stub;
- int _channelIdSeed;
- bool _open;
- MixerChannel *_channels[kMaxChannels];
- MixerImpl *_impl;
+ static const int kDefaultSoundId = -1;
+
+ Mixer() {}
+ virtual ~Mixer() {}
+
+ virtual void open() = 0;
+ virtual void close() = 0;
+
+ virtual void playSound(File *f, int *id) = 0;
+ virtual void playMusic(File *f, int *id) = 0;
+ virtual bool isSoundPlaying(int id) = 0;
+ virtual void stopSound(int id) = 0;
+ virtual void stopAll() = 0;
+
+ virtual void setMusicMix(void *param, void (*mix)(void *, uint8_t *, int)) = 0;
};
+Mixer *Mixer_SDL_create(SystemStub *);
+Mixer *Mixer_Software_create(SystemStub *);
+
#endif // MIXER_H__
View
@@ -0,0 +1,100 @@
+
+#include <SDL.h>
+#include <SDL_mixer.h>
+#include "file.h"
+#include "mixer.h"
+#include "util.h"
+
+struct MixerSDL: Mixer {
+
+ static const int kMixFreq = 22050;
+ static const int kMixBufSize = 4096;
+ static const int kChannels = 4;
+
+ bool _isOpen;
+ Mix_Music *_music;
+
+ MixerSDL()
+ : _isOpen(false), _music(0) {
+ }
+
+ virtual ~MixerSDL() {
+ }
+
+ virtual void open() {
+ if (_isOpen) {
+ return;
+ }
+ 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);
+ _isOpen = true;
+ }
+ virtual void close() {
+ if (!_isOpen) {
+ return;
+ }
+ stopAll();
+ Mix_CloseAudio();
+ Mix_Quit();
+ _isOpen = false;
+ }
+
+ virtual void playSound(File *f, int *id) {
+ debug(DBG_MIXER, "MixerSDL::playSound() path '%s'", f->_path);
+ Mix_Chunk *chunk = Mix_LoadWAV(f->_path);
+ if (chunk) {
+ *id = Mix_PlayChannel(-1, chunk, 0);
+ } else {
+ *id = -1;
+ }
+ }
+ virtual void playMusic(File *f, int *id) {
+ debug(DBG_MIXER, "MixerSDL::playSoundMusic() path '%s'", f->_path);
+ playMusic(f->_path);
+ *id = -1;
+ }
+ virtual bool isSoundPlaying(int id) {
+ return Mix_Playing(id) != 0;
+ }
+ virtual void stopSound(int id) {
+ debug(DBG_MIXER, "MixerSDL::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 setMusicMix(void *param, void (*mix)(void *, uint8_t *, int)) {
+ if (mix) {
+ Mix_HookMusic(mix, param);
+ } else {
+ Mix_HookMusic(0, 0);
+ }
+ }
+
+ virtual void stopAll() {
+ debug(DBG_MIXER, "MixerSDL::stopAll()");
+ Mix_HaltChannel(-1);
+ stopMusic();
+ }
+};
+
+Mixer *Mixer_SDL_create(SystemStub *stub) {
+ return new MixerSDL();
+}
Oops, something went wrong.

0 comments on commit 1994c00

Please sign in to comment.