Add minimal example with tao
(#299)
* Add minimal example with `tao` - Closes #298
This commit is contained in:
parent
0b380b637d
commit
bcbe9d84cb
8
.github/workflows/ci.yml
vendored
8
.github/workflows/ci.yml
vendored
|
@ -20,7 +20,7 @@ jobs:
|
||||||
- name: Update apt repos
|
- name: Update apt repos
|
||||||
run: sudo apt-get -y update
|
run: sudo apt-get -y update
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: sudo apt -y install libsdl2-dev
|
run: sudo apt -y install libsdl2-dev libgtk-3-dev
|
||||||
- name: Install toolchain
|
- name: Install toolchain
|
||||||
uses: actions-rs/toolchain@v1
|
uses: actions-rs/toolchain@v1
|
||||||
with:
|
with:
|
||||||
|
@ -41,7 +41,7 @@ jobs:
|
||||||
- name: Update apt repos
|
- name: Update apt repos
|
||||||
run: sudo apt-get -y update
|
run: sudo apt-get -y update
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: sudo apt -y install libsdl2-dev
|
run: sudo apt -y install libsdl2-dev libgtk-3-dev
|
||||||
- name: Install toolchain
|
- name: Install toolchain
|
||||||
uses: actions-rs/toolchain@v1
|
uses: actions-rs/toolchain@v1
|
||||||
with:
|
with:
|
||||||
|
@ -80,7 +80,7 @@ jobs:
|
||||||
- name: Update apt repos
|
- name: Update apt repos
|
||||||
run: sudo apt-get -y update
|
run: sudo apt-get -y update
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: sudo apt -y install libsdl2-dev
|
run: sudo apt -y install libsdl2-dev libgtk-3-dev
|
||||||
- name: Install toolchain
|
- name: Install toolchain
|
||||||
uses: actions-rs/toolchain@v1
|
uses: actions-rs/toolchain@v1
|
||||||
with:
|
with:
|
||||||
|
@ -106,7 +106,7 @@ jobs:
|
||||||
- name: Update apt repos
|
- name: Update apt repos
|
||||||
run: sudo apt-get -y update
|
run: sudo apt-get -y update
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: sudo apt -y install libsdl2-dev
|
run: sudo apt -y install libsdl2-dev libgtk-3-dev
|
||||||
- name: Install toolchain
|
- name: Install toolchain
|
||||||
uses: actions-rs/toolchain@v1
|
uses: actions-rs/toolchain@v1
|
||||||
with:
|
with:
|
||||||
|
|
|
@ -35,6 +35,7 @@ The Minimum Supported Rust Version for `pixels` will always be made available in
|
||||||
- [Minimal example for WebGL2](./examples/minimal-web)
|
- [Minimal example for WebGL2](./examples/minimal-web)
|
||||||
- [Minimal example with SDL2](./examples/minimal-sdl2)
|
- [Minimal example with SDL2](./examples/minimal-sdl2)
|
||||||
- [Minimal example with `winit`](./examples/minimal-winit)
|
- [Minimal example with `winit`](./examples/minimal-winit)
|
||||||
|
- [Minimal example with `tao`](./examples/minimal-tao)
|
||||||
- [Minimal example with `fltk`](./examples/minimal-fltk)
|
- [Minimal example with `fltk`](./examples/minimal-fltk)
|
||||||
- [Pixel Invaders](./examples/invaders)
|
- [Pixel Invaders](./examples/invaders)
|
||||||
- [`raqote` example](./examples/raqote-winit)
|
- [`raqote` example](./examples/raqote-winit)
|
||||||
|
|
16
examples/minimal-tao/Cargo.toml
Normal file
16
examples/minimal-tao/Cargo.toml
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
[package]
|
||||||
|
name = "minimal-tao"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Jay Oster <jay@kodewerx.org>"]
|
||||||
|
edition = "2021"
|
||||||
|
publish = false
|
||||||
|
|
||||||
|
[features]
|
||||||
|
optimize = ["log/release_max_level_warn"]
|
||||||
|
default = ["optimize"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
env_logger = "0.9"
|
||||||
|
log = "0.4"
|
||||||
|
pixels = { path = "../.." }
|
||||||
|
tao = "0.12"
|
17
examples/minimal-tao/README.md
Normal file
17
examples/minimal-tao/README.md
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
# Hello Pixels/Tao
|
||||||
|
|
||||||
|
![Hello Pixels/Tao](../../img/minimal-tao.png)
|
||||||
|
|
||||||
|
Minimal example with `tao`.
|
||||||
|
|
||||||
|
## Running
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cargo run --release --package minimal-tao
|
||||||
|
```
|
||||||
|
|
||||||
|
## About
|
||||||
|
|
||||||
|
This example is based on `minimal-winit`, using `tao` for window and event handling.
|
||||||
|
|
||||||
|
It adds a native menubar with a single "File > Quit" menu item to demonstrate simple functionality.
|
145
examples/minimal-tao/src/main.rs
Normal file
145
examples/minimal-tao/src/main.rs
Normal file
|
@ -0,0 +1,145 @@
|
||||||
|
#![deny(clippy::all)]
|
||||||
|
#![forbid(unsafe_code)]
|
||||||
|
|
||||||
|
use log::error;
|
||||||
|
use pixels::{Error, Pixels, SurfaceTexture};
|
||||||
|
use tao::dpi::LogicalSize;
|
||||||
|
use tao::event::{Event, KeyEvent, WindowEvent};
|
||||||
|
use tao::event_loop::{ControlFlow, EventLoop};
|
||||||
|
use tao::keyboard::KeyCode;
|
||||||
|
use tao::menu::{MenuBar, MenuItem};
|
||||||
|
use tao::window::WindowBuilder;
|
||||||
|
|
||||||
|
const WIDTH: u32 = 320;
|
||||||
|
const HEIGHT: u32 = 240;
|
||||||
|
const BOX_SIZE: i16 = 64;
|
||||||
|
|
||||||
|
/// Representation of the application state. In this example, a box will bounce around the screen.
|
||||||
|
struct World {
|
||||||
|
box_x: i16,
|
||||||
|
box_y: i16,
|
||||||
|
velocity_x: i16,
|
||||||
|
velocity_y: i16,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<(), Error> {
|
||||||
|
env_logger::init();
|
||||||
|
let event_loop = EventLoop::new();
|
||||||
|
let window = {
|
||||||
|
let mut file_menu = MenuBar::new();
|
||||||
|
file_menu.add_native_item(MenuItem::Quit);
|
||||||
|
|
||||||
|
let mut menu = MenuBar::new();
|
||||||
|
menu.add_submenu("File", true, file_menu);
|
||||||
|
|
||||||
|
let size = LogicalSize::new(WIDTH as f64, HEIGHT as f64);
|
||||||
|
WindowBuilder::new()
|
||||||
|
.with_title("Hello Pixels/Tao")
|
||||||
|
.with_menu(menu)
|
||||||
|
.with_inner_size(size)
|
||||||
|
.with_min_inner_size(size)
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap()
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut pixels = {
|
||||||
|
let window_size = window.inner_size();
|
||||||
|
let surface_texture = SurfaceTexture::new(window_size.width, window_size.height, &window);
|
||||||
|
Pixels::new(WIDTH, HEIGHT, surface_texture)?
|
||||||
|
};
|
||||||
|
let mut world = World::new();
|
||||||
|
|
||||||
|
event_loop.run(move |event, _, control_flow| {
|
||||||
|
match event {
|
||||||
|
Event::WindowEvent { event, .. } => match event {
|
||||||
|
// Close events
|
||||||
|
WindowEvent::CloseRequested
|
||||||
|
| WindowEvent::KeyboardInput {
|
||||||
|
event:
|
||||||
|
KeyEvent {
|
||||||
|
physical_key: KeyCode::Escape,
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
*control_flow = ControlFlow::Exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resize the window
|
||||||
|
WindowEvent::Resized(size) => {
|
||||||
|
pixels.resize_surface(size.width, size.height);
|
||||||
|
}
|
||||||
|
|
||||||
|
_ => {}
|
||||||
|
},
|
||||||
|
|
||||||
|
// Update internal state and request a redraw
|
||||||
|
Event::MainEventsCleared => {
|
||||||
|
world.update();
|
||||||
|
window.request_redraw();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw the current frame
|
||||||
|
Event::RedrawRequested(_) => {
|
||||||
|
world.draw(pixels.get_frame_mut());
|
||||||
|
if pixels
|
||||||
|
.render()
|
||||||
|
.map_err(|e| error!("pixels.render() failed: {}", e))
|
||||||
|
.is_err()
|
||||||
|
{
|
||||||
|
*control_flow = ControlFlow::Exit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
impl World {
|
||||||
|
/// Create a new `World` instance that can draw a moving box.
|
||||||
|
fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
box_x: 24,
|
||||||
|
box_y: 16,
|
||||||
|
velocity_x: 1,
|
||||||
|
velocity_y: 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update the `World` internal state; bounce the box around the screen.
|
||||||
|
fn update(&mut self) {
|
||||||
|
if self.box_x <= 0 || self.box_x + BOX_SIZE > WIDTH as i16 {
|
||||||
|
self.velocity_x *= -1;
|
||||||
|
}
|
||||||
|
if self.box_y <= 0 || self.box_y + BOX_SIZE > HEIGHT as i16 {
|
||||||
|
self.velocity_y *= -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.box_x += self.velocity_x;
|
||||||
|
self.box_y += self.velocity_y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Draw the `World` state to the frame buffer.
|
||||||
|
///
|
||||||
|
/// Assumes the default texture format: `wgpu::TextureFormat::Rgba8UnormSrgb`
|
||||||
|
fn draw(&self, frame: &mut [u8]) {
|
||||||
|
for (i, pixel) in frame.chunks_exact_mut(4).enumerate() {
|
||||||
|
let x = (i % WIDTH as usize) as i16;
|
||||||
|
let y = (i / WIDTH as usize) as i16;
|
||||||
|
|
||||||
|
let inside_the_box = x >= self.box_x
|
||||||
|
&& x < self.box_x + BOX_SIZE
|
||||||
|
&& y >= self.box_y
|
||||||
|
&& y < self.box_y + BOX_SIZE;
|
||||||
|
|
||||||
|
let rgba = if inside_the_box {
|
||||||
|
[0x5e, 0x48, 0xe8, 0xff]
|
||||||
|
} else {
|
||||||
|
[0x48, 0xb2, 0xe8, 0xff]
|
||||||
|
};
|
||||||
|
|
||||||
|
pixel.copy_from_slice(&rgba);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
BIN
img/minimal-tao.png
Normal file
BIN
img/minimal-tao.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.6 KiB |
Loading…
Reference in a new issue