changeset 14088:cc9b4f428e10 draft

(svn r18635) -Codechange: store TextEffects in a SmallVector
author smatz <smatz@openttd.org>
date Fri, 25 Dec 2009 23:15:08 +0000
parents 5290a3a69842
children 511acd6d8554
files src/texteff.cpp
diffstat 1 files changed, 14 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- 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 @@
 	}
 };
 
-/* 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;
+	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();
 
-	/* 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;
-	}
-
-	TextEffect *te = &_text_effect_list[i];
+	TextEffect *te = _text_effects.Get(i);
 
 	/* Start defining this object */
 	te->string_id = msg;
@@ -81,10 +67,8 @@
 
 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 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 @@
 
 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 @@
 	/* 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);
 		}