FIFE 2008.0
floatingtextrenderer.cpp
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 
00024 // 3rd party library includes
00025 
00026 // FIFE includes
00027 // These includes are split up in two parts, separated by one empty line
00028 // First block: files included from the FIFE root src directory
00029 // Second block: files included from the same folder
00030 #include "video/renderbackend.h"
00031 #include "util/math/fife_math.h"
00032 #include "util/log/logger.h"
00033 #include "video/fonts/abstractfont.h"
00034 #include "video/image.h"
00035 #include "model/structures/instance.h"
00036 #include "model/structures/layer.h"
00037 #include "model/structures/location.h"
00038 
00039 #include "view/visual.h"
00040 #include "view/camera.h"
00041 #include "floatingtextrenderer.h"
00042 
00043 
00044 namespace FIFE {
00045     static Logger _log(LM_VIEWVIEW);
00046 
00047 
00048     FloatingTextRenderer::FloatingTextRenderer(RenderBackend* renderbackend, int position, AbstractFont* font):
00049         RendererBase(renderbackend, position),
00050         m_renderbackend(renderbackend),
00051         m_font(font) {
00052         setEnabled(false);
00053         m_font_color = false;
00054         m_color = m_font->getColor();
00055     }
00056 
00057     FloatingTextRenderer::FloatingTextRenderer(const FloatingTextRenderer& old):
00058         RendererBase(old),
00059         m_renderbackend(old.m_renderbackend),
00060         m_font(old.m_font),
00061         m_font_color(old.m_font_color),
00062         m_color(old.m_color) {
00063         setEnabled(false);
00064         m_font_color = m_background = m_backborder = false;
00065     }
00066 
00067     RendererBase* FloatingTextRenderer::clone() {
00068         return new FloatingTextRenderer(*this);
00069     }
00070 
00071     FloatingTextRenderer::~FloatingTextRenderer() {
00072     }
00073 
00074     void FloatingTextRenderer::render(Camera* cam, Layer* layer, RenderList& instances) {
00075         if (!m_font) {
00076             return;
00077         }
00078 
00079         RenderList::const_iterator instance_it = instances.begin();
00080         const std::string* saytext = NULL;
00081         unsigned int lm = m_renderbackend->getLightingModel();
00082         SDL_Color old_color = m_font->getColor();
00083         if(m_font_color) {
00084             m_font->setColor(m_color.r, m_color.g, m_color.b, m_color.unused);
00085         }
00086         if(lm != 0) {
00087             m_renderbackend->disableLighting();
00088             m_renderbackend->setStencilTest(255, 2, 7);
00089             m_renderbackend->setAlphaTest(0.0);
00090         }
00091         for (;instance_it != instances.end(); ++instance_it) {
00092             Instance* instance = (*instance_it)->instance;
00093             saytext = instance->getSayText();
00094             if (saytext) {
00095                 const Rect& ir = (*instance_it)->dimensions;
00096                 Image* img = m_font->getAsImageMultiline(*saytext);
00097                 Rect r;
00098                 r.x = (ir.x + ir.w/2) - img->getWidth()/2; 
00099                 r.y = ir.y- img->getHeight(); 
00100                 r.w = img->getWidth();
00101                 r.h = img->getHeight();
00102 
00103                 if(m_background || m_backborder) {
00104                     const int overdraw = 5;
00105 
00106                     Point p = Point(r.x-overdraw, r.y-overdraw);
00107 
00108                     if(m_background) {
00109                         m_renderbackend->fillRectangle(p, r.w+2*overdraw, r.h+2*overdraw, m_backcolor.r, m_backcolor.g, m_backcolor.b, m_backcolor.unused);
00110                     }
00111 
00112                     if(m_backborder) {
00113                         m_renderbackend->drawRectangle(p, r.w+2*overdraw, r.h+2*overdraw, m_backbordercolor.r, m_backbordercolor.g, m_backbordercolor.b, m_backbordercolor.unused);
00114                     }
00115                 }
00116                 img->render(r);
00117             }
00118         }
00119         if(lm != 0) {
00120             m_renderbackend->disableAlphaTest();
00121             m_renderbackend->disableStencilTest();
00122             m_renderbackend->enableLighting();
00123         }
00124         if(m_font_color) {
00125             m_font->setColor(old_color.r, old_color.g, old_color.b, old_color.unused);
00126         }
00127     }
00128 
00129     void FloatingTextRenderer::setColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
00130         m_color.r = r;
00131         m_color.g = g;
00132         m_color.b = b;
00133         m_color.unused = a;
00134 
00135         m_font_color = true;
00136     }
00137 
00138     void FloatingTextRenderer::setBackground(uint8_t br, uint8_t bg, uint8_t bb, uint8_t ba) {
00139         m_backcolor.r = br;
00140         m_backcolor.g = bg;
00141         m_backcolor.b = bb;
00142         m_backcolor.unused = ba;
00143 
00144         m_background = true;
00145     }
00146 
00147     void FloatingTextRenderer::setBorder(uint8_t bbr, uint8_t bbg, uint8_t bbb, uint8_t bba) {
00148         m_backbordercolor.r = bbr;
00149         m_backbordercolor.g = bbg;
00150         m_backbordercolor.b = bbb;
00151         m_backbordercolor.unused = bba;
00152 
00153         m_backborder = true;
00154     }
00155 
00156     void FloatingTextRenderer::resetBackground() {
00157         m_background = false;
00158     }
00159 
00160     void FloatingTextRenderer::resetBorder() {
00161         m_backborder = false;
00162     }
00163 
00164     FloatingTextRenderer* FloatingTextRenderer::getInstance(IRendererContainer* cnt) {
00165         return dynamic_cast<FloatingTextRenderer*>(cnt->getRenderer("FloatingTextRenderer"));
00166     }
00167 }
 All Classes Namespaces Functions Variables Enumerations Enumerator