From b8d27eb909f1ed67c2b9ba3a4b02b451f346d8d1 Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Fri, 23 Feb 2024 08:27:17 +0100 Subject: [PATCH] add field and themeing --- src/engine.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++--- src/main.rs | 10 +++++----- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index b102913..4872bbd 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1,4 +1,7 @@ const GAME_SIZE: (u32, u32) = (1200, 800); +const BORDER_WIDTH: u32 = 10; +const FG: u32 = 0xebdbb2; +const BG: u32 = 0x282828; use std::{ collections::HashMap, num::NonZeroU32, @@ -81,6 +84,44 @@ impl Engine { Event::NewEvents(StartCause::ResumeTimeReached { .. }) => { 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 { window_id, event: WindowEvent::RedrawRequested, @@ -182,7 +223,7 @@ mod render; pub mod objs { use std::rc::Rc; - use super::render::RenderCtx; + use super::{render::RenderCtx, BG, FG}; pub struct World { objects: Vec>, @@ -318,7 +359,7 @@ pub mod objs { fn display(&self, ctx: &mut RenderCtx<'_, '_>) { let Position { x, y } = self.position(); 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(); 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); } } diff --git a/src/main.rs b/src/main.rs index 7cb82eb..9ff4dde 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,13 +7,13 @@ use engine::{ fn main() { 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 .set_render_fn(|ctx, world, timer| { - println!("t: {}", timer.game_time_passed()); - let obj = Rc::get_mut(world.get_mut(0)).unwrap(); - obj.move_obj(1, 0); - obj.draw_move(ctx) + // println!("t: {}", timer.game_time_passed()); + // let obj = Rc::get_mut(world.get_mut(0)).unwrap(); + // obj.move_obj(1, 0); + // obj.draw_move(ctx) // Rect::square((timer.game_time_passed() * 20.) as u32, 0, 200).display(ctx); }) .run();