diff options
| author | Freya Murphy <freya@freyacat.org> | 2025-11-07 18:15:23 -0500 |
|---|---|---|
| committer | Freya Murphy <freya@freyacat.org> | 2025-11-07 18:15:32 -0500 |
| commit | 2f10c9b84500b62fbca7606254c4a0bdd2fde247 (patch) | |
| tree | 04b1e1b877c902e6a0413efe86083d2bed808cbf /graphics | |
| parent | Basic enemy pathfinding/ai added (diff) | |
| download | DungeonCrawl-2f10c9b84500b62fbca7606254c4a0bdd2fde247.tar.gz DungeonCrawl-2f10c9b84500b62fbca7606254c4a0bdd2fde247.tar.bz2 DungeonCrawl-2f10c9b84500b62fbca7606254c4a0bdd2fde247.zip | |
graphics: add direction based entity rendering
Diffstat (limited to 'graphics')
| -rw-r--r-- | graphics/src/render.rs | 77 |
1 files changed, 54 insertions, 23 deletions
diff --git a/graphics/src/render.rs b/graphics/src/render.rs index aac41b7..20ef41e 100644 --- a/graphics/src/render.rs +++ b/graphics/src/render.rs @@ -41,10 +41,10 @@ macro_rules! vec2 { } /// The baseline size of all ingame sprites and tile textures -const BASE_TILE_SIZE: u16 = 32; +const BASE_TEXTURE_SIZE: u16 = 16; /// The height of the wall (offset between tile layers) -const WALL_HEIGHT: u16 = 13; +const WALL_HEIGHT: u16 = 7; /// The (prefered) view distance of the game const VIEW_DISTANCE: u16 = 5; @@ -56,9 +56,6 @@ const ATLAS_FLOOR_EMPTY: (u16, u16) = (2, 0); const ATLAS_WALL_TOP: (u16, u16) = (3, 0); const ATLAS_WALL_EDGE: (u16, u16) = (0, 1); -// Entity atlas.bmp textures -const ATLAS_PLAYER: (u16, u16) = (0, 2); - // UI atlas.bmp textures const ATLAS_INV_CONTAINER: (u16, u16) = (1, 1); const ATLAS_HEART_ICON: (u16, u16) = (2, 1); @@ -71,14 +68,16 @@ const ATLAS_ERROR: (u16, u16) = (3, 3); const FULL_SOURCE_REC: Rectangle = rect! { 0.0, 0.0, - BASE_TILE_SIZE, - BASE_TILE_SIZE, + BASE_TEXTURE_SIZE, + BASE_TEXTURE_SIZE, }; #[derive(Debug)] struct Textures { // Tilemap atlas: Texture2D, + // Entity + player: Texture2D, // Misc error: Texture2D, // Fonts @@ -87,11 +86,13 @@ struct Textures { impl Textures { fn new(handle: &mut RaylibHandle, thread: &RaylibThread) -> crate::Result<Self> { let atlas = handle.load_texture(thread, "assets/atlas.bmp")?; + let player = handle.load_texture(thread, "assets/player.bmp")?; let error = handle.load_texture(thread, "assets/error.bmp")?; let pixantiqua = handle.load_font(thread, "assets/pixantiqua.fnt")?; Ok(Self { atlas, + player, error, pixantiqua, }) @@ -131,7 +132,7 @@ impl Renderer { // Load resources let textures = Textures::new(handle, thread)?; // Load render textures - let tex_size = (MAP_SIZE * BASE_TILE_SIZE) as u32; + let tex_size = (MAP_SIZE * BASE_TEXTURE_SIZE) as u32; let tilemap_fg = handle.load_render_texture(thread, tex_size, tex_size)?; let tilemap_bg = handle.load_render_texture(thread, tex_size, tex_size)?; let tilemap_mm = @@ -176,7 +177,7 @@ impl Renderer { self.tile_size = { let size = self.width.min(self.height); let dist = VIEW_DISTANCE * 2 + 1; - let pixels = size.div(dist).max(BASE_TILE_SIZE); + let pixels = size.div(dist).max(BASE_TEXTURE_SIZE); 1 << (u16::BITS - pixels.leading_zeros()) }; } @@ -238,7 +239,7 @@ impl Renderer { where R: RaylibDraw + RaylibTextureModeExt, { - let size = BASE_TILE_SIZE; + let size = BASE_TEXTURE_SIZE; let mut rt = r.begin_texture_mode(t, &mut self.tilemap_fg); rt.clear_background(Color::BLANK); @@ -276,7 +277,7 @@ impl Renderer { where R: RaylibDraw + RaylibTextureModeExt, { - let size = BASE_TILE_SIZE; + let size = BASE_TEXTURE_SIZE; let mut rt = r.begin_texture_mode(t, &mut self.tilemap_bg); rt.clear_background(Color::BLACK); @@ -322,7 +323,7 @@ impl Renderer { &self.tilemap_fg, 0, 0, - self.tile_size / BASE_TILE_SIZE, + self.tile_size / BASE_TEXTURE_SIZE, WALL_HEIGHT, ); } @@ -332,7 +333,13 @@ impl Renderer { where R: RaylibDraw, { - r.draw_tilemap(&self.tilemap_bg, 0, 0, self.tile_size / BASE_TILE_SIZE, 0); + r.draw_tilemap( + &self.tilemap_bg, + 0, + 0, + self.tile_size / BASE_TEXTURE_SIZE, + 0, + ); } /// Draws the entities on the map @@ -352,13 +359,37 @@ impl Renderer { R: RaylibDraw, { let size = self.tile_size as f32; - let x = entity.fpos.x(); - let y = entity.fpos.y(); - let tex = match entity.kind { - EntityKind::Player => ATLAS_PLAYER, - _ => ATLAS_ERROR, + let (fx, fy) = entity.fpos.xy(); + let texture = match entity.kind { + EntityKind::Player => &self.textures.player, + _ => &self.textures.error, + }; + let (x, y) = match entity.dir { + Direction::North => (0, 0), + Direction::South => (0, 1), + Direction::East => (1, 0), + Direction::West => (1, 1), + }; + let source_rec = rect! { + x * BASE_TEXTURE_SIZE, + y * BASE_TEXTURE_SIZE, + BASE_TEXTURE_SIZE, + BASE_TEXTURE_SIZE, + }; + let dest_rec = rect! { + fx * size - size/2.0, + fy * size - size/2.0, + size, + size, }; - r.draw_atlas(&self.textures.atlas, tex, x * size, y * size, size, 0.0); + r.draw_texture_pro( + texture, + source_rec, + dest_rec, + Vector2::zero(), + 0.0, + Color::WHITE, + ); } /// Returns the known UI height for this frame @@ -635,10 +666,10 @@ where ) { let size_into = size.into(); let source_rec = rect! { - ax * BASE_TILE_SIZE, - ay * BASE_TILE_SIZE, - BASE_TILE_SIZE, - BASE_TILE_SIZE, + ax * BASE_TEXTURE_SIZE, + ay * BASE_TEXTURE_SIZE, + BASE_TEXTURE_SIZE, + BASE_TEXTURE_SIZE, }; let dest_rec = rect! { x.into(), |