OpenTTD
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 
99  te.MarkDirty(ZOOM_LVL_OUT_8X);
100  te.duration -= count;
101  te.top -= count * ZOOM_LVL_BASE;
102  te.MarkDirty(ZOOM_LVL_OUT_8X);
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 }
Functions related to OTTD&#39;s strings.
uint64 params_2
second DParam parameter
Definition: texteff.cpp:24
Data about how and where to blit pixels.
Definition: gfx_type.h:154
uint CountElapsed(uint delta)
Count how many times the interval has elapsed.
Definition: guitimer_func.h:40
Simple vector class that allows allocating an item without the need to copy this->data needlessly...
StringID string_id
String to draw for the text effect, if INVALID_STRING_ID then it&#39;s not valid.
Definition: texteff.cpp:25
loading indicators
Definition: transparency.h:31
void UpdatePosition(int center, int top, StringID str, StringID str_small=STR_NULL)
Update the position of the viewport sign.
Definition: viewport.cpp:1442
void MarkDirty(ZoomLevel maxzoom=ZOOM_LVL_MAX) const
Mark the sign dirty in all viewports.
Definition: viewport.cpp:1469
Functions related to (drawing on) viewports.
GUI Timers.
int32 top
The top of the sign.
Definition: viewport_type.h:48
Zoomed 8 times out.
Definition: zoom_type.h:25
uint64 params_1
DParam parameter.
Definition: texteff.cpp:23
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:78
Types related to global configuration settings.
Definition of base types and functions in a cross-platform compatible way.
Location information about a sign as seen on the viewport.
Definition: viewport_type.h:46
TextEffectMode mode
Type of text effect.
Definition: texteff.cpp:27
A number of safeguards to prevent using unsafe methods.
static const uint MILLISECONDS_PER_TICK
The number of milliseconds per game tick.
Definition: gfx_type.h:305
uint16 width_normal
The width when not zoomed out (normal font)
Definition: viewport_type.h:49
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
int32 center
The center position of the sign.
Definition: viewport_type.h:47
GUISettings gui
settings related to the GUI
static uint64 GetDParam(uint n)
Get the current string parameter at index n from the global string parameter array.
Definition: strings_func.h:229
Functions related to transparency.
void Reset()
Reset the text effect.
Definition: texteff.cpp:30
uint8 loading_indicators
show loading indicators
Make the text effect slowly go upwards.
Definition: texteff.hpp:21
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:17
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:1292
Functions related to text effects.
uint8 duration
How long the text effect should stay, in ticks (applies only when mode == TE_RISING) ...
Definition: texteff.cpp:26
TextEffectMode
Text effect modes.
Definition: texteff.hpp:20
Container for all information about a text effect.
Definition: texteff.cpp:22
static bool IsTransparencySet(TransparencyOption to)
Check if the transparency option bit is set and if we aren&#39;t in the game menu (there&#39;s never transpar...
Definition: transparency.h:48
static std::vector< struct TextEffect > _text_effects
Text effects are stored there.
Definition: texteff.cpp:38