Browse Source

Load sprite

master
Noëlle Anthony 4 years ago
parent
commit
719edcc65e
2 changed files with 14 additions and 4 deletions
  1. 1
    1
      Cargo.toml
  2. 13
    3
      src/main.rs

+ 1
- 1
Cargo.toml View File

@@ -7,4 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
sdl2 = "0.32"
sdl2 = { version = "0.32", default_features = false, features = ["image"] }

+ 13
- 3
src/main.rs View File

@@ -1,18 +1,25 @@
use sdl2::pixels::Color;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::render::WindowCanvas;
use sdl2::render::{WindowCanvas, Texture};
use sdl2::image::{self, LoadTexture, InitFlag};
use std::time::Duration;

fn render(canvas: &mut WindowCanvas, color: Color) {
fn render(canvas: &mut WindowCanvas, color: Color, texture: &Texture) -> Result<(), String> {
canvas.set_draw_color(color);
canvas.clear();

canvas.copy(texture, None, None)?;

canvas.present();

Ok(())
}

fn main() -> Result<(), String> {
let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?;
let _image_context = image::init(InitFlag::PNG | InitFlag::JPG)?;

let window = video_subsystem.window("game tutorial", 800, 600)
.position_centered()
@@ -22,6 +29,9 @@ fn main() -> Result<(), String> {
let mut canvas = window.into_canvas().build()
.expect("could not make a canvas");
let texture_creator = canvas.texture_creator();
let texture = texture_creator.load_texture("assets/bardo.png")?;

let mut event_pump = sdl_context.event_pump()?;
let mut i = 0;
'running: loop {
@@ -37,7 +47,7 @@ fn main() -> Result<(), String> {

i = (i + 1) % 255;

render(&mut canvas, Color::RGB(i, 64, 255-i));
render(&mut canvas, Color::RGB(i, 64, 255-i), &texture)?;

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

Loading…
Cancel
Save