OpenTTD Source: src/texteff.cpp Source File

OpenTTD Source 14.0-beta1

texteff.cpp
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 #include "stdafx.h"
11 #include "texteff.hpp"
12 #include "transparency.h"
13 #include "strings_func.h"
14 #include "viewport_func.h"
15 #include "settings_type.h"
16 #include "command_type.h"
17 #include "timer/timer.h"
18 #include "timer/timer_window.h"
19
20 #include "safeguards.h"
21
23 struct TextEffect : public ViewportSign {
24 std::vector<StringParameterBackup> params;
25 StringID string_id;
26 uint8_t duration;
27 TextEffectMode mode;
28
30 void Reset()
31 {
32 this->MarkDirty();
33 this->width_normal = 0;
34 this->string_id = INVALID_STRING_ID;
35 }
36 };
37
38 static std::vector<struct TextEffect> _text_effects;
39
40 /* Text Effects */
41 TextEffectID AddTextEffect(StringID msg, int center, int y, uint8_t duration, TextEffectMode mode)
42 {
43 if (_game_mode == GM_MENU) return INVALID_TE_ID;
44
45 auto it = std::find_if(std::begin(_text_effects), std::end(_text_effects), [](const TextEffect &te) { return te.string_id == INVALID_STRING_ID; });
46 if (it == std::end(_text_effects)) {
47 /* _text_effects.size() is the maximum ID + 1 that has been allocated. We should not allocate INVALID_TE_ID or beyond. */
48 if (_text_effects.size() >= INVALID_TE_ID) return INVALID_TE_ID;
49 it = _text_effects.emplace(std::end(_text_effects));
50 }
51
52 TextEffect &te = *it;
53
54 /* Start defining this object */
55 te.string_id = msg;
56 te.duration = duration;
57 CopyOutDParam(te.params, 2);
58 te.mode = mode;
59
60 /* Make sure we only dirty the new area */
61 te.width_normal = 0;
62 te.UpdatePosition(center, y, msg);
63
64 return static_cast<TextEffectID>(it - std::begin(_text_effects));
65 }
66
67 void UpdateTextEffect(TextEffectID te_id, StringID msg)
68 {
69 /* Update details */
70 TextEffect &te = _text_effects[te_id];
71 if (msg == te.string_id && !HaveDParamChanged(te.params)) return;
72 te.string_id = msg;
73 CopyOutDParam(te.params, 2);
74
75 te.UpdatePosition(te.center, te.top, te.string_id, te.string_id - 1);
76 }
77
78 void UpdateAllTextEffectVirtCoords()
79 {
80 for (auto &te : _text_effects) {
81 if (te.string_id == INVALID_STRING_ID) continue;
82 CopyInDParam(te.params);
83 te.UpdatePosition(te.center, te.top, te.string_id, te.string_id - 1);
84 }
85 }
86
87 void RemoveTextEffect(TextEffectID te_id)
88 {
89 _text_effects[te_id].Reset();
90 }
91
93 IntervalTimer<TimerWindow> move_all_text_effects_interval = {std::chrono::milliseconds(30), [](uint count) {
94 if (_pause_mode && _game_mode != GM_EDITOR && _settings_game.construction.command_pause_level <= CMDPL_NO_CONSTRUCTION) return;
95
96 for (TextEffect &te : _text_effects) {
97 if (te.string_id == INVALID_STRING_ID) continue;
98 if (te.mode != TE_RISING) continue;
99
100 if (te.duration < count) {
101 te.Reset();
102 continue;
103 }
104
105 te.MarkDirty(ZOOM_LVL_OUT_8X);
106 te.duration -= count;
107 te.top -= count * ZOOM_LVL_BASE;
108 te.MarkDirty(ZOOM_LVL_OUT_8X);
109 }
110 }};
111
112 void InitTextEffects()
113 {
114 _text_effects.clear();
115 _text_effects.shrink_to_fit();
116 }
117
118 void DrawTextEffects(DrawPixelInfo *dpi)
119 {
120 /* Don't draw the text effects when zoomed out a lot */
121 if (dpi->zoom > ZOOM_LVL_OUT_8X) return;
122 if (IsTransparencySet(TO_TEXT)) return;
123 for (TextEffect &te : _text_effects) {
124 if (te.string_id == INVALID_STRING_ID) continue;
125 if (te.mode == TE_RISING || _settings_client.gui.loading_indicators) {
126 CopyInDParam(te.params);
127 ViewportAddString(dpi, ZOOM_LVL_OUT_8X, &te, te.string_id, te.string_id - 1, STR_NULL);
128 }
129 }
130 }
CopyInDParam
void CopyInDParam(const std::span< const StringParameterBackup > backup)
Copy the parameters from the backup into the global string parameter array.
Definition: strings.cpp:159
TO_TEXT
@ TO_TEXT
loading and cost/income text
Definition: transparency.h:31
ConstructionSettings::command_pause_level
uint8_t command_pause_level
level/amount of commands that can't be executed while paused
Definition: settings_type.h:385
GUISettings::loading_indicators
uint8_t loading_indicators
show loading indicators
Definition: settings_type.h:151
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
IntervalTimer< TimerWindow >
IsTransparencySet
bool IsTransparencySet(TransparencyOption to)
Check if the transparency option bit is set and if we aren't in the game menu (there's never transpar...
Definition: transparency.h:48
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:54
TextEffect::string_id
StringID string_id
String to draw for the text effect, if INVALID_STRING_ID then it's not valid.
Definition: texteff.cpp:25
ViewportSign
Location information about a sign as seen on the viewport.
Definition: viewport_type.h:38
ViewportSign::top
int32_t top
The top of the sign.
Definition: viewport_type.h:40
TextEffect
Container for all information about a text effect.
Definition: texteff.cpp:23
ViewportSign::center
int32_t center
The center position of the sign.
Definition: viewport_type.h:39
TextEffect::mode
TextEffectMode mode
Type of text effect.
Definition: texteff.cpp:27
_pause_mode
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:50
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:55
ViewportSign::MarkDirty
void MarkDirty(ZoomLevel maxzoom=ZOOM_LVL_MAX) const
Mark the sign dirty in all viewports.
Definition: viewport.cpp:1489
ZOOM_LVL_OUT_8X
@ ZOOM_LVL_OUT_8X
Zoomed 8 times out.
Definition: zoom_type.h:25
safeguards.h
timer.h
CopyOutDParam
void CopyOutDParam(std::vector< StringParameterBackup > &backup, size_t num)
Copy num string parameters from the global string parameter array to the backup.
Definition: strings.cpp:176
settings_type.h
stdafx.h
ViewportSign::width_normal
uint16_t width_normal
The width when not zoomed out (normal font)
Definition: viewport_type.h:41
TextEffectMode
TextEffectMode
Text effect modes.
Definition: texteff.hpp:20
_text_effects
static std::vector< struct TextEffect > _text_effects
Text effects are stored there.
Definition: texteff.cpp:38
viewport_func.h
command_type.h
TextEffect::Reset
void Reset()
Reset the text effect.
Definition: texteff.cpp:30
texteff.hpp
strings_func.h
ViewportSign::UpdatePosition
void UpdatePosition(int center, int top, StringID str, StringID str_small=STR_NULL)
Update the position of the viewport sign.
Definition: viewport.cpp:1464
HaveDParamChanged
bool HaveDParamChanged(const std::vector< StringParameterBackup > &backup)
Checks whether the global string parameters have changed compared to the given backup.
Definition: strings.cpp:194
transparency.h
TextEffect::duration
uint8_t duration
How long the text effect should stay, in ticks (applies only when mode == TE_RISING)
Definition: texteff.cpp:26
TE_RISING
@ TE_RISING
Make the text effect slowly go upwards.
Definition: texteff.hpp:21
ViewportAddString
void ViewportAddString(const DrawPixelInfo *dpi, ZoomLevel small_from, const ViewportSign *sign, StringID string_normal, StringID string_small, StringID string_small_shadow, Colours colour)
Add a string to draw in the viewport.
Definition: viewport.cpp:1308
TextEffect::params
std::vector< StringParameterBackup > params
Backup of string parameters.
Definition: texteff.cpp:24
timer_window.h
GameSettings::construction
ConstructionSettings construction
construction of things in-game
Definition: settings_type.h:620
CMDPL_NO_CONSTRUCTION
@ CMDPL_NO_CONSTRUCTION
No construction actions may be executed.
Definition: command_type.h:425
move_all_text_effects_interval
IntervalTimer< TimerWindow > move_all_text_effects_interval
Slowly move text effects upwards.
Definition: texteff.cpp:93
INVALID_STRING_ID
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:17
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:636
DrawPixelInfo
Data about how and where to blit pixels.
Definition: gfx_type.h:151