OpenTTD Source  1.11.0-beta2
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;
26  uint8 duration;
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, msg);
77 }
78 
79 void RemoveTextEffect(TextEffectID te_id)
80 {
81  _text_effects[te_id].Reset();
82 }
83 
84 void MoveAllTextEffects(uint delta_ms)
85 {
86  static GUITimer texteffecttimer = GUITimer(MILLISECONDS_PER_TICK);
87  uint count = texteffecttimer.CountElapsed(delta_ms);
88  if (count == 0) return;
89 
90  for (TextEffect &te : _text_effects) {
91  if (te.string_id == INVALID_STRING_ID) continue;
92  if (te.mode != TE_RISING) continue;
93 
94  if (te.duration < count) {
95  te.Reset();
96  continue;
97  }
98 
100  te.duration -= count;
101  te.top -= count * ZOOM_LVL_BASE;
103  }
104 }
105 
106 void InitTextEffects()
107 {
108  _text_effects.clear();
109  _text_effects.shrink_to_fit();
110 }
111 
112 void DrawTextEffects(DrawPixelInfo *dpi)
113 {
114  /* Don't draw the text effects when zoomed out a lot */
115  if (dpi->zoom > ZOOM_LVL_OUT_8X) return;
116 
117  for (TextEffect &te : _text_effects) {
118  if (te.string_id == INVALID_STRING_ID) continue;
120  ViewportAddString(dpi, ZOOM_LVL_OUT_8X, &te, te.string_id, te.string_id - 1, STR_NULL, te.params_1, te.params_2);
121  }
122  }
123 }
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:79
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
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:103
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:1466
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:229
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:1289
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:1439
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:567
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