-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
render: drawing grid, removed dep on gmath lib
- Loading branch information
Showing
5 changed files
with
72 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use ggez::graphics::{Color, Mesh, MeshBuilder}; | ||
use ggez::nalgebra as na; | ||
use ggez::Context; | ||
use ggez::{graphics, GameResult}; | ||
|
||
const LINE_WIDTH: f32 = 2.0; | ||
|
||
pub struct GridView { | ||
ncols: u32, | ||
nrows: u32, | ||
tile_size: u32, | ||
mesh: Option<Mesh>, | ||
} | ||
|
||
impl GridView { | ||
pub fn new(ncols: u32, nrows: u32, tile_size: u32) -> Self { | ||
GridView { | ||
ncols, | ||
nrows, | ||
tile_size, | ||
mesh: None, | ||
} | ||
} | ||
|
||
pub fn render(&mut self, ctx: &mut Context) -> GameResult<()> { | ||
let max_x = (self.ncols * self.tile_size) as f32; | ||
let max_y = (self.nrows * self.tile_size) as f32; | ||
match &self.mesh { | ||
None => { | ||
let mut mesh_builder = MeshBuilder::new(); | ||
for row in 0..self.nrows + 1 { | ||
let y = (row * self.tile_size) as f32; | ||
mesh_builder.line( | ||
&[na::Point2::new(0.0, y), na::Point2::new(max_x, y)], | ||
LINE_WIDTH, | ||
(0x0, 0x0, 0x0).into(), | ||
)?; | ||
} | ||
for col in 0..self.ncols + 1 { | ||
let x = (col * self.tile_size) as f32; | ||
mesh_builder.line( | ||
&[na::Point2::new(x, 0.0), na::Point2::new(x, max_y)], | ||
LINE_WIDTH, | ||
(0x0, 0x0, 0x0).into(), | ||
)?; | ||
} | ||
|
||
let mesh = mesh_builder.build(ctx)?; | ||
graphics::draw( | ||
ctx, | ||
&mesh, | ||
(na::Point2::new(0.0, 0.0), 0.0, graphics::WHITE), | ||
)?; | ||
self.mesh = Some(mesh); | ||
} | ||
Some(mesh) => { | ||
graphics::draw(ctx, mesh, (na::Point2::new(0.0, 0.0), 0.0, graphics::WHITE))?; | ||
} | ||
}; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub(crate) mod floor_view; | ||
pub(crate) mod floor_view; | ||
pub(crate) mod grid_view; |