FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
opengl_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 fifechan includes gl.h
26 
27 // 3rd party library includes
28 #include <fifechan/opengl.hpp>
29 #include <fifechan/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 "opengl_gui_graphics.h"
44 
45 namespace FIFE {
46  static Logger _log(LM_GUI);
47 
49  SDL_Surface* target = SDL_GetVideoSurface();
50  assert(target);
51  setTargetPlane(target->w, target->h);
52  mColor = fcn::Color(255, 255, 255, 255);
54  }
55 
56  void OpenGLGuiGraphics::drawImage(const fcn::Image* image, int32_t srcX, int32_t srcY, int32_t dstX, int32_t dstY, int32_t width, int32_t height) {
57  const GuiImage* g_img = dynamic_cast<const GuiImage*>(image);
58  assert(g_img);
59 
60  ImagePtr fifeimg = g_img->getFIFEImage();
61  const fcn::ClipRectangle& clip = mClipStack.top();
62  fifeimg->render(Rect(dstX + clip.xOffset, dstY + clip.yOffset,
63  width, height));
64  }
65 
66  void OpenGLGuiGraphics::drawText(const std::string& text, int32_t x, int32_t y,
67  uint32_t alignment) {
68  if (mFont == NULL)
69  {
70  throw GuiException("OpenGLGuiGraphics::drawText() - No font set!");
71  }
72 
73  switch (alignment)
74  {
75  case Left:
76  mFont->drawString(this, text, x, y);
77  break;
78  case Center:
79  mFont->drawString(this, text, x - mFont->getWidth(text) / 2, y);
80  break;
81  case Right:
82  mFont->drawString(this, text, x - mFont->getWidth(text), y);
83  break;
84  default:
85  FL_WARN(_log, LMsg("OpenGLGuiGraphics::drawText() - ") << "Unknown alignment: " << alignment);
86  mFont->drawString(this, text, x, y);
87  }
88  }
89 
90  void OpenGLGuiGraphics::drawPoint(int32_t x, int32_t y) {
91  const fcn::ClipRectangle& top = mClipStack.top();
92  m_renderbackend->putPixel(x + top.xOffset, y + top.yOffset,
93  mColor.r, mColor.g, mColor.b, mColor.a);
94  }
95 
96  void OpenGLGuiGraphics::drawLine(int32_t x1, int32_t y1, int32_t x2, int32_t y2) {
97  const fcn::ClipRectangle& top = mClipStack.top();
98  x1 += top.xOffset;
99  x2 += top.xOffset;
100  y1 += top.yOffset;
101  y2 += top.yOffset;
102 
103  Point pbegin(static_cast<int32_t>(ceil(x1 + 0.375f)), static_cast<int32_t>(ceil(y1 + 0.375f)));
104  Point pend(static_cast<int32_t>(ceil(x2 + 0.625f)), static_cast<int32_t>(ceil(y2 + 0.625f)));
105 
106  m_renderbackend->drawLine(pbegin, pend,
107  mColor.r, mColor.g, mColor.b, mColor.a);
108  m_renderbackend->putPixel(pbegin.x, pbegin.y,
109  mColor.r, mColor.g, mColor.b, mColor.a);
110  m_renderbackend->putPixel(pend.x, pend.y,
111  mColor.r, mColor.g, mColor.b, mColor.a);
112  }
113 
114  void OpenGLGuiGraphics::drawRectangle(const fcn::Rectangle& rectangle) {
115  const fcn::ClipRectangle& top = mClipStack.top();
117  Point(rectangle.x + top.xOffset, rectangle.y + top.yOffset),
118  rectangle.width, rectangle.height,
119  mColor.r, mColor.g, mColor.b, mColor.a);
120  }
121 
122  void OpenGLGuiGraphics::fillRectangle(const fcn::Rectangle& rectangle) {
123  const fcn::ClipRectangle& top = mClipStack.top();
125  Point(rectangle.x + top.xOffset, rectangle.y + top.yOffset),
126  rectangle.width, rectangle.height,
127  mColor.r, mColor.g, mColor.b, mColor.a);
128  }
129 
131  fcn::Rectangle area(0, 0, mWidth, mHeight);
132  fcn::Graphics::pushClipArea(area);
133  m_renderbackend->pushClipArea(Rect(0, 0, mWidth, mHeight), false);
134  }
135 
138 
139  // Cleanup
140  fcn::Graphics::popClipArea();
142  }
143 
144  bool OpenGLGuiGraphics::pushClipArea(fcn::Rectangle area) {
145  // Render what we gathered so far
147  fcn::Graphics::pushClipArea(area);
148 
149  // Due to some odd conception in guiChan some of area
150  // has xOffset and yOffset > 0. And if it happens we
151  // need to offset our clip area. Or we can use fifechan stack.
152  const fcn::ClipRectangle& top = mClipStack.top();
153 
155  Rect(top.x, top.y, top.width, top.height), false);
156 
157  return true;
158  }
159 
161  // Render what we gathered so far
163  fcn::Graphics::popClipArea();
165  }
166 
167  void OpenGLGuiGraphics::setColor(const fcn::Color& color) {
168  mColor = color;
169  }
170 }
Definition: modules.h:41
#define FL_WARN(logger, msg)
Definition: logger.h:72
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.
Helper class to create log strings out from separate parts Usage: LMsg("some text") << variable << "...
Definition: logger.h:82
virtual void setColor(const fcn::Color &color)
virtual void fillRectangle(const fcn::Rectangle &rectangle)
virtual void renderVertexArrays()
Render the Vertex Arrays, only for primitives (points, lines,...)
virtual void drawRectangle(const fcn::Rectangle &rectangle)
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)
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.
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.
static RenderBackend * instance()
Definition: singleton.h:84
virtual void drawLine(int32_t x1, int32_t y1, int32_t x2, int32_t y2)
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.
void popClipArea()
Pops clip area from clip stack.
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
RenderBackendOpenGL * m_renderbackend
virtual void drawText(const std::string &text, int32_t x, int32_t y, uint32_t alignment)
virtual bool pushClipArea(fcn::Rectangle area)
ImagePtr getFIFEImage() const
Definition: gui_image.h:52
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.
unsigned int uint32_t
Definition: core.h:40
virtual void drawPoint(int32_t x, int32_t y)
The main class of the OpenGL-based renderer.