From b90c2c4bc61248d5ea2e894fc70b84bc00582406 Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 7 Apr 2021 19:07:46 +0530 Subject: implement iso grids; lisp primitives to manipulate grids --- src/grid.rs | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lisp/prelude.rs | 11 ++++++ 2 files changed, 118 insertions(+) create mode 100644 src/grid.rs (limited to 'src') diff --git a/src/grid.rs b/src/grid.rs new file mode 100644 index 0000000..38efaba --- /dev/null +++ b/src/grid.rs @@ -0,0 +1,107 @@ +// most of this module was written out of frustration +// TODO: make all this less messy + +use sdl2::{pixels::Color, rect::Point, render::Canvas, video::Window}; + +use crate::{ + consts::{colors::*, ANGLE}, + rect, +}; + +#[derive(Debug, Copy, Clone)] +pub enum GridKind { + Rectangle, + Isometric, +} + +pub struct Grid { + pub enabled: bool, + pub color: Color, + pub kind: GridKind, +} + +impl Grid { + pub fn new() -> Self { + Self { + enabled: true, + color: GRID_COLOR, + kind: GridKind::Rectangle, + } + } + + pub fn draw( + &self, + canvas: &mut Canvas, + zoom: u8, + start: &Point, + width: u32, + height: u32, + ) { + let cs = zoom as u32; + let (x, y) = (start.x(), start.y()); + canvas.set_draw_color(self.color); + + match self.kind { + GridKind::Rectangle => { + let mut vertical_tuples = (1..width) + .map(|i| { + let x = (i * cs) as i32; + let y = (height * cs) as i32; + if i % 2 == 0 { + (*start + Point::new(x, 0), *start + Point::new(x, y)) + } else { + (*start + Point::new(x, y), *start + Point::new(x, 0)) + } + }) + .collect::>(); + + // SAFETY + // The mem space occupied by a [(T, T); N] is the same as twice the mem space of a + // [T; 2 * N], where T is not a dynamically sized type. + let verticals: Vec = unsafe { + vertical_tuples.set_len(vertical_tuples.len() * 2); + std::mem::transmute(vertical_tuples) + }; + + let mut horizontal_tuples = (1..height) + .map(|j| { + let x = (width * cs) as i32; + let y = (j * cs) as i32; + if j % 2 == 0 { + (*start + Point::new(0, y), *start + Point::new(x, y)) + } else { + (*start + Point::new(x, y), *start + Point::new(0, y)) + } + }) + .collect::>(); + let horizontals: Vec = unsafe { + horizontal_tuples.set_len(horizontal_tuples.len() * 2); + std::mem::transmute(horizontal_tuples) + }; + + canvas.draw_lines(&verticals[..]).unwrap(); + canvas.draw_lines(&horizontals[..]).unwrap(); + } + GridKind::Isometric => { + // tan 26.525 ~= 0.5 + canvas.set_clip_rect(Some(rect!(x, y, width * cs, height * cs))); + for i in (0..2 * height + width).step_by(4) { + let begin = Point::from(((i * cs) as i32, 0i32)); + let end = Point::from((0i32, ((i * cs) as f64 * 0.5).floor() as i32)); + canvas.draw_line(*start + begin, *start + end).unwrap(); + } + for j in (0..height + width / 2).step_by(2) { + let idx = (j as i32 - (width as i32 / 2)) * cs as i32; + let begin = Point::from((-100i32, idx)); + let end = Point::from(( + (width * cs) as i32, + (idx as f64 + (width * cs) as f64 * ANGLE.to_radians().tan()).floor() + as i32, + )); + canvas.draw_line(*start + begin, *start + end).unwrap(); + } + canvas.set_clip_rect(None); + } + } + } +} diff --git a/src/lisp/prelude.rs b/src/lisp/prelude.rs index fee787e..d0667d8 100644 --- a/src/lisp/prelude.rs +++ b/src/lisp/prelude.rs @@ -1,6 +1,7 @@ use crate::{ bitmap::MapPoint, brush::Brush, + grid::{Grid, GridKind}, guide::Guide, lisp::{ error::{EvalError, LispError}, @@ -257,6 +258,16 @@ pub fn new_env() -> Result { Ok(LispExpr::Unit) }); + primitive!(env, Arity::Exact(0), "grid-rectangle", |_, app| { + app.grid.kind = GridKind::Rectangle; + Ok(LispExpr::Unit) + }); + + primitive!(env, Arity::Exact(0), "grid-isometric", |_, app| { + app.grid.kind = GridKind::Isometric; + Ok(LispExpr::Unit) + }); + primitive!(env, Arity::Exact(0), "brush-fill", |_, app| { app.brush = Brush::Fill; Ok(LispExpr::Unit) -- cgit v1.2.3