-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
main.rs
117 lines (99 loc) 路 3.21 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#![deny(clippy::all)]
#![forbid(unsafe_code)]
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use beryllium::{
event::Event,
init::{InitFlags, Sdl},
window::WindowFlags,
};
use fermium::keycode;
use pixels::{Pixels, SurfaceTexture};
use zstring::zstr;
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<(), Box<dyn std::error::Error>> {
env_logger::init();
let sdl = Sdl::init(InitFlags::EVERYTHING)?;
let window = sdl.create_vk_window(
zstr!("Hello Pixels"),
None,
(WIDTH as i32, HEIGHT as i32),
WindowFlags::ALLOW_HIGHDPI,
)?;
let mut pixels = {
// TODO: Beryllium does not expose the SDL2 `GetDrawableSize` APIs, so choosing the correct
// surface texture size is not possible.
let surface_texture = SurfaceTexture::new(WIDTH, HEIGHT, &*window);
Pixels::new(WIDTH, HEIGHT, surface_texture)?
};
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 { keycode: key, .. } if key == keycode::SDLK_ESCAPE => {
break 'game_loop
}
// Resize the window
Event::WindowResized { width, height, .. } => pixels.resize_surface(width, height),
_ => (),
}
}
// 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 > 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);
}
}
}