pub struct View {
pub width: usize,
pub height: usize,
pub background_char: ColChar,
pub wrapping_mode: WrappingMode,
pub block_until_resized: bool,
/* private fields */
}Expand description
The View struct implements Canvas, and can be used to draw to stdout. In normal use, you would clear the View, draw all your CanDraws implementing elements to it and then render to stdout with View::display_render. The following example demonstrates a piece of code that will render a View of width 9 and height 3, with a single Pixel in the middle
use gemini_engine::{view::{WrappingMode, View}, core::{ColChar, Vec2D}, primitives::Pixel};
let mut view = View::new(9, 3, ColChar::BACKGROUND)
.with_wrapping_mode(WrappingMode::Panic);
let pixel = Pixel::new(view.center(), ColChar::SOLID);
view.draw(&pixel);
view.display_render().unwrap();Fields§
§width: usizeThe width of the View. If modified, the View should be cleared to account for the new size
height: usizeThe height of the View. If modified, the View should be cleared to account for the new size
background_char: ColCharThe character that the View will be filled with by default when View::clear is called
wrapping_mode: WrappingModeDetermine how to handle pixels that are plotted outside the View
block_until_resized: boolIf true, View::display_render will block until the console window is resized to fit the View
Implementations§
Source§impl View
impl View
Sourcepub fn new(width: usize, height: usize, background_char: ColChar) -> Self
pub fn new(width: usize, height: usize, background_char: ColChar) -> Self
Create a new View
Examples found in repository?
More examples
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
fn main() {
let mut view = View::new(40, 8, ColChar::BACKGROUND).with_wrapping_mode(WrappingMode::Wrap);
let mut pixel = Pixel::new(Vec2D::new(10, 5), ColChar::SOLID);
loop {
view.clear();
pixel.pos.x += 1;
view.draw(&pixel);
let _ = view.display_render();
let _ = gameloop::sleep_fps(FPS, None);
}
}13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
fn main() {
let mut view = View::new(100, 45, ColChar::EMPTY);
let mut viewport = Viewport::new(
Transform3D::look_at_lh(Vec3D::new(0.0, -3.0, 6.0), Vec3D::ZERO, Vec3D::Y),
FOV,
view.center(),
);
viewport.display_mode = DisplayMode::Illuminated {
lights: vec![
Light::new_ambient(0.3),
Light::new_directional(0.7, Vec3D::new(1.0, 1.0, 1.0)),
],
};
viewport.objects.push(Mesh3D::torus(1.8, 1.0, 32, 16));
fps_gameloop!(
{
let donut_tr = &mut viewport.objects[0].transform;
*donut_tr = donut_tr.mul_mat4(&Transform3D::from_rotation_y(-0.03));
*donut_tr = donut_tr.mul_mat4(&Transform3D::from_rotation_x(0.03));
},
{
view.clear();
view.draw(&viewport);
let _ = view.display_render();
},
FPS
);
}14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
fn main() {
let mut view = View::new(100, 50, ColChar::EMPTY);
let mut viewport = Viewport::new(
Transform3D::look_at_lh(Vec3D::new(0.0, -1.5, 4.3), Vec3D::ZERO, Vec3D::Y),
FOV,
view.center(),
);
viewport.objects.push(Mesh3D::default_cube());
viewport.display_mode = DisplayMode::Illuminated {
lights: vec![
Light::new_ambient(0.3),
Light::new_directional(0.6, Vec3D::new(0.5, 1.0, 1.0)),
],
};
fps_gameloop!(
{
viewport.objects[0].transform = viewport.objects[0]
.transform
.mul_mat4(&Transform3D::from_rotation_y(-0.05));
},
{
view.clear();
view.draw(&viewport);
let _ = view.display_render();
},
FPS,
|elapsed: Duration, frame_skip| {
println!(
"Elapsed: {:.2?}µs | Frame skip: {}",
elapsed.as_micros(),
frame_skip
);
}
);
}15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn main() {
let mut view = View::new(50, 12, ColChar::BACKGROUND).with_wrapping_mode(WrappingMode::Wrap);
let mut blocks = vec![
Rect::new(Vec2D::new(0, 0), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 2), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 4), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 6), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 8), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 10), BLOCK_SIZE, FILL_CHAR),
];
let mut i = 0;
fps_gameloop!(
{
i += 1;
for (j, block) in (0u32..).zip(blocks.iter_mut()) {
if i % 2_u32.pow(j) == 0 {
block.pos.x += 1;
}
}
},
{
view.clear();
for block in &blocks {
view.draw(block);
}
let _ = view.display_render();
if blocks.iter().all(|b| b.pos.x % view.width as i64 == 0) {
thread::sleep(Duration::from_secs(2));
};
},
60.0
);
}15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
fn main() {
let mut view = View::new(60, 10, BACKGROUND_CHAR);
let mut pixel = Pixel::new(Vec2D::new(5, 9), FILL_CHAR);
let mut line = Line::new(Vec2D::new(2, 8), Vec2D::new(28, 7), FILL_CHAR);
let mut line1_direction = -1;
let rect = Rect::new(
Vec2D { x: 11, y: 1 },
Vec2D { x: 9, y: 3 },
ColChar::SOLID.with_rgb(200, 30, 0),
);
let test_image = r"
______
/|_||_\`.__
( _ _ _\
=`-(_)--(_)-' ";
let mut sprite = Sprite::new(
Vec2D::new(30, 1),
test_image,
Modifier::from_rgb(20, 200, 0),
);
let mut draw_elapsed = Duration::default();
let mut render_elapsed = Duration::default();
fps_gameloop!(
{
pixel.pos.x += 2;
// loop the position back to the other side. This can be done with `WrappingMode::Wrap` but it won't change the element's actual position, so the pixel position being printed would continue to increase without looping
pixel.pos %= view.size();
line.pos1.y += line1_direction;
line.pos0.y = 10 - line.pos1.y;
if line.pos1.y > 7 {
line1_direction = -1;
} else if line.pos1.y < 3 {
line1_direction = 1;
}
sprite.pos.x += 1;
},
{
view.clear();
let now = Instant::now();
view.wrapping_mode = WrappingMode::Panic;
view.draw(&pixel);
view.draw(&line);
view.draw(&rect);
view.wrapping_mode = WrappingMode::Wrap;
view.draw(&sprite);
draw_elapsed = now.elapsed();
let now = Instant::now();
let _ = view.display_render();
render_elapsed = now.elapsed();
},
FPS,
|total_elapsed: Duration, _frame_skip| {
println!(
"Drawing: {:.2?} microseconds | Rendering: {:.2?} microseconds| Total: {:.2?}",
draw_elapsed.as_micros(),
render_elapsed.as_micros(),
total_elapsed.as_micros()
);
println!("Pixel position: {}", pixel.pos);
}
);
}Sourcepub const fn with_wrapping_mode(self, wrapping_mode: WrappingMode) -> Self
pub const fn with_wrapping_mode(self, wrapping_mode: WrappingMode) -> Self
Return the View with an updated wrapping_mode property. Consumes the original View
§Example
let mut view = View::new(20, 7, ColChar::BACKGROUND)
.with_wrapping_mode(WrappingMode::Wrap);
// The pixel will be wrapped and drawn at `(0, 4)`
view.plot(Vec2D::new(20,4), ColChar::SOLID);Examples found in repository?
More examples
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
fn main() {
let mut view = View::new(40, 8, ColChar::BACKGROUND).with_wrapping_mode(WrappingMode::Wrap);
let mut pixel = Pixel::new(Vec2D::new(10, 5), ColChar::SOLID);
loop {
view.clear();
pixel.pos.x += 1;
view.draw(&pixel);
let _ = view.display_render();
let _ = gameloop::sleep_fps(FPS, None);
}
}15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn main() {
let mut view = View::new(50, 12, ColChar::BACKGROUND).with_wrapping_mode(WrappingMode::Wrap);
let mut blocks = vec![
Rect::new(Vec2D::new(0, 0), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 2), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 4), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 6), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 8), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 10), BLOCK_SIZE, FILL_CHAR),
];
let mut i = 0;
fps_gameloop!(
{
i += 1;
for (j, block) in (0u32..).zip(blocks.iter_mut()) {
if i % 2_u32.pow(j) == 0 {
block.pos.x += 1;
}
}
},
{
view.clear();
for block in &blocks {
view.draw(block);
}
let _ = view.display_render();
if blocks.iter().all(|b| b.pos.x % view.width as i64 == 0) {
thread::sleep(Duration::from_secs(2));
};
},
60.0
);
}Sourcepub const fn with_block_until_resized(self) -> Self
pub const fn with_block_until_resized(self) -> Self
Return the View with an updated block_until_resized property. Consumes the original View
§Example
let mut view = View::new(20, 7, ColChar::BACKGROUND)
.with_block_until_resized();
// If the terminal size is smaller than (20, 7), this will wait until the terminal has been resized
view.display_render().unwrap();Sourcepub const fn size(&self) -> Vec2D
pub const fn size(&self) -> Vec2D
Return the width and height of the View as a Vec2D
Examples found in repository?
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
fn main() {
let mut view = View::new(60, 10, BACKGROUND_CHAR);
let mut pixel = Pixel::new(Vec2D::new(5, 9), FILL_CHAR);
let mut line = Line::new(Vec2D::new(2, 8), Vec2D::new(28, 7), FILL_CHAR);
let mut line1_direction = -1;
let rect = Rect::new(
Vec2D { x: 11, y: 1 },
Vec2D { x: 9, y: 3 },
ColChar::SOLID.with_rgb(200, 30, 0),
);
let test_image = r"
______
/|_||_\`.__
( _ _ _\
=`-(_)--(_)-' ";
let mut sprite = Sprite::new(
Vec2D::new(30, 1),
test_image,
Modifier::from_rgb(20, 200, 0),
);
let mut draw_elapsed = Duration::default();
let mut render_elapsed = Duration::default();
fps_gameloop!(
{
pixel.pos.x += 2;
// loop the position back to the other side. This can be done with `WrappingMode::Wrap` but it won't change the element's actual position, so the pixel position being printed would continue to increase without looping
pixel.pos %= view.size();
line.pos1.y += line1_direction;
line.pos0.y = 10 - line.pos1.y;
if line.pos1.y > 7 {
line1_direction = -1;
} else if line.pos1.y < 3 {
line1_direction = 1;
}
sprite.pos.x += 1;
},
{
view.clear();
let now = Instant::now();
view.wrapping_mode = WrappingMode::Panic;
view.draw(&pixel);
view.draw(&line);
view.draw(&rect);
view.wrapping_mode = WrappingMode::Wrap;
view.draw(&sprite);
draw_elapsed = now.elapsed();
let now = Instant::now();
let _ = view.display_render();
render_elapsed = now.elapsed();
},
FPS,
|total_elapsed: Duration, _frame_skip| {
println!(
"Drawing: {:.2?} microseconds | Rendering: {:.2?} microseconds| Total: {:.2?}",
draw_elapsed.as_micros(),
render_elapsed.as_micros(),
total_elapsed.as_micros()
);
println!("Pixel position: {}", pixel.pos);
}
);
}Sourcepub fn center(&self) -> Vec2D
pub fn center(&self) -> Vec2D
Return Vec2D coordinates of the centre of the View
Examples found in repository?
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
fn main() {
let mut view = View::new(100, 45, ColChar::EMPTY);
let mut viewport = Viewport::new(
Transform3D::look_at_lh(Vec3D::new(0.0, -3.0, 6.0), Vec3D::ZERO, Vec3D::Y),
FOV,
view.center(),
);
viewport.display_mode = DisplayMode::Illuminated {
lights: vec![
Light::new_ambient(0.3),
Light::new_directional(0.7, Vec3D::new(1.0, 1.0, 1.0)),
],
};
viewport.objects.push(Mesh3D::torus(1.8, 1.0, 32, 16));
fps_gameloop!(
{
let donut_tr = &mut viewport.objects[0].transform;
*donut_tr = donut_tr.mul_mat4(&Transform3D::from_rotation_y(-0.03));
*donut_tr = donut_tr.mul_mat4(&Transform3D::from_rotation_x(0.03));
},
{
view.clear();
view.draw(&viewport);
let _ = view.display_render();
},
FPS
);
}More examples
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
fn main() {
let mut view = View::new(100, 50, ColChar::EMPTY);
let mut viewport = Viewport::new(
Transform3D::look_at_lh(Vec3D::new(0.0, -1.5, 4.3), Vec3D::ZERO, Vec3D::Y),
FOV,
view.center(),
);
viewport.objects.push(Mesh3D::default_cube());
viewport.display_mode = DisplayMode::Illuminated {
lights: vec![
Light::new_ambient(0.3),
Light::new_directional(0.6, Vec3D::new(0.5, 1.0, 1.0)),
],
};
fps_gameloop!(
{
viewport.objects[0].transform = viewport.objects[0]
.transform
.mul_mat4(&Transform3D::from_rotation_y(-0.05));
},
{
view.clear();
view.draw(&viewport);
let _ = view.display_render();
},
FPS,
|elapsed: Duration, frame_skip| {
println!(
"Elapsed: {:.2?}µs | Frame skip: {}",
elapsed.as_micros(),
frame_skip
);
}
);
}Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear the View of all pixels, overwriting them all with the set background_char
Examples found in repository?
More examples
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
fn main() {
let mut view = View::new(40, 8, ColChar::BACKGROUND).with_wrapping_mode(WrappingMode::Wrap);
let mut pixel = Pixel::new(Vec2D::new(10, 5), ColChar::SOLID);
loop {
view.clear();
pixel.pos.x += 1;
view.draw(&pixel);
let _ = view.display_render();
let _ = gameloop::sleep_fps(FPS, None);
}
}13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
fn main() {
let mut view = View::new(100, 45, ColChar::EMPTY);
let mut viewport = Viewport::new(
Transform3D::look_at_lh(Vec3D::new(0.0, -3.0, 6.0), Vec3D::ZERO, Vec3D::Y),
FOV,
view.center(),
);
viewport.display_mode = DisplayMode::Illuminated {
lights: vec![
Light::new_ambient(0.3),
Light::new_directional(0.7, Vec3D::new(1.0, 1.0, 1.0)),
],
};
viewport.objects.push(Mesh3D::torus(1.8, 1.0, 32, 16));
fps_gameloop!(
{
let donut_tr = &mut viewport.objects[0].transform;
*donut_tr = donut_tr.mul_mat4(&Transform3D::from_rotation_y(-0.03));
*donut_tr = donut_tr.mul_mat4(&Transform3D::from_rotation_x(0.03));
},
{
view.clear();
view.draw(&viewport);
let _ = view.display_render();
},
FPS
);
}14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
fn main() {
let mut view = View::new(100, 50, ColChar::EMPTY);
let mut viewport = Viewport::new(
Transform3D::look_at_lh(Vec3D::new(0.0, -1.5, 4.3), Vec3D::ZERO, Vec3D::Y),
FOV,
view.center(),
);
viewport.objects.push(Mesh3D::default_cube());
viewport.display_mode = DisplayMode::Illuminated {
lights: vec![
Light::new_ambient(0.3),
Light::new_directional(0.6, Vec3D::new(0.5, 1.0, 1.0)),
],
};
fps_gameloop!(
{
viewport.objects[0].transform = viewport.objects[0]
.transform
.mul_mat4(&Transform3D::from_rotation_y(-0.05));
},
{
view.clear();
view.draw(&viewport);
let _ = view.display_render();
},
FPS,
|elapsed: Duration, frame_skip| {
println!(
"Elapsed: {:.2?}µs | Frame skip: {}",
elapsed.as_micros(),
frame_skip
);
}
);
}15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn main() {
let mut view = View::new(50, 12, ColChar::BACKGROUND).with_wrapping_mode(WrappingMode::Wrap);
let mut blocks = vec![
Rect::new(Vec2D::new(0, 0), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 2), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 4), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 6), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 8), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 10), BLOCK_SIZE, FILL_CHAR),
];
let mut i = 0;
fps_gameloop!(
{
i += 1;
for (j, block) in (0u32..).zip(blocks.iter_mut()) {
if i % 2_u32.pow(j) == 0 {
block.pos.x += 1;
}
}
},
{
view.clear();
for block in &blocks {
view.draw(block);
}
let _ = view.display_render();
if blocks.iter().all(|b| b.pos.x % view.width as i64 == 0) {
thread::sleep(Duration::from_secs(2));
};
},
60.0
);
}15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
fn main() {
let mut view = View::new(60, 10, BACKGROUND_CHAR);
let mut pixel = Pixel::new(Vec2D::new(5, 9), FILL_CHAR);
let mut line = Line::new(Vec2D::new(2, 8), Vec2D::new(28, 7), FILL_CHAR);
let mut line1_direction = -1;
let rect = Rect::new(
Vec2D { x: 11, y: 1 },
Vec2D { x: 9, y: 3 },
ColChar::SOLID.with_rgb(200, 30, 0),
);
let test_image = r"
______
/|_||_\`.__
( _ _ _\
=`-(_)--(_)-' ";
let mut sprite = Sprite::new(
Vec2D::new(30, 1),
test_image,
Modifier::from_rgb(20, 200, 0),
);
let mut draw_elapsed = Duration::default();
let mut render_elapsed = Duration::default();
fps_gameloop!(
{
pixel.pos.x += 2;
// loop the position back to the other side. This can be done with `WrappingMode::Wrap` but it won't change the element's actual position, so the pixel position being printed would continue to increase without looping
pixel.pos %= view.size();
line.pos1.y += line1_direction;
line.pos0.y = 10 - line.pos1.y;
if line.pos1.y > 7 {
line1_direction = -1;
} else if line.pos1.y < 3 {
line1_direction = 1;
}
sprite.pos.x += 1;
},
{
view.clear();
let now = Instant::now();
view.wrapping_mode = WrappingMode::Panic;
view.draw(&pixel);
view.draw(&line);
view.draw(&rect);
view.wrapping_mode = WrappingMode::Wrap;
view.draw(&sprite);
draw_elapsed = now.elapsed();
let now = Instant::now();
let _ = view.display_render();
render_elapsed = now.elapsed();
},
FPS,
|total_elapsed: Duration, _frame_skip| {
println!(
"Drawing: {:.2?} microseconds | Rendering: {:.2?} microseconds| Total: {:.2?}",
draw_elapsed.as_micros(),
render_elapsed.as_micros(),
total_elapsed.as_micros()
);
println!("Pixel position: {}", pixel.pos);
}
);
}Sourcepub fn draw(&mut self, element: &impl CanDraw)
pub fn draw(&mut self, element: &impl CanDraw)
Draw a struct implementing CanDraw to the View
Examples found in repository?
More examples
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
fn main() {
let mut view = View::new(40, 8, ColChar::BACKGROUND).with_wrapping_mode(WrappingMode::Wrap);
let mut pixel = Pixel::new(Vec2D::new(10, 5), ColChar::SOLID);
loop {
view.clear();
pixel.pos.x += 1;
view.draw(&pixel);
let _ = view.display_render();
let _ = gameloop::sleep_fps(FPS, None);
}
}16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
fn main() {
let mut scale_view = ScaleFitView::new(ColChar::BACKGROUND);
let mut text = Text::new(Vec2D::ZERO, "This is some centered text!", Modifier::None)
.with_align(TextAlign::Centered);
let mut sprite =
Sprite::new(Vec2D::ZERO, TEXTURE, Modifier::None).with_align(TextAlign2D::CENTERED);
loop {
text.pos = scale_view.intended_size() / 2;
sprite.pos = scale_view.intended_size() / 2;
sprite.pos.y -= 5;
scale_view.update();
scale_view.view.draw(&text);
scale_view.view.draw(&sprite);
let _ = scale_view.view.display_render();
sleep(Duration::from_millis(10));
}
}13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
fn main() {
let mut view = View::new(100, 45, ColChar::EMPTY);
let mut viewport = Viewport::new(
Transform3D::look_at_lh(Vec3D::new(0.0, -3.0, 6.0), Vec3D::ZERO, Vec3D::Y),
FOV,
view.center(),
);
viewport.display_mode = DisplayMode::Illuminated {
lights: vec![
Light::new_ambient(0.3),
Light::new_directional(0.7, Vec3D::new(1.0, 1.0, 1.0)),
],
};
viewport.objects.push(Mesh3D::torus(1.8, 1.0, 32, 16));
fps_gameloop!(
{
let donut_tr = &mut viewport.objects[0].transform;
*donut_tr = donut_tr.mul_mat4(&Transform3D::from_rotation_y(-0.03));
*donut_tr = donut_tr.mul_mat4(&Transform3D::from_rotation_x(0.03));
},
{
view.clear();
view.draw(&viewport);
let _ = view.display_render();
},
FPS
);
}14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
fn main() {
let mut view = View::new(100, 50, ColChar::EMPTY);
let mut viewport = Viewport::new(
Transform3D::look_at_lh(Vec3D::new(0.0, -1.5, 4.3), Vec3D::ZERO, Vec3D::Y),
FOV,
view.center(),
);
viewport.objects.push(Mesh3D::default_cube());
viewport.display_mode = DisplayMode::Illuminated {
lights: vec![
Light::new_ambient(0.3),
Light::new_directional(0.6, Vec3D::new(0.5, 1.0, 1.0)),
],
};
fps_gameloop!(
{
viewport.objects[0].transform = viewport.objects[0]
.transform
.mul_mat4(&Transform3D::from_rotation_y(-0.05));
},
{
view.clear();
view.draw(&viewport);
let _ = view.display_render();
},
FPS,
|elapsed: Duration, frame_skip| {
println!(
"Elapsed: {:.2?}µs | Frame skip: {}",
elapsed.as_micros(),
frame_skip
);
}
);
}15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn main() {
let mut view = View::new(50, 12, ColChar::BACKGROUND).with_wrapping_mode(WrappingMode::Wrap);
let mut blocks = vec![
Rect::new(Vec2D::new(0, 0), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 2), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 4), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 6), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 8), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 10), BLOCK_SIZE, FILL_CHAR),
];
let mut i = 0;
fps_gameloop!(
{
i += 1;
for (j, block) in (0u32..).zip(blocks.iter_mut()) {
if i % 2_u32.pow(j) == 0 {
block.pos.x += 1;
}
}
},
{
view.clear();
for block in &blocks {
view.draw(block);
}
let _ = view.display_render();
if blocks.iter().all(|b| b.pos.x % view.width as i64 == 0) {
thread::sleep(Duration::from_secs(2));
};
},
60.0
);
}Sourcepub fn draw_double_width(&mut self, element: &impl CanDraw)
pub fn draw_double_width(&mut self, element: &impl CanDraw)
Draw a struct implementing CanDraw to the View with a doubled width. Drawing a Pixel at Vec2D(5,3), for example, will result in pixels at at Vec2D(10,3) and Vec2D(11,3) being plotted to. Useful when you want to work with more square pixels, as single text characters are much taller than they are wide
Sourcepub fn display_render(&self) -> Result<()>
pub fn display_render(&self) -> Result<()>
Display the View. View implements the Display trait and so can be rendered in many ways (such as println!("{view}");), but this is intended to be the fastest way possible.
§Errors
Returns the Result from writing to io::stdout().lock(). You can simply ignore it with let _ = or .unwrap() most of the time
Examples found in repository?
More examples
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
fn main() {
let mut view = View::new(40, 8, ColChar::BACKGROUND).with_wrapping_mode(WrappingMode::Wrap);
let mut pixel = Pixel::new(Vec2D::new(10, 5), ColChar::SOLID);
loop {
view.clear();
pixel.pos.x += 1;
view.draw(&pixel);
let _ = view.display_render();
let _ = gameloop::sleep_fps(FPS, None);
}
}16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
fn main() {
let mut scale_view = ScaleFitView::new(ColChar::BACKGROUND);
let mut text = Text::new(Vec2D::ZERO, "This is some centered text!", Modifier::None)
.with_align(TextAlign::Centered);
let mut sprite =
Sprite::new(Vec2D::ZERO, TEXTURE, Modifier::None).with_align(TextAlign2D::CENTERED);
loop {
text.pos = scale_view.intended_size() / 2;
sprite.pos = scale_view.intended_size() / 2;
sprite.pos.y -= 5;
scale_view.update();
scale_view.view.draw(&text);
scale_view.view.draw(&sprite);
let _ = scale_view.view.display_render();
sleep(Duration::from_millis(10));
}
}13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
fn main() {
let mut view = View::new(100, 45, ColChar::EMPTY);
let mut viewport = Viewport::new(
Transform3D::look_at_lh(Vec3D::new(0.0, -3.0, 6.0), Vec3D::ZERO, Vec3D::Y),
FOV,
view.center(),
);
viewport.display_mode = DisplayMode::Illuminated {
lights: vec![
Light::new_ambient(0.3),
Light::new_directional(0.7, Vec3D::new(1.0, 1.0, 1.0)),
],
};
viewport.objects.push(Mesh3D::torus(1.8, 1.0, 32, 16));
fps_gameloop!(
{
let donut_tr = &mut viewport.objects[0].transform;
*donut_tr = donut_tr.mul_mat4(&Transform3D::from_rotation_y(-0.03));
*donut_tr = donut_tr.mul_mat4(&Transform3D::from_rotation_x(0.03));
},
{
view.clear();
view.draw(&viewport);
let _ = view.display_render();
},
FPS
);
}14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
fn main() {
let mut view = View::new(100, 50, ColChar::EMPTY);
let mut viewport = Viewport::new(
Transform3D::look_at_lh(Vec3D::new(0.0, -1.5, 4.3), Vec3D::ZERO, Vec3D::Y),
FOV,
view.center(),
);
viewport.objects.push(Mesh3D::default_cube());
viewport.display_mode = DisplayMode::Illuminated {
lights: vec![
Light::new_ambient(0.3),
Light::new_directional(0.6, Vec3D::new(0.5, 1.0, 1.0)),
],
};
fps_gameloop!(
{
viewport.objects[0].transform = viewport.objects[0]
.transform
.mul_mat4(&Transform3D::from_rotation_y(-0.05));
},
{
view.clear();
view.draw(&viewport);
let _ = view.display_render();
},
FPS,
|elapsed: Duration, frame_skip| {
println!(
"Elapsed: {:.2?}µs | Frame skip: {}",
elapsed.as_micros(),
frame_skip
);
}
);
}15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn main() {
let mut view = View::new(50, 12, ColChar::BACKGROUND).with_wrapping_mode(WrappingMode::Wrap);
let mut blocks = vec![
Rect::new(Vec2D::new(0, 0), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 2), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 4), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 6), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 8), BLOCK_SIZE, FILL_CHAR),
Rect::new(Vec2D::new(0, 10), BLOCK_SIZE, FILL_CHAR),
];
let mut i = 0;
fps_gameloop!(
{
i += 1;
for (j, block) in (0u32..).zip(blocks.iter_mut()) {
if i % 2_u32.pow(j) == 0 {
block.pos.x += 1;
}
}
},
{
view.clear();
for block in &blocks {
view.draw(block);
}
let _ = view.display_render();
if blocks.iter().all(|b| b.pos.x % view.width as i64 == 0) {
thread::sleep(Duration::from_secs(2));
};
},
60.0
);
}