FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
floatingtextrenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2019 by the FIFE team *
3  * http://www.fifengine.net *
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/ifont.h"
34 #include "video/image.h"
36 #include "model/structures/layer.h"
38 
39 #include "view/visual.h"
40 #include "view/camera.h"
41 #include "floatingtextrenderer.h"
42 
43 
44 namespace FIFE {
48  static Logger _log(LM_VIEWVIEW);
49 
50  FloatingTextRenderer::FloatingTextRenderer(RenderBackend* renderbackend, int32_t position):
51  RendererBase(renderbackend, position),
52  m_renderbackend(renderbackend),
53  m_font(0),
54  m_font_color(false),
55  m_background(false),
56  m_backborder(false) {
57  setEnabled(false);
58  }
59 
61  RendererBase(old),
63  m_font(old.m_font),
65  m_color(old.m_color),
68  setEnabled(false);
69  }
70 
72  return new FloatingTextRenderer(*this);
73  }
74 
76  }
77 
78  void FloatingTextRenderer::render(Camera* cam, Layer* layer, RenderList& instances) {
79  if (!m_font) {
80  //no font selected.. nothing to render
81  return;
82  }
83 
84  RenderList::const_iterator instance_it = instances.begin();
86  SDL_Color old_color = m_font->getColor();
87  if(m_font_color) {
89  }
90  for (;instance_it != instances.end(); ++instance_it) {
91  Instance* instance = (*instance_it)->instance;
92  const std::string* saytext = instance->getSayText();
93  if (saytext) {
94  const Rect& ir = (*instance_it)->dimensions;
95  Image* img = m_font->getAsImageMultiline(*saytext);
96  Rect r;
97  r.x = (ir.x + ir.w/2) - img->getWidth()/2;
98  r.y = ir.y- img->getHeight();
99  r.w = img->getWidth();
100  r.h = img->getHeight();
101  // Without this check it can happen that changeRenderInfos() call produces an out_of_range error
102  // because the image rendering can be skipped, if it's not on the screen.
103  // The result is that it tried to modify more objects as exist.
104  if (r.right() < 0 || r.x > static_cast<int32_t>(m_renderbackend->getWidth()) ||
105  r.bottom() < 0 || r.y > static_cast<int32_t>(m_renderbackend->getHeight())) {
106  continue;
107  }
108  if(m_background || m_backborder) {
109  const int32_t overdraw = 5;
110 
111  Point p = Point(r.x-overdraw, r.y-overdraw);
112 
113  if(m_background) {
114  m_renderbackend->fillRectangle(p, r.w+2*overdraw, r.h+2*overdraw, m_backcolor.r, m_backcolor.g, m_backcolor.b, m_backcolor.a);
115  }
116 
117  if(m_backborder) {
119  }
120  }
121  img->render(r);
122  if(lm > 0) {
123  uint16_t elements = 1;
124  if (m_background) {
125  ++elements;
126  }
127  if (m_backborder) {
128  ++elements;
129  }
130  m_renderbackend->changeRenderInfos(RENDER_DATA_WITHOUT_Z, elements, 4, 5, false, true, 255, REPLACE, ALWAYS);
131  }
132  }
133  }
134  if(m_font_color) {
135  m_font->setColor(old_color.r, old_color.g, old_color.b, old_color.a);
136  }
137  }
138 
140  m_color.r = r;
141  m_color.g = g;
142  m_color.b = b;
143  m_color.a = a;
144 
145  m_font_color = true;
146  }
147 
149  m_backcolor.r = br;
150  m_backcolor.g = bg;
151  m_backcolor.b = bb;
152  m_backcolor.a = ba;
153 
154  m_background = true;
155  }
156 
158  m_backbordercolor.r = bbr;
159  m_backbordercolor.g = bbg;
160  m_backbordercolor.b = bbb;
161  m_backbordercolor.a = bba;
162 
163  m_backborder = true;
164  }
165 
167  m_background = false;
168  }
169 
171  m_backborder = false;
172  }
173 
175  return dynamic_cast<FloatingTextRenderer*>(cnt->getRenderer("FloatingTextRenderer"));
176  }
177 }
void render(Camera *cam, Layer *layer, RenderList &instances)
This method is called by the view to ask renderer to draw its rendering aspect based on given paramet...
Abstract interface for all the renderbackends.
uint32_t getHeight() const
std::vector< RenderItem * > RenderList
Definition: renderitem.h:130
RendererBase * clone()
Makes copy of this renderer.
virtual void drawRectangle(const Point &p, uint16_t w, uint16_t h, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Draws an axis parallel rectangle.
Base Class for Images.
Definition: image.h:48
T h
Height of the rectangle.
Definition: rect.h:93
void resetBackground()
Disable the default background.
T x
The X Coordinate.
Definition: rect.h:84
Interface to class owning the renderers Used to get correct subclass of renderer in scripting side (v...
Definition: rendererbase.h:66
static Logger _log(LM_AUDIO)
virtual Image * getAsImageMultiline(const std::string &text)=0
Gets given text as Image.
static FloatingTextRenderer * getInstance(IRendererContainer *cnt)
Gets instance for interface access.
void setBorder(uint8_t bbr, uint8_t bbg, uint8_t bbb, uint8_t bba=255)
Set default border r,g,b,a values for border.
virtual void setColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Set the color the text should be rendered in.
virtual RendererBase * getRenderer(const std::string &renderername)=0
Returns renderer with given name.
Camera describes properties of a view port shown in the main screen Main screen can have multiple cam...
Definition: camera.h:59
virtual SDL_Color getColor() const =0
Get the color the text was rendered in.
virtual uint32_t getLightingModel() const =0
Gets the current light model.
const std::string * getSayText() const
Returns pointer to currently set saytext.
Definition: instance.cpp:610
void resetBorder()
Disable the default border.
unsigned char uint8_t
Definition: core.h:38
virtual void changeRenderInfos(RenderDataType type, uint16_t elements, int32_t src, int32_t dst, bool light, bool stentest, uint8_t stenref, GLConstants stenop, GLConstants stenfunc, OverlayType otype=OVERLAY_TYPE_NONE)=0
Dirty helper function to change the render infos.
PointType2D< int32_t > Point
Definition: point.h:195
uint32_t getHeight() const
Definition: image.cpp:160
Base class for all view renderers View renderer renders one aspect of the view shown on screen...
Definition: rendererbase.h:78
void setBackground(uint8_t br, uint8_t bg, uint8_t bb, uint8_t ba=255)
Set default background quad r,g,b,a values for background.
A basic layer on a map.
Definition: layer.h:99
virtual void setEnabled(bool enabled)
Enables renderer.
unsigned short uint16_t
Definition: core.h:39
T y
The Y Coordinate.
Definition: rect.h:87
T right() const
The X coordinate of the right edge.
Definition: rect.h:168
void setColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Changes default font color Only useful for .ttf fonts.
virtual void render(const Rect &rect, uint8_t alpha=255, uint8_t const *rgb=0)=0
Renders itself to the current render target (main screen or attached destination image) at the rectan...
FloatingTextRenderer(RenderBackend *renderbackend, int32_t position)
Constructor.
virtual void fillRectangle(const Point &p, uint16_t w, uint16_t h, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Draws a filled axis parallel rectangle.
uint32_t getWidth() const
unsigned int uint32_t
Definition: core.h:40
virtual ~FloatingTextRenderer()
Destructor.
T w
Width of the rectangle.
Definition: rect.h:90
An Instance is an "instantiation" of an Object at a Location.
Definition: instance.h:94
T bottom() const
The Y coordinate of the bottom edge.
Definition: rect.h:173
uint32_t getWidth() const
Definition: image.cpp:151