mirror of
https://github.com/ApfelTeeSaft/gameBase.git
synced 2026-08-26 19:23:30 +00:00
Merge pull request #17 from devZoGok/ft-16-sound-manager
Ft 16 sound manager
This commit is contained in:
@@ -7,3 +7,6 @@
|
||||
[submodule "external/sol2"]
|
||||
path = external/sol2
|
||||
url = https://github.com/ThePhD/sol2
|
||||
[submodule "external/SFML"]
|
||||
path = external/SFML
|
||||
url = [email protected]:SFML/SFML.git
|
||||
|
||||
+9
-3
@@ -1,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.2)
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(gameBase)
|
||||
|
||||
set(CMAKE_BUILD_TYPE Debug)
|
||||
@@ -6,7 +6,7 @@ cmake_policy(SET CMP0015 NEW)
|
||||
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
set(states abstractAppState.cpp)
|
||||
set(core inputManager.cpp mapping.h stateManager.cpp)
|
||||
set(core inputManager.cpp mapping.h stateManager.cpp soundManager.cpp)
|
||||
set(util solUtil.cpp util.cpp)
|
||||
|
||||
set(LIB_NAME gameBase)
|
||||
@@ -25,4 +25,10 @@ set(SOL_DIR external/sol2)
|
||||
add_subdirectory(${SOL_DIR})
|
||||
target_include_directories(${LIB_NAME} PUBLIC ${SOL_DIR}/include)
|
||||
|
||||
target_link_libraries(${LIB_NAME} glfw lua::lib)
|
||||
set(SFML_DIR external/SFML)
|
||||
add_subdirectory(${SFML_DIR})
|
||||
set(SFML_LIB_DIR build/${SFML_DIR}/src)
|
||||
target_include_directories(${LIB_NAME} PUBLIC ${SFML_DIR}/include)
|
||||
target_link_directories(${LIB_NAME} PUBLIC ${SFML_LIB_DIR})
|
||||
|
||||
target_link_libraries(${LIB_NAME} sfml-system sfml-audio glfw lua::lib)
|
||||
|
||||
+1
Submodule external/SFML added at 3651072cc0
@@ -0,0 +1,94 @@
|
||||
#include "soundManager.h"
|
||||
|
||||
#include <SFML/Audio.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <random>
|
||||
|
||||
namespace gameBase{
|
||||
using namespace std;
|
||||
|
||||
SoundManager::Track::Track(string p) : path(p){
|
||||
soundObj = new sf::Music(p);
|
||||
soundObj->openFromFile(p.c_str());
|
||||
}
|
||||
|
||||
static SoundManager* soundManager = nullptr;
|
||||
|
||||
SoundManager* SoundManager::getSingleton(){
|
||||
if(soundManager == nullptr)
|
||||
soundManager = new SoundManager();
|
||||
|
||||
return soundManager;
|
||||
}
|
||||
|
||||
void SoundManager::update(){
|
||||
if(!currentPlaylistTracks.empty()){
|
||||
sf::SoundSource::Status status = currentPlaylistTracks[currentTrackId].soundObj->getStatus();
|
||||
|
||||
if(status == sf::SoundSource::Status::Stopped){
|
||||
if(getTime() - lastPlayTime < trackDelay) return;
|
||||
|
||||
bool lastTrack = (currentTrackId == currentPlaylistTracks.size() - 1);
|
||||
|
||||
if(lastTrack && loop)
|
||||
currentTrackId = 0;
|
||||
else if(lastTrack && !loop)
|
||||
clearPlaylist();
|
||||
else if(!lastTrack)
|
||||
currentTrackId++;
|
||||
|
||||
if(!currentPlaylistTracks.empty()){
|
||||
currentPlaylistTracks[currentTrackId].soundObj->play();
|
||||
currentPlaylistTracks[currentTrackId].soundObj->setVolume(volume);
|
||||
currentPlaylistTracks[currentTrackId].soundObj->setLooping(false);
|
||||
}
|
||||
}
|
||||
else if(status == sf::SoundSource::Status::Playing)
|
||||
lastPlayTime = getTime();
|
||||
}
|
||||
}
|
||||
|
||||
void SoundManager::clearPlaylist(){
|
||||
for(Track &track : currentPlaylistTracks)
|
||||
delete track.soundObj;
|
||||
|
||||
currentPlaylistTracks.clear();
|
||||
originalPlaylist.clear();
|
||||
}
|
||||
|
||||
void SoundManager::play(vector<string> playlist, int volume, int trackDelay, bool loop, bool shuffle){
|
||||
bool samePlaylist = (playlist.size() == originalPlaylist.size());
|
||||
|
||||
if(samePlaylist)
|
||||
for(int i = 0; i < originalPlaylist.size(); i++)
|
||||
if(originalPlaylist[i] != playlist[i]){
|
||||
samePlaylist = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if(samePlaylist) return;
|
||||
|
||||
this->loop = loop;
|
||||
this->shuffle = shuffle;
|
||||
this->volume = volume;
|
||||
this->trackDelay = trackDelay;
|
||||
|
||||
clearPlaylist();
|
||||
|
||||
for(string path : playlist)
|
||||
currentPlaylistTracks.push_back(Track(path));
|
||||
|
||||
if(shuffle){
|
||||
random_device rd;
|
||||
mt19937 g(rd());
|
||||
std::shuffle(currentPlaylistTracks.begin(), currentPlaylistTracks.end(), g);
|
||||
}
|
||||
|
||||
originalPlaylist = playlist;
|
||||
currentTrackId = 0;
|
||||
currentPlaylistTracks[currentTrackId].soundObj->play();
|
||||
currentPlaylistTracks[currentTrackId].soundObj->setVolume(volume);
|
||||
currentPlaylistTracks[currentTrackId].soundObj->setLooping(false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef SOUND_MANAGER_H
|
||||
#define SOUND_MANAGER_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
namespace sf{
|
||||
class Music;
|
||||
}
|
||||
|
||||
namespace gameBase{
|
||||
class SoundManager{
|
||||
public:
|
||||
static SoundManager* getSingleton();
|
||||
void update();
|
||||
void play(std::vector<std::string>, int, int, bool, bool);
|
||||
void clearPlaylist();
|
||||
private:
|
||||
struct Track{
|
||||
std::string path = "";
|
||||
sf::Music *soundObj = nullptr;
|
||||
|
||||
Track(std::string);
|
||||
};
|
||||
SoundManager(){}
|
||||
|
||||
int currentTrackId = 0, volume, trackDelay;
|
||||
bool loop, shuffle;
|
||||
s64 lastPlayTime = 0;
|
||||
std::vector<Track> currentPlaylistTracks;
|
||||
std::vector<std::string> originalPlaylist;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user