#engine #gamedev #game-engine #game

thin-engine

一个介于 glium、winit、glium-types 和 winit-input-map 之间的轻量级引擎,包含一些额外的辅助函数。

5 个版本

0.1.4 2024 年 7 月 1 日
0.1.3 2024 年 6 月 23 日
0.1.2 2024 年 6 月 23 日
0.1.1 2024 年 6 月 23 日
0.1.0 2024 年 5 月 11 日

490游戏开发

Download history 128/week @ 2024-05-11 2/week @ 2024-05-18 347/week @ 2024-06-22 232/week @ 2024-06-29 6/week @ 2024-07-06 12/week @ 2024-07-27

每月 72 次下载

MIT OR Apache-2.0 OR CC-BY-4.0

20KB
287

一个轻量级游戏引擎(因此得名)。使用 glium 进行绘图,使用 glium-types 进行游戏变量,使用 winit 进行窗口管理,使用 winit-input-map 进行输入。它具有简单的 fxaa 支持和低样板代码,尽管拥有很多控制功能。

use thin_engine::{prelude::*, meshes::screen};
use Action::*;

#[derive(ToUsize)]
enum Action {
    Left,
    Right,
    Jump,
    Exit
}
let (event_loop, window, display) = thin_engine::set_up().unwrap();
let mut input = input_map!(
    (Left,  KeyCode::KeyA, MouseButton::Left, KeyCode::ArrowLeft),
    (Right, KeyCode::KeyD, MouseButton::Right, KeyCode::ArrowRight),
    (Jump,  KeyCode::KeyW, KeyCode::ArrowUp, KeyCode::Space),
    (Exit,  KeyCode::Escape)
);
let (box_indices, box_verts) = mesh!(
    &display, &screen::INDICES, &screen::VERTICES
);
let box_shader = Program::from_source(
&display, shaders::VERTEX, 
"#version 140
out vec4 colour;
void main() {
    colour = vec4(1.0, 0.0, 0.0, 1.0);
}", None).unwrap();

let mut player_pos = Vec2::ZERO;
let mut player_gravity = 0.0;
let mut player_can_jump = true;
// camera matrix must be inverse
let camera = Mat4::from_scale(Vec3::splat(10.0)).inverse();

// target of 60 fps
let target_delay = Duration::from_secs_f32(1.0/60.0);
let mut delta_time = 0.016; // change in time between frames

thin_engine::run(event_loop, &mut input, |input, target| {
    let frame_start = Instant::now();
    // set up frame
    let size = window.inner_size().into();
    display.resize(size);
    let mut frame = display.draw();
    let view2d = Mat4::view_matrix_2d(size);
    
    // game logic
    player_pos.x += input.axis(Right, Left) * 10.0 * delta_time;
    player_gravity += delta_time * 50.0;
    player_pos.y -= player_gravity * delta_time;
    if player_pos.y < 0.0 {
        player_pos.y = 0.0;
        player_can_jump = true;
    }
    if player_can_jump && input.pressed(Jump) {
        player_gravity = -20.0;
        player_can_jump = false;
    }
    // draw
    frame.clear_color(0.0, 0.0, 0.0, 1.0);
    frame.draw(
        &box_verts, &box_indices, &box_shader, &uniform! {
            view: view2d, camera: camera,
            model: Mat4::from_pos(player_pos.extend(0.0)),
        }, &DrawParameters::default()
    );
    
    frame.finish().unwrap();
    if input.pressed(Exit) { target.exit() }
    thread::sleep(target_delay.saturating_sub(frame_start.elapse()));
    delta_time = frame_start.elapsed().as_secs_f32();
}).unwrap();

依赖项

~7–25MB
~344K SLoC