OpenTTD Source: src/texteff.cpp Source File

OpenTTD Source 12.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 "core/smallvec_type.hpp"
15 #include "viewport_func.h"
16 #include "settings_type.h"
17 #include "guitimer_func.h"
18
19 #include "safeguards.h"
20
22 struct TextEffect : public ViewportSign {
23 uint64 params_1;
24 uint64 params_2;
25 StringID string_id;
26 uint8 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 duration, TextEffectMode mode)
42 {
43 if (_game_mode == GM_MENU) return INVALID_TE_ID;
44
45 TextEffectID i;
46 for (i = 0; i < _text_effects.size(); i++) {
47 if (_text_effects[i].string_id == INVALID_STRING_ID) break;
48 }
49 if (i == _text_effects.size()) _text_effects.emplace_back();
50
51 TextEffect &te = _text_effects[i];
52
53 /* Start defining this object */
54 te.string_id = msg;
55 te.duration = duration;
56 te.params_1 = GetDParam(0);
57 te.params_2 = GetDParam(1);
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 i;
65 }
66
67 void UpdateTextEffect(TextEffectID te_id, StringID msg)
68 {
69 /* Update details */
70 TextEffect *te = _text_effects.data() + te_id;
71 if (msg == te->string_id && GetDParam(0) == te->params_1) return;
72 te->string_id = msg;
73 te->params_1 = GetDParam(0);
74 te->params_2 = GetDParam(1);
75
76 te->UpdatePosition(te->center, te->top, te->string_id, te->string_id - 1);
77 }
78
79 void UpdateAllTextEffectVirtCoords()
80 {
81 for (auto &te : _text_effects) {
82 if (te.string_id == INVALID_STRING_ID) continue;
83 SetDParam(0, te.params_1);
84 SetDParam(1, te.params_2);
85 te.UpdatePosition(te.center, te.top, te.string_id, te.string_id - 1);
86 }
87 }
88
89 void RemoveTextEffect(TextEffectID te_id)
90 {
91 _text_effects[te_id].Reset();
92 }
93
94 void MoveAllTextEffects(uint delta_ms)
95 {
96 static GUITimer texteffecttimer = GUITimer(MILLISECONDS_PER_TICK);
97 uint count = texteffecttimer.CountElapsed(delta_ms);
98 if (count == 0) return;
99
100 for (TextEffect &te : _text_effects) {
101 if (te.string_id == INVALID_STRING_ID) continue;
102 if (te.mode != TE_RISING) continue;
103
104 if (te.duration < count) {
105 te.Reset();
106 continue;
107 }
108
109 te.MarkDirty(ZOOM_LVL_OUT_8X);
110 te.duration -= count;
111 te.top -= count * ZOOM_LVL_BASE;
112 te.MarkDirty(ZOOM_LVL_OUT_8X);
113 }
114 }
115
116 void InitTextEffects()
117 {
118 _text_effects.clear();
119 _text_effects.shrink_to_fit();
120 }
121
122 void DrawTextEffects(DrawPixelInfo *dpi)
123 {
124 /* Don't draw the text effects when zoomed out a lot */
125 if (dpi->zoom > ZOOM_LVL_OUT_8X) return;
126
127 for (TextEffect &te : _text_effects) {
128 if (te.string_id == INVALID_STRING_ID) continue;
129 if (te.mode == TE_RISING || (_settings_client.gui.loading_indicators && !IsTransparencySet(TO_LOADING))) {
130 ViewportAddString(dpi, ZOOM_LVL_OUT_8X, &te, te.string_id, te.string_id - 1, STR_NULL, te.params_1, te.params_2);
131 }
132 }
133 }
TE_RISING
@ TE_RISING
Make the text effect slowly go upwards.
Definition: texteff.hpp:21
guitimer_func.h
IsTransparencySet
static 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
TO_LOADING
@ TO_LOADING
loading indicators
Definition: transparency.h:31
smallvec_type.hpp
ViewportSign::center
int32 center
The center position of the sign.
Definition: viewport_type.h:47
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:52
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::top
int32 top
The top of the sign.
Definition: viewport_type.h:48
ViewportSign
Location information about a sign as seen on the viewport.
Definition: viewport_type.h:46
SetDParam
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:196
TextEffect
Container for all information about a text effect.
Definition: texteff.cpp:22
TextEffectMode
TextEffectMode
Text effect modes.
Definition: texteff.hpp:20
GUITimer
Definition: guitimer_func.h:13
TextEffect::params_1
uint64 params_1
DParam parameter.
Definition: texteff.cpp:23
GUISettings::loading_indicators
uint8 loading_indicators
show loading indicators
Definition: settings_type.h:123
TextEffect::mode
TextEffectMode mode
Type of text effect.
Definition: texteff.cpp:27
ViewportSign::MarkDirty
void MarkDirty(ZoomLevel maxzoom=ZOOM_LVL_MAX) const
Mark the sign dirty in all viewports.
Definition: viewport.cpp:1471
ZOOM_LVL_OUT_8X
@ ZOOM_LVL_OUT_8X
Zoomed 8 times out.
Definition: zoom_type.h:27
safeguards.h
ViewportSign::width_normal
uint16 width_normal
The width when not zoomed out (normal font)
Definition: viewport_type.h:49
GetDParam
static uint64 GetDParam(uint n)
Get the current string parameter at index n from the global string parameter array.
Definition: strings_func.h:227
settings_type.h
stdafx.h
_text_effects
static std::vector< struct TextEffect > _text_effects
Text effects are stored there.
Definition: texteff.cpp:38
viewport_func.h
TextEffect::Reset
void Reset()
Reset the text effect.
Definition: texteff.cpp:30
ViewportAddString
void ViewportAddString(const DrawPixelInfo *dpi, ZoomLevel small_from, const ViewportSign *sign, StringID string_normal, StringID string_small, StringID string_small_shadow, uint64 params_1, uint64 params_2, Colours colour)
Add a string to draw in the viewport.
Definition: viewport.cpp:1294
texteff.hpp
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
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:1444
transparency.h
GUITimer::CountElapsed
uint CountElapsed(uint delta)
Count how many times the interval has elapsed.
Definition: guitimer_func.h:40
MILLISECONDS_PER_TICK
static const uint MILLISECONDS_PER_TICK
The number of milliseconds per game tick.
Definition: gfx_type.h:310
TextEffect::params_2
uint64 params_2
second DParam parameter
Definition: texteff.cpp:24
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:593
DrawPixelInfo
Data about how and where to blit pixels.
Definition: gfx_type.h:155
TextEffect::duration
uint8 duration
How long the text effect should stay, in ticks (applies only when mode == TE_RISING)
Definition: texteff.cpp:26