| 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" |
| 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 |
/** Logger to use for this source file. |
| 46 |
* @relates Logger |
| 47 |
*/ |
| 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 |
|
| 60 |
FloatingTextRenderer::FloatingTextRenderer(const FloatingTextRenderer& old): |
| 61 |
RendererBase(old), |
| 62 |
m_renderbackend(old.m_renderbackend), |
| 63 |
m_font(old.m_font), |
| 64 |
m_font_color(old.m_font_color), |
| 65 |
m_color(old.m_color), |
| 66 |
m_background(old.m_background), |
| 67 |
m_backborder(old.m_backborder) { |
| 68 |
setEnabled(false); |
| 69 |
} |
| 70 |
|
| 71 |
RendererBase* FloatingTextRenderer::clone() { |
| 72 |
return new FloatingTextRenderer(*this); |
| 73 |
} |
| 74 |
|
| 75 |
FloatingTextRenderer::~FloatingTextRenderer() { |
| 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(); |
| 85 |
uint32_t lm = m_renderbackend->getLightingModel(); |
| 86 |
SDL_Color old_color = m_font->getColor(); |
| 87 |
if(m_font_color) { |
| 88 |
m_font->setColor(m_color.r, m_color.g, m_color.b, m_color.a); |
| 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; /// the center of the text rect is always aligned to the instance's rect center. |
| 98 |
r.y = ir.y- img->getHeight(); /// make the text rect floating higher than the instance. |
| 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) { |
| 118 |
m_renderbackend->drawRectangle(p, r.w+2*overdraw, r.h+2*overdraw, m_backbordercolor.r, m_backbordercolor.g, m_backbordercolor.b, m_backbordercolor.a); |
| 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 |
|
| 139 |
void FloatingTextRenderer::setColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a) { |
| 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 |
|
| 148 |
void FloatingTextRenderer::setBackground(uint8_t br, uint8_t bg, uint8_t bb, uint8_t ba) { |
| 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 |
|
| 157 |
void FloatingTextRenderer::setBorder(uint8_t bbr, uint8_t bbg, uint8_t bbb, uint8_t bba) { |
| 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 |
|
| 166 |
void FloatingTextRenderer::resetBackground() { |
| 167 |
m_background = false; |
| 168 |
} |
| 169 |
|
| 170 |
void FloatingTextRenderer::resetBorder() { |
| 171 |
m_backborder = false; |
| 172 |
} |
| 173 |
|
| 174 |
FloatingTextRenderer* FloatingTextRenderer::getInstance(IRendererContainer* cnt) { |
| 175 |
return dynamic_cast<FloatingTextRenderer*>(cnt->getRenderer("FloatingTextRenderer")); |
| 176 |
} |
| 177 |
} |