diff --git a/.gitmodules b/.gitmodules index f70a6e3..b0c5343 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "external/sol2"] path = external/sol2 url = https://github.com/ThePhD/sol2 +[submodule "external/SFML"] + path = external/SFML + url = git@github.com:SFML/SFML.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ffab7f..dd8631a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/external/SFML b/external/SFML new file mode 160000 index 0000000..3651072 --- /dev/null +++ b/external/SFML @@ -0,0 +1 @@ +Subproject commit 3651072cc0861eb10d67f342d8146b61e3629134 diff --git a/soundManager.cpp b/soundManager.cpp new file mode 100644 index 0000000..ef55e0e --- /dev/null +++ b/soundManager.cpp @@ -0,0 +1,60 @@ +#include "soundManager.h" + +#include + +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(currentPlaylist[currentTrackId].soundObj->getStatus() == sf::SoundSource::Status::Stopped){ + bool lastTrack = (currentTrackId == currentPlaylist.size() - 1); + + if(lastTrack && loop) + currentTrackId = 0; + else if(lastTrack && !loop) + clearPlaylist(); + else if(!lastTrack) + currentTrackId++; + + if(!currentPlaylist.empty()){ + currentPlaylist[currentTrackId].soundObj->play(); + currentPlaylist[currentTrackId].soundObj->setLooping(false); + } + } + } + + void SoundManager::clearPlaylist(){ + for(Track &track : currentPlaylist) + delete track.soundObj; + + currentPlaylist.clear(); + } + + void SoundManager::play(vector playlist, int volume, bool loop, bool shuffle){ + this->loop = loop; + this->shuffle = shuffle; + + clearPlaylist(); + + for(string path : playlist) + currentPlaylist.push_back(Track(path)); + + currentTrackId = 0; + currentPlaylist[currentTrackId].soundObj->play(); + currentPlaylist[currentTrackId].soundObj->setLooping(false); + } +} diff --git a/soundManager.h b/soundManager.h new file mode 100644 index 0000000..b1101b2 --- /dev/null +++ b/soundManager.h @@ -0,0 +1,33 @@ +#ifndef SOUND_MANAGER_H +#define SOUND_MANAGER_H + +#include +#include + +namespace sf{ + class Music; +} + +namespace gameBase{ + class SoundManager{ + public: + static SoundManager* getSingleton(); + void update(); + void play(std::vector, int, bool, bool); + private: + struct Track{ + std::string path = ""; + sf::Music *soundObj = nullptr; + + Track(std::string); + }; + SoundManager(){} + void clearPlaylist(); + + int currentTrackId = 0; + bool loop, shuffle; + std::vector currentPlaylist; + }; +} + +#endif