spottedcat 0.7.7

Rusty SpottedCat simple game engine
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
use crate::DrawOption;
use crate::audio::AudioSystem;
#[cfg(feature = "model-3d")]
use crate::context_3d::{Model3dRegistry, Model3dRuntime};
use crate::drawable::DrawCommand;
use crate::graphics::core::Graphics;
use crate::input::InputManager;
use crate::pt::Pt;
use crate::shader_opts::ShaderOpts;
use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::rc::Rc;

#[derive(Debug, Clone, Copy)]
pub(crate) struct DrawState {
    pub position: [Pt; 2],
    pub clip: Option<[Pt; 4]>,
    pub shader_id: Option<u32>,
    pub shader_opts: Option<ShaderOpts>,
    pub layer: i32,
}

impl Default for DrawState {
    fn default() -> Self {
        Self {
            position: [Pt(0.0), Pt(0.0)],
            clip: None,
            shader_id: None,
            shader_opts: None,
            layer: 0,
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct LastImageDrawInfo {
    pub(crate) opts: DrawOption,
}

#[derive(Default)]
struct ResourceMap {
    inner: HashMap<TypeId, Rc<dyn Any>>,
}

impl std::fmt::Debug for ResourceMap {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ResourceMap")
            .field("len", &self.inner.len())
            .finish()
    }
}

#[derive(Debug)]
pub(crate) struct ContextRuntime {
    pub(crate) draw_list: Vec<DrawCommand>,
    #[cfg(feature = "model-3d")]
    pub(crate) model_3d: Model3dRuntime,
    pub(crate) input: InputManager,
    pub(crate) scale_factor: f64,
    pub(crate) window_logical_size: (Pt, Pt),
    pub(crate) state_stack: Vec<DrawState>,
    pub(crate) current_state: DrawState,
    pub(crate) last_image_opts: HashMap<u32, LastImageDrawInfo>,
    pub(crate) graphics: Option<Graphics>,
    pub(crate) audio: Option<AudioSystem>,
    pub(crate) delta_time: std::time::Duration,
    pub(crate) total_elapsed: std::time::Duration,
    pub(crate) pending_window_title: Option<String>,
    pub(crate) pending_cursor_visible: Option<bool>,
    pub(crate) pending_fullscreen: Option<bool>,
}

impl ContextRuntime {
    fn new() -> Self {
        Self {
            draw_list: Vec::new(),
            #[cfg(feature = "model-3d")]
            model_3d: Model3dRuntime::default(),
            input: InputManager::new(),
            scale_factor: 1.0,
            window_logical_size: (Pt(0.0), Pt(0.0)),
            state_stack: Vec::new(),
            current_state: DrawState::default(),
            last_image_opts: HashMap::new(),
            graphics: None,
            audio: None,
            delta_time: std::time::Duration::from_secs(0),
            total_elapsed: std::time::Duration::from_secs(0),
            pending_window_title: None,
            pending_cursor_visible: None,
            pending_fullscreen: None,
        }
    }
}

#[derive(Debug)]
pub(crate) struct ResourceRegistry {
    resources: ResourceMap,
    pub(crate) images: Vec<Option<crate::image::ImageEntry>>,
    #[cfg(feature = "model-3d")]
    pub(crate) model_3d: Model3dRegistry,
    pub(crate) fonts: HashMap<u32, Vec<u8>>,
    pub(crate) image_shaders: HashMap<u32, String>,
    pub(crate) next_image_id: u32,
    pub(crate) next_font_id: u32,
    pub(crate) next_image_shader_id: u32,
    pub(crate) gpu_generation: u32,
    pub(crate) dirty_assets: bool,
}

impl ResourceRegistry {
    fn new() -> Self {
        Self {
            resources: ResourceMap::default(),
            images: Vec::new(),
            #[cfg(feature = "model-3d")]
            model_3d: Model3dRegistry::default(),
            fonts: HashMap::new(),
            image_shaders: HashMap::new(),
            next_image_id: 1,
            next_font_id: 1,
            next_image_shader_id: 1,
            gpu_generation: 1,
            dirty_assets: true,
        }
    }
}

/// Drawing context for managing render commands.
///
/// The context accumulates drawing commands during a frame and is used by the
/// graphics system to render the scene.
#[derive(Debug)]
pub struct Context {
    pub(crate) runtime: ContextRuntime,
    pub(crate) registry: ResourceRegistry,
}

impl Context {
    /// Creates a new drawing context.
    pub(crate) fn new() -> Self {
        let mut ctx = Self {
            runtime: ContextRuntime::new(),
            registry: ResourceRegistry::new(),
        };
        ctx.register_defaults();
        ctx
    }

    pub(crate) fn set_delta_time(&mut self, dt: std::time::Duration) {
        self.runtime.delta_time = dt;
        self.runtime.total_elapsed = self.runtime.total_elapsed.saturating_add(dt);
    }

    /// Returns the time elapsed since the last frame.
    pub(crate) fn delta_time(&self) -> std::time::Duration {
        self.runtime.delta_time
    }

    /// Returns the total time elapsed since the engine started.
    pub(crate) fn total_elapsed(&self) -> std::time::Duration {
        self.runtime.total_elapsed
    }

    pub(crate) fn with_audio<R>(&mut self, f: impl FnOnce(&mut AudioSystem) -> R) -> Option<R> {
        self.runtime.audio.as_mut().map(f)
    }

    fn register_defaults(&mut self) {
        self.register_image(1, 1, Pt::from(1.0), Pt::from(1.0), &[255, 255, 255, 255]); // ID 1
        self.register_image(1, 1, Pt::from(1.0), Pt::from(1.0), &[0, 0, 0, 255]); // ID 2
        #[cfg(feature = "model-3d")]
        self.register_image(1, 1, Pt::from(1.0), Pt::from(1.0), &[128, 128, 255, 255]); // ID 3 (Normal)

        let text_shader_src = r#"
            fn user_fs_hook() {
                let tint = user_globals[0];
                color = vec4<f32>(color.rgb * tint.rgb, color.a * tint.a);
            }
        "#;
        self.register_image_shader(text_shader_src);
    }

    pub(crate) fn set_window_logical_size(&mut self, width: Pt, height: Pt) {
        let w = Pt(width.0.max(0.0));
        let h = Pt(height.0.max(0.0));
        self.runtime.window_logical_size = (w, h);
    }

    pub(crate) fn update_window_metrics_physical(
        &mut self,
        width: u32,
        height: u32,
        scale_factor: f64,
    ) {
        self.set_scale_factor(scale_factor);
        self.set_window_logical_size(
            Pt::from_physical_px(width as f64, scale_factor),
            Pt::from_physical_px(height as f64, scale_factor),
        );
    }

    pub(crate) fn set_window_title(&mut self, title: impl Into<String>) {
        self.runtime.pending_window_title = Some(title.into());
    }

    pub(crate) fn set_cursor_visible(&mut self, visible: bool) {
        self.runtime.pending_cursor_visible = Some(visible);
    }

    pub(crate) fn set_fullscreen(&mut self, enabled: bool) {
        self.runtime.pending_fullscreen = Some(enabled);
    }

    pub(crate) fn take_window_title_request(&mut self) -> Option<String> {
        self.runtime.pending_window_title.take()
    }

    pub(crate) fn take_cursor_visible_request(&mut self) -> Option<bool> {
        self.runtime.pending_cursor_visible.take()
    }

    pub(crate) fn take_fullscreen_request(&mut self) -> Option<bool> {
        self.runtime.pending_fullscreen.take()
    }

    /// Returns the logical size of the window in Pt.
    pub(crate) fn window_logical_size(&self) -> (Pt, Pt) {
        self.runtime.window_logical_size
    }

    /// Returns a horizontal length equivalent to `percent` of the window width.
    pub(crate) fn vw(&self, percent: f32) -> Pt {
        let (w, _) = self.runtime.window_logical_size;
        let p = if percent.is_finite() { percent } else { 0.0 };
        Pt::from(w.as_f32() * (p / 100.0))
    }

    /// Returns a vertical length equivalent to `percent` of the window height.
    pub(crate) fn vh(&self, percent: f32) -> Pt {
        let (_, h) = self.runtime.window_logical_size;
        let p = if percent.is_finite() { percent } else { 0.0 };
        Pt::from(h.as_f32() * (p / 100.0))
    }

    pub fn insert_resource<T: Any>(&mut self, value: Rc<T>) {
        self.registry
            .resources
            .inner
            .insert(TypeId::of::<T>(), value as Rc<dyn Any>);
    }

    pub(crate) fn get_resource<T: Any>(&self) -> Option<Rc<T>> {
        self.registry
            .resources
            .inner
            .get(&TypeId::of::<T>())
            .cloned()
            .and_then(|v| Rc::downcast::<T>(v).ok())
    }

    pub fn take_resource<T: Any>(&mut self) -> Option<Rc<T>> {
        self.registry
            .resources
            .inner
            .remove(&TypeId::of::<T>())
            .and_then(|v| Rc::downcast::<T>(v).ok())
    }

    pub(crate) fn register_image(
        &mut self,
        pixel_width: u32,
        pixel_height: u32,
        width: Pt,
        height: Pt,
        rgba: &[u8],
    ) -> crate::Image {
        let id = self.registry.next_image_id;
        self.registry.next_image_id += 1;
        let bounds = crate::image::Bounds::new(Pt(0.0), Pt(0.0), width, height);
        let entry = crate::image::ImageEntry::new(
            None,
            bounds,
            pixel_width,
            pixel_height,
            None,
            Some(std::sync::Arc::from(rgba)),
            None,
        );

        while self.registry.images.len() <= id as usize {
            self.registry.images.push(None);
        }
        self.registry.images[id as usize] = Some(entry);
        self.registry.dirty_assets = true;

        crate::Image {
            id,
            x: bounds.x,
            y: bounds.y,
            width: bounds.width,
            height: bounds.height,
            pixel_width,
            pixel_height,
        }
    }

    pub(crate) fn register_sub_image(
        &mut self,
        image: crate::image::Image,
        bounds: crate::image::Bounds,
    ) -> anyhow::Result<u32> {
        let parent_id = image.id();
        let id = self.registry.next_image_id;
        self.registry.next_image_id += 1;

        let width_ratio = if image.width.0 > 0.0 {
            bounds.width.0 / image.width.0
        } else {
            0.0
        };
        let height_ratio = if image.height.0 > 0.0 {
            bounds.height.0 / image.height.0
        } else {
            0.0
        };
        let pixel_width = ((image.pixel_width as f32) * width_ratio).round().max(1.0) as u32;
        let pixel_height = ((image.pixel_height as f32) * height_ratio)
            .round()
            .max(1.0) as u32;

        let entry = crate::image::ImageEntry::new(
            None,
            bounds,
            pixel_width,
            pixel_height,
            None,
            None,
            Some(parent_id),
        );

        while self.registry.images.len() <= id as usize {
            self.registry.images.push(None);
        }
        self.registry.images[id as usize] = Some(entry);
        self.registry.dirty_assets = true;
        Ok(id)
    }

    pub(crate) fn register_font(&mut self, font_data: Vec<u8>) -> u32 {
        let id = self.registry.next_font_id;
        self.registry.next_font_id += 1;
        self.registry.fonts.insert(id, font_data);
        self.registry.dirty_assets = true;
        id
    }

    pub(crate) fn register_image_shader(&mut self, user_functions: &str) -> u32 {
        let id = self.registry.next_image_shader_id;
        self.registry.next_image_shader_id += 1;
        self.registry
            .image_shaders
            .insert(id, user_functions.to_string());
        self.registry.dirty_assets = true;
        id
    }

    pub(crate) fn insert_resource_dyn(&mut self, type_id: TypeId, value: Rc<dyn Any>) {
        self.registry.resources.inner.insert(type_id, value);
    }

    pub(crate) fn take_resource_dyn(&mut self, type_id: TypeId) {
        self.registry.resources.inner.remove(&type_id);
    }

    pub(crate) fn begin_frame(&mut self) {
        self.runtime.draw_list.clear();
        #[cfg(feature = "model-3d")]
        self.runtime.model_3d.begin_frame();
        self.runtime.state_stack.clear();
        self.runtime.current_state = DrawState::default();
        self.runtime.last_image_opts.clear();
    }

    pub(crate) fn clear_transient_state(&mut self) {
        self.begin_frame();
        self.runtime.pending_window_title = None;
        self.runtime.pending_cursor_visible = None;
        self.runtime.pending_fullscreen = None;
    }

    pub(crate) fn clear_transient_input(&mut self) {
        self.runtime.input.clear_transient_state();
    }

    pub(crate) fn input(&self) -> &InputManager {
        &self.runtime.input
    }

    pub(crate) fn input_mut(&mut self) -> &mut InputManager {
        &mut self.runtime.input
    }

    pub(crate) fn set_scale_factor(&mut self, scale_factor: f64) {
        self.runtime.scale_factor = scale_factor;
    }

    pub(crate) fn graphics_mut(&mut self) -> Option<&mut Graphics> {
        self.runtime.graphics.as_mut()
    }

    #[cfg_attr(not(target_os = "android"), allow(dead_code))]
    pub(crate) fn has_graphics(&self) -> bool {
        self.runtime.graphics.is_some()
    }

    pub(crate) fn attach_graphics(&mut self, graphics: Graphics) {
        self.runtime.graphics = Some(graphics);
    }

    pub(crate) fn detach_graphics(&mut self) -> Option<Graphics> {
        self.runtime.graphics.take()
    }

    #[cfg_attr(not(target_os = "android"), allow(dead_code))]
    pub(crate) fn bump_gpu_generation(&mut self) {
        self.registry.gpu_generation = self.registry.gpu_generation.saturating_add(1);
        self.registry.dirty_assets = true;
    }

    /// Returns the window's scale factor (DPI).
    pub(crate) fn scale_factor(&self) -> f64 {
        self.runtime.scale_factor
    }

    pub(crate) fn push(&mut self, mut drawable: DrawCommand) {
        match &mut drawable {
            DrawCommand::Image(cmd) => {
                let opts = &mut cmd.opts;
                let shader_id = &mut cmd.shader_id;
                let shader_opts = &mut cmd.shader_opts;
                *opts = opts.apply_state(&self.runtime.current_state);
                *opts = opts.with_layer(opts.layer() + self.runtime.current_state.layer);
                if *shader_id == 0
                    && let Some(parent_shader_id) = self.runtime.current_state.shader_id
                {
                    *shader_id = parent_shader_id;
                    if let Some(parent_shader_opts) = self.runtime.current_state.shader_opts {
                        *shader_opts = parent_shader_opts;
                    }
                }
            }
            DrawCommand::Text(_, opts) => {
                *opts = opts.apply_state(&self.runtime.current_state);
                *opts = opts.with_layer(opts.layer() + self.runtime.current_state.layer);
            }
            DrawCommand::ClearImage(_, _) | DrawCommand::CopyImage(_, _) => {}
        }

        if let DrawCommand::Image(cmd) = &drawable {
            let id = cmd.id;
            let opts = &cmd.opts;
            let size = cmd.size;
            let pos = opts.position();
            let scale = opts.scale();
            let rot = opts.rotation();
            let w = size[0].as_f32() * scale[0];
            let h = size[1].as_f32() * scale[1];

            let (vw, vh) = self.runtime.window_logical_size;
            let screen_w = vw.as_f32();
            let screen_h = vh.as_f32();

            let is_visible = if rot == 0.0 {
                let x0 = pos[0].as_f32();
                let y0 = pos[1].as_f32();
                let x1 = x0 + w;
                let y1 = y0 + h;

                let min_x = x0.min(x1);
                let max_x = x0.max(x1);
                let min_y = y0.min(y1);
                let max_y = y0.max(y1);

                !(max_x < 0.0 || min_x > screen_w || max_y < 0.0 || min_y > screen_h)
            } else {
                let c = rot.cos();
                let s = rot.sin();
                let x2 = w * c;
                let y2 = w * s;
                let x3 = -h * s;
                let y3 = h * c;
                let x4 = x2 + x3;
                let y4 = y2 + y3;

                let min_x = 0.0f32.min(x2).min(x3).min(x4);
                let max_x = 0.0f32.max(x2).max(x3).max(x4);
                let min_y = 0.0f32.min(y2).min(y3).min(y4);
                let max_y = 0.0f32.max(y2).max(y3).max(y4);

                !(pos[0].as_f32() + max_x < 0.0
                    || pos[0].as_f32() + min_x > screen_w
                    || pos[1].as_f32() + max_y < 0.0
                    || pos[1].as_f32() + min_y > screen_h)
            };

            if !is_visible {
                if std::env::var("SPOT_DEBUG_CULL").is_ok() {
                    eprintln!(
                        "[spot][cull] image id={} at {:?} (size {:?}) is culled (screen: {:?})",
                        id,
                        pos,
                        [w, h],
                        self.runtime.window_logical_size
                    );
                }
                return;
            }

            self.runtime
                .last_image_opts
                .insert(id, LastImageDrawInfo { opts: *opts });
        }

        if std::env::var("SPOT_DEBUG_DRAW").is_ok() {
            match &drawable {
                DrawCommand::Image(cmd) => {
                    eprintln!(
                        "[spot][debug] draw image id={} shader_id={} pos={:?} clip={:?}",
                        cmd.id,
                        cmd.shader_id,
                        cmd.opts.position(),
                        cmd.opts.get_clip()
                    );
                }
                DrawCommand::Text(_, opts) => {
                    eprintln!(
                        "[spot][debug] draw text pos={:?} clip={:?}",
                        opts.position(),
                        opts.get_clip()
                    );
                }
                DrawCommand::ClearImage(_, _) | DrawCommand::CopyImage(_, _) => {}
            }
        }

        self.runtime.draw_list.push(drawable);
    }

    pub(crate) fn current_draw_state(&self) -> DrawState {
        self.runtime.current_state
    }

    pub(crate) fn last_image_draw_info(&self, image_id: u32) -> Option<LastImageDrawInfo> {
        self.runtime.last_image_opts.get(&image_id).copied()
    }

    pub(crate) fn push_state(&mut self, state: DrawState) {
        self.runtime.state_stack.push(self.runtime.current_state);
        self.runtime.current_state.position[0] += state.position[0];
        self.runtime.current_state.position[1] += state.position[1];
        self.runtime.current_state.layer += state.layer;

        if let Some(new_clip_abs) = state.clip {
            let merged_clip = if let Some(old_clip_abs) = self.runtime.current_state.clip {
                let x = old_clip_abs[0].as_f32().max(new_clip_abs[0].as_f32());
                let y = old_clip_abs[1].as_f32().max(new_clip_abs[1].as_f32());
                let right = (old_clip_abs[0].as_f32() + old_clip_abs[2].as_f32())
                    .min(new_clip_abs[0].as_f32() + new_clip_abs[2].as_f32());
                let bottom = (old_clip_abs[1].as_f32() + old_clip_abs[3].as_f32())
                    .min(new_clip_abs[1].as_f32() + new_clip_abs[3].as_f32());

                let w = (right - x).max(0.0);
                let h = (bottom - y).max(0.0);
                Some([Pt::from(x), Pt::from(y), Pt::from(w), Pt::from(h)])
            } else {
                Some(new_clip_abs)
            };
            self.runtime.current_state.clip = merged_clip;
        }

        if let Some(sid) = state.shader_id {
            self.runtime.current_state.shader_id = Some(sid);
        }
        if let Some(sopts) = state.shader_opts {
            self.runtime.current_state.shader_opts = Some(sopts);
        }
    }

    pub(crate) fn pop_state(&mut self) {
        if let Some(prev_state) = self.runtime.state_stack.pop() {
            self.runtime.current_state = prev_state;
        }
    }
}