OpenTTD
texteff.cpp
Go to the documentation of this file.
1 /* $Id: texteff.cpp 27248 2015-04-25 11:58:19Z frosch $ */
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 
20 #include "safeguards.h"
21 
23 struct TextEffect : public ViewportSign {
24  uint64 params_1;
25  uint64 params_2;
27  uint8 duration;
29 
31  void Reset()
32  {
33  this->MarkDirty();
34  this->width_normal = 0;
36  }
37 };
38 
40 
41 /* Text Effects */
42 TextEffectID AddTextEffect(StringID msg, int center, int y, uint8 duration, TextEffectMode mode)
43 {
44  if (_game_mode == GM_MENU) return INVALID_TE_ID;
45 
46  TextEffectID i;
47  for (i = 0; i < _text_effects.Length(); i++) {
48  if (_text_effects[i].string_id == INVALID_STRING_ID) break;
49  }
50  if (i == _text_effects.Length()) _text_effects.Append();
51 
52  TextEffect *te = _text_effects.Get(i);
53 
54  /* Start defining this object */
55  te->string_id = msg;
56  te->duration = duration;
57  te->params_1 = GetDParam(0);
58  te->params_2 = GetDParam(1);
59  te->mode = mode;
60 
61  /* Make sure we only dirty the new area */
62  te->width_normal = 0;
63  te->UpdatePosition(center, y, msg);
64 
65  return i;
66 }
67 
68 void UpdateTextEffect(TextEffectID te_id, StringID msg)
69 {
70  /* Update details */
71  TextEffect *te = _text_effects.Get(te_id);
72  if (msg == te->string_id && GetDParam(0) == te->params_1) return;
73  te->string_id = msg;
74  te->params_1 = GetDParam(0);
75  te->params_2 = GetDParam(1);
76 
77  te->UpdatePosition(te->center, te->top, msg);
78 }
79 
80 void RemoveTextEffect(TextEffectID te_id)
81 {
82  _text_effects[te_id].Reset();
83 }
84 
85 void MoveAllTextEffects()
86 {
87  const TextEffect *end = _text_effects.End();
88  for (TextEffect *te = _text_effects.Begin(); te != end; te++) {
89  if (te->string_id == INVALID_STRING_ID) continue;
90  if (te->mode != TE_RISING) continue;
91 
92  if (te->duration-- == 0) {
93  te->Reset();
94  continue;
95  }
96 
98  te->top -= ZOOM_LVL_BASE;
100  }
101 }
102 
103 void InitTextEffects()
104 {
105  _text_effects.Reset();
106 }
107 
108 void DrawTextEffects(DrawPixelInfo *dpi)
109 {
110  /* Don't draw the text effects when zoomed out a lot */
111  if (dpi->zoom > ZOOM_LVL_OUT_8X) return;
112 
113  const TextEffect *end = _text_effects.End();
114  for (TextEffect *te = _text_effects.Begin(); te != end; te++) {
115  if (te->string_id == INVALID_STRING_ID) continue;
117  ViewportAddString(dpi, ZOOM_LVL_OUT_8X, te, te->string_id, te->string_id - 1, STR_NULL, te->params_1, te->params_2);
118  }
119  }
120 }