R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
rtype::Simulation Class Reference

Runs a GameInstance on its own thread, tuning::tick_rate times per second. More...

#include </__w/project-rtype/project-rtype/server/include/rtype/simulation.hpp>

Public Types

using Command = std::function< void(GameInstance &instance)>
 A change to make to the game, run on the simulation thread.
 
using TickHandler = std::function< void(GameInstance &instance)>
 Called on the simulation thread after each tick, to read the game.
 

Public Member Functions

 Simulation (GameInstance &instance, std::uint32_t rate=tuning::tick_rate, TickHandler after_tick={}, bool trace=false)
 Starts the thread.
 
 Simulation (const Simulation &)=delete
 
Simulation & operator= (const Simulation &)=delete
 
 Simulation (Simulation &&)=delete
 
Simulation & operator= (Simulation &&)=delete
 
 ~Simulation ()
 Stops the thread and waits for it.
 
void post (Command command)
 Queues a command for the simulation thread. Thread-safe.
 
void stop ()
 Stops the thread after its current tick and waits for it. Thread-safe, but not from a command; calling it again does nothing.
 
std::uint64_t ticks () const noexcept
 Number of ticks run. Thread-safe.
 
const aegis2d::core::Profiler & profiler () const noexcept
 The timings of the ticks, recorded with trace: a frame per tick, the tick and its systems, then after_tick (the snapshots and events).
 
std::uint32_t rate () const noexcept
 Ticks per second the thread aims at.
 

Static Public Attributes

static constexpr std::chrono::seconds report_period {10}
 How often the measured rate is logged.
 

Detailed Description

Runs a GameInstance on its own thread, tuning::tick_rate times per second.

The thread starts in the constructor and ticks the game at a fixed rate, with or without players, using the engine's MainLoop: it sleeps until each step is due and, after a stall, catches up at most FixedTimestep::default_max_frame of steps. Every report_period it logs how many ticks ran and the rate measured.

The game belongs to the thread: other threads (the network) never touch it, they post() commands that run on the thread before the next tick. An exception thrown by a command or by a tick is logged and the thread goes on.

Member Typedef Documentation

◆ Command

using rtype::Simulation::Command = std::function<void(GameInstance& instance)>

A change to make to the game, run on the simulation thread.

◆ TickHandler

using rtype::Simulation::TickHandler = std::function<void(GameInstance& instance)>

Called on the simulation thread after each tick, to read the game.

Constructor & Destructor Documentation

◆ Simulation() [1/3]

rtype::Simulation::Simulation ( GameInstance &  instance,
std::uint32_t  rate = tuning::tick_rate,
TickHandler  after_tick = {},
bool  trace = false 
)
explicit

Starts the thread.

Parameters
instanceGame to run; it must outlive this object and is only used by the thread from now on.
rateTicks per second, tuning::tick_rate by default.
after_tickCalled after each tick (snapshots, events); may be empty.
tracetrue to time each tick, its systems and what follows it, for profiler().

◆ Simulation() [2/3]

rtype::Simulation::Simulation ( const Simulation &  )
delete

◆ Simulation() [3/3]

rtype::Simulation::Simulation ( Simulation &&  )
delete

◆ ~Simulation()

rtype::Simulation::~Simulation ( )

Stops the thread and waits for it.

Member Function Documentation

◆ operator=() [1/2]

Simulation & rtype::Simulation::operator= ( const Simulation &  )
delete

◆ operator=() [2/2]

Simulation & rtype::Simulation::operator= ( Simulation &&  )
delete

◆ post()

void rtype::Simulation::post ( Command  command)

Queues a command for the simulation thread. Thread-safe.

Commands run in the order they were posted, all of them before the next tick. Commands posted after stop() never run.

Parameters
commandChange to make to the game.

◆ profiler()

const aegis2d::core::Profiler & rtype::Simulation::profiler ( ) const
noexcept

The timings of the ticks, recorded with trace: a frame per tick, the tick and its systems, then after_tick (the snapshots and events).

Used by the thread: read it once stop() returned.

Returns
The profiler; off without trace.

◆ rate()

std::uint32_t rtype::Simulation::rate ( ) const
noexcept

Ticks per second the thread aims at.

Returns
The rate given to the constructor.

◆ stop()

void rtype::Simulation::stop ( )

Stops the thread after its current tick and waits for it. Thread-safe, but not from a command; calling it again does nothing.

◆ ticks()

std::uint64_t rtype::Simulation::ticks ( ) const
noexcept

Number of ticks run. Thread-safe.

Returns
The count since the start.

Member Data Documentation

◆ report_period

constexpr std::chrono::seconds rtype::Simulation::report_period {10}
staticconstexpr

How often the measured rate is logged.


The documentation for this class was generated from the following file: