Skip to content

Instantly share code, notes, and snippets.

@newvertex
Created August 3, 2021 14:54
Show Gist options
  • Save newvertex/f7c6cb6c74c0fab57be72a6a5b5e995d to your computer and use it in GitHub Desktop.
Save newvertex/f7c6cb6c74c0fab57be72a6a5b5e995d to your computer and use it in GitHub Desktop.
Basic setup for SFML in Rust lang
// Cargo.toml
// ------------------------
// [dependencies]
// # on Windows OS don't forget to copy native CSFML *.dll & *.lib files next to your project Cargo.toml file
// sfml = "0.16.0"
// I did not find any way to use OpenGL with SFML,
// if you know a way I will be happy to hear it ;-)
// But you can still use the library with the same template
use sfml::{ self, graphics::*, window::*, system::* };
const WIDTH: u32 = 480;
const HEIGHT: u32 = 320;
const TITLE: &str = "Hello from SFML";
fn main() {
create_sfml_window(WIDTH, HEIGHT, TITLE);
}
fn create_sfml_window(width: u32, height: u32, title: &str) {
let settings = ContextSettings::default();
let mode = VideoMode::new(width, height, 32);
let style = Style::TITLEBAR | Style::CLOSE;
// create main window
let mut window = RenderWindow::new(mode, title, style, &settings);
window.set_framerate_limit(60);
window.set_key_repeat_enabled(false);
window.set_mouse_cursor_visible(true);
// Create a simple rectangle for test
let mut rect = RectangleShape::default();
rect.set_size(Vector2::new(100.0, 150.0));
rect.set_position(Vector2{ x: 50.0, y: 80.0 });
rect.set_fill_color(Color::CYAN);
let mut is_running = true;
loop {
// handle events
while let Some(event) = window.poll_event() {
sfml_handle_event(&mut window, event, &mut is_running);
}
if !is_running {
break;
}
// render
window.clear(Color::BLACK);
window.draw(&rect);
// draw on screen
window.display();
}
}
fn sfml_handle_event(window: &mut RenderWindow, event: Event, is_running: &mut bool) {
match event {
Event::Closed | Event::KeyPressed { code: Key::ESCAPE, ..} => {
*is_running = false;
},
// handle other events here
_ => {},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment