tinygame_core/
core_plugin.rs1use sdl3_sys::{events::SDL_Event, init::*};
2
3use crate::{
4 AppResult,
5 core::{
6 plugins::{Plugin, PluginMeta},
7 utils::{EventQueue, get_error},
8 world::World,
9 },
10};
11
12#[derive(Clone)]
13pub struct CoreSettings {
14 pub init_sensors: bool,
15 pub init_cameras: bool,
16 pub init_controllers: bool,
17}
18
19impl Default for CoreSettings {
20 fn default() -> Self {
21 Self {
22 init_sensors: false,
23 init_cameras: false,
24 init_controllers: false,
25 }
26 }
27}
28
29pub struct Core {
30 pub sdl_events: EventQueue<SDL_Event>,
31}
32
33impl Core {
34 pub fn new() -> Self {
35 Self {
36 sdl_events: EventQueue::new(),
37 }
38 }
39}
40
41pub struct CorePlugin;
42
43impl Plugin for CorePlugin {
44 fn get_meta() -> PluginMeta {
45 PluginMeta {
46 name: "core",
47 depends_on: &[],
48 }
49 }
50
51 fn init(world: &mut World) -> AppResult {
52 let settings = world
53 .try_get::<CoreSettings>()
54 .map(|settings| settings.clone())
55 .unwrap_or_default();
56
57 let mut flags = SDL_INIT_EVENTS | SDL_INIT_VIDEO | SDL_INIT_AUDIO;
58 if settings.init_controllers {
59 flags |= SDL_INIT_GAMEPAD;
60 flags |= SDL_INIT_HAPTIC;
61 }
62 if settings.init_sensors {
63 flags |= SDL_INIT_SENSOR;
64 }
65 if settings.init_cameras {
66 flags |= SDL_INIT_CAMERA;
67 }
68
69 if !unsafe { SDL_Init(flags) } {
70 let _ = get_error();
71 return AppResult::Failure;
72 }
73
74 world.insert(Core::new());
75
76 AppResult::Continue
77 }
78
79 fn step(world: &World) -> AppResult {
80 let mut core = world.get_mut::<Core>();
81
82 core.sdl_events.clear();
83 core.sdl_events.collect();
84
85 AppResult::Continue
86 }
87
88 fn exit(_world: &World) {
89 unsafe { SDL_Quit() }
90 }
91}