C++ Pthread/SFML 音乐 - 错误 AL lib: (EE) alc_cleanup: 1 设备未关闭
C++ Pthread/SFML Music - Error AL lib: (EE) alc_cleanup: 1 device not closed
我有一个要重新编码炸弹人的项目,我实现了 sf :: Music for music 添加到主菜单,但我遇到了一个错误:
AL lib: (EE) alc_cleanup: 1 device not closed
它依赖于此:
void *pthreadSound(void *);
int main(int, char **){
GameEngine *game = new GameEngine();
pthread_attr_t attr;
pthread_t sound;
void *retval;
try {
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (!game->initialize())
throw ErrorBomberman("Game fail initialisation", "main");
game->m_mutex = PTHREAD_MUTEX_INITIALIZER;
game->setLock(true);
pthread_create(&sound, &attr, pthreadSound, (void *)game);
game->ChangeState(IntroState::Instance());
while (game->Running()){
game->HandleEvents();
game->update();
game->draw();
}
game->Cleanup();
pthread_join(sound, &retval);
} catch (ErrorBomberman const &e) {
std::cerr << "Error caught in " << e.getComponent() << ": " << e.what() << std::endl;
}
return (EXIT_SUCCESS);
}
void *pthreadSound(void *game){
sf::Music music1;
if (!music1.OpenFromFile("./ressources/Bomberman_Theme.ogg"))
std::cout << "Erreur : load" << std::endl;
music1.Play();
while (((GameEngine *)game)->getLock()){
}
std::cout << "Stop" << std::endl;
music1.Stop();
pthread_exit(NULL);
return (NULL);
}
也许是因为 Stop 不起作用?我不知道。如果你有想法,谢谢。
根据 docs:
As a sound stream, a music is played in its own thread in order not to block the rest of the program. This means that you can leave the music alone after calling play()
, it will manage itself very well.
所以你真的不需要创建另一个线程来播放音乐。
我有一个要重新编码炸弹人的项目,我实现了 sf :: Music for music 添加到主菜单,但我遇到了一个错误:
AL lib: (EE) alc_cleanup: 1 device not closed
它依赖于此: void *pthreadSound(void *);
int main(int, char **){
GameEngine *game = new GameEngine();
pthread_attr_t attr;
pthread_t sound;
void *retval;
try {
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (!game->initialize())
throw ErrorBomberman("Game fail initialisation", "main");
game->m_mutex = PTHREAD_MUTEX_INITIALIZER;
game->setLock(true);
pthread_create(&sound, &attr, pthreadSound, (void *)game);
game->ChangeState(IntroState::Instance());
while (game->Running()){
game->HandleEvents();
game->update();
game->draw();
}
game->Cleanup();
pthread_join(sound, &retval);
} catch (ErrorBomberman const &e) {
std::cerr << "Error caught in " << e.getComponent() << ": " << e.what() << std::endl;
}
return (EXIT_SUCCESS);
}
void *pthreadSound(void *game){
sf::Music music1;
if (!music1.OpenFromFile("./ressources/Bomberman_Theme.ogg"))
std::cout << "Erreur : load" << std::endl;
music1.Play();
while (((GameEngine *)game)->getLock()){
}
std::cout << "Stop" << std::endl;
music1.Stop();
pthread_exit(NULL);
return (NULL);
}
也许是因为 Stop 不起作用?我不知道。如果你有想法,谢谢。
根据 docs:
As a sound stream, a music is played in its own thread in order not to block the rest of the program. This means that you can leave the music alone after calling
play()
, it will manage itself very well.
所以你真的不需要创建另一个线程来播放音乐。