diff options
Diffstat (limited to 'src/grid.rs')
| -rw-r--r-- | src/grid.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/grid.rs b/src/grid.rs new file mode 100644 index 0000000..1ec49a3 --- /dev/null +++ b/src/grid.rs @@ -0,0 +1,38 @@ +use crate::transform::Transform; +use raylib::drawing::RaylibDraw; +use raylib::ffi::Color; + +pub const LINE_COLOUR: Color = Color { + r: 255, + g: 255, + b: 255, + a: 75, +}; + +/// Draw an infinite grid that can be moved around on the screen and zoomed in and out of. +pub fn draw_grid<D>(rld: &mut D, screen_width: i32, screen_height: i32, transform: &Transform) +where + D: RaylibDraw, +{ + /* Calculate the actual screen offset of the grid, by modulo-ing the translation of the + * transform. + */ + let translation_x_px: i32 = + transform.translation_px().x as i32 % transform.pixels_per_m() as i32; + let translation_y_px: i32 = + transform.translation_px().y as i32 % transform.pixels_per_m() as i32; + + // Draw the row lines. + let mut line_y: f32 = translation_y_px as f32; + while line_y <= screen_height as f32 { + rld.draw_line(0, line_y as i32, screen_width, line_y as i32, LINE_COLOUR); + line_y += transform.pixels_per_m(); + } + + // Draw the column lines. + let mut line_x: f32 = translation_x_px as f32; + while line_x <= screen_width as f32 { + rld.draw_line(line_x as i32, 0, line_x as i32, screen_height, LINE_COLOUR); + line_x += transform.pixels_per_m(); + } +} |
