我完全按照说明做了一切,结果和往常一样,我读了文档,这个错误没有指定,在控制台中引号中应该是音频文件的目录,但它是空的,你怎么理解它?我在网上搜索了很多,没有关于这个主题的内容。 视觉工作室 2022, C++, CMake, SFML 2.6.1, 视窗 11, 英特尔酷睿 i5 9400f, 内存 16GB。
#include "chucknorris.h"
#include "Button.h"
#include <SFML/Audio.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(1280, 720), "SFML works!"/*, sf::Style::Fullscreen*/);
sf::CircleShape shape(100.f, 100);
shape.setFillColor(sf::Color::Green);
int x = 500;
int y = 250;
Button button(sf::Vector2i(5, 5), sf::Vector2i(5, 5));
sf::Music music;
if (!music.openFromFile("./ex.ogg"))
{
std::cout << "idi nahuy\n";
}
music.play();
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
sf::Mouse::getPosition();
if (event.type == sf::Event::Closed)
window.close();
}
shape.setPosition(sf::Vector2f(x, y));
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
我希望 sfml 能够正常工作并正常搜索目录中的文件。
我认为问题可能与您用来访问音频文件的路径有关......
您必须确保音频文件的文件路径正确。您当前正在使用 ./ex.ogg。这意味着程序正在当前工作目录中查找 ex.ogg。仔细检查 ex.ogg 是否与可执行文件位于同一目录中,如果不存在,请修改文件所在的路径,或者将其移动到当前工作目录中。
if (!music.openFromFile("./ex.ogg"))
{
// Print the current directory
char cwd[1024];
if (_getcwd(cwd, sizeof(cwd)) != NULL)
{
std::cout << "Current directory: " << cwd << std::endl;
}
else
{
perror("_getcwd() error");
}
// Print SFML error message
std::cout << "Failed to open sound file: " << sf::err().last << std::endl;
}
此代码将打印当前目录和 SFML 错误消息以帮助调试。