Initial Sprite struct

This commit is contained in:
Yash Karandikar 2022-03-07 10:19:09 -06:00
parent 2d8e75acd3
commit adee07fbc7
Signed by untrusted user: karx
GPG key ID: A794DA2529474BA5
2 changed files with 30 additions and 5 deletions

View file

@ -5,5 +5,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
sdl2 = "0.35.2"
[dependencies.sdl2]
version = "0.35.2"
features = ["image"]

View file

@ -1,9 +1,9 @@
use std::cell::Cell;
use std::{cell::Cell, path::Path};
use sdl2::{
render::Canvas,
video::{Window, WindowBuildError},
IntegerOrSdlError,
video::{Window, WindowBuildError, WindowSurfaceRef},
IntegerOrSdlError, rect::Rect, surface::Surface, rwops::RWops, image::ImageRWops,
};
pub use sdl2::event::Event;
@ -45,6 +45,30 @@ impl From<IntegerOrSdlError> for CatboxError {
pub type Result<T> = std::result::Result<T, CatboxError>;
pub struct Sprite {
rect: Rect,
surf: Surface<'static>
}
impl Sprite {
pub fn new<P: AsRef<Path>>(path: P, x: i32, y: i32) -> Result<Self> {
let ops = RWops::from_file(path, "r")?;
let surf = ops.load()?;
let srect = surf.rect();
let dest_rect: Rect = Rect::new(x, y, srect.width(), srect.height());
Ok(Self {
rect: dest_rect,
surf
})
}
pub fn draw(&self, canvas: Canvas<Window>) {
let surface = canvas.window();
}
}
pub struct Game {
pub title: String,
pub width: u32,