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
use crate::Context;
use std::any::{Any, TypeId};
use std::cell::RefCell;
use std::rc::Rc;
use std::time::Duration;
pub(crate) type SceneFactory = Box<dyn Fn(&mut Context) -> Box<dyn Spot> + Send + Sync>;
pub(crate) struct ScenePayload {
pub(crate) type_id: TypeId,
pub(crate) value: Rc<dyn Any>,
}
pub(crate) struct ScenePayloadTypeId(pub(crate) TypeId);
pub(crate) struct SceneSwitchRequest {
pub(crate) factory: SceneFactory,
pub(crate) payload: Option<ScenePayload>,
}
thread_local! {
static SCENE_SWITCH_REQUEST: RefCell<Option<SceneSwitchRequest>> = const { RefCell::new(None) };
static QUIT_REQUEST: RefCell<bool> = const { RefCell::new(false) };
}
fn request_scene_switch<F>(factory: F, payload: Option<ScenePayload>)
where
F: Fn(&mut Context) -> Box<dyn Spot> + Send + Sync + 'static,
{
SCENE_SWITCH_REQUEST.with(|request| {
*request.borrow_mut() = Some(SceneSwitchRequest {
factory: Box::new(factory),
payload,
});
});
}
pub(crate) fn take_scene_switch_request() -> Option<SceneSwitchRequest> {
SCENE_SWITCH_REQUEST.with(|request| request.borrow_mut().take())
}
/// Signals the engine to quit the application.
pub fn quit() {
QUIT_REQUEST.with(|request| *request.borrow_mut() = true);
}
pub(crate) fn take_quit_request() -> bool {
QUIT_REQUEST.with(|request| request.replace(false))
}
/// Switches to a new scene of type `T`.
///
/// The current scene will be removed and the new scene will be initialized.
pub fn switch_scene<T: Spot + 'static>() {
request_scene_switch(|ctx| Box::new(T::initialize(ctx)), None);
}
/// Switches to a new scene of type `T` and passes a payload.
///
/// The payload can be retrieved in the new scene's `initialize` method
/// using `ctx.take_resource::<P>()`.
pub fn switch_scene_with<T: Spot + 'static, P: Any>(payload: P) {
request_scene_switch(
|ctx| Box::new(T::initialize(ctx)),
Some(ScenePayload {
type_id: TypeId::of::<P>(),
value: Rc::new(payload),
}),
);
}
/// The core trait for defining application logic and rendering.
///
/// Implement this trait on your application state struct to handle lifecycle
/// events, updates, and drawing.
///
/// # Example
/// ```no_run
/// use spottedcat::{Context, Spot, Image, DrawOption};
///
/// struct MyScene;
///
/// impl Spot for MyScene {
/// fn initialize(ctx: &mut Context) -> Self { Self }
///
/// fn draw(&mut self, ctx: &mut Context, screen: Image) {
/// // Draw to the provided screen
/// }
/// }
/// ```
pub trait Spot {
/// Initializes the scene. This is called once when the scene is created.
fn initialize(ctx: &mut Context) -> Self
where
Self: Sized;
/// Called every frame to draw the scene.
///
/// The `screen` image represents the primary backbuffer/window. All draw commands
/// should be issued via a target image's `draw` method (e.g., `screen.draw(ctx, &image, opts)`).
fn draw(&mut self, ctx: &mut Context, screen: crate::Image);
/// Called every frame to update the scene logic.
///
/// # Arguments
/// * `dt` - The time elapsed since the last frame.
fn update(&mut self, _ctx: &mut Context, _dt: Duration) {}
/// Called when the application is resumed (e.g., from background).
fn resumed(&mut self, _ctx: &mut Context) {}
/// Called when the application is suspended (e.g., to background).
fn suspended(&mut self, _ctx: &mut Context) {}
/// Called when the scene is being removed or the application is quitting.
fn remove(&mut self, _ctx: &mut Context) {}
}