OpenTTD
texteff.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "stdafx.h"
13 #include "texteff.hpp"
14 #include "transparency.h"
15 #include "strings_func.h"
16 #include "core/smallvec_type.hpp"
17 #include "viewport_func.h"
18 #include "settings_type.h"
19 #include "guitimer_func.h"
20 
21 #include "safeguards.h"
22 
24 struct TextEffect : public ViewportSign {
25  uint64 params_1;
26  uint64 params_2;
28  uint8 duration;
30 
32  void Reset()
33  {
34  this->MarkDirty();
35  this->width_normal = 0;
36  this->string_id = INVALID_STRING_ID;
37  }
38 };
39 
40 static std::vector<struct TextEffect> _text_effects;
41 
42 /* Text Effects */
43 TextEffectID AddTextEffect(StringID msg, int center, int y, uint8 duration, TextEffectMode mode)
44 {
45  if (_game_mode == GM_MENU) return INVALID_TE_ID;
46 
47  TextEffectID i;
48  for (i = 0; i < _text_effects.size(); i++) {
49  if (_text_effects[i].string_id == INVALID_STRING_ID) break;
50  }
51  if (i == _text_effects.size()) _text_effects.emplace_back();
52 
53  TextEffect &te = _text_effects[i];
54 
55  /* Start defining this object */
56  te.string_id = msg;
57  te.duration = duration;
58  te.params_1 = GetDParam(0);
59  te.params_2 = GetDParam(1);
60  te.mode = mode;
61 
62  /* Make sure we only dirty the new area */
63  te.width_normal = 0;
64  te.UpdatePosition(center, y, msg);
65 
66  return i;
67 }
68 
69 void UpdateTextEffect(TextEffectID te_id, StringID msg)
70 {
71  /* Update details */
72  TextEffect *te = _text_effects.data() + te_id;
73  if (msg == te->string_id && GetDParam(0) == te->params_1) return;
74  te->string_id = msg;
75  te->params_1 = GetDParam(0);
76  te->params_2 = GetDParam(1);
77 
78  te->UpdatePosition(te->center, te->top, msg);
79 }
80 
81 void RemoveTextEffect(TextEffectID te_id)
82 {
83  _text_effects[te_id].Reset();
84 }
85 
86 void MoveAllTextEffects(uint delta_ms)
87 {
88  static GUITimer texteffecttimer = GUITimer(MILLISECONDS_PER_TICK);
89  uint count = texteffecttimer.CountElapsed(delta_ms);
90  if (count == 0) return;
91 
92  for (TextEffect &te : _text_effects) {
93  if (te.string_id == INVALID_STRING_ID) continue;
94  if (te.mode != TE_RISING) continue;
95 
96  if (te.duration < count) {
97  te.Reset();
98  continue;
99  }
100 
101  te.MarkDirty(ZOOM_LVL_OUT_8X);
102  te.duration -= count;
103  te.top -= count * ZOOM_LVL_BASE;
104  te.MarkDirty(ZOOM_LVL_OUT_8X);
105  }
106 }
107 
108 void InitTextEffects()
109 {
110  _text_effects.clear();
111  _text_effects.shrink_to_fit();
112 }
113 
114 void DrawTextEffects(DrawPixelInfo *dpi)
115 {
116  /* Don't draw the text effects when zoomed out a lot */
117  if (dpi->zoom > ZOOM_LVL_OUT_8X) return;
118 
119  for (TextEffect &te : _text_effects) {
120  if (te.string_id == INVALID_STRING_ID) continue;
122  ViewportAddString(dpi, ZOOM_LVL_OUT_8X, &te, te.string_id, te.string_id - 1, STR_NULL, te.params_1, te.params_2);
123  }
124  }
125 }
Functions related to OTTD&#39;s strings.
uint64 params_2
second DParam parameter
Definition: texteff.cpp:26
Data about how and where to blit pixels.
Definition: gfx_type.h:156
uint CountElapsed(uint delta)
Count how many times the interval has elapsed.
Definition: guitimer_func.h:42
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:27
loading indicators
Definition: transparency.h:33
void UpdatePosition(int center, int top, StringID str, StringID str_small=STR_NULL)
Update the position of the viewport sign.
Definition: viewport.cpp:1444
void MarkDirty(ZoomLevel maxzoom=ZOOM_LVL_MAX) const
Mark the sign dirty in all viewports.
Definition: viewport.cpp:1471
Functions related to (drawing on) viewports.
GUI Timers.
int32 top
The top of the sign.
Definition: viewport_type.h:50
Zoomed 8 times out.
Definition: zoom_type.h:27
uint64 params_1
DParam parameter.
Definition: texteff.cpp:25
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:80
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:48
TextEffectMode mode
Type of text effect.
Definition: texteff.cpp:29
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:306
uint16 width_normal
The width when not zoomed out (normal font)
Definition: viewport_type.h:51
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
int32 center
The center position of the sign.
Definition: viewport_type.h:49
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:231
Functions related to transparency.
void Reset()
Reset the text effect.
Definition: texteff.cpp:32
uint8 loading_indicators
show loading indicators
Make the text effect slowly go upwards.
Definition: texteff.hpp:23
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:19
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
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:28
TextEffectMode
Text effect modes.
Definition: texteff.hpp:22
Container for all information about a text effect.
Definition: texteff.cpp:24
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:50
static std::vector< struct TextEffect > _text_effects
Text effects are stored there.
Definition: texteff.cpp:40