mirror of https://git.sr.ht/~garritfra/midgard
Garrit Franke
3 years ago
commit
0a5c1836ce
4 changed files with 243 additions and 0 deletions
@ -0,0 +1,117 @@
|
||||
# This file is automatically @generated by Cargo. |
||||
# It is not intended for manual editing. |
||||
[[package]] |
||||
name = "cfg-if" |
||||
version = "1.0.0" |
||||
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" |
||||
|
||||
[[package]] |
||||
name = "game-of-life" |
||||
version = "0.1.0" |
||||
dependencies = [ |
||||
"rand", |
||||
"termion", |
||||
] |
||||
|
||||
[[package]] |
||||
name = "getrandom" |
||||
version = "0.2.1" |
||||
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||
checksum = "4060f4657be78b8e766215b02b18a2e862d83745545de804638e2b545e81aee6" |
||||
dependencies = [ |
||||
"cfg-if", |
||||
"libc", |
||||
"wasi", |
||||
] |
||||
|
||||
[[package]] |
||||
name = "libc" |
||||
version = "0.2.82" |
||||
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||
checksum = "89203f3fba0a3795506acaad8ebce3c80c0af93f994d5a1d7a0b1eeb23271929" |
||||
|
||||
[[package]] |
||||
name = "numtoa" |
||||
version = "0.1.0" |
||||
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||
checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef" |
||||
|
||||
[[package]] |
||||
name = "ppv-lite86" |
||||
version = "0.2.10" |
||||
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" |
||||
|
||||
[[package]] |
||||
name = "rand" |
||||
version = "0.8.2" |
||||
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||
checksum = "18519b42a40024d661e1714153e9ad0c3de27cd495760ceb09710920f1098b1e" |
||||
dependencies = [ |
||||
"libc", |
||||
"rand_chacha", |
||||
"rand_core", |
||||
"rand_hc", |
||||
] |
||||
|
||||
[[package]] |
||||
name = "rand_chacha" |
||||
version = "0.3.0" |
||||
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||
checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" |
||||
dependencies = [ |
||||
"ppv-lite86", |
||||
"rand_core", |
||||
] |
||||
|
||||
[[package]] |
||||
name = "rand_core" |
||||
version = "0.6.1" |
||||
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||
checksum = "c026d7df8b298d90ccbbc5190bd04d85e159eaf5576caeacf8741da93ccbd2e5" |
||||
dependencies = [ |
||||
"getrandom", |
||||
] |
||||
|
||||
[[package]] |
||||
name = "rand_hc" |
||||
version = "0.3.0" |
||||
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||
checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" |
||||
dependencies = [ |
||||
"rand_core", |
||||
] |
||||
|
||||
[[package]] |
||||
name = "redox_syscall" |
||||
version = "0.1.57" |
||||
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||
checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" |
||||
|
||||
[[package]] |
||||
name = "redox_termios" |
||||
version = "0.1.1" |
||||
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||
checksum = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" |
||||
dependencies = [ |
||||
"redox_syscall", |
||||
] |
||||
|
||||
[[package]] |
||||
name = "termion" |
||||
version = "1.5.5" |
||||
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||
checksum = "c22cec9d8978d906be5ac94bceb5a010d885c626c4c8855721a4dbd20e3ac905" |
||||
dependencies = [ |
||||
"libc", |
||||
"numtoa", |
||||
"redox_syscall", |
||||
"redox_termios", |
||||
] |
||||
|
||||
[[package]] |
||||
name = "wasi" |
||||
version = "0.10.1+wasi-snapshot-preview1" |
||||
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||
checksum = "93c6c3420963c5c64bca373b25e77acb562081b9bb4dd5bb864187742186cea9" |
@ -0,0 +1,11 @@
|
||||
[package] |
||||
name = "game-of-life" |
||||
version = "0.1.0" |
||||
authors = ["Garrit Franke <garrit@slashdev.space>"] |
||||
edition = "2018" |
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
||||
|
||||
[dependencies] |
||||
termion = "1.5.5" |
||||
rand = "0.8.2" |
@ -0,0 +1,113 @@
|
||||
extern crate rand; |
||||
extern crate termion; |
||||
|
||||
use crate::termion::cursor::DetectCursorPos; |
||||
use std::fmt::Debug; |
||||
use std::fmt::Display; |
||||
use std::io::{stdin, stdout, Write}; |
||||
use std::path::Path; |
||||
use termion::event::Key; |
||||
use termion::input::TermRead; |
||||
use termion::raw::IntoRawMode; |
||||
use termion::{clear, cursor, screen}; |
||||
|
||||
struct GOL { |
||||
screen: Box<dyn Write>, |
||||
player: Player, |
||||
} |
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)] |
||||
pub struct Player { |
||||
pub pos: Pos, |
||||
} |
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)] |
||||
pub struct Pos { |
||||
pub x: i16, |
||||
pub y: i16, |
||||
} |
||||
|
||||
impl GOL { |
||||
pub fn new() -> Self { |
||||
let screen = screen::AlternateScreen::from(stdout().into_raw_mode().unwrap()); |
||||
let mut s = Self { |
||||
screen: Box::new(screen), |
||||
player: Player { |
||||
pos: Pos { x: 0, y: 0 }, |
||||
}, |
||||
}; |
||||
|
||||
s.write(&termion::clear::All); |
||||
s.write(&termion::cursor::Goto(1, 1)); |
||||
|
||||
log(format!("{:?}", s.player)); |
||||
s.flush(); |
||||
|
||||
s |
||||
} |
||||
|
||||
pub fn write(&mut self, w: &dyn Display) { |
||||
log(w); |
||||
write!(self.screen, "{}", w); |
||||
} |
||||
|
||||
pub fn pos(&mut self) -> (u16, u16) { |
||||
return self.screen.cursor_pos().unwrap(); |
||||
} |
||||
|
||||
pub fn flush(&mut self) { |
||||
self.screen.flush().unwrap(); |
||||
} |
||||
} |
||||
|
||||
fn log(x: impl Display) { |
||||
std::fs::File::create(Path::new("log")).unwrap(); |
||||
let mut file = std::fs::OpenOptions::new() |
||||
.write(true) |
||||
.append(true) |
||||
.open("./log") |
||||
.unwrap(); |
||||
|
||||
if let Err(e) = writeln!(file, "{}", x) { |
||||
eprintln!("Couldn't write to file: {}", e); |
||||
} |
||||
} |
||||
|
||||
fn main() { |
||||
let stdin = stdin(); |
||||
|
||||
let mut gol = GOL::new(); |
||||
|
||||
gol.flush(); |
||||
|
||||
for c in stdin.keys() { |
||||
update(&gol); |
||||
draw(&mut gol); |
||||
let key = c.unwrap(); |
||||
match key { |
||||
Key::Left => gol.player.pos.x -= 1, |
||||
Key::Right => gol.player.pos.x += 1, |
||||
Key::Up => gol.player.pos.y += 1, |
||||
Key::Down => gol.player.pos.y -= 1, |
||||
Key::Char('q') => break, |
||||
_ => {} |
||||
}; |
||||
|
||||
gol.flush(); |
||||
} |
||||
gol.write(&termion::cursor::Show); |
||||
} |
||||
|
||||
fn update(game: &GOL) {} |
||||
|
||||
fn draw(game: &mut GOL) { |
||||
game.write(&clear::All); |
||||
let size = termion::terminal_size().unwrap(); |
||||
let x = size.0 / 2; |
||||
let y = size.1 / 2; |
||||
log(x); |
||||
log(y); |
||||
game.write(&cursor::Goto(size.0 / 2, size.1 / 2)); |
||||
game.write(&'X'); |
||||
game.flush(); |
||||
} |
Loading…
Reference in new issue