FIFE  2008.0
opengl_gui_graphics.cpp
1 /***************************************************************************
2  * Copyright (C) 2005-2011 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
25 #include "video/opengl/fife_opengl.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"
37 #include "gui/guichan/base/gui_image.h"
38 #include "util/structures/rect.h"
39 #include "video/image.h"
40 #include "video/imagemanager.h"
41 #include "video/opengl/renderbackendopengl.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 = gcn::Color(255, 255, 255, 255);
53  m_renderbackend = static_cast<RenderBackendOpenGL*>(RenderBackend::instance());
54  }
55 
56  void OpenGLGuiGraphics::drawImage(const gcn::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 gcn::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 gcn::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 gcn::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 gcn::Rectangle& rectangle) {
115  const gcn::ClipRectangle& top = mClipStack.top();
116  m_renderbackend->drawRectangle(
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 gcn::Rectangle& rectangle) {
123  const gcn::ClipRectangle& top = mClipStack.top();
124  m_renderbackend->fillRectangle(
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 
130  void OpenGLGuiGraphics::_beginDraw() {
131  gcn::Rectangle area(0, 0, mWidth, mHeight);
132  gcn::Graphics::pushClipArea(area);
133  m_renderbackend->pushClipArea(Rect(0, 0, mWidth, mHeight), false);
134  }
135 
136  void OpenGLGuiGraphics::_endDraw() {
137  m_renderbackend->renderVertexArrays();
138 
139  // Cleanup
140  gcn::Graphics::popClipArea();
141  m_renderbackend->popClipArea();
142  }
143 
144  bool OpenGLGuiGraphics::pushClipArea(gcn::Rectangle area) {
145  // Render what we gathered so far
146  m_renderbackend->renderVertexArrays();
147  gcn::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 guichan stack.
152  const gcn::ClipRectangle& top = mClipStack.top();
153 
154  m_renderbackend->pushClipArea(
155  Rect(top.x, top.y, top.width, top.height), false);
156 
157  return true;
158  }
159 
160  void OpenGLGuiGraphics::popClipArea() {
161  // Render what we gathered so far
162  m_renderbackend->renderVertexArrays();
163  gcn::Graphics::popClipArea();
164  m_renderbackend->popClipArea();
165  }
166 
167  void OpenGLGuiGraphics::setColor(const gcn::Color& color) {
168  mColor = color;
169  }
170 }
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)
virtual bool putPixel(int32_t x, int32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
virtual void drawLine(const Point &p1, const Point &p2, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
void pushClipArea(const Rect &cliparea, bool clear=true)
virtual void render(const Rect &rect, uint8_t alpha=255, uint8_t const *rgb=0)=0
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)
credit to phoku for his NodeDisplay example which the visitor code is adapted from ( he coded the qua...
Definition: soundclip.cpp:39