1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
#![cfg_attr(all(windows, not(debug_assertions)), windows_subsystem = "windows")]
use std::io::Cursor;
use std::path::Path;
use engine::asset::Assets;
use engine::camera::fly::{
Extension as FlyCameraExtension,
Fly as FlyCamera,
Options as FlyCameraOptions,
};
use engine::camera::{
Active as ActiveCamera,
Camera,
Controllable as ControllableCamera,
};
use engine::color::Color;
use engine::data_types::dimens::Dimens3;
use engine::ecs::actions::Actions;
use engine::ecs::error::Context as _;
use engine::ecs::phase::START as START_PHASE;
use engine::ecs::query::term::With;
use engine::ecs::sole::Single;
use engine::ecs::system::initializable::Initializable;
use engine::ecs::system::Into;
use engine::ecs::Query;
use engine::image::{Format as ImageFormat, Image};
use engine::input::keyboard::Keyboard;
use engine::input::Extension as InputExtension;
use engine::lighting::{AttenuationParams, GlobalLight, PointLight};
use engine::material::{Flags as MaterialFlags, Material};
use engine::mesh::cube::{
create as cube_mesh_create,
CreationSpec as CubeMeshCreationSpec,
};
use engine::model::{Materials as ModelMaterials, Model, Spec as ModelSpec};
use engine::rendering::{
Extension as RenderingExtension,
GraphicsProperties,
TargetWindow as RenderingTargetWindow,
};
use engine::transform::WorldPosition;
use engine::ui::dear_imgui::{
Context as DearImguiContext,
Extension as DearImguiExtension,
TargetWindow as DearImguiTargetWindow,
};
use engine::vector::Vec3;
use engine::windowing::window::{
CreationAttributes as WindowCreationAttributes,
CursorGrabMode as WindowCursorGrabMode,
Icon as WindowIcon,
Window,
};
use engine::{Engine, Error as EngineError};
use tracing::level_filters::LevelFilter;
use tracing_subscriber::fmt::time::ChronoLocal;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::EnvFilter;
const YELLOW: Color<f32> = Color {
red: 0.988235294118,
green: 0.941176470588,
blue: 0.0274509803922,
};
const RESOURCE_DIR: &str = "res";
fn main() -> Result<(), EngineError>
{
configure_logging()?;
Engine::new()
.with_extension(engine::windowing::Extension::default())
.with_extension(
RenderingExtension::builder()
.graphics_props(
GraphicsProperties::builder()
.debug(cfg!(debug_assertions))
.build(),
)
.build(),
)
.with_extension(InputExtension::default())
.with_extension(FlyCameraExtension(FlyCameraOptions {
mouse_sensitivity: 0.2,
}))
.with_extension(DearImguiExtension::default())
.with_extension(Application)
.start();
Ok(())
}
struct Application;
impl engine::ecs::extension::Extension for Application
{
fn collect(self, mut collector: engine::ecs::extension::Collector<'_>)
{
let _ = collector.add_sole(GlobalLight::default());
collector.add_system(*START_PHASE, init);
collector.add_system(*engine::ecs::phase::UPDATE, toggle_ui);
collector.add_system(
*engine::ecs::phase::UPDATE,
engine::ui::view::world::show
.into_system()
.initialize((engine::ui::view::world::State::default(),)),
);
}
}
fn init(mut assets: Single<Assets>, mut actions: Actions) -> Result<(), EngineError>
{
let assets = assets.get_mut()?;
actions.spawn_named(
"Main window",
(
WindowCreationAttributes::default()
.with_title("Game")
.with_icon(Some(WindowIcon::Image(
Image::from_reader(
Cursor::new(include_bytes!("../res/icon.ico")),
ImageFormat::Ico,
)
.with_context(|| "Invalid icon image")?,
)))
.with_cursor_visible(false)
.with_cursor_grab_mode(WindowCursorGrabMode::Locked),
RenderingTargetWindow,
DearImguiTargetWindow,
),
);
actions.spawn_named(
"Main camera",
(
Camera::default(),
WorldPosition {
position: Vec3 { x: 0.0, y: 0.0, z: 3.0 },
},
ActiveCamera,
FlyCamera::default(),
),
);
actions.spawn_named(
"Sun cube",
(
PointLight::builder()
.diffuse(YELLOW)
.attenuation_params(AttenuationParams {
linear: 0.045,
quadratic: 0.0075,
..Default::default()
})
.build(),
WorldPosition::from(Vec3 { x: -6.0, y: 3.0, z: 3.0 }),
Model::new(assets.store_with_name_with("light_cube", |assets| {
ModelSpec::builder()
.mesh(
assets.store_with_name(
"light_cube_mesh",
cube_mesh_create(
CubeMeshCreationSpec::builder()
.dimens(Dimens3::from(2.0))
.build(),
),
),
)
.materials(ModelMaterials::direct([(
"surface",
assets.store_with_name(
"light_cube_surface_mat",
Material::builder().ambient(YELLOW * 5.0).build(),
),
)]))
.material_name("surface")
.build()
})),
MaterialFlags::builder().use_ambient_color(true).build(),
),
);
actions.spawn_named(
"Big teapot",
(
Model::new(
assets.load::<ModelSpec>(Path::new(RESOURCE_DIR).join("teapot.obj")),
),
WorldPosition::from(Vec3 { x: 1.6, y: 0.0, z: 0.0 }),
),
);
Ok(())
}
fn configure_logging() -> Result<(), EngineError>
{
if !cfg!(debug_assertions) {
return Ok(());
}
#[cfg(windows)]
nu_ansi_term::enable_ansi_support()
.map_err(EngineError::from_system_defined_win32_error)
.with_context(|| "Failed to enable ANSI support")?;
tracing_subscriber::registry()
.with(
tracing_subscriber::fmt::layer()
.with_timer(ChronoLocal::new("%T%.6f".to_string())),
)
.with(EnvFilter::try_new("trace,winit=warn")?)
.with(
EnvFilter::builder()
.with_default_directive(LevelFilter::DEBUG.into())
.from_env()?,
)
.init();
Ok(())
}
fn toggle_ui(
window_query: Query<(&mut Window,), (With<DearImguiTargetWindow>,)>,
camera_query: Query<(&mut ControllableCamera,), (With<Camera>, With<ActiveCamera>)>,
mut dear_imgui_context: Single<DearImguiContext>,
keyboard: Single<Keyboard>,
) -> Result<(), EngineError>
{
let dear_imgui_context = dear_imgui_context.get_mut()?;
let keyboard = keyboard.get()?;
let Some((mut window,)) = window_query.iter().next() else {
return Ok(());
};
let Some((mut controllable_camera,)) = camera_query.iter().next() else {
return Ok(());
};
if keyboard.just_pressed(engine::input::keyboard::Key::Escape) {
if dear_imgui_context.enabled {
dear_imgui_context.enabled = false;
window.cursor_visible = false;
window.cursor_grab_mode = WindowCursorGrabMode::Locked;
controllable_camera.control_enabled = true;
} else {
dear_imgui_context.enabled = true;
window.cursor_visible = true;
window.cursor_grab_mode = WindowCursorGrabMode::None;
controllable_camera.control_enabled = false;
}
window.set_changed();
}
Ok(())
}
|