Parcourir la source

Convert to struct

master
Noëlle Anthony il y a 4 ans
Parent
révision
f33f4fd236
1 fichiers modifiés avec 15 ajouts et 8 suppressions
  1. 15
    8
      src/main.rs

+ 15
- 8
src/main.rs Voir le fichier

@@ -6,18 +6,23 @@ use sdl2::image::{self, LoadTexture, InitFlag};
use sdl2::rect::{Point, Rect};
use std::time::Duration;

#[derive(Debug)]
struct Player {
position: Point,
sprite: Rect,
}

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

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();

@@ -40,8 +45,10 @@ fn main() -> Result<(), String> {
let texture_creator = canvas.texture_creator();
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 i = 0;
@@ -58,7 +65,7 @@ fn main() -> Result<(), String> {

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));
}

Chargement…
Annuler
Enregistrer