Crate sdl2

source · []
Expand description

Getting started

extern crate sdl2;

use sdl2::pixels::Color;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use std::time::Duration;

pub fn main() {
    let sdl_context = sdl2::init().unwrap();
    let video_subsystem =;

    let window = video_subsystem.window("rust-sdl2 demo", 800, 600)

    let mut canvas = window.into_canvas().build().unwrap();

    canvas.set_draw_color(Color::RGB(0, 255, 255));
    let mut event_pump = sdl_context.event_pump().unwrap();
    let mut i = 0;
    'running: loop {
        i = (i + 1) % 255;
        canvas.set_draw_color(Color::RGB(i, 64, 255 - i));
        for event in event_pump.poll_iter() {
            match event {
                Event::Quit {..} |
                Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
                    break 'running
                _ => {}
        // The rest of the game loop goes here...

        ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));


pub extern crate libc;
pub extern crate sdl2_sys as sys;


Audio Functions

Event Handling

A binding for the library SDL2_gfx

Haptic Functions

A binding for the library SDL2_image

A binding for the library SDL2_mixer

Rectangles and points.

2D accelerated rendering

A binding for the library SDL2_ttf

Opening URLs in default system handlers

Querying SDL Version


A thread-safe type that encapsulates SDL event-pumping functions.

The SDL context type. Initialize with sdl2::init().


A given integer was so big that its representation as a C integer would be negative.


Get platform name

Initializes the SDL library. This must be called before using any other SDL function.