|
|
|
|
|
|
|
|
use sdl2::rect::{Point, Rect}; |
|
|
use sdl2::rect::{Point, Rect}; |
|
|
use std::time::Duration; |
|
|
use std::time::Duration; |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)] |
|
|
|
|
|
struct Player { |
|
|
|
|
|
position: Point, |
|
|
|
|
|
sprite: Rect, |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
fn render(canvas: &mut WindowCanvas, |
|
|
fn render(canvas: &mut WindowCanvas, |
|
|
color: Color, |
|
|
color: Color, |
|
|
texture: &Texture, |
|
|
texture: &Texture, |
|
|
position: Point, |
|
|
|
|
|
sprite: Rect) -> Result<(), String> { |
|
|
|
|
|
|
|
|
player: &Player) -> Result<(), String> { |
|
|
canvas.set_draw_color(color); |
|
|
canvas.set_draw_color(color); |
|
|
canvas.clear(); |
|
|
canvas.clear(); |
|
|
|
|
|
|
|
|
let (width, height) = canvas.output_size()?; |
|
|
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)?; |
|
|
|
|
|
|
|
|
let screen_position = player.position + Point::new(width as i32 / 2, height as i32 / 2); |
|
|
|
|
|
let screen_rect = Rect::from_center(screen_position, player.sprite.width(), player.sprite.height()); |
|
|
|
|
|
canvas.copy(texture, player.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 player = Player { |
|
|
|
|
|
position: Point::new(0,0), |
|
|
|
|
|
sprite: Rect::new(0, 0, 26, 36), |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
let mut event_pump = sdl_context.event_pump()?; |
|
|
let mut event_pump = sdl_context.event_pump()?; |
|
|
let mut i = 0; |
|
|
let mut i = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
i = (i + 1) % 255; |
|
|
i = (i + 1) % 255; |
|
|
|
|
|
|
|
|
render(&mut canvas, Color::RGB(i, 64, 255-i), &texture, position, sprite)?; |
|
|
|
|
|
|
|
|
render(&mut canvas, Color::RGB(i, 64, 255-i), &texture, &player)?; |
|
|
|
|
|
|
|
|
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60)); |
|
|
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60)); |
|
|
} |
|
|
} |