From a1346780ab1524df809198c0df48f96686d94026 Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Fri, 23 Feb 2024 08:42:03 +0100 Subject: [PATCH] move constants to main and add resize fn --- src/engine.rs | 38 +++++++++++------------------------ src/engine/objs.rs | 2 +- src/engine/objs/obj_traits.rs | 5 ++++- src/main.rs | 27 +++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 28 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index c29f59b..bb4240b 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1,3 +1,5 @@ +use crate::{BG, GAME_SIZE}; + use self::objs::{obj_traits::MovingObject, World}; use std::{ num::NonZeroU32, @@ -18,16 +20,12 @@ pub mod objs; mod render; mod timer; -const GAME_SIZE: (u32, u32) = (1200, 800); -const BORDER_WIDTH: u32 = 10; -const FG: u32 = 0xebdbb2; -const BG: u32 = 0x282828; - type RenderFn = fn(&mut render::RenderCtx<'_, '_>, &mut World, &timer::GameTimer); // core game engine struct pub struct Engine { event_loop: EventLoop<()>, render_fn: RenderFn, + resize_fn: RenderFn, world: World, } @@ -38,6 +36,7 @@ impl Engine { Self { event_loop, render_fn: |_, _, _| {}, + resize_fn: |_, _, _| {}, world: World::new(), } } @@ -52,12 +51,19 @@ impl Engine { self } + // sets the resize render function for the game + pub fn set_resize_fn(mut self, f: RenderFn) -> Self { + self.resize_fn = f; + self + } + // runs the game and consumes self, this will finish the process pub fn run(self) -> ! { let Self { event_loop, mut world, render_fn, + resize_fn, } = self; // set up window @@ -99,27 +105,7 @@ impl Engine { 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); + resize_fn(&mut ctx, &mut world, &timer); } } } diff --git a/src/engine/objs.rs b/src/engine/objs.rs index 3199951..fa62949 100644 --- a/src/engine/objs.rs +++ b/src/engine/objs.rs @@ -2,7 +2,7 @@ use std::rc::Rc; use self::obj_traits::MovingObject; -use super::{render::RenderCtx, BG, FG}; +use super::render::RenderCtx; pub mod geometry; pub mod obj_traits; diff --git a/src/engine/objs/obj_traits.rs b/src/engine/objs/obj_traits.rs index dabd454..4d0431f 100644 --- a/src/engine/objs/obj_traits.rs +++ b/src/engine/objs/obj_traits.rs @@ -1,4 +1,7 @@ -use crate::engine::{render::RenderCtx, BG, FG}; +use crate::{ + engine::{render::RenderCtx, BG}, + FG, +}; use super::geometry::{Position, Size}; diff --git a/src/main.rs b/src/main.rs index 1e11bf1..2d26fb4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,10 +2,37 @@ use std::rc::Rc; use engine::Engine; +const GAME_SIZE: (u32, u32) = (1200, 800); +const BORDER_WIDTH: u32 = 10; +const FG: u32 = 0xebdbb2; +const BG: u32 = 0x282828; + fn main() { let mut engine = Engine::new(); // let _ = engine.insert_into_world(Rc::new(MovingRect::new(0, 0, 50, 100))); engine + .set_resize_fn(|ctx, world, timer| { + // 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); + }) .set_render_fn(|ctx, world, timer| { // println!("t: {}", timer.game_time_passed()); // let obj = Rc::get_mut(world.get_mut(0)).unwrap();