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
use crate::{
    menu::main::MainMenuState,
    settings::get_fps,
    ui,
    window::{Event, EventLoop},
    Direction, GlobalState, PlayState, PlayStateResult,
};
use common_base::{prof_span, span};
use std::{mem, time::Duration};
use tracing::debug;

pub fn run(mut global_state: GlobalState, event_loop: EventLoop, server: Option<String>) {
    // Set up the initial play state.
    let mut states: Vec<Box<dyn PlayState>> =
        vec![Box::new(MainMenuState::new(&mut global_state, server))];
    states.last_mut().map(|current_state| {
        current_state.enter(&mut global_state, Direction::Forwards);
        let current_state = current_state.name();
        debug!(?current_state, "Started game with state");
    });

    // Used to ignore every other `MainEventsCleared`
    // This is a workaround for a bug on macos in which mouse motion events are only
    // reported every other cycle of the event loop
    // See: https://github.com/rust-windowing/winit/issues/1418
    let mut polled_twice = false;

    let mut poll_span = None;
    let mut event_span = None;

    event_loop.run(move |event, _, control_flow| {
        // Continuously run loop since we handle sleeping
        *control_flow = winit::event_loop::ControlFlow::Poll;

        #[cfg(feature = "egui-ui")]
        {
            let enabled_for_current_state =
                states.last().map_or(false, |state| state.egui_enabled());

            // Only send events to the egui UI when it is being displayed.
            if enabled_for_current_state && global_state.settings.interface.egui_enabled() {
                global_state.egui_state.platform.handle_event(&event);
                if global_state.egui_state.platform.captures_event(&event) {
                    return;
                }
            }
        }

        // Don't pass resize events to the ui, `Window` is responsible for:
        // - deduplicating them
        // - generating resize events for the ui
        // - ensuring consistent sizes are passed to the ui and to the renderer
        if !matches!(&event, winit::event::Event::WindowEvent {
            event: winit::event::WindowEvent::Resized(_),
            ..
        }) {
            // Get events for the ui.
            if let Some(event) = ui::Event::try_from(&event, global_state.window.window()) {
                global_state.window.send_event(Event::Ui(event));
            }
            // iced ui events
            // TODO: no clone
            if let winit::event::Event::WindowEvent { event, .. } = &event {
                let window = &mut global_state.window;
                if let Some(event) =
                    ui::ice::window_event(event, window.scale_factor(), window.modifiers())
                {
                    window.send_event(Event::IcedUi(event));
                }
            }
        }

        match event {
            winit::event::Event::NewEvents(_) => {
                event_span.take();
                prof_span!(span, "Process Events");
                event_span = Some(span);
            },
            winit::event::Event::MainEventsCleared => {
                event_span.take();
                poll_span.take();
                if polled_twice {
                    handle_main_events_cleared(&mut states, control_flow, &mut global_state);
                }
                prof_span!(span, "Poll Winit");
                poll_span = Some(span);
                polled_twice = !polled_twice;
            },
            winit::event::Event::WindowEvent { event, .. } => {
                span!(_guard, "Handle WindowEvent");

                if let winit::event::WindowEvent::Focused(focused) = event {
                    global_state.audio.set_master_volume(if focused {
                        global_state.settings.audio.master_volume.get_checked()
                    } else {
                        global_state
                            .settings
                            .audio
                            .inactive_master_volume_perc
                            .get_checked()
                            * global_state.settings.audio.master_volume.get_checked()
                    });
                }

                global_state
                    .window
                    .handle_window_event(event, &mut global_state.settings)
            },
            winit::event::Event::DeviceEvent { event, .. } => {
                span!(_guard, "Handle DeviceEvent");
                global_state.window.handle_device_event(event)
            },
            winit::event::Event::LoopDestroyed => {
                // Save any unsaved changes to settings and profile
                global_state
                    .settings
                    .save_to_file_warn(&global_state.config_dir);
                global_state
                    .profile
                    .save_to_file_warn(&global_state.config_dir);
            },
            _ => {},
        }
    });
}

fn handle_main_events_cleared(
    states: &mut Vec<Box<dyn PlayState>>,
    control_flow: &mut winit::event_loop::ControlFlow,
    global_state: &mut GlobalState,
) {
    span!(guard, "Handle MainEventsCleared");
    // Screenshot / Fullscreen toggle
    global_state
        .window
        .resolve_deduplicated_events(&mut global_state.settings, &global_state.config_dir);
    // Run tick here

    // What's going on here?
    // ---------------------
    // The state system used by Voxygen allows for the easy development of
    // stack-based menus. For example, you may want a "title" state
    // that can push a "main menu" state on top of it, which can in
    // turn push a "settings" state or a "game session" state on top of it.
    // The code below manages the state transfer logic automatically so that we
    // don't have to re-engineer it for each menu we decide to add
    // to the game.
    let mut exit = true;
    while let Some(state_result) = states.last_mut().map(|last| {
        let events = global_state.window.fetch_events();
        last.tick(global_state, events)
    }) {
        // Implement state transfer logic.
        match state_result {
            PlayStateResult::Continue => {
                exit = false;
                break;
            },
            PlayStateResult::Shutdown => {
                // Clear the Discord activity before shutting down
                #[cfg(feature = "discord")]
                global_state.discord.clear_activity();

                debug!("Shutting down all states...");
                while states.last().is_some() {
                    states.pop().map(|old_state| {
                        debug!("Popped state '{}'.", old_state.name());
                        global_state.on_play_state_changed();
                    });
                }
            },
            PlayStateResult::Pop => {
                states.pop().map(|old_state| {
                    debug!("Popped state '{}'.", old_state.name());
                    global_state.on_play_state_changed();
                });
                states.last_mut().map(|new_state| {
                    new_state.enter(global_state, Direction::Backwards);
                });
            },
            PlayStateResult::Push(mut new_state) => {
                new_state.enter(global_state, Direction::Forwards);
                debug!("Pushed state '{}'.", new_state.name());
                states.push(new_state);
                global_state.on_play_state_changed();
            },
            PlayStateResult::Switch(mut new_state) => {
                new_state.enter(global_state, Direction::Forwards);
                states.last_mut().map(|old_state| {
                    debug!(
                        "Switching to state '{}' from state '{}'.",
                        new_state.name(),
                        old_state.name()
                    );
                    mem::swap(old_state, &mut new_state);
                    global_state.on_play_state_changed();
                });
            },
        }
    }

    if exit {
        *control_flow = winit::event_loop::ControlFlow::Exit;
    }

    let mut capped_fps = false;

    drop(guard);

    #[cfg(feature = "egui-ui")]
    let scale_factor = global_state.window.window().scale_factor() as f32;

    if let Some(last) = states.last_mut() {
        capped_fps = last.capped_fps();

        span!(guard, "Render");

        // Render the screen using the global renderer
        if let Some(mut drawer) = global_state
            .window
            .renderer_mut()
            .start_recording_frame(last.globals_bind_group())
            .expect("Unrecoverable render error when starting a new frame!")
        {
            if global_state.clear_shadows_next_frame {
                drawer.clear_shadows();
            }

            last.render(&mut drawer, &global_state.settings);

            #[cfg(feature = "egui-ui")]
            if last.egui_enabled() && global_state.settings.interface.egui_enabled() {
                drawer.draw_egui(&mut global_state.egui_state.platform, scale_factor);
            }
        };
        if global_state.clear_shadows_next_frame {
            global_state.clear_shadows_next_frame = false;
        }

        drop(guard);
    }

    if !exit {
        // Wait for the next tick.
        span!(guard, "Main thread sleep");

        // Enforce an FPS cap for the non-game session play states to prevent them
        // running at hundreds/thousands of FPS resulting in high GPU usage for
        // effectively doing nothing.
        let max_fps = get_fps(global_state.settings.graphics.max_fps);
        let max_background_fps = u32::min(
            max_fps,
            get_fps(global_state.settings.graphics.max_background_fps),
        );
        let max_fps_focus_adjusted = if global_state.window.focused {
            max_fps
        } else {
            max_background_fps
        };

        const TITLE_SCREEN_FPS_CAP: u32 = 60;

        let target_fps = if capped_fps {
            u32::min(TITLE_SCREEN_FPS_CAP, max_fps_focus_adjusted)
        } else {
            max_fps_focus_adjusted
        };

        global_state
            .clock
            .set_target_dt(Duration::from_secs_f64(1.0 / target_fps as f64));
        global_state.clock.tick();
        drop(guard);
        #[cfg(feature = "tracy")]
        common_base::tracy_client::frame_mark();

        // Maintain global state.
        global_state.maintain(global_state.clock.dt());
    }
}