Debian Codegraph @

engine/core/gui/fifechan/base/sdl/sdl_gui_graphics.cpp

raw
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 #include <SDL.h>
26 #include <fifechan/font.hpp>
27
28 // FIFE includes
29 // These includes are split up in two parts, separated by one empty line
30 // First block: files included from the FIFE root src dir
31 #include "gui/fifechan/base/gui_image.h"
32 #include "util/base/exception.h"
33 #include "util/structures/rect.h"
34 #include "video/image.h"
35 #include "video/renderbackend.h"
36
37 #include "sdl_gui_graphics.h"
38
39 namespace FIFE {
40 static Logger _log(LM_GUI);
41
42 SdlGuiGraphics::SdlGuiGraphics() {
43 m_renderbackend = RenderBackend::instance();
44 setTarget(m_renderbackend->getScreenSurface());
45 }
46
47 void SdlGuiGraphics::updateTarget() {
48 setTarget(m_renderbackend->getScreenSurface());
49 }
50
51 void SdlGuiGraphics::drawImage(const fcn::Image* image, int32_t srcX, int32_t srcY, int32_t dstX, int32_t dstY, int32_t width, int32_t height) {
52 const GuiImage* g_img = dynamic_cast<const GuiImage*>(image);
53 assert(g_img);
54
55 ImagePtr fifeimg = g_img->getFIFEImage();
56 const fcn::ClipRectangle& clip = getCurrentClipArea();
57 Rect rect(dstX, dstY, width, height);
58 rect.x += clip.xOffset;
59 rect.y += clip.yOffset;
60
61 fifeimg->render(rect);
62 }
63
64 void SdlGuiGraphics::drawText(const std::string& text, int32_t x, int32_t y, uint32_t alignment) {
65 if (mFont == NULL)
66 {
67 throw GuiException("SdlGuiGraphics::drawText() - No font set!");
68 }
69
70 switch (alignment)
71 {
72 case Left:
73 mFont->drawString(this, text, x, y);
74 break;
75 case Center:
76 mFont->drawString(this, text, x - mFont->getWidth(text) / 2, y);
77 break;
78 case Right:
79 mFont->drawString(this, text, x - mFont->getWidth(text), y);
80 break;
81 default:
82 FL_WARN(_log, LMsg("SdlGuiGraphics::drawText() - ") << "Unknown alignment: " << alignment);
83 mFont->drawString(this, text, x, y);
84 }
85 }
86
87 void SdlGuiGraphics::drawPoint(int32_t x, int32_t y) {
88 const fcn::ClipRectangle& top = mClipStack.top();
89 m_renderbackend->putPixel(x + top.xOffset, y + top.yOffset,
90 mColor.r, mColor.g, mColor.b, mColor.a);
91 }
92
93 void SdlGuiGraphics::drawLine(int32_t x1, int32_t y1, int32_t x2, int32_t y2) {
94 const fcn::ClipRectangle& top = mClipStack.top();
95 m_renderbackend->drawLine(Point(x1 + top.xOffset, y1 + top.yOffset), Point(x2 + top.xOffset, y2 + top.yOffset), mColor.r, mColor.g, mColor.b, mColor.a);
96 }
97
98 void SdlGuiGraphics::drawLine(int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t width) {
99 const fcn::ClipRectangle& top = mClipStack.top();
100 m_renderbackend->drawThickLine(Point(x1+top.xOffset, y1+top.yOffset), Point(x2+top.xOffset, y2+top.yOffset), width, mColor.r, mColor.g, mColor.b, mColor.a);
101 }
102
103 void SdlGuiGraphics::drawPolyLine(const fcn::PointVector& points, uint32_t width) {
104 const fcn::ClipRectangle& top = mClipStack.top();
105 std::vector<Point> npoints;
106 fcn::PointVector::const_iterator it = points.begin();
107 for (; it != points.end(); ++it) {
108 npoints.push_back(Point((*it).x+top.xOffset, (*it).y+top.yOffset));
109 }
110 m_renderbackend->drawPolyLine(npoints, width, mColor.r, mColor.g, mColor.b, mColor.a);
111 }
112
113 void SdlGuiGraphics::drawBezier(const fcn::PointVector& points, int32_t steps, uint32_t width) {
114 const fcn::ClipRectangle& top = mClipStack.top();
115 std::vector<Point> npoints;
116 fcn::PointVector::const_iterator it = points.begin();
117 for (; it != points.end(); ++it) {
118 npoints.push_back(Point((*it).x+top.xOffset, (*it).y+top.yOffset));
119 }
120 m_renderbackend->drawBezier(npoints, steps, width, mColor.r, mColor.g, mColor.b, mColor.a);
121 }
122
123 void SdlGuiGraphics::drawRectangle(const fcn::Rectangle& rectangle) {
124 const fcn::ClipRectangle& top = mClipStack.top();
125 m_renderbackend->drawRectangle(
126 Point(rectangle.x + top.xOffset, rectangle.y + top.yOffset),
127 rectangle.width, rectangle.height,
128 mColor.r, mColor.g, mColor.b, mColor.a);
129 }
130
131 void SdlGuiGraphics::fillRectangle(const fcn::Rectangle& rectangle) {
132 const fcn::ClipRectangle& top = mClipStack.top();
133 m_renderbackend->fillRectangle(
134 Point(rectangle.x + top.xOffset, rectangle.y + top.yOffset),
135 rectangle.width, rectangle.height,
136 mColor.r, mColor.g, mColor.b, mColor.a);
137 }
138
139 void SdlGuiGraphics::drawCircle(const fcn::Point& p, uint32_t radius) {
140 const fcn::ClipRectangle& top = mClipStack.top();
141 m_renderbackend->drawCircle(Point(p.x+top.xOffset, p.y+top.yOffset), radius, mColor.r, mColor.g, mColor.b, mColor.a);
142 }
143
144 void SdlGuiGraphics::drawFillCircle(const fcn::Point& p, uint32_t radius) {
145 const fcn::ClipRectangle& top = mClipStack.top();
146 m_renderbackend->drawFillCircle(Point(p.x+top.xOffset, p.y+top.yOffset), radius, mColor.r, mColor.g, mColor.b, mColor.a);
147 }
148
149 void SdlGuiGraphics::drawCircleSegment(const fcn::Point& p, uint32_t radius, int32_t sangle, int32_t eangle) {
150 const fcn::ClipRectangle& top = mClipStack.top();
151 m_renderbackend->drawCircleSegment(Point(p.x+top.xOffset, p.y+top.yOffset), radius, sangle, eangle, mColor.r, mColor.g, mColor.b, mColor.a);
152 }
153
154 void SdlGuiGraphics::drawFillCircleSegment(const fcn::Point& p, uint32_t radius, int32_t sangle, int32_t eangle) {
155 const fcn::ClipRectangle& top = mClipStack.top();
156 m_renderbackend->drawFillCircleSegment(Point(p.x+top.xOffset, p.y+top.yOffset), radius, sangle, eangle, mColor.r, mColor.g, mColor.b, mColor.a);
157 }
158
159 void SdlGuiGraphics::_beginDraw() {
160 fcn::Rectangle area(0, 0, mTarget->w, mTarget->h);
161 fcn::Graphics::pushClipArea(area);
162 m_renderbackend->pushClipArea(Rect(0, 0, mTarget->w, mTarget->h), false);
163 }
164
165 void SdlGuiGraphics::_endDraw() {
166 // Cleanup
167 fcn::Graphics::popClipArea();
168 m_renderbackend->popClipArea();
169 }
170
171 bool SdlGuiGraphics::pushClipArea(fcn::Rectangle area) {
172 fcn::Graphics::pushClipArea(area);
173
174 // Due to some odd conception in Fifechan some of area
175 // has xOffset and yOffset > 0. And if it happens we
176 // need to offset our clip area. Or we can use Fifechan stack.
177 const fcn::ClipRectangle& top = mClipStack.top();
178
179 m_renderbackend->pushClipArea(
180 Rect(top.x, top.y, top.width, top.height), false);
181
182 return true;
183 }
184
185 void SdlGuiGraphics::popClipArea() {
186 fcn::Graphics::popClipArea();
187 m_renderbackend->popClipArea();
188 }
189
190 void SdlGuiGraphics::setColor(const fcn::Color& color) {
191 mColor = color;
192 }
193 }