Commit 77a6fd25 authored by Ilya Prokhorov's avatar Ilya Prokhorov

Separate music and sound

parent 3cd8525c
#include <FlameSteelBattleHorn/Music.h>
#include <iostream>
using namespace FlameSteelBattleHorn;
Music::Music(shared_ptr<string> path) {
#ifndef __EMSCRIPTEN__
music = Mix_LoadMUS(path->c_str());
if (music == nullptr) {
cout << "Can't load music " << Mix_GetError() << endl;
throw runtime_error("Can't load music");
}
#endif
}
void Music::play() {
#ifndef __EMSCRIPTEN__
if (music) {
if (Mix_PlayMusic(music, -1) == -1) {
throw runtime_error("Can't play music");
}
}
else {
throw runtime_error("Can't play music - music is null");
}
#endif
}
void Music::stop() {
}
Music::~Music() {
#ifndef __EMSCRIPTEN__
if (music) {
Mix_FreeMusic(music);
}
#endif
}
\ No newline at end of file
#ifndef FSBH_MUSIC_H_
#define FSBH_MUSIC_H_
#include <FlameSteelBattleHorn/Playable.h>
#include <memory>
#include <string>
#ifndef __EMSCRIPTEN__
#include <SDL2/SDL_mixer.h>
#endif
using namespace std;
namespace FlameSteelBattleHorn {
class Music: public Playable {
public:
Music(shared_ptr<string> path);
void play();
void stop();
virtual ~Music();
private:
#ifndef __EMSCRIPTEN__
Mix_Music *music = nullptr;
#endif
};
}
#endif
\ No newline at end of file
#ifndef FSBH_SOUND_SYSTEM_PLAYABLE_H
#define FSBH_SOUND_SYSTEM_PLAYABLE_H
#include <stdexcept>
using namespace std;
namespace FlameSteelBattleHorn {
class Playable {
public:
virtual void play() { throw logic_error("Call to not overriden method play, playable abstract class"); };
virtual void stop() { throw logic_error("Call to not overriden method stop, playable abstract class"); };
};
};
#endif
\ No newline at end of file
...@@ -26,7 +26,6 @@ void Sound::initializeSoundSystem() { ...@@ -26,7 +26,6 @@ void Sound::initializeSoundSystem() {
cout << "SDL_MIXER initialized correctly" << endl; cout << "SDL_MIXER initialized correctly" << endl;
} }
#endif #endif
} }
...@@ -34,11 +33,11 @@ Sound::Sound(shared_ptr<string> path) { ...@@ -34,11 +33,11 @@ Sound::Sound(shared_ptr<string> path) {
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
music = Mix_LoadMUS(path->c_str()); chunk = Mix_LoadWAV(path->c_str());
if (music == nullptr) { if (chunk == nullptr) {
cout << "Can't load music " << Mix_GetError() << endl; cout << "Can't load sound " << Mix_GetError() << endl;
throw runtime_error("Can't load music"); throw runtime_error("Can't load sound");
} }
#endif #endif
...@@ -50,15 +49,6 @@ void Sound::play() { ...@@ -50,15 +49,6 @@ void Sound::play() {
if (chunk) { if (chunk) {
} }
if (music) {
if (Mix_PlayMusic(music, -1) == -1) {
throw runtime_error("Can't play music");
}
}
else {
throw runtime_error("Can't play music - music is null");
}
#endif #endif
} }
...@@ -68,10 +58,6 @@ void Sound::stop() { ...@@ -68,10 +58,6 @@ void Sound::stop() {
Sound::~Sound() { Sound::~Sound() {
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
if (music) {
Mix_FreeMusic(music);
}
if (chunk) { if (chunk) {
Mix_FreeChunk(chunk); Mix_FreeChunk(chunk);
} }
......
#ifndef FSBH_SOUND_H_ #ifndef FSBH_SOUND_H_
#define FSBH_SOUND_H_ #define FSBH_SOUND_H_
#include <FlameSteelBattleHorn/Playable.h>
#include <memory> #include <memory>
#include <string> #include <string>
...@@ -9,21 +11,21 @@ ...@@ -9,21 +11,21 @@
#endif #endif
using namespace std; using namespace std;
using namespace FlameSteelBattleHorn;
namespace FlameSteelBattleHorn { namespace FlameSteelBattleHorn {
class Sound { class Sound: public Playable {
public: public:
static void initializeSoundSystem(); static void initializeSoundSystem();
Sound(shared_ptr<string> path); Sound(shared_ptr<string> path);
void play(); void play();
void stop(); void stop();
~Sound(); virtual ~Sound();
private: private:
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
Mix_Music *music = nullptr;
Mix_Chunk *chunk = nullptr; Mix_Chunk *chunk = nullptr;
#endif #endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment