summaryrefslogtreecommitdiff
path: root/graphics
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2025-11-14 21:48:24 -0500
committerFreya Murphy <freya@freyacat.org>2025-11-14 21:48:34 -0500
commit220d5c8aa8418d37ee9027c817d27b3e50380b81 (patch)
tree4a092dd67d9b2b824504e9cc851df5145c9606ea /graphics
parentgraphics: make more constants dependant on texture size (diff)
downloadDungeonCrawl-220d5c8aa8418d37ee9027c817d27b3e50380b81.tar.gz
DungeonCrawl-220d5c8aa8418d37ee9027c817d27b3e50380b81.tar.bz2
DungeonCrawl-220d5c8aa8418d37ee9027c817d27b3e50380b81.zip
graphics: new textures!
Diffstat (limited to 'graphics')
-rw-r--r--graphics/src/render.rs29
-rw-r--r--graphics/src/timer.rs31
2 files changed, 41 insertions, 19 deletions
diff --git a/graphics/src/render.rs b/graphics/src/render.rs
index a206e04..340189b 100644
--- a/graphics/src/render.rs
+++ b/graphics/src/render.rs
@@ -92,8 +92,8 @@ pub const RENDER_WIDTH: u16 = RENDER_HEIGHT * 4 / 3;
// Tile atlas.bmp textures
const ATLAS_WALL_SIDE: (u16, u16) = (0, 0);
-const ATLAS_FLOOR_FULL: (u16, u16) = (1, 0);
-const ATLAS_FLOOR_EMPTY: (u16, u16) = (2, 0);
+const ATLAS_FLOOR: (u16, u16) = (1, 0);
+const ATLAS_STAIR: (u16, u16) = (2, 0);
const ATLAS_WALL_TOP: (u16, u16) = (3, 0);
const ATLAS_WALL_EDGE: (u16, u16) = (0, 1);
@@ -103,7 +103,7 @@ const ATLAS_HEART_ICON: (u16, u16) = (2, 1);
const ATLAS_DAMAGE_ICON: (u16, u16) = (3, 1);
// Misc atlas.bmp textures
-const ATLAS_ERROR: (u16, u16) = (3, 3);
+//const ATLAS_ERROR: (u16, u16) = (3, 3);
/// Full source rec for any texture
const FULL_SOURCE_REC: Rectangle = rect! {
@@ -376,11 +376,9 @@ impl Renderer {
let idx = match tile {
Tile::Wall if y + 1 == MAP_SIZE => ATLAS_WALL_TOP,
Tile::Wall => ATLAS_WALL_SIDE,
- Tile::Room if (x + y) % 2 == 0 => ATLAS_FLOOR_FULL,
- Tile::Room if (x + y) % 2 == 1 => ATLAS_FLOOR_EMPTY,
- Tile::Hallway if (x + y) % 2 == 0 => ATLAS_FLOOR_FULL,
- Tile::Hallway if (x + y) % 2 == 1 => ATLAS_FLOOR_EMPTY,
- _ => ATLAS_ERROR,
+ Tile::Room | Tile::Hallway => ATLAS_FLOOR,
+ Tile::Stairs => ATLAS_STAIR,
+ //_ => ATLAS_ERROR,
};
rt.draw_atlas(
&self.textures.atlas,
@@ -458,19 +456,30 @@ impl Renderer {
EntityKind::Player => &self.textures.player,
_ => &self.textures.error,
};
- let (ax, ay) = match entity.dir {
+ let (mut ax, ay) = match entity.dir {
Direction::North => (0, 0),
Direction::South => (0, 1),
Direction::East => (1, 0),
Direction::West => (1, 1),
};
+ if self.timer.since_start().as_millis() / 250 % 2 == 0 {
+ // entity animtion frame every 250 millis
+ ax += 2;
+ }
let dest_rec = rect! {
fx * size,
fy * size,
size,
size,
};
- r.draw_atlas(texture, (ax, ay), dest_rec.x, dest_rec.y, size, 0.0);
+ r.draw_atlas(
+ texture,
+ (ax, ay),
+ dest_rec.x,
+ dest_rec.y - WALL_HEIGHT as f32,
+ size,
+ 0.0,
+ );
if self.debug {
r.draw_rectangle_lines_ex(dest_rec, 1.0, Color::YELLOW);
diff --git a/graphics/src/timer.rs b/graphics/src/timer.rs
index e2209c7..9d4fcfc 100644
--- a/graphics/src/timer.rs
+++ b/graphics/src/timer.rs
@@ -5,6 +5,8 @@ const MAX_SAMPLES: usize = 100;
/// Keeps track of frame intervals and FPS
pub struct Timer {
+ start: Instant,
+ since_start: Duration,
previous: Instant,
tick_index: usize,
tick_sum: Duration,
@@ -13,13 +15,17 @@ pub struct Timer {
}
impl Timer {
pub fn new() -> Self {
- let previous = Instant::now();
+ let start = Instant::now();
+ let since_start = Duration::ZERO;
+ let previous = start;
let tick_index = 0usize;
let tick_sum = Duration::ZERO;
let tick_list = Box::new([tick_sum; MAX_SAMPLES]);
let frame = 0u32;
Self {
+ start,
+ since_start,
previous,
tick_index,
tick_sum,
@@ -39,8 +45,11 @@ impl Timer {
self.tick_sum += time;
self.tick_list[self.tick_index] = time;
- // update frame frame
+ // update frame count
self.frame += 1;
+
+ // update since
+ self.since_start = self.previous.duration_since(self.start);
}
pub const fn frame(&self) -> u32 {
@@ -49,13 +58,13 @@ impl Timer {
#[expect(clippy::cast_possible_truncation)]
pub const fn fps(&self) -> u32 {
- match self.frame_time().as_nanos() {
- 0 => u32::MAX,
- // fps can never be bigger than u32::MAX since we're
- // computing in nanoseconds
- nanos => 1_000_000_000u128.div_ceil(nanos) as u32,
- }
- }
+ match self.frame_time().as_nanos() {
+ 0 => u32::MAX,
+ // fps can never be bigger than u32::MAX since we're
+ // computing in nanoseconds
+ nanos => 1_000_000_000u128.div_ceil(nanos) as u32,
+ }
+ }
#[expect(clippy::cast_possible_truncation)]
pub const fn frame_time(&self) -> Duration {
@@ -68,4 +77,8 @@ impl Timer {
pub const fn delta_time(&self) -> Duration {
self.tick_list[self.tick_index]
}
+
+ pub const fn since_start(&self) -> Duration {
+ self.since_start
+ }
}