M1 Mac使用SFML 2.5.1时窗口无法打开问题求助
问题:M1 MacBook上SFML 2.5.1窗口无法打开+弃用警告
环境与代码
我使用M1 MacBook,当前SFML版本为2.5.1,编写的代码如下:
// Include important libraries here #include <SFML/Graphics.hpp> // Make code easier to type with "using namespace" using namespace sf; // This is where our game starts from int main() { // Create a video mode object VideoMode vm(1920, 1080); // Create and open a window for the game RenderWindow window(vm, "Timber!!!", Style::Fullscreen); while (window.isOpen()) { // Handle the players input if (Keyboard::isKeyPressed(Keyboard::Escape)) { window.close(); } // Update the scene // Draw the scene // Clear everything from the last scene window.clear(); // Draw our game scene here // Show everything we just drew window.display(); } return 0; }
运行异常
代码运行后窗口未打开,同时弹出多个警告,内容为**"Declaration is marked with '\deprecated' command but does not have a deprecation attribute"**,不确定这些警告是否与窗口无法打开相关,求助解决。
警告详情:
/Users/charlescreighton/Desktop/stuff/xcode_projects/External Libraries/SFML/include/SFML/Window/Keyboard.hpp:161:41: Declaration is marked with '\deprecated' command but does not have a deprecation attribute /Users/charlescreighton/Desktop/stuff/xcode_projects/Timber/Timber/main.cpp:9:10: in file included from /Users/charlescreighton/Desktop/stuff/xcode_projects/Timber/Timber/main.cpp:9: /Users/charlescreighton/Desktop/stuff/xcode_projects/External Libraries/SFML/include/SFML/Graphics.hpp:32:10: in file included from /Users/charlescreighton/Desktop/stuff/xcode_projects/External Libraries/SFML/include/SFML/Graphics.hpp:32: /Users/charlescreighton/Desktop/stuff/xcode_projects/External Libraries/SFML/include/SFML/Window.hpp:37:10: in file included from /Users/charlescreighton/Desktop/stuff/xcode_projects/External Libraries/SFML/include/SFML/Window.hpp:37: /Users/charlescreighton/Desktop/stuff/xcode_projects/External Libraries/SFML/include/SFML/Window/Event.hpp:33:10: in file included from /Users/charlescreighton/Desktop/stuff/xcode_projects/External Libraries/SFML/include/SFML/Window/Event.hpp:33: /Users/charlescreighton/Desktop/stuff/xcode_projects/External Libraries/SFML/include/SFML/Window/Event.hpp:105:10: Declaration is marked with '\deprecated' command but does not have a deprecation attribute /Users/charlescreighton/Desktop/stuff/xcode_projects/Timber/Timber/main.cpp:9:10: in file included from /Users/charlescreighton/Desktop/stuff/xcode_projects/Timber/Timber/main.cpp:9: /Users/charlescreighton/Desktop/stuff/xcode_projects/External Libraries/SFML/include/SFML/Graphics.hpp:32:10: in file included from /Users/charlescreighton/Desktop/stuff/xcode_projects/External Libraries/SFML/include/SFML/Graphics.hpp:32: /Users/charlescreighton/Desktop/stuff/xcode_projects/External Libraries/SFML/include/SFML/Window.hpp:37:10: in file included from /Users/charlescreighton/Desktop/stuff/xcode_projects/External Libraries/SFML/include/SFML/Window.hpp:37: /Users/charlescreighton/Desktop/stuff/xcode_projects/External Libraries/SFML/include/SFML/Graphics/PrimitiveType.hpp:52:43: Declaration is marked with '\deprecated' command but does not have a deprecation attribute /Users/charlescreighton/Desktop/stuff/xcode_projects/Timber/Timber/main.cpp:9:10: in file included from /Users/charlescreighton/Desktop/stuff/xcode_projects/Timber/Timber/main.cpp:9: /Users/charlescreighton/Desktop/stuff/xcode_projects/External Libraries/SFML/include/SFML/Graphics.hpp:34:10: in file included from /Users/charlescreighton/Desktop/stuff/xcode_projects/External Libraries/SFML/include/SFML/Graphics.hpp:34: /Users/charlescreighton/Desktop/stuff/xcode_projects/External Libraries/SFML/include/SFML/Graphics/CircleShape.hpp:32:10: in file included from /Users/charlescreighton/Desktop/stuff/xcode_projects/External Libraries/SFML/include/SFML/Graphics/CircleShape.hpp:32: /Users/charlescreighton/Desktop/stuff/xcode_projects/External Libraries/SFML/include/SFML/Graphics/Shape.hpp:34:10: in file included from /Users/charlescreighton/Desktop/stuff/xcode_projects/External Libraries/SFML/include/SFML/Graphics/Shape.hpp:34: /Users/charlescreighton/Desktop/stuff/xcode_projects/External Libraries/SFML/include/SFML/Graphics/VertexArray.hpp:33:10: in file included from /Users/charlescreighton/Desktop/stuff/xcode_projects/External Libraries/SFML/include/SFML/Graphics/VertexArray.hpp:33:
解决办法
关于弃用警告
这些警告是SFML 2.5.1的文档注释问题,和代码功能无关,不会导致窗口无法打开。原因是SFML头文件中使用了\deprecated标记,但未添加对应的C++弃用属性(如[[deprecated]]),clang编译器会抛出该警告。可通过以下方式处理:
- 升级到SFML 2.6.0及以上版本,官方已修复此问题;
- 在Xcode中禁用该警告:进入项目Build Settings,找到
Other Warning Flags,添加-Wno-documentation-deprecated-sync。
关于窗口无法打开(M1 MacBook专属问题)
SFML 2.5.1对Apple Silicon(M1/M2)支持不完善,全屏模式易出问题,可尝试以下方案:
- 修正全屏初始化逻辑:不要直接指定分辨率,改用系统桌面分辨率创建窗口,或者先创建窗口再切换全屏:
或:// 方案1:获取主显示器分辨率 VideoMode vm = VideoMode::getDesktopMode(); RenderWindow window(vm, "Timber!!!", Style::Fullscreen);// 方案2:先创建窗口再切换全屏 RenderWindow window(VideoMode(1920, 1080), "Timber!!!"); window.setFullscreen(true); - 使用原生Apple Silicon编译的SFML:SFML 2.5.1预编译包多为x86_64架构,在M1上通过Rosetta运行易出问题。建议自行从源码编译SFML 2.5.1,或直接升级到SFML 2.6.0+(官方提供原生Apple Silicon预编译包)。
- 检查Xcode权限:前往系统设置→隐私与安全性→屏幕录制,勾选Xcode,确保其拥有全屏权限。
- 完善事件循环:当前代码未处理SFML事件队列,可能导致窗口无响应。修改主循环添加事件处理:
while (window.isOpen()) { // 处理事件队列 Event event; while (window.pollEvent(event)) { if (event.type == Event::Closed) window.close(); if (event.type == Event::KeyPressed && event.key.code == Keyboard::Escape) window.close(); } // 原有绘制逻辑 window.clear(); window.display(); }
内容的提问来源于stack exchange,提问作者Charles Creighton
相关产品推荐
相关产品推荐

