| 1 |
/* _______ __ __ __ ______ __ __ _______ __ __ |
| 2 |
* / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\ |
| 3 |
* / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / / |
| 4 |
* / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / / |
| 5 |
* / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / / |
| 6 |
* /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ / |
| 7 |
* \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/ |
| 8 |
* |
| 9 |
* Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson |
| 10 |
* |
| 11 |
* |
| 12 |
* Per Larsson a.k.a finalman |
| 13 |
* Olof Naessén a.k.a jansem/yakslem |
| 14 |
* |
| 15 |
* Visit: http://guichan.sourceforge.net |
| 16 |
* |
| 17 |
* License: (BSD) |
| 18 |
* Redistribution and use in source and binary forms, with or without |
| 19 |
* modification, are permitted provided that the following conditions |
| 20 |
* are met: |
| 21 |
* 1. Redistributions of source code must retain the above copyright |
| 22 |
* notice, this list of conditions and the following disclaimer. |
| 23 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 24 |
* notice, this list of conditions and the following disclaimer in |
| 25 |
* the documentation and/or other materials provided with the |
| 26 |
* distribution. |
| 27 |
* 3. Neither the name of Guichan nor the names of its contributors may |
| 28 |
* be used to endorse or promote products derived from this software |
| 29 |
* without specific prior written permission. |
| 30 |
* |
| 31 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 32 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 33 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 34 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 35 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 36 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 37 |
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 38 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 39 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 40 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 41 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 42 |
*/ |
| 43 |
|
| 44 |
/* |
| 45 |
* For comments regarding functions please see the header file. |
| 46 |
*/ |
| 47 |
|
| 48 |
#include "guichan/openlayer/openlayergraphics.hpp" |
| 49 |
|
| 50 |
#include <OpenLayer.hpp> |
| 51 |
|
| 52 |
#include <string> |
| 53 |
|
| 54 |
#include "guichan/exception.hpp" |
| 55 |
#include "guichan/openlayer/openlayerimage.hpp" |
| 56 |
|
| 57 |
namespace gcn |
| 58 |
{ |
| 59 |
OpenLayerGraphics::OpenLayerGraphics() |
| 60 |
{ |
| 61 |
setTargetPlane(640, 480); |
| 62 |
} |
| 63 |
|
| 64 |
OpenLayerGraphics::OpenLayerGraphics(int width, int height) |
| 65 |
{ |
| 66 |
setTargetPlane(width, height); |
| 67 |
} |
| 68 |
|
| 69 |
OpenLayerGraphics::~OpenLayerGraphics() |
| 70 |
{ |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
void OpenLayerGraphics::_beginDraw() |
| 75 |
{ |
| 76 |
pushClipArea(Rectangle(0, 0, mWidth, mHeight)); |
| 77 |
} |
| 78 |
|
| 79 |
void OpenLayerGraphics::_endDraw() |
| 80 |
{ |
| 81 |
popClipArea(); |
| 82 |
} |
| 83 |
|
| 84 |
bool OpenLayerGraphics::pushClipArea(Rectangle area) |
| 85 |
{ |
| 86 |
bool result = Graphics::pushClipArea(area); |
| 87 |
|
| 88 |
ol::Transforms::SetPosition(mClipStack.top().xOffset, |
| 89 |
mClipStack.top().yOffset); |
| 90 |
|
| 91 |
ol::Canvas::SetClipping(mClipStack.top().x, |
| 92 |
mClipStack.top().y, |
| 93 |
mClipStack.top().width, |
| 94 |
mClipStack.top().height); |
| 95 |
|
| 96 |
return result; |
| 97 |
} |
| 98 |
|
| 99 |
void OpenLayerGraphics::popClipArea() |
| 100 |
{ |
| 101 |
Graphics::popClipArea(); |
| 102 |
|
| 103 |
if (mClipStack.empty()) |
| 104 |
{ |
| 105 |
ol::Transforms::SetPosition(0, 0); |
| 106 |
ol::Canvas::DisableClipping(); |
| 107 |
} |
| 108 |
else |
| 109 |
{ |
| 110 |
const ClipRectangle top = mClipStack.top(); |
| 111 |
ol::Transforms::SetPosition(top.xOffset, |
| 112 |
top.yOffset); |
| 113 |
ol::Canvas::SetClipping(top.x, |
| 114 |
top.y, |
| 115 |
top.width, |
| 116 |
top.height); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
void OpenLayerGraphics::setTargetPlane(int width, int height) |
| 121 |
{ |
| 122 |
mWidth = width; |
| 123 |
mHeight = height; |
| 124 |
} |
| 125 |
|
| 126 |
void OpenLayerGraphics::drawImage(const Image* image, |
| 127 |
int srcX, |
| 128 |
int srcY, |
| 129 |
int dstX, |
| 130 |
int dstY, |
| 131 |
int width, |
| 132 |
int height) |
| 133 |
{ |
| 134 |
const OpenLayerImage* srcImage = dynamic_cast<const OpenLayerImage*>(image); |
| 135 |
|
| 136 |
if (srcImage == NULL) |
| 137 |
{ |
| 138 |
throw GCN_EXCEPTION("Trying to draw an image of unknown format, must be an OpenLayerImage."); |
| 139 |
} |
| 140 |
|
| 141 |
srcImage->getBitmap()->Blit(dstX - srcX, |
| 142 |
dstY - srcY, |
| 143 |
ol::ClippedMode(srcX, |
| 144 |
srcY, |
| 145 |
width, |
| 146 |
height), |
| 147 |
1.0f); |
| 148 |
} |
| 149 |
|
| 150 |
void OpenLayerGraphics::drawPoint(int x, int y) |
| 151 |
{ |
| 152 |
ol::GfxRend::Point(x + 0.5f, y - 0.5f, mRgba); |
| 153 |
} |
| 154 |
|
| 155 |
void OpenLayerGraphics::drawLine(int x1, int y1, int x2, int y2) |
| 156 |
{ |
| 157 |
mRgba.Select(); |
| 158 |
glDisable(GL_TEXTURE_2D); |
| 159 |
glLineWidth(1.0f); |
| 160 |
|
| 161 |
glBegin(GL_LINES); |
| 162 |
glVertex2f(x1+0.5f, y1+0.5f); |
| 163 |
glVertex2f(x2+0.5f, y2+0.5f); |
| 164 |
glEnd(); |
| 165 |
|
| 166 |
glBegin(GL_POINTS); |
| 167 |
glVertex2f(x1+0.5f, y1+0.5f); |
| 168 |
glEnd(); |
| 169 |
|
| 170 |
glBegin(GL_POINTS); |
| 171 |
glVertex2f(x2+0.5f, y2+0.5f); |
| 172 |
glEnd(); |
| 173 |
|
| 174 |
if (ol::Settings::TextureMappingUsed()) |
| 175 |
{ |
| 176 |
glEnable(GL_TEXTURE_2D); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
void OpenLayerGraphics::drawRectangle(const Rectangle& rectangle) |
| 181 |
{ |
| 182 |
ol::GfxRend::RectOutline(rectangle.x + 0.5f, |
| 183 |
rectangle.y + 0.5f, |
| 184 |
rectangle.width - 0.5f, |
| 185 |
rectangle.height - 0.5f, |
| 186 |
mRgba); |
| 187 |
} |
| 188 |
|
| 189 |
void OpenLayerGraphics::fillRectangle(const Rectangle& rectangle) |
| 190 |
{ |
| 191 |
ol::GfxRend::Rect(rectangle.x, |
| 192 |
rectangle.y, |
| 193 |
rectangle.width, |
| 194 |
rectangle.height, |
| 195 |
mRgba); |
| 196 |
} |
| 197 |
|
| 198 |
void OpenLayerGraphics::setColor(const Color& color) |
| 199 |
{ |
| 200 |
mColor = color; |
| 201 |
mRgba.r = color.r / 255.0f; |
| 202 |
mRgba.g = color.g / 255.0f; |
| 203 |
mRgba.b = color.b / 255.0f; |
| 204 |
mRgba.a = color.a / 255.0f; |
| 205 |
mRgba.Select(); |
| 206 |
} |
| 207 |
|
| 208 |
const Color& OpenLayerGraphics::getColor() const |
| 209 |
{ |
| 210 |
return mColor; |
| 211 |
} |
| 212 |
|
| 213 |
const ol::Rgba& OpenLayerGraphics::getOpenLayerColor() const |
| 214 |
{ |
| 215 |
return mRgba; |
| 216 |
} |
| 217 |
} |