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;
26 StringID string_id;
27 uint8 duration;
28 TextEffectMode mode;
29
31 void Reset()
32 {
33 this->MarkDirty();
34 this->width_normal = 0;
35 this->string_id = INVALID_STRING_ID;
36 }
37 };
38
39 static SmallVector<struct TextEffect, 32> _text_effects;
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
97 te->MarkDirty(ZOOM_LVL_OUT_8X);
98 te->top -= ZOOM_LVL_BASE;
99 te->MarkDirty(ZOOM_LVL_OUT_8X);
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;
116 if (te->mode == TE_RISING || (_settings_client.gui.loading_indicators && !IsTransparencySet(TO_LOADING))) {
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 }
strings_func.h
Functions related to OTTD&#39;s strings.
TextEffect::params_2
uint64 params_2
second DParam parameter
Definition: texteff.cpp:25
DrawPixelInfo
Data about how and where to blit pixels.
Definition: gfx_type.h:156
smallvec_type.hpp
Simple vector class that allows allocating an item without the need to copy this->data needlessly...
TextEffect::string_id
StringID string_id
String to draw for the text effect, if INVALID_STRING_ID then it&#39;s not valid.
Definition: texteff.cpp:26
SmallVector::Reset
void Reset()
Remove all items from the list and free allocated memory.
Definition: smallvec_type.hpp:109
SmallVector::Begin
const T * Begin() const
Get the pointer to the first item (const)
Definition: smallvec_type.hpp:272
SmallVector
Simple vector template class.
Definition: smallvec_type.hpp:29
TO_LOADING
loading indicators
Definition: transparency.h:33
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:1314
SmallVector::End
const T * End() const
Get the pointer behind the last valid item (const)
Definition: smallvec_type.hpp:292
ViewportSign::MarkDirty
void MarkDirty(ZoomLevel maxzoom=ZOOM_LVL_MAX) const
Mark the sign dirty in all viewports.
Definition: viewport.cpp:1341
SmallVector::Append
T * Append(uint to_add=1)
Append an item and return it.
Definition: smallvec_type.hpp:134
viewport_func.h
Functions related to (drawing on) viewports.
SmallVector::Length
uint Length() const
Get the number of items in the list.
Definition: smallvec_type.hpp:262
ViewportSign::top
int32 top
The top of the sign.
Definition: viewport_type.h:50
TextEffect::params_1
uint64 params_1
DParam parameter.
Definition: texteff.cpp:24
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:76
settings_type.h
Types related to global configuration settings.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
ViewportSign
Location information about a sign as seen on the viewport.
Definition: viewport_type.h:48
TextEffect::mode
TextEffectMode mode
Type of text effect.
Definition: texteff.cpp:28
safeguards.h
A number of safeguards to prevent using unsafe methods.
ViewportSign::width_normal
uint16 width_normal
The width when not zoomed out (normal font)
Definition: viewport_type.h:51
ZOOM_LVL_OUT_8X
Zoomed 8 times out.
Definition: zoom_type.h:27
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
ViewportSign::center
int32 center
The center position of the sign.
Definition: viewport_type.h:49
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:561
GetDParam
static uint64 GetDParam(uint n)
Get the current string parameter at index n from the global string parameter array.
Definition: strings_func.h:231
transparency.h
Functions related to transparency.
_text_effects
static SmallVector< struct TextEffect, 32 > _text_effects
Text effects are stored there.
Definition: texteff.cpp:39
TextEffect::Reset
void Reset()
Reset the text effect.
Definition: texteff.cpp:31
GUISettings::loading_indicators
uint8 loading_indicators
show loading indicators
Definition: settings_type.h:95
TE_RISING
Make the text effect slowly go upwards.
Definition: texteff.hpp:23
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:19
SmallVector::Get
const T * Get(uint index) const
Get the pointer to item "number" (const)
Definition: smallvec_type.hpp:313
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:1219
texteff.hpp
Functions related to text effects.
TextEffect::duration
uint8 duration
How long the text effect should stay, in ticks (applies only when mode == TE_RISING) ...
Definition: texteff.cpp:27
TextEffectMode
TextEffectMode
Text effect modes.
Definition: texteff.hpp:22
TextEffect
Container for all information about a text effect.
Definition: texteff.cpp:23
IsTransparencySet
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