From ed1ced7cbe480b4ba7c5f8619acfa94664596539 Mon Sep 17 00:00:00 2001 From: Luke Jones Date: Sun, 20 Nov 2016 17:59:51 +1300 Subject: [PATCH] Update animation example. - There is a problem with the SDL2 libraries software renderer such that `RenderCopyEx` will draw a flipped or rotated rect in the wrong place if; - the position of both source and destination are 0, or; - the dimensions of source and destination are the same. - a workaround if using the software renderer is to make the destination 1 pixel larger x,y, or shift the source over 1 pixel. Not ideal. Issue #492 contains more details. --- examples/animation.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/examples/animation.rs b/examples/animation.rs index 8f521f88..fcc32f49 100644 --- a/examples/animation.rs +++ b/examples/animation.rs @@ -4,6 +4,7 @@ use std::path::Path; use sdl2::event::Event; use sdl2::keyboard::Keycode; use sdl2::rect::Rect; +use sdl2::rect::Point; use std::time::Duration; fn main() { @@ -14,7 +15,7 @@ fn main() { .position_centered().build().unwrap(); let mut renderer = window.renderer() - .software().build().unwrap(); + .accelerated().build().unwrap(); renderer.set_draw_color(sdl2::pixels::Color::RGBA(0,0,0,255)); @@ -23,11 +24,13 @@ fn main() { let mut event_pump = sdl_context.event_pump().unwrap(); let temp_surface = sdl2::surface::Surface::load_bmp(Path::new("assets/animate.bmp")).unwrap(); - let texture = renderer.create_texture_from_surface(temp_surface).unwrap(); + let texture = renderer.create_texture_from_surface(&temp_surface).unwrap(); let texture_query = texture.query(); + let mut center = Point::new(320,240); let mut source_rect = Rect::new(0, 0, 128, 82); - let dest_rect = Rect::new(0, 0, 128, 82); + let mut dest_rect = Rect::new(0,0, 128, 82); + dest_rect.center_on(center); let mut running = true; while running { @@ -42,15 +45,9 @@ fn main() { let ticks = timer.ticks(); - // Cycle through coords - // 0 - // 128 - // 384 - // 512 - // 640 - // source_rect.set_x((128 * ((ticks / 100) % 6) ) as i32); + source_rect.set_x((128 * ((ticks / 100) % 6) ) as i32); renderer.clear(); - renderer.copy_ex(&texture, Some(source_rect), Some(dest_rect), 0.0, None, true, false); + renderer.copy_ex(&texture, Some(source_rect), Some(dest_rect), 10.0, None, true, false); renderer.present(); std::thread::sleep(Duration::from_millis(100));