move constants to main and add resize fn

This commit is contained in:
Schrottkatze 2024-02-23 08:42:03 +01:00
parent 11f9fecba1
commit a1346780ab
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc
4 changed files with 44 additions and 28 deletions

View file

@ -1,3 +1,5 @@
use crate::{BG, GAME_SIZE};
use self::objs::{obj_traits::MovingObject, World}; use self::objs::{obj_traits::MovingObject, World};
use std::{ use std::{
num::NonZeroU32, num::NonZeroU32,
@ -18,16 +20,12 @@ pub mod objs;
mod render; mod render;
mod timer; 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); type RenderFn = fn(&mut render::RenderCtx<'_, '_>, &mut World, &timer::GameTimer);
// core game engine struct // core game engine struct
pub struct Engine { pub struct Engine {
event_loop: EventLoop<()>, event_loop: EventLoop<()>,
render_fn: RenderFn, render_fn: RenderFn,
resize_fn: RenderFn,
world: World, world: World,
} }
@ -38,6 +36,7 @@ impl Engine {
Self { Self {
event_loop, event_loop,
render_fn: |_, _, _| {}, render_fn: |_, _, _| {},
resize_fn: |_, _, _| {},
world: World::new(), world: World::new(),
} }
} }
@ -52,12 +51,19 @@ impl Engine {
self 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 // runs the game and consumes self, this will finish the process
pub fn run(self) -> ! { pub fn run(self) -> ! {
let Self { let Self {
event_loop, event_loop,
mut world, mut world,
render_fn, render_fn,
resize_fn,
} = self; } = self;
// set up window // set up window
@ -99,27 +105,7 @@ impl Engine {
let mut ctx = let mut ctx =
render::RenderCtx::new(buffer, (width, height), GAME_SIZE); render::RenderCtx::new(buffer, (width, height), GAME_SIZE);
resize_fn(&mut ctx, &mut 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);
} }
} }
} }

View file

@ -2,7 +2,7 @@ use std::rc::Rc;
use self::obj_traits::MovingObject; use self::obj_traits::MovingObject;
use super::{render::RenderCtx, BG, FG}; use super::render::RenderCtx;
pub mod geometry; pub mod geometry;
pub mod obj_traits; pub mod obj_traits;

View file

@ -1,4 +1,7 @@
use crate::engine::{render::RenderCtx, BG, FG}; use crate::{
engine::{render::RenderCtx, BG},
FG,
};
use super::geometry::{Position, Size}; use super::geometry::{Position, Size};

View file

@ -2,10 +2,37 @@ use std::rc::Rc;
use engine::Engine; 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() { 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_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| { .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();