OpenTTD Source 20260815-master-gdf55704e7d
screensaver.cpp File Reference

Code for automatically following vehicles for a screensaver effect. More...

#include "screensaver.h"
#include "stdafx.h"
#include "core/random_func.hpp"
#include "company_base.h"
#include "timer/timer.h"
#include "timer/timer_game_tick.h"
#include "vehicle_base.h"
#include "vehicle_func.h"
#include "vehicle_type.h"
#include "window_func.h"
#include "window_gui.h"
#include "safeguards.h"

Go to the source code of this file.

Functions

static const VehiclePickRandomVehicle ()
 Selects a random vehicle of any type.
static void SwitchVehicle (bool exit_on_invalid=true)
 Switches the current followed vehicle to a new random one.
void ExitScreensaverMode ()
 Exits "Screensaver Mode".
void ToggleScreensaverMode ()
 Enters or exits "Screensaver Mode".

Variables

static const auto _screensaver_check_interval = TimerGameTick::TPeriod(TimerGameTick::Priority::None, 128)
 We use a fixed period for updating the screensaver's followed vehicle, stored in this variable.
static std::unique_ptr< IntervalTimer< TimerGameTick > > _switch_timer
 The screensaver runs on an interval timer, stored in this pointer when it is running and freed when it is no longer in use.

Detailed Description

Code for automatically following vehicles for a screensaver effect.

Definition in file screensaver.cpp.

Function Documentation

◆ ExitScreensaverMode()

void ExitScreensaverMode ( )

Exits "Screensaver Mode".

This function always disables screensaver mode, which is useful for any automated actions that should exit the screensaver.

If the screensaver is not enabled, this function is a no-op.

See also
ToggleScreensaverMode

Definition at line 150 of file screensaver.cpp.

References _switch_timer, GetMainWindow(), and Window::viewport.

Referenced by SwitchVehicle(), and ToggleScreensaverMode().

◆ PickRandomVehicle()

const Vehicle * PickRandomVehicle ( )
static

Selects a random vehicle of any type.

This function can return nullptr if it cannot find any valid vehicles to pick from.

Returns
A pointer to the selected vehicle or nullptr if there are no vehicles.

Definition at line 33 of file screensaver.cpp.

References Begin, CompanyEnd, and IsCompanyBuildableVehicleType().

Referenced by SwitchVehicle().

◆ SwitchVehicle()

void SwitchVehicle ( bool exit_on_invalid = true)
static

Switches the current followed vehicle to a new random one.

This is intended to be used as the callback of an IntervalTimer object to periodically update the main window's followed vehicle so that it rotates between randomly selected vehicles in a "screensaver" effect.

Parameters
exit_on_invalidif true (the default) this function exits screensaver mode if following was cancelled
See also
PickRandomVehicle

Definition at line 76 of file screensaver.cpp.

References Chance16I(), ExitScreensaverMode(), GetMainWindow(), Menu, PickRandomVehicle(), and Window::viewport.

Referenced by ToggleScreensaverMode().

◆ ToggleScreensaverMode()

void ToggleScreensaverMode ( )

Enters or exits "Screensaver Mode".

Screensaver mode is simply rotating the main window's followed vehicle every so often to show off a world full of moving trains, planes, boats, and automobiles.

If not activated, the function allocates a new IntervalTimer which fires every so often to pick new vehicles to follow. There is some randomness in this to keep things interesting.

If "Screensaver Mode" was previously activated, this instead deletes the aforementioned timer and stops following vehicles, returning the camera to the original position.

See also
SwitchVehicle

Definition at line 172 of file screensaver.cpp.

References _screensaver_check_interval, _switch_timer, ExitScreensaverMode(), and SwitchVehicle().

Referenced by MenuClickHelp().

Variable Documentation

◆ _screensaver_check_interval

const auto _screensaver_check_interval = TimerGameTick::TPeriod(TimerGameTick::Priority::None, 128)
static

We use a fixed period for updating the screensaver's followed vehicle, stored in this variable.

See also
ToggleScreensaverMode

Definition at line 131 of file screensaver.cpp.

Referenced by ToggleScreensaverMode().

◆ _switch_timer

std::unique_ptr<IntervalTimer<TimerGameTick> > _switch_timer
static

The screensaver runs on an interval timer, stored in this pointer when it is running and freed when it is no longer in use.

See also
ToggleScreensaverMode

Definition at line 139 of file screensaver.cpp.

Referenced by ExitScreensaverMode(), and ToggleScreensaverMode().