00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <guichan/opengl.hpp>
00026 #include <guichan/font.hpp>
00027 #include <guichan/exception.hpp>
00028
00029
00030
00031
00032
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 }