This repository has been archived on 2021-12-01. You can view files and clone it, but cannot push or open issues/pull-requests.
AGL_Simple_Impl/src/graphics_api_impl.rs

150 lines
4.8 KiB
Rust

#![allow(dead_code, unused)]
use ab_glyph::{FontRef, ScaleFont};
use able_graphics_library::{AglApi, FrameBuffer, Point, HEIGHT, RGBA, WIDTH};
use mini_gl_fb::glutin::platform::unix::x11::ffi::XK_R10;
#[derive(Clone, Debug, Copy)]
pub struct GraphicsRenderer {
pub buff: FrameBuffer,
}
impl GraphicsRenderer {
pub fn new() -> GraphicsRenderer {
let rgba = RGBA {
r: 0,
g: 0,
b: 0,
a: 0,
};
let buffer = [rgba; WIDTH * HEIGHT];
Self { buff: buffer }
}
}
impl AglApi for GraphicsRenderer {
fn put_line(&mut self, coords_start: Point, coords_end: Point, thickness: u32, color: RGBA) {
// Wikipedia's naive line drawing algorithm
let x1 = coords_start.x;
let y1 = coords_start.y;
let x2 = coords_end.x;
let y2 = coords_end.y;
let dx = x2.wrapping_sub(x1);
let dy = y2.wrapping_sub(y1);
for x in x1..x2 {
let y = y1 + dy * (x - x1) / dx;
let coordinates = x + (WIDTH as u16) * y;
self.buff[coordinates as usize] = color;
}
}
fn put_rect(&mut self, coords_start: Point, coords_end: Point, color: RGBA) {
todo!();
}
fn put_circle(&mut self, coords: Point, radius: u32) {
todo!();
}
fn put_pixel(&mut self, coords: Point, color: RGBA) {
let coordinates = coords.x + (WIDTH as u16).wrapping_mul(coords.y);
self.buff[coordinates as usize] = color;
}
fn put_triangle(
&mut self,
coords_1: Point,
coords_2: Point,
coords_3: Point,
thickness: u32,
color: RGBA,
) {
self.put_line(coords_1, coords_2, thickness, color);
self.put_line(coords_2, coords_3, thickness, color);
self.put_line(coords_3, coords_1, thickness, color);
}
fn paint_cursor(&mut self, coords: Point) {
todo!();
}
fn hide_cursor() {
todo!();
}
fn show_cursor() {
todo!();
}
fn draw_text(&mut self, coords: Point, scale: u8, color: RGBA, text: String) {
use ab_glyph::{point, Font, FontRef, Glyph};
let font = FontRef::try_from_slice(include_bytes!("../font/Roboto-Regular.ttf")).unwrap();
let font_scaled = font.as_scaled(scale as f32);
// Individual characters are drawn at floating-point offsets.
let mut coords = point(coords.x as f32, coords.y as f32);
let mut last_char = None;
for ch in text.chars() {
let glyph_id = font.glyph_id(ch);
// Kern character pairs as necessary.
if let Some(last) = last_char {
coords.x -= font_scaled.kern(last, glyph_id);
}
last_char = Some(font.glyph_id(ch));
// Get a glyph with a scale & position.
let glyph: Glyph = glyph_id
.with_scale_and_position(scale as f32, point(coords.x as f32, coords.y as f32));
// Draw it.
if let Some(outline) = font.outline_glyph(glyph) {
outline.draw(|x, y, c| {
// ab_glyph gives us coordinates within the
// bounding box; convert these to coordinates
// within the image.
let corner = outline.px_bounds().min;
let (x, y) = (x + corner.x as u32, y + corner.y as u32);
// Flip y to draw characters right-side-up.
let y = 2 * coords.y as u32 - y;
// draw pixel `(x, y)` with coverage: `c`
let coordinates = x as u16 + (WIDTH as u16).wrapping_mul(y as u16);
let pixel = &mut self.buff[coordinates as usize];
// Interpolate between the existing background
// character and the new foreground color.
*pixel = lerp_rgba(*pixel, color, c);
});
}
// Advance to the next glyph's position.
coords.x += font_scaled.h_advance(glyph_id);
}
}
/// Actually move the double buffer to the single buffer and "update" the screen
fn draw(&mut self) {}
fn clear(&mut self, color: RGBA) {
for x in 0..600 * 400 {
self.buff[x as usize] = color;
}
}
}
/// Linearly interpolate between two colors according to a factor.
/// This method is cheap, perfectly accurate for shades of gray, and
/// reasonably accurate for other colors; if better results are
/// needed, LCH interpolation should be used instead.
fn lerp_rgba(a: RGBA, b: RGBA, fac: f32) -> RGBA {
RGBA {
r: ((b.r as f32 - a.r as f32) * fac + a.r as f32) as _,
g: ((b.g as f32 - a.g as f32) * fac + a.g as f32) as _,
b: ((b.b as f32 - a.b as f32) * fac + a.b as f32) as _,
a: ((b.a as f32 - a.a as f32) * fac + a.a as f32) as _,
}
}