最近在写一个Halma Chess的课程作业,因为想为简陋的C++控制台程序简单的配置一个UI,所以选择了SFML。
SFML为PC的各种组件提供了一个简单的界面,可以简化游戏和多媒体应用程序的开发。所以用来做小游戏的demo非常便利~
SFML下载传送门:https://www.sfml-dev.org/download.php
SFML官方配置及使用指南: https://www.sfml-dev.org/tutorials/2.5/
发现了这么简便的库,马上就想试试呢!!可是!!我居然在配环境上花了几天时间!!(蛋疼,重锤自己一下T^T)
因为官网提供的是以下指引:
- SFML and Visual Studio
- SFML and Code:: Blocks (MinGW)
- SFML and Linux
- SFML and Xcode (macOS)
- Compiling SFML with CMake
不过我用的是Visual Studio Code + MinGW 编译器 = =。。 我果然是离开了傻瓜式教学就不行了 = =。。只好开始DIY在VSCode上的环境了。
- 下载:官方强调编译器版本和软件包版本一致,所以在下载之后使用之前请确定好版本对应!!(我就因为偷懒没注意在上面载了几天= =。详情请戳https://www.sfml-dev.org/download/sfml/2.5.1/)
我使用的是:
SFML 软件包 GCC 7.3.0 MinGW(DW2)64位 下载:https://www.sfml-dev.org/files/SFML-2.5.1-windows-gcc-7.3.0-mingw-64-bit.zip
MinGW 7.3.0 64位 下载:https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/7.3.0/threads-posix/seh/x86_64-7.3.0-release-posix-seh-rt_v5-rev0.7z/download
2.配置:VSCode build task文件
task.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "g++",
"args": [
"-g",
"-o",
"path/to/output",
"-std=c++11",
"path/to/source/file",
"-IC:/SFML-2.5.1/include",
"-LC:/SFML-2.5.1/lib",
"-lsfml-graphics",
"-lsfml-window",
"-lsfml-system"
],
"options": {
"cwd": "C:\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "path/to/gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/path/to/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
setting.json
{
"files.associations": {
"iostream": "cpp"
}
}
其实,只要注意一下在使用时把tasks.json里面的-I、-L、-l写好,注意版本对应,应该就不会有使用上的大问题了。
3.样例:
#include <SFML/Graphics.hpp>
#include<time.h>
using namespace sf;
int main()
{
RenderWindow window(VideoMode(350,350), "Sample");
Texture t1;
t1.loadFromFile("res/board.jpg");
Sprite s(t1);
while (window.isOpen())
{
Event e;
while (window.pollEvent(e))
{
if (e.type == Event::Closed)
window.close();
}
//draw
window.clear();
window.draw(s);
window.display();
}
return 0;
}
4. 编译:Terminal-> Run Build Task... (或 Ctrl+Shift+B)
5. 运行:此时要注意讲SFML/bin下的dll放到生成的exe下的目录噢~~, 否则会报缺少dll的辣!
相关链接:
1. tasks.json sample:https://git.1248.nz/1248/SFML/src/branch/master/.vscode/tasks.json
2. vscode + sfml template:https://github.com/andrew-r-king/sfml-vscode-boilerplate