Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
asciiGame/music_play.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
40 lines (29 sloc)
1.08 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*music_play.cpp | |
This file will allow us to create a thread to play music in the background of our game on a loop | |
*/ | |
//this file will create a thread that will play music, because it is a thread it can be just called at the start of the game and then left to loop and it will keep repeating whilst the rest of the program continues | |
#include <iostream> | |
#include <thread> | |
#include <SFML/Audio.hpp> | |
#include "music_play.h" | |
void playMusic(){ | |
sf::Music Music1; | |
//this opens the file and begins to play it, the argument for openFromFile is filepath, in this case it is the relative file path and so the music must be in the project folder, we can add more music or change the name etc this just demonstrates that it works. | |
while (true){ | |
if (!Music1.openFromFile("./background_music.wav")) | |
{ | |
std::cout<<"Error"; | |
} | |
else | |
{ | |
std::cout<<"Opened."; | |
Music1.play(); | |
while (true){} | |
} | |
} | |
} | |
void createMusicThread() { | |
std::thread playThread(playMusic); | |
playThread.detach(); | |
return; | |
} |