Skip to content

Commit

Permalink
Add support for softbuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
emmabritton committed Sep 30, 2024
1 parent 1c5bc29 commit 2aeb865
Show file tree
Hide file tree
Showing 31 changed files with 1,083 additions and 332 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

### Verion 0.20.0

### Breaking

- You **MUST** pick either the `pixels` or `softbuffer` feature now
- Previously this was using pixels only, so set to `pixels` and everything should work the exact same
- Add support for `softbuffer`
- Remove exact dep versions
- Add `set_mouse_cursor()` for TextField
- Add `&Window` as last param on `update()` methods

### Version 0.19.1

- Draw submenus on the left if there's not enough room on the right
Expand Down
39 changes: 23 additions & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "pixels-graphics-lib"
version = "0.19.1"
version = "0.20.0"
edition = "2021"
authors = ["Emma Britton <emmabritton@pm.me>"]
description = "Simple wrapper library around Pixels/Buffer Graphics"
description = "Simple pixel graphics and GUI library"
license-file = "LICENSE"
repository = "https://github.com/emmabritton/pixel-graphics-lib"
readme = "README.md"
Expand All @@ -18,27 +18,34 @@ sound = ["simple-game-utils/sound"]
file_dialogs = ["directories"]
controller_xinput = ["serde", "simple-game-utils/controller_xinput"]
images = ["buffer-graphics-lib/image_loading"]
serde = ["dep:serde", "buffer-graphics-lib/serde", "simple-game-utils/serde", "winit/serde"]
serde = ["dep:serde", "buffer-graphics-lib/serde", "simple-game-utils/serde"]
mint = ["buffer-graphics-lib/mint"]
scenes = ["window_prefs"]
embedded = ["buffer-graphics-lib/embedded"]
notosans = ["buffer-graphics-lib/notosans"]
pixels = ["dep:pixels", "winit_29", "winit_input_helper"]
softbuffer = ["dep:softbuffer", "winit_30", "window_prefs"]
pixels_serde = ["pixels", "serde", "winit_29/serde"]
softbuffer_serde = ["softbuffer", "serde", "winit_30/serde"]

[dependencies]
pixels = "0.13.0"
winit = { version = "0.29.15", features = ["rwh_05"] }
winit_input_helper = { version = "0.16.0" }
thiserror = "1.0.59"
serde = { version = "1.0.202", features = ["derive"], optional = true }
directories = { version = "5.0.1", optional = true }
buffer-graphics-lib = { version = "0.18.1", default-features = false }
rustc-hash = "2.0.0"
simple-game-utils = { version = "0.4.2", default-features = false }
log = "0.4.21"
screen_size = "0.1.0"
pixels = { version = "0.14.0", optional = true }
winit_29 = { package = "winit", version = "0.29", features = ["rwh_05"], optional = true }
winit_30 = { package = "winit", version = "0.30", features = ["rwh_06"], optional = true }
softbuffer = { version = "0.4", optional = true }
winit_input_helper = { version = "0.16", optional = true }
thiserror = "1.0"
serde = { version = "1.0", features = ["derive"], optional = true }
directories = { version = "5.0", optional = true }
buffer-graphics-lib = { version = "0.19.0", default-features = false }
rustc-hash = "2.0"
simple-game-utils = { version = "0.4", default-features = false }
log = "0.4"

[dev-dependencies]
fastrand = "2.1.1"
anyhow = "1.0.86"
fastrand = "2.1"
anyhow = "1.0"

[[example]]
name = "test_dialogs"
Expand All @@ -54,4 +61,4 @@ required-features = ["images"]

[[example]]
name = "pre_post_w_controller"
required-features = ["controller"]
required-features = ["controller"]
56 changes: 49 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[![Crates.io](https://img.shields.io/crates/v/pixels-graphics-lib)](https://crates.io/crates/pixels-graphics-lib "Crates.io version")
[![Documentation](https://img.shields.io/docsrs/pixels-graphics-lib)](https://docs.rs/pixels-graphics-lib "Documentation")

# Graphics Lib
# Pixels Graphics Lib

This is a simple wrapper around [Pixels](https://github.com/parasyte/pixels), designed to be used
with [Buffer Graphics Lib](https://github.com/emmabritton/buffer-graphics-lib)
Pixel buffer graphics and GUI library. It helps simplify window setup and creation, and event looping.
It uses [buffer graphics lib](https://github.com/emmabritton/buffer-graphics-lib) for drawing to the buffer.

## Usage

Expand All @@ -13,10 +13,23 @@ with [Buffer Graphics Lib](https://github.com/emmabritton/buffer-graphics-lib)
In your `Cargo.toml` file add

```toml
pixels-graphics-lib = "0.19.1"
winit_input_helper = "0.16.0" #only needed if you're not using `run()`
pixels-graphics-lib = { version = "0.20", features = [] }
```

Inside `features` you **MUST** put one of these:

| Feature | Renderer | Window creation |
|--------------|------------------------------------------------------------|--------------------------------------------------------|
| `pixels` | [Pixels](https://github.com/parasyte/pixels) | [Winit](https://github.com/rust-windowing/winit) v0.29 |
| `softbuffer` | [Softbuffer](https://github.com/rust-windowing/softbuffer) | [Winit](https://github.com/rust-windowing/winit) v0.30 |

Both of these use `rwh06`

This will control how the window is created and managed and how the buffer is rendered to the screen. The main
differences are when the window is scaled to a non integer value (1.2 opposed than 2.0) then pixels will draw your
content in the middle of the window, whereas softbuffer will draw in the top left. Additionally, pixels uses hardware
scaling and softbuffer uses software scaling.

### Code

You can use scenes using `run_scenes` (requires default feature `scenes`):
Expand Down Expand Up @@ -145,21 +158,50 @@ Built in file selection dialogs, not recommended, use `rfd`

### `mint`

Enables `graphic-shapes/mint`
Enables `buffer-graphics-lib/mint`,
see [Buffer graphics readme](https://github.com/emmabritton/buffer-graphics-lib?tab=readme-ov-file#features)

### `notosan`

Enables `buffer-graphics-lib/notosans`,
see [Buffer graphics readme](https://github.com/emmabritton/buffer-graphics-lib?tab=readme-ov-file#features)

### `embedded`

Enables `buffer-graphics-lib/embedded`,
see [Buffer graphics readme](https://github.com/emmabritton/buffer-graphics-lib?tab=readme-ov-file#features)

### `pixels_serde` and `softbuffer_serde`

Enables `serde` for the `winit` crate being used by `pixels` or `softbuffer`

## Examples

Each example must be run with a renderer (pixels or softbuffer), like this:

`cargo run --example basic --features "pixels"`

or

`cargo run --example relative_test --features "softbuffer"`

## Projects

### [Retro Games](https://github.com/emmabritton/retro-games)

A few retro games

### [Wordle](https://github.com/emmabritton/wordle)

A wordle clone

### [ICI Image editor](https://github.com/emmabritton/ici-image-editor)

Editor for `IndexedImage`, ICI files

### [USFX Tester](https://github.com/emmabritton/uxfs-test)

Test GUI for [USFX](https://github.com/tversteeg/usfx)
GUI for [USFX](https://github.com/tversteeg/usfx)

### [Fontpad](https://github.com/emmabritton/fontpad)

Expand Down
3 changes: 2 additions & 1 deletion examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use pixels_graphics_lib::prelude::*;
use winit::keyboard::KeyCode;
use winit::window::Window;

/// This example shows the minimum code needed to use the library
Expand All @@ -25,7 +26,7 @@ impl Basic {
}

impl System for Basic {
fn update(&mut self, _delta: &Timing) {
fn update(&mut self, _delta: &Timing, _: &Window) {
if self.greyscale < 255 {
self.greyscale += 1;
} else {
Expand Down
2 changes: 1 addition & 1 deletion examples/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl ImageScene {
}

impl System for ImageScene {
fn update(&mut self, timing: &Timing) {
fn update(&mut self, timing: &Timing, _: &Window) {
let sw = self.width;
let sh = self.height;

Expand Down
1 change: 1 addition & 0 deletions examples/layout_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ impl Scene<SceneResult, SceneName> for LayoutTest {
timing: &Timing,
_: &MouseData,
_: &FxHashSet<KeyCode>,
_: &Window,
) -> SceneUpdateResult<SceneResult, SceneName> {
self.text_field.update(timing);
self.spacing.update(timing);
Expand Down
1 change: 1 addition & 0 deletions examples/menu_bar_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ impl Scene<SceneResult, SceneName> for MenuTest {
_: &Timing,
mouse: &MouseData,
_: &FxHashSet<KeyCode>,
_: &Window,
) -> SceneUpdateResult<SceneResult, SceneName> {
self.menubar.on_mouse_move(mouse.xy);
Nothing
Expand Down
5 changes: 5 additions & 0 deletions examples/pre_post.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use pixels_graphics_lib::prelude::*;
use pixels_graphics_lib::scenes::SceneUpdateResult::Nothing;
use pixels_graphics_lib::ui::prelude::*;
use winit::keyboard::KeyCode;
use winit::window::Window;

#[derive(Debug, Clone, PartialEq)]
enum SR {}
Expand Down Expand Up @@ -34,6 +36,7 @@ impl Scene<SR, SN> for WhiteTextScene {
_: &Timing,
_: &MouseData,
_: &FxHashSet<KeyCode>,
_: &Window,
) -> SceneUpdateResult<SR, SN> {
Nothing
}
Expand Down Expand Up @@ -84,6 +87,7 @@ impl PrePost<SR, SN> for ExtrasImpl {
_: &MouseData,
_: &FxHashSet<KeyCode>,
_: &mut [Box<dyn Scene<SR, SN>>],
_: &Window,
) {
}

Expand All @@ -93,6 +97,7 @@ impl PrePost<SR, SN> for ExtrasImpl {
_: &MouseData,
_: &FxHashSet<KeyCode>,
_: &mut [Box<dyn Scene<SR, SN>>],
_: &Window,
) {
if self.timer.update(timing) {
self.pixel.x += 1;
Expand Down
5 changes: 4 additions & 1 deletion examples/pre_post_w_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl Scene<SR, SN> for WhiteTextScene {
_: &MouseData,
_: &FxHashSet<KeyCode>,
_: &GameController,
_: &Window,
) -> SceneUpdateResult<SR, SN> {
Nothing
}
Expand Down Expand Up @@ -94,6 +95,7 @@ impl PrePost<SR, SN> for ExtrasImpl {
_: &FxHashSet<KeyCode>,
_: &mut [Box<dyn Scene<SR, SN>>],
_: &GameController,
_: &Window,
) {
}

Expand All @@ -104,6 +106,7 @@ impl PrePost<SR, SN> for ExtrasImpl {
_: &FxHashSet<KeyCode>,
_: &mut [Box<dyn Scene<SR, SN>>],
_: &GameController,
_: &Window,
) {
if self.timer.update(timing) {
self.pixel.x += 1;
Expand All @@ -115,7 +118,7 @@ impl PrePost<SR, SN> for ExtrasImpl {
}

fn main() {
let switcher = |style: &UiStyle, scenes: &mut Vec<Box<dyn Scene<SR, SN>>>, new_scene: SN| {};
let switcher = |_: &UiStyle, _: &mut Vec<Box<dyn Scene<SR, SN>>>, _: SN| {};
run_scenes(
100,
100,
Expand Down
1 change: 1 addition & 0 deletions examples/relative_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ impl Scene<SceneResult, SceneName> for LayoutTest {
_: &Timing,
_: &MouseData,
_: &FxHashSet<KeyCode>,
_: &Window,
) -> SceneUpdateResult<SceneResult, SceneName> {
Nothing
}
Expand Down
1 change: 1 addition & 0 deletions examples/relative_test2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ impl Scene<SceneResult, SceneName> for LayoutTest {
_: &Timing,
_: &MouseData,
_: &FxHashSet<KeyCode>,
_: &Window,
) -> SceneUpdateResult<SceneResult, SceneName> {
Nothing
}
Expand Down
1 change: 1 addition & 0 deletions examples/relative_test3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ impl Scene<SceneResult, SceneName> for LayoutTest {
_: &Timing,
_: &MouseData,
_: &FxHashSet<KeyCode>,
_: &Window,
) -> SceneUpdateResult<SceneResult, SceneName> {
Nothing
}
Expand Down
1 change: 1 addition & 0 deletions examples/relative_test4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ impl Scene<SceneResult, SceneName> for LayoutTest {
_: &Timing,
_: &MouseData,
_: &FxHashSet<KeyCode>,
_: &Window,
) -> SceneUpdateResult<SceneResult, SceneName> {
Nothing
}
Expand Down
1 change: 1 addition & 0 deletions examples/relative_test5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl Scene<SceneResult, SceneName> for LayoutTest {
_: &Timing,
_: &MouseData,
_: &FxHashSet<KeyCode>,
_: &Window,
) -> SceneUpdateResult<SceneResult, SceneName> {
Nothing
}
Expand Down
3 changes: 3 additions & 0 deletions examples/scenes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl Scene<SceneResult, SceneName> for Scene1 {
_: &Timing,
_: &MouseData,
_: &FxHashSet<KeyCode>,
_: &Window,
) -> SceneUpdateResult<SceneResult, SceneName> {
self.result.clone()
}
Expand Down Expand Up @@ -114,6 +115,7 @@ impl Scene<SceneResult, SceneName> for Scene2 {
_: &Timing,
_: &MouseData,
_: &FxHashSet<KeyCode>,
_: &Window,
) -> SceneUpdateResult<SceneResult, SceneName> {
self.result.clone()
}
Expand All @@ -140,6 +142,7 @@ impl Scene<SceneResult, SceneName> for Scene3 {
_: &Timing,
_: &MouseData,
_: &FxHashSet<KeyCode>,
_: &Window,
) -> SceneUpdateResult<SceneResult, SceneName> {
self.result.clone()
}
Expand Down
3 changes: 2 additions & 1 deletion examples/ui_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn main() -> Result<()> {
WIDTH,
HEIGHT,
"UI Tester",
Some(WindowPreferences::new("app", "emmabritton", "pixels_ui_tester", 1).unwrap()),
Some(WindowPreferences::new("app", "emmabritton", "pixels_ui_tester", 5).unwrap()),
switcher,
menu,
options,
Expand Down Expand Up @@ -209,6 +209,7 @@ impl Scene<SceneResult, SceneName> for Menu {
timing: &Timing,
_: &MouseData,
_: &FxHashSet<KeyCode>,
_: &Window,
) -> SceneUpdateResult<SceneResult, SceneName> {
self.field1.update(timing);
self.field2.update(timing);
Expand Down
2 changes: 1 addition & 1 deletion examples/window_pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl System for WindowPrefsScene {
)
}

fn update(&mut self, _delta: &Timing) {
fn update(&mut self, _delta: &Timing, _: &Window) {
if self.idx < self.colors.len() - 1 {
self.idx += 1;
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/dialogs/load_file_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::*;
use buffer_graphics_lib::prelude::*;
use directories::UserDirs;
use std::fmt::Debug;
use winit::window::Window;

/// You should use something like `rfd` instead of this
#[derive(Debug)]
Expand Down Expand Up @@ -234,6 +235,7 @@ where
_: &MouseData,
_: &FxHashSet<KeyCode>,
_: &GameController,
_: &Window,
) -> SceneUpdateResult<SR, SN> {
self.update(timing)
}
Expand All @@ -244,6 +246,7 @@ where
timing: &Timing,
_: &MouseData,
_: &FxHashSet<KeyCode>,
_: &Window,
) -> SceneUpdateResult<SR, SN> {
self.update(timing)
}
Expand Down
Loading

0 comments on commit 2aeb865

Please sign in to comment.