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
use std::sync::{Arc, Mutex};

use crate::scheduler::{Frames, Ticks};

/// Provides access to information and controls for the [Scheduler](crate::scheduler::GameLoop).
///
/// # Examples
///
/// The SchedulerContext can be created directly using the new method.
///
/// ```
/// # use wolf_engine::context::SchedulerContext;
/// #
/// let scheduler_context = SchedulerContext::new();
/// ```
///
/// Once created, the SchedulerContext exposes information about the [GameLoop](crate::scheduler::GameLoop).
///
/// ```
/// # use wolf_engine::context::SchedulerContext;
/// #
/// # let scheduler_context = SchedulerContext::new();
/// #
/// scheduler_context.ticks();
/// scheduler_context.frames();
/// ```
///
/// Tick and frame information can be added to the context.  
///
/// **Note:** These method are only intended for the [Scheduler](crate::scheduler::GameLoop) and other parts of
/// the engine. If you are not providing a custom game loop, you **should not** touch these.
///
/// ```
/// # use wolf_engine::context::SchedulerContext;
/// #
/// # let scheduler_context = SchedulerContext::new();
/// #
/// # assert_eq!(scheduler_context.ticks(), 0, "There should be 0 ticks before add_tick is called");
/// # assert_eq!(scheduler_context.frames(), 0, "There should be 0 frames before add_tick is called");
/// #
/// scheduler_context.add_tick();
/// scheduler_context.add_frame();
/// #
/// # scheduler_context.ticks();
/// # scheduler_context.frames();
/// #
/// # assert_eq!(scheduler_context.ticks(), 1, "1 tick should have been added");
/// # assert_eq!(scheduler_context.frames(), 1, "1 frame should have been added");
/// ```
pub struct SchedulerContext {
    ticks: Arc<Mutex<Ticks>>,
    frames: Arc<Mutex<Frames>>,
}

impl SchedulerContext {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn add_tick(&self) {
        *self.ticks.lock().unwrap() += 1;
    }

    pub fn ticks(&self) -> Ticks {
        *self.ticks.lock().unwrap()
    }

    pub fn add_frame(&self) {
        *self.frames.lock().unwrap() += 1;
    }

    pub fn frames(&self) -> Frames {
        *self.frames.lock().unwrap()
    }
}

impl Default for SchedulerContext {
    fn default() -> Self {
        Self {
            ticks: Arc::from(Mutex::from(0)),
            frames: Arc::from(Mutex::from(0)),
        }
    }
}