rustraight 0.5.2

A simple 2D game library for Rust, inspired by DXLib
// draw.rs — 図形描画の実装。GPU に渡す三角形頂点列を生成する(`verts_*`)。

/// RGBA カラー(各チャンネル 0-255)。
#[derive(Clone, Copy)]
pub struct Color {
    pub r: u8,
    pub g: u8,
    pub b: u8,
    pub a: u8,
}

impl Color {
    pub const fn rgb(r: u8, g: u8, b: u8) -> Self { Self { r, g, b, a: 255 } }
    pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self { Self { r, g, b, a } }

    pub const WHITE:   Color = Color::rgb(255, 255, 255);
    pub const BLACK:   Color = Color::rgb(0,   0,   0  );
    pub const RED:     Color = Color::rgb(255, 0,   0  );
    pub const GREEN:   Color = Color::rgb(0,   255, 0  );
    pub const BLUE:    Color = Color::rgb(0,   0,   255);
    pub const YELLOW:  Color = Color::rgb(255, 255, 0  );
    pub const CYAN:    Color = Color::rgb(0,   255, 255);
    pub const MAGENTA: Color = Color::rgb(255, 0,   255);
}

// ── GPU vertex generation ──────────────────────────────────────────────────────
// wgpu にそのまま流し込める三角形リストを生成する関数群。
// ピクセル座標 → NDC(正規化デバイス座標、-1..1)への変換は to_ndc() が担う。

#[repr(C)]
#[derive(Copy, Clone)]
pub(crate) struct ColorVert {
    pub pos:   [f32; 2],
    pub color: [f32; 4],
}

/// ピクセル座標 (px, py) をスクリーンサイズ (sw, sh) 基準で NDC 座標に変換する。
pub(crate) fn to_ndc(px: f32, py: f32, sw: u32, sh: u32) -> [f32; 2] {
    [px / sw as f32 * 2.0 - 1.0, 1.0 - py / sh as f32 * 2.0]
}

/// Color を 0.0-1.0 の浮動小数点 RGBA に変換する。
fn cf(c: Color) -> [f32; 4] {
    [c.r as f32 / 255.0, c.g as f32 / 255.0, c.b as f32 / 255.0, c.a as f32 / 255.0]
}

/// 矩形1つ分の頂点(2三角形=6頂点)を追加する。
fn push_quad(v: &mut Vec<ColorVert>, x0: f32, y0: f32, x1: f32, y1: f32, sw: u32, sh: u32, c: Color) {
    let col = cf(c);
    for (x, y) in [(x0,y0),(x1,y0),(x0,y1),(x1,y0),(x1,y1),(x0,y1)] {
        v.push(ColorVert { pos: to_ndc(x, y, sw, sh), color: col });
    }
}

/// 太さ1ピクセル相当の線分を、進行方向に垂直な矩形(2三角形)として追加する。
fn push_line(v: &mut Vec<ColorVert>, x0: f32, y0: f32, x1: f32, y1: f32, sw: u32, sh: u32, c: Color) {
    let dx = x1 - x0;
    let dy = y1 - y0;
    let len = (dx*dx + dy*dy).sqrt();
    if len < 0.5 { push_quad(v, x0, y0, x0+1.0, y0+1.0, sw, sh, c); return; }
    let (nx, ny) = (-dy / len, dx / len); // 進行方向に垂直な単位法線ベクトル
    let col = cf(c);
    for (x, y) in [(x0+nx,y0+ny),(x1+nx,y1+ny),(x0-nx,y0-ny),(x1+nx,y1+ny),(x1-nx,y1-ny),(x0-nx,y0-ny)] {
        v.push(ColorVert { pos: to_ndc(x, y, sw, sh), color: col });
    }
}

/// 画面全体を塗りつぶす矩形の頂点を生成する。
pub(crate) fn verts_fill(sw: u32, sh: u32, c: Color) -> Vec<ColorVert> {
    let mut v = Vec::with_capacity(6);
    push_quad(&mut v, 0.0, 0.0, sw as f32, sh as f32, sw, sh, c);
    v
}

/// 1ピクセル分の頂点を生成する。
pub(crate) fn verts_pixel(x: i32, y: i32, sw: u32, sh: u32, c: Color) -> Vec<ColorVert> {
    let mut v = Vec::with_capacity(6);
    push_quad(&mut v, x as f32, y as f32, x as f32 + 1.0, y as f32 + 1.0, sw, sh, c);
    v
}

/// 線分の頂点を生成する。
pub(crate) fn verts_line(x1: i32, y1: i32, x2: i32, y2: i32, sw: u32, sh: u32, c: Color) -> Vec<ColorVert> {
    let mut v = Vec::with_capacity(6);
    push_line(&mut v, x1 as f32, y1 as f32, x2 as f32, y2 as f32, sw, sh, c);
    v
}

/// 矩形の頂点を生成する(`filled` = true で塗りつぶし、false で4辺を1px幅の矩形として生成)。
pub(crate) fn verts_rectangle(x: i32, y: i32, w: i32, h: i32, sw: u32, sh: u32, c: Color, filled: bool) -> Vec<ColorVert> {
    let mut v = Vec::new();
    if filled {
        push_quad(&mut v, x as f32, y as f32, (x+w) as f32, (y+h) as f32, sw, sh, c);
    } else {
        push_quad(&mut v, x as f32,       y as f32,       (x+w) as f32,   y as f32 + 1.0,    sw, sh, c);
        push_quad(&mut v, x as f32,       (y+h-1) as f32, (x+w) as f32,   (y+h) as f32,       sw, sh, c);
        push_quad(&mut v, x as f32,       (y+1) as f32,   x as f32 + 1.0, (y+h-1) as f32,     sw, sh, c);
        push_quad(&mut v, (x+w-1) as f32, (y+1) as f32,   (x+w) as f32,   (y+h-1) as f32,     sw, sh, c);
    }
    v
}

/// 三角形の頂点を生成する(`filled` = true で塗りつぶし1枚、false で3辺の線分)。
pub(crate) fn verts_triangle(x1: i32, y1: i32, x2: i32, y2: i32, x3: i32, y3: i32, sw: u32, sh: u32, c: Color, filled: bool) -> Vec<ColorVert> {
    let col = cf(c);
    let mut v = Vec::new();
    if filled {
        for (x, y) in [(x1,y1),(x2,y2),(x3,y3)] {
            v.push(ColorVert { pos: to_ndc(x as f32, y as f32, sw, sh), color: col });
        }
    } else {
        push_line(&mut v, x1 as f32, y1 as f32, x2 as f32, y2 as f32, sw, sh, c);
        push_line(&mut v, x2 as f32, y2 as f32, x3 as f32, y3 as f32, sw, sh, c);
        push_line(&mut v, x3 as f32, y3 as f32, x1 as f32, y1 as f32, sw, sh, c);
    }
    v
}

/// 円の頂点を生成する。円周を n 個の線分に近似し、塗りつぶしは扇形三角形、
/// 枠線は各線分を push_line() で生成する。n は半径に応じて 16〜256 に調整する。
pub(crate) fn verts_circle(cx: i32, cy: i32, radius: i32, sw: u32, sh: u32, c: Color, filled: bool) -> Vec<ColorVert> {
    let col = cf(c);
    let (cx, cy, r) = (cx as f32, cy as f32, radius.max(1) as f32);
    let n = (radius * 4).clamp(16, 256) as usize;
    let mut v = Vec::with_capacity(if filled { n * 3 } else { n * 6 });
    for i in 0..n {
        let a0 = i       as f32 / n as f32 * std::f32::consts::TAU;
        let a1 = (i + 1) as f32 / n as f32 * std::f32::consts::TAU;
        let (p0x, p0y) = (cx + r * a0.cos(), cy + r * a0.sin());
        let (p1x, p1y) = (cx + r * a1.cos(), cy + r * a1.sin());
        if filled {
            v.push(ColorVert { pos: to_ndc(cx,  cy,  sw, sh), color: col });
            v.push(ColorVert { pos: to_ndc(p0x, p0y, sw, sh), color: col });
            v.push(ColorVert { pos: to_ndc(p1x, p1y, sw, sh), color: col });
        } else {
            push_line(&mut v, p0x, p0y, p1x, p1y, sw, sh, c);
        }
    }
    v
}