Commit dc36ec04 authored by Eisha Chen-yen-su's avatar Eisha Chen-yen-su

Implement backend

parent f3134b1b
This diff is collapsed.
......@@ -9,6 +9,7 @@ gdk = "0.9.0"
gdk-pixbuf = "0.5.0"
gio = "0.5.0"
glib = "0.6.0"
image = "*"
[dependencies.gtk]
features = ["v3_22"]
......
use std::sync::mpsc::{Receiver, Sender};
use std::thread;
pub fn launch(cmd_chan: Receiver<String>, update_chan: Sender<String>) {
use image::{ImageBuffer, Luma};
use super::foundry::Grid;
use super::bkmessages::BackendUpdate;
const image_buffer_path: &str = "/tmp/automata_buffer.png";
pub fn launch(cmd_chan: Receiver<String>, update_chan: Sender<BackendUpdate>) {
thread::spawn(move || {
let mut count = 0;
let mut grid: Option<Grid> = None;
loop {
match cmd_chan.recv() {
Ok(_) => {
count += 1;
if grid.is_none() {
grid = Some(Grid::new_random(
&String::from("#Toroidal Life"),
true,
&vec![2, 3],
&vec![3],
80,
24,
));
} else {
grid.as_mut().unwrap().randomize();
}
let width = grid.as_ref().unwrap().get_width();
let height = grid.as_ref().unwrap().get_height();
let mut bytes: Vec<u8> = Vec::with_capacity(width * height);
for y in 0..height {
for x in 0..width {
bytes.push(grid.as_ref().unwrap().get_cell_state(x as i64, y as i64));
}
}
let img =
ImageBuffer::<Luma<u8>, _>::from_raw(width as u32, height as u32, bytes)
.unwrap();
img.save(image_buffer_path).unwrap();
update_chan
.send(String::from(format!(
"Action button clicked {} times!",
count
)))
.send(BackendUpdate::NewState(String::from(image_buffer_path)))
.unwrap();
}
Err(_) => {
......
......@@ -2,6 +2,10 @@ extern crate gio;
extern crate glib;
extern crate gtk;
extern crate image;
extern crate foundry;
// http://gtk-rs.org/tuto/closures
#[macro_export]
macro_rules! clone {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment