chuot 0.3.2

AGPL licensed and opinionated game engine for pixel-art games
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
#![forbid(unsafe_code)]
#![doc(
    html_logo_url = "https://github.com/tversteeg/chuot-website/blob/main/static/favicon-32x32.png?raw=true"
)]

//! AGPL licensed and opinionated game engine for 2D pixel-art games.
//!
//! # Features
//!
//! - Pixel-perfect pixel art rendering with built-in rotsprite rotation shader.
//! - Window creation with independent update and render game loop.
//! - Hot-reloadable assets, seeing your assets update live in the game when you save them is a great boost in productivity for quickly iterating on ideas.
//! - Single-binary, all non-texture assets will be embedded directly, and textures will be diced into a single atlas map embedded in the binary when deploying.
//! - Simple bitmap font drawing.
//! - OGG audio playback.
//! - First-class gamepad support.
//! - Separate managed camera systems for UI and game elements.
//!
//! # Goals
//!
//! - Ergonomic API with a focus on quickly creating small games, especially for game jams.
//! - Reasonable performance, drawing thousands of animated sprites at the same time shouldn't be a problem.
//! - Proper web support, it should be very easy to bundle as WASM for the web.
//!
//! # Non-Goals
//!
//! - An ECS (Entity component system), although an ECS architecture is great for cache locality and thus performance, I feel that it's overkill for most small games. Nothing is stopping you to add your own on top of this engine if that's what you want though!
//! - 3D, this engine is only for 2D pixel art.
//! - Vector graphics, similar to the above, this engine is focused specifically on pixel art with lower resolutions.
//! - Reinventing the wheel for everything, when there's a proper crate with good support I prefer to use that instead of creating additional maintainer burden.
//! - Support all possible file formats, this bloats the engine.
//!
//! # Usage
//!
//! Using this crate is quite simple, there is a single trait [`Game`] with two required functions, [`Game::update`] and [`Game::render`], that need to be implemented for a game state object.
//!
//! ```
//! use chuot::{Config, Context, Game};
//!
//! struct MyGame;
//!
//! impl Game for MyGame {
//!     fn update(&mut self, ctx: Context) {
//!         // ..
//!     }
//!
//!     fn render(&mut self, ctx: Context) {
//!         // ..
//!     }
//! }
//!
//! # fn try_main() {
//! // In main
//!
//! let game = MyGame;
//!
//! game.run(chuot::load_assets!(), Config::default());
//! # }
//! ```
//!
//! # Features
//!
//! ## `embed-assets`
//!
//! Embed all assets into the binary when building.
//!
//! _Must_ be enabled when building for the web.
//! If disabled all assets will be loaded from disk.
//!
//! This will dice all PNG assets into a single tiny optimized PNG atlas.
//! On startup this diced atlas will be efficiently uploaded to the GPU as a single bigger atlas, which will be used for all static sprites.
//!
//! ## `read-texture` (default)
//!
//! Expose read operations on images, if disabled sprites will be uploaded to the GPU and their data will be removed from memory.
//!
//! # Install Requirements
//!
//! On Linux you need to install `asound2-dev` for audio and `udev-dev` for gamepads:
//!
//! ```sh
//! sudo apt install libasound2-dev libudev-dev
//! ```
//!
//! # Example
//!
//! This example will show a window with a counter that's incremented when pressing the left mouse button[^left-mouse].
//! The counter is rendered as text[^text] loaded from a font in the top-left corner.
//! When the 'Escape' key is pressed[^escape-key] the game will exit and the window will close.
//!
//! ```
//! use chuot::{Game, Context, Config, MouseButton, KeyCode};
//!
//! /// Object holding all game state.
//! struct MyGame {
//!   /// A simple counter we increment by clicking on the screen.
//!   counter: u32,
//! }
//!
//! impl Game for MyGame {
//!   fn update(&mut self, ctx: Context) {
//!     // ^1
//!     // Increment the counter when we press the left mouse button
//!     if ctx.mouse_pressed(MouseButton::Left) {
//!       self.counter += 1;
//!     }
//!
//!     // ^3
//!     // Exit the game if 'Escape' is pressed
//!     if ctx.key_pressed(KeyCode::Escape) {
//!       ctx.exit();
//!     }
//!   }
//!
//!   fn render(&mut self, ctx: Context) {
//!     // ^2
//!     // Display the counter with a font called 'font' automatically loaded from the `assets/` directory
//!     // It will be shown in the top-left corner
//!     ctx.text("font", &format!("Counter: {}", self.counter)).draw();
//!   }
//! }
//!
//! # fn try_main()  {
//! // In main
//!
//! // Initialize the game state
//! let game = MyGame { counter: 0 };
//!
//! // Run the game until exit is requested
//! game.run(chuot::load_assets!(), Config::default().with_title("My Game"));
//! # }
//! ```
//!
//! [^left-mouse]: [`Context::mouse_pressed`]
//! [^text]: [`Context::text`]
//! [^escape-key]: [`Context::key_pressed`]

pub mod assets;
mod camera;
pub mod config;
pub mod context;
mod graphics;
mod input;
mod math;
mod random;

pub use assets::source::AssetSource;
/// Define the directory of the assets.
///
/// *MUST* be passed as first argument to [`Game::run`].
///
/// The assets will be embedded in the binary when using the `embed-assets` feature flag.
///
/// # Arguments
///
/// * `path` - Local directory where the game assets reside. Defaults to `"assets/"`.
///
/// # Example
///
/// ```
/// chuot::load_assets!("assets/");
/// // Is the same as..
/// chuot::load_assets!();
/// ```
pub use chuot_macros::load_assets;
pub use config::Config;
pub use context::Context;
/// Re-exported [`gilrs`](https://docs.rs/gilrs) type.
pub use gilrs::ev::{Axis as GamepadAxis, Button as GamepadButton};
pub use math::lerp;
pub use random::random;
/// Re-exported [`rgb`](https://docs.rs/rgb) type.
pub use rgb::RGBA8;
use web_time::Instant;
use winit::{
    application::ApplicationHandler,
    dpi::{LogicalSize, PhysicalSize},
    event::WindowEvent,
    event_loop::{ActiveEventLoop, ControlFlow, EventLoop},
    window::{WindowAttributes, WindowId},
};
/// Re-exported [`winit`](https://docs.rs/winit) type.
pub use winit::{event::MouseButton, keyboard::KeyCode};

/// How fast old FPS values decay in the smoothed average.
const FPS_SMOOTHED_AVERAGE_ALPHA: f32 = 0.8;

/// Maximum of the amount of `update` calls for a single `render` call.
const MAX_UPDATE_CALLS_PER_RENDER: f32 = 20.0;

/// Main entrypoint containing game state for running the game.
///
/// This is the main interface with the game engine.
///
/// See [`Context`] for all functions interfacing with the game engine from both functions.
pub trait Game: Sized
where
    Self: 'static,
{
    /// A single update tick in the game loop.
    ///
    /// Will run on a different rate from the render loop specified in the game configuration.
    ///
    /// Must be used for updating the game state.
    /// It's possible to queue draw calls on the update context since that's the same object as render, but that will result in very erratic drawing since the render loop is uncoupled from the update loop.
    ///
    /// # Arguments
    ///
    /// * `ctx` - Game context, used to obtain information and mutate the game state.
    ///
    /// # Example
    ///
    /// ```
    /// use chuot::{Context, Game, KeyCode};
    ///
    /// struct MyGame;
    ///
    /// impl Game for MyGame {
    ///     fn update(&mut self, ctx: Context) {
    ///         // Stop the game and close the window when 'Escape' is pressed
    ///         if ctx.key_pressed(KeyCode::Escape) {
    ///             ctx.exit();
    ///         }
    ///     }
    ///
    ///     fn render(&mut self, ctx: Context) {
    ///         // ..
    ///     }
    /// }
    /// ```
    fn update(&mut self, ctx: Context);

    /// A single render tick in the game loop.
    ///
    /// Will run on a different rate from the update loop specified in the game configuration.
    ///
    /// Must be used for rendering the game.
    /// Be careful with mutating game state here, when it's dependent on external state the result will be erratic and incorrect, such as handling any form of input.
    ///
    /// # Arguments
    ///
    /// * `ctx` - Game context, used to obtain information and queue draw calls.
    ///
    /// # Example
    ///
    /// ```
    /// use chuot::{Context, Game};
    ///
    /// struct MyGame;
    ///
    /// impl Game for MyGame {
    ///     fn render(&mut self, ctx: Context) {
    ///         // Draw a sprite on the screen
    ///         ctx.sprite("sprite").draw();
    ///     }
    ///
    ///     fn update(&mut self, ctx: Context) {
    ///         // ..
    ///     }
    /// }
    /// ```
    fn render(&mut self, ctx: Context);

    /// Optionally implement this method to run this function at startup.
    ///
    /// Will run after the window is set up and the context is created.
    ///
    /// # Arguments
    ///
    /// * `ctx` - Game context, used to obtain information and queue draw calls.
    ///
    /// # Example
    ///
    /// ```
    /// use chuot::{Context, Game};
    ///
    /// struct MyGame;
    ///
    /// impl Game for MyGame {
    ///     fn init(&mut self, ctx: Context) {
    ///         // Do something you only want to do once, such as setting the camera offset:
    ///         ctx.main_camera().set_top_left();
    ///     }
    ///
    ///     fn render(&mut self, ctx: Context) {
    ///         // ..
    ///     }
    ///
    ///     fn update(&mut self, ctx: Context) {
    ///         // ..
    ///     }
    /// }
    /// ```
    #[inline(always)]
    #[allow(unused_variables)]
    fn init(&mut self, ctx: Context) {}

    /// Run the game, spawning the window.
    ///
    /// <div class="warning">
    ///
    /// Don't implement/override this method.
    ///
    /// </div>
    ///
    /// # Arguments
    ///
    /// * `asset_source` - Source of the assets, should probably be `chuot::load_assets!()`, unless you don't need any assets.
    /// * `config` - Configuration for the window, can be used to set the buffer size, the window title and other things.
    ///
    /// # Errors
    ///
    /// - When a window could not be opened (desktop only).
    /// - If no GPU could be found or accessed.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use chuot::{Config, Context, Game, KeyCode};
    ///
    /// struct MyGame;
    ///
    /// impl Game for MyGame {
    ///     fn update(&mut self, ctx: Context) {
    ///         // Stop the game and close the window when 'Escape' is pressed
    ///         if ctx.key_pressed(KeyCode::Escape) {
    ///             ctx.exit();
    ///         }
    ///     }
    ///
    ///     fn render(&mut self, ctx: Context) {
    ///         // ..
    ///     }
    /// }
    ///
    /// // In main
    /// let game = MyGame;
    ///
    /// game.run(chuot::load_assets!(), Config::default());
    /// ```
    #[inline(always)]
    fn run(self, asset_source: AssetSource, config: Config) {
        // Show panics in the browser console log
        #[cfg(target_arch = "wasm32")]
        console_error_panic_hook::set_once();

        // Setup the timestep variables for calculating the update loop
        let accumulator = 0.0;
        let last_time = Instant::now();

        // Context must be initialized later when creating the window
        let ctx = None;

        // Create a polling event loop, which redraws the window whenever possible
        let event_loop = EventLoop::with_user_event().build().unwrap();
        event_loop.set_control_flow(ControlFlow::Poll);

        #[cfg(target_arch = "wasm32")]
        {
            use winit::platform::web::{EventLoopExtWebSys, PollStrategy, WaitUntilStrategy};

            // Ensure the game on the web runs as smooth as possible
            event_loop.set_poll_strategy(PollStrategy::IdleCallback);
            event_loop.set_wait_until_strategy(WaitUntilStrategy::Worker);
        }

        // Put the asset source on the heap
        let asset_source = Some(Box::new(asset_source));

        // Get the event loop proxy so we can instantiate on the web
        #[cfg(target_arch = "wasm32")]
        let event_loop_proxy = Some(event_loop.create_proxy());

        // Move the game struct to the state
        let game = self;

        // Run the game
        let _ = event_loop.run_app(&mut State {
            ctx,
            asset_source,
            game,
            config,
            last_time,
            accumulator,
            #[cfg(target_arch = "wasm32")]
            event_loop_proxy,
        });
    }
}

/// State of setting up a window that can still be uninitialized.
///
/// All optional fields are tied to the window creation flow of winit.
struct State<G: Game> {
    /// Game context.
    ///
    /// `None` if the window still needs to be initialized.
    ctx: Option<Context>,
    /// Source of all assets.
    ///
    /// Will be taken from the option once.
    asset_source: Option<Box<AssetSource>>,
    /// User supplied game.
    game: G,
    /// User supplied configuration.
    config: Config,
    /// Time for calculating the update rate.
    last_time: Instant,
    /// Timestep accumulator for the update rate.
    accumulator: f32,
    /// Proxy required to send the context on the web platform.
    #[cfg(target_arch = "wasm32")]
    event_loop_proxy: Option<winit::event_loop::EventLoopProxy<Context>>,
}

impl<G: Game> ApplicationHandler<Context> for State<G> {
    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
        // Setup the window
        if self.ctx.is_none() {
            // Define the properties of the window
            #[allow(unused_mut)]
            let mut window_attributes = WindowAttributes::default()
                .with_title(&self.config.title)
                // Apply scaling for the requested size
                .with_inner_size(LogicalSize::new(
                    self.config.buffer_width * self.config.scaling,
                    self.config.buffer_height * self.config.scaling,
                ))
                // Don't allow the window to be smaller than the pixel size
                .with_min_inner_size(LogicalSize::new(
                    self.config.buffer_width,
                    self.config.buffer_height,
                ));

            #[cfg(target_arch = "wasm32")]
            {
                use web_sys::{HtmlCanvasElement, wasm_bindgen::JsCast};
                use winit::platform::web::WindowAttributesExtWebSys;

                // Create or find a canvas the winit window can be attached to
                let window = web_sys::window().unwrap();
                let document = window.document().unwrap();
                let canvas = document
                    .get_element_by_id("chuot")
                    .map(|canvas| canvas.dyn_into::<HtmlCanvasElement>().unwrap());

                // If the canvas is not found a new one will be created
                window_attributes = window_attributes
                    .with_canvas(canvas)
                    // Add the canvas to the web page
                    .with_append(true)
                    // Handle all input events
                    .with_prevent_default(true);
            }

            // Spawn a new window using the event loop
            let window = event_loop
                .create_window(window_attributes)
                .expect("Error creating window");

            // Adjust the canvas for proper integer scale rendering
            #[cfg(target_arch = "wasm32")]
            {
                use winit::platform::web::WindowExtWebSys;

                // Ensure the pixels are not rendered with wrong filtering and that the size is correct
                window
                    .canvas()
                    .unwrap()
                    .style()
                    .set_css_text(
                        &format!(
                            "image-rendering: pixelated; outline: none; border: none; width: {}px; height: {}px",
                            self.config.buffer_width * self.config.scaling,
                            self.config.buffer_height * self.config.scaling,
                        )
                    );
            }

            // Setup the context
            #[cfg(not(target_arch = "wasm32"))]
            {
                // Because pollster returns the value we can set it immediately
                let ctx = pollster::block_on(async {
                    Context::new(
                        self.config.clone(),
                        self.asset_source.take().unwrap(),
                        window,
                    )
                    .await
                });

                // Set the context
                self.ctx = Some(ctx.clone());

                // Call user passed init function
                self.game.init(ctx);
            }
            #[cfg(target_arch = "wasm32")]
            {
                // We only need the proxy once to send the context
                let event_loop_proxy = self.event_loop_proxy.take().unwrap();
                let asset_source = self.asset_source.take().unwrap();
                let config = self.config.clone();

                wasm_bindgen_futures::spawn_local(async move {
                    // Because WASM futures can't block we need to send it with a user event
                    let ctx = Context::new(config, asset_source, window).await;

                    let _ = event_loop_proxy.send_event(ctx);
                });
            }
        }
    }

    fn window_event(
        &mut self,
        event_loop: &ActiveEventLoop,
        _window_id: WindowId,
        event: WindowEvent,
    ) {
        // Do nothing if the window is not set up yet
        let Some(ctx) = &mut self.ctx else {
            return;
        };

        // Handle the window events
        match event {
            // Handle the update loop and render loop
            WindowEvent::RedrawRequested => {
                // Update the timestep
                let current_time = Instant::now();
                let frame_time = (current_time - self.last_time)
                    .as_secs_f32()
                    // Ensure that the update loop cannot be called too often
                    .min(MAX_UPDATE_CALLS_PER_RENDER * self.config.update_delta_time);
                self.last_time = current_time;

                self.accumulator += frame_time
                    // Ensure the frametime will never surpass this amount
                    .min(self.config.max_frame_time_secs);

                // Call the user update function with the context
                while self.accumulator >= self.config.update_delta_time {
                    // Call the implemented update function on the 'Game' trait
                    self.game.update(ctx.clone());

                    // Mark this tick as executed
                    self.accumulator -= self.config.update_delta_time;

                    ctx.write(|ctx| {
                        // Update the input so pressed and released events can be handled
                        ctx.input.update();

                        // Update camera targets
                        ctx.main_camera.update_target();
                        ctx.ui_camera.update_target();

                        // Handle hot reloaded assets
                        #[cfg(not(target_arch = "wasm32"))]
                        assets::hot_reload::handle_changed_asset_files(ctx);
                    });
                }

                ctx.write(|ctx| {
                    // Set the blending factor
                    ctx.blending_factor = self.accumulator / self.config.update_delta_time;

                    // Set the FPS with a smoothed average function
                    ctx.frames_per_second = FPS_SMOOTHED_AVERAGE_ALPHA.mul_add(
                        ctx.frames_per_second,
                        (1.0 - FPS_SMOOTHED_AVERAGE_ALPHA) * frame_time.recip(),
                    );

                    // Update cameras
                    ctx.main_camera.update(frame_time, ctx.blending_factor);
                    ctx.ui_camera.update(frame_time, ctx.blending_factor);
                });

                // Only call render loop when the window is not minimized
                let not_minimized = !ctx.is_minimized();

                // Call the user render function with the context
                if not_minimized {
                    self.game.render(ctx.clone());
                }

                ctx.write(|ctx| {
                    // Draw the window and GPU graphics
                    if not_minimized {
                        ctx.graphics.render();
                    }

                    if ctx.exit {
                        // Tell winit that we want to exit
                        event_loop.exit();
                    }
                });
            }
            // Resize the render surface
            WindowEvent::Resized(PhysicalSize { width, height }) => {
                ctx.write(|ctx| {
                    // Resize the GPU surface
                    ctx.graphics.resize(width, height);

                    // On MacOS the window needs to be redrawn manually after resizing
                    #[cfg(target_os = "macos")]
                    ctx.window.request_redraw();
                });
            }
            // Close the window if requested
            WindowEvent::CloseRequested => {
                // Tell winit that we want to exit
                event_loop.exit();
            }
            // Handle other window events with the input manager
            WindowEvent::KeyboardInput { .. }
            | WindowEvent::CursorMoved { .. }
            | WindowEvent::MouseWheel { .. }
            | WindowEvent::MouseInput { .. } => {
                ctx.write(|ctx| ctx.input.handle_event(event, &ctx.graphics));
            }
            // Ignore the rest of the events
            _ => (),
        }
    }

    fn user_event(&mut self, _event_loop: &ActiveEventLoop, ctx: Context) {
        // Call user passed init function
        self.game.init(ctx.clone());

        // We received the context from initializing, set it
        self.ctx = Some(ctx);
    }

    fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
        let Some(ctx) = &mut self.ctx else {
            return;
        };

        // Ensure the control flow doesn't change
        event_loop.set_control_flow(ControlFlow::Poll);

        // Application is about to wait, request a redraw
        ctx.write(|ctx| ctx.window.request_redraw());
    }

    fn exiting(&mut self, _event_loop: &ActiveEventLoop) {
        // Destroy all state(s), anarchy for all
        self.ctx = None;
        self.asset_source = None;
        #[cfg(target_arch = "wasm32")]
        {
            self.event_loop_proxy = None;
        }
    }
}