shura 0.6.0

A fast cross-plattform 2D component-based game 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
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
use rustc_hash::FxHashMap;

#[cfg(feature = "log")]
use crate::log::info;
#[cfg(feature = "physics")]
use crate::physics::World;
use crate::{
    Arena, BoxedComponent, CallableType, CameraBuffer, ComponentConfig, ComponentController,
    ComponentHandle, ComponentSet, ComponentSetMut, ComponentType, ComponentTypeId, Gpu, Group,
    GroupActivation, GroupHandle, InstanceBuffer, InstanceIndex, InstanceIndices, RenderConfig,
    RenderEncoder, Renderer, TypeIndex, Vector,
};
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::rc::Rc;

#[macro_export]
/// Register multiple components at once
macro_rules! register {
    ($components: expr,[$($C:ty),*]) => {
        {
            $(
                $components.register::<$C>();
            )*
        }
    };
}

#[macro_export]
/// Wrapper for getting multiple [ComponentSets](crate::ComponentSet)
macro_rules! sets {
    ($components: expr, [$($C:ty),*]) => {
        {
            ($(
                $components.set::<$C>(),
            )*)
        }
    };
}

#[macro_export]
/// Wrapper for getting multiple [ComponentSets](crate::ComponentSet) from the specified groups
macro_rules! sets_of {
    ($components: expr, [$(($filter: expr, $C:ty)),*]) => {
        {
            ($(
                $components.set_of::<$C>($filter),
            )*)
        }
    }
}

pub struct RawSet<'a> {
    component_type: &'a mut ComponentType,
    groups: &'a [GroupHandle],
    check: bool,
}

impl<'a> RawSet<'a> {
    pub fn cast<C: ComponentController>(self) -> ComponentSetMut<'a, C> {
        ComponentSetMut::new(self.component_type, self.groups, self.check)
    }
}

#[macro_export]
/// Wrapper around unsafeties of getting multiple [ComponentSets](crate::ComponentSetMut)
macro_rules! sets_mut {
    ($components: expr, [$($C:ty),*]) => {
        {
            let raw: Vec<_> = $components.sets_mut(&[$((shura::ComponentFilter::Active, <$C>::IDENTIFIER), )*]);
            let mut iter = raw.into_iter();
            (
                $(
                    iter.next().unwrap().cast::<$C>(),
                )*
            )
        }
    };
}

#[macro_export]
/// Wrapper around unsafeties of getting multiple [ComponentSets](crate::ComponentSetMut)
macro_rules! sets_mut_of {
    ($components: expr, [$(($filter: expr, $C:ty)),*]) => {
        {
            let raw: Vec<_> = $components.sets_mut(&[$(($filter, <$C>::IDENTIFIER), )*]);
            let mut iter = raw.into_iter();
            (
                $(
                    iter.next().unwrap().cast::<$C>(),
                )*
            )
        }
    };
}

macro_rules! group_filter {
    ($self:ident, $filter: ident) => {
        match $filter {
            ComponentFilter::All => (false, &$self.all_groups[..]),
            ComponentFilter::Active => (false, &$self.active_groups[..]),
            ComponentFilter::Specific(h) => (true, h),
        }
    };
}

fn no_type_error<C: ComponentController>() -> String {
    format!("The type '{}' first needs to be registered!", C::TYPE_NAME)
}

macro_rules! type_ref {
    ($self:ident, $C: ident) => {{
        let idx = $self
            .type_map
            .get(&$C::IDENTIFIER)
            .expect(&no_type_error::<$C>());
        let ty = $self.types.get(idx.0).unwrap();
        ty
    }};
}

macro_rules! type_mut {
    ($self:ident, $C: ident) => {{
        let idx = $self
            .type_map
            .get(&$C::IDENTIFIER)
            .expect(&no_type_error::<$C>());
        let ty = $self.types.get_mut(idx.0).unwrap();
        ty
    }};
}

#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Ord, Eq)]
/// Filter components by groups
pub enum ComponentFilter<'a> {
    All,
    Active,
    Specific(&'a [GroupHandle]),
}

impl<'a> Default for ComponentFilter<'a> {
    fn default() -> Self {
        return ComponentFilter::Active;
    }
}

impl ComponentFilter<'static> {
    pub const DEFAULT_GROUP: Self = ComponentFilter::Specific(&[GroupHandle::DEFAULT_GROUP]);
}

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
/// Access to the component system
pub struct ComponentManager {
    type_map: FxHashMap<ComponentTypeId, TypeIndex>,
    types: Arena<ComponentType>,

    #[cfg_attr(feature = "serde", serde(skip))]
    #[cfg_attr(feature = "serde", serde(default))]
    callables: FxHashMap<TypeIndex, CallableType>,
    #[cfg_attr(feature = "serde", serde(skip))]
    #[cfg_attr(feature = "serde", serde(default))]
    update_priorities: Rc<RefCell<BTreeMap<(i16, ComponentTypeId), TypeIndex>>>,
    #[cfg_attr(feature = "serde", serde(skip))]
    #[cfg_attr(feature = "serde", serde(default))]
    render_priorities: Rc<RefCell<BTreeMap<(i16, ComponentTypeId), TypeIndex>>>,
    #[cfg_attr(feature = "serde", serde(skip))]
    #[cfg_attr(feature = "serde", serde(default))]
    new_priorities: Vec<(i16, i16, ComponentTypeId, TypeIndex)>,
    groups: Arena<Group>,
    active_groups: Vec<GroupHandle>,
    all_groups: Vec<GroupHandle>,
}

impl ComponentManager {
    pub(crate) fn new() -> Self {
        let default_component_group = Group::new(GroupActivation::Always, 0);
        let mut groups = Arena::default();
        let index = groups.insert(default_component_group);
        let group_handle = GroupHandle(index);
        Self {
            types: Default::default(),
            type_map: Default::default(),
            callables: Default::default(),
            new_priorities: Default::default(),
            update_priorities: Default::default(),
            render_priorities: Default::default(),
            all_groups: Vec::from_iter([group_handle]),
            active_groups: Vec::from_iter([group_handle]),
            groups,
        }
    }

    pub(crate) fn update_sets(&mut self, camera: &CameraBuffer) {
        let cam_aabb = camera.model().aabb(Vector::new(0.0, 0.0).into()); // Translation is already applied
        self.active_groups.clear();
        for (index, group) in &mut self.groups {
            if group.intersects_camera(cam_aabb) {
                group.set_active(true);
                self.active_groups.push(GroupHandle(index));
            }
        }
    }

    pub(crate) fn buffer(&mut self, #[cfg(feature = "physics")] world: &mut World, gpu: &Gpu) {
        for (_, ty) in &mut self.types {
            ty.buffer(
                #[cfg(feature = "physics")]
                world,
                &self.active_groups,
                gpu,
            );
        }
    }

    pub(crate) fn apply_priorities(&mut self) {
        if self.new_priorities.len() > 0 {
            let mut update_priorities = self.update_priorities.borrow_mut();
            let mut render_priorities = self.render_priorities.borrow_mut();
            for (update_priority, render_priority, type_id, index) in self.new_priorities.drain(..)
            {
                update_priorities.insert((update_priority, type_id), index);
                render_priorities.insert((render_priority, type_id), index);
            }
        }
    }

    pub(crate) fn update_priorities(
        &self,
    ) -> Rc<RefCell<BTreeMap<(i16, ComponentTypeId), TypeIndex>>> {
        return self.update_priorities.clone();
    }

    pub(crate) fn render_priorities(
        &self,
    ) -> Rc<RefCell<BTreeMap<(i16, ComponentTypeId), TypeIndex>>> {
        return self.render_priorities.clone();
    }

    pub(crate) fn callable(&self, t: &TypeIndex) -> &CallableType {
        self.callables.get(t).unwrap()
    }

    pub(crate) fn callable_mut(&mut self, t: &TypeIndex) -> &mut CallableType {
        self.callables.get_mut(t).unwrap()
    }

    #[cfg(feature = "serde")]
    pub(crate) fn reregister<C: ComponentController>(&mut self) {
        let index = *self.type_map.get(&C::IDENTIFIER).unwrap();
        self.new_priorities.push((
            C::CONFIG.update_priority,
            C::CONFIG.render_priority,
            C::IDENTIFIER,
            index,
        ));
        self.callables.insert(index, CallableType::new::<C>());
    }

    #[cfg(feature = "serde")]
    pub(crate) fn type_ref<C: ComponentController>(&self) -> &ComponentType {
        type_ref!(self, C)
    }

    #[cfg(feature = "serde")]
    pub(crate) fn type_mut<C: ComponentController>(&mut self) -> &mut ComponentType {
        type_mut!(self, C)
    }

    #[cfg(all(feature = "serde", feature = "physics"))]
    pub(crate) fn types(&self) -> &Arena<ComponentType> {
        &self.types
    }

    #[cfg(feature = "physics")]
    pub fn apply_world_mapping(&mut self, world: &mut World) {
        for (_, ty) in &mut self.types {
            ty.apply_world_mapping(world)
        }
    }

    pub fn register<C: ComponentController>(&mut self) {
        self.register_with_config::<C>(C::CONFIG);
    }

    pub fn register_with_config<C: ComponentController>(&mut self, config: ComponentConfig) {
        if !self.type_map.contains_key(&C::IDENTIFIER) {
            let index = self.types.insert_with(|idx| {
                ComponentType::with_config::<C>(config, TypeIndex(idx), &self.groups)
            });
            #[cfg(feature = "log")]
            info!(
                "Register component '{}' with ID '{}'",
                C::TYPE_NAME,
                C::IDENTIFIER
            );
            self.type_map.insert(C::IDENTIFIER, TypeIndex(index));
            self.new_priorities.push((
                C::CONFIG.update_priority,
                C::CONFIG.render_priority,
                C::IDENTIFIER,
                TypeIndex(index),
            ));
            self.callables
                .insert(TypeIndex(index), CallableType::new::<C>());
        }
    }

    pub fn active_groups(&self) -> &[GroupHandle] {
        &self.active_groups
    }

    pub fn all_groups(&self) -> &[GroupHandle] {
        &self.all_groups
    }

    pub fn groups(&self) -> impl Iterator<Item = (GroupHandle, &Group)> + Clone {
        return self
            .groups
            .iter()
            .map(|(index, group)| (GroupHandle(index), group));
    }

    pub fn groups_mut(&mut self) -> impl Iterator<Item = (GroupHandle, &mut Group)> {
        return self
            .groups
            .iter_mut()
            .map(|(index, group)| (GroupHandle(index), group));
    }

    pub fn contains_group(&self, handle: GroupHandle) -> bool {
        return self.groups.contains(handle.0);
    }

    pub fn group(&self, handle: GroupHandle) -> Option<&Group> {
        return self.groups.get(handle.0);
    }

    pub fn group_mut(&mut self, handle: GroupHandle) -> Option<&mut Group> {
        return self.groups.get_mut(handle.0);
    }

    pub fn is_type_of<C: ComponentController>(&self, component: ComponentHandle) -> bool {
        if let Some(ty) = self.type_map.get(&C::IDENTIFIER) {
            return component.type_index() == *ty;
        }
        return false;
    }

    pub fn type_id_of(&self, component: ComponentHandle) -> ComponentTypeId {
        return self.types[component.type_index().0].component_type_id();
    }

    pub fn add_group(&mut self, group: Group) -> GroupHandle {
        let handle = GroupHandle(self.groups.insert(group));
        for (_, ty) in &mut self.types {
            ty.add_group();
        }
        self.all_groups.push(handle);
        return handle;
    }

    pub fn remove_group(&mut self, handle: GroupHandle) -> Option<Group> {
        if handle == GroupHandle::DEFAULT_GROUP {
            panic!("Cannot remove default group!");
        }
        let group = self.groups.remove(handle.0);
        for (_, ty) in &mut self.types {
            ty.remove_group(handle);
        }
        self.all_groups.retain(|h| *h != handle);
        return group;
    }

    pub fn index<C: ComponentController>(&self, index: usize) -> Option<&C> {
        self.index_of(GroupHandle::DEFAULT_GROUP, index)
    }

    pub fn index_mut<C: ComponentController>(&mut self, index: usize) -> Option<&mut C> {
        self.index_mut_of(GroupHandle::DEFAULT_GROUP, index)
    }

    pub fn index_of<C: ComponentController>(&self, group: GroupHandle, index: usize) -> Option<&C> {
        let ty = type_ref!(self, C);
        ty.index(group, index)
    }

    pub fn index_mut_of<C: ComponentController>(
        &mut self,
        group: GroupHandle,
        index: usize,
    ) -> Option<&mut C> {
        let ty = type_mut!(self, C);
        ty.index_mut(group, index)
    }

    pub fn get<C: ComponentController>(&self, handle: ComponentHandle) -> Option<&C> {
        self.types.get(handle.type_index().0).unwrap().get(handle)
    }

    pub fn get_mut<C: ComponentController>(&mut self, handle: ComponentHandle) -> Option<&mut C> {
        self.types
            .get_mut(handle.type_index().0)
            .unwrap()
            .get_mut(handle)
    }

    pub fn get2_mut<C1: ComponentController, C2: ComponentController>(
        &mut self,
        handle1: ComponentHandle,
        handle2: ComponentHandle,
    ) -> (Option<&mut C1>, Option<&mut C2>) {
        assert_ne!(handle1, handle2);
        if handle1.type_index() == handle2.type_index() {
            let ty = self.types.get_mut(handle1.type_index().0).unwrap();
            return ty.get2_mut::<C1, C2>(handle1, handle2);
        } else {
            let (ty1, ty2) = self
                .types
                .get2_mut(handle1.type_index().0, handle2.type_index().0);
            return (
                ty1.unwrap().get_mut::<C1>(handle1),
                ty2.unwrap().get_mut::<C2>(handle2),
            );
        }
    }

    pub fn get2_mut_boxed(
        &mut self,
        handle1: ComponentHandle,
        handle2: ComponentHandle,
    ) -> (Option<&mut BoxedComponent>, Option<&mut BoxedComponent>) {
        assert_ne!(handle1, handle2);
        if handle1.type_index() == handle2.type_index() {
            let ty = self.types.get_mut(handle1.type_index().0).unwrap();
            return ty.get2_mut_boxed(handle1, handle2);
        } else {
            let (ty1, ty2) = self
                .types
                .get2_mut(handle1.type_index().0, handle2.type_index().0);
            return (
                ty1.unwrap().get_boxed_mut(handle1),
                ty2.unwrap().get_boxed_mut(handle2),
            );
        }
    }

    pub fn get_boxed(&self, handle: ComponentHandle) -> Option<&BoxedComponent> {
        self.types
            .get(handle.type_index().0)
            .unwrap()
            .get_boxed(handle)
    }

    pub fn get_boxed_mut(&mut self, handle: ComponentHandle) -> Option<&mut BoxedComponent> {
        self.types
            .get_mut(handle.type_index().0)
            .unwrap()
            .get_boxed_mut(handle)
    }

    pub fn remove<C: ComponentController>(&mut self, handle: ComponentHandle) -> Option<C> {
        self.types
            .get_mut(handle.type_index().0)
            .unwrap()
            .remove(handle)
    }

    pub fn remove_boxed(&mut self, handle: ComponentHandle) -> Option<BoxedComponent> {
        self.types
            .get_mut(handle.type_index().0)
            .unwrap()
            .remove_boxed(handle)
    }

    #[inline]
    pub fn add<C: ComponentController>(&mut self, component: C) -> ComponentHandle {
        self.add_to(GroupHandle::DEFAULT_GROUP, component)
    }

    pub fn add_to<C: ComponentController>(
        &mut self,
        group_handle: GroupHandle,
        component: C,
    ) -> ComponentHandle {
        let ty = type_mut!(self, C);
        ty.add(group_handle, component)
    }

    #[inline]
    pub fn add_many<C: ComponentController>(
        &mut self,
        components: impl IntoIterator<Item = C>,
    ) -> Vec<ComponentHandle> {
        self.add_many_to(GroupHandle::DEFAULT_GROUP, components)
    }

    pub fn add_many_to<C: ComponentController>(
        &mut self,
        group_handle: GroupHandle,
        components: impl IntoIterator<Item = C>,
    ) -> Vec<ComponentHandle> {
        let ty = type_mut!(self, C);
        ty.add_many::<C>(group_handle, components)
    }

    #[inline]
    pub fn add_with<C: ComponentController + ComponentController>(
        &mut self,
        create: impl FnOnce(ComponentHandle) -> C,
    ) -> ComponentHandle {
        self.add_with_to(GroupHandle::DEFAULT_GROUP, create)
    }

    pub fn add_with_to<C: ComponentController + ComponentController>(
        &mut self,
        group_handle: GroupHandle,
        create: impl FnOnce(ComponentHandle) -> C,
    ) -> ComponentHandle {
        let ty = type_mut!(self, C);
        ty.add_with::<C>(group_handle, create)
    }

    #[inline]
    pub fn remove_all<C: ComponentController>(&mut self) -> Vec<C> {
        self.remove_all_of(ComponentFilter::All)
    }

    pub fn remove_all_of<C: ComponentController>(&mut self, filter: ComponentFilter) -> Vec<C> {
        let groups = group_filter!(self, filter).1;
        let ty = type_mut!(self, C);
        ty.remove_all(groups)
    }

    #[inline]
    pub fn force_buffer<C: ComponentController>(&mut self) {
        self.force_buffer_of::<C>(ComponentFilter::All)
    }

    pub fn force_buffer_of<C: ComponentController>(&mut self, filter: ComponentFilter) {
        let groups = group_filter!(self, filter).1;
        let ty = type_mut!(self, C);
        ty.force_buffer(groups)
    }

    #[inline]
    pub fn len<C: ComponentController>(&self) -> usize {
        self.len_of::<C>(ComponentFilter::All)
    }

    pub fn len_of<C: ComponentController>(&self, filter: ComponentFilter) -> usize {
        let groups = group_filter!(self, filter).1;
        let ty = type_ref!(self, C);
        ty.len(groups)
    }

    #[inline]
    pub fn iter<C: ComponentController>(&self) -> impl DoubleEndedIterator<Item = &C> {
        self.iter_of::<C>(ComponentFilter::Active)
    }

    pub fn iter_of<C: ComponentController>(
        &self,
        filter: ComponentFilter,
    ) -> impl DoubleEndedIterator<Item = &C> {
        let groups = group_filter!(self, filter).1;
        let ty = type_ref!(self, C);
        ty.iter(groups)
    }

    #[inline]
    pub fn iter_mut<C: ComponentController>(&mut self) -> impl DoubleEndedIterator<Item = &mut C> {
        self.iter_mut_of::<C>(ComponentFilter::Active)
    }

    pub fn iter_mut_of<C: ComponentController>(
        &mut self,
        filter: ComponentFilter,
    ) -> impl DoubleEndedIterator<Item = &mut C> {
        let (check, groups) = group_filter!(self, filter);
        let ty = type_mut!(self, C);
        ty.iter_mut(groups, check)
    }

    #[inline]
    pub fn iter_render<C: ComponentController>(
        &self,
    ) -> impl DoubleEndedIterator<Item = (&InstanceBuffer, InstanceIndex, &C)> {
        self.iter_render_of::<C>(ComponentFilter::Active)
    }

    pub fn iter_render_of<C: ComponentController>(
        &self,
        filter: ComponentFilter,
    ) -> impl DoubleEndedIterator<Item = (&InstanceBuffer, InstanceIndex, &C)> {
        let groups = group_filter!(self, filter).1;
        let ty = type_ref!(self, C);
        ty.iter_render(groups)
    }

    #[inline]
    pub fn iter_with_handles<'a, C: ComponentController>(
        &'a self,
    ) -> impl DoubleEndedIterator<Item = (ComponentHandle, &'a C)> {
        self.iter_with_handles_of::<C>(ComponentFilter::Active)
    }

    pub fn iter_with_handles_of<'a, C: ComponentController>(
        &'a self,
        filter: ComponentFilter<'a>,
    ) -> impl DoubleEndedIterator<Item = (ComponentHandle, &'a C)> {
        let groups = group_filter!(self, filter).1;
        let ty = type_ref!(self, C);
        ty.iter_with_handles(groups)
    }

    #[inline]
    pub fn iter_mut_with_handles<'a, C: ComponentController>(
        &'a mut self,
    ) -> impl DoubleEndedIterator<Item = (ComponentHandle, &'a mut C)> {
        self.iter_mut_with_handles_of::<C>(ComponentFilter::Active)
    }

    pub fn iter_mut_with_handles_of<'a, C: ComponentController>(
        &'a mut self,
        filter: ComponentFilter<'a>,
    ) -> impl DoubleEndedIterator<Item = (ComponentHandle, &'a mut C)> {
        let (check, groups) = group_filter!(self, filter);
        let ty = type_mut!(self, C);
        ty.iter_mut_with_handles(groups, check)
    }

    #[inline]
    pub fn set<'a, C: ComponentController>(&'a self) -> ComponentSet<'a, C> {
        self.set_of(ComponentFilter::Active)
    }

    pub fn set_of<'a, C: ComponentController>(
        &'a self,
        filter: ComponentFilter<'a>,
    ) -> ComponentSet<'a, C> {
        let groups = group_filter!(self, filter).1;
        let ty = type_ref!(self, C);
        return ComponentSet::new(ty, groups);
    }

    #[inline]
    pub fn set_mut<'a, C: ComponentController>(&'a mut self) -> ComponentSetMut<'a, C> {
        self.set_mut_of(ComponentFilter::Active)
    }

    pub fn sets_mut<'a>(
        &'a mut self,
        ids: &[(ComponentFilter<'a>, ComponentTypeId)],
    ) -> Vec<RawSet<'a>> {
        let mut result = Vec::with_capacity(ids.len());
        for (index, value) in ids.iter().enumerate() {
            for other in ids.iter().skip(index + 1) {
                if value == other {
                    panic!("Duplicate component type!");
                }
            }
        }

        // We can use unsafe here because we validate for no duplicates
        unsafe {
            for (filter, id) in ids {
                let filter = *filter;
                let (check, groups) = group_filter!(self, filter);
                let idx = self.type_map.get(id).unwrap();
                let ty = self.types.get(idx.0).unwrap();
                let ptr = ty as *const _ as *mut _;
                let component_type = &mut *ptr;
                result.push(RawSet {
                    groups,
                    component_type,
                    check,
                });
            }
        }
        return result;
    }

    pub fn set_mut_of<'a, C: ComponentController>(
        &'a mut self,
        filter: ComponentFilter<'a>,
    ) -> ComponentSetMut<'a, C> {
        let (check, groups) = group_filter!(self, filter);
        let ty = type_mut!(self, C);
        return ComponentSetMut::new(ty, groups, check);
    }

    #[inline]
    pub fn for_each<C: ComponentController>(&self, each: impl FnMut(&C)) {
        self.for_each_of(ComponentFilter::Active, each)
    }

    pub fn for_each_of<C: ComponentController>(
        &self,
        filter: ComponentFilter,
        each: impl FnMut(&C),
    ) {
        let groups = group_filter!(self, filter).1;
        let ty = type_ref!(self, C);
        ty.for_each(groups, each);
    }

    #[inline]
    pub fn for_each_mut<C: ComponentController>(&mut self, each: impl FnMut(&mut C)) {
        self.for_each_mut_of(ComponentFilter::Active, each)
    }

    pub fn for_each_mut_of<C: ComponentController>(
        &mut self,
        filter: ComponentFilter,
        each: impl FnMut(&mut C),
    ) {
        let groups = group_filter!(self, filter).1;
        let ty = type_mut!(self, C);
        ty.for_each_mut(groups, each);
    }

    #[inline]
    pub fn retain<C: ComponentController>(&mut self, keep: impl FnMut(&mut C) -> bool) {
        self.retain_of(ComponentFilter::Active, keep)
    }

    pub fn retain_of<C: ComponentController>(
        &mut self,
        filter: ComponentFilter,
        keep: impl FnMut(&mut C) -> bool,
    ) {
        let groups = group_filter!(self, filter).1;
        let ty = type_mut!(self, C);
        ty.retain(groups, keep);
    }

    pub fn render_each<'a, C: ComponentController>(
        &'a self,
        encoder: &'a mut RenderEncoder,
        config: RenderConfig<'a>,
        each: impl FnMut(&mut Renderer<'a>, &'a C, InstanceIndex),
    ) -> Renderer<'a> {
        let ty = type_ref!(self, C);
        ty.render_each(encoder, config, each)
    }

    pub fn render_single<'a, C: ComponentController>(
        &'a self,
        encoder: &'a mut RenderEncoder,
        config: RenderConfig<'a>,
        each: impl FnOnce(&mut Renderer<'a>, &'a C, InstanceIndex),
    ) -> Renderer<'a> {
        let ty = type_ref!(self, C);
        ty.render_single(encoder, config, each)
    }

    pub fn render_each_prepare<'a, C: ComponentController>(
        &'a self,
        encoder: &'a mut RenderEncoder,
        config: RenderConfig<'a>,
        prepare: impl FnOnce(&mut Renderer<'a>),
        each: impl FnMut(&mut Renderer<'a>, &'a C, InstanceIndex),
    ) -> Renderer<'a> {
        let ty = type_ref!(self, C);
        ty.render_each_prepare(encoder, config, prepare, each)
    }

    pub fn render_all<'a, C: ComponentController>(
        &'a self,
        encoder: &'a mut RenderEncoder,
        config: RenderConfig<'a>,
        all: impl FnMut(&mut Renderer<'a>, InstanceIndices),
    ) -> Renderer<'a> {
        let ty = type_ref!(self, C);
        ty.render_all::<C>(encoder, config, all)
    }

    pub fn single<C: ComponentController>(&self) -> Option<&C> {
        let ty = type_ref!(self, C);
        ty.single()
    }

    pub fn single_mut<C: ComponentController>(&mut self) -> Option<&mut C> {
        let ty = type_mut!(self, C);
        ty.single_mut()
    }

    pub fn remove_single<C: ComponentController>(&mut self) -> Option<C> {
        let ty = type_mut!(self, C);
        ty.remove_single()
    }

    pub fn set_single<C: ComponentController>(&mut self, new: C) -> ComponentHandle {
        let ty = type_mut!(self, C);
        ty.set_single(new)
    }

    pub fn set_single_with<C: ComponentController>(
        &mut self,
        create: impl FnOnce(ComponentHandle) -> C,
    ) -> ComponentHandle {
        let ty = type_mut!(self, C);
        ty.set_single_with(create)
    }
}