Spinning Cube

This commit is contained in:
2026-02-11 01:45:21 +01:00
parent 498d0b1ba8
commit 77ed489f22
3 changed files with 6170 additions and 116 deletions

View File

@@ -1,94 +1,79 @@
use std::f32::consts::TAU;
use std::time::Duration;
use embedded_graphics::{
image::{Image, ImageRaw},
mono_font::{MonoTextStyle, ascii::*},
pixelcolor::BinaryColor,
prelude::*,
text::{Alignment, Text, TextStyleBuilder},
};
use tokio::time::Instant;
use bevy::app::ScheduleRunnerPlugin;
use bevy::camera::RenderTarget;
use bevy::color::Color;
use bevy::prelude::*;
use bevy::render::RenderPlugin;
use bevy::winit::WinitPlugin;
use wgpu::{TextureFormat, TextureUsages};
use g13_driver::{
G13, G13_LCD_COLUMNS, G13_LCD_ROWS,
joystick::{Axis, Button},
};
mod post_process;
const DEADZONE: i32 = 30;
use crate::post_process::PostProcessSettings;
use crate::renderer::{ImageExport, ImageExportPlugin, ImageExportSource};
use crate::shared::Spinner;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut g13 = G13::new()?;
let _g13 = g13.clone();
tokio::spawn(async move {
// background thread to update button state
loop {
if let Err(e) = _g13.read() {
eprintln!("{:?}", e);
break; // probably due to unplug
}
}
});
mod renderer;
mod shared;
g13.set_lcd_color(4, 8, 96)?;
// let character_style = MonoTextStyle::new(&FONT_5X7, BinaryColor::On);
// let textstyle = TextStyleBuilder::new()
// .baseline(embedded_graphics::text::Baseline::Top)
// .alignment(Alignment::Left)
// .build();
let mut dt = 0.0;
let mut x = 20.0;
let mut y = (G13_LCD_ROWS as f64 / 2.0) - 3.0;
let speed = 20.0;
let player_sprite = ImageRaw::<BinaryColor>::new(DATA, 16);
loop {
let start = Instant::now();
let mut jx = g13.state().x;
if (-DEADZONE..=DEADZONE).contains(&jx) {
jx = 0;
}
let mut jy: i32 = g13.state().y;
if (-DEADZONE..=DEADZONE).contains(&jy) {
jy = 0;
}
let joyx = jx as f64 / 502.0;
let joyy = jy as f64 / 502.0;
x += joyx * speed * dt;
y += joyy * speed * dt;
// Render
g13.clear(BinaryColor::Off).unwrap();
// Player
let image = Image::new(&player_sprite, Point::new(x as i32, y as i32));
image.draw(&mut g13)?;
// Terrain Generation
g13.render().unwrap();
// Calculate delta time
let delta = Instant::now() - start;
tokio::time::sleep(Duration::from_millis(33) - delta).await;
dt = (Instant::now() - start).as_secs_f64();
}
fn main() {
App::new()
.add_plugins((
DefaultPlugins
.set(RenderPlugin {
synchronous_pipeline_compilation: true,
..default()
})
.build()
.disable::<WinitPlugin>(),
ScheduleRunnerPlugin::run_loop(Duration::from_secs_f64(1. / 24.)),
post_process::PostProcessPlugin,
ImageExportPlugin,
))
.insert_resource(ClearColor(Color::linear_rgba(0.0, 0.0, 0.0, 1.0)))
.add_systems(Startup, setup_scene_system)
.add_systems(Update, rotate_cube)
.run();
}
#[rustfmt::skip]
const DATA: &[u8] = &[
0b11100000, 0b00000000,
0b11111111, 0b00000000,
0b10000000, 0b11000000,
0b10011111, 0b00111000,
0b10000000, 0b00000110,
0b01111111, 0b11111111,
];
fn setup_scene_system(
mut commands: Commands,
meshes: ResMut<Assets<Mesh>>,
materials: ResMut<Assets<StandardMaterial>>,
mut images: ResMut<Assets<Image>>,
mut export_sources: ResMut<Assets<ImageExportSource>>,
) {
shared::spawn_3d_scene(commands.reborrow(), meshes, materials);
let mut image = Image::new_target_texture(
160,
43,
TextureFormat::Rgba8Unorm,
Some(TextureFormat::Rgba8UnormSrgb),
);
image.texture_descriptor.usage = TextureUsages::TEXTURE_BINDING
| TextureUsages::COPY_SRC
| TextureUsages::COPY_DST
| TextureUsages::RENDER_ATTACHMENT;
let image_handle = images.add(image);
commands.spawn((
Camera3d::default(),
Camera::default(),
RenderTarget::Image(image_handle.clone().into()),
Transform::from_xyz(0.0, 1.0, 2.5).looking_at(Vec3::ZERO, Vec3::Y),
PostProcessSettings { scale: 1.0 },
));
commands.spawn((ImageExport(export_sources.add(image_handle)),));
}
fn rotate_cube(mut cubes: Query<(&mut Transform, &Spinner)>, timer: Res<Time>) {
for (mut transform, _cube) in &mut cubes {
transform.rotate_y(0.25 * TAU * timer.delta_secs());
}
}