Created
January 10, 2016 06:42
-
-
Save niconii/a5ae9522b6ed19397ab1 to your computer and use it in GitHub Desktop.
Messing around with an idea mentioned in https://news.ycombinator.com/item?id=10873525
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate sdl2; | |
use sdl2::pixels::Color; | |
use sdl2::event::Event; | |
use sdl2::keyboard::Keycode; | |
use std::sync::mpsc::{Receiver, Sender}; | |
enum Command { | |
SetBG(u8, u8, u8), | |
Quit | |
} | |
type GameProgram = fn(&Receiver<&[u8]>, &Sender<Command>) -> Option<u8>; | |
static PROGRAMS: &'static [GameProgram] = &[red, green, blue]; | |
macro_rules! quit_if_fail { | |
($e:expr) => ( | |
match $e { | |
Ok(_) => { }, | |
Err(_) => { | |
println!("Lost connection to console!"); | |
return None | |
} | |
} | |
); | |
} | |
fn red(input: &Receiver<&[u8]>, output: &Sender<Command>) -> Option<u8> { | |
quit_if_fail!(output.send(Command::SetBG(255, 0, 0))); | |
std::thread::sleep_ms(2000); | |
Some(1) | |
} | |
fn green(input: &Receiver<&[u8]>, output: &Sender<Command>) -> Option<u8> { | |
quit_if_fail!(output.send(Command::SetBG(0, 255, 0))); | |
std::thread::sleep_ms(2000); | |
Some(2) | |
} | |
fn blue(input: &Receiver<&[u8]>, output: &Sender<Command>) -> Option<u8> { | |
quit_if_fail!(output.send(Command::SetBG(0, 0, 255))); | |
std::thread::sleep_ms(2000); | |
None | |
} | |
fn main() { | |
let (cmds_tx, cmds_rx) = std::sync::mpsc::channel(); | |
let (sram_tx, sram_rx) = std::sync::mpsc::channel(); | |
let console_thread = std::thread::spawn(move || console(cmds_rx, sram_tx)); | |
let mut program = 0; | |
loop { | |
match (PROGRAMS[program as usize])(&sram_rx, &cmds_tx) { | |
Some(p) => program = p, | |
None => break | |
} | |
} | |
let _ = cmds_tx.send(Command::Quit); | |
console_thread.join(); | |
} | |
fn console(input: Receiver<Command>, output: Sender<&[u8]>) { | |
let sdl_context = sdl2::init().unwrap(); | |
let video_subsystem = sdl_context.video().unwrap(); | |
let window = video_subsystem.window("bank switch video", 640, 400) | |
.position_centered() | |
.opengl() | |
.build() | |
.unwrap(); | |
let mut renderer = window.renderer().build().unwrap(); | |
renderer.set_draw_color(Color::RGB(0, 0, 0)); | |
renderer.clear(); | |
renderer.present(); | |
let mut event_pump = sdl_context.event_pump().unwrap(); | |
'running: loop { | |
for event in event_pump.poll_iter() { | |
match event { | |
Event::Quit {..} | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => { | |
break 'running | |
}, | |
_ => { } | |
} | |
} | |
loop { | |
let command = match input.try_recv() { | |
Ok(c) => c, | |
Err(_) => break | |
}; | |
match command { | |
Command::SetBG(r, g, b) => { | |
renderer.set_draw_color(Color::RGB(r, g, b)); | |
renderer.clear(); | |
renderer.present(); | |
}, | |
Command::Quit => { | |
break 'running | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment