Browse Source

Display single sprite

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

+ 14
- 3
src/main.rs View File

use sdl2::keyboard::Keycode; use sdl2::keyboard::Keycode;
use sdl2::render::{WindowCanvas, Texture}; use sdl2::render::{WindowCanvas, Texture};
use sdl2::image::{self, LoadTexture, InitFlag}; use sdl2::image::{self, LoadTexture, InitFlag};
use sdl2::rect::{Point, Rect};
use std::time::Duration; use std::time::Duration;


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


canvas.copy(texture, None, None)?;
let (width, height) = canvas.output_size()?;
let screen_position = position + Point::new(width as i32 / 2, height as i32 / 2);
let screen_rect = Rect::from_center(screen_position, sprite.width(), sprite.height());
canvas.copy(texture, sprite, screen_rect)?;


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


let texture_creator = canvas.texture_creator(); let texture_creator = canvas.texture_creator();
let texture = texture_creator.load_texture("assets/bardo.png")?; let texture = texture_creator.load_texture("assets/bardo.png")?;


let position = Point::new(0,0);
let sprite = Rect::new(0, 0, 26, 26);

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), &texture)?;
render(&mut canvas, Color::RGB(i, 64, 255-i), &texture, position, sprite)?;


::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