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 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<BitmapAtlas>,
pub atlas: Rc<BitmapAtlas<Bitmap>>,
pub index: usize,
}
@ -317,7 +318,7 @@ pub struct SpriteIndexByDirection {
}
pub struct Weapon {
pub atlas: Rc<BitmapAtlas>,
pub atlas: Rc<BitmapAtlas<Bitmap>>,
pub base_index: usize,
pub offsets: [Vector2; 4],
pub damage: i32,

View file

@ -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<Event>,
pub palette: Palette,
pub fade_out_palette: Palette,
pub tiles: Rc<BitmapAtlas>,
pub hero_male: Rc<BitmapAtlas>,
pub hero_female: Rc<BitmapAtlas>,
pub green_slime: Rc<BitmapAtlas>,
pub blue_slime: Rc<BitmapAtlas>,
pub orange_slime: Rc<BitmapAtlas>,
pub fist: Rc<BitmapAtlas>,
pub sword: Rc<BitmapAtlas>,
pub particles: Rc<BitmapAtlas>,
pub items: Rc<BitmapAtlas>,
pub ui: Rc<BitmapAtlas>,
pub tiles: Rc<BitmapAtlas<Bitmap>>,
pub hero_male: Rc<BitmapAtlas<Bitmap>>,
pub hero_female: Rc<BitmapAtlas<Bitmap>>,
pub green_slime: Rc<BitmapAtlas<Bitmap>>,
pub blue_slime: Rc<BitmapAtlas<Bitmap>>,
pub orange_slime: Rc<BitmapAtlas<Bitmap>>,
pub fist: Rc<BitmapAtlas<Bitmap>>,
pub sword: Rc<BitmapAtlas<Bitmap>>,
pub particles: Rc<BitmapAtlas<Bitmap>>,
pub items: Rc<BitmapAtlas<Bitmap>>,
pub ui: Rc<BitmapAtlas<Bitmap>>,
pub tilemap: TileMap,
pub slime_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 ggdt::graphics::*;
use ggdt::graphics::indexed::*;
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))
}
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 mut atlas = BitmapAtlas::new(bmp);
atlas.add_grid(TILE_WIDTH, TILE_HEIGHT)?;
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 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<Bitmap>, left: i32, top: i32, right: i32, bottom: i32) {
dest.filled_rect(left + 8, top + 8, right - 8, bottom - 8, 1);
// corners

View file

@ -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<Bitmap>, 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;

View file

@ -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);

View file

@ -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<BitmapType>
where
BitmapType: BasicImage
{
bitmap: BitmapType,
bounds: Rect,
tiles: Vec<Rect>,
}
impl BitmapAtlas {
pub fn new(bitmap: Bitmap) -> BitmapAtlas {
impl<BitmapType> BitmapAtlas<BitmapType>
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<usize> for BitmapAtlas {
impl<BitmapType> Index<usize> for BitmapAtlas<BitmapType>
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());

View file

@ -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<Self>, 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<Self>, 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);
}

View file

@ -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;

View file

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