Skip to content
Kesavan Yogeswaran edited this page Dec 23, 2024 · 8 revisions

You can find an (out-of-date) starter here: https://github.com/fabienjuif/rust-sfml-starter

Prerequisites

  • git
  • cmake: For building SFML/CSFML
  • SFML dependencies
    • flac
    • freetype
    • libogg
    • libvorbis

Installation via Homebrew:

brew install git cmake flac freetype libogg libvorbis

Summary

Running examples

Clone rust-sfml

git clone https://github.com/jeremyletang/rust-sfml.git
cd rust-sfml
git submodule update --init --recursive

Launching pong example

## from within rust-sfml directory

## indicate where to find SFML's lib dependencies
export LIBRARY_PATH=/opt/homebrew/lib

## running pong example
cargo run --example pong

Creating your project

## create your project name
cargo new my-project
cd my-project
cargo add sfml

Create my-project/build.rs with the following contents:

fn main() {
    println!("cargo::rustc-link-search=native=/opt/homebrew/lib");
    println!("cargo::rustc-link-arg=-ObjC");
}

Running

Open src/main.rs and add these lines:

extern crate sfml;

use sfml::graphics::*;
use sfml::system::*;
use sfml::window::*;

fn main() {
    let mut window = RenderWindow::new(
        (800, 600),
        "SFML VertexArray accessors Example",
        Style::CLOSE,
        &Default::default(),
    )
    .unwrap();
    window.set_vertical_sync_enabled(true);

    let mut rectangle = RectangleShape::new();
    rectangle.set_size(Vector2f::new(100.0, 100.0));
    rectangle.set_fill_color(Color::rgb(255, 0, 100));
    rectangle.set_position(Vector2f::new(100.0, 100.0));

    loop {
        // events
        while let Some(ev) = window.poll_event() {
            match ev {
                Event::Closed => {
                    window.close();
                    return;
                }
                _ => {}
            }
        }

        // drawing
        window.clear(Color::BLACK);
        window.draw(&rectangle);
        window.display();
    }
}

Then run your project: cargo run

You should see a new window opening with a red square in it!

Clone this wiki locally