Giter VIP home page Giter VIP logo

pixel-graphics-lib's Introduction

Crates.io Documentation

Graphics Lib

This is a simple wrapper around Pixels, designed to be used with Buffer Graphics Lib

Usage

Cargo

In your Cargo.toml file add

pixels-graphics-lib = "0.18.0"
winit_input_helper = "0.16.0" #only needed if you're not using `run()`

Code

You can use scenes using run_scenes (requires default feature scenes):

fn main() -> Result<()> {
    // Window prefs allow the size and position of the window to be saved and restored
    let window_prefs = WindowPreferences::new("com", "example", "app", 1)?;
    // Options contains scaling, UPS, etc
    let options = Options::default();
    // The switcher is how new scenes are created
    let switcher: SceneSwitcher<SceneResult, SceneName> =
        |style, scene_stack, new_scene| match new_scene {
            SceneName::Example => scene_stack.push(ExampleScene::new()),
        };
    let first_scene = ExampleScene::new();
    run_scenes(
        300,
        300,
        "Scenes Example",
        Some(window_prefs),
        switcher,
        first_scene,
        options,
        empty_pre_post()
    )?;
    Ok(())
}

// The scene name is the id used so the switcher knows which one to create
#[derive(Clone, Debug, PartialEq)]
enum SceneName {
    Example,
}

// After a scene is finished it can return values to it's parent using scene result
#[derive(Clone, Debug, PartialEq)]
enum SceneResult {}

struct ExampleScene {}

impl ExampleScene {
    pub fn new() -> Box<Self> {
        Box::new(Self {})
    }
}

impl Scene<SceneResult, SceneName> for ExampleScene {
    fn render(
        &mut self,
        graphics: &mut Graphics,
        mouse_xy: Coord,
        held_keys: &[KeyCode]) {
        todo!()
    }

    fn update(
        &mut self,
        timing: &Timing,
        mouse_xy: Coord,
        held_keys: &[KeyCode],
    ) -> SceneUpdateResult<SceneResult, SceneName> {
        todo!()
    }
}

or a more low level with run

struct Example {}

fn main() -> Result<()> {
    let system = Box::new(Example {});
    run(240, 160, "Example", Box::new(system), Options::default())?;
    Ok(())
}

//Check `src/scenes.rs` for examples of implementing held keys, etc
impl System for Example {
    fn update(&mut self, timing: &Timing) {}
    fn render(&mut self, graphics: &mut Graphics) {}
}

Features

Default features: window_prefs, sound, serde, scenes

window_prefs

Save and restore window position and size

To use this the impl System must override System::window_prefs()

scenes

Enables Scene and run_scenes

Includes window_prefs

controller

  • Adds gamepad support
  • Adds gamepad state to Scene::update, Scene::render

controller_xinput

As above but using xinput, windows only

sound

Play music or sound effects

serde

Adds Serialize and Deserialize to most structs and enums

images

Loading and displaying of PNGs, JPEGs, BMPs

file_dialogs

Built in file selection dialogs, not recommended, use rfd

mint

Enables graphic-shapes/mint

Projects

A few retro games

Editor for IndexedImage

Test GUI for USFX

pixel-graphics-lib's People

Contributors

emmabritton avatar

Stargazers

Thomas Versteeg avatar Nurzhan Saken avatar  avatar Andrew Wheeler(Genusis) avatar  avatar

Watchers

 avatar

Forkers

fekie

pixel-graphics-lib's Issues

prefs underflow crash

mid_x = {u32} 1920
mid_y = {u32} 1080
mid_w = {u32} 1280
mid_h = {u32} 1280

The application panicked (crashed).
Message:  attempt to subtract with overflow
Location: /Users/emma/.cargo/registry/src/index.crates.io-6f17d22bba15001f/pixels-graphics-lib-0.16.0/src/window_prefs.rs:65

  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ BACKTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
                                ⋮ 13 frames hidden ⋮                              
  14: core::panicking::panic::h57fd475c037a9df3
      at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:144
  15: pixels_graphics_lib::window_prefs::WindowPreferences::restore::h357d69b2478298ae
      at /Users/emma/.cargo/registry/src/index.crates.io-6f17d22bba15001f/pixels-graphics-lib-0.16.0/src/window_prefs.rs:65
  16: pixels_graphics_lib::run::h062f38b0b3f3928f
      at /Users/emma/.cargo/registry/src/index.crates.io-6f17d22bba15001f/pixels-graphics-lib-0.16.0/src/lib.rs:325
  17: pixels_graphics_lib::scenes::run_scenes::h33f95c8ced46f194
      at /Users/emma/.cargo/registry/src/index.crates.io-6f17d22bba15001f/pixels-graphics-lib-0.16.0/src/scenes.rs:41
  18: tank_bot::main::hc79aa16ae6eb551d
      at /Users/emma/Documents/rust/active/tank_bot/src/main.rs:24
  19: core::ops::function::FnOnce::call_once::h17ca301d8c6b3a06
      at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/ops/function.rs:250

Add check at window_prefs:65 and use mid monitor I guess?
Maybe an error should be returned as the window will be bigger than the monitor

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.