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_font(font) {
00051         setEnabled(true);
00052     }
00053 
00054     FloatingTextRenderer::FloatingTextRenderer(const FloatingTextRenderer& old):
00055         RendererBase(old),
00056         m_font(old.m_font) {
00057         setEnabled(true);
00058     }
00059     
00060     RendererBase* FloatingTextRenderer::clone() {
00061         return new FloatingTextRenderer(*this);
00062     }
00063 
00064     FloatingTextRenderer::~FloatingTextRenderer() {
00065     }
00066 
00067     void FloatingTextRenderer::render(Camera* cam, Layer* layer, std::vector<Instance*>& instances) {
00068         if (!m_font) {
00069             return;
00070         }
00071         
00072         std::vector<Instance*>::const_iterator instance_it = instances.begin();
00073         const std::string* saytext = NULL;
00074 
00075         for (;instance_it != instances.end(); ++instance_it) {
00076             Instance* instance = *instance_it;
00077             saytext = instance->getSayText();
00078             if (saytext) {
00079                 InstanceVisual* visual = instance->getVisual<InstanceVisual>();
00080                 const Rect& ir = visual->getCacheItem(cam).dimensions;
00081                 m_font->setColor(25,25,112);
00082                 Image* img = m_font->getAsImageMultiline(*saytext);
00083                 Rect r;
00084                 r.x = (ir.x + ir.w/2) - img->getWidth()/2; 
00085                 r.y = ir.y- img->getHeight(); 
00086                 r.w = img->getWidth();
00087                 r.h = img->getHeight();
00088                 img->render(r);
00089             }
00090         }
00091     }
00092     
00093     FloatingTextRenderer* FloatingTextRenderer::getInstance(IRendererContainer* cnt) {
00094         return dynamic_cast<FloatingTextRenderer*>(cnt->getRenderer("FloatingTextRenderer"));
00095     }
00096 }
Generated by  doxygen 1.6.2-20100208