Skip to content
crumblingstatue edited this page Oct 18, 2024 · 7 revisions

Note that_ In this page we don't install libraries to the system (we are not calling sudo make install).

You can find a starter here: https://github.com/fabienjuif/rust-sfml-starter

Prerequisites

  • git: brew install git
  • cmake: brew install cmake

Summary

Running examples

Fetching dependencies

## create your project name
mkdir my-project
cd my-project

## create a vendors directory containing dependencies
mkdir vendors
cd vendors
## rust-sfml
git clone https://github.com/jeremyletang/rust-sfml.git
## CSFML
git clone https://github.com/SFML/CSFML.git
cd CSFML
### - latest stable release
git checkout $(git describe --tags $(git rev-list --tags --max-count=1))
cd ..
## SFML
git clone https://github.com/SFML/SFML.git
cd SFML
### - latest stable release
git checkout $(git describe --tags $(git rev-list --tags --max-count=1))
cd ..

Building dependencies

## SFML
cd SFML
cmake .
make all
cd ..

## CSFML
cd CSFML
### - tell CSFML where to find SFML
export SFML_DIR="$(pwd -P)/../SFML"
cmake -DCMAKE_MODULE_PATH="$SFML_DIR/cmake" .
make all
cd ..

Launching pong example

cd rust-sfml

## telling where to find dependencies libs
export LIBRARY_PATH=$(pwd)/../SFML/lib:$(pwd)/../CSFML/lib
export DYLD_LIBRARY_PATH=$(pwd)/../SFML/lib:$(pwd)/../CSFML/lib

## runing pong example
cd examples
cargo run --example pong

Creating your project

Fetching dependencies

## create your project name
cargo init my-project
cd my-project

## create a vendors directory containing dependencies
mkdir vendors
cd vendors
## CSFML
git clone https://github.com/SFML/CSFML.git
cd CSFML
### - latest stable release
git checkout $(git describe --tags $(git rev-list --tags --max-count=1))
cd ..
## SFML
git clone https://github.com/SFML/SFML.git
cd SFML
### - latest stable release
git checkout $(git describe --tags $(git rev-list --tags --max-count=1))
cd ..
cd ..

Tells your binary where to find libs

export LIBRARY_PATH=$(pwd)/vendors/SFML/lib:$(pwd)/vendors/CSFML/lib
export DYLD_LIBRARY_PATH=$(pwd)/vendors/SFML/lib:$(pwd)/vendors/CSFML/lib

Building dependencies

## SFML
cd vendors/SFML
cmake .
make all
cd ..

## CSFML
cd CSFML
### - tell CSFML where to find SFML
export SFML_DIR="$(pwd -P)/../SFML"
cmake -DCMAKE_MODULE_PATH="$SFML_DIR/cmake" .
make all
cd ..

Running

Open src/main.rs and add these lines:

extern crate sfml;

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

fn main() {
    let mut window = RenderWindow::new(
        (800, 600),
        "SFML VertexArray accessors Example",
        Style::CLOSE,
        &Default::default(),
    );
    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!