Skip to content
Permalink
master
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
/*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;
}