From 64b484f7c9cb135112d486a3caae57551cf0a160 Mon Sep 17 00:00:00 2001 From: gallant Date: Wed, 17 May 2023 10:29:05 -0500 Subject: [PATCH] added the ability to make sprite primitives, will add onto this later --- Cargo.toml | 2 +- src/lib.rs | 6 ++---- src/sprite/sprite.rs | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cc8fd2e..b4fa4eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ zip-extract = "0.1.2" tempfile = "3.4.0" [features] -default = ["audio"] +default = ["audio", "sdl2/gfx"] static = ["sdl2/static-link", "sdl2/bundled"] audio = ["dep:rodio"] vulkan = ["dep:vulkano"] diff --git a/src/lib.rs b/src/lib.rs index 4306030..016231e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -334,8 +334,7 @@ pub fn draw_text, I: Into>( /// Representation of the mouse state. pub struct MouseRepr { pub buttons: Vec, - pub x: i32, - pub y: i32, + pub pos: Vec2Int } /// Representation of the keyboard state. @@ -358,8 +357,7 @@ pub fn get_mouse_state(ctx: &mut Context) -> MouseRepr { MouseRepr { buttons: mouse.pressed_mouse_buttons().collect(), - x: mouse.x(), - y: mouse.y(), + pos: Vec2Int::new(mouse.x(), mouse.y()) } } diff --git a/src/sprite/sprite.rs b/src/sprite/sprite.rs index 3d03146..602a922 100644 --- a/src/sprite/sprite.rs +++ b/src/sprite/sprite.rs @@ -47,6 +47,28 @@ impl Sprite { }) } + pub fn rect_sprite(x: i32, y: i32,width: u32, height: u32, color: C) -> Result { + let (rmask,gmask,bmask,amask) = color.as_rgba(); + + let mask = sdl2::pixels::PixelMasks { + bpp: 32, + rmask: rmask.into(), + gmask: gmask.into(), + bmask: bmask.into(), + amask: amask.into() + }; + + let surf = Surface::from_pixelmasks(width, height, mask).unwrap(); + surf.rect().set_y(y); + surf.rect().set_x(x); + + Ok(Self{ + rect: surf.rect(), + surf, + angle: 0.0, + }) + } + /// Create a new sprite using a slice of bytes, like what is returned from `include_bytes!` /// /// Don't forget to call [`draw()`](Self::draw()) after this.