summaryrefslogtreecommitdiff
path: root/graphics/src
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2025-11-07 16:52:34 -0500
committerFreya Murphy <freya@freyacat.org>2025-11-07 16:52:34 -0500
commitc852e0c5887da6e4365c894ac0930fd6a84eedf5 (patch)
treea10b702df47d7ab8122962b00c5f738d1e6e12e8 /graphics/src
parentupdate textures (diff)
downloadDungeonCrawl-c852e0c5887da6e4365c894ac0930fd6a84eedf5.tar.gz
DungeonCrawl-c852e0c5887da6e4365c894ac0930fd6a84eedf5.tar.bz2
DungeonCrawl-c852e0c5887da6e4365c894ac0930fd6a84eedf5.zip
graphics: have tilemap cached with a render texture
Diffstat (limited to 'graphics/src')
-rw-r--r--graphics/src/render.rs135
1 files changed, 56 insertions, 79 deletions
diff --git a/graphics/src/render.rs b/graphics/src/render.rs
index 35b0ae3..a3fa528 100644
--- a/graphics/src/render.rs
+++ b/graphics/src/render.rs
@@ -104,6 +104,8 @@ pub struct Renderer {
tilemap_fg: RenderTexture2D,
/// Bottom layer of the tilemap (floor and half-height walls)
tilemap_bg: RenderTexture2D,
+ /// Tilemap texture used for the minimap
+ tilemap_mm: RenderTexture2D,
/// Last computed hash of the tilemap (floor)
tiles_hash: Option<u64>,
/* Per Frame Caculated Data */
@@ -124,11 +126,14 @@ impl Renderer {
let tex_size = (MAP_SIZE * BASE_TILE_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 =
+ handle.load_render_texture(thread, MAP_SIZE as u32, MAP_SIZE as u32)?;
Ok(Self {
textures,
tilemap_fg,
tilemap_bg,
+ tilemap_mm,
tiles_hash: None,
tile_size: 0,
fps: 0,
@@ -217,6 +222,7 @@ impl Renderer {
self.update_fg_tilemap(r, t, floor);
self.update_bg_tilemap(r, t, floor);
+ self.update_mm_tilemap(r, t, floor);
}
/// Draws the foregound tile map
@@ -279,12 +285,38 @@ impl Renderer {
}
}
+ /// Draws the foregound tile map
+ fn update_mm_tilemap<R>(&mut self, r: &mut R, t: &RaylibThread, floor: &Floor)
+ where
+ R: RaylibDraw + RaylibTextureModeExt,
+ {
+ let mut rt = r.begin_texture_mode(t, &mut self.tilemap_mm);
+ rt.clear_background(Color::DARKGRAY);
+
+ for pos in Pos::values() {
+ let (x, y) = pos.xy();
+ let tile = floor.get(pos);
+ let color = match tile {
+ Tile::Wall => Color::DARKGRAY,
+ Tile::Air => Color::GRAY,
+ Tile::Stairs => Color::WHITE,
+ };
+ rt.draw_pixel(x.into(), y.into(), color);
+ }
+ }
+
/// Draw dungeon tiles (foreground layer)
fn draw_fg_tilemap<R>(&mut self, r: &mut R)
where
R: RaylibDraw,
{
- r.draw_tilemap(&self.tilemap_fg, self.tile_size, WALL_HEIGHT);
+ r.draw_tilemap(
+ &self.tilemap_fg,
+ 0,
+ 0,
+ self.tile_size / BASE_TILE_SIZE,
+ WALL_HEIGHT,
+ );
}
/// Draw dungeon tiles (background layer)
@@ -292,7 +324,7 @@ impl Renderer {
where
R: RaylibDraw,
{
- r.draw_tilemap(&self.tilemap_bg, self.tile_size, 0);
+ r.draw_tilemap(&self.tilemap_bg, 0, 0, self.tile_size / BASE_TILE_SIZE, 0);
}
/// Draws the entities on the map
@@ -368,49 +400,32 @@ impl Renderer {
// Draw minimap in top left of UI bar
let minimap_x = text_x + self.get_ui_font_size() + padding;
+ let minimap_y = padding;
let size = self.get_ui_height() - padding * 2;
- let group_size = (MAP_SIZE / size).max(1);
- let groups = MAP_SIZE / group_size;
- let dot_size = size / groups;
+ r.draw_tilemap(&self.tilemap_mm, minimap_x, minimap_y, size / MAP_SIZE, 0);
- let mut draw_dot = |x_idx: u16, y_idx: u16, color| {
+ // Draw minimap entity's
+ let dot_size = (size / MAP_SIZE).max(1);
+ let mut draw_dot = |pos: Pos, color| {
+ let (x, y) = pos.xy();
+ let offset_x = x * size / MAP_SIZE;
+ let offset_y = y * size / MAP_SIZE;
r.draw_rectangle_ext(
- minimap_x + x_idx * dot_size,
- padding + y_idx * dot_size,
+ minimap_x + offset_x,
+ minimap_y + offset_y,
dot_size,
dot_size,
color,
);
};
- // TODO: fog of war!
- for x_idx in 0..groups {
- for y_idx in 0..groups {
- let x = x_idx * group_size;
- let y = y_idx * group_size;
- let Some(pos) = Pos::new(x, y) else { continue };
-
- // Get the color of the tile
- let color = dungeon.floor.minimap_color(pos, group_size);
- draw_dot(x_idx, y_idx, color);
- }
- }
-
// Draw enemy dots
for enemy in &dungeon.enemies {
- let (x, y) = enemy.entity.pos.xy();
- let x_idx = x / group_size;
- let y_idx = y / group_size;
- draw_dot(x_idx, y_idx, Color::RED);
+ draw_dot(enemy.entity.pos, Color::RED);
}
// Draw player dot
- {
- let (x, y) = dungeon.player.entity.pos.xy();
- let x_idx = x / group_size;
- let y_idx = y / group_size;
- draw_dot(x_idx, y_idx, Color::LIME);
- }
+ draw_dot(dungeon.player.entity.pos, Color::LIME);
}
/// Draws vertical text
@@ -586,50 +601,6 @@ impl Vector2Ext for Vector2 {
}
}
-trait TileExt {
- fn minimap_score(&self) -> u8;
-}
-impl TileExt for Tile {
- fn minimap_score(&self) -> u8 {
- match self {
- // Walls
- Self::Wall => 0,
- // Empty
- Self::Air => 1,
- // Stairs
- Self::Stairs => 2,
- }
- }
-}
-
-trait FloorExt {
- fn minimap_color(&self, pos: Pos, group_size: u16) -> Color;
-}
-impl FloorExt for Floor {
- fn minimap_color(&self, pos: Pos, group_size: u16) -> Color {
- let mut score = 0;
-
- let (x, y_start) = pos.xy();
- for y in y_start..y_start + group_size {
- let idx = (x + y * MAP_SIZE) as usize;
- let tiles = &self.tiles()[idx..idx + group_size as usize];
- let new_score = tiles.iter().map(TileExt::minimap_score).max().unwrap_or(0);
- score = score.max(new_score);
- }
-
- match score {
- // Walls
- 0 => Color::DARKGRAY,
- // Empty
- 1 => Color::GRAY,
- // Stairs
- 2 => Color::WHITE,
- // UNKNOWN (ERROR)
- _ => Color::PINK,
- }
- }
-}
-
trait RaylibDrawExt
where
Self: RaylibDraw,
@@ -693,8 +664,14 @@ where
}
/// Draw dungeon tiles helper function
- fn draw_tilemap(&mut self, tex: &RenderTexture2D, size: u16, offset: u16) {
- let scale = size / BASE_TILE_SIZE;
+ fn draw_tilemap(
+ &mut self,
+ tex: &RenderTexture2D,
+ x: u16,
+ y: u16,
+ scale: u16,
+ offset: u16,
+ ) {
let width = downcast!(tex.width(), u16);
let height = downcast!(tex.height(), u16);
let source_rec = rect! {
@@ -704,8 +681,8 @@ where
-height.cast_signed(),
};
let dest_rec = rect! {
- 0,
- -(offset * scale).cast_signed(),
+ x,
+ y.cast_signed() - (offset * scale).cast_signed(),
width * scale,
height * scale,
};