FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
floatingtextrenderer.cpp
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 
24 // 3rd party library includes
25 
26 // FIFE includes
27 // These includes are split up in two parts, separated by one empty line
28 // First block: files included from the FIFE root src directory
29 // Second block: files included from the same folder
30 #include "video/renderbackend.h"
31 #include "util/math/fife_math.h"
32 #include "util/log/logger.h"
33 #include "video/fonts/abstractfont.h"
34 #include "video/image.h"
35 #include "model/structures/instance.h"
36 #include "model/structures/layer.h"
37 #include "model/structures/location.h"
38 
39 #include "view/visual.h"
40 #include "view/camera.h"
41 #include "floatingtextrenderer.h"
42 
43 
44 namespace FIFE {
45  static Logger _log(LM_VIEWVIEW);
46 
47 
48  FloatingTextRenderer::FloatingTextRenderer(RenderBackend* renderbackend, int position, AbstractFont* font):
49  RendererBase(renderbackend, position),
50  m_renderbackend(renderbackend),
51  m_font(font) {
52  setEnabled(false);
53  m_font_color = false;
54  m_color = m_font->getColor();
55  }
56 
57  FloatingTextRenderer::FloatingTextRenderer(const FloatingTextRenderer& old):
58  RendererBase(old),
59  m_renderbackend(old.m_renderbackend),
60  m_font(old.m_font),
61  m_font_color(old.m_font_color),
62  m_color(old.m_color) {
63  setEnabled(false);
64  m_font_color = m_background = m_backborder = false;
65  }
66 
67  RendererBase* FloatingTextRenderer::clone() {
68  return new FloatingTextRenderer(*this);
69  }
70 
71  FloatingTextRenderer::~FloatingTextRenderer() {
72  }
73 
74  void FloatingTextRenderer::render(Camera* cam, Layer* layer, RenderList& instances) {
75  if (!m_font) {
76  return;
77  }
78 
79  RenderList::const_iterator instance_it = instances.begin();
80  const std::string* saytext = NULL;
81  unsigned int lm = m_renderbackend->getLightingModel();
82  SDL_Color old_color = m_font->getColor();
83  if(m_font_color) {
84  m_font->setColor(m_color.r, m_color.g, m_color.b, m_color.unused);
85  }
86  if(lm != 0) {
87  m_renderbackend->disableLighting();
88  m_renderbackend->setStencilTest(255, 2, 7);
89  m_renderbackend->setAlphaTest(0.0);
90  }
91  for (;instance_it != instances.end(); ++instance_it) {
92  Instance* instance = (*instance_it)->instance;
93  saytext = instance->getSayText();
94  if (saytext) {
95  const Rect& ir = (*instance_it)->dimensions;
96  Image* img = m_font->getAsImageMultiline(*saytext);
97  Rect r;
98  r.x = (ir.x + ir.w/2) - img->getWidth()/2;
99  r.y = ir.y- img->getHeight();
100  r.w = img->getWidth();
101  r.h = img->getHeight();
102 
103  if(m_background || m_backborder) {
104  const int overdraw = 5;
105 
106  Point p = Point(r.x-overdraw, r.y-overdraw);
107 
108  if(m_background) {
109  m_renderbackend->fillRectangle(p, r.w+2*overdraw, r.h+2*overdraw, m_backcolor.r, m_backcolor.g, m_backcolor.b, m_backcolor.unused);
110  }
111 
112  if(m_backborder) {
113  m_renderbackend->drawRectangle(p, r.w+2*overdraw, r.h+2*overdraw, m_backbordercolor.r, m_backbordercolor.g, m_backbordercolor.b, m_backbordercolor.unused);
114  }
115  }
116  img->render(r);
117  }
118  }
119  if(lm != 0) {
120  m_renderbackend->disableAlphaTest();
121  m_renderbackend->disableStencilTest();
122  m_renderbackend->enableLighting();
123  }
124  if(m_font_color) {
125  m_font->setColor(old_color.r, old_color.g, old_color.b, old_color.unused);
126  }
127  }
128 
129  void FloatingTextRenderer::setColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
130  m_color.r = r;
131  m_color.g = g;
132  m_color.b = b;
133  m_color.unused = a;
134 
135  m_font_color = true;
136  }
137 
138  void FloatingTextRenderer::setBackground(uint8_t br, uint8_t bg, uint8_t bb, uint8_t ba) {
139  m_backcolor.r = br;
140  m_backcolor.g = bg;
141  m_backcolor.b = bb;
142  m_backcolor.unused = ba;
143 
144  m_background = true;
145  }
146 
147  void FloatingTextRenderer::setBorder(uint8_t bbr, uint8_t bbg, uint8_t bbb, uint8_t bba) {
148  m_backbordercolor.r = bbr;
149  m_backbordercolor.g = bbg;
150  m_backbordercolor.b = bbb;
151  m_backbordercolor.unused = bba;
152 
153  m_backborder = true;
154  }
155 
156  void FloatingTextRenderer::resetBackground() {
157  m_background = false;
158  }
159 
160  void FloatingTextRenderer::resetBorder() {
161  m_backborder = false;
162  }
163 
164  FloatingTextRenderer* FloatingTextRenderer::getInstance(IRendererContainer* cnt) {
165  return dynamic_cast<FloatingTextRenderer*>(cnt->getRenderer("FloatingTextRenderer"));
166  }
167 }