0.3.0 2021年9月30日
0.2.0 2021年9月6日
0.1.1 2021年4月13日

#9 in #视口


用于 wonky

自定义许可

45KB
965

Florp引擎:基于终端的绘图/游戏库

支持视口、摄像头以及字符的通用绘图,甚至一些控件如 TextBorder

截图

Screenshot

示例

Examples


lib.rs:

终端游戏引擎

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) => {}
        }
    }
}

依赖

~1.6–2.4MB
~43K SLoC