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 
00037 #include "opengl_gui_graphics.h"
00038 
00039 namespace FIFE {
00040     struct GLEnable {
00041         GLenum m_flag;
00042         GLboolean m_oldval;
00043         GLEnable(GLenum flag) : m_flag(flag) { 
00044             glGetBooleanv(flag, &m_oldval);
00045             if (!m_oldval) {
00046                 glEnable(flag);
00047             }
00048         }
00049         ~GLEnable() { 
00050             if (!m_oldval) {
00051                 glDisable(m_flag);
00052             }
00053         }
00054     };
00055 
00056     struct GLDisable {
00057         GLenum m_flag;
00058         GLboolean m_oldval;
00059         GLDisable(GLenum flag) : m_flag(flag) { 
00060             glGetBooleanv(flag, &m_oldval);
00061             if (m_oldval) {
00062                 glDisable(flag);
00063             }
00064         }
00065         ~GLDisable() { 
00066             if (m_oldval) {
00067                 glEnable(m_flag);
00068             }
00069         }
00070     };
00071 
00072     OpenGLGuiGraphics::OpenGLGuiGraphics(ImagePool& pool): m_pool(pool) {
00073         mTarget = SDL_GetVideoSurface();
00074         assert(mTarget);
00075         setTargetPlane(mTarget->w, mTarget->h);
00076         
00077     }
00078 
00079     void OpenGLGuiGraphics::drawImage(const gcn::Image* image, int srcX, int srcY, int dstX, int dstY, int width, int height) {
00080         const GuiImage* g_img = dynamic_cast<const GuiImage*>(image);
00081         assert(g_img);
00082         Image& fifeimg = m_pool.getImage(g_img->getPoolId());
00083         const gcn::ClipRectangle& clip = getCurrentClipArea();
00084         Rect rect(dstX, dstY, width, height);
00085         rect.x += clip.xOffset;
00086         rect.y += clip.yOffset;
00087         GLEnable flag(GL_TEXTURE_2D);
00088         fifeimg.render(rect, mTarget);
00089     }
00090 
00091     void OpenGLGuiGraphics::drawText(const std::string& text, int x, int y,
00092             unsigned int alignment) {
00093         if (mFont == NULL)
00094         {
00095             throw GCN_EXCEPTION("No font set.");
00096         }
00097 
00098         GLEnable flag(GL_TEXTURE_2D);
00099         switch (alignment)
00100         {
00101             case LEFT:
00102                 mFont->drawString(this, text, x, y);
00103                 break;
00104             case CENTER:
00105                 mFont->drawString(this, text, x - mFont->getWidth(text) / 2, y);
00106                 break;
00107             case RIGHT:
00108                 mFont->drawString(this, text, x - mFont->getWidth(text), y);
00109                 break;
00110             default:
00111                 throw GCN_EXCEPTION("Unknown alignment.");
00112         }
00113     }
00114 
00115     void OpenGLGuiGraphics::drawPoint(int x, int y) {
00116         GLDisable flag(GL_TEXTURE_2D);
00117         gcn::OpenGLGraphics::drawPoint(x, y);
00118     }
00119 
00120     void OpenGLGuiGraphics::drawLine(int x1, int y1, int x2, int y2) {
00121         GLDisable flag(GL_TEXTURE_2D);
00122         gcn::OpenGLGraphics::drawLine(x1, y1, x2, y2);
00123     }
00124 
00125     void OpenGLGuiGraphics::drawRectangle(const gcn::Rectangle& rectangle) {
00126         GLDisable flag(GL_TEXTURE_2D);
00127         gcn::OpenGLGraphics::drawRectangle(rectangle);
00128     }
00129 }