Skip to content

Commit

Permalink
render: drawing grid, removed dep on gmath lib
Browse files Browse the repository at this point in the history
  • Loading branch information
thlorenz committed Jul 26, 2020
1 parent 082c200 commit afbb811
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 20 deletions.
13 changes: 0 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ edition = "2018"

[dependencies]
ggez = "0.5.1"
cgmath = { version = "0.17", features = ["mint"]}


[[bin]]
name="generate_conf"
Expand Down
12 changes: 8 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,32 @@ use crate::arena::arena::Arena;
use crate::engine::image_asset::ImageAsset;
use crate::game_props::TILE_SIZE;
use crate::views::floor_view::FloorView;
use cgmath;
use crate::views::grid_view::GridView;
use ggez;
use ggez::conf::Backend;
use ggez::event;
use ggez::graphics;
use ggez::graphics::{Color, Image};
use ggez::nalgebra as na;
use ggez::{Context, GameResult};

struct GameState {
frames: usize,
floor_view: FloorView,
grid_view: GridView,
font: graphics::Font,
}

impl GameState {
fn new(ctx: &mut Context, arena: Arena) -> GameResult<GameState> {
let font = graphics::Font::new(ctx, "/fonts/RobotoMono.ttf")?;
let floor_view = init_floor_view(ctx, &arena)?;
let grid_view = GridView::new(arena.ncols, arena.nrows, TILE_SIZE);

let s = GameState {
frames: 0,
font,
floor_view,
grid_view,
};
Ok(s)
}
Expand All @@ -53,14 +56,15 @@ impl event::EventHandler for GameState {
graphics::clear(ctx, Color::from_rgb(0xaa, 0xaa, 0xaa));

let offset = self.frames as f32 / 10.0;
let dest_point = cgmath::Point2::new(offset, offset);
let dest_point = na::Point2::new(offset, offset);
let text = graphics::Text::new((
format!("Frame: {}, Hello world! at {}", self.frames, offset),
self.font,
48.0,
));

self.floor_view.render(ctx, game_props::USE_SPRITE_BATCH)?;
// self.floor_view.render(ctx, game_props::USE_SPRITE_BATCH)?;
self.grid_view.render(ctx)?;
graphics::draw(ctx, &text, (dest_point, Color::from_rgb(255, 0, 0)))?;
graphics::present(ctx)?;

Expand Down
62 changes: 62 additions & 0 deletions src/views/grid_view.rs
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(())
}
}
3 changes: 2 additions & 1 deletion src/views/mod.rs
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;

0 comments on commit afbb811

Please sign in to comment.