Noëlle Anthony 4 лет назад
Родитель
Сommit
2f3c5b5cbd
1 измененных файлов: 14 добавлений и 3 удалений
  1. 14
    3
      src/main.rs

+ 14
- 3
src/main.rs Просмотреть файл

@@ -3,13 +3,21 @@ use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::render::{WindowCanvas, Texture};
use sdl2::image::{self, LoadTexture, InitFlag};
use sdl2::rect::{Point, Rect};
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.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();

@@ -32,6 +40,9 @@ 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 mut event_pump = sdl_context.event_pump()?;
let mut i = 0;
'running: loop {
@@ -47,7 +58,7 @@ fn main() -> Result<(), String> {

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

Загрузка…
Отмена
Сохранить