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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//! The core engine framework.

use std::sync::Arc;

use input::InputHandler;
use rayon::ThreadPool;
use renderer::Config as DisplayConfig;
use renderer::PipelineBuilder;
use shred::{Resource, ResourceId};
#[cfg(feature = "profiler")]
use thread_profiler::{register_thread_with_profiler, write_profile};
use winit::{Event, EventsLoop};

use assets::{Asset, Loader, Store};
use ecs::{Component, Dispatcher, DispatcherBuilder, System, World};
use engine::Engine;
use error::{Error, Result};
use state::{State, StateMachine};
use timing::{Stopwatch, Time};

/// User-friendly facade for building games. Manages main loop.
#[derive(Derivative)]
#[derivative(Debug)]
pub struct Application<'a, 'b> {
    /// The `engine` struct, holding world and thread pool.
    #[derivative(Debug = "ignore")]
    pub engine: Engine,

    #[derivative(Debug = "ignore")]
    dispatcher: Dispatcher<'a, 'b>,
    #[derivative(Debug = "ignore")]
    events: EventsLoop,
    states: StateMachine<'a>,
    time: Time,
    timer: Stopwatch,
}

impl<'a, 'b> Application<'a, 'b> {
    /// Creates a new Application with the given initial game state.
    pub fn new<S: State + 'a>(initial_state: S) -> Result<Application<'a, 'b>> {
        ApplicationBuilder::new(initial_state)?.build()
    }

    /// Builds a new Application with the given settings.
    pub fn build<S>(initial_state: S) -> Result<ApplicationBuilder<'a, 'b, S>>
    where
        S: State + 'a,
    {
        ApplicationBuilder::new(initial_state)
    }

    /// Starts the application and manages the game loop.
    pub fn run(&mut self) {
        self.initialize();

        while self.states.is_running() {
            self.timer.restart();
            self.advance_frame();
            self.timer.stop();
            self.time.delta_time = self.timer.elapsed();
        }

        self.shutdown();
    }

    /// Sets up the application.
    fn initialize(&mut self) {
        #[cfg(feature = "profiler")]
        profile_scope!("initialize");

        self.engine.world.add_resource(self.time.clone());
        self.states.start(&mut self.engine);
    }

    /// Advances the game world by one tick.
    fn advance_frame(&mut self) {
        {
            let mut time = self.engine.world.write_resource::<Time>();
            time.delta_time = self.time.delta_time;
            time.fixed_step = self.time.fixed_step;
            time.last_fixed_update = self.time.last_fixed_update;
        }

        {
            let engine = &mut self.engine;
            let states = &mut self.states;
            if engine.world.res.has_value(
                ResourceId::new::<InputHandler>(),
            )
            {
                engine
                    .world
                    .write_resource::<InputHandler>()
                    .advance_frame();
            }
            #[cfg(feature = "profiler")]
            profile_scope!("handle_event");

            self.events.poll_events(|event| {
                if engine.world.res.has_value(
                    ResourceId::new::<InputHandler>(),
                )
                {
                    let mut input = engine.world.write_resource::<InputHandler>();
                    if let Event::WindowEvent { ref event, .. } = event {
                        input.send_event(&event);
                    }
                }
                states.handle_event(engine, event);
            });
        }
        {
            #[cfg(feature = "profiler")]
            profile_scope!("fixed_update");
            if self.time.last_fixed_update.elapsed() >= self.time.fixed_step {
                self.states.fixed_update(&mut self.engine);
                self.time.last_fixed_update += self.time.fixed_step;
            }

            #[cfg(feature = "profiler")]
            profile_scope!("update");
            self.states.update(&mut self.engine);
        }

        #[cfg(feature = "profiler")]
        profile_scope!("dispatch");
        self.dispatcher.dispatch(&mut self.engine.world.res);

        #[cfg(feature="profiler")]
        profile_scope!("maintain");
        self.engine.world.maintain();
    }

    /// Cleans up after the quit signal is received.
    fn shutdown(&mut self) {
        // Placeholder.
    }
}

#[cfg(feature = "profiler")]
impl<'a, 'b> Drop for Application<'a, 'b> {
    fn drop(&mut self) {
        // TODO: Specify filename in config.
        let path = format!("{}/thread_profile.json", env!("CARGO_MANIFEST_DIR"));
        write_profile(path.as_str());
    }
}

/// Helper builder for Applications.
pub struct ApplicationBuilder<'a, 'b, T: State + 'a> {
    // config: Config,
    disp_builder: DispatcherBuilder<'a, 'b>,
    initial_state: T,
    world: World,
    pool: Arc<ThreadPool>,
    /// Allows to create `RenderSystem`
    // TODO: Come up with something clever
    pub events: EventsLoop,
}

impl<'a, 'b, T: State + 'a> ApplicationBuilder<'a, 'b, T> {
    /// Creates a new ApplicationBuilder with the given initial game state and
    /// display configuration.
    pub fn new(initial_state: T) -> Result<Self> {
        use num_cpus;
        use rayon::Configuration;

        let num_cores = num_cpus::get();
        let cfg = Configuration::new().num_threads(num_cores);
        let pool = ThreadPool::new(cfg).map(|p| Arc::new(p)).map_err(|_| {
            Error::Application
        })?;
        let mut world = World::new();
        let base_path = format!("{}/resources", env!("CARGO_MANIFEST_DIR"));
        world.add_resource(Loader::new(base_path, pool.clone()));

        Ok(ApplicationBuilder {
            disp_builder: DispatcherBuilder::new(),
            initial_state: initial_state,
            world: world,
            events: EventsLoop::new(),
            pool: pool,
        })
    }

    /// Registers a given component type.
    pub fn register<C: Component>(mut self) -> Self {
        self.world.register::<C>();
        self
    }

    /// Adds an ECS resource which can be accessed from systems.
    pub fn add_resource<R>(mut self, res: R) -> Self
    where
        R: Resource,
    {
        self.world.add_resource(res);

        self
    }

    /// Inserts a barrier which assures that all systems added before the
    /// barrier are executed before the ones after this barrier.
    ///
    /// Does nothing if there were no systems added since the last call to
    /// `add_barrier()`. Thread-local systems are not affected by barriers;
    /// they're always executed at the end.
    pub fn add_barrier(mut self) -> Self {
        self.disp_builder = self.disp_builder.add_barrier();
        self
    }

    /// Adds a given system `sys`, assigns it the string identifier `name`,
    /// and marks it dependent on systems `dep`.
    /// Note: all dependencies should be added before you add depending system
    /// If you want to register systems which can not be specified as dependencies,
    /// you can use "" as their name, which will not panic (using another name twice will).
    pub fn with<S>(mut self, sys: S, name: &str, dep: &[&str]) -> Self
    where
        for<'c> S: System<'c> + Send + 'a + 'b,
    {
        self.disp_builder = self.disp_builder.add(sys, name, dep);
        self
    }

    /// Adds a given thread-local system `sys` to the Application.
    ///
    /// All thread-local systems are executed sequentially after all
    /// non-thread-local systems.
    pub fn with_thread_local<S>(mut self, sys: S) -> Self
    where
        for<'c> S: System<'c> + 'a + 'b,
    {
        self.disp_builder = self.disp_builder.add_thread_local(sys);
        self
    }

    /// Automatically registers components, adds resources and the rendering system.
    pub fn with_renderer(
        mut self,
        pipe: PipelineBuilder,
        config: Option<DisplayConfig>,
    ) -> Result<Self> {
        use ecs::SystemExt;
        use ecs::rendering::RenderSystem;
        let render_sys = RenderSystem::build((&self.events, pipe, config), &mut self.world)?;
        self = self.with_thread_local(render_sys);

        Ok(
            self.register_mesh_asset()
                .register_texture_asset()
                .register_material_not_yet_asset(),
        )


    }

    /// Add asset loader to resources
    pub fn add_store<I, S>(self, name: I, store: S) -> Self
    where
        I: Into<String>,
        S: Store + Send + Sync + 'static,
    {
        {
            let mut loader = self.world.write_resource::<Loader>();
            loader.add_store(name, store);
        }
        self
    }

    /// Register new context within the loader
    pub fn register_asset<A, F>(mut self, make_context: F) -> Self
    where
        A: Component + Asset + Clone + Send + Sync + 'static,
        F: FnOnce(&mut World) -> A::Context,
    {
        use assets::AssetFuture;
        use specs::common::{Merge, Errors};

        self.world.register::<A>();
        self.world.register::<AssetFuture<A>>();
        self.world.add_resource(Errors::new());
        self = self.with(Merge::<AssetFuture<A>>::new(), "", &[]);
        {
            let context = make_context(&mut self.world);
            let mut loader = self.world.write_resource::<Loader>();
            loader.register(context);
        }
        self
    }

    /// Builds the Application and returns the result.
    pub fn build(self) -> Result<Application<'a, 'b>> {

        #[cfg(feature = "profiler")] register_thread_with_profiler("Main".into());
        #[cfg(feature = "profiler")]
        profile_scope!("new");

        Ok(Application {
            engine: Engine::new(self.pool.clone(), self.world),
            // config: self.config,
            states: StateMachine::new(self.initial_state),
            events: self.events,
            dispatcher: self.disp_builder.with_pool(self.pool).build(),
            time: Time::default(),
            timer: Stopwatch::new(),
        })
    }




    /// Register new context within the loader
    fn register_mesh_asset(self) -> Self {
        use ecs::rendering::{MeshComponent, MeshContext, Factory};
        self.register_asset::<MeshComponent, _>(|world| {
            let factory = world.read_resource::<Factory>();
            MeshContext::new((&*factory).clone())
        })
    }

    /// Register new context within the loader
    fn register_material_not_yet_asset(mut self) -> Self {
        use assets::AssetFuture;
        use ecs::rendering::MaterialComponent;
        use specs::common::Merge;

        self.world.register::<MaterialComponent>();
        self.world.register::<AssetFuture<MaterialComponent>>();
        self = self.with(Merge::<AssetFuture<MaterialComponent>>::new(), "", &[]);
        self
    }

    /// Register new context within the loader
    fn register_texture_asset(self) -> Self {
        use ecs::rendering::{TextureComponent, TextureContext, Factory};
        self.register_asset::<TextureComponent, _>(|world| {
            let factory = world.read_resource::<Factory>();
            TextureContext::new((&*factory).clone())
        })
    }
}