texteff.cpp

Go to the documentation of this file.
00001 /* $Id: texteff.cpp 18602 2009-12-22 12:50:41Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "openttd.h"
00014 #include "strings_type.h"
00015 #include "texteff.hpp"
00016 #include "core/bitmath_func.hpp"
00017 #include "transparency.h"
00018 #include "strings_func.h"
00019 #include "core/alloc_func.hpp"
00020 #include "viewport_func.h"
00021 #include "settings_type.h"
00022 
00023 enum {
00024   INIT_NUM_TEXT_EFFECTS  =  20,
00025 };
00026 
00028 struct TextEffect : public ViewportSign{
00029   StringID string_id;  
00030   uint16 duration;     
00031   uint64 params_1;     
00032   TextEffectMode mode; 
00033 
00035   void Reset()
00036   {
00037     this->MarkDirty();
00038     this->width_normal = 0;
00039     this->string_id = INVALID_STRING_ID;
00040   }
00041 };
00042 
00043 /* used for text effects */
00044 static TextEffect *_text_effect_list = NULL;
00045 static uint16 _num_text_effects = INIT_NUM_TEXT_EFFECTS;
00046 
00047 /* Text Effects */
00048 TextEffectID AddTextEffect(StringID msg, int center, int y, uint16 duration, TextEffectMode mode)
00049 {
00050   TextEffectID i;
00051 
00052   if (_game_mode == GM_MENU) return INVALID_TE_ID;
00053 
00054   /* Look for a free spot in the text effect array */
00055   for (i = 0; i < _num_text_effects; i++) {
00056     if (_text_effect_list[i].string_id == INVALID_STRING_ID) break;
00057   }
00058 
00059   /* If there is none found, we grow the array */
00060   if (i == _num_text_effects) {
00061     _num_text_effects += 25;
00062     _text_effect_list = ReallocT<TextEffect>(_text_effect_list, _num_text_effects);
00063     for (; i < _num_text_effects; i++) _text_effect_list[i].string_id = INVALID_STRING_ID;
00064     i = _num_text_effects - 1;
00065   }
00066 
00067   TextEffect *te = &_text_effect_list[i];
00068 
00069   /* Start defining this object */
00070   te->string_id = msg;
00071   te->duration = duration;
00072   te->params_1 = GetDParam(0);
00073   te->mode = mode;
00074 
00075   /* Make sure we only dirty the new area */
00076   te->width_normal = 0;
00077   te->UpdatePosition(center, y, msg);
00078 
00079   return i;
00080 }
00081 
00082 void UpdateTextEffect(TextEffectID te_id, StringID msg)
00083 {
00084   assert(te_id < _num_text_effects);
00085 
00086   /* Update details */
00087   TextEffect *te = &_text_effect_list[te_id];
00088   te->string_id = msg;
00089   te->params_1 = GetDParam(0);
00090 
00091   te->UpdatePosition(te->center, te->top, msg);
00092 }
00093 
00094 void RemoveTextEffect(TextEffectID te_id)
00095 {
00096   assert(te_id < _num_text_effects);
00097   _text_effect_list[te_id].Reset();
00098 }
00099 
00100 static void MoveTextEffect(TextEffect *te)
00101 {
00102   /* Never expire for duration of 0xFFFF */
00103   if (te->duration == 0xFFFF) return;
00104   if (te->duration < 8) {
00105     te->Reset();
00106   } else {
00107     te->duration -= 8;
00108     te->MarkDirty();
00109     te->top--;
00110     te->MarkDirty();
00111   }
00112 }
00113 
00114 void MoveAllTextEffects()
00115 {
00116   for (TextEffectID i = 0; i < _num_text_effects; i++) {
00117     TextEffect *te = &_text_effect_list[i];
00118     if (te->string_id != INVALID_STRING_ID && te->mode == TE_RISING) MoveTextEffect(te);
00119   }
00120 }
00121 
00122 void InitTextEffects()
00123 {
00124   if (_text_effect_list == NULL) _text_effect_list = MallocT<TextEffect>(_num_text_effects);
00125 
00126   for (TextEffectID i = 0; i < _num_text_effects; i++) _text_effect_list[i].string_id = INVALID_STRING_ID;
00127 }
00128 
00129 void DrawTextEffects(DrawPixelInfo *dpi)
00130 {
00131   /* Don't draw the text effects when zoomed out a lot */
00132   if (dpi->zoom > ZOOM_LVL_OUT_2X) return;
00133 
00134   for (TextEffectID i = 0; i < _num_text_effects; i++) {
00135     const TextEffect *te = &_text_effect_list[i];
00136     if (te->string_id == INVALID_STRING_ID) continue;
00137 
00138     if (te->mode == TE_RISING || (_settings_client.gui.loading_indicators && !IsTransparencySet(TO_LOADING))) {
00139       ViewportAddString(dpi, ZOOM_LVL_OUT_2X, te, te->string_id, te->string_id - 1, 0, te->params_1);
00140     }
00141   }
00142 }

Generated on Wed Dec 23 23:27:55 2009 for OpenTTD by  doxygen 1.5.6