Services Module

Dook is an inherently service based system as mentioned in Working with Services, these classes are how.

Service Locator

class ServiceLocator

Get services safely.

Public Static Functions

static void provide(std::unique_ptr<LevelService> service)

Register a level service.

Parameters:

serviceLevel service unique ptr.

static void provide(std::unique_ptr<GraphicsService> service)

Register a graphics service.

Parameters:

service – Graphics service to register.

static void provide(std::unique_ptr<LoggerService> service)

Register a logger service.

Parameters:

service – Logger service to register.

static void provide(std::unique_ptr<InputService> service)

Register an input service.

Parameters:

service – Input Service to register.

static LevelService &level()

Get the level service.

Returns:

LevelService& Reference to level service.

static GraphicsService &graphics()

Get the registered graphics service.

Returns:

GraphicsService& Reference to graphics server.

static LoggerService &logger()

Get the registered logger.

Returns:

LoggerService& Reference to the logger.

static InputService &input()

Get the registered input service.

Returns:

InputService& Regerence to the input processor service.

Abstract & Null Services

These services are the ones that need to be extended with concrete implementations before being used, each has a null variant which is by default registered to the service locator.

class GraphicsService

This service handles the drawing and rendering.

Subclassed by dook::NullGraphicsService, dook::SDLGraphicsService

Public Functions

inline GraphicsService(Camera viewport)

Construct a new Graphics Service object.

Constructs the window object as well.

Parameters:

viewport – the size of the “screen” and its offset in the total texture.

virtual ~GraphicsService() = default

Destroy the Graphics Service object.

Deallocate the window resources as well.

void tick()

Re-draw the screen.

virtual std::unique_ptr<Texture> load_texture(std::string filename, const Rect &draw_rect) = 0

Load a texture into the memory with the given draw rect.

Parameters:
  • filename – Path to the texture.

  • draw_rect – Draw rectangle information.

Returns:

std::unique_ptr<Texture> Unique ptr to the texture.

virtual std::unique_ptr<Texture> load_texture(std::string filename) = 0

Load a texture into memory from its filename.

Parameters:

filename – Filename to load from.

Returns:

std::unique_ptr<Texture> Unique ptr to the texture.

class InputService

This service user input.

Fun fact, I planned to implement this in co_await, co_yield. This plan was sadly destroyed when I see the boilerplate to do so, and no, I am NOT adding a library for this purpose.

Subclassed by dook::NullInputService, dook::SDLInputService

Public Functions

virtual std::unique_ptr<Command> tick() = 0

Process the next user input.

Returns:

A unique ptr that either includes a user input or nullptr if not available.

Returns:

false otherwise.

virtual bool quit() = 0

Check if the user have inputted a keycharacter that causes the game to be quit.

Returns:

true When user have requested game to quit.

Returns:

false Otherwise.

class LevelService

This service handles level mainloops.

Loads levels, and keeps track of the current level.

Subclassed by dook::NullLevelService

Public Functions

virtual bool load_level(std::string name) = 0

Load the level with the given name.

Parameters:

name – Name of the level.

Returns:

true If the load is sucessful.

Returns:

false otherwise.

void tick()

Tick every single object, make them take action, etc.

class LoggerService

Used to log events.

Subclassed by dook::CommonLoggerService, dook::NullLoggerService

Public Functions

virtual void log(std::string message) = 0

Log a message to… somewhere.

Parameters:

message

virtual void error(std::string message) = 0

Log an error to… somewhere.

Parameters:

message

class NullGraphicsService : public dook::GraphicsService

Noop graphics service.

class NullInputService : public dook::InputService

Null placeholder for input generation.

class NullLevelService : public dook::LevelService
class NullLoggerService : public dook::LoggerService

Common Services

Some services are so common accross various domains that, their concrete implementations are also written in the module.

class CommonLoggerService : public dook::LoggerService

Public Functions

virtual void error(std::string message) override

Log an error to… somewhere.

Parameters:

message

virtual void log(std::string message) override

Log a message to… somewhere.

Parameters:

message