make BitmapAtlas generic for its type of bitmap

This commit is contained in:
Gered 2023-03-08 17:23:49 -05:00
parent bcb21bc2ba
commit 3e8ec9f73e
9 changed files with 51 additions and 31 deletions

View file

@ -3,6 +3,7 @@ use std::path::Path;
use std::rc::Rc; use std::rc::Rc;
use ggdt::entities::*; use ggdt::entities::*;
use ggdt::graphics::*;
use ggdt::graphics::indexed::*; use ggdt::graphics::indexed::*;
use ggdt::math::*; use ggdt::math::*;
use ggdt::utils::rnd_value; use ggdt::utils::rnd_value;
@ -221,7 +222,7 @@ pub struct LifeTime(pub f32);
pub struct Pixel(pub u8); pub struct Pixel(pub u8);
pub struct Sprite { pub struct Sprite {
pub atlas: Rc<BitmapAtlas>, pub atlas: Rc<BitmapAtlas<Bitmap>>,
pub index: usize, pub index: usize,
} }
@ -317,7 +318,7 @@ pub struct SpriteIndexByDirection {
} }
pub struct Weapon { pub struct Weapon {
pub atlas: Rc<BitmapAtlas>, pub atlas: Rc<BitmapAtlas<Bitmap>>,
pub base_index: usize, pub base_index: usize,
pub offsets: [Vector2; 4], pub offsets: [Vector2; 4],
pub damage: i32, pub damage: i32,

View file

@ -9,6 +9,7 @@ use anyhow::{Context, Result};
use ggdt::base::*; use ggdt::base::*;
use ggdt::entities::*; use ggdt::entities::*;
use ggdt::events::*; use ggdt::events::*;
use ggdt::graphics::*;
use ggdt::graphics::indexed::*; use ggdt::graphics::indexed::*;
use ggdt::math::*; use ggdt::math::*;
use ggdt::system::*; use ggdt::system::*;
@ -34,17 +35,17 @@ pub struct Core {
pub event_publisher: EventPublisher<Event>, pub event_publisher: EventPublisher<Event>,
pub palette: Palette, pub palette: Palette,
pub fade_out_palette: Palette, pub fade_out_palette: Palette,
pub tiles: Rc<BitmapAtlas>, pub tiles: Rc<BitmapAtlas<Bitmap>>,
pub hero_male: Rc<BitmapAtlas>, pub hero_male: Rc<BitmapAtlas<Bitmap>>,
pub hero_female: Rc<BitmapAtlas>, pub hero_female: Rc<BitmapAtlas<Bitmap>>,
pub green_slime: Rc<BitmapAtlas>, pub green_slime: Rc<BitmapAtlas<Bitmap>>,
pub blue_slime: Rc<BitmapAtlas>, pub blue_slime: Rc<BitmapAtlas<Bitmap>>,
pub orange_slime: Rc<BitmapAtlas>, pub orange_slime: Rc<BitmapAtlas<Bitmap>>,
pub fist: Rc<BitmapAtlas>, pub fist: Rc<BitmapAtlas<Bitmap>>,
pub sword: Rc<BitmapAtlas>, pub sword: Rc<BitmapAtlas<Bitmap>>,
pub particles: Rc<BitmapAtlas>, pub particles: Rc<BitmapAtlas<Bitmap>>,
pub items: Rc<BitmapAtlas>, pub items: Rc<BitmapAtlas<Bitmap>>,
pub ui: Rc<BitmapAtlas>, pub ui: Rc<BitmapAtlas<Bitmap>>,
pub tilemap: TileMap, pub tilemap: TileMap,
pub slime_activity_states: Rc<HashMap<EntityActivity, Rc<AnimationDef>>>, pub slime_activity_states: Rc<HashMap<EntityActivity, Rc<AnimationDef>>>,
pub hero_activity_states: Rc<HashMap<EntityActivity, Rc<AnimationDef>>>, pub hero_activity_states: Rc<HashMap<EntityActivity, Rc<AnimationDef>>>,

View file

@ -2,6 +2,7 @@ use std::path::Path;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use ggdt::graphics::*;
use ggdt::graphics::indexed::*; use ggdt::graphics::indexed::*;
use ggdt::states::*; use ggdt::states::*;
@ -15,20 +16,20 @@ pub fn load_font(path: &Path) -> Result<BitmaskFont> {
BitmaskFont::load_from_file(path).context(format!("Loading font: {:?}", path)) BitmaskFont::load_from_file(path).context(format!("Loading font: {:?}", path))
} }
pub fn load_bitmap_atlas_autogrid(path: &Path) -> Result<BitmapAtlas> { pub fn load_bitmap_atlas_autogrid(path: &Path) -> Result<BitmapAtlas<Bitmap>> {
let (bmp, _) = Bitmap::load_file(path).context(format!("Loading bitmap atlas: {:?}", path))?; let (bmp, _) = Bitmap::load_file(path).context(format!("Loading bitmap atlas: {:?}", path))?;
let mut atlas = BitmapAtlas::new(bmp); let mut atlas = BitmapAtlas::new(bmp);
atlas.add_grid(TILE_WIDTH, TILE_HEIGHT)?; atlas.add_grid(TILE_WIDTH, TILE_HEIGHT)?;
Ok(atlas) Ok(atlas)
} }
pub fn load_bitmap_atlas(path: &Path) -> Result<BitmapAtlas> { pub fn load_bitmap_atlas(path: &Path) -> Result<BitmapAtlas<Bitmap>> {
let (bmp, _) = Bitmap::load_file(path).context(format!("Loading bitmap atlas: {:?}", path))?; let (bmp, _) = Bitmap::load_file(path).context(format!("Loading bitmap atlas: {:?}", path))?;
let atlas = BitmapAtlas::new(bmp); let atlas = BitmapAtlas::new(bmp);
Ok(atlas) 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<Bitmap>, left: i32, top: i32, right: i32, bottom: i32) {
dest.filled_rect(left + 8, top + 8, right - 8, bottom - 8, 1); dest.filled_rect(left + 8, top + 8, right - 8, bottom - 8, 1);
// corners // corners

View file

@ -5,6 +5,7 @@ use std::path::Path;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use serde::Deserialize; use serde::Deserialize;
use ggdt::graphics::*;
use ggdt::graphics::indexed::*; use ggdt::graphics::indexed::*;
use ggdt::math::*; use ggdt::math::*;
use ggdt::utils::rnd_value; use ggdt::utils::rnd_value;
@ -63,7 +64,7 @@ impl TileMap {
&self.layers[2] &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<Bitmap>, camera_x: i32, camera_y: i32) {
let xt = camera_x / TILE_WIDTH as i32; let xt = camera_x / TILE_WIDTH as i32;
let yt = camera_y / TILE_HEIGHT as i32; let yt = camera_y / TILE_HEIGHT as i32;
let xp = camera_x % TILE_WIDTH as i32; let xp = camera_x % TILE_WIDTH as i32;

View file

@ -99,6 +99,11 @@ impl BasicImage for indexed::Bitmap {
self.height() self.height()
} }
#[inline]
fn full_bounds(&self) -> Rect {
self.full_bounds()
}
#[inline] #[inline]
fn clear(&mut self, color: u8) { fn clear(&mut self, color: u8) {
self.clear(color); self.clear(color);

View file

@ -2,7 +2,7 @@ use std::ops::Index;
use thiserror::Error; use thiserror::Error;
use crate::graphics::indexed::*; use crate::graphics::*;
use crate::math::*; use crate::math::*;
#[derive(Error, Debug)] #[derive(Error, Debug)]
@ -12,14 +12,20 @@ pub enum BitmapAtlasError {
} }
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub struct BitmapAtlas { pub struct BitmapAtlas<BitmapType>
bitmap: Bitmap, where
BitmapType: BasicImage
{
bitmap: BitmapType,
bounds: Rect, bounds: Rect,
tiles: Vec<Rect>, tiles: Vec<Rect>,
} }
impl BitmapAtlas { impl<BitmapType> BitmapAtlas<BitmapType>
pub fn new(bitmap: Bitmap) -> BitmapAtlas { where
BitmapType: BasicImage
{
pub fn new(bitmap: BitmapType) -> Self {
let bounds = bitmap.full_bounds(); let bounds = bitmap.full_bounds();
BitmapAtlas { BitmapAtlas {
bitmap, bitmap,
@ -108,12 +114,14 @@ impl BitmapAtlas {
} }
#[inline] #[inline]
pub fn bitmap(&self) -> &Bitmap { pub fn bitmap(&self) -> &BitmapType {
&self.bitmap &self.bitmap
} }
} }
impl Index<usize> for BitmapAtlas { impl<BitmapType> Index<usize> for BitmapAtlas<BitmapType>
where
BitmapType: BasicImage {
type Output = Rect; type Output = Rect;
#[inline] #[inline]
@ -128,9 +136,11 @@ pub mod tests {
use super::*; use super::*;
use crate::graphics::indexed;
#[test] #[test]
pub fn adding_rects() { 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 mut atlas = BitmapAtlas::new(bmp);
let rect = Rect::new(0, 0, 16, 16); let rect = Rect::new(0, 0, 16, 16);
@ -164,7 +174,7 @@ pub mod tests {
#[test] #[test]
pub fn adding_grid() { 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); let mut atlas = BitmapAtlas::new(bmp);
assert_eq!(3, atlas.add_custom_grid(0, 0, 8, 8, 2, 2, 0).unwrap()); assert_eq!(3, atlas.add_custom_grid(0, 0, 8, 8, 2, 2, 0).unwrap());

View file

@ -1,4 +1,5 @@
use std::rc::Rc; use std::rc::Rc;
use crate::graphics::BitmapAtlas;
use crate::graphics::indexed::*; use crate::graphics::indexed::*;
use crate::math::*; use crate::math::*;
@ -947,7 +948,7 @@ impl Bitmap {
} }
#[inline] #[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<Self>, index: usize, x: i32, y: i32) {
if let Some(src_region) = src.get(index) { if let Some(src_region) = src.get(index) {
self.blit_region(method, src.bitmap(), src_region, x, y); self.blit_region(method, src.bitmap(), src_region, x, y);
} }
@ -960,7 +961,7 @@ impl Bitmap {
} }
#[inline] #[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<Self>, index: usize, x: i32, y: i32) {
if let Some(src_region) = src.get(index) { if let Some(src_region) = src.get(index) {
self.blit_region_unchecked(method, src.bitmap(), &src_region, x, y); self.blit_region_unchecked(method, src.bitmap(), &src_region, x, y);
} }

View file

@ -1,11 +1,9 @@
pub use self::bitmap::*; pub use self::bitmap::*;
pub use self::bitmapatlas::*;
pub use self::blendmap::*; pub use self::blendmap::*;
pub use self::font::*; pub use self::font::*;
pub use self::palette::*; pub use self::palette::*;
pub mod bitmap; pub mod bitmap;
pub mod bitmapatlas;
pub mod blendmap; pub mod blendmap;
pub mod font; pub mod font;
pub mod palette; pub mod palette;

View file

@ -1,4 +1,6 @@
pub use self::basicimage::*; pub use self::basicimage::*;
pub use self::bitmapatlas::*;
pub mod indexed;
pub mod basicimage; pub mod basicimage;
pub mod bitmapatlas;
pub mod indexed;