summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsmatz <smatz@openttd.org>2009-12-25 23:15:08 +0000
committersmatz <smatz@openttd.org>2009-12-25 23:15:08 +0000
commite6d6704f32ee5cd7179a75d4808a72e9c2ea9622 (patch)
tree19fe7ea50b2af67a7c97dc8c10fdf6fb03625bd5
parent69f1db204ecf188ff5682c29750d61d2ca2bd3c3 (diff)
downloadopenttd-e6d6704f32ee5cd7179a75d4808a72e9c2ea9622.tar.xz
(svn r18635) -Codechange: store TextEffects in a SmallVector
-rw-r--r--src/texteff.cpp48
1 files changed, 14 insertions, 34 deletions
diff --git a/src/texteff.cpp b/src/texteff.cpp
index 65bea25c0..caee473cd 100644
--- a/src/texteff.cpp
+++ b/src/texteff.cpp
@@ -17,13 +17,10 @@
#include "transparency.h"
#include "strings_func.h"
#include "core/alloc_func.hpp"
+#include "core/smallvec_type.hpp"
#include "viewport_func.h"
#include "settings_type.h"
-enum {
- INIT_NUM_TEXT_EFFECTS = 20,
-};
-
/** Container for all information about a text effect */
struct TextEffect : public ViewportSign{
StringID string_id; ///< String to draw for the text effect, if INVALID_STRING_ID then it's not valid
@@ -40,31 +37,20 @@ struct TextEffect : public ViewportSign{
}
};
-/* used for text effects */
-static TextEffect *_text_effect_list = NULL;
-static uint16 _num_text_effects = INIT_NUM_TEXT_EFFECTS;
+static SmallVector<struct TextEffect, 32> _text_effects; ///< Text effects are stored there
/* Text Effects */
TextEffectID AddTextEffect(StringID msg, int center, int y, uint16 duration, TextEffectMode mode)
{
- TextEffectID i;
-
if (_game_mode == GM_MENU) return INVALID_TE_ID;
- /* Look for a free spot in the text effect array */
- for (i = 0; i < _num_text_effects; i++) {
- if (_text_effect_list[i].string_id == INVALID_STRING_ID) break;
- }
-
- /* If there is none found, we grow the array */
- if (i == _num_text_effects) {
- _num_text_effects += 25;
- _text_effect_list = ReallocT<TextEffect>(_text_effect_list, _num_text_effects);
- for (; i < _num_text_effects; i++) _text_effect_list[i].string_id = INVALID_STRING_ID;
- i = _num_text_effects - 1;
+ TextEffectID i;
+ for (i = 0; i < _text_effects.Length(); i++) {
+ if (_text_effects[i].string_id == INVALID_STRING_ID) break;
}
+ if (i == _text_effects.Length()) _text_effects.Append();
- TextEffect *te = &_text_effect_list[i];
+ TextEffect *te = _text_effects.Get(i);
/* Start defining this object */
te->string_id = msg;
@@ -81,10 +67,8 @@ TextEffectID AddTextEffect(StringID msg, int center, int y, uint16 duration, Tex
void UpdateTextEffect(TextEffectID te_id, StringID msg)
{
- assert(te_id < _num_text_effects);
-
/* Update details */
- TextEffect *te = &_text_effect_list[te_id];
+ TextEffect *te = _text_effects.Get(te_id);
te->string_id = msg;
te->params_1 = GetDParam(0);
@@ -93,8 +77,7 @@ void UpdateTextEffect(TextEffectID te_id, StringID msg)
void RemoveTextEffect(TextEffectID te_id)
{
- assert(te_id < _num_text_effects);
- _text_effect_list[te_id].Reset();
+ _text_effects[te_id].Reset();
}
static void MoveTextEffect(TextEffect *te)
@@ -113,17 +96,15 @@ static void MoveTextEffect(TextEffect *te)
void MoveAllTextEffects()
{
- for (TextEffectID i = 0; i < _num_text_effects; i++) {
- TextEffect *te = &_text_effect_list[i];
+ const TextEffect *end = _text_effects.End();
+ for (TextEffect *te = _text_effects.Begin(); te != end; te++) {
if (te->string_id != INVALID_STRING_ID && te->mode == TE_RISING) MoveTextEffect(te);
}
}
void InitTextEffects()
{
- if (_text_effect_list == NULL) _text_effect_list = MallocT<TextEffect>(_num_text_effects);
-
- for (TextEffectID i = 0; i < _num_text_effects; i++) _text_effect_list[i].string_id = INVALID_STRING_ID;
+ _text_effects.Reset();
}
void DrawTextEffects(DrawPixelInfo *dpi)
@@ -131,10 +112,9 @@ void DrawTextEffects(DrawPixelInfo *dpi)
/* Don't draw the text effects when zoomed out a lot */
if (dpi->zoom > ZOOM_LVL_OUT_2X) return;
- for (TextEffectID i = 0; i < _num_text_effects; i++) {
- const TextEffect *te = &_text_effect_list[i];
+ const TextEffect *end = _text_effects.End();
+ for (TextEffect *te = _text_effects.Begin(); te != end; te++) {
if (te->string_id == INVALID_STRING_ID) continue;
-
if (te->mode == TE_RISING || (_settings_client.gui.loading_indicators && !IsTransparencySet(TO_LOADING))) {
ViewportAddString(dpi, ZOOM_LVL_OUT_2X, te, te->string_id, te->string_id - 1, 0, te->params_1);
}