apparatus

Crates.ioapparatus
lib.rsapparatus
version0.0.2
sourcesrc
created_at2022-02-13 15:43:52.848213
updated_at2022-02-21 21:21:27.146541
descriptionA 2D game engine
homepage
repositoryhttps://github.com/junglie85/apparatus
max_upload_size
id531798
size340,334
junglie85 (junglie85)

documentation

README

Apparatus Game Engine

Apparatus is a game engine that takes its inspiration from a number of sources including Dragonfly, olcPixelGameEngine and Arcade, amongst others.

Getting started

Implement the Game trait and tell the Apparatus to run your struct:

use anyhow::Result;
use log::info;

use apparatus::color::Color;
use apparatus::engine::apparatus::{Apparatus, ApparatusSettings};
use apparatus::engine::game::Game;
use apparatus::engine::key::Key;
use apparatus::errors::ApparatusError;
use apparatus::maths::clamp;

struct Example {
    color: Color,
}

impl Game for Example {
    fn on_create(app: &Apparatus) -> Result<Self, ApparatusError> {
        let r = app.screen_width() % 255;
        let g = app.screen_height() % 255;
        let b = (app.screen_width() * app.screen_height()) % 255;
        let color = Color::rgba(r as u8, g as u8, b as u8, 255);

        let game = Example { color };

        Ok(game)
    }

    fn on_update(&mut self, app: &mut Apparatus) {
        info!("updating");

        let dt = app.elapsed_time();

        if app.is_key_held(Key::Up) {
            let r = clamp(
                0.0,
                self.color.r() as f32 + (100.0 * dt.as_secs_f32()),
                255.0,
            ) as u8;
            let g = clamp(
                0.0,
                self.color.g() as f32 + (100.0 * dt.as_secs_f32()),
                255.0,
            ) as u8;
            let b = clamp(
                0.0,
                self.color.b() as f32 + (100.0 * dt.as_secs_f32()),
                255.0,
            ) as u8;

            self.color = Color::rgba(r, g, b, 255);
        }

        if app.is_key_held(Key::Down) {
            let r = clamp(
                0.0,
                self.color.r() as f32 - (50.0 * dt.as_secs_f32()),
                255.0,
            ) as u8;
            let g = clamp(
                0.0,
                self.color.g() as f32 - (50.0 * dt.as_secs_f32()),
                255.0,
            ) as u8;
            let b = clamp(
                0.0,
                self.color.b() as f32 - (50.0 * dt.as_secs_f32()),
                255.0,
            ) as u8;

            self.color = Color::rgba(r, g, b, 255);
        }

        info!("rendering");

        app.clear(self.color);
    }
}

fn main() -> Result<()> {
    let engine = Apparatus::new("Getting Started", ApparatusSettings::default())?;
    engine.run::<Example>()?;

    Ok(())
}

See the examples for more in-depth usage.

Examples

Development

Running tests

Run tests from the command line with cargo:

cargo test

Versioning

Apparatus is in very early development and does not currently follow semver. Neither does it commit to a minimum supported Rust version.

Commit count: 0

cargo fmt