OpenTTD Source: src/newgrf_animation_base.h Source File

OpenTTD Source 12.2

newgrf_animation_base.h
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10 /* No inclusion guards as this file must only be included from .cpp files. */
11
12 #include "animated_tile_func.h"
13 #include "core/random_func.hpp"
14 #include "date_func.h"
15 #include "viewport_func.h"
16 #include "newgrf_animation_type.h"
17 #include "newgrf_callbacks.h"
18 #include "tile_map.h"
19
28 template <typename Tbase, typename Tspec, typename Tobj, typename Textra, uint16 (*GetCallback)(CallbackID callback, uint32 param1, uint32 param2, const Tspec *statspec, Tobj *st, TileIndex tile, Textra extra_data)>
29 struct AnimationBase {
38 static void AnimateTile(const Tspec *spec, Tobj *obj, TileIndex tile, bool random_animation, Textra extra_data = 0)
39 {
40 assert(spec != nullptr);
41
42 /* Acquire the animation speed from the NewGRF. */
43 uint8 animation_speed = spec->animation.speed;
44 if (HasBit(spec->callback_mask, Tbase::cbm_animation_speed)) {
45 uint16 callback = GetCallback(Tbase::cb_animation_speed, 0, 0, spec, obj, tile, extra_data);
46 if (callback != CALLBACK_FAILED) {
47 if (callback >= 0x100 && spec->grf_prop.grffile->grf_version >= 8) ErrorUnknownCallbackResult(spec->grf_prop.grffile->grfid, Tbase::cb_animation_speed, callback);
48 animation_speed = Clamp(callback & 0xFF, 0, 16);
49 }
50 }
51
52 /* An animation speed of 2 means the animation frame changes 4 ticks, and
53 * increasing this value by one doubles the wait. 0 is the minimum value
54 * allowed for animation_speed, which corresponds to 30ms, and 16 is the
55 * maximum, corresponding to around 33 minutes. */
56 if (_tick_counter % (1 << animation_speed) != 0) return;
57
58 uint8 frame = GetAnimationFrame(tile);
59 uint8 num_frames = spec->animation.frames;
60
61 bool frame_set_by_callback = false;
62
63 if (HasBit(spec->callback_mask, Tbase::cbm_animation_next_frame)) {
64 uint16 callback = GetCallback(Tbase::cb_animation_next_frame, random_animation ? Random() : 0, 0, spec, obj, tile, extra_data);
65
66 if (callback != CALLBACK_FAILED) {
67 frame_set_by_callback = true;
68
69 switch (callback & 0xFF) {
70 case 0xFF:
71 DeleteAnimatedTile(tile);
72 break;
73
74 case 0xFE:
75 frame_set_by_callback = false;
76 break;
77
78 default:
79 frame = callback & 0xFF;
80 break;
81 }
82
83 /* If the lower 7 bits of the upper byte of the callback
84 * result are not empty, it is a sound effect. */
85 if (GB(callback, 8, 7) != 0 && _settings_client.sound.ambient) PlayTileSound(spec->grf_prop.grffile, GB(callback, 8, 7), tile);
86 }
87 }
88
89 if (!frame_set_by_callback) {
90 if (frame < num_frames) {
91 frame++;
92 } else if (frame == num_frames && spec->animation.status == ANIM_STATUS_LOOPING) {
93 /* This animation loops, so start again from the beginning */
94 frame = 0;
95 } else {
96 /* This animation doesn't loop, so stay here */
97 DeleteAnimatedTile(tile);
98 }
99 }
100
101 SetAnimationFrame(tile, frame);
102 MarkTileDirtyByTile(tile);
103 }
104
117 static void ChangeAnimationFrame(CallbackID cb, const Tspec *spec, Tobj *obj, TileIndex tile, uint32 random_bits, uint32 trigger, Textra extra_data = 0)
118 {
119 uint16 callback = GetCallback(cb, random_bits, trigger, spec, obj, tile, extra_data);
120 if (callback == CALLBACK_FAILED) return;
121
122 switch (callback & 0xFF) {
123 case 0xFD: /* Do nothing. */ break;
124 case 0xFE: AddAnimatedTile(tile); break;
125 case 0xFF: DeleteAnimatedTile(tile); break;
126 default:
127 SetAnimationFrame(tile, callback);
128 AddAnimatedTile(tile);
129 break;
130 }
131
132 /* If the lower 7 bits of the upper byte of the callback
133 * result are not empty, it is a sound effect. */
134 if (GB(callback, 8, 7) != 0 && _settings_client.sound.ambient) PlayTileSound(spec->grf_prop.grffile, GB(callback, 8, 7), tile);
135 }
136 };
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
AnimationBase::ChangeAnimationFrame
static void ChangeAnimationFrame(CallbackID cb, const Tspec *spec, Tobj *obj, TileIndex tile, uint32 random_bits, uint32 trigger, Textra extra_data=0)
Check a callback to determine what the next animation step is and execute that step.
Definition: newgrf_animation_base.h:117
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:52
newgrf_callbacks.h
AnimationBase::AnimateTile
static void AnimateTile(const Tspec *spec, Tobj *obj, TileIndex tile, bool random_animation, Textra extra_data=0)
Animate a single tile.
Definition: newgrf_animation_base.h:38
CallbackID
CallbackID
List of implemented NewGRF callbacks.
Definition: newgrf_callbacks.h:20
SoundSettings::ambient
bool ambient
Play ambient, industry and town sounds.
Definition: settings_type.h:214
GetAnimationFrame
static byte GetAnimationFrame(TileIndex t)
Get the current animation frame.
Definition: tile_map.h:250
tile_map.h
PlayTileSound
void PlayTileSound(const GRFFile *file, SoundID sound_id, TileIndex tile)
Play a NewGRF sound effect at the location of a specific tile.
Definition: newgrf_sound.cpp:219
ClientSettings::sound
SoundSettings sound
sound effect settings
Definition: settings_type.h:597
_tick_counter
uint16 _tick_counter
Ever incrementing (and sometimes wrapping) tick counter for setting off various events.
Definition: date.cpp:30
date_func.h
viewport_func.h
animated_tile_func.h
CALLBACK_FAILED
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
Definition: newgrf_callbacks.h:404
Clamp
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:77
DeleteAnimatedTile
void DeleteAnimatedTile(TileIndex tile)
Removes the given tile from the animated tile table.
Definition: animated_tile.cpp:26
MarkTileDirtyByTile
void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset, int tile_height_override)
Mark a tile given by its index dirty for repaint.
Definition: viewport.cpp:1987
AnimationBase
Helper class for a unified approach to NewGRF animation.
Definition: newgrf_animation_base.h:29
random_func.hpp
ANIM_STATUS_LOOPING
static const uint8 ANIM_STATUS_LOOPING
Animation is looping.
Definition: newgrf_animation_type.h:14
ErrorUnknownCallbackResult
void ErrorUnknownCallbackResult(uint32 grfid, uint16 cbid, uint16 cb_res)
Record that a NewGRF returned an unknown/invalid callback result.
Definition: newgrf_commons.cpp:516
SetAnimationFrame
static void SetAnimationFrame(TileIndex t, byte frame)
Set a new animation frame.
Definition: tile_map.h:262
newgrf_animation_type.h
AddAnimatedTile
void AddAnimatedTile(TileIndex tile)
Add the given tile to the animated tile table (if it does not exist on that table yet).
Definition: animated_tile.cpp:41