[][src]Crate specs_blit

Specs ECS Rendering System

This library exposes a 2D rendering system to be used in specs. It is based around the blit library. All the images will be rendered to a buffer which can be used in various graphics libraries, e.g. minifb.

All sprites are loaded onto a big array on the heap.

use anyhow::Result;
use blit::{BlitBuffer, Color};
use specs::prelude::*;
use specs_blit::{load, PixelBuffer, RenderSystem, Sprite};

const WIDTH: usize = 800;
const HEIGHT: usize = 800;
const MASK_COLOR: u32 = 0xFF00FF;

fn main() -> Result<()> {
    // Setup the specs world
    let mut world = World::new();

    // Load the blit components into the world
    world.register::<Sprite>();

    // Add the pixel buffer as a resource so it can be accessed from the RenderSystem later
    world.insert(PixelBuffer::new(WIDTH, HEIGHT));

    let sprite_ref = {
        // Create a sprite of 2 pixels
        let sprite = BlitBuffer::from_buffer(&[0, MASK_COLOR], 1, Color::from_u32(MASK_COLOR));

        // Load the sprite and get a reference
        load(sprite)?
    };

    // Create a new sprite entity in the ECS system
    world.create_entity()
        .with(Sprite::new(sprite_ref))
        .build();

    // Setup the dispatcher with the blit system
    let mut dispatcher = DispatcherBuilder::new()
        .with_thread_local(RenderSystem)
        .build();

    Ok(())
}

Structs

PixelBuffer

Array of pixels resource that can be written to from the RenderSystem system.

RenderSystem

Specs system for rendering to a buffer.

Sprite

Specs component representing a sprite that can be drawn.

SpriteRef

Reference to a heap-allocated sprite. Contains the index of the vector, only this crate is allowed to access this.

Functions

load

Load a sprite buffer and place it onto the heap.