Add minimal-sdl2 example (#48)
This commit is contained in:
parent
8061ce0ea4
commit
bb898d78de
35
Cargo.lock
generated
35
Cargo.lock
generated
|
@ -91,6 +91,17 @@ name = "base-x"
|
|||
version = "0.2.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "beryllium"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"fermium 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"objc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"phantom-fields 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"raw-window-handle 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.2.1"
|
||||
|
@ -245,6 +256,16 @@ dependencies = [
|
|||
"termcolor 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fermium"
|
||||
version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fnv"
|
||||
version = "1.0.6"
|
||||
|
@ -765,10 +786,21 @@ name = "percent-encoding"
|
|||
version = "2.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "phantom-fields"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pixels"
|
||||
version = "0.0.2"
|
||||
dependencies = [
|
||||
"beryllium 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"getrandom 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
@ -1514,6 +1546,7 @@ dependencies = [
|
|||
"checksum backtrace 0.3.40 (registry+https://github.com/rust-lang/crates.io-index)" = "924c76597f0d9ca25d762c25a4d369d51267536465dc5064bdf0eb073ed477ea"
|
||||
"checksum backtrace-sys 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "5d6575f128516de27e3ce99689419835fce9643a9b215a14d2b5b685be018491"
|
||||
"checksum base-x 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "1b20b618342cf9891c292c4f5ac2cde7287cc5c87e87e9c769d617793607dec1"
|
||||
"checksum beryllium 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1c821518d537a3ac0299396c4cfe84a877c0d83c494fd5c4310ec87873711d12"
|
||||
"checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
|
||||
"checksum block 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a"
|
||||
"checksum bumpalo 2.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ad807f2fc2bf185eeb98ff3a901bd46dc5ad58163d0fa4577ba0d25674d71708"
|
||||
|
@ -1535,6 +1568,7 @@ dependencies = [
|
|||
"checksum dlib 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "77e51249a9d823a4cb79e3eca6dcd756153e8ed0157b6c04775d04bf1b13b76a"
|
||||
"checksum downcast-rs 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "52ba6eb47c2131e784a38b726eb54c1e1484904f013e576a25354d0124161af6"
|
||||
"checksum env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
|
||||
"checksum fermium 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6989d270a6fa93ffa2c4c1c7c2ef56c8315d2aa099e000e413665808a4c68050"
|
||||
"checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3"
|
||||
"checksum foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1"
|
||||
"checksum foreign-types-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
|
||||
|
@ -1590,6 +1624,7 @@ dependencies = [
|
|||
"checksum parking_lot_core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b876b1b9e7ac6e1a74a6da34d25c42e17e8862aa409cbbbdcfc8d86c6f3bc62b"
|
||||
"checksum pcx 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1b00b062973776578e7863f8395f86e821760d827384f1ad9371d0893c87481a"
|
||||
"checksum percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
|
||||
"checksum phantom-fields 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "cc7d5c2ec53ed64f6d636d44717906952de3d7930178b680b272c62fdd7e7968"
|
||||
"checksum pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677"
|
||||
"checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"
|
||||
"checksum proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "9c9e470a8dc4aeae2dee2f335e8f533e2d4b347e1434e5671afc49b054592f27"
|
||||
|
|
|
@ -27,6 +27,7 @@ wgpu = "0.4"
|
|||
|
||||
# These are only used by the examples, and enabled with features
|
||||
# See: https://github.com/rust-lang/cargo/issues/1982
|
||||
beryllium = { version = " 0.1", optional = true }
|
||||
byteorder = { version = "1.3", optional = true }
|
||||
env_logger = { version = "0.7", optional = true }
|
||||
getrandom = { version = "0.1", optional = true }
|
||||
|
@ -50,6 +51,10 @@ required-features = ["conway"]
|
|||
name = "invaders"
|
||||
required-features = ["invaders"]
|
||||
|
||||
[[example]]
|
||||
name = "minimal-sdl2"
|
||||
required-features = ["minimal-sdl2"]
|
||||
|
||||
[[example]]
|
||||
name = "minimal-winit"
|
||||
required-features = ["minimal-winit"]
|
||||
|
@ -58,10 +63,12 @@ required-features = ["minimal-winit"]
|
|||
default = []
|
||||
log-deps = ["env_logger", "log"]
|
||||
random-deps = ["byteorder", "getrandom", "randomize"]
|
||||
sdl2-deps = ["beryllium"]
|
||||
winit-deps = ["winit", "winit_input_helper"]
|
||||
|
||||
conway = ["line_drawing", "log-deps", "random-deps", "winit-deps"]
|
||||
invaders = ["gilrs", "log-deps", "random-deps", "simple-invaders", "winit-deps"]
|
||||
minimal-sdl2 = ["log-deps", "sdl2-deps"]
|
||||
minimal-winit = ["log-deps", "winit-deps"]
|
||||
|
||||
[workspace]
|
||||
|
|
17
examples/minimal-sdl2/README.md
Normal file
17
examples/minimal-sdl2/README.md
Normal file
|
@ -0,0 +1,17 @@
|
|||
# Hello Pixels
|
||||
|
||||
![Hello Pixels](../../img/minimal-sdl2.png)
|
||||
|
||||
Minimal example with SDL2 using `beryllium`.
|
||||
|
||||
## Running
|
||||
|
||||
```bash
|
||||
cargo run --release --example minimal-sdl2 --features minimal-sdl2
|
||||
```
|
||||
|
||||
## About
|
||||
|
||||
This example demonstrates the absolute minimum for creating an SDL2 window and pixel buffer. It animates a purple box moving on a blue background, just for _something_ interesting to display.
|
||||
|
||||
The example currently does not run on macOS. See [#47](https://github.com/parasyte/pixels/issues/47) for details.
|
120
examples/minimal-sdl2/main.rs
Normal file
120
examples/minimal-sdl2/main.rs
Normal file
|
@ -0,0 +1,120 @@
|
|||
#![deny(clippy::all)]
|
||||
#![forbid(unsafe_code)]
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
use beryllium::*;
|
||||
use pixels::{wgpu::Surface, Pixels, SurfaceTexture};
|
||||
|
||||
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<(), String> {
|
||||
env_logger::init();
|
||||
let sdl = beryllium::init()?;
|
||||
let window = sdl.create_window(
|
||||
"Hello Pixels",
|
||||
WINDOW_POSITION_CENTERED,
|
||||
WINDOW_POSITION_CENTERED,
|
||||
WIDTH as i32,
|
||||
HEIGHT as i32,
|
||||
WindowFlags::default(),
|
||||
)?;
|
||||
window.set_minimum_size(WIDTH as i32, HEIGHT as i32);
|
||||
window.set_resizable(true);
|
||||
|
||||
let mut pixels = {
|
||||
let surface = Surface::create(&window);
|
||||
let surface_texture = SurfaceTexture::new(WIDTH, HEIGHT, surface);
|
||||
Pixels::new(WIDTH, HEIGHT, surface_texture).map_err(|e| format!("{:?}", e))?
|
||||
};
|
||||
let mut world = World::new();
|
||||
|
||||
'game_loop: loop {
|
||||
while let Some(event) = sdl.poll_event() {
|
||||
match event {
|
||||
// Close events
|
||||
Event::Quit { .. } => break 'game_loop,
|
||||
Event::Keyboard {
|
||||
key_info:
|
||||
KeyInfo {
|
||||
keycode: Some(key), ..
|
||||
},
|
||||
..
|
||||
} if key == Keycode::Escape => break 'game_loop,
|
||||
|
||||
// Resize the window
|
||||
Event::WindowSizeChanged { width, height, .. } => {
|
||||
pixels.resize(width as u32, height as u32)
|
||||
}
|
||||
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
// Update internal state
|
||||
world.update();
|
||||
|
||||
// Draw the current frame
|
||||
world.draw(pixels.get_frame());
|
||||
pixels.render();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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 - 1 >= WIDTH as i16 {
|
||||
self.velocity_x *= -1;
|
||||
}
|
||||
if self.box_y <= 0 || self.box_y + BOX_SIZE - 1 >= 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-sdl2.png
Executable file
BIN
img/minimal-sdl2.png
Executable file
Binary file not shown.
After Width: | Height: | Size: 1.4 KiB |
Loading…
Reference in a new issue