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 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);
}
}
}

View file

@ -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;

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};

View file

@ -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();