adding in a simple line algorithm

This commit is contained in:
Able 2021-11-28 19:30:17 -06:00
parent 5ca212986e
commit 0c86164b08
2 changed files with 78 additions and 18 deletions

View file

@ -1,6 +1,7 @@
#![allow(dead_code, unused)] #![allow(dead_code, unused)]
use able_graphics_library::{AglApi, FrameBuffer, Point, HEIGHT, RGBA, WIDTH}; 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)] #[derive(Clone, Debug, Copy)]
pub struct GraphicsRenderer { pub struct GraphicsRenderer {
@ -23,7 +24,25 @@ impl GraphicsRenderer {
impl AglApi for GraphicsRenderer { impl AglApi for GraphicsRenderer {
fn put_line(&mut self, coords_start: Point, coords_end: Point, thickness: u32, color: RGBA) { fn put_line(&mut self, coords_start: Point, coords_end: Point, thickness: u32, color: RGBA) {
todo!(); // 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 - x1;
let dy = y2 - 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;
}
// todo!();
} }
fn put_rect(&mut self, coords_start: Point, coords_end: Point, color: RGBA) { fn put_rect(&mut self, coords_start: Point, coords_end: Point, color: RGBA) {
todo!(); todo!();

View file

@ -1,6 +1,10 @@
use able_graphics_library::{AglApi, HEIGHT, WIDTH}; use able_graphics_library::{AglApi, HEIGHT, WIDTH};
use able_graphics_library::{Point, RGBA}; use able_graphics_library::{Point, RGBA};
use graphics_api_impl::GraphicsRenderer; use graphics_api_impl::GraphicsRenderer;
use mini_gl_fb::{
config,
glutin::{dpi::LogicalSize, event_loop::EventLoop},
};
mod graphics_api_impl; mod graphics_api_impl;
extern crate mini_gl_fb; extern crate mini_gl_fb;
@ -9,12 +13,25 @@ fn main() {
let mut xyz = GraphicsRenderer::new(); let mut xyz = GraphicsRenderer::new();
// Create the event loop and framebuffer // Create the event loop and framebuffer
let (mut event_loop, mut fb) = mini_gl_fb::gotta_go_fast("AGL", WIDTH as f64, HEIGHT as f64); // mini_gl_fb::gotta_go_fast( );
let mut event_loop = EventLoop::new();
let mut fb = mini_gl_fb::get_fancy(
config! {
window_title: String::from("AGL"),
window_size: LogicalSize::new(WIDTH as f64, HEIGHT as f64),
buffer_size: Some(LogicalSize::new(WIDTH as u32, HEIGHT as u32)),
resizable: true
},
&event_loop,
);
// Fill the buffer with something // Fill the buffer with something
let mut buffer = vec![[0, 0, 0, 0]; WIDTH * HEIGHT]; let mut buffer = vec![[0, 0, 0, 0]; WIDTH * HEIGHT];
// Use cool fancy drawing api // Use cool fancy drawing api
{
xyz.clear(RGBA { xyz.clear(RGBA {
r: 255, r: 255,
g: 255, g: 255,
@ -41,6 +58,30 @@ fn main() {
a: 123, a: 123,
}, },
); );
}
xyz.put_line(
Point { x: 3, y: 30 },
Point { x: 324, y: 50 },
1,
RGBA {
r: 0,
g: 0,
b: 0,
a: 0,
},
);
xyz.put_line(
Point { x: 0, y: 0 },
Point { x: 300, y: 200 },
1,
RGBA {
r: 0,
g: 0,
b: 0,
a: 0,
},
);
// update pixels // update pixels
for x in 0..600 * 400 { for x in 0..600 * 400 {