• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Xevion / Pac-Man / 16896410642

12 Aug 2025 01:23AM UTC coverage: 43.298% (-2.1%) from 45.41%
16896410642

push

github

Xevion
feat!: implement proper error handling, drop most expect() & unwrap() usages

126 of 337 new or added lines in 10 files covered. (37.39%)

8 existing lines in 5 files now uncovered.

743 of 1716 relevant lines covered (43.3%)

280.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

15.38
/src/map/render.rs
1
//! Map rendering functionality.
2

3
use crate::texture::sprite::{AtlasTile, SpriteAtlas};
4
use crate::texture::text::TextTexture;
5
use glam::Vec2;
6
use sdl2::pixels::Color;
7
use sdl2::rect::{Point, Rect};
8
use sdl2::render::{Canvas, RenderTarget};
9

10
use crate::error::{EntityError, GameError, GameResult};
11

12
/// Handles rendering operations for the map.
13
pub struct MapRenderer;
14

15
impl MapRenderer {
16
    /// Renders the map to the given canvas.
17
    ///
18
    /// This function draws the static map texture to the screen at the correct
19
    /// position and scale.
20
    pub fn render_map<T: RenderTarget>(canvas: &mut Canvas<T>, atlas: &mut SpriteAtlas, map_texture: &mut AtlasTile) {
×
21
        let dest = Rect::new(
×
22
            crate::constants::BOARD_PIXEL_OFFSET.x as i32,
×
23
            crate::constants::BOARD_PIXEL_OFFSET.y as i32,
×
24
            crate::constants::BOARD_PIXEL_SIZE.x,
×
25
            crate::constants::BOARD_PIXEL_SIZE.y,
×
26
        );
×
NEW
27
        if let Err(e) = map_texture.render(canvas, atlas, dest) {
×
NEW
28
            tracing::error!("Failed to render map: {}", e);
×
NEW
29
        }
×
UNCOV
30
    }
×
31

32
    /// Renders a debug visualization with cursor-based highlighting.
33
    ///
34
    /// This function provides interactive debugging by highlighting the nearest node
35
    /// to the cursor, showing its ID, and highlighting its connections.
36
    pub fn debug_render_with_cursor<T: RenderTarget>(
×
37
        graph: &crate::entity::graph::Graph,
×
38
        canvas: &mut Canvas<T>,
×
39
        text_renderer: &mut TextTexture,
×
40
        atlas: &mut SpriteAtlas,
×
41
        cursor_pos: Vec2,
×
NEW
42
    ) -> GameResult<()> {
×
43
        // Find the nearest node to the cursor
×
44
        let nearest_node = Self::find_nearest_node(graph, cursor_pos);
×
45

×
46
        // Draw all connections in blue
×
47
        canvas.set_draw_color(Color::RGB(0, 0, 128)); // Dark blue for regular connections
×
48
        for i in 0..graph.node_count() {
×
NEW
49
            let node = graph.get_node(i).ok_or(GameError::Entity(EntityError::NodeNotFound(i)))?;
×
50
            let pos = node.position + crate::constants::BOARD_PIXEL_OFFSET.as_vec2();
×
51

52
            for edge in graph.adjacency_list[i].edges() {
×
NEW
53
                let end_pos = graph
×
NEW
54
                    .get_node(edge.target)
×
NEW
55
                    .ok_or(GameError::Entity(EntityError::NodeNotFound(edge.target)))?
×
56
                    .position
NEW
57
                    + crate::constants::BOARD_PIXEL_OFFSET.as_vec2();
×
58
                canvas
×
59
                    .draw_line((pos.x as i32, pos.y as i32), (end_pos.x as i32, end_pos.y as i32))
×
NEW
60
                    .map_err(|e| GameError::Sdl(e.to_string()))?;
×
61
            }
62
        }
63

64
        // Draw all nodes in green
65
        canvas.set_draw_color(Color::RGB(0, 128, 0)); // Dark green for regular nodes
×
66
        for i in 0..graph.node_count() {
×
NEW
67
            let node = graph.get_node(i).ok_or(GameError::Entity(EntityError::NodeNotFound(i)))?;
×
68
            let pos = node.position + crate::constants::BOARD_PIXEL_OFFSET.as_vec2();
×
69

×
70
            canvas
×
71
                .fill_rect(Rect::new(0, 0, 3, 3).centered_on(Point::new(pos.x as i32, pos.y as i32)))
×
NEW
72
                .map_err(|e| GameError::Sdl(e.to_string()))?;
×
73
        }
74

75
        // Highlight connections from the nearest node in bright blue
76
        if let Some(nearest_id) = nearest_node {
×
NEW
77
            let nearest_pos = graph
×
NEW
78
                .get_node(nearest_id)
×
NEW
79
                .ok_or(GameError::Entity(EntityError::NodeNotFound(nearest_id)))?
×
80
                .position
NEW
81
                + crate::constants::BOARD_PIXEL_OFFSET.as_vec2();
×
82

×
83
            canvas.set_draw_color(Color::RGB(0, 255, 255)); // Bright cyan for highlighted connections
×
84
            for edge in graph.adjacency_list[nearest_id].edges() {
×
NEW
85
                let end_pos = graph
×
NEW
86
                    .get_node(edge.target)
×
NEW
87
                    .ok_or(GameError::Entity(EntityError::NodeNotFound(edge.target)))?
×
88
                    .position
NEW
89
                    + crate::constants::BOARD_PIXEL_OFFSET.as_vec2();
×
90
                canvas
×
91
                    .draw_line(
×
92
                        (nearest_pos.x as i32, nearest_pos.y as i32),
×
93
                        (end_pos.x as i32, end_pos.y as i32),
×
94
                    )
×
NEW
95
                    .map_err(|e| GameError::Sdl(e.to_string()))?;
×
96
            }
97

98
            // Highlight the nearest node in bright green
99
            canvas.set_draw_color(Color::RGB(0, 255, 0)); // Bright green for highlighted node
×
100
            canvas
×
101
                .fill_rect(Rect::new(0, 0, 5, 5).centered_on(Point::new(nearest_pos.x as i32, nearest_pos.y as i32)))
×
NEW
102
                .map_err(|e| GameError::Sdl(e.to_string()))?;
×
103

104
            // Draw node ID text (small, offset to top right)
105
            text_renderer.set_scale(0.5); // Small text
×
106
            let id_text = format!("#{nearest_id}");
×
107
            let text_pos = glam::UVec2::new(
×
108
                (nearest_pos.x + 4.0) as u32, // Offset to the right
×
109
                (nearest_pos.y - 6.0) as u32, // Offset to the top
×
110
            );
×
NEW
111
            if let Err(e) = text_renderer.render(canvas, atlas, &id_text, text_pos) {
×
NEW
112
                tracing::error!("Failed to render node ID text: {}", e);
×
NEW
113
            }
×
114
        }
×
115

NEW
116
        Ok(())
×
UNCOV
117
    }
×
118

119
    /// Finds the nearest node to the given cursor position.
120
    pub fn find_nearest_node(graph: &crate::entity::graph::Graph, cursor_pos: Vec2) -> Option<usize> {
3✔
121
        let mut nearest_id = None;
3✔
122
        let mut nearest_distance = f32::INFINITY;
3✔
123

124
        for i in 0..graph.node_count() {
9✔
125
            if let Some(node) = graph.get_node(i) {
9✔
126
                let node_pos = node.position + crate::constants::BOARD_PIXEL_OFFSET.as_vec2();
9✔
127
                let distance = cursor_pos.distance(node_pos);
9✔
128

9✔
129
                if distance < nearest_distance {
9✔
130
                    nearest_distance = distance;
6✔
131
                    nearest_id = Some(i);
6✔
132
                }
6✔
UNCOV
133
            }
×
134
        }
135

136
        nearest_id
3✔
137
    }
3✔
138
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc