Refactor to use custom Context struct

This commit is contained in:
Yash Karandikar 2022-03-14 12:32:42 -05:00
parent 247eb13591
commit 0902130e7d
2 changed files with 39 additions and 10 deletions

View file

@ -53,10 +53,10 @@ use std::{cell::Cell, path::Path};
use sdl2::{ use sdl2::{
image::ImageRWops, image::ImageRWops,
rect::Rect, rect::Rect,
render::{Canvas, TextureValueError}, render::{Canvas, TextureValueError, TextureCreator},
rwops::RWops, rwops::RWops,
surface::Surface, surface::Surface,
video::{Window, WindowBuildError}, video::{Window, WindowBuildError, WindowContext},
EventPump, IntegerOrSdlError, EventPump, IntegerOrSdlError,
}; };
@ -175,8 +175,8 @@ impl Sprite {
/// s.draw(canvas); /// s.draw(canvas);
/// # }); /// # });
/// ``` /// ```
pub fn draw(&mut self, canvas: &mut Canvas<Window>) -> Result<()> { pub fn draw(&mut self, ctx: &mut Context) -> Result<()> {
let creator = canvas.texture_creator(); let (creator, canvas) = ctx.inner();
let text = creator.create_texture_from_surface(&self.surf)?; let text = creator.create_texture_from_surface(&self.surf)?;
canvas.fill_rect(None)?; canvas.fill_rect(None)?;
@ -235,6 +235,33 @@ impl Sprite {
} }
} }
pub struct Context {
canvas: Canvas<Window>,
texture_creator: TextureCreator<WindowContext>,
}
impl Context {
pub fn new(canvas: Canvas<Window>) -> Self {
let creator = canvas.texture_creator();
Self {
canvas,
texture_creator: creator,
}
}
pub fn canvas(&mut self) -> &mut Canvas<Window> {
&mut self.canvas
}
pub fn inner(&mut self) -> (&TextureCreator<WindowContext>, &mut Canvas<Window>) {
(&self.texture_creator, &mut self.canvas)
}
pub fn update(&mut self) {
self.canvas.present();
}
}
/// Representation of the game. /// Representation of the game.
pub struct Game { pub struct Game {
/// The title that the window displays. /// The title that the window displays.
@ -274,7 +301,7 @@ impl Game {
/// // Game logic goes here /// // Game logic goes here
/// }); /// });
/// ``` /// ```
pub fn run<F: FnMut(&mut Canvas<Window>, &mut Events)>(&self, mut func: F) -> Result<()> { pub fn run<F: FnMut(&mut Context, &mut Events)>(&self, mut func: F) -> Result<()> {
let sdl_context = sdl2::init()?; let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?; let video_subsystem = sdl_context.video()?;
@ -285,18 +312,20 @@ impl Game {
.vulkan() .vulkan()
.build()?; .build()?;
let mut canvas = window.into_canvas().build()?; let canvas = window.into_canvas().build()?;
let event_pump = sdl_context.event_pump()?; let event_pump = sdl_context.event_pump()?;
let mut events = Events { pump: event_pump }; let mut events = Events { pump: event_pump };
let mut ctx = Context::new(canvas);
loop { loop {
if self.stopped.get() { if self.stopped.get() {
break; break;
} }
func(&mut canvas, &mut events); func(&mut ctx, &mut events);
canvas.present(); ctx.update();
} }
Ok(()) Ok(())

View file

@ -5,7 +5,7 @@ fn main() {
let mut i = 0.0; let mut i = 0.0;
let mut s = Sprite::new("duck.png", 500, 400).unwrap(); let mut s = Sprite::new("duck.png", 500, 400).unwrap();
game.run(|canvas, event_pump| { game.run(|ctx, event_pump| {
i = (i + 1.0) % 360.0; i = (i + 1.0) % 360.0;
let (start_x, start_y) = s.position(); let (start_x, start_y) = s.position();
@ -39,7 +39,7 @@ fn main() {
} }
} }
s.draw(canvas).unwrap(); s.draw(ctx).unwrap();
}) })
.unwrap(); .unwrap();
} }