add field and themeing

This commit is contained in:
Schrottkatze 2024-02-23 08:27:17 +01:00
parent c2cb73fbc7
commit b8d27eb909
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc
2 changed files with 49 additions and 8 deletions

View file

@ -1,4 +1,7 @@
const GAME_SIZE: (u32, u32) = (1200, 800); const GAME_SIZE: (u32, u32) = (1200, 800);
const BORDER_WIDTH: u32 = 10;
const FG: u32 = 0xebdbb2;
const BG: u32 = 0x282828;
use std::{ use std::{
collections::HashMap, collections::HashMap,
num::NonZeroU32, num::NonZeroU32,
@ -81,6 +84,44 @@ impl Engine {
Event::NewEvents(StartCause::ResumeTimeReached { .. }) => { Event::NewEvents(StartCause::ResumeTimeReached { .. }) => {
window.request_redraw(); window.request_redraw();
} }
Event::WindowEvent {
window_id,
event: WindowEvent::Resized(PhysicalSize { width, height }),
} => {
if window_id == window.id() {
if let (Some(width), Some(height)) =
(NonZeroU32::new(width), NonZeroU32::new(height))
{
surface.resize(width, height).unwrap();
let mut buffer = surface.buffer_mut().unwrap();
buffer.fill(BG);
let mut ctx =
render::RenderCtx::new(buffer, (width, height), GAME_SIZE);
// top
ctx.rect(0, 0, GAME_SIZE.0, BORDER_WIDTH, FG);
// left
ctx.rect(
0,
BORDER_WIDTH,
BORDER_WIDTH,
GAME_SIZE.1 - BORDER_WIDTH * 2,
FG,
);
// right
ctx.rect(
GAME_SIZE.0 - BORDER_WIDTH,
BORDER_WIDTH,
BORDER_WIDTH,
GAME_SIZE.1 - BORDER_WIDTH * 2,
FG,
);
// bottom
ctx.rect(0, GAME_SIZE.1 - BORDER_WIDTH, GAME_SIZE.0, 10, FG);
}
}
}
Event::WindowEvent { Event::WindowEvent {
window_id, window_id,
event: WindowEvent::RedrawRequested, event: WindowEvent::RedrawRequested,
@ -182,7 +223,7 @@ mod render;
pub mod objs { pub mod objs {
use std::rc::Rc; use std::rc::Rc;
use super::render::RenderCtx; use super::{render::RenderCtx, BG, FG};
pub struct World { pub struct World {
objects: Vec<Rc<dyn MovingObject>>, objects: Vec<Rc<dyn MovingObject>>,
@ -318,7 +359,7 @@ pub mod objs {
fn display(&self, ctx: &mut RenderCtx<'_, '_>) { fn display(&self, ctx: &mut RenderCtx<'_, '_>) {
let Position { x, y } = self.position(); let Position { x, y } = self.position();
let Size { width, height } = self.size(); let Size { width, height } = self.size();
ctx.rect(x, y, width, height, 0xffffff); ctx.rect(x, y, width, height, FG);
} }
} }
@ -342,7 +383,7 @@ pub mod objs {
} = self.previous_pos(); } = self.previous_pos();
let Size { width, height } = self.size(); let Size { width, height } = self.size();
ctx.rect(prev_x, prev_y, width, height, 0x000000); ctx.rect(prev_x, prev_y, width, height, BG);
self.display(ctx); self.display(ctx);
} }
} }

View file

@ -7,13 +7,13 @@ use engine::{
fn main() { fn main() {
let mut engine = Engine::new(); let mut engine = Engine::new();
let _ = engine.insert_into_world(Rc::new(MovingRect::new(0, 0, 50, 100))); // let _ = engine.insert_into_world(Rc::new(MovingRect::new(0, 0, 50, 100)));
engine engine
.set_render_fn(|ctx, world, timer| { .set_render_fn(|ctx, world, timer| {
println!("t: {}", timer.game_time_passed()); // println!("t: {}", timer.game_time_passed());
let obj = Rc::get_mut(world.get_mut(0)).unwrap(); // let obj = Rc::get_mut(world.get_mut(0)).unwrap();
obj.move_obj(1, 0); // obj.move_obj(1, 0);
obj.draw_move(ctx) // obj.draw_move(ctx)
// Rect::square((timer.game_time_passed() * 20.) as u32, 0, 200).display(ctx); // Rect::square((timer.game_time_passed() * 20.) as u32, 0, 200).display(ctx);
}) })
.run(); .run();