Out of curiosity i wanted to take a look at Rust and to see how the "Hello World" with the SFML Library would look like, which would basically open a graphics window and render something onto the screen. Following the available documentation on several online-sources turned out to not work out for me and getting Rust-SFML to compile with Mingw-w64 on Windows turned out to be tricky. Here are the steps how i have done it:
Prerequisites:
1. Mingw-w64 Compiler (get it from here: https://winlibs.com/)
2. CSFML 2.5.1 (get source from here: https://www.sfml-dev.org/download/csfml/)
3. SFML 2.5.1 (get source from here: https://www.sfml-dev.org/download/sfml/2.5.1/)
4. Rust Toolchain (download rustup-init.exe from https://rustup.rs/)
5. CMake (get from https://cmake.org/download/)
I. Installing Rust Toolchain
When running rustup-init.exe we need to tell rustup that we do not want to use the default MSVC ABI but Mingw-w64.
When starting up rustup-init.exe you will see that the toolchain defaults to "x86_64-pc-windows-msvc". Here we need to change that to mingw. For this we choose the 2) option ("Customize installation") and provide the value "x86_64-pc-windows-gnu":
Leave the rest at default.
We end up with something like this
and can now proceed with option 1) ("Proceed with installation (default")
II. Install SFML and CSFML
SFML must be installed before CSFML, since CSFML depends on SFML (at least thats what CMake tells us if we try to install CSFML first).
When CMake asks to specify the generator for this project select "Mingw Makefiles" and generate the CMake-Files in the previously specified build-folder (here i selected C:/SFML as my build-folder for SFML). Use CMD with admin-privileges, enter into the build-folder and install SFML with the command "mingw32-make install".
The compiled lib- and dll-files will reside in C:\Program Files (x86)\SFML.
Repeat the process now with CSFML, just remember to use a different build-folder (like C:/CSFML) for the CMake-Files. We should end up with lib-and dll-files for CSFML in the appropriate subfolders in C:\Program Files (x86)\CSFML.
We are already halfway there.
III. Setting up IDE and Libraries
add sfml to Cargo.toml:
[package] name = "your_project_name" version = "0.1.0" edition = "2021" [dependencies] sfml = "0.16.0"
The rust-sfml example code that is being found on the official docs (https://docs.rs/sfml/0.11.2/sfml/) does unfortunately not compile because it is outdated and relies on old versions of SFML and CSFML.
However, the following "Hello World" should do:
extern crate sfml; use sfml::system::Vector2f; use sfml::window::{ContextSettings, VideoMode, Event, Style}; use sfml::graphics::{RenderWindow, RenderTarget, CircleShape, Color, Transformable, Shape}; fn main() { // Create the window of the application let mut window : RenderWindow = RenderWindow::new(VideoMode::new(800, 600, 32), "SFML Example", Style::CLOSE, &ContextSettings::default()); // Create a CircleShape let mut circle = CircleShape::new(1.0, 12); circle.set_radius(30.); circle.set_fill_color(Color::BLUE); circle.set_position(Vector2f::new(100., 100.)); while window.is_open() { // Handle events for event in window.poll_event() { match event { Event::Closed => window.close(), _ => {/* do nothing */} } } // Clear the window window.clear(Color::RED); // Draw the shape window.draw(&circle); // Display things on screen window.display() } }
Unfortunately, this will still not compile, because Rust will not find the needed libs. Here the previously compiled SFML and CSFML libraries come into place:
Grab all *.a files from C:\Program Files (x86)\CSFML\lib
and copy them to
C:\Users\USER\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib
Now the code example should compile. If you try to actually run it, it will complain that dll-files are missing. These can be found in C:\Program Files (x86)\CSFML\bin and must be copied to the target-folder of the project, where the compiled *.exe file of the program is located.
Now the rust-sfml project should compile and run.