penguins  1.0.0
GameLogEntry Struct Reference

Detailed Description

An entry in the Game::log_buffer, implemented as a tagged union.

The way log entries are stored is basically a union + a type tag. A union in C is a construct which allows interpreting the same data stored in the same memory location in different ways. Sometimes this is used for converting between types (e.g. unpacking an int into the bytes it is comprised of), in our case it is used for the purpose of compact storage of the different log entry types. Basically, the field type indicates the kind of entry stored within the data field, so, for inspecting this struct, you first ALWAYS need to check the type, and then you can access the appropriate entry data:

const GameLogEntry* entry = ...;
switch (entry->type) {
const GameLogPlacement* data = &entry->data.placement;
...
}
const GameLogMovement* data = &entry->data.movement;
...
}
...
}
@ GAME_LOG_ENTRY_PLACEMENT
See GameLogPlacement.
Definition: game.h:90
@ GAME_LOG_ENTRY_MOVEMENT
See GameLogMovement.
Definition: game.h:91
An entry in the Game::log_buffer, implemented as a tagged union.
Definition: game.h:156
union GameLogEntry::GameLogEntryData data
GameLogEntryType type
Definition: game.h:157
A GameLogEntry created by move_penguin.
Definition: game.h:113
A GameLogEntry created by place_penguin.
Definition: game.h:107
GameLogMovement movement
Definition: game.h:162
GameLogPlacement placement
Definition: game.h:161

The data structs of particular entry kinds should contain the delta (difference) between the previous and the next game state – this is enough information to both undo and redo the performed action. Also note that the total union (and consequently this struct) will be as large as the longest struct it can contain, the shorter structs will be padded to the longest length – this padding is wasted memory (currently the largest entry is GameLogMovement, though most of entries will be of this kind anyway, so we aren't wasting much).

See also
https://en.wikipedia.org/wiki/Tagged_union

Definition at line 156 of file game.h.

Data Structures

union  GameLogEntryData
 

Data Fields

GameLogEntryType type
 
union GameLogEntry::GameLogEntryData data
 

Field Documentation

◆ type

◆ data


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