我试图构建一个使用SDL2的游戏(编译很好,但是在执行二进制文件时我遇到了seg错误)和SFML(我无法成功编译)。
这些是我得到的错误:
pi@raspberrypi:~/spaceinvaders $ scons --use_sfml
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o audio/player.o -c -Wextra -I. -DUSE_BOOST_CHRONO -DBOOST_SP_DISABLE_THREADS -DTIXML_USE_STL -O3 -DUSE_SFML -DNDEBUG audio/player.cpp
audio/player.cpp: In member function 'sf::SoundBuffer oci::audio::{anonymous}::Loader::operator()(const string&)':
audio/player.cpp:52:16: error: 'class sf::SoundBuffer' has no member named 'LoadFromMemory'
audio/player.cpp: In constructor 'oci::audio::{anonymous}::ControllerImpl::ControllerImpl(const sf::SoundBuffer&, bool, bool)':
audio/player.cpp:61:24: error: no matching function for call to 'sf::Sound::Sound(const sf::SoundBuffer&, bool&)'
audio/player.cpp:61:24: note: candidates are:
/usr/local/include/SFML/Audio/Sound.hpp:69:5: note: sf::Sound::Sound(const sf::Sound&)
/usr/local/include/SFML/Audio/Sound.hpp:69:5: note: candidate expects 1 argument, 2 provided
/usr/local/include/SFML/Audio/Sound.hpp:61:14: note: sf::Sound::Sound(const sf::SoundBuffer&)
/usr/local/include/SFML/Audio/Sound.hpp:61:14: note: candidate expects 1 argument, 2 provided
/usr/local/include/SFML/Audio/Sound.hpp:53:5: note: sf::Sound::Sound()
/usr/local/include/SFML/Audio/Sound.hpp:53:5: note: candidate expects 0 arguments, 2 provided
audio/player.cpp:64:20: error: 'class sf::Sound' has no member named 'Play'
audio/player.cpp: In member function 'virtual bool oci::audio::{anonymous}::ControllerImpl::IsPlaying() const':
audio/player.cpp:68:23: error: 'const class sf::Sound' has no member named 'GetStatus'
audio/player.cpp: At global scope:
audio/player.cpp:77:1: error: 'shared_ptr' in namespace 'std' does not name a type
scons: *** [audio/player.o] Error 1
scons: building terminated because of errors.
代码已于2015年上次更新,因此我假设它使用的是一些较旧的SFML版本(我不知道),在最新版本中,某些功能已更改且兼容性已损坏。我无法测试旧版本,因为只有一个版本可用于RPi3(2.4)。
audio / player.cpp文件:
#include "player.h"
#ifdef USE_SFML
# include <SFML/Audio/Sound.hpp>
# include <SFML/Audio/SoundBuffer.hpp>
#else
# include <SDL2/SDL_mixer.h>
#endif
#include <portability/cpp11.h>
#include <resources/loader.h>
#include <stdexcept>
#include <utils/cache.h>
namespace oci {
namespace audio {
#ifdef USE_SFML
namespace {
class Loader {
public:
sf::SoundBuffer operator()(const std::string& name) {
std::vector<char> data =
resources::ResourcesLoader::Instance().GetData("sfx/" + name);
if(data.empty())
throw std::logic_error("Sound resource \"sfx/" + name +
"\" is empty");
sf::SoundBuffer sb;
if(!sb.LoadFromMemory(&data[0], data.size()))
throw std::logic_error("Cannot load sound \"" + name + "\"");
return sb;
}
};
class ControllerImpl : public Controller {
public:
ControllerImpl(const sf::SoundBuffer& sb, bool autoplay, bool loop) :
mSound(sb, loop)
{
if(autoplay)
mSound.Play();
}
virtual bool IsPlaying() const override {
return mSound.GetStatus() == sf::Sound::Playing;
}
private:
sf::Sound mSound;
};
} // namespace
std::shared_ptr<Controller> Play(const std::string& name, bool autoplay,
bool loop) {
static Cache<sf::SoundBuffer, Loader> cache;
const sf::SoundBuffer& sb = cache.Get(name);
return std::make_shared<ControllerImpl>(sb, autoplay, loop);
}
#else
// SDL audio code, not needed
#endif
} // namespace audio
} // namespace oci