跳转到内容

迁移指南

从 SFML 2 或 3.0 升到 3.1 时,下列变更最容易踩坑。完整清单见官方 2→3 与 3.0→3.1 迁移页,以及源码 migration.md。

  1. C++17 + 新 CMake 目标名(SFML::Graphics,组件首字母大写)。
  2. 所有 (x,y) 参数 → Vector2:setPosition({x,y})、VideoMode({w,h})、IntRect({x,y},{w,h})。
  3. Event 全面重写:无 event.type / event.key;用 optional + is / getIf 或 handleEvents。
  4. Angle:setRotation(sf::degrees(90)),读角 .asDegrees()。
  5. Scoped enums:Keyboard::Key::A、Mouse::Button::Left、PrimitiveType::Triangles …
  6. Rect:.position / .size;findIntersection。
  7. 无默认构造:Sprite(texture)、Text(font)、Sound(buffer)。
  8. 资源构造即加载,失败抛 sf::Exception;旧式 loadFromFile / openFromFile 仍可用。
  9. 重命名: Font openFromFile;Texture / Image / RenderTexture resize;Sound setLooping;getNativeHandle;antiAliasing。
  10. 删除: 线程原语、NonCopyable、View::reset、Vertex 构造、Text::setColor、Shader::setParameter、RenderWindow::capture、PrimitiveType::Quads、旧 MouseWheel 事件等。
  11. Fullscreen:用 sf::State::Fullscreen,不再靠 Style::Fullscreen。
  12. RenderStates 增加 CoordinateType(及 StencilMode 等)成员。
  13. 音频 Channel Map 改 loadFromSamples 等签名。
  14. Clipboard / Keyboard / … 是 namespace 不是类。
  • 文本塑形 / BiDi / 竖排 / GlyphPreProcessor;弃用 findCharacterPos。
  • Dns 替代 IpAddress::resolve;Sftp 替代 Ftp;Http HTTPS;SocketSelector 回调。
  • QOI 图像;Vector3u;String 的 string_view;PlaybackDevice::getDeviceSampleRate。
  • Touch 实时 API 弃用。