FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
sdl_gui_graphics.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 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
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 
44  setTarget(m_renderbackend->getScreenSurface());
45  }
46 
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 
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();
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();
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 
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 
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 
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 
166  // Cleanup
167  fcn::Graphics::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 
180  Rect(top.x, top.y, top.width, top.height), false);
181 
182  return true;
183  }
184 
186  fcn::Graphics::popClipArea();
188  }
189 
190  void SdlGuiGraphics::setColor(const fcn::Color& color) {
191  mColor = color;
192  }
193 }
Definition: modules.h:41
#define FL_WARN(logger, msg)
Definition: logger.h:72
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.
virtual void drawRectangle(const fcn::Rectangle &rectangle)
Helper class to create log strings out from separate parts Usage: LMsg("some text") << variable << "...
Definition: logger.h:82
SdlGuiGraphics()
Constructor.
T x
The X Coordinate.
Definition: rect.h:84
virtual void setColor(const fcn::Color &color)
virtual void drawCircleSegment(const fcn::Point &p, uint32_t radius, int32_t sangle, int32_t eangle)
static Logger _log(LM_AUDIO)
std::vector< Point > PointVector
Definition: point.h:197
virtual void drawThickLine(const Point &p1, const Point &p2, uint8_t width, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Draws line between given points with given RGBA and width.
virtual void drawCircle(const Point &p, uint32_t radius, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Draws a circle.
virtual void drawFillCircleSegment(const fcn::Point &p, uint32_t radius, int32_t sangle, int32_t eangle)
static RenderBackend * instance()
Definition: singleton.h:84
virtual void drawPolyLine(const std::vector< Point > &points, uint8_t width, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Draws lines between given points with given RGBA and width.
virtual void drawFillCircle(const Point &p, uint32_t radius, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Draws a filled circle.
virtual void drawCircleSegment(const Point &p, uint32_t radius, int32_t sangle, int32_t eangle, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Draws a circle segment.
virtual void drawPoint(int32_t x, int32_t y)
PointType2D< int32_t > Point
Definition: point.h:195
void pushClipArea(const Rect &cliparea, bool clear=true)
Pushes clip area to clip stack Clip areas define which area is drawn on screen.
virtual void drawText(const std::string &text, int32_t x, int32_t y, uint32_t alignment)
virtual void popClipArea()
SDL_Surface * getScreenSurface()
Returns screen render surface.
virtual bool putPixel(int32_t x, int32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Writes pixel to given position.
void popClipArea()
Pops clip area from clip stack.
virtual void drawLine(const Point &p1, const Point &p2, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Draws line between given points with given RGBA.
virtual void drawImage(const fcn::Image *image, int32_t srcX, int32_t srcY, int32_t dstX, int32_t dstY, int32_t width, int32_t height)
T y
The Y Coordinate.
Definition: rect.h:87
virtual void drawPolyLine(const fcn::PointVector &points, uint32_t width)
virtual bool pushClipArea(fcn::Rectangle area)
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...
RectType< int32_t > Rect
Definition: rect.h:258
virtual void fillRectangle(const fcn::Rectangle &rectangle)
virtual void drawFillCircle(const fcn::Point &p, uint32_t radius)
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.
ImagePtr getFIFEImage() const
Definition: gui_image.h:52
unsigned int uint32_t
Definition: core.h:40
virtual void drawCircle(const fcn::Point &p, uint32_t radius)
virtual void drawLine(int32_t x1, int32_t y1, int32_t x2, int32_t y2)
virtual void drawBezier(const fcn::PointVector &points, int32_t steps, uint32_t width)
virtual void drawBezier(const std::vector< Point > &points, int32_t steps, uint8_t width, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Draws bezier curve between given points with given RGBA and width.
RenderBackend * m_renderbackend
virtual void drawFillCircleSegment(const Point &p, uint32_t radius, int32_t sangle, int32_t eangle, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)=0
Draws a filled circle segment.