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

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html


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

+ 13
- 3
src/main.rs View File

use sdl2::pixels::Color; use sdl2::pixels::Color;
use sdl2::event::Event; use sdl2::event::Event;
use sdl2::keyboard::Keycode; use sdl2::keyboard::Keycode;
use sdl2::render::WindowCanvas;
use sdl2::render::{WindowCanvas, Texture};
use sdl2::image::{self, LoadTexture, InitFlag};
use std::time::Duration; 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.set_draw_color(color);
canvas.clear(); canvas.clear();

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

canvas.present(); canvas.present();

Ok(())
} }


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


let window = video_subsystem.window("game tutorial", 800, 600) let window = video_subsystem.window("game tutorial", 800, 600)
.position_centered() .position_centered()
let mut canvas = window.into_canvas().build() let mut canvas = window.into_canvas().build()
.expect("could not make a canvas"); .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 event_pump = sdl_context.event_pump()?;
let mut i = 0; let mut i = 0;
'running: loop { 'running: loop {


i = (i + 1) % 255; 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)); ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
} }

Loading…
Cancel
Save