From 12d4439037a55c13b9e5202b2cc7890e6b748a20 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Fri, 8 Jan 2021 20:18:09 -0600 Subject: [PATCH] draw the grid --- src/game/grid.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ src/game/mod.rs | 20 ++++++---------- todo.txt | 1 - 3 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 src/game/grid.rs diff --git a/src/game/grid.rs b/src/game/grid.rs new file mode 100644 index 0000000..d7dd8ea --- /dev/null +++ b/src/game/grid.rs @@ -0,0 +1,62 @@ +use anyhow::Result; +use ggez::{ + graphics::{self, Color, DrawMode, DrawParam, Mesh, StrokeOptions}, + nalgebra::Point2, + Context, +}; + +use super::{Game, PLAYFIELD_BOUNDS as BOUNDS}; + +pub const GRID_HEAVY: Color = Color::new(1.0, 1.0, 1.0, 0.25); +pub const GRID_LIGHT: Color = Color::new(1.0, 1.0, 1.0, 0.05); + +impl Game { + pub(super) fn draw_grid(&self, ctx: &mut Context) -> Result<()> { + let playfield = Mesh::new_rectangle( + ctx, + DrawMode::Stroke(StrokeOptions::default()), + BOUNDS, + Color::new(1.0, 1.0, 1.0, 0.5), + )?; + graphics::draw(ctx, &playfield, DrawParam::default())?; + + let min_x = BOUNDS.x; + let min_y = BOUNDS.y; + let max_x = min_x + BOUNDS.w; + let max_y = min_y + BOUNDS.h; + + let grid_size = self.beatmap.inner.grid_size; + + for x in (0..512).step_by(grid_size as usize) { + let (weight, color) = match x { + x if x == 0 || x == 256 || x == 512 => (2.0, GRID_HEAVY), + _ => (1.0, GRID_LIGHT), + }; + let x = BOUNDS.x + x as f32 * BOUNDS.w / 512.0; + let line = Mesh::new_line( + ctx, + &[Point2::new(x, min_y), Point2::new(x, max_y)], + weight, + color, + )?; + graphics::draw(ctx, &line, DrawParam::default())?; + } + + for y in (0..384).step_by(grid_size as usize) { + let (weight, color) = match y { + y if y == 0 || y == 192 || y == 384 => (2.0, GRID_HEAVY), + _ => (1.0, GRID_LIGHT), + }; + let y = BOUNDS.y + y as f32 * BOUNDS.h / 384.0; + let line = Mesh::new_line( + ctx, + &[Point2::new(min_x, y), Point2::new(max_x, y)], + weight, + color, + )?; + graphics::draw(ctx, &line, DrawParam::default())?; + } + + Ok(()) + } +} diff --git a/src/game/mod.rs b/src/game/mod.rs index 8d6c81d..17ae341 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -1,3 +1,4 @@ +mod grid; mod sliders; mod timeline; @@ -10,13 +11,11 @@ use anyhow::Result; use ggez::{ event::{EventHandler, KeyCode, KeyMods}, graphics::{ - self, Color, DrawMode, DrawParam, FillOptions, FilterMode, Mesh, Rect, StrokeOptions, Text, - WHITE, + self, Color, DrawMode, DrawParam, FilterMode, Mesh, Rect, StrokeOptions, Text, WHITE, }, - nalgebra::Point2, Context, GameError, GameResult, }; -use libosu::{Beatmap, HitObject, HitObjectKind, Point, SpinnerInfo, Spline}; +use libosu::{Beatmap, HitObjectKind, Point, SpinnerInfo, Spline}; use crate::audio::{AudioEngine, Sound}; use crate::beatmap::{BeatmapExt, STACK_DISTANCE}; @@ -25,6 +24,9 @@ use crate::skin::Skin; use self::sliders::render_slider; +pub const PLAYFIELD_BOUNDS: Rect = Rect::new(112.0, 112.0, 800.0, 600.0); +pub const SEEKER_BOUNDS: Rect = Rect::new(46.0, 722.0, 932.0, 36.0); + pub type SliderCache = HashMap>, Spline>; pub struct Game { @@ -89,18 +91,10 @@ impl Game { fn priv_draw(&mut self, ctx: &mut Context) -> Result<()> { // TODO: lol - const PLAYFIELD_BOUNDS: Rect = Rect::new(112.0, 112.0, 800.0, 600.0); - const SEEKER_BOUNDS: Rect = Rect::new(46.0, 722.0, 932.0, 36.0); graphics::clear(ctx, [0.0, 0.0, 0.0, 1.0].into()); - let playfield = Mesh::new_rectangle( - ctx, - DrawMode::Stroke(StrokeOptions::default()), - PLAYFIELD_BOUNDS, - Color::new(1.0, 1.0, 1.0, 0.5), - )?; - graphics::draw(ctx, &playfield, DrawParam::default())?; + self.draw_grid(ctx)?; let time = self.song.as_ref().unwrap().position()?; let text = Text::new(format!("time: {}", time).as_ref()); diff --git a/todo.txt b/todo.txt index 4c77ee7..1f185ab 100644 --- a/todo.txt +++ b/todo.txt @@ -1,4 +1,3 @@ - stacking - don't draw overlaps between slider segments -- use skin components - timeline