astrelis 0.2.4

A modular 2D/3D game engine framework
Documentation
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! High-level application builder for simplified game/app initialization.
//!
//! The ApplicationBuilder eliminates 35-50 lines of boilerplate by providing
//! a clean, declarative API for common app setup patterns.

use crate::plugin::{Plugin, PluginDyn, PluginGroup};
use crate::{Engine, EngineBuilder};

#[cfg(all(feature = "render", feature = "winit"))]
use astrelis_render::{WindowContextDescriptor, WindowManager};

#[cfg(feature = "winit")]
use astrelis_winit::{
    app::{App, AppCtx, run_app},
    window::{WindowDescriptor, WinitPhysicalSize},
};

/// Factory function type that creates an `App` from application context and engine.
#[cfg(feature = "winit")]
type AppFactory = Box<dyn FnOnce(&mut AppCtx, &Engine) -> Box<dyn App>>;

/// Internal plugin group that wraps stored plugins
struct StoredPlugins {
    plugins: std::cell::RefCell<Vec<Box<dyn PluginDyn>>>,
}

impl PluginGroup for StoredPlugins {
    fn name(&self) -> &'static str {
        "StoredPlugins"
    }

    fn plugins(&self) -> Vec<Box<dyn PluginDyn>> {
        self.plugins.borrow_mut().drain(..).collect()
    }
}

/// High-level builder for creating Astrelis applications.
///
/// The ApplicationBuilder provides a clean, declarative API for common app
/// initialization patterns, eliminating 35-50 lines of boilerplate.
///
/// # Example
///
/// ```ignore
/// use astrelis::prelude::*;
///
/// struct MyGame {
///     window_id: WindowId,
/// }
///
/// impl App for MyGame {
///     fn update(&mut self, ctx: &mut AppCtx, time: &FrameTime) {
///         // Game logic
///     }
///
///     fn render(&mut self, ctx: &mut AppCtx, window_id: WindowId, events: &mut EventBatch) {
///         // Rendering
///     }
/// }
///
/// fn main() {
///     ApplicationBuilder::new()
///         .with_title("My Game")
///         .with_size(1280, 720)
///         .add_plugins(DefaultPlugins)
///         .run(|ctx, engine| {
///             // Window already created! Get its ID:
///             let window_id = ctx.windows().keys().next().copied().unwrap();
///             MyGame { window_id }
///         });
/// }
/// ```
pub struct ApplicationBuilder {
    title: String,
    size: (u32, u32),
    plugins: Vec<Box<dyn PluginDyn>>,
    #[cfg(all(feature = "render", feature = "winit"))]
    window_descriptor: Option<WindowContextDescriptor>,
    create_window: bool,
}

impl Default for ApplicationBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl ApplicationBuilder {
    /// Creates a new ApplicationBuilder with default settings.
    ///
    /// Defaults:
    /// - Title: "Astrelis Application"
    /// - Size: 1280x720
    /// - Plugins: None (add with `add_plugin` or `add_plugins`)
    /// - Window: Automatically created
    pub fn new() -> Self {
        Self {
            title: "Astrelis Application".to_string(),
            size: (1280, 720),
            plugins: Vec::new(),
            #[cfg(all(feature = "render", feature = "winit"))]
            window_descriptor: None,
            create_window: true,
        }
    }

    /// Sets the window title.
    ///
    /// # Example
    ///
    /// ```ignore
    /// # use astrelis::ApplicationBuilder;
    /// ApplicationBuilder::new()
    ///     .with_title("My Game");
    /// ```
    pub fn with_title(mut self, title: impl Into<String>) -> Self {
        self.title = title.into();
        self
    }

    /// Sets the window size in logical pixels.
    ///
    /// # Example
    ///
    /// ```ignore
    /// # use astrelis::ApplicationBuilder;
    /// ApplicationBuilder::new()
    ///     .with_size(1920, 1080);
    /// ```
    pub fn with_size(mut self, width: u32, height: u32) -> Self {
        self.size = (width, height);
        self
    }

    /// Adds a plugin to the engine.
    ///
    /// Plugins are initialized in the order they are added, with dependency
    /// ordering handled automatically.
    ///
    /// # Example
    ///
    /// ```ignore
    /// # use astrelis::ApplicationBuilder;
    /// # use astrelis::FnPlugin;
    /// ApplicationBuilder::new()
    ///     .add_plugin(FnPlugin::new("setup", |resources| {
    ///         // Plugin initialization
    ///     }));
    /// ```
    pub fn add_plugin(mut self, plugin: impl Plugin + 'static) -> Self {
        self.plugins.push(Box::new(plugin));
        self
    }

    /// Adds a plugin group (multiple plugins) to the engine.
    ///
    /// This is useful for adding default plugins or custom plugin bundles.
    ///
    /// # Example
    ///
    /// ```ignore
    /// # use astrelis::{ApplicationBuilder, DefaultPlugins};
    /// ApplicationBuilder::new()
    ///     .add_plugins(DefaultPlugins);
    /// ```
    pub fn add_plugins(mut self, group: impl PluginGroup) -> Self {
        for plugin in group.plugins() {
            self.plugins.push(plugin);
        }
        self
    }

    /// Sets custom window rendering context descriptor.
    ///
    /// This allows configuring advanced rendering options like present mode,
    /// texture format, and alpha mode.
    ///
    /// # Example
    ///
    /// ```ignore
    /// # use astrelis::ApplicationBuilder;
    /// # #[cfg(all(feature = "render", feature = "winit"))]
    /// # {
    /// use astrelis_render::WindowContextDescriptor;
    ///
    /// ApplicationBuilder::new()
    ///     .with_window_descriptor(WindowContextDescriptor {
    ///         present_mode: Some(wgpu::PresentMode::Mailbox),
    ///         ..Default::default()
    ///     });
    /// # }
    /// ```
    #[cfg(all(feature = "render", feature = "winit"))]
    pub fn with_window_descriptor(mut self, descriptor: WindowContextDescriptor) -> Self {
        self.window_descriptor = Some(descriptor);
        self
    }

    /// Disables automatic window creation.
    ///
    /// By default, ApplicationBuilder creates a window automatically. Use this
    /// if you want to create windows manually in your app factory function.
    ///
    /// # Example
    ///
    /// ```ignore
    /// # use astrelis::ApplicationBuilder;
    /// ApplicationBuilder::new()
    ///     .without_window()
    ///     .run(|ctx, engine| {
    ///         // Create windows manually here
    ///         MyApp::new()
    ///     });
    /// # struct MyApp;
    /// # impl MyApp { fn new() -> Self { Self } }
    /// ```
    pub fn without_window(mut self) -> Self {
        self.create_window = false;
        self
    }

    /// Builds and runs the application with the given factory function.
    ///
    /// The factory function receives:
    /// - `AppCtx` - For creating additional windows or accessing window resources
    /// - `Engine` - The initialized engine with all plugins
    ///
    /// The factory should return your App implementation.
    ///
    /// # Example
    ///
    /// ```ignore
    /// # use astrelis::prelude::*;
    /// # struct MyApp { window_id: WindowId }
    /// # impl App for MyApp {
    /// #     fn render(&mut self, ctx: &mut AppCtx, window_id: WindowId, events: &mut EventBatch) {}
    /// # }
    /// ApplicationBuilder::new()
    ///     .with_title("My Game")
    ///     .run(|ctx, engine| {
    ///         // Engine is already built with plugins
    ///         // Window is already created (unless you called without_window)
    ///         MyApp { window_id: WindowId::from(0u64) }
    ///     });
    /// ```
    #[cfg(feature = "winit")]
    pub fn run<T>(self, factory: impl FnOnce(&mut AppCtx, &Engine) -> T + 'static)
    where
        T: App + 'static,
    {
        self.run_internal(factory)
    }

    #[cfg(feature = "winit")]
    fn run_internal<T>(self, factory: impl FnOnce(&mut AppCtx, &Engine) -> T + 'static)
    where
        T: App + 'static,
    {
        let title = self.title;
        let size = self.size;
        #[cfg(all(feature = "render", feature = "winit"))]
        let window_descriptor = self.window_descriptor;
        let create_window = self.create_window;

        // Build engine with plugins using custom plugin group
        let mut builder = EngineBuilder::new();
        if !self.plugins.is_empty() {
            use std::cell::RefCell;
            builder = builder.add_plugins(StoredPlugins {
                plugins: RefCell::new(self.plugins),
            });
        }
        let engine = builder.build();

        // We need to pass engine and other data into run_app
        // Since AppFactory is a fn pointer, we use a trick:
        // Store data in a static and use a helper function
        use std::cell::RefCell;
        thread_local! {
            static APP_BUILDER_DATA: RefCell<Option<AppBuilderData>> = const { RefCell::new(None) };
        }

        #[allow(dead_code)] // Fields are used in app_builder_factory below
        struct AppBuilderData {
            engine: Engine,
            title: String,
            size: (u32, u32),
            #[cfg(all(feature = "render", feature = "winit"))]
            window_descriptor: Option<WindowContextDescriptor>,
            create_window: bool,
            factory: AppFactory,
        }

        // Store data in thread-local
        APP_BUILDER_DATA.with(|data| {
            *data.borrow_mut() = Some(AppBuilderData {
                engine,
                title,
                size,
                #[cfg(all(feature = "render", feature = "winit"))]
                window_descriptor,
                create_window,
                factory: Box::new(move |ctx, engine| Box::new(factory(ctx, engine))),
            });
        });

        // Helper function that can be used as AppFactory
        fn app_builder_factory(ctx: &mut AppCtx) -> Box<dyn App> {
            use std::cell::RefCell;
            thread_local! {
                static APP_BUILDER_DATA: RefCell<Option<AppBuilderData>> = const { RefCell::new(None) };
            }

            struct AppBuilderData {
                engine: Engine,
                title: String,
                size: (u32, u32),
                #[cfg(all(feature = "render", feature = "winit"))]
                window_descriptor: Option<WindowContextDescriptor>,
                create_window: bool,
                factory: AppFactory,
            }

            let mut data = APP_BUILDER_DATA
                .with(|d| d.borrow_mut().take())
                .expect("ApplicationBuilder data not found");

            #[cfg(all(feature = "render", feature = "winit"))]
            {
                // Create initial window if requested
                if data.create_window
                    && let Some(window_manager) = data.engine.get_mut::<WindowManager>()
                {
                    // Use WindowManager if available
                    let descriptor = WindowDescriptor {
                        title: data.title.clone(),
                        size: Some(WinitPhysicalSize::new(
                            data.size.0 as f32,
                            data.size.1 as f32,
                        )),
                        ..Default::default()
                    };

                    if let Some(window_desc) = data.window_descriptor.take() {
                        if let Err(e) = window_manager.create_window_with_descriptor(
                            ctx,
                            descriptor,
                            window_desc,
                        ) {
                            tracing::error!("Failed to create window with descriptor: {}", e);
                        }
                    } else if let Err(e) = window_manager.create_window(ctx, descriptor) {
                        tracing::error!("Failed to create window: {}", e);
                    }
                }
            }

            #[cfg(not(all(feature = "render", feature = "winit")))]
            {
                let _ = data.create_window; // Suppress unused warning
            }

            (data.factory)(ctx, &data.engine)
        }

        run_app(app_builder_factory)
    }

    /// Builds the engine without running the application.
    ///
    /// This is useful for testing or when you want to use the engine
    /// without the winit event loop.
    ///
    /// # Example
    ///
    /// ```ignore
    /// # use astrelis::ApplicationBuilder;
    /// let engine = ApplicationBuilder::new()
    ///     .add_plugins(astrelis::DefaultPlugins)
    ///     .build_engine();
    ///
    /// // Use engine...
    /// ```
    pub fn build_engine(self) -> Engine {
        let mut builder = EngineBuilder::new();
        if !self.plugins.is_empty() {
            use std::cell::RefCell;
            builder = builder.add_plugins(StoredPlugins {
                plugins: RefCell::new(self.plugins),
            });
        }
        builder.build()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_application_builder_defaults() {
        let builder = ApplicationBuilder::new();
        assert_eq!(builder.title, "Astrelis Application");
        assert_eq!(builder.size, (1280, 720));
        assert!(builder.plugins.is_empty());
        assert!(builder.create_window);
    }

    #[test]
    fn test_application_builder_with_title() {
        let builder = ApplicationBuilder::new().with_title("Test Game");
        assert_eq!(builder.title, "Test Game");
    }

    #[test]
    fn test_application_builder_with_size() {
        let builder = ApplicationBuilder::new().with_size(1920, 1080);
        assert_eq!(builder.size, (1920, 1080));
    }

    #[test]
    fn test_application_builder_without_window() {
        let builder = ApplicationBuilder::new().without_window();
        assert!(!builder.create_window);
    }

    #[test]
    fn test_application_builder_add_plugin() {
        use crate::FnPlugin;

        let builder = ApplicationBuilder::new().add_plugin(FnPlugin::new("test", |_| {}));
        assert_eq!(builder.plugins.len(), 1);
    }

    #[test]
    fn test_application_builder_build_engine() {
        use crate::FnPlugin;

        let engine = ApplicationBuilder::new()
            .add_plugin(FnPlugin::new("test", |resources| {
                resources.insert(42i32);
            }))
            .build_engine();

        assert_eq!(*engine.get::<i32>().unwrap(), 42);
    }

    #[test]
    fn test_application_builder_chaining() {
        let builder = ApplicationBuilder::new()
            .with_title("Test")
            .with_size(800, 600)
            .without_window();

        assert_eq!(builder.title, "Test");
        assert_eq!(builder.size, (800, 600));
        assert!(!builder.create_window);
    }
}