extern crate cgui;
#[macro_use]
extern crate scan_fmt;
use cgui::{RenderBack,CWin};
use std::ffi::{CStr};
fn main() {
let mut w = CWin::new();
let square_size = 5;
let mut x = 15;
let mut y = 15;
w.init();
loop {
let evt_string = w.event_tick();
if evt_string.starts_with("WinClose") {
println!("Quitting!");
break;
}
else if evt_string.starts_with("KeyPress,") {
let pressed_key = scan_fmt!(&evt_string, "KeyPress,{}", String);
match pressed_key {
None => { continue; } _ => { }
}
let pressed_key = pressed_key.unwrap();
if pressed_key == "q" || pressed_key == "Q" {
println!("Quitting!");
break;
}
if pressed_key == "w" || pressed_key == "a" || pressed_key == "s" || pressed_key == "d" {
write_square(&mut w, x as u32, y as u32, square_size as u32, [0, 0, 255]);
}
if pressed_key == "w" {
if y - square_size > 0 {
y -= square_size;
}
}
else if pressed_key == "a" {
if x - square_size > 0 {
x -= square_size;
}
}
else if pressed_key == "s" {
if y + square_size < w.rep.height {
y += square_size;
}
}
else if pressed_key == "d" {
if x + square_size < w.rep.width {
x += square_size;
}
}
if pressed_key == "w" || pressed_key == "a" || pressed_key == "s" || pressed_key == "d" {
write_square(&mut w, x as u32, y as u32, square_size as u32, [255, 0, 0]);
w.redraw_dirty();
}
if y > 50 {
write_msg(&mut w);
w.redraw_dirty();
}
}
else if evt_string.starts_with("WinShown") {
for x in 0..w.rep.width {
for y in 0..w.rep.height {
w.write_px(x as u32, y as u32, [255, 255, 255]); }
}
write_square(&mut w, x as u32, y as u32, square_size as u32, [0, 0, 0]);
w.redraw_dirty();
}
else {
println!("Got unknown evt_string: {}", evt_string);
}
}
}
fn write_square(w: &mut CWin, x: u32, y: u32, square_size: u32, pixel: [u8; 3]) {
write_rect(w, x, y, x+square_size, y+square_size, pixel);
}
fn write_rect(w: &mut CWin, x1: u32, y1: u32, x2: u32, y2: u32, pixel: [u8; 3]) {
for x in x1..x2 {
for y in y1..y2 {
w.write_px(x, y, pixel); }
}
}
fn write_msg(w: &mut CWin) {
{ write_rect(w, 5, 5, 6, 20, [0, 0, 0]);
write_rect(w, 5, 12, 15, 13, [0, 0, 0]);
write_rect(w, 15, 5, 16, 20, [0, 0, 0]);
}
{ write_rect(w, 20, 5, 30, 6, [0, 0, 0]);
write_rect(w, 24, 5, 25, 20, [0, 0, 0]);
write_rect(w, 20, 19, 30, 20, [0, 0, 0]);
}
}