FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
opengle_gui_graphics.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 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 // This needs to be here, before guichan includes gl.h
26 
27 // 3rd party library includes
28 #include <guichan/opengl.hpp>
29 #include <guichan/font.hpp>
30 
31 
32 // FIFE includes
33 // These includes are split up in two parts, separated by one empty line
34 // First block: files included from the FIFE root src dir
35 #include "util/log/logger.h"
36 #include "util/base/exception.h"
38 #include "util/structures/rect.h"
39 #include "video/image.h"
40 #include "video/imagemanager.h"
42 
43 #include "opengle_gui_graphics.h"
44 
45 namespace FIFE {
49  static Logger _log(LM_GUI);
50 
52  SDL_Surface* target = SDL_GetVideoSurface();
53  assert(target);
54  setTargetPlane(target->w, target->h);
55  mColor = gcn::Color(255, 255, 255, 255);
57  }
58 
59  void OpenGLeGuiGraphics::drawImage(const gcn::Image* image, int32_t srcX, int32_t srcY, int32_t dstX, int32_t dstY, int32_t width, int32_t height) {
60  const GuiImage* g_img = dynamic_cast<const GuiImage*>(image);
61  assert(g_img);
62 
63  ImagePtr fifeimg = g_img->getFIFEImage();
64  const gcn::ClipRectangle& clip = mClipStack.top();
65  fifeimg->render(Rect(dstX + clip.xOffset, dstY + clip.yOffset,
66  width, height));
67  }
68 
69  void OpenGLeGuiGraphics::drawText(const std::string& text, int32_t x, int32_t y,
70  uint32_t alignment) {
71  if (mFont == NULL)
72  {
73  throw GuiException("OpenGLGuiGraphics::drawText() - No font set!");
74  }
75 
76  switch (alignment)
77  {
78  case LEFT:
79  mFont->drawString(this, text, x, y);
80  break;
81  case CENTER:
82  mFont->drawString(this, text, x - mFont->getWidth(text) / 2, y);
83  break;
84  case RIGHT:
85  mFont->drawString(this, text, x - mFont->getWidth(text), y);
86  break;
87  default:
88  FL_WARN(_log, LMsg("OpenGLGuiGraphics::drawText() - ") << "Unknown alignment: " << alignment);
89  mFont->drawString(this, text, x, y);
90  }
91  }
92 
93  void OpenGLeGuiGraphics::drawPoint(int32_t x, int32_t y) {
94  const gcn::ClipRectangle& top = mClipStack.top();
95  m_renderbackend->putPixel(x + top.xOffset, y + top.yOffset,
96  mColor.r, mColor.g, mColor.b, mColor.a);
97  }
98 
99  void OpenGLeGuiGraphics::drawLine(int32_t x1, int32_t y1, int32_t x2, int32_t y2) {
100  const gcn::ClipRectangle& top = mClipStack.top();
101  x1 += top.xOffset;
102  x2 += top.xOffset;
103  y1 += top.yOffset;
104  y2 += top.yOffset;
105 
106  Point pbegin(static_cast<int32_t>(ceil(x1 + 0.375f)), static_cast<int32_t>(ceil(y1 + 0.375f)));
107  Point pend(static_cast<int32_t>(ceil(x2 + 0.625f)), static_cast<int32_t>(ceil(y2 + 0.625f)));
108 
109  m_renderbackend->drawLine(pbegin, pend,
110  mColor.r, mColor.g, mColor.b, mColor.a);
111  m_renderbackend->putPixel(pbegin.x, pbegin.y,
112  mColor.r, mColor.g, mColor.b, mColor.a);
113  m_renderbackend->putPixel(pend.x, pend.y,
114  mColor.r, mColor.g, mColor.b, mColor.a);
115  }
116 
117  void OpenGLeGuiGraphics::drawRectangle(const gcn::Rectangle& rectangle) {
118  const gcn::ClipRectangle& top = mClipStack.top();
120  Point(rectangle.x + top.xOffset, rectangle.y + top.yOffset),
121  rectangle.width, rectangle.height,
122  mColor.r, mColor.g, mColor.b, mColor.a);
123  }
124 
125  void OpenGLeGuiGraphics::fillRectangle(const gcn::Rectangle& rectangle) {
126  const gcn::ClipRectangle& top = mClipStack.top();
128  Point(rectangle.x + top.xOffset, rectangle.y + top.yOffset),
129  rectangle.width, rectangle.height,
130  mColor.r, mColor.g, mColor.b, mColor.a);
131  }
132 
134  gcn::Rectangle area(0, 0, mWidth, mHeight);
135  gcn::Graphics::pushClipArea(area);
136  m_renderbackend->pushClipArea(Rect(0, 0, mWidth, mHeight), false);
137  }
138 
141 
142  // Cleanup
143  gcn::Graphics::popClipArea();
145  }
146 
147  bool OpenGLeGuiGraphics::pushClipArea(gcn::Rectangle area) {
148  // Render what we gathered so far
150  gcn::Graphics::pushClipArea(area);
151 
152  // Due to some odd conception in guiChan some of area
153  // has xOffset and yOffset > 0. And if it happens we
154  // need to offset our clip area. Or we can use guichan stack.
155  const gcn::ClipRectangle& top = mClipStack.top();
156 
158  Rect(top.x, top.y, top.width, top.height), false);
159 
160  return true;
161  }
162 
164  // Render what we gathered so far
166  gcn::Graphics::popClipArea();
168  }
169 
170  void OpenGLeGuiGraphics::setColor(const gcn::Color& color) {
171  mColor = color;
172  }
173 }
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)
Draws an axis parallel rectangle.
Helper class to create log strings out from separate parts Usage: LMsg(&quot;some text&quot;) &lt;&lt; variable &lt;&lt; &quot;...
Definition: logger.h:82
static Logger _log(LM_AUDIO)
virtual void drawLine(const Point &p1, const Point &p2, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Draws line between given points with given RGBA.
virtual void renderVertexArrays()
Render the Vertex Arrays, only for primitives (points, lines,...)
virtual void fillRectangle(const gcn::Rectangle &rectangle)
virtual bool pushClipArea(gcn::Rectangle area)
static RenderBackend * instance()
Definition: singleton.h:84
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)
Draws a filled axis parallel rectangle.
virtual void drawPoint(int32_t x, int32_t y)
PointType2D< int32_t > Point
Definition: point.h:194
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 drawLine(int32_t x1, int32_t y1, int32_t x2, int32_t y2)
virtual bool putPixel(int32_t x, int32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Writes pixel to given position.
void popClipArea()
Pops clip area from clip stack.
RenderBackendOpenGLe * m_renderbackend
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 drawText(const std::string &text, int32_t x, int32_t y, uint32_t alignment)
ImagePtr getFIFEImage() const
Definition: gui_image.h:52
virtual void drawRectangle(const gcn::Rectangle &rectangle)
The main class of the OpenGL-based experimental renderer.
virtual void setColor(const gcn::Color &color)
unsigned int uint32_t
Definition: core.h:40
virtual void drawImage(const gcn::Image *image, int32_t srcX, int32_t srcY, int32_t dstX, int32_t dstY, int32_t width, int32_t height)