This is some test code I wrote for an alarm clock program. It seems that the sound object only plays if its inside the inner while loop. If I move the mouse cursor in and out of the window, the sound restarts. I only want the sound to play if the time is equal to a time that I want the sound to go off. I wrote a function for that (not shown here) and the function works, so thats not the problem. Why can't sound play outside of an event loop?
int main(){
sf::RenderWindow window(sf::VideoMode(400,400),"");
sf::SoundBuffer buffer;
sf::Sound sound;
sf::Event event;
if(!buffer.loadFromFile("alarmClock.ogg"))
return EXIT_FAILURE;
sound.setBuffer(buffer);
while(window.isOpen()){
while(window.pollEvent(event)){
if(event.type == sf::Event::Closed){
window.close();
}
sound.play();
}
//sound.play() <--- doesn't play here
window.display();
}
}