From cb630ffb8e433b420233560f2aff39f0b8685509 Mon Sep 17 00:00:00 2001 From: Michel Heily Date: Fri, 20 Dec 2019 17:48:24 +0200 Subject: [PATCH] Optimise SDL2 rendering code Former-commit-id: 4a0e4f57a97c98bc8a930901bd1ba2125d96138b --- src/plat/sdl2/video.rs | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/src/plat/sdl2/video.rs b/src/plat/sdl2/video.rs index fe13564..78fe2e7 100644 --- a/src/plat/sdl2/video.rs +++ b/src/plat/sdl2/video.rs @@ -1,11 +1,10 @@ use sdl2::pixels::{Color, PixelFormatEnum}; -use sdl2::rect::{Point, Rect}; +use sdl2::rect::Rect; use sdl2::render::{TextureCreator, WindowCanvas}; use sdl2::video::WindowContext; use sdl2::Sdl; use rustboyadvance_ng::core::gpu::{DISPLAY_HEIGHT, DISPLAY_WIDTH}; -use rustboyadvance_ng::util::FpsCounter; use rustboyadvance_ng::VideoInterface; const SCREEN_WIDTH: u32 = DISPLAY_WIDTH as u32; @@ -15,12 +14,11 @@ const SCALE: u32 = 3; // TODO control via CLI & support window resize pub struct Sdl2Video { tc: TextureCreator, canvas: WindowCanvas, - fps_counter: FpsCounter, } impl Sdl2Video { pub fn set_window_title(&mut self, title: &str) { - self.canvas.window_mut().set_title(&title); + self.canvas.window_mut().set_title(&title).unwrap(); } } @@ -28,19 +26,14 @@ impl VideoInterface for Sdl2Video { fn render(&mut self, buffer: &[u32]) { let mut texture = self .tc - .create_texture_target(PixelFormatEnum::RGB24, SCREEN_WIDTH, SCREEN_HEIGHT) + .create_texture_streaming(PixelFormatEnum::BGRA32, SCREEN_WIDTH, SCREEN_HEIGHT) .unwrap(); - self.canvas - .with_texture_canvas(&mut texture, |texture_canvas| { - for y in 0i32..(SCREEN_HEIGHT as i32) { - for x in 0i32..(SCREEN_WIDTH as i32) { - let c = buffer[index2d!(x, y, SCREEN_WIDTH as i32) as usize]; - let color = Color::RGB((c >> 16) as u8, (c >> 8) as u8, c as u8); - texture_canvas.set_draw_color(color); - let _ = texture_canvas.draw_point(Point::from((x, y))); - } - } - }) + texture + .update( + None, + unsafe { std::mem::transmute::<&[u32], &[u8]>(buffer) }, + (SCREEN_WIDTH as usize) * 4, + ) .unwrap(); self.canvas .copy( @@ -72,6 +65,5 @@ pub fn create_video_interface(sdl: &Sdl) -> Sdl2Video { Sdl2Video { tc: tc, canvas: canvas, - fps_counter: Default::default(), } }