Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在 M1 Mac 上配置 SFML #1

Open
sko00o opened this issue Feb 5, 2023 · 1 comment
Open

在 M1 Mac 上配置 SFML #1

sko00o opened this issue Feb 5, 2023 · 1 comment
Labels
game game development

Comments

@sko00o
Copy link
Owner

sko00o commented Feb 5, 2023

准备

获取已构建好的发布包 (SFML 3 SDK)

https://artifacts.sfml-dev.org/by-branch/master/macos-clang-arm64.tar.gz

配置

mkdir sfml-macos-arm64

# 用 --strip-components 跳过第一级目录名,因为它名字真的很丑
tar xf macos-clang-arm64.tar.gz -C sfml-macos-arm64 --strip-components 1

cd sfml-macos-arm64
sudo rsync -a Library/Frameworks/*.framework /Library/Frameworks
sudo rsync -a Library/Frameworks/lib/*.framework /Library/Frameworks
sudo rsync -a Library/Frameworks/lib/*.dylib /usr/local/lib
sudo rsync -a Library/Frameworks/include/SFML /usr/local/include

测试

创建一个 main.cpp 文件

#include <SFML/Graphics.hpp>                                                                                                                                           
                                                                                                                                                                       
int main() {                                                                                                                                                           
  sf::RenderWindow window(sf::VideoMode(sf::Vector2u(200, 200)), "SFML works!");                                                                                       
  sf::CircleShape shape(100.f);                                                                                                                                        
  shape.setFillColor(sf::Color::Green);                                                                                                                                
                                                                                                                                                                       
  while (window.isOpen()) {                                                                                                                                            
    sf::Event event;                                                                                                                                                   
    while (window.pollEvent(event)) {                                                                                                                                  
      if (event.type == sf::Event::Closed) {                                                                                                                           
        window.close();                                                                                                                                                
      }                                                                                                                                                                
    }                                                                                                                                                                  
    window.clear();                                                                                                                                                    
    window.draw(shape);                                                                                                                                                
    window.display();                                                                                                                                                  
  }                                                                                                                                                                    
  return 0;                                                                                                                                                            
}
g++ -std=c++17 -c main.cpp -I/usr/local/include
g++ main.o -o sfml -lsfml-network -lsfml-audio -lsfml-graphics -lsfml-window -lsfml-system
./sfml

运行结果:

image

因为 Apple 麻烦的安全机制,需要自行在系统的 “Privacy & Security” 设置中允许依赖库的运行。

参考

@sko00o
Copy link
Owner Author

sko00o commented Feb 5, 2023

当前 master 分支是 SFML 3, API 有改动,并且教程不多。

为了运行官方教程中的例子,选用 2.6.x 分支的 SDK

https://artifacts.sfml-dev.org/by-branch/2.6.x/macos-clang-arm64.tar.gz

相应地测试代码改成

#include <SFML/Graphics.hpp>

int main() {
  sf::RenderWindow window({200, 200}, "SFML works!");
  sf::CircleShape shape(100.f);
  shape.setFillColor(sf::Color::Green);

  while (window.isOpen()) {
    sf::Event event;
    while (window.pollEvent(event)) {
      if (event.type == sf::Event::Closed) {
        window.close();
      }
    }
    window.clear();
    window.draw(shape);
    window.display();
  }
  return 0;
}

@sko00o sko00o added the game game development label Feb 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
game game development
Projects
None yet
Development

No branches or pull requests

1 participant