0.3.0 Sep 30, 2021
0.2.0 Sep 6, 2021
0.1.1 Apr 13, 2021

#9 in #viewport

30 downloads per month
Used in wonky

Custom license

45KB
965 lines

Florp engine: a terminal based drawing / game library

Supports viewports, cameras, and general drawing of characters, and even some widgets like Text and Border.

Screenshot:

Screenshot

Example:

Examples


lib.rs:

Terminal game engine

use tinybit::events::{events, Event, KeyCode, KeyEvent, EventModel};
use tinybit::{
    term_size, Camera, Renderer, ScreenPos, ScreenSize, StdoutTarget, Viewport,
    WorldPos, WorldSize, Pixel
};

fn main() {
    let (width, height) = term_size().expect("Can't get the term size? Can't play the game!");

    // Viewport
    let viewport_size = ScreenSize::new(width / 2, height / 2);
    let mut viewport = Viewport::new(ScreenPos::new(0, 4), viewport_size);

    // Camera
    let (width, height) = (width as i64, height as i64);
    let camera_size = WorldSize::new(width / 2, height / 2);
    let camera_pos = WorldPos::new(width, height);
    let mut camera = Camera::new(camera_pos, camera_size);

    // Renderer
    let stdout_renderer = StdoutTarget::new().expect("Failed to enter raw mode");
    let mut renderer = Renderer::new(stdout_renderer);

    // Player
    let mut player = ('@', camera_pos);

    let (_tx, events) = events::<()> (EventModel::Fps(20)); 
    for event in events {
        match event {
            Event::User(()) => {}
            Event::Tick => {
                let pixel = Pixel::new(player.0, camera.to_screen(player.1), None, None);
                viewport.draw_pixel(pixel);
                let _ = renderer.render(&mut viewport);
                viewport.swap_buffers();
            }
            Event::Key(KeyEvent { code: KeyCode::Esc, ..  }) => break,
            Event::Key(KeyEvent { code: kc, .. }) => {
                match kc {
                    KeyCode::Left => { player.1.x -= 1; }
                    KeyCode::Right => { player.1.x += 1; }
                    KeyCode::Up => { player.1.y -= 1; }
                    KeyCode::Down => { player.1.y += 1; }
                    _ => {}
                }
            }
            Event::Resize(w, h) => {}
        }
    }
}

Dependencies

~1.6–2.3MB
~42K SLoC