diff --git a/examples/slimed/src/entities/mod.rs b/examples/slimed/src/entities/mod.rs index 5840266..d4e112d 100644 --- a/examples/slimed/src/entities/mod.rs +++ b/examples/slimed/src/entities/mod.rs @@ -3,6 +3,7 @@ use std::path::Path; use std::rc::Rc; use ggdt::entities::*; +use ggdt::graphics::*; use ggdt::graphics::indexed::*; use ggdt::math::*; use ggdt::utils::rnd_value; @@ -221,7 +222,7 @@ pub struct LifeTime(pub f32); pub struct Pixel(pub u8); pub struct Sprite { - pub atlas: Rc, + pub atlas: Rc>, pub index: usize, } @@ -317,7 +318,7 @@ pub struct SpriteIndexByDirection { } pub struct Weapon { - pub atlas: Rc, + pub atlas: Rc>, pub base_index: usize, pub offsets: [Vector2; 4], pub damage: i32, diff --git a/examples/slimed/src/main.rs b/examples/slimed/src/main.rs index 2b921d4..6110210 100644 --- a/examples/slimed/src/main.rs +++ b/examples/slimed/src/main.rs @@ -9,6 +9,7 @@ use anyhow::{Context, Result}; use ggdt::base::*; use ggdt::entities::*; use ggdt::events::*; +use ggdt::graphics::*; use ggdt::graphics::indexed::*; use ggdt::math::*; use ggdt::system::*; @@ -34,17 +35,17 @@ pub struct Core { pub event_publisher: EventPublisher, pub palette: Palette, pub fade_out_palette: Palette, - pub tiles: Rc, - pub hero_male: Rc, - pub hero_female: Rc, - pub green_slime: Rc, - pub blue_slime: Rc, - pub orange_slime: Rc, - pub fist: Rc, - pub sword: Rc, - pub particles: Rc, - pub items: Rc, - pub ui: Rc, + pub tiles: Rc>, + pub hero_male: Rc>, + pub hero_female: Rc>, + pub green_slime: Rc>, + pub blue_slime: Rc>, + pub orange_slime: Rc>, + pub fist: Rc>, + pub sword: Rc>, + pub particles: Rc>, + pub items: Rc>, + pub ui: Rc>, pub tilemap: TileMap, pub slime_activity_states: Rc>>, pub hero_activity_states: Rc>>, diff --git a/examples/slimed/src/support.rs b/examples/slimed/src/support.rs index 3a10936..4846027 100644 --- a/examples/slimed/src/support.rs +++ b/examples/slimed/src/support.rs @@ -2,6 +2,7 @@ use std::path::Path; use anyhow::{Context, Result}; +use ggdt::graphics::*; use ggdt::graphics::indexed::*; use ggdt::states::*; @@ -15,20 +16,20 @@ pub fn load_font(path: &Path) -> Result { BitmaskFont::load_from_file(path).context(format!("Loading font: {:?}", path)) } -pub fn load_bitmap_atlas_autogrid(path: &Path) -> Result { +pub fn load_bitmap_atlas_autogrid(path: &Path) -> Result> { let (bmp, _) = Bitmap::load_file(path).context(format!("Loading bitmap atlas: {:?}", path))?; let mut atlas = BitmapAtlas::new(bmp); atlas.add_grid(TILE_WIDTH, TILE_HEIGHT)?; Ok(atlas) } -pub fn load_bitmap_atlas(path: &Path) -> Result { +pub fn load_bitmap_atlas(path: &Path) -> Result> { let (bmp, _) = Bitmap::load_file(path).context(format!("Loading bitmap atlas: {:?}", path))?; let atlas = BitmapAtlas::new(bmp); Ok(atlas) } -pub fn draw_window(dest: &mut Bitmap, ui: &BitmapAtlas, left: i32, top: i32, right: i32, bottom: i32) { +pub fn draw_window(dest: &mut Bitmap, ui: &BitmapAtlas, left: i32, top: i32, right: i32, bottom: i32) { dest.filled_rect(left + 8, top + 8, right - 8, bottom - 8, 1); // corners diff --git a/examples/slimed/src/tilemap.rs b/examples/slimed/src/tilemap.rs index 42d6062..3f316e5 100644 --- a/examples/slimed/src/tilemap.rs +++ b/examples/slimed/src/tilemap.rs @@ -5,6 +5,7 @@ use std::path::Path; use anyhow::{Context, Result}; use serde::Deserialize; +use ggdt::graphics::*; use ggdt::graphics::indexed::*; use ggdt::math::*; use ggdt::utils::rnd_value; @@ -63,7 +64,7 @@ impl TileMap { &self.layers[2] } - pub fn draw(&self, dest: &mut Bitmap, tiles: &BitmapAtlas, camera_x: i32, camera_y: i32) { + pub fn draw(&self, dest: &mut Bitmap, tiles: &BitmapAtlas, camera_x: i32, camera_y: i32) { let xt = camera_x / TILE_WIDTH as i32; let yt = camera_y / TILE_HEIGHT as i32; let xp = camera_x % TILE_WIDTH as i32; diff --git a/ggdt/src/graphics/basicimage.rs b/ggdt/src/graphics/basicimage.rs index 4c2aa03..010847b 100644 --- a/ggdt/src/graphics/basicimage.rs +++ b/ggdt/src/graphics/basicimage.rs @@ -99,6 +99,11 @@ impl BasicImage for indexed::Bitmap { self.height() } + #[inline] + fn full_bounds(&self) -> Rect { + self.full_bounds() + } + #[inline] fn clear(&mut self, color: u8) { self.clear(color); diff --git a/ggdt/src/graphics/indexed/bitmapatlas.rs b/ggdt/src/graphics/bitmapatlas.rs similarity index 89% rename from ggdt/src/graphics/indexed/bitmapatlas.rs rename to ggdt/src/graphics/bitmapatlas.rs index bcd4950..754d497 100644 --- a/ggdt/src/graphics/indexed/bitmapatlas.rs +++ b/ggdt/src/graphics/bitmapatlas.rs @@ -2,7 +2,7 @@ use std::ops::Index; use thiserror::Error; -use crate::graphics::indexed::*; +use crate::graphics::*; use crate::math::*; #[derive(Error, Debug)] @@ -12,14 +12,20 @@ pub enum BitmapAtlasError { } #[derive(Debug, Clone, Eq, PartialEq)] -pub struct BitmapAtlas { - bitmap: Bitmap, +pub struct BitmapAtlas +where + BitmapType: BasicImage +{ + bitmap: BitmapType, bounds: Rect, tiles: Vec, } -impl BitmapAtlas { - pub fn new(bitmap: Bitmap) -> BitmapAtlas { +impl BitmapAtlas +where + BitmapType: BasicImage +{ + pub fn new(bitmap: BitmapType) -> Self { let bounds = bitmap.full_bounds(); BitmapAtlas { bitmap, @@ -108,12 +114,14 @@ impl BitmapAtlas { } #[inline] - pub fn bitmap(&self) -> &Bitmap { + pub fn bitmap(&self) -> &BitmapType { &self.bitmap } } -impl Index for BitmapAtlas { +impl Index for BitmapAtlas +where + BitmapType: BasicImage { type Output = Rect; #[inline] @@ -128,9 +136,11 @@ pub mod tests { use super::*; + use crate::graphics::indexed; + #[test] pub fn adding_rects() { - let bmp = Bitmap::new(64, 64).unwrap(); + let bmp = indexed::Bitmap::new(64, 64).unwrap(); let mut atlas = BitmapAtlas::new(bmp); let rect = Rect::new(0, 0, 16, 16); @@ -164,7 +174,7 @@ pub mod tests { #[test] pub fn adding_grid() { - let bmp = Bitmap::new(64, 64).unwrap(); + let bmp = indexed::Bitmap::new(64, 64).unwrap(); let mut atlas = BitmapAtlas::new(bmp); assert_eq!(3, atlas.add_custom_grid(0, 0, 8, 8, 2, 2, 0).unwrap()); diff --git a/ggdt/src/graphics/indexed/bitmap/blit.rs b/ggdt/src/graphics/indexed/bitmap/blit.rs index 4258a96..8e65857 100644 --- a/ggdt/src/graphics/indexed/bitmap/blit.rs +++ b/ggdt/src/graphics/indexed/bitmap/blit.rs @@ -1,4 +1,5 @@ use std::rc::Rc; +use crate::graphics::BitmapAtlas; use crate::graphics::indexed::*; use crate::math::*; @@ -947,7 +948,7 @@ impl Bitmap { } #[inline] - pub fn blit_atlas(&mut self, method: BlitMethod, src: &BitmapAtlas, index: usize, x: i32, y: i32) { + pub fn blit_atlas(&mut self, method: BlitMethod, src: &BitmapAtlas, index: usize, x: i32, y: i32) { if let Some(src_region) = src.get(index) { self.blit_region(method, src.bitmap(), src_region, x, y); } @@ -960,7 +961,7 @@ impl Bitmap { } #[inline] - pub unsafe fn blit_atlas_unchecked(&mut self, method: BlitMethod, src: &BitmapAtlas, index: usize, x: i32, y: i32) { + pub unsafe fn blit_atlas_unchecked(&mut self, method: BlitMethod, src: &BitmapAtlas, index: usize, x: i32, y: i32) { if let Some(src_region) = src.get(index) { self.blit_region_unchecked(method, src.bitmap(), &src_region, x, y); } diff --git a/ggdt/src/graphics/indexed/mod.rs b/ggdt/src/graphics/indexed/mod.rs index ce64155..248791f 100644 --- a/ggdt/src/graphics/indexed/mod.rs +++ b/ggdt/src/graphics/indexed/mod.rs @@ -1,11 +1,9 @@ pub use self::bitmap::*; -pub use self::bitmapatlas::*; pub use self::blendmap::*; pub use self::font::*; pub use self::palette::*; pub mod bitmap; -pub mod bitmapatlas; pub mod blendmap; pub mod font; pub mod palette; \ No newline at end of file diff --git a/ggdt/src/graphics/mod.rs b/ggdt/src/graphics/mod.rs index 6ccc6bc..b166bd8 100644 --- a/ggdt/src/graphics/mod.rs +++ b/ggdt/src/graphics/mod.rs @@ -1,4 +1,6 @@ pub use self::basicimage::*; +pub use self::bitmapatlas::*; -pub mod indexed; pub mod basicimage; +pub mod bitmapatlas; +pub mod indexed;