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

Xevion / Pac-Man / 16919127499

12 Aug 2025 07:40PM UTC coverage: 45.274% (-0.7%) from 45.95%
16919127499

push

github

Xevion
refactor: restructure game logic and state management into separate modules

- Moved game logic from `game.rs` to `game/mod.rs` and `game/state.rs` for better organization.
- Updated `App` to utilize the new `Game` struct and its state management.
- Refactored error handling
- Removed unused audio subsystem references

11 of 319 new or added lines in 10 files covered. (3.45%)

4 existing lines in 3 files now uncovered.

891 of 1968 relevant lines covered (45.27%)

413.62 hits per line

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

46.25
/src/texture/sprite.rs
1
use anyhow::Result;
2
use glam::U16Vec2;
3
use sdl2::pixels::Color;
4
use sdl2::rect::Rect;
5
use sdl2::render::{Canvas, RenderTarget, Texture};
6
use serde::Deserialize;
7
use std::collections::HashMap;
8

9
use crate::error::TextureError;
10

11
/// A simple sprite for stationary items like pellets and energizers.
12
#[derive(Clone, Debug)]
13
pub struct Sprite {
14
    pub atlas_tile: AtlasTile,
15
}
16

17
impl Sprite {
18
    pub fn new(atlas_tile: AtlasTile) -> Self {
494✔
19
        Self { atlas_tile }
494✔
20
    }
494✔
21

NEW
22
    pub fn render<C: RenderTarget>(
×
NEW
23
        &self,
×
NEW
24
        canvas: &mut Canvas<C>,
×
NEW
25
        atlas: &mut SpriteAtlas,
×
NEW
26
        position: glam::Vec2,
×
NEW
27
    ) -> Result<(), TextureError> {
×
28
        let dest = crate::helpers::centered_with_size(
×
29
            glam::IVec2::new(position.x as i32, position.y as i32),
×
30
            glam::UVec2::new(self.atlas_tile.size.x as u32, self.atlas_tile.size.y as u32),
×
31
        );
×
32
        let mut tile = self.atlas_tile;
×
NEW
33
        tile.render(canvas, atlas, dest)?;
×
NEW
34
        Ok(())
×
UNCOV
35
    }
×
36
}
37

38
#[derive(Clone, Debug, Deserialize)]
39
pub struct AtlasMapper {
40
    pub frames: HashMap<String, MapperFrame>,
41
}
42

43
#[derive(Copy, Clone, Debug, Deserialize)]
44
pub struct MapperFrame {
45
    pub x: u16,
46
    pub y: u16,
47
    pub width: u16,
48
    pub height: u16,
49
}
50

51
#[derive(Copy, Clone, Debug)]
52
pub struct AtlasTile {
53
    pub pos: U16Vec2,
54
    pub size: U16Vec2,
55
    pub color: Option<Color>,
56
}
57

58
impl AtlasTile {
NEW
59
    pub fn render<C: RenderTarget>(
×
NEW
60
        &mut self,
×
NEW
61
        canvas: &mut Canvas<C>,
×
NEW
62
        atlas: &mut SpriteAtlas,
×
NEW
63
        dest: Rect,
×
NEW
64
    ) -> Result<(), TextureError> {
×
65
        let color = self.color.unwrap_or(atlas.default_color.unwrap_or(Color::WHITE));
×
NEW
66
        self.render_with_color(canvas, atlas, dest, color)?;
×
NEW
67
        Ok(())
×
UNCOV
68
    }
×
69

70
    pub fn render_with_color<C: RenderTarget>(
×
71
        &mut self,
×
72
        canvas: &mut Canvas<C>,
×
73
        atlas: &mut SpriteAtlas,
×
74
        dest: Rect,
×
75
        color: Color,
×
NEW
76
    ) -> Result<(), TextureError> {
×
77
        let src = Rect::new(self.pos.x as i32, self.pos.y as i32, self.size.x as u32, self.size.y as u32);
×
78

×
79
        if atlas.last_modulation != Some(color) {
×
80
            atlas.texture.set_color_mod(color.r, color.g, color.b);
×
81
            atlas.last_modulation = Some(color);
×
82
        }
×
83

NEW
84
        canvas.copy(&atlas.texture, src, dest).map_err(TextureError::RenderFailed)?;
×
85
        Ok(())
×
86
    }
×
87

88
    /// Creates a new atlas tile.
89
    #[allow(dead_code)]
90
    pub fn new(pos: U16Vec2, size: U16Vec2, color: Option<Color>) -> Self {
4✔
91
        Self { pos, size, color }
4✔
92
    }
4✔
93

94
    /// Sets the color of the tile.
95
    #[allow(dead_code)]
96
    pub fn with_color(mut self, color: Color) -> Self {
2✔
97
        self.color = Some(color);
2✔
98
        self
2✔
99
    }
2✔
100
}
101

102
pub struct SpriteAtlas {
103
    texture: Texture<'static>,
104
    tiles: HashMap<String, MapperFrame>,
105
    default_color: Option<Color>,
106
    last_modulation: Option<Color>,
107
}
108

109
impl SpriteAtlas {
110
    pub fn new(texture: Texture<'static>, mapper: AtlasMapper) -> Self {
22✔
111
        Self {
22✔
112
            texture,
22✔
113
            tiles: mapper.frames,
22✔
114
            default_color: None,
22✔
115
            last_modulation: None,
22✔
116
        }
22✔
117
    }
22✔
118

119
    pub fn get_tile(&self, name: &str) -> Option<AtlasTile> {
272✔
120
        self.tiles.get(name).map(|frame| AtlasTile {
272✔
121
            pos: U16Vec2::new(frame.x, frame.y),
270✔
122
            size: U16Vec2::new(frame.width, frame.height),
270✔
123
            color: None,
270✔
124
        })
272✔
125
    }
272✔
126

127
    #[allow(dead_code)]
128
    pub fn set_color(&mut self, color: Color) {
2✔
129
        self.default_color = Some(color);
2✔
130
    }
2✔
131

132
    #[allow(dead_code)]
133
    pub fn texture(&self) -> &Texture<'static> {
×
134
        &self.texture
×
135
    }
×
136

137
    /// Returns the number of tiles in the atlas.
138
    #[allow(dead_code)]
139
    pub fn tiles_count(&self) -> usize {
2✔
140
        self.tiles.len()
2✔
141
    }
2✔
142

143
    /// Returns true if the atlas has a tile with the given name.
144
    #[allow(dead_code)]
145
    pub fn has_tile(&self, name: &str) -> bool {
6✔
146
        self.tiles.contains_key(name)
6✔
147
    }
6✔
148

149
    /// Returns the default color of the atlas.
150
    #[allow(dead_code)]
151
    pub fn default_color(&self) -> Option<Color> {
4✔
152
        self.default_color
4✔
153
    }
4✔
154
}
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