Use RenderCopyEx instead of BlitSurface

This commit is contained in:
Yash Karandikar 2022-03-09 11:19:01 -06:00
parent e2e0d4286b
commit 9d0ce1aafb
Signed by: karx
GPG key ID: A794DA2529474BA5
2 changed files with 20 additions and 15 deletions

View file

@ -1,11 +1,13 @@
use std::{cell::Cell, path::Path};
use sdl2::{
render::Canvas,
image::ImageRWops,
rect::Rect,
render::{Canvas, TextureValueError},
rwops::RWops,
surface::Surface,
video::{Window, WindowBuildError},
EventPump, IntegerOrSdlError, image::ImageRWops, rect::Rect,
EventPump, IntegerOrSdlError,
};
pub use sdl2::event::Event;
@ -45,6 +47,12 @@ impl From<IntegerOrSdlError> for CatboxError {
}
}
impl From<TextureValueError> for CatboxError {
fn from(e: TextureValueError) -> Self {
CatboxError(format!("{}", e))
}
}
pub type Result<T> = std::result::Result<T, CatboxError>;
pub struct Events {
@ -90,15 +98,13 @@ impl Sprite {
})
}
pub fn draw(&self, canvas: &mut Canvas<Window>, events: &Events) -> Result<()> {
pub fn draw(&mut self, canvas: &mut Canvas<Window>) -> Result<()> {
let creator = canvas.texture_creator();
let text = creator.create_texture_from_surface(&self.surf)?;
canvas.fill_rect(None)?;
canvas.clear();
let mut surface = canvas.window().surface(events.as_ref())?;
self.surf.blit(None, &mut *surface, self.rect)?;
surface.finish()?;
canvas.copy_ex(&text, None, self.rect, 0.0, None, false, false)?;
Ok(())
}
@ -148,9 +154,8 @@ impl Game {
if self.stopped.get() {
break;
}
canvas.set_draw_color(Color::RGB(0, 0, 0));
canvas.clear();
func(&mut canvas, &mut events);
canvas.present();
}
Ok(())

View file

@ -5,8 +5,6 @@ fn main() {
let mut s = Sprite::new("duck.png", 500, 400).unwrap();
game.run(|canvas, event_pump| {
s.draw(canvas, event_pump).unwrap();
for event in event_pump {
match event {
Event::Quit { .. }
@ -19,8 +17,8 @@ fn main() {
let offset = match keycode.unwrap() {
Keycode::W | Keycode::Up => (0, 5),
Keycode::S | Keycode::Down => (0, -5),
Keycode::A | Keycode::Left => (-5, 0),
Keycode::D | Keycode::Right => (5, 0),
Keycode::A | Keycode::Left => (5, 0),
Keycode::D | Keycode::Right => (-5, 0),
_ => (0, 0),
};
@ -29,6 +27,8 @@ fn main() {
_ => {}
}
}
s.draw(canvas).unwrap();
})
.unwrap();
}