opengl_gui_graphics.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 
00024 // 3rd party library includes
00025 #include <guichan/opengl.hpp>
00026 #include <guichan/font.hpp>
00027 #include <guichan/exception.hpp>
00028 
00029 
00030 // FIFE includes
00031 // These includes are split up in two parts, separated by one empty line
00032 // First block: files included from the FIFE root src dir
00033 #include "video/image.h"
00034 #include "gui/base/gui_image.h"
00035 #include "util/structures/rect.h"
00036 #include "video/opengl/fife_opengl.h"
00037 
00038 #include "opengl_gui_graphics.h"
00039 
00040 namespace FIFE {
00041     OpenGLGuiGraphics::OpenGLGuiGraphics(ImagePool& pool): m_pool(pool) {
00042         mTarget = SDL_GetVideoSurface();
00043         assert(mTarget);
00044         setTargetPlane(mTarget->w, mTarget->h);
00045 
00046     }
00047 
00048     void OpenGLGuiGraphics::drawImage(const gcn::Image* image, int srcX, int srcY, int dstX, int dstY, int width, int height) {
00049         const GuiImage* g_img = dynamic_cast<const GuiImage*>(image);
00050         assert(g_img);
00051         Image& fifeimg = m_pool.getImage(g_img->getPoolId());
00052         const gcn::ClipRectangle& clip = getCurrentClipArea();
00053         Rect rect(dstX, dstY, width, height);
00054         rect.x += clip.xOffset;
00055         rect.y += clip.yOffset;
00056         GLEnable flag(GL_TEXTURE_2D);
00057         fifeimg.render(rect, mTarget);
00058     }
00059 
00060     void OpenGLGuiGraphics::drawText(const std::string& text, int x, int y,
00061             unsigned int alignment) {
00062         if (mFont == NULL)
00063         {
00064             throw GCN_EXCEPTION("No font set.");
00065         }
00066 
00067         GLEnable flag(GL_TEXTURE_2D);
00068         switch (alignment)
00069         {
00070             case LEFT:
00071                 mFont->drawString(this, text, x, y);
00072                 break;
00073             case CENTER:
00074                 mFont->drawString(this, text, x - mFont->getWidth(text) / 2, y);
00075                 break;
00076             case RIGHT:
00077                 mFont->drawString(this, text, x - mFont->getWidth(text), y);
00078                 break;
00079             default:
00080                 throw GCN_EXCEPTION("Unknown alignment.");
00081         }
00082     }
00083 
00084     void OpenGLGuiGraphics::drawPoint(int x, int y) {
00085         GLDisable flag(GL_TEXTURE_2D);
00086         gcn::OpenGLGraphics::drawPoint(x, y);
00087     }
00088 
00089     void OpenGLGuiGraphics::drawLine(int x1, int y1, int x2, int y2) {
00090         GLDisable flag(GL_TEXTURE_2D);
00091         gcn::OpenGLGraphics::drawLine(x1, y1, x2, y2);
00092     }
00093 
00094     void OpenGLGuiGraphics::drawRectangle(const gcn::Rectangle& rectangle) {
00095         GLDisable flag(GL_TEXTURE_2D);
00096         gcn::OpenGLGraphics::drawRectangle(rectangle);
00097     }
00098 }
Generated on Wed Nov 23 13:04:42 2011 for FIFE by  doxygen 1.6.3