forked from CleanCut/invaders
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Invaders Part 2: Rendering & Multithreading
- Loading branch information
Showing
4 changed files
with
108 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use crate::{NUM_COLS, NUM_ROWS}; | ||
|
||
pub type Frame = Vec<Vec<&'static str>>; | ||
|
||
pub fn new_frame() -> Frame { | ||
let mut cols = Vec::with_capacity(NUM_COLS); | ||
for _ in 0..NUM_COLS { | ||
let mut col = Vec::with_capacity(NUM_ROWS); | ||
for _ in 0..NUM_ROWS { | ||
col.push(" "); | ||
} | ||
cols.push(col); | ||
} | ||
cols | ||
} | ||
|
||
pub trait Drawable { | ||
fn draw(&self, frame: &mut Frame); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub mod frame; | ||
pub mod render; | ||
|
||
pub const NUM_ROWS: usize = 20; | ||
pub const NUM_COLS: usize = 40; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use crate::frame::Frame; | ||
use crossterm::cursor::MoveTo; | ||
use crossterm::style::{Color, SetBackgroundColor}; | ||
use crossterm::terminal::{Clear, ClearType}; | ||
use crossterm::QueueableCommand; | ||
use std::io::{Stdout, Write}; | ||
|
||
pub fn render(stdout: &mut Stdout, last_frame: &Frame, curr_frame: &Frame, force: bool) { | ||
if force { | ||
stdout.queue(SetBackgroundColor(Color::Blue)).unwrap(); | ||
stdout.queue(Clear(ClearType::All)).unwrap(); | ||
stdout.queue(SetBackgroundColor(Color::Black)).unwrap(); | ||
} | ||
for (x, col) in curr_frame.iter().enumerate() { | ||
for (y, s) in col.iter().enumerate() { | ||
if *s != last_frame[x][y] || force { | ||
stdout.queue(MoveTo(x as u16, y as u16)).unwrap(); | ||
print!("{}", *s); | ||
} | ||
} | ||
} | ||
stdout.flush().unwrap(); | ||
} |