Skip to content

Commit 6c21853

Browse files
committed
Refactor code to allow construction of game in binary
1 parent 389e86c commit 6c21853

File tree

9 files changed

+264
-207
lines changed

9 files changed

+264
-207
lines changed

Cargo.toml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,15 @@ anyhow = "1.0"
1111
bytemuck = "1.3"
1212
failure = "0.1"
1313
futures = "0.3"
14-
winit = "0.22"
15-
rand = "0.7.3"
14+
winit = "0.24"
1615
shaderc = "0.6"
1716
serde = { version = "1", features = ["derive"], optional = true }
18-
gltf = { version = "0.15", default-features = false, features = ["utils"] }
1917
thiserror = "1.0"
2018
base64 = "0.12"
21-
glam = "0.9"
19+
glam = "0.12"
2220
log = "0.4"
2321
wgpu = "0.7"
24-
hecs = "0.2"
25-
legion = "0.3"
26-
bevy_ecs = "0.2"
22+
hecs = "0.3"
2723
image = "0.23"
2824
num-traits = "0.2"
2925

src/app.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::renderer::Renderer;
2-
1+
use crate::{renderer::Renderer, Game};
32
use winit::{
43
dpi::LogicalSize,
54
event::{self, WindowEvent},
@@ -80,7 +79,7 @@ impl App {
8079
}
8180
}
8281

83-
pub fn run(mut self, event_loop: EventLoop<()>) {
82+
pub fn run(mut self, event_loop: EventLoop<()>, mut game: Game<'static>) {
8483
let sc_desc = wgpu::SwapChainDescriptor {
8584
usage: wgpu::TextureUsage::RENDER_ATTACHMENT,
8685
width: self.size.width,
@@ -91,8 +90,6 @@ impl App {
9190
};
9291
let mut swap_chain = self.device.create_swap_chain(&self.surface, &sc_desc);
9392

94-
let mut game = crate::game::Game::new();
95-
9693
let mut renderer = Renderer::init(&sc_desc, &mut self.device, &self.queue);
9794

9895
log::info!("Entering render loop...");

src/bin/game.rs

Lines changed: 149 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,155 @@
11
extern crate erlking;
22

3-
use erlking::app::App;
4-
use winit::event_loop::EventLoop;
3+
use cgmath::{Deg, Quaternion, Rotation3, Vector3};
4+
use erlking::{
5+
app::App, camera::ParallaxCamera, ActiveCamera, Game, KeyboardInput, Position, Rotation, Scale,
6+
Sprite,
7+
};
8+
use glam::Vec3;
9+
use hecs::World;
10+
use std::time::Duration;
11+
use winit::{
12+
event::{ElementState, VirtualKeyCode},
13+
event_loop::EventLoop,
14+
};
15+
16+
#[derive(Clone, Copy)]
17+
struct MoveSpeed(f32);
518

619
fn main() {
720
let event_loop = EventLoop::new();
8-
let app = futures::executor::block_on(App::new("erlking", &event_loop));
9-
app.run(event_loop);
21+
let app = futures::executor::block_on(App::new("parallax-demo", &event_loop));
22+
let mut parallax_demo = Game::new();
23+
24+
let movespeed = MoveSpeed(10.0);
25+
26+
let player = (
27+
Position(cgmath::Vector3::new(0.0, 0.0, 20.0)),
28+
Rotation(Quaternion::from_axis_angle(
29+
Vector3::new(0.0, 1.0, 0.0),
30+
Deg(0.0),
31+
)),
32+
Scale(1),
33+
KeyboardInput(None),
34+
Sprite("player".to_string()),
35+
movespeed,
36+
);
37+
38+
let camera = (
39+
ParallaxCamera::new(
40+
Vec3::new(0.0, 3.0, 0.0),
41+
Vec3::new(0.0, 0.0, 1.0),
42+
1.0,
43+
0.1,
44+
500.0,
45+
),
46+
KeyboardInput(None),
47+
ActiveCamera,
48+
movespeed,
49+
);
50+
51+
let apple = (
52+
Position(cgmath::Vector3::new(-2.0, 0.0, 30.0)),
53+
Rotation(Quaternion::from_axis_angle(
54+
Vector3::new(0.0, 1.0, 0.0),
55+
Deg(0.0),
56+
)),
57+
Scale(1),
58+
Sprite("apple".to_string()),
59+
);
60+
61+
let ashberry = (
62+
Position(cgmath::Vector3::new(2.0, 0.0, 30.0)),
63+
Rotation(Quaternion::from_axis_angle(
64+
Vector3::new(0.0, 1.0, 0.0),
65+
Deg(0.0),
66+
)),
67+
Scale(1),
68+
Sprite("ashberry".to_string()),
69+
);
70+
71+
let baobab = (
72+
Position(cgmath::Vector3::new(3.0, 0.0, 55.0)),
73+
Rotation(Quaternion::from_axis_angle(
74+
Vector3::new(0.0, 1.0, 0.0),
75+
Deg(0.0),
76+
)),
77+
Scale(1),
78+
Sprite("baobab".to_string()),
79+
);
80+
81+
let beech = (
82+
Position(cgmath::Vector3::new(-3.5, 0.0, 95.0)),
83+
Rotation(Quaternion::from_axis_angle(
84+
Vector3::new(0.0, 1.0, 0.0),
85+
Deg(0.0),
86+
)),
87+
Scale(1),
88+
Sprite("beech".to_string()),
89+
);
90+
parallax_demo.spawn_entity(player);
91+
parallax_demo.spawn_entity(apple);
92+
parallax_demo.spawn_entity(ashberry);
93+
parallax_demo.spawn_entity(baobab);
94+
parallax_demo.spawn_entity(beech);
95+
parallax_demo.spawn_entity(camera);
96+
97+
parallax_demo.add_system(&move_player);
98+
99+
app.run(event_loop, parallax_demo);
100+
}
101+
102+
fn move_player(world: &World, dt: Duration) {
103+
let mut q = world.query::<(&KeyboardInput, &mut Position, &MoveSpeed)>();
104+
105+
for (_, (key, pos, speed)) in q.iter() {
106+
if let Some(input) = key.0 {
107+
let dx = Vector3::new(speed.0 * dt.as_secs_f32(), 0.0, 0.0);
108+
match input {
109+
winit::event::KeyboardInput {
110+
state: ElementState::Pressed,
111+
virtual_keycode: Some(VirtualKeyCode::Left),
112+
..
113+
} => {
114+
pos.0 -= dx;
115+
}
116+
winit::event::KeyboardInput {
117+
state: ElementState::Pressed,
118+
virtual_keycode: Some(VirtualKeyCode::Right),
119+
..
120+
} => {
121+
pos.0 += dx;
122+
}
123+
_ => (),
124+
}
125+
}
126+
}
127+
let mut q = world.query::<(
128+
&ActiveCamera,
129+
&mut ParallaxCamera,
130+
&KeyboardInput,
131+
&MoveSpeed,
132+
)>();
133+
134+
let (_, (_, cam, key, speed)) = q.iter().next().expect("active camera is preset");
135+
if let Some(input) = key.0 {
136+
let dx = Vec3::new(speed.0 * dt.as_secs_f32(), 0.0, 0.0);
137+
match input {
138+
winit::event::KeyboardInput {
139+
state: ElementState::Pressed,
140+
virtual_keycode: Some(VirtualKeyCode::Left),
141+
..
142+
} => {
143+
cam.eye -= dx;
144+
}
145+
winit::event::KeyboardInput {
146+
state: ElementState::Pressed,
147+
virtual_keycode: Some(VirtualKeyCode::Right),
148+
..
149+
} => {
150+
cam.eye += dx;
151+
}
152+
_ => (),
153+
}
154+
}
10155
}

src/camera.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ use std::f32;
55
pub const SPRITE_SCALING_FACTOR: u8 = 2;
66

77
pub trait Camera {
8-
fn generate_matrix(&self) -> Mat4;
8+
fn generate_matrix(&self) -> CameraUniform;
99
}
1010

1111
#[derive(Clone, Copy)]
1212
pub struct ParallaxCamera {
1313
pub eye: glam::Vec3,
14-
pub look_to: glam::Vec3,
14+
pub look_dir: glam::Vec3,
1515
pub fov_y: f32,
1616
pub near: f32,
1717
pub far: f32,
@@ -21,7 +21,7 @@ impl ParallaxCamera {
2121
pub fn new(eye: glam::Vec3, look_to: glam::Vec3, fov_y: f32, near: f32, far: f32) -> Self {
2222
ParallaxCamera {
2323
eye,
24-
look_to,
24+
look_dir: look_to,
2525
fov_y,
2626
near,
2727
far,
@@ -36,7 +36,7 @@ impl ParallaxCamera {
3636
let mx_ortho =
3737
glam::Mat4::orthographic_lh(-w / 2.0, w / 2.0, -h / 2.0, h / 2.0, self.near, self.far);
3838

39-
let mx_view = look_to_lh(self.eye, self.look_to, Vec3::unit_y());
39+
let mx_view = look_to_lh(self.eye, self.look_dir, Vec3::unit_y());
4040

4141
mx_ortho * mx_view
4242
}
@@ -49,12 +49,14 @@ impl ParallaxCamera {
4949
self.far,
5050
);
5151

52-
let mx_view = look_to_lh(self.eye, self.look_to, Vec3::unit_y());
52+
let mx_view = look_to_lh(self.eye, self.look_dir, Vec3::unit_y());
5353

5454
mx_perspective * mx_view
5555
}
56+
}
5657

57-
pub fn camera_uniform(&self) -> CameraUniform {
58+
impl Camera for ParallaxCamera {
59+
fn generate_matrix(&self) -> CameraUniform {
5860
let ortho = *self.generate_ortho().as_ref();
5961
let persp = *self.generate_perspective().as_ref();
6062
CameraUniform { ortho, persp }

0 commit comments

Comments
 (0)